blob: e2749755f58c128950f7118561c958d366c03e04 [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"
10
11#ifndef CONFIG_SMARTCARD
12#ifndef OPENSSL_NO_ENGINE
Kenny Rootdb3c5a42012-03-20 17:00:47 -070013#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#define OPENSSL_NO_ENGINE
15#endif
16#endif
Kenny Rootdb3c5a42012-03-20 17:00:47 -070017#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018
19#include <openssl/ssl.h>
20#include <openssl/err.h>
Dmitry Shmidt849734c2016-05-27 09:59:01 -070021#include <openssl/opensslv.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include <openssl/pkcs12.h>
23#include <openssl/x509v3.h>
24#ifndef OPENSSL_NO_ENGINE
25#include <openssl/engine.h>
26#endif /* OPENSSL_NO_ENGINE */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080027#ifndef OPENSSL_NO_DSA
28#include <openssl/dsa.h>
29#endif
30#ifndef OPENSSL_NO_DH
31#include <openssl/dh.h>
32#endif
33
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "common.h"
35#include "crypto.h"
Dmitry Shmidtaf9da312015-04-03 10:03:11 -070036#include "sha1.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080037#include "sha256.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038#include "tls.h"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080039#include "tls_openssl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
Dmitry Shmidt849734c2016-05-27 09:59:01 -070041#if !defined(CONFIG_FIPS) && \
42 (defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || \
43 defined(EAP_SERVER_FAST))
44#define OPENSSL_NEED_EAP_FAST_PRF
45#endif
46
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -070047#if defined(OPENSSL_IS_BORINGSSL)
48/* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */
49typedef size_t stack_index_t;
50#else
51typedef int stack_index_t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052#endif
53
Dmitry Shmidt34af3062013-07-11 10:46:32 -070054#ifdef SSL_set_tlsext_status_type
55#ifndef OPENSSL_NO_TLSEXT
56#define HAVE_OCSP
57#include <openssl/ocsp.h>
58#endif /* OPENSSL_NO_TLSEXT */
59#endif /* SSL_set_tlsext_status_type */
60
Dmitry Shmidt849734c2016-05-27 09:59:01 -070061#if (OPENSSL_VERSION_NUMBER < 0x10100000L || \
62 defined(LIBRESSL_VERSION_NUMBER)) && \
63 !defined(BORINGSSL_API_VERSION)
Dmitry Shmidtde47be72016-01-07 12:52:55 -080064/*
65 * SSL_get_client_random() and SSL_get_server_random() were added in OpenSSL
Dmitry Shmidt849734c2016-05-27 09:59:01 -070066 * 1.1.0 and newer BoringSSL revisions. Provide compatibility wrappers for
67 * older versions.
Dmitry Shmidtde47be72016-01-07 12:52:55 -080068 */
69
70static size_t SSL_get_client_random(const SSL *ssl, unsigned char *out,
71 size_t outlen)
72{
73 if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE)
74 return 0;
75 os_memcpy(out, ssl->s3->client_random, SSL3_RANDOM_SIZE);
76 return SSL3_RANDOM_SIZE;
77}
78
79
80static size_t SSL_get_server_random(const SSL *ssl, unsigned char *out,
81 size_t outlen)
82{
83 if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE)
84 return 0;
85 os_memcpy(out, ssl->s3->server_random, SSL3_RANDOM_SIZE);
86 return SSL3_RANDOM_SIZE;
87}
88
89
Dmitry Shmidt849734c2016-05-27 09:59:01 -070090#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtde47be72016-01-07 12:52:55 -080091static size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
92 unsigned char *out, size_t outlen)
93{
94 if (!session || session->master_key_length < 0 ||
95 (size_t) session->master_key_length > outlen)
96 return 0;
97 if ((size_t) session->master_key_length < outlen)
98 outlen = session->master_key_length;
99 os_memcpy(out, session->master_key, outlen);
100 return outlen;
101}
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700102#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800103
104#endif
105
Dmitry Shmidtff079172013-11-08 14:10:30 -0800106#ifdef ANDROID
107#include <openssl/pem.h>
108#include <keystore/keystore_get.h>
109
110static BIO * BIO_from_keystore(const char *key)
111{
112 BIO *bio = NULL;
113 uint8_t *value = NULL;
114 int length = keystore_get(key, strlen(key), &value);
115 if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL)
116 BIO_write(bio, value, length);
117 free(value);
118 return bio;
119}
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800120
121
122static int tls_add_ca_from_keystore(X509_STORE *ctx, const char *key_alias)
123{
124 BIO *bio = BIO_from_keystore(key_alias);
125 STACK_OF(X509_INFO) *stack = NULL;
126 stack_index_t i;
127
128 if (bio) {
129 stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
130 BIO_free(bio);
131 }
132
133 if (!stack) {
134 wpa_printf(MSG_WARNING, "TLS: Failed to parse certificate: %s",
135 key_alias);
136 return -1;
137 }
138
139 for (i = 0; i < sk_X509_INFO_num(stack); ++i) {
140 X509_INFO *info = sk_X509_INFO_value(stack, i);
141
142 if (info->x509)
143 X509_STORE_add_cert(ctx, info->x509);
144 if (info->crl)
145 X509_STORE_add_crl(ctx, info->crl);
146 }
147
148 sk_X509_INFO_pop_free(stack, X509_INFO_free);
149
150 return 0;
151}
152
153
154static int tls_add_ca_from_keystore_encoded(X509_STORE *ctx,
155 const char *encoded_key_alias)
156{
157 int rc = -1;
158 int len = os_strlen(encoded_key_alias);
159 unsigned char *decoded_alias;
160
161 if (len & 1) {
162 wpa_printf(MSG_WARNING, "Invalid hex-encoded alias: %s",
163 encoded_key_alias);
164 return rc;
165 }
166
167 decoded_alias = os_malloc(len / 2 + 1);
168 if (decoded_alias) {
169 if (!hexstr2bin(encoded_key_alias, decoded_alias, len / 2)) {
170 decoded_alias[len / 2] = '\0';
171 rc = tls_add_ca_from_keystore(
172 ctx, (const char *) decoded_alias);
173 }
174 os_free(decoded_alias);
175 }
176
177 return rc;
178}
179
Dmitry Shmidtff079172013-11-08 14:10:30 -0800180#endif /* ANDROID */
181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182static int tls_openssl_ref_count = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800183static int tls_ex_idx_session = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700185struct tls_context {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700186 void (*event_cb)(void *ctx, enum tls_event ev,
187 union tls_event_data *data);
188 void *cb_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800189 int cert_in_cb;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700190 char *ocsp_stapling_response;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191};
192
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700193static struct tls_context *tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194
195
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800196struct tls_data {
197 SSL_CTX *ssl;
198 unsigned int tls_session_lifetime;
199};
200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201struct tls_connection {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700202 struct tls_context *context;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800203 SSL_CTX *ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204 SSL *ssl;
205 BIO *ssl_in, *ssl_out;
Adam Langley1eb02ed2015-04-21 19:00:05 -0700206#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207 ENGINE *engine; /* functional reference to the engine */
208 EVP_PKEY *private_key; /* the private key if using engine */
209#endif /* OPENSSL_NO_ENGINE */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800210 char *subject_match, *altsubject_match, *suffix_match, *domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 int read_alerts, write_alerts, failed;
212
213 tls_session_ticket_cb session_ticket_cb;
214 void *session_ticket_cb_ctx;
215
216 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
217 u8 *session_ticket;
218 size_t session_ticket_len;
219
220 unsigned int ca_cert_verify:1;
221 unsigned int cert_probe:1;
222 unsigned int server_cert_only:1;
Jouni Malinen26af48b2014-04-09 13:02:53 +0300223 unsigned int invalid_hb_used:1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800224 unsigned int success_data:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225
226 u8 srv_cert_hash[32];
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700227
228 unsigned int flags;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700229
230 X509 *peer_cert;
231 X509 *peer_issuer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800232 X509 *peer_issuer_issuer;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800233
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800234 unsigned char client_random[SSL3_RANDOM_SIZE];
235 unsigned char server_random[SSL3_RANDOM_SIZE];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236};
237
238
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700239static struct tls_context * tls_context_new(const struct tls_config *conf)
240{
241 struct tls_context *context = os_zalloc(sizeof(*context));
242 if (context == NULL)
243 return NULL;
244 if (conf) {
245 context->event_cb = conf->event_cb;
246 context->cb_ctx = conf->cb_ctx;
247 context->cert_in_cb = conf->cert_in_cb;
248 }
249 return context;
250}
251
252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700253#ifdef CONFIG_NO_STDOUT_DEBUG
254
255static void _tls_show_errors(void)
256{
257 unsigned long err;
258
259 while ((err = ERR_get_error())) {
260 /* Just ignore the errors, since stdout is disabled */
261 }
262}
263#define tls_show_errors(l, f, t) _tls_show_errors()
264
265#else /* CONFIG_NO_STDOUT_DEBUG */
266
267static void tls_show_errors(int level, const char *func, const char *txt)
268{
269 unsigned long err;
270
271 wpa_printf(level, "OpenSSL: %s - %s %s",
272 func, txt, ERR_error_string(ERR_get_error(), NULL));
273
274 while ((err = ERR_get_error())) {
275 wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
276 ERR_error_string(err, NULL));
277 }
278}
279
280#endif /* CONFIG_NO_STDOUT_DEBUG */
281
282
283#ifdef CONFIG_NATIVE_WINDOWS
284
285/* Windows CryptoAPI and access to certificate stores */
286#include <wincrypt.h>
287
288#ifdef __MINGW32_VERSION
289/*
290 * MinGW does not yet include all the needed definitions for CryptoAPI, so
291 * define here whatever extra is needed.
292 */
293#define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
294#define CERT_STORE_READONLY_FLAG 0x00008000
295#define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
296
297#endif /* __MINGW32_VERSION */
298
299
300struct cryptoapi_rsa_data {
301 const CERT_CONTEXT *cert;
302 HCRYPTPROV crypt_prov;
303 DWORD key_spec;
304 BOOL free_crypt_prov;
305};
306
307
308static void cryptoapi_error(const char *msg)
309{
310 wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u",
311 msg, (unsigned int) GetLastError());
312}
313
314
315static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from,
316 unsigned char *to, RSA *rsa, int padding)
317{
318 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
319 return 0;
320}
321
322
323static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from,
324 unsigned char *to, RSA *rsa, int padding)
325{
326 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
327 return 0;
328}
329
330
331static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from,
332 unsigned char *to, RSA *rsa, int padding)
333{
334 struct cryptoapi_rsa_data *priv =
335 (struct cryptoapi_rsa_data *) rsa->meth->app_data;
336 HCRYPTHASH hash;
337 DWORD hash_size, len, i;
338 unsigned char *buf = NULL;
339 int ret = 0;
340
341 if (priv == NULL) {
342 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
343 ERR_R_PASSED_NULL_PARAMETER);
344 return 0;
345 }
346
347 if (padding != RSA_PKCS1_PADDING) {
348 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
349 RSA_R_UNKNOWN_PADDING_TYPE);
350 return 0;
351 }
352
353 if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) {
354 wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported",
355 __func__);
356 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
357 RSA_R_INVALID_MESSAGE_LENGTH);
358 return 0;
359 }
360
361 if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash))
362 {
363 cryptoapi_error("CryptCreateHash failed");
364 return 0;
365 }
366
367 len = sizeof(hash_size);
368 if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len,
369 0)) {
370 cryptoapi_error("CryptGetHashParam failed");
371 goto err;
372 }
373
374 if ((int) hash_size != flen) {
375 wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)",
376 (unsigned) hash_size, flen);
377 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
378 RSA_R_INVALID_MESSAGE_LENGTH);
379 goto err;
380 }
381 if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) {
382 cryptoapi_error("CryptSetHashParam failed");
383 goto err;
384 }
385
386 len = RSA_size(rsa);
387 buf = os_malloc(len);
388 if (buf == NULL) {
389 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
390 goto err;
391 }
392
393 if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) {
394 cryptoapi_error("CryptSignHash failed");
395 goto err;
396 }
397
398 for (i = 0; i < len; i++)
399 to[i] = buf[len - i - 1];
400 ret = len;
401
402err:
403 os_free(buf);
404 CryptDestroyHash(hash);
405
406 return ret;
407}
408
409
410static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from,
411 unsigned char *to, RSA *rsa, int padding)
412{
413 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
414 return 0;
415}
416
417
418static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv)
419{
420 if (priv == NULL)
421 return;
422 if (priv->crypt_prov && priv->free_crypt_prov)
423 CryptReleaseContext(priv->crypt_prov, 0);
424 if (priv->cert)
425 CertFreeCertificateContext(priv->cert);
426 os_free(priv);
427}
428
429
430static int cryptoapi_finish(RSA *rsa)
431{
432 cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data);
433 os_free((void *) rsa->meth);
434 rsa->meth = NULL;
435 return 1;
436}
437
438
439static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store)
440{
441 HCERTSTORE cs;
442 const CERT_CONTEXT *ret = NULL;
443
444 cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0,
445 store | CERT_STORE_OPEN_EXISTING_FLAG |
446 CERT_STORE_READONLY_FLAG, L"MY");
447 if (cs == NULL) {
448 cryptoapi_error("Failed to open 'My system store'");
449 return NULL;
450 }
451
452 if (strncmp(name, "cert://", 7) == 0) {
453 unsigned short wbuf[255];
454 MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255);
455 ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING |
456 PKCS_7_ASN_ENCODING,
457 0, CERT_FIND_SUBJECT_STR,
458 wbuf, NULL);
459 } else if (strncmp(name, "hash://", 7) == 0) {
460 CRYPT_HASH_BLOB blob;
461 int len;
462 const char *hash = name + 7;
463 unsigned char *buf;
464
465 len = os_strlen(hash) / 2;
466 buf = os_malloc(len);
467 if (buf && hexstr2bin(hash, buf, len) == 0) {
468 blob.cbData = len;
469 blob.pbData = buf;
470 ret = CertFindCertificateInStore(cs,
471 X509_ASN_ENCODING |
472 PKCS_7_ASN_ENCODING,
473 0, CERT_FIND_HASH,
474 &blob, NULL);
475 }
476 os_free(buf);
477 }
478
479 CertCloseStore(cs, 0);
480
481 return ret;
482}
483
484
485static int tls_cryptoapi_cert(SSL *ssl, const char *name)
486{
487 X509 *cert = NULL;
488 RSA *rsa = NULL, *pub_rsa;
489 struct cryptoapi_rsa_data *priv;
490 RSA_METHOD *rsa_meth;
491
492 if (name == NULL ||
493 (strncmp(name, "cert://", 7) != 0 &&
494 strncmp(name, "hash://", 7) != 0))
495 return -1;
496
497 priv = os_zalloc(sizeof(*priv));
498 rsa_meth = os_zalloc(sizeof(*rsa_meth));
499 if (priv == NULL || rsa_meth == NULL) {
500 wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory "
501 "for CryptoAPI RSA method");
502 os_free(priv);
503 os_free(rsa_meth);
504 return -1;
505 }
506
507 priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER);
508 if (priv->cert == NULL) {
509 priv->cert = cryptoapi_find_cert(
510 name, CERT_SYSTEM_STORE_LOCAL_MACHINE);
511 }
512 if (priv->cert == NULL) {
513 wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate "
514 "'%s'", name);
515 goto err;
516 }
517
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800518 cert = d2i_X509(NULL,
519 (const unsigned char **) &priv->cert->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700520 priv->cert->cbCertEncoded);
521 if (cert == NULL) {
522 wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER "
523 "encoding");
524 goto err;
525 }
526
527 if (!CryptAcquireCertificatePrivateKey(priv->cert,
528 CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
529 NULL, &priv->crypt_prov,
530 &priv->key_spec,
531 &priv->free_crypt_prov)) {
532 cryptoapi_error("Failed to acquire a private key for the "
533 "certificate");
534 goto err;
535 }
536
537 rsa_meth->name = "Microsoft CryptoAPI RSA Method";
538 rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc;
539 rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec;
540 rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc;
541 rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec;
542 rsa_meth->finish = cryptoapi_finish;
543 rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK;
544 rsa_meth->app_data = (char *) priv;
545
546 rsa = RSA_new();
547 if (rsa == NULL) {
548 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,
549 ERR_R_MALLOC_FAILURE);
550 goto err;
551 }
552
553 if (!SSL_use_certificate(ssl, cert)) {
554 RSA_free(rsa);
555 rsa = NULL;
556 goto err;
557 }
558 pub_rsa = cert->cert_info->key->pkey->pkey.rsa;
559 X509_free(cert);
560 cert = NULL;
561
562 rsa->n = BN_dup(pub_rsa->n);
563 rsa->e = BN_dup(pub_rsa->e);
564 if (!RSA_set_method(rsa, rsa_meth))
565 goto err;
566
567 if (!SSL_use_RSAPrivateKey(ssl, rsa))
568 goto err;
569 RSA_free(rsa);
570
571 return 0;
572
573err:
574 if (cert)
575 X509_free(cert);
576 if (rsa)
577 RSA_free(rsa);
578 else {
579 os_free(rsa_meth);
580 cryptoapi_free_data(priv);
581 }
582 return -1;
583}
584
585
586static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name)
587{
588 HCERTSTORE cs;
589 PCCERT_CONTEXT ctx = NULL;
590 X509 *cert;
591 char buf[128];
592 const char *store;
593#ifdef UNICODE
594 WCHAR *wstore;
595#endif /* UNICODE */
596
597 if (name == NULL || strncmp(name, "cert_store://", 13) != 0)
598 return -1;
599
600 store = name + 13;
601#ifdef UNICODE
602 wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR));
603 if (wstore == NULL)
604 return -1;
605 wsprintf(wstore, L"%S", store);
606 cs = CertOpenSystemStore(0, wstore);
607 os_free(wstore);
608#else /* UNICODE */
609 cs = CertOpenSystemStore(0, store);
610#endif /* UNICODE */
611 if (cs == NULL) {
612 wpa_printf(MSG_DEBUG, "%s: failed to open system cert store "
613 "'%s': error=%d", __func__, store,
614 (int) GetLastError());
615 return -1;
616 }
617
618 while ((ctx = CertEnumCertificatesInStore(cs, ctx))) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800619 cert = d2i_X509(NULL,
620 (const unsigned char **) &ctx->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621 ctx->cbCertEncoded);
622 if (cert == NULL) {
623 wpa_printf(MSG_INFO, "CryptoAPI: Could not process "
624 "X509 DER encoding for CA cert");
625 continue;
626 }
627
628 X509_NAME_oneline(X509_get_subject_name(cert), buf,
629 sizeof(buf));
630 wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for "
631 "system certificate store: subject='%s'", buf);
632
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700633 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
634 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635 tls_show_errors(MSG_WARNING, __func__,
636 "Failed to add ca_cert to OpenSSL "
637 "certificate store");
638 }
639
640 X509_free(cert);
641 }
642
643 if (!CertCloseStore(cs, 0)) {
644 wpa_printf(MSG_DEBUG, "%s: failed to close system cert store "
645 "'%s': error=%d", __func__, name + 13,
646 (int) GetLastError());
647 }
648
649 return 0;
650}
651
652
653#else /* CONFIG_NATIVE_WINDOWS */
654
655static int tls_cryptoapi_cert(SSL *ssl, const char *name)
656{
657 return -1;
658}
659
660#endif /* CONFIG_NATIVE_WINDOWS */
661
662
663static void ssl_info_cb(const SSL *ssl, int where, int ret)
664{
665 const char *str;
666 int w;
667
668 wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret);
669 w = where & ~SSL_ST_MASK;
670 if (w & SSL_ST_CONNECT)
671 str = "SSL_connect";
672 else if (w & SSL_ST_ACCEPT)
673 str = "SSL_accept";
674 else
675 str = "undefined";
676
677 if (where & SSL_CB_LOOP) {
678 wpa_printf(MSG_DEBUG, "SSL: %s:%s",
679 str, SSL_state_string_long(ssl));
680 } else if (where & SSL_CB_ALERT) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700681 struct tls_connection *conn = SSL_get_app_data((SSL *) ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682 wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s",
683 where & SSL_CB_READ ?
684 "read (remote end reported an error)" :
685 "write (local SSL3 detected an error)",
686 SSL_alert_type_string_long(ret),
687 SSL_alert_desc_string_long(ret));
688 if ((ret >> 8) == SSL3_AL_FATAL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700689 if (where & SSL_CB_READ)
690 conn->read_alerts++;
691 else
692 conn->write_alerts++;
693 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700694 if (conn->context->event_cb != NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700695 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700696 struct tls_context *context = conn->context;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700697 os_memset(&ev, 0, sizeof(ev));
698 ev.alert.is_local = !(where & SSL_CB_READ);
699 ev.alert.type = SSL_alert_type_string_long(ret);
700 ev.alert.description = SSL_alert_desc_string_long(ret);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700701 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700702 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703 } else if (where & SSL_CB_EXIT && ret <= 0) {
704 wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s",
705 str, ret == 0 ? "failed" : "error",
706 SSL_state_string_long(ssl));
707 }
708}
709
710
711#ifndef OPENSSL_NO_ENGINE
712/**
713 * tls_engine_load_dynamic_generic - load any openssl engine
714 * @pre: an array of commands and values that load an engine initialized
715 * in the engine specific function
716 * @post: an array of commands and values that initialize an already loaded
717 * engine (or %NULL if not required)
718 * @id: the engine id of the engine to load (only required if post is not %NULL
719 *
720 * This function is a generic function that loads any openssl engine.
721 *
722 * Returns: 0 on success, -1 on failure
723 */
724static int tls_engine_load_dynamic_generic(const char *pre[],
725 const char *post[], const char *id)
726{
727 ENGINE *engine;
728 const char *dynamic_id = "dynamic";
729
730 engine = ENGINE_by_id(id);
731 if (engine) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732 wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
733 "available", id);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700734 /*
735 * If it was auto-loaded by ENGINE_by_id() we might still
736 * need to tell it which PKCS#11 module to use in legacy
737 * (non-p11-kit) environments. Do so now; even if it was
738 * properly initialised before, setting it again will be
739 * harmless.
740 */
741 goto found;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742 }
743 ERR_clear_error();
744
745 engine = ENGINE_by_id(dynamic_id);
746 if (engine == NULL) {
747 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
748 dynamic_id,
749 ERR_error_string(ERR_get_error(), NULL));
750 return -1;
751 }
752
753 /* Perform the pre commands. This will load the engine. */
754 while (pre && pre[0]) {
755 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
756 if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
757 wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
758 "%s %s [%s]", pre[0], pre[1],
759 ERR_error_string(ERR_get_error(), NULL));
760 ENGINE_free(engine);
761 return -1;
762 }
763 pre += 2;
764 }
765
766 /*
767 * Free the reference to the "dynamic" engine. The loaded engine can
768 * now be looked up using ENGINE_by_id().
769 */
770 ENGINE_free(engine);
771
772 engine = ENGINE_by_id(id);
773 if (engine == NULL) {
774 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
775 id, ERR_error_string(ERR_get_error(), NULL));
776 return -1;
777 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700778 found:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779 while (post && post[0]) {
780 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
781 if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
782 wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
783 " %s %s [%s]", post[0], post[1],
784 ERR_error_string(ERR_get_error(), NULL));
785 ENGINE_remove(engine);
786 ENGINE_free(engine);
787 return -1;
788 }
789 post += 2;
790 }
791 ENGINE_free(engine);
792
793 return 0;
794}
795
796
797/**
798 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
799 * @pkcs11_so_path: pksc11_so_path from the configuration
800 * @pcks11_module_path: pkcs11_module_path from the configuration
801 */
802static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
803 const char *pkcs11_module_path)
804{
805 char *engine_id = "pkcs11";
806 const char *pre_cmd[] = {
807 "SO_PATH", NULL /* pkcs11_so_path */,
808 "ID", NULL /* engine_id */,
809 "LIST_ADD", "1",
810 /* "NO_VCHECK", "1", */
811 "LOAD", NULL,
812 NULL, NULL
813 };
814 const char *post_cmd[] = {
815 "MODULE_PATH", NULL /* pkcs11_module_path */,
816 NULL, NULL
817 };
818
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800819 if (!pkcs11_so_path)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820 return 0;
821
822 pre_cmd[1] = pkcs11_so_path;
823 pre_cmd[3] = engine_id;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800824 if (pkcs11_module_path)
825 post_cmd[1] = pkcs11_module_path;
826 else
827 post_cmd[0] = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828
829 wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
830 pkcs11_so_path);
831
832 return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
833}
834
835
836/**
837 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
838 * @opensc_so_path: opensc_so_path from the configuration
839 */
840static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
841{
842 char *engine_id = "opensc";
843 const char *pre_cmd[] = {
844 "SO_PATH", NULL /* opensc_so_path */,
845 "ID", NULL /* engine_id */,
846 "LIST_ADD", "1",
847 "LOAD", NULL,
848 NULL, NULL
849 };
850
851 if (!opensc_so_path)
852 return 0;
853
854 pre_cmd[1] = opensc_so_path;
855 pre_cmd[3] = engine_id;
856
857 wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
858 opensc_so_path);
859
860 return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
861}
862#endif /* OPENSSL_NO_ENGINE */
863
864
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800865static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
866{
867 struct wpabuf *buf;
868
869 if (tls_ex_idx_session < 0)
870 return;
871 buf = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
872 if (!buf)
873 return;
874 wpa_printf(MSG_DEBUG,
875 "OpenSSL: Free application session data %p (sess %p)",
876 buf, sess);
877 wpabuf_free(buf);
878
879 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL);
880}
881
882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883void * tls_init(const struct tls_config *conf)
884{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800885 struct tls_data *data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 SSL_CTX *ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700887 struct tls_context *context;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800888 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889
890 if (tls_openssl_ref_count == 0) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700891 tls_global = context = tls_context_new(conf);
892 if (context == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894#ifdef CONFIG_FIPS
895#ifdef OPENSSL_FIPS
896 if (conf && conf->fips_mode) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800897 static int fips_enabled = 0;
898
899 if (!fips_enabled && !FIPS_mode_set(1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 wpa_printf(MSG_ERROR, "Failed to enable FIPS "
901 "mode");
902 ERR_load_crypto_strings();
903 ERR_print_errors_fp(stderr);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700904 os_free(tls_global);
905 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800907 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 wpa_printf(MSG_INFO, "Running in FIPS mode");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800909 fips_enabled = 1;
910 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 }
912#else /* OPENSSL_FIPS */
913 if (conf && conf->fips_mode) {
914 wpa_printf(MSG_ERROR, "FIPS mode requested, but not "
915 "supported");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700916 os_free(tls_global);
917 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918 return NULL;
919 }
920#endif /* OPENSSL_FIPS */
921#endif /* CONFIG_FIPS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800922#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923 SSL_load_error_strings();
924 SSL_library_init();
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800925#ifndef OPENSSL_NO_SHA256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 EVP_add_digest(EVP_sha256());
927#endif /* OPENSSL_NO_SHA256 */
928 /* TODO: if /dev/urandom is available, PRNG is seeded
929 * automatically. If this is not the case, random data should
930 * be added here. */
931
932#ifdef PKCS12_FUNCS
933#ifndef OPENSSL_NO_RC2
934 /*
935 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
936 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
937 * versions, but it looks like OpenSSL 1.0.0 does not do that
938 * anymore.
939 */
940 EVP_add_cipher(EVP_rc2_40_cbc());
941#endif /* OPENSSL_NO_RC2 */
942 PKCS12_PBE_add();
943#endif /* PKCS12_FUNCS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800944#endif /* < 1.1.0 */
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700945 } else {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700946 context = tls_context_new(conf);
947 if (context == NULL)
948 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949 }
950 tls_openssl_ref_count++;
951
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800952 data = os_zalloc(sizeof(*data));
953 if (data)
954 ssl = SSL_CTX_new(SSLv23_method());
955 else
956 ssl = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700957 if (ssl == NULL) {
958 tls_openssl_ref_count--;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700959 if (context != tls_global)
960 os_free(context);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700961 if (tls_openssl_ref_count == 0) {
962 os_free(tls_global);
963 tls_global = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700964 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800965 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966 return NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700967 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800968 data->ssl = ssl;
969 if (conf)
970 data->tls_session_lifetime = conf->tls_session_lifetime;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800972 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv2);
973 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv3);
974
Dmitry Shmidt29333592017-01-09 12:27:11 -0800975#ifdef SSL_MODE_NO_AUTO_CHAIN
976 /* Number of deployed use cases assume the default OpenSSL behavior of
977 * auto chaining the local certificate is in use. BoringSSL removed this
978 * functionality by default, so we need to restore it here to avoid
979 * breaking existing use cases. */
980 SSL_CTX_clear_mode(ssl, SSL_MODE_NO_AUTO_CHAIN);
981#endif /* SSL_MODE_NO_AUTO_CHAIN */
982
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 SSL_CTX_set_info_callback(ssl, ssl_info_cb);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700984 SSL_CTX_set_app_data(ssl, context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800985 if (data->tls_session_lifetime > 0) {
986 SSL_CTX_set_quiet_shutdown(ssl, 1);
987 /*
988 * Set default context here. In practice, this will be replaced
989 * by the per-EAP method context in tls_connection_set_verify().
990 */
991 SSL_CTX_set_session_id_context(ssl, (u8 *) "hostapd", 7);
992 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_SERVER);
993 SSL_CTX_set_timeout(ssl, data->tls_session_lifetime);
994 SSL_CTX_sess_set_remove_cb(ssl, remove_session_cb);
995 } else {
996 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_OFF);
997 }
998
999 if (tls_ex_idx_session < 0) {
1000 tls_ex_idx_session = SSL_SESSION_get_ex_new_index(
1001 0, NULL, NULL, NULL, NULL);
1002 if (tls_ex_idx_session < 0) {
1003 tls_deinit(data);
1004 return NULL;
1005 }
1006 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007
1008#ifndef OPENSSL_NO_ENGINE
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001009 wpa_printf(MSG_DEBUG, "ENGINE: Loading dynamic engine");
1010 ERR_load_ENGINE_strings();
1011 ENGINE_load_dynamic();
1012
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013 if (conf &&
1014 (conf->opensc_engine_path || conf->pkcs11_engine_path ||
1015 conf->pkcs11_module_path)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001016 if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
1017 tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
1018 conf->pkcs11_module_path)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001019 tls_deinit(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020 return NULL;
1021 }
1022 }
1023#endif /* OPENSSL_NO_ENGINE */
1024
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001025 if (conf && conf->openssl_ciphers)
1026 ciphers = conf->openssl_ciphers;
1027 else
1028 ciphers = "DEFAULT:!EXP:!LOW";
1029 if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) {
1030 wpa_printf(MSG_ERROR,
1031 "OpenSSL: Failed to set cipher string '%s'",
1032 ciphers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001033 tls_deinit(data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001034 return NULL;
1035 }
1036
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001037 return data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038}
1039
1040
1041void tls_deinit(void *ssl_ctx)
1042{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001043 struct tls_data *data = ssl_ctx;
1044 SSL_CTX *ssl = data->ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001045 struct tls_context *context = SSL_CTX_get_app_data(ssl);
1046 if (context != tls_global)
1047 os_free(context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001048 if (data->tls_session_lifetime > 0)
1049 SSL_CTX_flush_sessions(ssl, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 SSL_CTX_free(ssl);
1051
1052 tls_openssl_ref_count--;
1053 if (tls_openssl_ref_count == 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001054#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055#ifndef OPENSSL_NO_ENGINE
1056 ENGINE_cleanup();
1057#endif /* OPENSSL_NO_ENGINE */
1058 CRYPTO_cleanup_all_ex_data();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001059 ERR_remove_thread_state(NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 ERR_free_strings();
1061 EVP_cleanup();
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001062#endif /* < 1.1.0 */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001063 os_free(tls_global->ocsp_stapling_response);
1064 tls_global->ocsp_stapling_response = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065 os_free(tls_global);
1066 tls_global = NULL;
1067 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001068
1069 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070}
1071
1072
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001073#ifndef OPENSSL_NO_ENGINE
1074
1075/* Cryptoki return values */
1076#define CKR_PIN_INCORRECT 0x000000a0
1077#define CKR_PIN_INVALID 0x000000a1
1078#define CKR_PIN_LEN_RANGE 0x000000a2
1079
1080/* libp11 */
1081#define ERR_LIB_PKCS11 ERR_LIB_USER
1082
1083static int tls_is_pin_error(unsigned int err)
1084{
1085 return ERR_GET_LIB(err) == ERR_LIB_PKCS11 &&
1086 (ERR_GET_REASON(err) == CKR_PIN_INCORRECT ||
1087 ERR_GET_REASON(err) == CKR_PIN_INVALID ||
1088 ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE);
1089}
1090
1091#endif /* OPENSSL_NO_ENGINE */
1092
1093
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001094#ifdef ANDROID
1095/* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */
1096EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id);
1097#endif /* ANDROID */
1098
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
1100 const char *pin, const char *key_id,
1101 const char *cert_id, const char *ca_cert_id)
1102{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001103#if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL)
1104#if !defined(OPENSSL_NO_ENGINE)
1105#error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
1106#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001107 if (!key_id)
1108 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Adam Langley1eb02ed2015-04-21 19:00:05 -07001109 conn->engine = NULL;
1110 conn->private_key = EVP_PKEY_from_keystore(key_id);
1111 if (!conn->private_key) {
1112 wpa_printf(MSG_ERROR,
1113 "ENGINE: cannot load private key with id '%s' [%s]",
1114 key_id,
1115 ERR_error_string(ERR_get_error(), NULL));
1116 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1117 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001118#endif /* ANDROID && OPENSSL_IS_BORINGSSL */
Adam Langley1eb02ed2015-04-21 19:00:05 -07001119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001120#ifndef OPENSSL_NO_ENGINE
1121 int ret = -1;
1122 if (engine_id == NULL) {
1123 wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
1124 return -1;
1125 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126
1127 ERR_clear_error();
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001128#ifdef ANDROID
1129 ENGINE_load_dynamic();
1130#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001131 conn->engine = ENGINE_by_id(engine_id);
1132 if (!conn->engine) {
1133 wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
1134 engine_id, ERR_error_string(ERR_get_error(), NULL));
1135 goto err;
1136 }
1137 if (ENGINE_init(conn->engine) != 1) {
1138 wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
1139 "(engine: %s) [%s]", engine_id,
1140 ERR_error_string(ERR_get_error(), NULL));
1141 goto err;
1142 }
1143 wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
1144
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001145#ifndef ANDROID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001146 if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
1148 ERR_error_string(ERR_get_error(), NULL));
1149 goto err;
1150 }
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001151#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001152 if (key_id) {
1153 /*
1154 * Ensure that the ENGINE does not attempt to use the OpenSSL
1155 * UI system to obtain a PIN, if we didn't provide one.
1156 */
1157 struct {
1158 const void *password;
1159 const char *prompt_info;
1160 } key_cb = { "", NULL };
1161
1162 /* load private key first in-case PIN is required for cert */
1163 conn->private_key = ENGINE_load_private_key(conn->engine,
1164 key_id, NULL,
1165 &key_cb);
1166 if (!conn->private_key) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001167 unsigned long err = ERR_get_error();
1168
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001169 wpa_printf(MSG_ERROR,
1170 "ENGINE: cannot load private key with id '%s' [%s]",
1171 key_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001172 ERR_error_string(err, NULL));
1173 if (tls_is_pin_error(err))
1174 ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
1175 else
1176 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001177 goto err;
1178 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 }
1180
1181 /* handle a certificate and/or CA certificate */
1182 if (cert_id || ca_cert_id) {
1183 const char *cmd_name = "LOAD_CERT_CTRL";
1184
1185 /* test if the engine supports a LOAD_CERT_CTRL */
1186 if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
1187 0, (void *)cmd_name, NULL)) {
1188 wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
1189 " loading certificates");
1190 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1191 goto err;
1192 }
1193 }
1194
1195 return 0;
1196
1197err:
1198 if (conn->engine) {
1199 ENGINE_free(conn->engine);
1200 conn->engine = NULL;
1201 }
1202
1203 if (conn->private_key) {
1204 EVP_PKEY_free(conn->private_key);
1205 conn->private_key = NULL;
1206 }
1207
1208 return ret;
1209#else /* OPENSSL_NO_ENGINE */
1210 return 0;
1211#endif /* OPENSSL_NO_ENGINE */
1212}
1213
1214
1215static void tls_engine_deinit(struct tls_connection *conn)
1216{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001217#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
1219 if (conn->private_key) {
1220 EVP_PKEY_free(conn->private_key);
1221 conn->private_key = NULL;
1222 }
1223 if (conn->engine) {
Adam Langley1eb02ed2015-04-21 19:00:05 -07001224#if !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001225 ENGINE_finish(conn->engine);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001226#endif /* !OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 conn->engine = NULL;
1228 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001229#endif /* ANDROID || !OPENSSL_NO_ENGINE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001230}
1231
1232
1233int tls_get_errors(void *ssl_ctx)
1234{
1235 int count = 0;
1236 unsigned long err;
1237
1238 while ((err = ERR_get_error())) {
1239 wpa_printf(MSG_INFO, "TLS - SSL error: %s",
1240 ERR_error_string(err, NULL));
1241 count++;
1242 }
1243
1244 return count;
1245}
1246
Jouni Malinen26af48b2014-04-09 13:02:53 +03001247
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001248static const char * openssl_content_type(int content_type)
1249{
1250 switch (content_type) {
1251 case 20:
1252 return "change cipher spec";
1253 case 21:
1254 return "alert";
1255 case 22:
1256 return "handshake";
1257 case 23:
1258 return "application data";
1259 case 24:
1260 return "heartbeat";
1261 case 256:
1262 return "TLS header info"; /* pseudo content type */
1263 default:
1264 return "?";
1265 }
1266}
1267
1268
1269static const char * openssl_handshake_type(int content_type, const u8 *buf,
1270 size_t len)
1271{
1272 if (content_type != 22 || !buf || len == 0)
1273 return "";
1274 switch (buf[0]) {
1275 case 0:
1276 return "hello request";
1277 case 1:
1278 return "client hello";
1279 case 2:
1280 return "server hello";
1281 case 4:
1282 return "new session ticket";
1283 case 11:
1284 return "certificate";
1285 case 12:
1286 return "server key exchange";
1287 case 13:
1288 return "certificate request";
1289 case 14:
1290 return "server hello done";
1291 case 15:
1292 return "certificate verify";
1293 case 16:
1294 return "client key exchange";
1295 case 20:
1296 return "finished";
1297 case 21:
1298 return "certificate url";
1299 case 22:
1300 return "certificate status";
1301 default:
1302 return "?";
1303 }
1304}
1305
1306
Jouni Malinen26af48b2014-04-09 13:02:53 +03001307static void tls_msg_cb(int write_p, int version, int content_type,
1308 const void *buf, size_t len, SSL *ssl, void *arg)
1309{
1310 struct tls_connection *conn = arg;
1311 const u8 *pos = buf;
1312
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001313 if (write_p == 2) {
1314 wpa_printf(MSG_DEBUG,
1315 "OpenSSL: session ver=0x%x content_type=%d",
1316 version, content_type);
1317 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len);
1318 return;
1319 }
1320
1321 wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
1322 write_p ? "TX" : "RX", version, content_type,
1323 openssl_content_type(content_type),
1324 openssl_handshake_type(content_type, buf, len));
Jouni Malinen26af48b2014-04-09 13:02:53 +03001325 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len);
1326 if (content_type == 24 && len >= 3 && pos[0] == 1) {
1327 size_t payload_len = WPA_GET_BE16(pos + 1);
1328 if (payload_len + 3 > len) {
1329 wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected");
1330 conn->invalid_hb_used = 1;
1331 }
1332 }
1333}
1334
1335
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001336struct tls_connection * tls_connection_init(void *ssl_ctx)
1337{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001338 struct tls_data *data = ssl_ctx;
1339 SSL_CTX *ssl = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340 struct tls_connection *conn;
1341 long options;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001342 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001343
1344 conn = os_zalloc(sizeof(*conn));
1345 if (conn == NULL)
1346 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001347 conn->ssl_ctx = ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001348 conn->ssl = SSL_new(ssl);
1349 if (conn->ssl == NULL) {
1350 tls_show_errors(MSG_INFO, __func__,
1351 "Failed to initialize new SSL connection");
1352 os_free(conn);
1353 return NULL;
1354 }
1355
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001356 conn->context = context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357 SSL_set_app_data(conn->ssl, conn);
Jouni Malinen26af48b2014-04-09 13:02:53 +03001358 SSL_set_msg_callback(conn->ssl, tls_msg_cb);
1359 SSL_set_msg_callback_arg(conn->ssl, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360 options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
1361 SSL_OP_SINGLE_DH_USE;
1362#ifdef SSL_OP_NO_COMPRESSION
1363 options |= SSL_OP_NO_COMPRESSION;
1364#endif /* SSL_OP_NO_COMPRESSION */
1365 SSL_set_options(conn->ssl, options);
1366
1367 conn->ssl_in = BIO_new(BIO_s_mem());
1368 if (!conn->ssl_in) {
1369 tls_show_errors(MSG_INFO, __func__,
1370 "Failed to create a new BIO for ssl_in");
1371 SSL_free(conn->ssl);
1372 os_free(conn);
1373 return NULL;
1374 }
1375
1376 conn->ssl_out = BIO_new(BIO_s_mem());
1377 if (!conn->ssl_out) {
1378 tls_show_errors(MSG_INFO, __func__,
1379 "Failed to create a new BIO for ssl_out");
1380 SSL_free(conn->ssl);
1381 BIO_free(conn->ssl_in);
1382 os_free(conn);
1383 return NULL;
1384 }
1385
1386 SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
1387
1388 return conn;
1389}
1390
1391
1392void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
1393{
1394 if (conn == NULL)
1395 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001396 if (conn->success_data) {
1397 /*
1398 * Make sure ssl_clear_bad_session() does not remove this
1399 * session.
1400 */
1401 SSL_set_quiet_shutdown(conn->ssl, 1);
1402 SSL_shutdown(conn->ssl);
1403 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404 SSL_free(conn->ssl);
1405 tls_engine_deinit(conn);
1406 os_free(conn->subject_match);
1407 os_free(conn->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001408 os_free(conn->suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001409 os_free(conn->domain_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410 os_free(conn->session_ticket);
1411 os_free(conn);
1412}
1413
1414
1415int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
1416{
1417 return conn ? SSL_is_init_finished(conn->ssl) : 0;
1418}
1419
1420
1421int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
1422{
1423 if (conn == NULL)
1424 return -1;
1425
1426 /* Shutdown previous TLS connection without notifying the peer
1427 * because the connection was already terminated in practice
1428 * and "close notify" shutdown alert would confuse AS. */
1429 SSL_set_quiet_shutdown(conn->ssl, 1);
1430 SSL_shutdown(conn->ssl);
Jouni Malinenf291c682015-08-17 22:50:41 +03001431 return SSL_clear(conn->ssl) == 1 ? 0 : -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432}
1433
1434
1435static int tls_match_altsubject_component(X509 *cert, int type,
1436 const char *value, size_t len)
1437{
1438 GENERAL_NAME *gen;
1439 void *ext;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001440 int found = 0;
1441 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442
1443 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1444
1445 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1446 gen = sk_GENERAL_NAME_value(ext, i);
1447 if (gen->type != type)
1448 continue;
1449 if (os_strlen((char *) gen->d.ia5->data) == len &&
1450 os_memcmp(value, gen->d.ia5->data, len) == 0)
1451 found++;
1452 }
1453
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001454 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
1455
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456 return found;
1457}
1458
1459
1460static int tls_match_altsubject(X509 *cert, const char *match)
1461{
1462 int type;
1463 const char *pos, *end;
1464 size_t len;
1465
1466 pos = match;
1467 do {
1468 if (os_strncmp(pos, "EMAIL:", 6) == 0) {
1469 type = GEN_EMAIL;
1470 pos += 6;
1471 } else if (os_strncmp(pos, "DNS:", 4) == 0) {
1472 type = GEN_DNS;
1473 pos += 4;
1474 } else if (os_strncmp(pos, "URI:", 4) == 0) {
1475 type = GEN_URI;
1476 pos += 4;
1477 } else {
1478 wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
1479 "match '%s'", pos);
1480 return 0;
1481 }
1482 end = os_strchr(pos, ';');
1483 while (end) {
1484 if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
1485 os_strncmp(end + 1, "DNS:", 4) == 0 ||
1486 os_strncmp(end + 1, "URI:", 4) == 0)
1487 break;
1488 end = os_strchr(end + 1, ';');
1489 }
1490 if (end)
1491 len = end - pos;
1492 else
1493 len = os_strlen(pos);
1494 if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1495 return 1;
1496 pos = end + 1;
1497 } while (end);
1498
1499 return 0;
1500}
1501
1502
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001503#ifndef CONFIG_NATIVE_WINDOWS
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001504static int domain_suffix_match(const u8 *val, size_t len, const char *match,
1505 int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001506{
1507 size_t i, match_len;
1508
1509 /* Check for embedded nuls that could mess up suffix matching */
1510 for (i = 0; i < len; i++) {
1511 if (val[i] == '\0') {
1512 wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject");
1513 return 0;
1514 }
1515 }
1516
1517 match_len = os_strlen(match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001518 if (match_len > len || (full && match_len != len))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001519 return 0;
1520
1521 if (os_strncasecmp((const char *) val + len - match_len, match,
1522 match_len) != 0)
1523 return 0; /* no match */
1524
1525 if (match_len == len)
1526 return 1; /* exact match */
1527
1528 if (val[len - match_len - 1] == '.')
1529 return 1; /* full label match completes suffix match */
1530
1531 wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match");
1532 return 0;
1533}
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001534#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001535
1536
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001537static int tls_match_suffix(X509 *cert, const char *match, int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001538{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001539#ifdef CONFIG_NATIVE_WINDOWS
1540 /* wincrypt.h has conflicting X509_NAME definition */
1541 return -1;
1542#else /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001543 GENERAL_NAME *gen;
1544 void *ext;
1545 int i;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001546 stack_index_t j;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001547 int dns_name = 0;
1548 X509_NAME *name;
1549
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001550 wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s",
1551 full ? "": "suffix ", match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001552
1553 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1554
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001555 for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) {
1556 gen = sk_GENERAL_NAME_value(ext, j);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001557 if (gen->type != GEN_DNS)
1558 continue;
1559 dns_name++;
1560 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName",
1561 gen->d.dNSName->data,
1562 gen->d.dNSName->length);
1563 if (domain_suffix_match(gen->d.dNSName->data,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001564 gen->d.dNSName->length, match, full) ==
1565 1) {
1566 wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found",
1567 full ? "Match" : "Suffix match");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001568 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001569 return 1;
1570 }
1571 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001572 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001573
1574 if (dns_name) {
1575 wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched");
1576 return 0;
1577 }
1578
1579 name = X509_get_subject_name(cert);
1580 i = -1;
1581 for (;;) {
1582 X509_NAME_ENTRY *e;
1583 ASN1_STRING *cn;
1584
1585 i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
1586 if (i == -1)
1587 break;
1588 e = X509_NAME_get_entry(name, i);
1589 if (e == NULL)
1590 continue;
1591 cn = X509_NAME_ENTRY_get_data(e);
1592 if (cn == NULL)
1593 continue;
1594 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName",
1595 cn->data, cn->length);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001596 if (domain_suffix_match(cn->data, cn->length, match, full) == 1)
1597 {
1598 wpa_printf(MSG_DEBUG, "TLS: %s in commonName found",
1599 full ? "Match" : "Suffix match");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001600 return 1;
1601 }
1602 }
1603
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001604 wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found",
1605 full ? "": "suffix ");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001606 return 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001607#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001608}
1609
1610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611static enum tls_fail_reason openssl_tls_fail_reason(int err)
1612{
1613 switch (err) {
1614 case X509_V_ERR_CERT_REVOKED:
1615 return TLS_FAIL_REVOKED;
1616 case X509_V_ERR_CERT_NOT_YET_VALID:
1617 case X509_V_ERR_CRL_NOT_YET_VALID:
1618 return TLS_FAIL_NOT_YET_VALID;
1619 case X509_V_ERR_CERT_HAS_EXPIRED:
1620 case X509_V_ERR_CRL_HAS_EXPIRED:
1621 return TLS_FAIL_EXPIRED;
1622 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
1623 case X509_V_ERR_UNABLE_TO_GET_CRL:
1624 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
1625 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1626 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1627 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1628 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1629 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
1630 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
1631 case X509_V_ERR_INVALID_CA:
1632 return TLS_FAIL_UNTRUSTED;
1633 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
1634 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
1635 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
1636 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
1637 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
1638 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
1639 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
1640 case X509_V_ERR_CERT_UNTRUSTED:
1641 case X509_V_ERR_CERT_REJECTED:
1642 return TLS_FAIL_BAD_CERTIFICATE;
1643 default:
1644 return TLS_FAIL_UNSPECIFIED;
1645 }
1646}
1647
1648
1649static struct wpabuf * get_x509_cert(X509 *cert)
1650{
1651 struct wpabuf *buf;
1652 u8 *tmp;
1653
1654 int cert_len = i2d_X509(cert, NULL);
1655 if (cert_len <= 0)
1656 return NULL;
1657
1658 buf = wpabuf_alloc(cert_len);
1659 if (buf == NULL)
1660 return NULL;
1661
1662 tmp = wpabuf_put(buf, cert_len);
1663 i2d_X509(cert, &tmp);
1664 return buf;
1665}
1666
1667
1668static void openssl_tls_fail_event(struct tls_connection *conn,
1669 X509 *err_cert, int err, int depth,
1670 const char *subject, const char *err_str,
1671 enum tls_fail_reason reason)
1672{
1673 union tls_event_data ev;
1674 struct wpabuf *cert = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001675 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001676
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001677 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678 return;
1679
1680 cert = get_x509_cert(err_cert);
1681 os_memset(&ev, 0, sizeof(ev));
1682 ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ?
1683 reason : openssl_tls_fail_reason(err);
1684 ev.cert_fail.depth = depth;
1685 ev.cert_fail.subject = subject;
1686 ev.cert_fail.reason_txt = err_str;
1687 ev.cert_fail.cert = cert;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001688 context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001689 wpabuf_free(cert);
1690}
1691
1692
1693static void openssl_tls_cert_event(struct tls_connection *conn,
1694 X509 *err_cert, int depth,
1695 const char *subject)
1696{
1697 struct wpabuf *cert = NULL;
1698 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001699 struct tls_context *context = conn->context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001700 char *altsubject[TLS_MAX_ALT_SUBJECT];
1701 int alt, num_altsubject = 0;
1702 GENERAL_NAME *gen;
1703 void *ext;
1704 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001705#ifdef CONFIG_SHA256
1706 u8 hash[32];
1707#endif /* CONFIG_SHA256 */
1708
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001709 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001710 return;
1711
1712 os_memset(&ev, 0, sizeof(ev));
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001713 if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) ||
1714 context->cert_in_cb) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001715 cert = get_x509_cert(err_cert);
1716 ev.peer_cert.cert = cert;
1717 }
1718#ifdef CONFIG_SHA256
1719 if (cert) {
1720 const u8 *addr[1];
1721 size_t len[1];
1722 addr[0] = wpabuf_head(cert);
1723 len[0] = wpabuf_len(cert);
1724 if (sha256_vector(1, addr, len, hash) == 0) {
1725 ev.peer_cert.hash = hash;
1726 ev.peer_cert.hash_len = sizeof(hash);
1727 }
1728 }
1729#endif /* CONFIG_SHA256 */
1730 ev.peer_cert.depth = depth;
1731 ev.peer_cert.subject = subject;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001732
1733 ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL);
1734 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1735 char *pos;
1736
1737 if (num_altsubject == TLS_MAX_ALT_SUBJECT)
1738 break;
1739 gen = sk_GENERAL_NAME_value(ext, i);
1740 if (gen->type != GEN_EMAIL &&
1741 gen->type != GEN_DNS &&
1742 gen->type != GEN_URI)
1743 continue;
1744
1745 pos = os_malloc(10 + gen->d.ia5->length + 1);
1746 if (pos == NULL)
1747 break;
1748 altsubject[num_altsubject++] = pos;
1749
1750 switch (gen->type) {
1751 case GEN_EMAIL:
1752 os_memcpy(pos, "EMAIL:", 6);
1753 pos += 6;
1754 break;
1755 case GEN_DNS:
1756 os_memcpy(pos, "DNS:", 4);
1757 pos += 4;
1758 break;
1759 case GEN_URI:
1760 os_memcpy(pos, "URI:", 4);
1761 pos += 4;
1762 break;
1763 }
1764
1765 os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length);
1766 pos += gen->d.ia5->length;
1767 *pos = '\0';
1768 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001769 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001770
1771 for (alt = 0; alt < num_altsubject; alt++)
1772 ev.peer_cert.altsubject[alt] = altsubject[alt];
1773 ev.peer_cert.num_altsubject = num_altsubject;
1774
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001775 context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001776 wpabuf_free(cert);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001777 for (alt = 0; alt < num_altsubject; alt++)
1778 os_free(altsubject[alt]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001779}
1780
1781
1782static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
1783{
1784 char buf[256];
1785 X509 *err_cert;
1786 int err, depth;
1787 SSL *ssl;
1788 struct tls_connection *conn;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001789 struct tls_context *context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001790 char *match, *altmatch, *suffix_match, *domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791 const char *err_str;
1792
1793 err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001794 if (!err_cert)
1795 return 0;
1796
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001797 err = X509_STORE_CTX_get_error(x509_ctx);
1798 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
1799 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1800 SSL_get_ex_data_X509_STORE_CTX_idx());
1801 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
1802
1803 conn = SSL_get_app_data(ssl);
1804 if (conn == NULL)
1805 return 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001806
1807 if (depth == 0)
1808 conn->peer_cert = err_cert;
1809 else if (depth == 1)
1810 conn->peer_issuer = err_cert;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001811 else if (depth == 2)
1812 conn->peer_issuer_issuer = err_cert;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001813
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001814 context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001815 match = conn->subject_match;
1816 altmatch = conn->altsubject_match;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001817 suffix_match = conn->suffix_match;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001818 domain_match = conn->domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819
1820 if (!preverify_ok && !conn->ca_cert_verify)
1821 preverify_ok = 1;
1822 if (!preverify_ok && depth > 0 && conn->server_cert_only)
1823 preverify_ok = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001824 if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) &&
1825 (err == X509_V_ERR_CERT_HAS_EXPIRED ||
1826 err == X509_V_ERR_CERT_NOT_YET_VALID)) {
1827 wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity "
1828 "time mismatch");
1829 preverify_ok = 1;
1830 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831
1832 err_str = X509_verify_cert_error_string(err);
1833
1834#ifdef CONFIG_SHA256
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001835 /*
1836 * Do not require preverify_ok so we can explicity allow otherwise
1837 * invalid pinned server certificates.
1838 */
1839 if (depth == 0 && conn->server_cert_only) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840 struct wpabuf *cert;
1841 cert = get_x509_cert(err_cert);
1842 if (!cert) {
1843 wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch "
1844 "server certificate data");
1845 preverify_ok = 0;
1846 } else {
1847 u8 hash[32];
1848 const u8 *addr[1];
1849 size_t len[1];
1850 addr[0] = wpabuf_head(cert);
1851 len[0] = wpabuf_len(cert);
1852 if (sha256_vector(1, addr, len, hash) < 0 ||
1853 os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
1854 err_str = "Server certificate mismatch";
1855 err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
1856 preverify_ok = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001857 } else if (!preverify_ok) {
1858 /*
1859 * Certificate matches pinned certificate, allow
1860 * regardless of other problems.
1861 */
1862 wpa_printf(MSG_DEBUG,
1863 "OpenSSL: Ignore validation issues for a pinned server certificate");
1864 preverify_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865 }
1866 wpabuf_free(cert);
1867 }
1868 }
1869#endif /* CONFIG_SHA256 */
1870
1871 if (!preverify_ok) {
1872 wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
1873 " error %d (%s) depth %d for '%s'", err, err_str,
1874 depth, buf);
1875 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1876 err_str, TLS_FAIL_UNSPECIFIED);
1877 return preverify_ok;
1878 }
1879
1880 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d "
1881 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
1882 preverify_ok, err, err_str,
1883 conn->ca_cert_verify, depth, buf);
1884 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
1885 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
1886 "match with '%s'", buf, match);
1887 preverify_ok = 0;
1888 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1889 "Subject mismatch",
1890 TLS_FAIL_SUBJECT_MISMATCH);
1891 } else if (depth == 0 && altmatch &&
1892 !tls_match_altsubject(err_cert, altmatch)) {
1893 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
1894 "'%s' not found", altmatch);
1895 preverify_ok = 0;
1896 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1897 "AltSubject mismatch",
1898 TLS_FAIL_ALTSUBJECT_MISMATCH);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001899 } else if (depth == 0 && suffix_match &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001900 !tls_match_suffix(err_cert, suffix_match, 0)) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001901 wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found",
1902 suffix_match);
1903 preverify_ok = 0;
1904 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1905 "Domain suffix mismatch",
1906 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001907 } else if (depth == 0 && domain_match &&
1908 !tls_match_suffix(err_cert, domain_match, 1)) {
1909 wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found",
1910 domain_match);
1911 preverify_ok = 0;
1912 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1913 "Domain mismatch",
1914 TLS_FAIL_DOMAIN_MISMATCH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001915 } else
1916 openssl_tls_cert_event(conn, err_cert, depth, buf);
1917
1918 if (conn->cert_probe && preverify_ok && depth == 0) {
1919 wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate "
1920 "on probe-only run");
1921 preverify_ok = 0;
1922 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1923 "Server certificate chain probe",
1924 TLS_FAIL_SERVER_CHAIN_PROBE);
1925 }
1926
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001927#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001928 if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) &&
1929 preverify_ok) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001930 enum ocsp_result res;
1931
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001932 res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert,
1933 conn->peer_issuer,
1934 conn->peer_issuer_issuer);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001935 if (res == OCSP_REVOKED) {
1936 preverify_ok = 0;
1937 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1938 "certificate revoked",
1939 TLS_FAIL_REVOKED);
1940 if (err == X509_V_OK)
1941 X509_STORE_CTX_set_error(
1942 x509_ctx, X509_V_ERR_CERT_REVOKED);
1943 } else if (res != OCSP_GOOD &&
1944 (conn->flags & TLS_CONN_REQUIRE_OCSP)) {
1945 preverify_ok = 0;
1946 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1947 "bad certificate status response",
1948 TLS_FAIL_UNSPECIFIED);
1949 }
1950 }
1951#endif /* OPENSSL_IS_BORINGSSL */
1952
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001953 if (depth == 0 && preverify_ok && context->event_cb != NULL)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001954 context->event_cb(context->cb_ctx,
1955 TLS_CERT_CHAIN_SUCCESS, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001956
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 return preverify_ok;
1958}
1959
1960
1961#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001962static int tls_load_ca_der(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001963{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001964 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965 X509_LOOKUP *lookup;
1966 int ret = 0;
1967
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001968 lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969 X509_LOOKUP_file());
1970 if (lookup == NULL) {
1971 tls_show_errors(MSG_WARNING, __func__,
1972 "Failed add lookup for X509 store");
1973 return -1;
1974 }
1975
1976 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
1977 unsigned long err = ERR_peek_error();
1978 tls_show_errors(MSG_WARNING, __func__,
1979 "Failed load CA in DER format");
1980 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
1981 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1982 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
1983 "cert already in hash table error",
1984 __func__);
1985 } else
1986 ret = -1;
1987 }
1988
1989 return ret;
1990}
1991#endif /* OPENSSL_NO_STDIO */
1992
1993
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001994static int tls_connection_ca_cert(struct tls_data *data,
1995 struct tls_connection *conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001996 const char *ca_cert, const u8 *ca_cert_blob,
1997 size_t ca_cert_blob_len, const char *ca_path)
1998{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001999 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002000 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002001
2002 /*
2003 * Remove previously configured trusted CA certificates before adding
2004 * new ones.
2005 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002006 store = X509_STORE_new();
2007 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002008 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
2009 "certificate store", __func__);
2010 return -1;
2011 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002012 SSL_CTX_set_cert_store(ssl_ctx, store);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013
2014 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2015 conn->ca_cert_verify = 1;
2016
2017 if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
2018 wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate "
2019 "chain");
2020 conn->cert_probe = 1;
2021 conn->ca_cert_verify = 0;
2022 return 0;
2023 }
2024
2025 if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
2026#ifdef CONFIG_SHA256
2027 const char *pos = ca_cert + 7;
2028 if (os_strncmp(pos, "server/sha256/", 14) != 0) {
2029 wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert "
2030 "hash value '%s'", ca_cert);
2031 return -1;
2032 }
2033 pos += 14;
2034 if (os_strlen(pos) != 32 * 2) {
2035 wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 "
2036 "hash length in ca_cert '%s'", ca_cert);
2037 return -1;
2038 }
2039 if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
2040 wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash "
2041 "value in ca_cert '%s'", ca_cert);
2042 return -1;
2043 }
2044 conn->server_cert_only = 1;
2045 wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server "
2046 "certificate match");
2047 return 0;
2048#else /* CONFIG_SHA256 */
2049 wpa_printf(MSG_INFO, "No SHA256 included in the build - "
2050 "cannot validate server certificate hash");
2051 return -1;
2052#endif /* CONFIG_SHA256 */
2053 }
2054
2055 if (ca_cert_blob) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002056 X509 *cert = d2i_X509(NULL,
2057 (const unsigned char **) &ca_cert_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002058 ca_cert_blob_len);
2059 if (cert == NULL) {
2060 tls_show_errors(MSG_WARNING, __func__,
2061 "Failed to parse ca_cert_blob");
2062 return -1;
2063 }
2064
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002065 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
2066 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002067 unsigned long err = ERR_peek_error();
2068 tls_show_errors(MSG_WARNING, __func__,
2069 "Failed to add ca_cert_blob to "
2070 "certificate store");
2071 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2072 ERR_GET_REASON(err) ==
2073 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2074 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2075 "cert already in hash table error",
2076 __func__);
2077 } else {
2078 X509_free(cert);
2079 return -1;
2080 }
2081 }
2082 X509_free(cert);
2083 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
2084 "to certificate store", __func__);
2085 return 0;
2086 }
2087
2088#ifdef ANDROID
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002089 /* Single alias */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090 if (ca_cert && os_strncmp("keystore://", ca_cert, 11) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002091 if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002092 &ca_cert[11]) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002093 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002094 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2095 return 0;
2096 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002098 /* Multiple aliases separated by space */
2099 if (ca_cert && os_strncmp("keystores://", ca_cert, 12) == 0) {
2100 char *aliases = os_strdup(&ca_cert[12]);
2101 const char *delim = " ";
2102 int rc = 0;
2103 char *savedptr;
2104 char *alias;
2105
2106 if (!aliases)
2107 return -1;
2108 alias = strtok_r(aliases, delim, &savedptr);
2109 for (; alias; alias = strtok_r(NULL, delim, &savedptr)) {
2110 if (tls_add_ca_from_keystore_encoded(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002111 SSL_CTX_get_cert_store(ssl_ctx), alias)) {
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002112 wpa_printf(MSG_WARNING,
2113 "OpenSSL: %s - Failed to add ca_cert %s from keystore",
2114 __func__, alias);
2115 rc = -1;
2116 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002117 }
2118 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002119 os_free(aliases);
2120 if (rc)
2121 return rc;
2122
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002123 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2124 return 0;
2125 }
2126#endif /* ANDROID */
2127
2128#ifdef CONFIG_NATIVE_WINDOWS
2129 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
2130 0) {
2131 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
2132 "system certificate store");
2133 return 0;
2134 }
2135#endif /* CONFIG_NATIVE_WINDOWS */
2136
2137 if (ca_cert || ca_path) {
2138#ifndef OPENSSL_NO_STDIO
2139 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
2140 1) {
2141 tls_show_errors(MSG_WARNING, __func__,
2142 "Failed to load root certificates");
2143 if (ca_cert &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002144 tls_load_ca_der(data, ca_cert) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
2146 "DER format CA certificate",
2147 __func__);
2148 } else
2149 return -1;
2150 } else {
2151 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2152 "certificate(s) loaded");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002153 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154 }
2155#else /* OPENSSL_NO_STDIO */
2156 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2157 __func__);
2158 return -1;
2159#endif /* OPENSSL_NO_STDIO */
2160 } else {
2161 /* No ca_cert configured - do not try to verify server
2162 * certificate */
2163 conn->ca_cert_verify = 0;
2164 }
2165
2166 return 0;
2167}
2168
2169
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002170static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002171{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002172 SSL_CTX *ssl_ctx = data->ssl;
2173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002174 if (ca_cert) {
2175 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
2176 {
2177 tls_show_errors(MSG_WARNING, __func__,
2178 "Failed to load root certificates");
2179 return -1;
2180 }
2181
2182 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2183 "certificate(s) loaded");
2184
2185#ifndef OPENSSL_NO_STDIO
2186 /* Add the same CAs to the client certificate requests */
2187 SSL_CTX_set_client_CA_list(ssl_ctx,
2188 SSL_load_client_CA_file(ca_cert));
2189#endif /* OPENSSL_NO_STDIO */
2190 }
2191
2192 return 0;
2193}
2194
2195
2196int tls_global_set_verify(void *ssl_ctx, int check_crl)
2197{
2198 int flags;
2199
2200 if (check_crl) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002201 struct tls_data *data = ssl_ctx;
2202 X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002203 if (cs == NULL) {
2204 tls_show_errors(MSG_INFO, __func__, "Failed to get "
2205 "certificate store when enabling "
2206 "check_crl");
2207 return -1;
2208 }
2209 flags = X509_V_FLAG_CRL_CHECK;
2210 if (check_crl == 2)
2211 flags |= X509_V_FLAG_CRL_CHECK_ALL;
2212 X509_STORE_set_flags(cs, flags);
2213 }
2214 return 0;
2215}
2216
2217
2218static int tls_connection_set_subject_match(struct tls_connection *conn,
2219 const char *subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07002220 const char *altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002221 const char *suffix_match,
2222 const char *domain_match)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223{
2224 os_free(conn->subject_match);
2225 conn->subject_match = NULL;
2226 if (subject_match) {
2227 conn->subject_match = os_strdup(subject_match);
2228 if (conn->subject_match == NULL)
2229 return -1;
2230 }
2231
2232 os_free(conn->altsubject_match);
2233 conn->altsubject_match = NULL;
2234 if (altsubject_match) {
2235 conn->altsubject_match = os_strdup(altsubject_match);
2236 if (conn->altsubject_match == NULL)
2237 return -1;
2238 }
2239
Dmitry Shmidt051af732013-10-22 13:52:46 -07002240 os_free(conn->suffix_match);
2241 conn->suffix_match = NULL;
2242 if (suffix_match) {
2243 conn->suffix_match = os_strdup(suffix_match);
2244 if (conn->suffix_match == NULL)
2245 return -1;
2246 }
2247
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002248 os_free(conn->domain_match);
2249 conn->domain_match = NULL;
2250 if (domain_match) {
2251 conn->domain_match = os_strdup(domain_match);
2252 if (conn->domain_match == NULL)
2253 return -1;
2254 }
2255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 return 0;
2257}
2258
2259
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002260static void tls_set_conn_flags(SSL *ssl, unsigned int flags)
2261{
2262#ifdef SSL_OP_NO_TICKET
2263 if (flags & TLS_CONN_DISABLE_SESSION_TICKET)
2264 SSL_set_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002265 else
2266 SSL_clear_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002267#endif /* SSL_OP_NO_TICKET */
2268
2269#ifdef SSL_OP_NO_TLSv1
2270 if (flags & TLS_CONN_DISABLE_TLSv1_0)
2271 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
2272 else
2273 SSL_clear_options(ssl, SSL_OP_NO_TLSv1);
2274#endif /* SSL_OP_NO_TLSv1 */
2275#ifdef SSL_OP_NO_TLSv1_1
2276 if (flags & TLS_CONN_DISABLE_TLSv1_1)
2277 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
2278 else
2279 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1);
2280#endif /* SSL_OP_NO_TLSv1_1 */
2281#ifdef SSL_OP_NO_TLSv1_2
2282 if (flags & TLS_CONN_DISABLE_TLSv1_2)
2283 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
2284 else
2285 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2);
2286#endif /* SSL_OP_NO_TLSv1_2 */
2287}
2288
2289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002290int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002291 int verify_peer, unsigned int flags,
2292 const u8 *session_ctx, size_t session_ctx_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293{
2294 static int counter = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002295 struct tls_data *data = ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002296
2297 if (conn == NULL)
2298 return -1;
2299
2300 if (verify_peer) {
2301 conn->ca_cert_verify = 1;
2302 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
2303 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
2304 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
2305 } else {
2306 conn->ca_cert_verify = 0;
2307 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
2308 }
2309
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002310 tls_set_conn_flags(conn->ssl, flags);
2311 conn->flags = flags;
2312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002313 SSL_set_accept_state(conn->ssl);
2314
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002315 if (data->tls_session_lifetime == 0) {
2316 /*
2317 * Set session id context to a unique value to make sure
2318 * session resumption cannot be used either through session
2319 * caching or TLS ticket extension.
2320 */
2321 counter++;
2322 SSL_set_session_id_context(conn->ssl,
2323 (const unsigned char *) &counter,
2324 sizeof(counter));
2325 } else if (session_ctx) {
2326 SSL_set_session_id_context(conn->ssl, session_ctx,
2327 session_ctx_len);
2328 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329
2330 return 0;
2331}
2332
2333
2334static int tls_connection_client_cert(struct tls_connection *conn,
2335 const char *client_cert,
2336 const u8 *client_cert_blob,
2337 size_t client_cert_blob_len)
2338{
2339 if (client_cert == NULL && client_cert_blob == NULL)
2340 return 0;
2341
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002342#ifdef PKCS12_FUNCS
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002343#if OPENSSL_VERSION_NUMBER < 0x10002000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002344 /*
2345 * Clear previously set extra chain certificates, if any, from PKCS#12
2346 * processing in tls_parse_pkcs12() to allow OpenSSL to build a new
2347 * chain properly.
2348 */
2349 SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx);
2350#endif /* OPENSSL_VERSION_NUMBER < 0x10002000L */
2351#endif /* PKCS12_FUNCS */
2352
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353 if (client_cert_blob &&
2354 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
2355 client_cert_blob_len) == 1) {
2356 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
2357 "OK");
2358 return 0;
2359 } else if (client_cert_blob) {
2360 tls_show_errors(MSG_DEBUG, __func__,
2361 "SSL_use_certificate_ASN1 failed");
2362 }
2363
2364 if (client_cert == NULL)
2365 return -1;
2366
2367#ifdef ANDROID
2368 if (os_strncmp("keystore://", client_cert, 11) == 0) {
2369 BIO *bio = BIO_from_keystore(&client_cert[11]);
2370 X509 *x509 = NULL;
2371 int ret = -1;
2372 if (bio) {
2373 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
2374 BIO_free(bio);
2375 }
2376 if (x509) {
2377 if (SSL_use_certificate(conn->ssl, x509) == 1)
2378 ret = 0;
2379 X509_free(x509);
2380 }
2381 return ret;
2382 }
2383#endif /* ANDROID */
2384
2385#ifndef OPENSSL_NO_STDIO
2386 if (SSL_use_certificate_file(conn->ssl, client_cert,
2387 SSL_FILETYPE_ASN1) == 1) {
2388 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
2389 " --> OK");
2390 return 0;
2391 }
2392
2393 if (SSL_use_certificate_file(conn->ssl, client_cert,
2394 SSL_FILETYPE_PEM) == 1) {
2395 ERR_clear_error();
2396 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
2397 " --> OK");
2398 return 0;
2399 }
2400
2401 tls_show_errors(MSG_DEBUG, __func__,
2402 "SSL_use_certificate_file failed");
2403#else /* OPENSSL_NO_STDIO */
2404 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
2405#endif /* OPENSSL_NO_STDIO */
2406
2407 return -1;
2408}
2409
2410
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002411static int tls_global_client_cert(struct tls_data *data,
2412 const char *client_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413{
2414#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002415 SSL_CTX *ssl_ctx = data->ssl;
2416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417 if (client_cert == NULL)
2418 return 0;
2419
2420 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
2421 SSL_FILETYPE_ASN1) != 1 &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002422 SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002423 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
2424 SSL_FILETYPE_PEM) != 1) {
2425 tls_show_errors(MSG_INFO, __func__,
2426 "Failed to load client certificate");
2427 return -1;
2428 }
2429 return 0;
2430#else /* OPENSSL_NO_STDIO */
2431 if (client_cert == NULL)
2432 return 0;
2433 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
2434 return -1;
2435#endif /* OPENSSL_NO_STDIO */
2436}
2437
2438
2439static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
2440{
2441 if (password == NULL) {
2442 return 0;
2443 }
2444 os_strlcpy(buf, (char *) password, size);
2445 return os_strlen(buf);
2446}
2447
2448
2449#ifdef PKCS12_FUNCS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002450static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002451 const char *passwd)
2452{
2453 EVP_PKEY *pkey;
2454 X509 *cert;
2455 STACK_OF(X509) *certs;
2456 int res = 0;
2457 char buf[256];
2458
2459 pkey = NULL;
2460 cert = NULL;
2461 certs = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002462 if (!passwd)
2463 passwd = "";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
2465 tls_show_errors(MSG_DEBUG, __func__,
2466 "Failed to parse PKCS12 file");
2467 PKCS12_free(p12);
2468 return -1;
2469 }
2470 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
2471
2472 if (cert) {
2473 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2474 sizeof(buf));
2475 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
2476 "subject='%s'", buf);
2477 if (ssl) {
2478 if (SSL_use_certificate(ssl, cert) != 1)
2479 res = -1;
2480 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002481 if (SSL_CTX_use_certificate(data->ssl, cert) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482 res = -1;
2483 }
2484 X509_free(cert);
2485 }
2486
2487 if (pkey) {
2488 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
2489 if (ssl) {
2490 if (SSL_use_PrivateKey(ssl, pkey) != 1)
2491 res = -1;
2492 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002493 if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494 res = -1;
2495 }
2496 EVP_PKEY_free(pkey);
2497 }
2498
2499 if (certs) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002500#if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002501 if (ssl)
2502 SSL_clear_chain_certs(ssl);
2503 else
2504 SSL_CTX_clear_chain_certs(data->ssl);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002505 while ((cert = sk_X509_pop(certs)) != NULL) {
2506 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2507 sizeof(buf));
2508 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
2509 " from PKCS12: subject='%s'", buf);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002510 if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) ||
2511 (!ssl && SSL_CTX_add1_chain_cert(data->ssl,
2512 cert) != 1)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002513 tls_show_errors(MSG_DEBUG, __func__,
2514 "Failed to add additional certificate");
2515 res = -1;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002516 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002517 break;
2518 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002519 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002520 }
2521 if (!res) {
2522 /* Try to continue anyway */
2523 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002524 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002525#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002526 if (ssl)
2527 res = SSL_build_cert_chain(
2528 ssl,
2529 SSL_BUILD_CHAIN_FLAG_CHECK |
2530 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
2531 else
2532 res = SSL_CTX_build_cert_chain(
2533 data->ssl,
2534 SSL_BUILD_CHAIN_FLAG_CHECK |
2535 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002536 if (!res) {
2537 tls_show_errors(MSG_DEBUG, __func__,
2538 "Failed to build certificate chain");
2539 } else if (res == 2) {
2540 wpa_printf(MSG_DEBUG,
2541 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
2542 }
2543#endif /* OPENSSL_IS_BORINGSSL */
2544 /*
2545 * Try to continue regardless of result since it is possible for
2546 * the extra certificates not to be required.
2547 */
2548 res = 0;
2549#else /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002550 SSL_CTX_clear_extra_chain_certs(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002551 while ((cert = sk_X509_pop(certs)) != NULL) {
2552 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2553 sizeof(buf));
2554 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
2555 " from PKCS12: subject='%s'", buf);
2556 /*
2557 * There is no SSL equivalent for the chain cert - so
2558 * always add it to the context...
2559 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002560 if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1)
2561 {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002562 X509_free(cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563 res = -1;
2564 break;
2565 }
2566 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002567 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002568#endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002569 }
2570
2571 PKCS12_free(p12);
2572
2573 if (res < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002574 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002575
2576 return res;
2577}
2578#endif /* PKCS12_FUNCS */
2579
2580
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002581static int tls_read_pkcs12(struct tls_data *data, SSL *ssl,
2582 const char *private_key, const char *passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583{
2584#ifdef PKCS12_FUNCS
2585 FILE *f;
2586 PKCS12 *p12;
2587
2588 f = fopen(private_key, "rb");
2589 if (f == NULL)
2590 return -1;
2591
2592 p12 = d2i_PKCS12_fp(f, NULL);
2593 fclose(f);
2594
2595 if (p12 == NULL) {
2596 tls_show_errors(MSG_INFO, __func__,
2597 "Failed to use PKCS#12 file");
2598 return -1;
2599 }
2600
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002601 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002602
2603#else /* PKCS12_FUNCS */
2604 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
2605 "p12/pfx files");
2606 return -1;
2607#endif /* PKCS12_FUNCS */
2608}
2609
2610
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002611static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612 const u8 *blob, size_t len, const char *passwd)
2613{
2614#ifdef PKCS12_FUNCS
2615 PKCS12 *p12;
2616
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002617 p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618 if (p12 == NULL) {
2619 tls_show_errors(MSG_INFO, __func__,
2620 "Failed to use PKCS#12 blob");
2621 return -1;
2622 }
2623
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002624 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002625
2626#else /* PKCS12_FUNCS */
2627 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
2628 "p12/pfx blobs");
2629 return -1;
2630#endif /* PKCS12_FUNCS */
2631}
2632
2633
2634#ifndef OPENSSL_NO_ENGINE
2635static int tls_engine_get_cert(struct tls_connection *conn,
2636 const char *cert_id,
2637 X509 **cert)
2638{
2639 /* this runs after the private key is loaded so no PIN is required */
2640 struct {
2641 const char *cert_id;
2642 X509 *cert;
2643 } params;
2644 params.cert_id = cert_id;
2645 params.cert = NULL;
2646
2647 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
2648 0, &params, NULL, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002649 unsigned long err = ERR_get_error();
2650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
2652 " '%s' [%s]", cert_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002653 ERR_error_string(err, NULL));
2654 if (tls_is_pin_error(err))
2655 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
2657 }
2658 if (!params.cert) {
2659 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
2660 " '%s'", cert_id);
2661 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
2662 }
2663 *cert = params.cert;
2664 return 0;
2665}
2666#endif /* OPENSSL_NO_ENGINE */
2667
2668
2669static int tls_connection_engine_client_cert(struct tls_connection *conn,
2670 const char *cert_id)
2671{
2672#ifndef OPENSSL_NO_ENGINE
2673 X509 *cert;
2674
2675 if (tls_engine_get_cert(conn, cert_id, &cert))
2676 return -1;
2677
2678 if (!SSL_use_certificate(conn->ssl, cert)) {
2679 tls_show_errors(MSG_ERROR, __func__,
2680 "SSL_use_certificate failed");
2681 X509_free(cert);
2682 return -1;
2683 }
2684 X509_free(cert);
2685 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
2686 "OK");
2687 return 0;
2688
2689#else /* OPENSSL_NO_ENGINE */
2690 return -1;
2691#endif /* OPENSSL_NO_ENGINE */
2692}
2693
2694
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002695static int tls_connection_engine_ca_cert(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002696 struct tls_connection *conn,
2697 const char *ca_cert_id)
2698{
2699#ifndef OPENSSL_NO_ENGINE
2700 X509 *cert;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002701 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002702 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703
2704 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
2705 return -1;
2706
2707 /* start off the same as tls_connection_ca_cert */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002708 store = X509_STORE_new();
2709 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002710 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
2711 "certificate store", __func__);
2712 X509_free(cert);
2713 return -1;
2714 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002715 SSL_CTX_set_cert_store(ssl_ctx, store);
2716 if (!X509_STORE_add_cert(store, cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002717 unsigned long err = ERR_peek_error();
2718 tls_show_errors(MSG_WARNING, __func__,
2719 "Failed to add CA certificate from engine "
2720 "to certificate store");
2721 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2722 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2723 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
2724 " already in hash table error",
2725 __func__);
2726 } else {
2727 X509_free(cert);
2728 return -1;
2729 }
2730 }
2731 X509_free(cert);
2732 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
2733 "to certificate store", __func__);
2734 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002735 conn->ca_cert_verify = 1;
2736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002737 return 0;
2738
2739#else /* OPENSSL_NO_ENGINE */
2740 return -1;
2741#endif /* OPENSSL_NO_ENGINE */
2742}
2743
2744
2745static int tls_connection_engine_private_key(struct tls_connection *conn)
2746{
Adam Langley1eb02ed2015-04-21 19:00:05 -07002747#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002748 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
2749 tls_show_errors(MSG_ERROR, __func__,
2750 "ENGINE: cannot use private key for TLS");
2751 return -1;
2752 }
2753 if (!SSL_check_private_key(conn->ssl)) {
2754 tls_show_errors(MSG_INFO, __func__,
2755 "Private key failed verification");
2756 return -1;
2757 }
2758 return 0;
2759#else /* OPENSSL_NO_ENGINE */
2760 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
2761 "engine support was not compiled in");
2762 return -1;
2763#endif /* OPENSSL_NO_ENGINE */
2764}
2765
2766
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002767static int tls_connection_private_key(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002768 struct tls_connection *conn,
2769 const char *private_key,
2770 const char *private_key_passwd,
2771 const u8 *private_key_blob,
2772 size_t private_key_blob_len)
2773{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002774 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002775 char *passwd;
2776 int ok;
2777
2778 if (private_key == NULL && private_key_blob == NULL)
2779 return 0;
2780
2781 if (private_key_passwd) {
2782 passwd = os_strdup(private_key_passwd);
2783 if (passwd == NULL)
2784 return -1;
2785 } else
2786 passwd = NULL;
2787
2788 SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
2789 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
2790
2791 ok = 0;
2792 while (private_key_blob) {
2793 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
2794 (u8 *) private_key_blob,
2795 private_key_blob_len) == 1) {
2796 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
2797 "ASN1(EVP_PKEY_RSA) --> OK");
2798 ok = 1;
2799 break;
2800 }
2801
2802 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
2803 (u8 *) private_key_blob,
2804 private_key_blob_len) == 1) {
2805 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
2806 "ASN1(EVP_PKEY_DSA) --> OK");
2807 ok = 1;
2808 break;
2809 }
2810
2811 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
2812 (u8 *) private_key_blob,
2813 private_key_blob_len) == 1) {
2814 wpa_printf(MSG_DEBUG, "OpenSSL: "
2815 "SSL_use_RSAPrivateKey_ASN1 --> OK");
2816 ok = 1;
2817 break;
2818 }
2819
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002820 if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002821 private_key_blob_len, passwd) == 0) {
2822 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
2823 "OK");
2824 ok = 1;
2825 break;
2826 }
2827
2828 break;
2829 }
2830
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002831 while (!ok && private_key) {
2832#ifndef OPENSSL_NO_STDIO
2833 if (SSL_use_PrivateKey_file(conn->ssl, private_key,
2834 SSL_FILETYPE_ASN1) == 1) {
2835 wpa_printf(MSG_DEBUG, "OpenSSL: "
2836 "SSL_use_PrivateKey_File (DER) --> OK");
2837 ok = 1;
2838 break;
2839 }
2840
2841 if (SSL_use_PrivateKey_file(conn->ssl, private_key,
2842 SSL_FILETYPE_PEM) == 1) {
2843 wpa_printf(MSG_DEBUG, "OpenSSL: "
2844 "SSL_use_PrivateKey_File (PEM) --> OK");
2845 ok = 1;
2846 break;
2847 }
2848#else /* OPENSSL_NO_STDIO */
2849 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2850 __func__);
2851#endif /* OPENSSL_NO_STDIO */
2852
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002853 if (tls_read_pkcs12(data, conn->ssl, private_key, passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002854 == 0) {
2855 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
2856 "--> OK");
2857 ok = 1;
2858 break;
2859 }
2860
2861 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
2862 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
2863 "access certificate store --> OK");
2864 ok = 1;
2865 break;
2866 }
2867
2868 break;
2869 }
2870
2871 if (!ok) {
2872 tls_show_errors(MSG_INFO, __func__,
2873 "Failed to load private key");
2874 os_free(passwd);
2875 return -1;
2876 }
2877 ERR_clear_error();
2878 SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
2879 os_free(passwd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881 if (!SSL_check_private_key(conn->ssl)) {
2882 tls_show_errors(MSG_INFO, __func__, "Private key failed "
2883 "verification");
2884 return -1;
2885 }
2886
2887 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
2888 return 0;
2889}
2890
2891
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002892static int tls_global_private_key(struct tls_data *data,
2893 const char *private_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894 const char *private_key_passwd)
2895{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002896 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897 char *passwd;
2898
2899 if (private_key == NULL)
2900 return 0;
2901
2902 if (private_key_passwd) {
2903 passwd = os_strdup(private_key_passwd);
2904 if (passwd == NULL)
2905 return -1;
2906 } else
2907 passwd = NULL;
2908
2909 SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
2910 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
2911 if (
2912#ifndef OPENSSL_NO_STDIO
2913 SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
2914 SSL_FILETYPE_ASN1) != 1 &&
2915 SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
2916 SSL_FILETYPE_PEM) != 1 &&
2917#endif /* OPENSSL_NO_STDIO */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002918 tls_read_pkcs12(data, NULL, private_key, passwd)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002919 tls_show_errors(MSG_INFO, __func__,
2920 "Failed to load private key");
2921 os_free(passwd);
2922 ERR_clear_error();
2923 return -1;
2924 }
2925 os_free(passwd);
2926 ERR_clear_error();
2927 SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002928
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002929 if (!SSL_CTX_check_private_key(ssl_ctx)) {
2930 tls_show_errors(MSG_INFO, __func__,
2931 "Private key failed verification");
2932 return -1;
2933 }
2934
2935 return 0;
2936}
2937
2938
2939static int tls_connection_dh(struct tls_connection *conn, const char *dh_file)
2940{
2941#ifdef OPENSSL_NO_DH
2942 if (dh_file == NULL)
2943 return 0;
2944 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
2945 "dh_file specified");
2946 return -1;
2947#else /* OPENSSL_NO_DH */
2948 DH *dh;
2949 BIO *bio;
2950
2951 /* TODO: add support for dh_blob */
2952 if (dh_file == NULL)
2953 return 0;
2954 if (conn == NULL)
2955 return -1;
2956
2957 bio = BIO_new_file(dh_file, "r");
2958 if (bio == NULL) {
2959 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
2960 dh_file, ERR_error_string(ERR_get_error(), NULL));
2961 return -1;
2962 }
2963 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
2964 BIO_free(bio);
2965#ifndef OPENSSL_NO_DSA
2966 while (dh == NULL) {
2967 DSA *dsa;
2968 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
2969 " trying to parse as DSA params", dh_file,
2970 ERR_error_string(ERR_get_error(), NULL));
2971 bio = BIO_new_file(dh_file, "r");
2972 if (bio == NULL)
2973 break;
2974 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
2975 BIO_free(bio);
2976 if (!dsa) {
2977 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
2978 "'%s': %s", dh_file,
2979 ERR_error_string(ERR_get_error(), NULL));
2980 break;
2981 }
2982
2983 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
2984 dh = DSA_dup_DH(dsa);
2985 DSA_free(dsa);
2986 if (dh == NULL) {
2987 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
2988 "params into DH params");
2989 break;
2990 }
2991 break;
2992 }
2993#endif /* !OPENSSL_NO_DSA */
2994 if (dh == NULL) {
2995 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
2996 "'%s'", dh_file);
2997 return -1;
2998 }
2999
3000 if (SSL_set_tmp_dh(conn->ssl, dh) != 1) {
3001 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
3002 "%s", dh_file,
3003 ERR_error_string(ERR_get_error(), NULL));
3004 DH_free(dh);
3005 return -1;
3006 }
3007 DH_free(dh);
3008 return 0;
3009#endif /* OPENSSL_NO_DH */
3010}
3011
3012
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003013static int tls_global_dh(struct tls_data *data, const char *dh_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014{
3015#ifdef OPENSSL_NO_DH
3016 if (dh_file == NULL)
3017 return 0;
3018 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
3019 "dh_file specified");
3020 return -1;
3021#else /* OPENSSL_NO_DH */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003022 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003023 DH *dh;
3024 BIO *bio;
3025
3026 /* TODO: add support for dh_blob */
3027 if (dh_file == NULL)
3028 return 0;
3029 if (ssl_ctx == NULL)
3030 return -1;
3031
3032 bio = BIO_new_file(dh_file, "r");
3033 if (bio == NULL) {
3034 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
3035 dh_file, ERR_error_string(ERR_get_error(), NULL));
3036 return -1;
3037 }
3038 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
3039 BIO_free(bio);
3040#ifndef OPENSSL_NO_DSA
3041 while (dh == NULL) {
3042 DSA *dsa;
3043 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
3044 " trying to parse as DSA params", dh_file,
3045 ERR_error_string(ERR_get_error(), NULL));
3046 bio = BIO_new_file(dh_file, "r");
3047 if (bio == NULL)
3048 break;
3049 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
3050 BIO_free(bio);
3051 if (!dsa) {
3052 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
3053 "'%s': %s", dh_file,
3054 ERR_error_string(ERR_get_error(), NULL));
3055 break;
3056 }
3057
3058 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
3059 dh = DSA_dup_DH(dsa);
3060 DSA_free(dsa);
3061 if (dh == NULL) {
3062 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
3063 "params into DH params");
3064 break;
3065 }
3066 break;
3067 }
3068#endif /* !OPENSSL_NO_DSA */
3069 if (dh == NULL) {
3070 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
3071 "'%s'", dh_file);
3072 return -1;
3073 }
3074
3075 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
3076 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
3077 "%s", dh_file,
3078 ERR_error_string(ERR_get_error(), NULL));
3079 DH_free(dh);
3080 return -1;
3081 }
3082 DH_free(dh);
3083 return 0;
3084#endif /* OPENSSL_NO_DH */
3085}
3086
3087
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003088int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
3089 struct tls_random *keys)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090{
3091 SSL *ssl;
3092
3093 if (conn == NULL || keys == NULL)
3094 return -1;
3095 ssl = conn->ssl;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003096 if (ssl == NULL)
3097 return -1;
3098
3099 os_memset(keys, 0, sizeof(*keys));
3100 keys->client_random = conn->client_random;
3101 keys->client_random_len = SSL_get_client_random(
3102 ssl, conn->client_random, sizeof(conn->client_random));
3103 keys->server_random = conn->server_random;
3104 keys->server_random_len = SSL_get_server_random(
3105 ssl, conn->server_random, sizeof(conn->server_random));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106
3107 return 0;
3108}
3109
3110
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003111#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003112static int openssl_get_keyblock_size(SSL *ssl)
3113{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003114#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003115 const EVP_CIPHER *c;
3116 const EVP_MD *h;
3117 int md_size;
3118
3119 if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL ||
3120 ssl->read_hash == NULL)
3121 return -1;
3122
3123 c = ssl->enc_read_ctx->cipher;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003124 h = EVP_MD_CTX_md(ssl->read_hash);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003125 if (h)
3126 md_size = EVP_MD_size(h);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003127 else if (ssl->s3)
3128 md_size = ssl->s3->tmp.new_mac_secret_size;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003129 else
3130 return -1;
3131
3132 wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
3133 "IV_len=%d", EVP_CIPHER_key_length(c), md_size,
3134 EVP_CIPHER_iv_length(c));
3135 return 2 * (EVP_CIPHER_key_length(c) +
3136 md_size +
3137 EVP_CIPHER_iv_length(c));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003138#else
3139 const SSL_CIPHER *ssl_cipher;
3140 int cipher, digest;
3141 const EVP_CIPHER *c;
3142 const EVP_MD *h;
3143
3144 ssl_cipher = SSL_get_current_cipher(ssl);
3145 if (!ssl_cipher)
3146 return -1;
3147 cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
3148 digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
3149 wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d",
3150 cipher, digest);
3151 if (cipher < 0 || digest < 0)
3152 return -1;
3153 c = EVP_get_cipherbynid(cipher);
3154 h = EVP_get_digestbynid(digest);
3155 if (!c || !h)
3156 return -1;
3157
3158 wpa_printf(MSG_DEBUG,
3159 "OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d",
3160 EVP_CIPHER_key_length(c), EVP_MD_size(h),
3161 EVP_CIPHER_iv_length(c));
3162 return 2 * (EVP_CIPHER_key_length(c) + EVP_MD_size(h) +
3163 EVP_CIPHER_iv_length(c));
3164#endif
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003165}
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003166#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003167
3168
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003169int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
3170 const char *label, u8 *out, size_t out_len)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003171{
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003172 if (!conn ||
3173 SSL_export_keying_material(conn->ssl, out, out_len, label,
3174 os_strlen(label), NULL, 0, 0) != 1)
3175 return -1;
3176 return 0;
3177}
3178
3179
3180int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
3181 u8 *out, size_t out_len)
3182{
3183#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003184 SSL *ssl;
3185 SSL_SESSION *sess;
3186 u8 *rnd;
3187 int ret = -1;
3188 int skip = 0;
3189 u8 *tmp_out = NULL;
3190 u8 *_out = out;
3191 unsigned char client_random[SSL3_RANDOM_SIZE];
3192 unsigned char server_random[SSL3_RANDOM_SIZE];
3193 unsigned char master_key[64];
3194 size_t master_key_len;
3195 const char *ver;
3196
3197 /*
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003198 * TLS library did not support EAP-FAST key generation, so get the
3199 * needed TLS session parameters and use an internal implementation of
3200 * TLS PRF to derive the key.
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003201 */
3202
3203 if (conn == NULL)
3204 return -1;
3205 ssl = conn->ssl;
3206 if (ssl == NULL)
3207 return -1;
3208 ver = SSL_get_version(ssl);
3209 sess = SSL_get_session(ssl);
3210 if (!ver || !sess)
3211 return -1;
3212
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003213 skip = openssl_get_keyblock_size(ssl);
3214 if (skip < 0)
3215 return -1;
3216 tmp_out = os_malloc(skip + out_len);
3217 if (!tmp_out)
3218 return -1;
3219 _out = tmp_out;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003220
3221 rnd = os_malloc(2 * SSL3_RANDOM_SIZE);
3222 if (!rnd) {
3223 os_free(tmp_out);
3224 return -1;
3225 }
3226
3227 SSL_get_client_random(ssl, client_random, sizeof(client_random));
3228 SSL_get_server_random(ssl, server_random, sizeof(server_random));
3229 master_key_len = SSL_SESSION_get_master_key(sess, master_key,
3230 sizeof(master_key));
3231
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003232 os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE);
3233 os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003234
3235 if (os_strcmp(ver, "TLSv1.2") == 0) {
3236 tls_prf_sha256(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003237 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003238 _out, skip + out_len);
3239 ret = 0;
3240 } else if (tls_prf_sha1_md5(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003241 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003242 _out, skip + out_len) == 0) {
3243 ret = 0;
3244 }
3245 os_memset(master_key, 0, sizeof(master_key));
3246 os_free(rnd);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003247 if (ret == 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003248 os_memcpy(out, _out + skip, out_len);
3249 bin_clear_free(tmp_out, skip);
3250
3251 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003252#else /* OPENSSL_NEED_EAP_FAST_PRF */
3253 wpa_printf(MSG_ERROR,
3254 "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode");
3255 return -1;
3256#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257}
3258
3259
3260static struct wpabuf *
3261openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data,
3262 int server)
3263{
3264 int res;
3265 struct wpabuf *out_data;
3266
3267 /*
3268 * Give TLS handshake data from the server (if available) to OpenSSL
3269 * for processing.
3270 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003271 if (in_data && wpabuf_len(in_data) > 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003272 BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
3273 < 0) {
3274 tls_show_errors(MSG_INFO, __func__,
3275 "Handshake failed - BIO_write");
3276 return NULL;
3277 }
3278
3279 /* Initiate TLS handshake or continue the existing handshake */
3280 if (server)
3281 res = SSL_accept(conn->ssl);
3282 else
3283 res = SSL_connect(conn->ssl);
3284 if (res != 1) {
3285 int err = SSL_get_error(conn->ssl, res);
3286 if (err == SSL_ERROR_WANT_READ)
3287 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
3288 "more data");
3289 else if (err == SSL_ERROR_WANT_WRITE)
3290 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
3291 "write");
3292 else {
3293 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
3294 conn->failed++;
3295 }
3296 }
3297
3298 /* Get the TLS handshake data to be sent to the server */
3299 res = BIO_ctrl_pending(conn->ssl_out);
3300 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
3301 out_data = wpabuf_alloc(res);
3302 if (out_data == NULL) {
3303 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
3304 "handshake output (%d bytes)", res);
3305 if (BIO_reset(conn->ssl_out) < 0) {
3306 tls_show_errors(MSG_INFO, __func__,
3307 "BIO_reset failed");
3308 }
3309 return NULL;
3310 }
3311 res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
3312 res);
3313 if (res < 0) {
3314 tls_show_errors(MSG_INFO, __func__,
3315 "Handshake failed - BIO_read");
3316 if (BIO_reset(conn->ssl_out) < 0) {
3317 tls_show_errors(MSG_INFO, __func__,
3318 "BIO_reset failed");
3319 }
3320 wpabuf_free(out_data);
3321 return NULL;
3322 }
3323 wpabuf_put(out_data, res);
3324
3325 return out_data;
3326}
3327
3328
3329static struct wpabuf *
3330openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
3331{
3332 struct wpabuf *appl_data;
3333 int res;
3334
3335 appl_data = wpabuf_alloc(max_len + 100);
3336 if (appl_data == NULL)
3337 return NULL;
3338
3339 res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
3340 wpabuf_size(appl_data));
3341 if (res < 0) {
3342 int err = SSL_get_error(conn->ssl, res);
3343 if (err == SSL_ERROR_WANT_READ ||
3344 err == SSL_ERROR_WANT_WRITE) {
3345 wpa_printf(MSG_DEBUG, "SSL: No Application Data "
3346 "included");
3347 } else {
3348 tls_show_errors(MSG_INFO, __func__,
3349 "Failed to read possible "
3350 "Application Data");
3351 }
3352 wpabuf_free(appl_data);
3353 return NULL;
3354 }
3355
3356 wpabuf_put(appl_data, res);
3357 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
3358 "message", appl_data);
3359
3360 return appl_data;
3361}
3362
3363
3364static struct wpabuf *
3365openssl_connection_handshake(struct tls_connection *conn,
3366 const struct wpabuf *in_data,
3367 struct wpabuf **appl_data, int server)
3368{
3369 struct wpabuf *out_data;
3370
3371 if (appl_data)
3372 *appl_data = NULL;
3373
3374 out_data = openssl_handshake(conn, in_data, server);
3375 if (out_data == NULL)
3376 return NULL;
Jouni Malinen26af48b2014-04-09 13:02:53 +03003377 if (conn->invalid_hb_used) {
3378 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3379 wpabuf_free(out_data);
3380 return NULL;
3381 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003382
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003383 if (SSL_is_init_finished(conn->ssl)) {
3384 wpa_printf(MSG_DEBUG,
3385 "OpenSSL: Handshake finished - resumed=%d",
3386 tls_connection_resumed(conn->ssl_ctx, conn));
3387 if (appl_data && in_data)
3388 *appl_data = openssl_get_appl_data(conn,
3389 wpabuf_len(in_data));
3390 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003391
Jouni Malinen26af48b2014-04-09 13:02:53 +03003392 if (conn->invalid_hb_used) {
3393 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3394 if (appl_data) {
3395 wpabuf_free(*appl_data);
3396 *appl_data = NULL;
3397 }
3398 wpabuf_free(out_data);
3399 return NULL;
3400 }
3401
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003402 return out_data;
3403}
3404
3405
3406struct wpabuf *
3407tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
3408 const struct wpabuf *in_data,
3409 struct wpabuf **appl_data)
3410{
3411 return openssl_connection_handshake(conn, in_data, appl_data, 0);
3412}
3413
3414
3415struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
3416 struct tls_connection *conn,
3417 const struct wpabuf *in_data,
3418 struct wpabuf **appl_data)
3419{
3420 return openssl_connection_handshake(conn, in_data, appl_data, 1);
3421}
3422
3423
3424struct wpabuf * tls_connection_encrypt(void *tls_ctx,
3425 struct tls_connection *conn,
3426 const struct wpabuf *in_data)
3427{
3428 int res;
3429 struct wpabuf *buf;
3430
3431 if (conn == NULL)
3432 return NULL;
3433
3434 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
3435 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
3436 (res = BIO_reset(conn->ssl_out)) < 0) {
3437 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
3438 return NULL;
3439 }
3440 res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
3441 if (res < 0) {
3442 tls_show_errors(MSG_INFO, __func__,
3443 "Encryption failed - SSL_write");
3444 return NULL;
3445 }
3446
3447 /* Read encrypted data to be sent to the server */
3448 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
3449 if (buf == NULL)
3450 return NULL;
3451 res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
3452 if (res < 0) {
3453 tls_show_errors(MSG_INFO, __func__,
3454 "Encryption failed - BIO_read");
3455 wpabuf_free(buf);
3456 return NULL;
3457 }
3458 wpabuf_put(buf, res);
3459
3460 return buf;
3461}
3462
3463
3464struct wpabuf * tls_connection_decrypt(void *tls_ctx,
3465 struct tls_connection *conn,
3466 const struct wpabuf *in_data)
3467{
3468 int res;
3469 struct wpabuf *buf;
3470
3471 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
3472 res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
3473 wpabuf_len(in_data));
3474 if (res < 0) {
3475 tls_show_errors(MSG_INFO, __func__,
3476 "Decryption failed - BIO_write");
3477 return NULL;
3478 }
3479 if (BIO_reset(conn->ssl_out) < 0) {
3480 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
3481 return NULL;
3482 }
3483
3484 /* Read decrypted data for further processing */
3485 /*
3486 * Even though we try to disable TLS compression, it is possible that
3487 * this cannot be done with all TLS libraries. Add extra buffer space
3488 * to handle the possibility of the decrypted data being longer than
3489 * input data.
3490 */
3491 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
3492 if (buf == NULL)
3493 return NULL;
3494 res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
3495 if (res < 0) {
3496 tls_show_errors(MSG_INFO, __func__,
3497 "Decryption failed - SSL_read");
3498 wpabuf_free(buf);
3499 return NULL;
3500 }
3501 wpabuf_put(buf, res);
3502
Jouni Malinen26af48b2014-04-09 13:02:53 +03003503 if (conn->invalid_hb_used) {
3504 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3505 wpabuf_free(buf);
3506 return NULL;
3507 }
3508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003509 return buf;
3510}
3511
3512
3513int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
3514{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003515 return conn ? SSL_cache_hit(conn->ssl) : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003516}
3517
3518
3519int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
3520 u8 *ciphers)
3521{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003522 char buf[500], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003523 u8 *c;
3524 int ret;
3525
3526 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
3527 return -1;
3528
3529 buf[0] = '\0';
3530 pos = buf;
3531 end = pos + sizeof(buf);
3532
3533 c = ciphers;
3534 while (*c != TLS_CIPHER_NONE) {
3535 const char *suite;
3536
3537 switch (*c) {
3538 case TLS_CIPHER_RC4_SHA:
3539 suite = "RC4-SHA";
3540 break;
3541 case TLS_CIPHER_AES128_SHA:
3542 suite = "AES128-SHA";
3543 break;
3544 case TLS_CIPHER_RSA_DHE_AES128_SHA:
3545 suite = "DHE-RSA-AES128-SHA";
3546 break;
3547 case TLS_CIPHER_ANON_DH_AES128_SHA:
3548 suite = "ADH-AES128-SHA";
3549 break;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003550 case TLS_CIPHER_RSA_DHE_AES256_SHA:
3551 suite = "DHE-RSA-AES256-SHA";
3552 break;
3553 case TLS_CIPHER_AES256_SHA:
3554 suite = "AES256-SHA";
3555 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003556 default:
3557 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
3558 "cipher selection: %d", *c);
3559 return -1;
3560 }
3561 ret = os_snprintf(pos, end - pos, ":%s", suite);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003562 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003563 break;
3564 pos += ret;
3565
3566 c++;
3567 }
3568
3569 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
3570
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003571#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003572#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3573 if (os_strstr(buf, ":ADH-")) {
3574 /*
3575 * Need to drop to security level 0 to allow anonymous
3576 * cipher suites for EAP-FAST.
3577 */
3578 SSL_set_security_level(conn->ssl, 0);
3579 } else if (SSL_get_security_level(conn->ssl) == 0) {
3580 /* Force at least security level 1 */
3581 SSL_set_security_level(conn->ssl, 1);
3582 }
3583#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3584#endif
3585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
3587 tls_show_errors(MSG_INFO, __func__,
3588 "Cipher suite configuration failed");
3589 return -1;
3590 }
3591
3592 return 0;
3593}
3594
3595
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003596int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
3597 char *buf, size_t buflen)
3598{
3599 const char *name;
3600 if (conn == NULL || conn->ssl == NULL)
3601 return -1;
3602
3603 name = SSL_get_version(conn->ssl);
3604 if (name == NULL)
3605 return -1;
3606
3607 os_strlcpy(buf, name, buflen);
3608 return 0;
3609}
3610
3611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003612int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
3613 char *buf, size_t buflen)
3614{
3615 const char *name;
3616 if (conn == NULL || conn->ssl == NULL)
3617 return -1;
3618
3619 name = SSL_get_cipher(conn->ssl);
3620 if (name == NULL)
3621 return -1;
3622
3623 os_strlcpy(buf, name, buflen);
3624 return 0;
3625}
3626
3627
3628int tls_connection_enable_workaround(void *ssl_ctx,
3629 struct tls_connection *conn)
3630{
3631 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
3632
3633 return 0;
3634}
3635
3636
3637#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3638/* ClientHello TLS extensions require a patch to openssl, so this function is
3639 * commented out unless explicitly needed for EAP-FAST in order to be able to
3640 * build this file with unmodified openssl. */
3641int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
3642 int ext_type, const u8 *data,
3643 size_t data_len)
3644{
3645 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
3646 return -1;
3647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
3649 data_len) != 1)
3650 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003651
3652 return 0;
3653}
3654#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3655
3656
3657int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
3658{
3659 if (conn == NULL)
3660 return -1;
3661 return conn->failed;
3662}
3663
3664
3665int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
3666{
3667 if (conn == NULL)
3668 return -1;
3669 return conn->read_alerts;
3670}
3671
3672
3673int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
3674{
3675 if (conn == NULL)
3676 return -1;
3677 return conn->write_alerts;
3678}
3679
3680
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003681#ifdef HAVE_OCSP
3682
3683static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
3684{
3685#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003686 BIO *out;
3687 size_t rlen;
3688 char *txt;
3689 int res;
3690
3691 if (wpa_debug_level > MSG_DEBUG)
3692 return;
3693
3694 out = BIO_new(BIO_s_mem());
3695 if (!out)
3696 return;
3697
3698 OCSP_RESPONSE_print(out, rsp, 0);
3699 rlen = BIO_ctrl_pending(out);
3700 txt = os_malloc(rlen + 1);
3701 if (!txt) {
3702 BIO_free(out);
3703 return;
3704 }
3705
3706 res = BIO_read(out, txt, rlen);
3707 if (res > 0) {
3708 txt[res] = '\0';
3709 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt);
3710 }
3711 os_free(txt);
3712 BIO_free(out);
3713#endif /* CONFIG_NO_STDOUT_DEBUG */
3714}
3715
3716
Dmitry Shmidt71757432014-06-02 13:50:35 -07003717static void debug_print_cert(X509 *cert, const char *title)
3718{
3719#ifndef CONFIG_NO_STDOUT_DEBUG
3720 BIO *out;
3721 size_t rlen;
3722 char *txt;
3723 int res;
3724
3725 if (wpa_debug_level > MSG_DEBUG)
3726 return;
3727
3728 out = BIO_new(BIO_s_mem());
3729 if (!out)
3730 return;
3731
3732 X509_print(out, cert);
3733 rlen = BIO_ctrl_pending(out);
3734 txt = os_malloc(rlen + 1);
3735 if (!txt) {
3736 BIO_free(out);
3737 return;
3738 }
3739
3740 res = BIO_read(out, txt, rlen);
3741 if (res > 0) {
3742 txt[res] = '\0';
3743 wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt);
3744 }
3745 os_free(txt);
3746
3747 BIO_free(out);
3748#endif /* CONFIG_NO_STDOUT_DEBUG */
3749}
3750
3751
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003752static int ocsp_resp_cb(SSL *s, void *arg)
3753{
3754 struct tls_connection *conn = arg;
3755 const unsigned char *p;
3756 int len, status, reason;
3757 OCSP_RESPONSE *rsp;
3758 OCSP_BASICRESP *basic;
3759 OCSP_CERTID *id;
3760 ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003761 X509_STORE *store;
3762 STACK_OF(X509) *certs = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003763
3764 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
3765 if (!p) {
3766 wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
3767 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
3768 }
3769
3770 wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
3771
3772 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
3773 if (!rsp) {
3774 wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
3775 return 0;
3776 }
3777
3778 ocsp_debug_print_resp(rsp);
3779
3780 status = OCSP_response_status(rsp);
3781 if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
3782 wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
3783 status, OCSP_response_status_str(status));
3784 return 0;
3785 }
3786
3787 basic = OCSP_response_get1_basic(rsp);
3788 if (!basic) {
3789 wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
3790 return 0;
3791 }
3792
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003793 store = SSL_CTX_get_cert_store(conn->ssl_ctx);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003794 if (conn->peer_issuer) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07003795 debug_print_cert(conn->peer_issuer, "Add OCSP issuer");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003796
3797 if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) {
3798 tls_show_errors(MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003799 "OpenSSL: Could not add issuer to certificate store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003800 }
3801 certs = sk_X509_new_null();
3802 if (certs) {
3803 X509 *cert;
3804 cert = X509_dup(conn->peer_issuer);
3805 if (cert && !sk_X509_push(certs, cert)) {
3806 tls_show_errors(
3807 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003808 "OpenSSL: Could not add issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003809 X509_free(cert);
3810 sk_X509_free(certs);
3811 certs = NULL;
3812 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003813 if (certs && conn->peer_issuer_issuer) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003814 cert = X509_dup(conn->peer_issuer_issuer);
3815 if (cert && !sk_X509_push(certs, cert)) {
3816 tls_show_errors(
3817 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003818 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003819 X509_free(cert);
3820 }
3821 }
3822 }
3823 }
3824
3825 status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
3826 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003827 if (status <= 0) {
3828 tls_show_errors(MSG_INFO, __func__,
3829 "OpenSSL: OCSP response failed verification");
3830 OCSP_BASICRESP_free(basic);
3831 OCSP_RESPONSE_free(rsp);
3832 return 0;
3833 }
3834
3835 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
3836
Dmitry Shmidt56052862013-10-04 10:23:25 -07003837 if (!conn->peer_cert) {
3838 wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
3839 OCSP_BASICRESP_free(basic);
3840 OCSP_RESPONSE_free(rsp);
3841 return 0;
3842 }
3843
3844 if (!conn->peer_issuer) {
3845 wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003846 OCSP_BASICRESP_free(basic);
3847 OCSP_RESPONSE_free(rsp);
3848 return 0;
3849 }
3850
3851 id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer);
3852 if (!id) {
3853 wpa_printf(MSG_DEBUG, "OpenSSL: Could not create OCSP certificate identifier");
3854 OCSP_BASICRESP_free(basic);
3855 OCSP_RESPONSE_free(rsp);
3856 return 0;
3857 }
3858
3859 if (!OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
3860 &this_update, &next_update)) {
3861 wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
3862 (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" :
3863 " (OCSP not required)");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003864 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003865 OCSP_BASICRESP_free(basic);
3866 OCSP_RESPONSE_free(rsp);
3867 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
3868 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003869 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003870
3871 if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
3872 tls_show_errors(MSG_INFO, __func__,
3873 "OpenSSL: OCSP status times invalid");
3874 OCSP_BASICRESP_free(basic);
3875 OCSP_RESPONSE_free(rsp);
3876 return 0;
3877 }
3878
3879 OCSP_BASICRESP_free(basic);
3880 OCSP_RESPONSE_free(rsp);
3881
3882 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
3883 OCSP_cert_status_str(status));
3884
3885 if (status == V_OCSP_CERTSTATUS_GOOD)
3886 return 1;
3887 if (status == V_OCSP_CERTSTATUS_REVOKED)
3888 return 0;
3889 if (conn->flags & TLS_CONN_REQUIRE_OCSP) {
3890 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
3891 return 0;
3892 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07003893 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 -07003894 return 1;
3895}
3896
3897
3898static int ocsp_status_cb(SSL *s, void *arg)
3899{
3900 char *tmp;
3901 char *resp;
3902 size_t len;
3903
3904 if (tls_global->ocsp_stapling_response == NULL) {
3905 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured");
3906 return SSL_TLSEXT_ERR_OK;
3907 }
3908
3909 resp = os_readfile(tls_global->ocsp_stapling_response, &len);
3910 if (resp == NULL) {
3911 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file");
3912 /* TODO: Build OCSPResponse with responseStatus = internalError
3913 */
3914 return SSL_TLSEXT_ERR_OK;
3915 }
3916 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response");
3917 tmp = OPENSSL_malloc(len);
3918 if (tmp == NULL) {
3919 os_free(resp);
3920 return SSL_TLSEXT_ERR_ALERT_FATAL;
3921 }
3922
3923 os_memcpy(tmp, resp, len);
3924 os_free(resp);
3925 SSL_set_tlsext_status_ocsp_resp(s, tmp, len);
3926
3927 return SSL_TLSEXT_ERR_OK;
3928}
3929
3930#endif /* HAVE_OCSP */
3931
3932
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003933int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
3934 const struct tls_connection_params *params)
3935{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003936 struct tls_data *data = tls_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003937 int ret;
3938 unsigned long err;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003939 int can_pkcs11 = 0;
3940 const char *key_id = params->key_id;
3941 const char *cert_id = params->cert_id;
3942 const char *ca_cert_id = params->ca_cert_id;
3943 const char *engine_id = params->engine ? params->engine_id : NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944
3945 if (conn == NULL)
3946 return -1;
3947
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08003948 if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
3949 wpa_printf(MSG_INFO,
3950 "OpenSSL: ocsp=3 not supported");
3951 return -1;
3952 }
3953
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003954 /*
3955 * If the engine isn't explicitly configured, and any of the
3956 * cert/key fields are actually PKCS#11 URIs, then automatically
3957 * use the PKCS#11 ENGINE.
3958 */
3959 if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0)
3960 can_pkcs11 = 1;
3961
3962 if (!key_id && params->private_key && can_pkcs11 &&
3963 os_strncmp(params->private_key, "pkcs11:", 7) == 0) {
3964 can_pkcs11 = 2;
3965 key_id = params->private_key;
3966 }
3967
3968 if (!cert_id && params->client_cert && can_pkcs11 &&
3969 os_strncmp(params->client_cert, "pkcs11:", 7) == 0) {
3970 can_pkcs11 = 2;
3971 cert_id = params->client_cert;
3972 }
3973
3974 if (!ca_cert_id && params->ca_cert && can_pkcs11 &&
3975 os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) {
3976 can_pkcs11 = 2;
3977 ca_cert_id = params->ca_cert;
3978 }
3979
3980 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
3981 if (can_pkcs11 == 2 && !engine_id)
3982 engine_id = "pkcs11";
3983
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003984#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003985#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003986 if (params->flags & TLS_CONN_EAP_FAST) {
3987 wpa_printf(MSG_DEBUG,
3988 "OpenSSL: Use TLSv1_method() for EAP-FAST");
3989 if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) {
3990 tls_show_errors(MSG_INFO, __func__,
3991 "Failed to set TLSv1_method() for EAP-FAST");
3992 return -1;
3993 }
3994 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003995#endif
3996#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998 while ((err = ERR_get_error())) {
3999 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
4000 __func__, ERR_error_string(err, NULL));
4001 }
4002
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004003 if (engine_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004004 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004005 ret = tls_engine_init(conn, engine_id, params->pin,
4006 key_id, cert_id, ca_cert_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004007 if (ret)
4008 return ret;
4009 }
4010 if (tls_connection_set_subject_match(conn,
4011 params->subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07004012 params->altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004013 params->suffix_match,
4014 params->domain_match))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004015 return -1;
4016
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004017 if (engine_id && ca_cert_id) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004018 if (tls_connection_engine_ca_cert(data, conn, ca_cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004019 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004020 } else if (tls_connection_ca_cert(data, conn, params->ca_cert,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004021 params->ca_cert_blob,
4022 params->ca_cert_blob_len,
4023 params->ca_path))
4024 return -1;
4025
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004026 if (engine_id && cert_id) {
4027 if (tls_connection_engine_client_cert(conn, cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004028 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
4029 } else if (tls_connection_client_cert(conn, params->client_cert,
4030 params->client_cert_blob,
4031 params->client_cert_blob_len))
4032 return -1;
4033
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004034 if (engine_id && key_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004035 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
4036 if (tls_connection_engine_private_key(conn))
4037 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004038 } else if (tls_connection_private_key(data, conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004039 params->private_key,
4040 params->private_key_passwd,
4041 params->private_key_blob,
4042 params->private_key_blob_len)) {
4043 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
4044 params->private_key);
4045 return -1;
4046 }
4047
4048 if (tls_connection_dh(conn, params->dh_file)) {
4049 wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
4050 params->dh_file);
4051 return -1;
4052 }
4053
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004054 if (params->openssl_ciphers &&
4055 SSL_set_cipher_list(conn->ssl, params->openssl_ciphers) != 1) {
4056 wpa_printf(MSG_INFO,
4057 "OpenSSL: Failed to set cipher string '%s'",
4058 params->openssl_ciphers);
4059 return -1;
4060 }
4061
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004062 tls_set_conn_flags(conn->ssl, params->flags);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004063
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004064#ifdef OPENSSL_IS_BORINGSSL
4065 if (params->flags & TLS_CONN_REQUEST_OCSP) {
4066 SSL_enable_ocsp_stapling(conn->ssl);
4067 }
4068#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004069#ifdef HAVE_OCSP
4070 if (params->flags & TLS_CONN_REQUEST_OCSP) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004071 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004072 SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp);
4073 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb);
4074 SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn);
4075 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004076#else /* HAVE_OCSP */
4077 if (params->flags & TLS_CONN_REQUIRE_OCSP) {
4078 wpa_printf(MSG_INFO,
4079 "OpenSSL: No OCSP support included - reject configuration");
4080 return -1;
4081 }
4082 if (params->flags & TLS_CONN_REQUEST_OCSP) {
4083 wpa_printf(MSG_DEBUG,
4084 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
4085 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004086#endif /* HAVE_OCSP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004087#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004088
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07004089 conn->flags = params->flags;
4090
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004091 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004092
4093 return 0;
4094}
4095
4096
4097int tls_global_set_params(void *tls_ctx,
4098 const struct tls_connection_params *params)
4099{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004100 struct tls_data *data = tls_ctx;
4101 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004102 unsigned long err;
4103
4104 while ((err = ERR_get_error())) {
4105 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
4106 __func__, ERR_error_string(err, NULL));
4107 }
4108
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004109 if (tls_global_ca_cert(data, params->ca_cert) ||
4110 tls_global_client_cert(data, params->client_cert) ||
4111 tls_global_private_key(data, params->private_key,
4112 params->private_key_passwd) ||
4113 tls_global_dh(data, params->dh_file)) {
4114 wpa_printf(MSG_INFO, "TLS: Failed to set global parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004115 return -1;
4116 }
4117
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004118 if (params->openssl_ciphers &&
4119 SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) {
4120 wpa_printf(MSG_INFO,
4121 "OpenSSL: Failed to set cipher string '%s'",
4122 params->openssl_ciphers);
4123 return -1;
4124 }
4125
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004126#ifdef SSL_OP_NO_TICKET
4127 if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET)
4128 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
4129 else
4130 SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET);
4131#endif /* SSL_OP_NO_TICKET */
4132
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004133#ifdef HAVE_OCSP
4134 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb);
4135 SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx);
4136 os_free(tls_global->ocsp_stapling_response);
4137 if (params->ocsp_stapling_response)
4138 tls_global->ocsp_stapling_response =
4139 os_strdup(params->ocsp_stapling_response);
4140 else
4141 tls_global->ocsp_stapling_response = NULL;
4142#endif /* HAVE_OCSP */
4143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144 return 0;
4145}
4146
4147
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004148#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4149/* Pre-shared secred requires a patch to openssl, so this function is
4150 * commented out unless explicitly needed for EAP-FAST in order to be able to
4151 * build this file with unmodified openssl. */
4152
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004153#if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004154static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
4155 STACK_OF(SSL_CIPHER) *peer_ciphers,
4156 const SSL_CIPHER **cipher, void *arg)
4157#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004158static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
4159 STACK_OF(SSL_CIPHER) *peer_ciphers,
4160 SSL_CIPHER **cipher, void *arg)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004161#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004162{
4163 struct tls_connection *conn = arg;
4164 int ret;
4165
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004166#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004167 if (conn == NULL || conn->session_ticket_cb == NULL)
4168 return 0;
4169
4170 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
4171 conn->session_ticket,
4172 conn->session_ticket_len,
4173 s->s3->client_random,
4174 s->s3->server_random, secret);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004175#else
4176 unsigned char client_random[SSL3_RANDOM_SIZE];
4177 unsigned char server_random[SSL3_RANDOM_SIZE];
4178
4179 if (conn == NULL || conn->session_ticket_cb == NULL)
4180 return 0;
4181
4182 SSL_get_client_random(s, client_random, sizeof(client_random));
4183 SSL_get_server_random(s, server_random, sizeof(server_random));
4184
4185 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
4186 conn->session_ticket,
4187 conn->session_ticket_len,
4188 client_random,
4189 server_random, secret);
4190#endif
4191
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004192 os_free(conn->session_ticket);
4193 conn->session_ticket = NULL;
4194
4195 if (ret <= 0)
4196 return 0;
4197
4198 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
4199 return 1;
4200}
4201
4202
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004203static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
4204 int len, void *arg)
4205{
4206 struct tls_connection *conn = arg;
4207
4208 if (conn == NULL || conn->session_ticket_cb == NULL)
4209 return 0;
4210
4211 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
4212
4213 os_free(conn->session_ticket);
4214 conn->session_ticket = NULL;
4215
4216 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
4217 "extension", data, len);
4218
4219 conn->session_ticket = os_malloc(len);
4220 if (conn->session_ticket == NULL)
4221 return 0;
4222
4223 os_memcpy(conn->session_ticket, data, len);
4224 conn->session_ticket_len = len;
4225
4226 return 1;
4227}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004228#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4229
4230
4231int tls_connection_set_session_ticket_cb(void *tls_ctx,
4232 struct tls_connection *conn,
4233 tls_session_ticket_cb cb,
4234 void *ctx)
4235{
4236#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4237 conn->session_ticket_cb = cb;
4238 conn->session_ticket_cb_ctx = ctx;
4239
4240 if (cb) {
4241 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
4242 conn) != 1)
4243 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004244 SSL_set_session_ticket_ext_cb(conn->ssl,
4245 tls_session_ticket_ext_cb, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004246 } else {
4247 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
4248 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004249 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250 }
4251
4252 return 0;
4253#else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4254 return -1;
4255#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4256}
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004257
4258
4259int tls_get_library_version(char *buf, size_t buf_len)
4260{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004261#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004262 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
4263 OPENSSL_VERSION_TEXT,
4264 OpenSSL_version(OPENSSL_VERSION));
4265#else
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004266 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
4267 OPENSSL_VERSION_TEXT,
4268 SSLeay_version(SSLEAY_VERSION));
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004269#endif
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004270}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004271
4272
4273void tls_connection_set_success_data(struct tls_connection *conn,
4274 struct wpabuf *data)
4275{
4276 SSL_SESSION *sess;
4277 struct wpabuf *old;
4278
4279 if (tls_ex_idx_session < 0)
4280 goto fail;
4281 sess = SSL_get_session(conn->ssl);
4282 if (!sess)
4283 goto fail;
4284 old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
4285 if (old) {
4286 wpa_printf(MSG_DEBUG, "OpenSSL: Replacing old success data %p",
4287 old);
4288 wpabuf_free(old);
4289 }
4290 if (SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1)
4291 goto fail;
4292
4293 wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p", data);
4294 conn->success_data = 1;
4295 return;
4296
4297fail:
4298 wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data");
4299 wpabuf_free(data);
4300}
4301
4302
4303void tls_connection_set_success_data_resumed(struct tls_connection *conn)
4304{
4305 wpa_printf(MSG_DEBUG,
4306 "OpenSSL: Success data accepted for resumed session");
4307 conn->success_data = 1;
4308}
4309
4310
4311const struct wpabuf *
4312tls_connection_get_success_data(struct tls_connection *conn)
4313{
4314 SSL_SESSION *sess;
4315
4316 if (tls_ex_idx_session < 0 ||
4317 !(sess = SSL_get_session(conn->ssl)))
4318 return NULL;
4319 return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
4320}
4321
4322
4323void tls_connection_remove_session(struct tls_connection *conn)
4324{
4325 SSL_SESSION *sess;
4326
4327 sess = SSL_get_session(conn->ssl);
4328 if (!sess)
4329 return;
4330
4331 if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1)
4332 wpa_printf(MSG_DEBUG,
4333 "OpenSSL: Session was not cached");
4334 else
4335 wpa_printf(MSG_DEBUG,
4336 "OpenSSL: Removed cached session to disable session resumption");
4337}