blob: c831fbaf2f3187c699db37f94fcfc29896baca93 [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) {
732 ENGINE_free(engine);
733 wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
734 "available", id);
735 return 0;
736 }
737 ERR_clear_error();
738
739 engine = ENGINE_by_id(dynamic_id);
740 if (engine == NULL) {
741 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
742 dynamic_id,
743 ERR_error_string(ERR_get_error(), NULL));
744 return -1;
745 }
746
747 /* Perform the pre commands. This will load the engine. */
748 while (pre && pre[0]) {
749 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
750 if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
751 wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
752 "%s %s [%s]", pre[0], pre[1],
753 ERR_error_string(ERR_get_error(), NULL));
754 ENGINE_free(engine);
755 return -1;
756 }
757 pre += 2;
758 }
759
760 /*
761 * Free the reference to the "dynamic" engine. The loaded engine can
762 * now be looked up using ENGINE_by_id().
763 */
764 ENGINE_free(engine);
765
766 engine = ENGINE_by_id(id);
767 if (engine == NULL) {
768 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
769 id, ERR_error_string(ERR_get_error(), NULL));
770 return -1;
771 }
772
773 while (post && post[0]) {
774 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
775 if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
776 wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
777 " %s %s [%s]", post[0], post[1],
778 ERR_error_string(ERR_get_error(), NULL));
779 ENGINE_remove(engine);
780 ENGINE_free(engine);
781 return -1;
782 }
783 post += 2;
784 }
785 ENGINE_free(engine);
786
787 return 0;
788}
789
790
791/**
792 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
793 * @pkcs11_so_path: pksc11_so_path from the configuration
794 * @pcks11_module_path: pkcs11_module_path from the configuration
795 */
796static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
797 const char *pkcs11_module_path)
798{
799 char *engine_id = "pkcs11";
800 const char *pre_cmd[] = {
801 "SO_PATH", NULL /* pkcs11_so_path */,
802 "ID", NULL /* engine_id */,
803 "LIST_ADD", "1",
804 /* "NO_VCHECK", "1", */
805 "LOAD", NULL,
806 NULL, NULL
807 };
808 const char *post_cmd[] = {
809 "MODULE_PATH", NULL /* pkcs11_module_path */,
810 NULL, NULL
811 };
812
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800813 if (!pkcs11_so_path)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814 return 0;
815
816 pre_cmd[1] = pkcs11_so_path;
817 pre_cmd[3] = engine_id;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800818 if (pkcs11_module_path)
819 post_cmd[1] = pkcs11_module_path;
820 else
821 post_cmd[0] = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822
823 wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
824 pkcs11_so_path);
825
826 return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
827}
828
829
830/**
831 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
832 * @opensc_so_path: opensc_so_path from the configuration
833 */
834static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
835{
836 char *engine_id = "opensc";
837 const char *pre_cmd[] = {
838 "SO_PATH", NULL /* opensc_so_path */,
839 "ID", NULL /* engine_id */,
840 "LIST_ADD", "1",
841 "LOAD", NULL,
842 NULL, NULL
843 };
844
845 if (!opensc_so_path)
846 return 0;
847
848 pre_cmd[1] = opensc_so_path;
849 pre_cmd[3] = engine_id;
850
851 wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
852 opensc_so_path);
853
854 return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
855}
856#endif /* OPENSSL_NO_ENGINE */
857
858
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800859static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
860{
861 struct wpabuf *buf;
862
863 if (tls_ex_idx_session < 0)
864 return;
865 buf = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
866 if (!buf)
867 return;
868 wpa_printf(MSG_DEBUG,
869 "OpenSSL: Free application session data %p (sess %p)",
870 buf, sess);
871 wpabuf_free(buf);
872
873 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL);
874}
875
876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877void * tls_init(const struct tls_config *conf)
878{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800879 struct tls_data *data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 SSL_CTX *ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700881 struct tls_context *context;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800882 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883
884 if (tls_openssl_ref_count == 0) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700885 tls_global = context = tls_context_new(conf);
886 if (context == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888#ifdef CONFIG_FIPS
889#ifdef OPENSSL_FIPS
890 if (conf && conf->fips_mode) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800891 static int fips_enabled = 0;
892
893 if (!fips_enabled && !FIPS_mode_set(1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 wpa_printf(MSG_ERROR, "Failed to enable FIPS "
895 "mode");
896 ERR_load_crypto_strings();
897 ERR_print_errors_fp(stderr);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700898 os_free(tls_global);
899 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800901 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 wpa_printf(MSG_INFO, "Running in FIPS mode");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800903 fips_enabled = 1;
904 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 }
906#else /* OPENSSL_FIPS */
907 if (conf && conf->fips_mode) {
908 wpa_printf(MSG_ERROR, "FIPS mode requested, but not "
909 "supported");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700910 os_free(tls_global);
911 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 return NULL;
913 }
914#endif /* OPENSSL_FIPS */
915#endif /* CONFIG_FIPS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800916#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 SSL_load_error_strings();
918 SSL_library_init();
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800919#ifndef OPENSSL_NO_SHA256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920 EVP_add_digest(EVP_sha256());
921#endif /* OPENSSL_NO_SHA256 */
922 /* TODO: if /dev/urandom is available, PRNG is seeded
923 * automatically. If this is not the case, random data should
924 * be added here. */
925
926#ifdef PKCS12_FUNCS
927#ifndef OPENSSL_NO_RC2
928 /*
929 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
930 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
931 * versions, but it looks like OpenSSL 1.0.0 does not do that
932 * anymore.
933 */
934 EVP_add_cipher(EVP_rc2_40_cbc());
935#endif /* OPENSSL_NO_RC2 */
936 PKCS12_PBE_add();
937#endif /* PKCS12_FUNCS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800938#endif /* < 1.1.0 */
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700939 } else {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700940 context = tls_context_new(conf);
941 if (context == NULL)
942 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 }
944 tls_openssl_ref_count++;
945
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800946 data = os_zalloc(sizeof(*data));
947 if (data)
948 ssl = SSL_CTX_new(SSLv23_method());
949 else
950 ssl = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700951 if (ssl == NULL) {
952 tls_openssl_ref_count--;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700953 if (context != tls_global)
954 os_free(context);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700955 if (tls_openssl_ref_count == 0) {
956 os_free(tls_global);
957 tls_global = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700958 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800959 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 return NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700961 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800962 data->ssl = ssl;
963 if (conf)
964 data->tls_session_lifetime = conf->tls_session_lifetime;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800966 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv2);
967 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv3);
968
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700969 SSL_CTX_set_info_callback(ssl, ssl_info_cb);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700970 SSL_CTX_set_app_data(ssl, context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800971 if (data->tls_session_lifetime > 0) {
972 SSL_CTX_set_quiet_shutdown(ssl, 1);
973 /*
974 * Set default context here. In practice, this will be replaced
975 * by the per-EAP method context in tls_connection_set_verify().
976 */
977 SSL_CTX_set_session_id_context(ssl, (u8 *) "hostapd", 7);
978 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_SERVER);
979 SSL_CTX_set_timeout(ssl, data->tls_session_lifetime);
980 SSL_CTX_sess_set_remove_cb(ssl, remove_session_cb);
981 } else {
982 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_OFF);
983 }
984
985 if (tls_ex_idx_session < 0) {
986 tls_ex_idx_session = SSL_SESSION_get_ex_new_index(
987 0, NULL, NULL, NULL, NULL);
988 if (tls_ex_idx_session < 0) {
989 tls_deinit(data);
990 return NULL;
991 }
992 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700993
994#ifndef OPENSSL_NO_ENGINE
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800995 wpa_printf(MSG_DEBUG, "ENGINE: Loading dynamic engine");
996 ERR_load_ENGINE_strings();
997 ENGINE_load_dynamic();
998
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700999 if (conf &&
1000 (conf->opensc_engine_path || conf->pkcs11_engine_path ||
1001 conf->pkcs11_module_path)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002 if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
1003 tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
1004 conf->pkcs11_module_path)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001005 tls_deinit(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006 return NULL;
1007 }
1008 }
1009#endif /* OPENSSL_NO_ENGINE */
1010
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001011 if (conf && conf->openssl_ciphers)
1012 ciphers = conf->openssl_ciphers;
1013 else
1014 ciphers = "DEFAULT:!EXP:!LOW";
1015 if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) {
1016 wpa_printf(MSG_ERROR,
1017 "OpenSSL: Failed to set cipher string '%s'",
1018 ciphers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001019 tls_deinit(data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001020 return NULL;
1021 }
1022
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001023 return data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024}
1025
1026
1027void tls_deinit(void *ssl_ctx)
1028{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001029 struct tls_data *data = ssl_ctx;
1030 SSL_CTX *ssl = data->ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001031 struct tls_context *context = SSL_CTX_get_app_data(ssl);
1032 if (context != tls_global)
1033 os_free(context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001034 if (data->tls_session_lifetime > 0)
1035 SSL_CTX_flush_sessions(ssl, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036 SSL_CTX_free(ssl);
1037
1038 tls_openssl_ref_count--;
1039 if (tls_openssl_ref_count == 0) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001040#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001041#ifndef OPENSSL_NO_ENGINE
1042 ENGINE_cleanup();
1043#endif /* OPENSSL_NO_ENGINE */
1044 CRYPTO_cleanup_all_ex_data();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001045 ERR_remove_thread_state(NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 ERR_free_strings();
1047 EVP_cleanup();
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001048#endif /* < 1.1.0 */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001049 os_free(tls_global->ocsp_stapling_response);
1050 tls_global->ocsp_stapling_response = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001051 os_free(tls_global);
1052 tls_global = NULL;
1053 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001054
1055 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056}
1057
1058
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001059#ifndef OPENSSL_NO_ENGINE
1060
1061/* Cryptoki return values */
1062#define CKR_PIN_INCORRECT 0x000000a0
1063#define CKR_PIN_INVALID 0x000000a1
1064#define CKR_PIN_LEN_RANGE 0x000000a2
1065
1066/* libp11 */
1067#define ERR_LIB_PKCS11 ERR_LIB_USER
1068
1069static int tls_is_pin_error(unsigned int err)
1070{
1071 return ERR_GET_LIB(err) == ERR_LIB_PKCS11 &&
1072 (ERR_GET_REASON(err) == CKR_PIN_INCORRECT ||
1073 ERR_GET_REASON(err) == CKR_PIN_INVALID ||
1074 ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE);
1075}
1076
1077#endif /* OPENSSL_NO_ENGINE */
1078
1079
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001080#ifdef ANDROID
1081/* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */
1082EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id);
1083#endif /* ANDROID */
1084
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
1086 const char *pin, const char *key_id,
1087 const char *cert_id, const char *ca_cert_id)
1088{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001089#if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL)
1090#if !defined(OPENSSL_NO_ENGINE)
1091#error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
1092#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001093 if (!key_id)
1094 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Adam Langley1eb02ed2015-04-21 19:00:05 -07001095 conn->engine = NULL;
1096 conn->private_key = EVP_PKEY_from_keystore(key_id);
1097 if (!conn->private_key) {
1098 wpa_printf(MSG_ERROR,
1099 "ENGINE: cannot load private key with id '%s' [%s]",
1100 key_id,
1101 ERR_error_string(ERR_get_error(), NULL));
1102 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1103 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001104#endif /* ANDROID && OPENSSL_IS_BORINGSSL */
Adam Langley1eb02ed2015-04-21 19:00:05 -07001105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106#ifndef OPENSSL_NO_ENGINE
1107 int ret = -1;
1108 if (engine_id == NULL) {
1109 wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
1110 return -1;
1111 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112
1113 ERR_clear_error();
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001114#ifdef ANDROID
1115 ENGINE_load_dynamic();
1116#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001117 conn->engine = ENGINE_by_id(engine_id);
1118 if (!conn->engine) {
1119 wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
1120 engine_id, ERR_error_string(ERR_get_error(), NULL));
1121 goto err;
1122 }
1123 if (ENGINE_init(conn->engine) != 1) {
1124 wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
1125 "(engine: %s) [%s]", engine_id,
1126 ERR_error_string(ERR_get_error(), NULL));
1127 goto err;
1128 }
1129 wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
1130
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001131#ifndef ANDROID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001132 if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
1134 ERR_error_string(ERR_get_error(), NULL));
1135 goto err;
1136 }
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001137#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001138 if (key_id) {
1139 /*
1140 * Ensure that the ENGINE does not attempt to use the OpenSSL
1141 * UI system to obtain a PIN, if we didn't provide one.
1142 */
1143 struct {
1144 const void *password;
1145 const char *prompt_info;
1146 } key_cb = { "", NULL };
1147
1148 /* load private key first in-case PIN is required for cert */
1149 conn->private_key = ENGINE_load_private_key(conn->engine,
1150 key_id, NULL,
1151 &key_cb);
1152 if (!conn->private_key) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001153 unsigned long err = ERR_get_error();
1154
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001155 wpa_printf(MSG_ERROR,
1156 "ENGINE: cannot load private key with id '%s' [%s]",
1157 key_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001158 ERR_error_string(err, NULL));
1159 if (tls_is_pin_error(err))
1160 ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
1161 else
1162 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001163 goto err;
1164 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165 }
1166
1167 /* handle a certificate and/or CA certificate */
1168 if (cert_id || ca_cert_id) {
1169 const char *cmd_name = "LOAD_CERT_CTRL";
1170
1171 /* test if the engine supports a LOAD_CERT_CTRL */
1172 if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
1173 0, (void *)cmd_name, NULL)) {
1174 wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
1175 " loading certificates");
1176 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1177 goto err;
1178 }
1179 }
1180
1181 return 0;
1182
1183err:
1184 if (conn->engine) {
1185 ENGINE_free(conn->engine);
1186 conn->engine = NULL;
1187 }
1188
1189 if (conn->private_key) {
1190 EVP_PKEY_free(conn->private_key);
1191 conn->private_key = NULL;
1192 }
1193
1194 return ret;
1195#else /* OPENSSL_NO_ENGINE */
1196 return 0;
1197#endif /* OPENSSL_NO_ENGINE */
1198}
1199
1200
1201static void tls_engine_deinit(struct tls_connection *conn)
1202{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001203#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001204 wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
1205 if (conn->private_key) {
1206 EVP_PKEY_free(conn->private_key);
1207 conn->private_key = NULL;
1208 }
1209 if (conn->engine) {
Adam Langley1eb02ed2015-04-21 19:00:05 -07001210#if !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211 ENGINE_finish(conn->engine);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001212#endif /* !OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213 conn->engine = NULL;
1214 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001215#endif /* ANDROID || !OPENSSL_NO_ENGINE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216}
1217
1218
1219int tls_get_errors(void *ssl_ctx)
1220{
1221 int count = 0;
1222 unsigned long err;
1223
1224 while ((err = ERR_get_error())) {
1225 wpa_printf(MSG_INFO, "TLS - SSL error: %s",
1226 ERR_error_string(err, NULL));
1227 count++;
1228 }
1229
1230 return count;
1231}
1232
Jouni Malinen26af48b2014-04-09 13:02:53 +03001233
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001234static const char * openssl_content_type(int content_type)
1235{
1236 switch (content_type) {
1237 case 20:
1238 return "change cipher spec";
1239 case 21:
1240 return "alert";
1241 case 22:
1242 return "handshake";
1243 case 23:
1244 return "application data";
1245 case 24:
1246 return "heartbeat";
1247 case 256:
1248 return "TLS header info"; /* pseudo content type */
1249 default:
1250 return "?";
1251 }
1252}
1253
1254
1255static const char * openssl_handshake_type(int content_type, const u8 *buf,
1256 size_t len)
1257{
1258 if (content_type != 22 || !buf || len == 0)
1259 return "";
1260 switch (buf[0]) {
1261 case 0:
1262 return "hello request";
1263 case 1:
1264 return "client hello";
1265 case 2:
1266 return "server hello";
1267 case 4:
1268 return "new session ticket";
1269 case 11:
1270 return "certificate";
1271 case 12:
1272 return "server key exchange";
1273 case 13:
1274 return "certificate request";
1275 case 14:
1276 return "server hello done";
1277 case 15:
1278 return "certificate verify";
1279 case 16:
1280 return "client key exchange";
1281 case 20:
1282 return "finished";
1283 case 21:
1284 return "certificate url";
1285 case 22:
1286 return "certificate status";
1287 default:
1288 return "?";
1289 }
1290}
1291
1292
Jouni Malinen26af48b2014-04-09 13:02:53 +03001293static void tls_msg_cb(int write_p, int version, int content_type,
1294 const void *buf, size_t len, SSL *ssl, void *arg)
1295{
1296 struct tls_connection *conn = arg;
1297 const u8 *pos = buf;
1298
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001299 if (write_p == 2) {
1300 wpa_printf(MSG_DEBUG,
1301 "OpenSSL: session ver=0x%x content_type=%d",
1302 version, content_type);
1303 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len);
1304 return;
1305 }
1306
1307 wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
1308 write_p ? "TX" : "RX", version, content_type,
1309 openssl_content_type(content_type),
1310 openssl_handshake_type(content_type, buf, len));
Jouni Malinen26af48b2014-04-09 13:02:53 +03001311 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len);
1312 if (content_type == 24 && len >= 3 && pos[0] == 1) {
1313 size_t payload_len = WPA_GET_BE16(pos + 1);
1314 if (payload_len + 3 > len) {
1315 wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected");
1316 conn->invalid_hb_used = 1;
1317 }
1318 }
1319}
1320
1321
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322struct tls_connection * tls_connection_init(void *ssl_ctx)
1323{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001324 struct tls_data *data = ssl_ctx;
1325 SSL_CTX *ssl = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001326 struct tls_connection *conn;
1327 long options;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001328 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329
1330 conn = os_zalloc(sizeof(*conn));
1331 if (conn == NULL)
1332 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001333 conn->ssl_ctx = ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 conn->ssl = SSL_new(ssl);
1335 if (conn->ssl == NULL) {
1336 tls_show_errors(MSG_INFO, __func__,
1337 "Failed to initialize new SSL connection");
1338 os_free(conn);
1339 return NULL;
1340 }
1341
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001342 conn->context = context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001343 SSL_set_app_data(conn->ssl, conn);
Jouni Malinen26af48b2014-04-09 13:02:53 +03001344 SSL_set_msg_callback(conn->ssl, tls_msg_cb);
1345 SSL_set_msg_callback_arg(conn->ssl, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346 options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
1347 SSL_OP_SINGLE_DH_USE;
1348#ifdef SSL_OP_NO_COMPRESSION
1349 options |= SSL_OP_NO_COMPRESSION;
1350#endif /* SSL_OP_NO_COMPRESSION */
1351 SSL_set_options(conn->ssl, options);
1352
1353 conn->ssl_in = BIO_new(BIO_s_mem());
1354 if (!conn->ssl_in) {
1355 tls_show_errors(MSG_INFO, __func__,
1356 "Failed to create a new BIO for ssl_in");
1357 SSL_free(conn->ssl);
1358 os_free(conn);
1359 return NULL;
1360 }
1361
1362 conn->ssl_out = BIO_new(BIO_s_mem());
1363 if (!conn->ssl_out) {
1364 tls_show_errors(MSG_INFO, __func__,
1365 "Failed to create a new BIO for ssl_out");
1366 SSL_free(conn->ssl);
1367 BIO_free(conn->ssl_in);
1368 os_free(conn);
1369 return NULL;
1370 }
1371
1372 SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
1373
1374 return conn;
1375}
1376
1377
1378void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
1379{
1380 if (conn == NULL)
1381 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001382 if (conn->success_data) {
1383 /*
1384 * Make sure ssl_clear_bad_session() does not remove this
1385 * session.
1386 */
1387 SSL_set_quiet_shutdown(conn->ssl, 1);
1388 SSL_shutdown(conn->ssl);
1389 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001390 SSL_free(conn->ssl);
1391 tls_engine_deinit(conn);
1392 os_free(conn->subject_match);
1393 os_free(conn->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001394 os_free(conn->suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001395 os_free(conn->domain_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 os_free(conn->session_ticket);
1397 os_free(conn);
1398}
1399
1400
1401int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
1402{
1403 return conn ? SSL_is_init_finished(conn->ssl) : 0;
1404}
1405
1406
1407int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
1408{
1409 if (conn == NULL)
1410 return -1;
1411
1412 /* Shutdown previous TLS connection without notifying the peer
1413 * because the connection was already terminated in practice
1414 * and "close notify" shutdown alert would confuse AS. */
1415 SSL_set_quiet_shutdown(conn->ssl, 1);
1416 SSL_shutdown(conn->ssl);
Jouni Malinenf291c682015-08-17 22:50:41 +03001417 return SSL_clear(conn->ssl) == 1 ? 0 : -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418}
1419
1420
1421static int tls_match_altsubject_component(X509 *cert, int type,
1422 const char *value, size_t len)
1423{
1424 GENERAL_NAME *gen;
1425 void *ext;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001426 int found = 0;
1427 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001428
1429 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1430
1431 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1432 gen = sk_GENERAL_NAME_value(ext, i);
1433 if (gen->type != type)
1434 continue;
1435 if (os_strlen((char *) gen->d.ia5->data) == len &&
1436 os_memcmp(value, gen->d.ia5->data, len) == 0)
1437 found++;
1438 }
1439
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001440 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
1441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442 return found;
1443}
1444
1445
1446static int tls_match_altsubject(X509 *cert, const char *match)
1447{
1448 int type;
1449 const char *pos, *end;
1450 size_t len;
1451
1452 pos = match;
1453 do {
1454 if (os_strncmp(pos, "EMAIL:", 6) == 0) {
1455 type = GEN_EMAIL;
1456 pos += 6;
1457 } else if (os_strncmp(pos, "DNS:", 4) == 0) {
1458 type = GEN_DNS;
1459 pos += 4;
1460 } else if (os_strncmp(pos, "URI:", 4) == 0) {
1461 type = GEN_URI;
1462 pos += 4;
1463 } else {
1464 wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
1465 "match '%s'", pos);
1466 return 0;
1467 }
1468 end = os_strchr(pos, ';');
1469 while (end) {
1470 if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
1471 os_strncmp(end + 1, "DNS:", 4) == 0 ||
1472 os_strncmp(end + 1, "URI:", 4) == 0)
1473 break;
1474 end = os_strchr(end + 1, ';');
1475 }
1476 if (end)
1477 len = end - pos;
1478 else
1479 len = os_strlen(pos);
1480 if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1481 return 1;
1482 pos = end + 1;
1483 } while (end);
1484
1485 return 0;
1486}
1487
1488
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001489#ifndef CONFIG_NATIVE_WINDOWS
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001490static int domain_suffix_match(const u8 *val, size_t len, const char *match,
1491 int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001492{
1493 size_t i, match_len;
1494
1495 /* Check for embedded nuls that could mess up suffix matching */
1496 for (i = 0; i < len; i++) {
1497 if (val[i] == '\0') {
1498 wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject");
1499 return 0;
1500 }
1501 }
1502
1503 match_len = os_strlen(match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001504 if (match_len > len || (full && match_len != len))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001505 return 0;
1506
1507 if (os_strncasecmp((const char *) val + len - match_len, match,
1508 match_len) != 0)
1509 return 0; /* no match */
1510
1511 if (match_len == len)
1512 return 1; /* exact match */
1513
1514 if (val[len - match_len - 1] == '.')
1515 return 1; /* full label match completes suffix match */
1516
1517 wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match");
1518 return 0;
1519}
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001520#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001521
1522
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001523static int tls_match_suffix(X509 *cert, const char *match, int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001524{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001525#ifdef CONFIG_NATIVE_WINDOWS
1526 /* wincrypt.h has conflicting X509_NAME definition */
1527 return -1;
1528#else /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001529 GENERAL_NAME *gen;
1530 void *ext;
1531 int i;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001532 stack_index_t j;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001533 int dns_name = 0;
1534 X509_NAME *name;
1535
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001536 wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s",
1537 full ? "": "suffix ", match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001538
1539 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1540
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001541 for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) {
1542 gen = sk_GENERAL_NAME_value(ext, j);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001543 if (gen->type != GEN_DNS)
1544 continue;
1545 dns_name++;
1546 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName",
1547 gen->d.dNSName->data,
1548 gen->d.dNSName->length);
1549 if (domain_suffix_match(gen->d.dNSName->data,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001550 gen->d.dNSName->length, match, full) ==
1551 1) {
1552 wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found",
1553 full ? "Match" : "Suffix match");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001554 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001555 return 1;
1556 }
1557 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001558 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001559
1560 if (dns_name) {
1561 wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched");
1562 return 0;
1563 }
1564
1565 name = X509_get_subject_name(cert);
1566 i = -1;
1567 for (;;) {
1568 X509_NAME_ENTRY *e;
1569 ASN1_STRING *cn;
1570
1571 i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
1572 if (i == -1)
1573 break;
1574 e = X509_NAME_get_entry(name, i);
1575 if (e == NULL)
1576 continue;
1577 cn = X509_NAME_ENTRY_get_data(e);
1578 if (cn == NULL)
1579 continue;
1580 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName",
1581 cn->data, cn->length);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001582 if (domain_suffix_match(cn->data, cn->length, match, full) == 1)
1583 {
1584 wpa_printf(MSG_DEBUG, "TLS: %s in commonName found",
1585 full ? "Match" : "Suffix match");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001586 return 1;
1587 }
1588 }
1589
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001590 wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found",
1591 full ? "": "suffix ");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001592 return 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001593#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001594}
1595
1596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001597static enum tls_fail_reason openssl_tls_fail_reason(int err)
1598{
1599 switch (err) {
1600 case X509_V_ERR_CERT_REVOKED:
1601 return TLS_FAIL_REVOKED;
1602 case X509_V_ERR_CERT_NOT_YET_VALID:
1603 case X509_V_ERR_CRL_NOT_YET_VALID:
1604 return TLS_FAIL_NOT_YET_VALID;
1605 case X509_V_ERR_CERT_HAS_EXPIRED:
1606 case X509_V_ERR_CRL_HAS_EXPIRED:
1607 return TLS_FAIL_EXPIRED;
1608 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
1609 case X509_V_ERR_UNABLE_TO_GET_CRL:
1610 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
1611 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1612 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1613 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1614 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1615 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
1616 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
1617 case X509_V_ERR_INVALID_CA:
1618 return TLS_FAIL_UNTRUSTED;
1619 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
1620 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
1621 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
1622 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
1623 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
1624 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
1625 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
1626 case X509_V_ERR_CERT_UNTRUSTED:
1627 case X509_V_ERR_CERT_REJECTED:
1628 return TLS_FAIL_BAD_CERTIFICATE;
1629 default:
1630 return TLS_FAIL_UNSPECIFIED;
1631 }
1632}
1633
1634
1635static struct wpabuf * get_x509_cert(X509 *cert)
1636{
1637 struct wpabuf *buf;
1638 u8 *tmp;
1639
1640 int cert_len = i2d_X509(cert, NULL);
1641 if (cert_len <= 0)
1642 return NULL;
1643
1644 buf = wpabuf_alloc(cert_len);
1645 if (buf == NULL)
1646 return NULL;
1647
1648 tmp = wpabuf_put(buf, cert_len);
1649 i2d_X509(cert, &tmp);
1650 return buf;
1651}
1652
1653
1654static void openssl_tls_fail_event(struct tls_connection *conn,
1655 X509 *err_cert, int err, int depth,
1656 const char *subject, const char *err_str,
1657 enum tls_fail_reason reason)
1658{
1659 union tls_event_data ev;
1660 struct wpabuf *cert = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001661 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001663 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001664 return;
1665
1666 cert = get_x509_cert(err_cert);
1667 os_memset(&ev, 0, sizeof(ev));
1668 ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ?
1669 reason : openssl_tls_fail_reason(err);
1670 ev.cert_fail.depth = depth;
1671 ev.cert_fail.subject = subject;
1672 ev.cert_fail.reason_txt = err_str;
1673 ev.cert_fail.cert = cert;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001674 context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 wpabuf_free(cert);
1676}
1677
1678
1679static void openssl_tls_cert_event(struct tls_connection *conn,
1680 X509 *err_cert, int depth,
1681 const char *subject)
1682{
1683 struct wpabuf *cert = NULL;
1684 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001685 struct tls_context *context = conn->context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001686 char *altsubject[TLS_MAX_ALT_SUBJECT];
1687 int alt, num_altsubject = 0;
1688 GENERAL_NAME *gen;
1689 void *ext;
1690 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691#ifdef CONFIG_SHA256
1692 u8 hash[32];
1693#endif /* CONFIG_SHA256 */
1694
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001695 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696 return;
1697
1698 os_memset(&ev, 0, sizeof(ev));
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001699 if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) ||
1700 context->cert_in_cb) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 cert = get_x509_cert(err_cert);
1702 ev.peer_cert.cert = cert;
1703 }
1704#ifdef CONFIG_SHA256
1705 if (cert) {
1706 const u8 *addr[1];
1707 size_t len[1];
1708 addr[0] = wpabuf_head(cert);
1709 len[0] = wpabuf_len(cert);
1710 if (sha256_vector(1, addr, len, hash) == 0) {
1711 ev.peer_cert.hash = hash;
1712 ev.peer_cert.hash_len = sizeof(hash);
1713 }
1714 }
1715#endif /* CONFIG_SHA256 */
1716 ev.peer_cert.depth = depth;
1717 ev.peer_cert.subject = subject;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001718
1719 ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL);
1720 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1721 char *pos;
1722
1723 if (num_altsubject == TLS_MAX_ALT_SUBJECT)
1724 break;
1725 gen = sk_GENERAL_NAME_value(ext, i);
1726 if (gen->type != GEN_EMAIL &&
1727 gen->type != GEN_DNS &&
1728 gen->type != GEN_URI)
1729 continue;
1730
1731 pos = os_malloc(10 + gen->d.ia5->length + 1);
1732 if (pos == NULL)
1733 break;
1734 altsubject[num_altsubject++] = pos;
1735
1736 switch (gen->type) {
1737 case GEN_EMAIL:
1738 os_memcpy(pos, "EMAIL:", 6);
1739 pos += 6;
1740 break;
1741 case GEN_DNS:
1742 os_memcpy(pos, "DNS:", 4);
1743 pos += 4;
1744 break;
1745 case GEN_URI:
1746 os_memcpy(pos, "URI:", 4);
1747 pos += 4;
1748 break;
1749 }
1750
1751 os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length);
1752 pos += gen->d.ia5->length;
1753 *pos = '\0';
1754 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001755 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001756
1757 for (alt = 0; alt < num_altsubject; alt++)
1758 ev.peer_cert.altsubject[alt] = altsubject[alt];
1759 ev.peer_cert.num_altsubject = num_altsubject;
1760
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001761 context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762 wpabuf_free(cert);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001763 for (alt = 0; alt < num_altsubject; alt++)
1764 os_free(altsubject[alt]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765}
1766
1767
1768static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
1769{
1770 char buf[256];
1771 X509 *err_cert;
1772 int err, depth;
1773 SSL *ssl;
1774 struct tls_connection *conn;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001775 struct tls_context *context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001776 char *match, *altmatch, *suffix_match, *domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001777 const char *err_str;
1778
1779 err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001780 if (!err_cert)
1781 return 0;
1782
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783 err = X509_STORE_CTX_get_error(x509_ctx);
1784 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
1785 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1786 SSL_get_ex_data_X509_STORE_CTX_idx());
1787 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
1788
1789 conn = SSL_get_app_data(ssl);
1790 if (conn == NULL)
1791 return 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001792
1793 if (depth == 0)
1794 conn->peer_cert = err_cert;
1795 else if (depth == 1)
1796 conn->peer_issuer = err_cert;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001797 else if (depth == 2)
1798 conn->peer_issuer_issuer = err_cert;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001799
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001800 context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801 match = conn->subject_match;
1802 altmatch = conn->altsubject_match;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001803 suffix_match = conn->suffix_match;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001804 domain_match = conn->domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001805
1806 if (!preverify_ok && !conn->ca_cert_verify)
1807 preverify_ok = 1;
1808 if (!preverify_ok && depth > 0 && conn->server_cert_only)
1809 preverify_ok = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001810 if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) &&
1811 (err == X509_V_ERR_CERT_HAS_EXPIRED ||
1812 err == X509_V_ERR_CERT_NOT_YET_VALID)) {
1813 wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity "
1814 "time mismatch");
1815 preverify_ok = 1;
1816 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001817
1818 err_str = X509_verify_cert_error_string(err);
1819
1820#ifdef CONFIG_SHA256
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001821 /*
1822 * Do not require preverify_ok so we can explicity allow otherwise
1823 * invalid pinned server certificates.
1824 */
1825 if (depth == 0 && conn->server_cert_only) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001826 struct wpabuf *cert;
1827 cert = get_x509_cert(err_cert);
1828 if (!cert) {
1829 wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch "
1830 "server certificate data");
1831 preverify_ok = 0;
1832 } else {
1833 u8 hash[32];
1834 const u8 *addr[1];
1835 size_t len[1];
1836 addr[0] = wpabuf_head(cert);
1837 len[0] = wpabuf_len(cert);
1838 if (sha256_vector(1, addr, len, hash) < 0 ||
1839 os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
1840 err_str = "Server certificate mismatch";
1841 err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
1842 preverify_ok = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001843 } else if (!preverify_ok) {
1844 /*
1845 * Certificate matches pinned certificate, allow
1846 * regardless of other problems.
1847 */
1848 wpa_printf(MSG_DEBUG,
1849 "OpenSSL: Ignore validation issues for a pinned server certificate");
1850 preverify_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001851 }
1852 wpabuf_free(cert);
1853 }
1854 }
1855#endif /* CONFIG_SHA256 */
1856
1857 if (!preverify_ok) {
1858 wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
1859 " error %d (%s) depth %d for '%s'", err, err_str,
1860 depth, buf);
1861 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1862 err_str, TLS_FAIL_UNSPECIFIED);
1863 return preverify_ok;
1864 }
1865
1866 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d "
1867 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
1868 preverify_ok, err, err_str,
1869 conn->ca_cert_verify, depth, buf);
1870 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
1871 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
1872 "match with '%s'", buf, match);
1873 preverify_ok = 0;
1874 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1875 "Subject mismatch",
1876 TLS_FAIL_SUBJECT_MISMATCH);
1877 } else if (depth == 0 && altmatch &&
1878 !tls_match_altsubject(err_cert, altmatch)) {
1879 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
1880 "'%s' not found", altmatch);
1881 preverify_ok = 0;
1882 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1883 "AltSubject mismatch",
1884 TLS_FAIL_ALTSUBJECT_MISMATCH);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001885 } else if (depth == 0 && suffix_match &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001886 !tls_match_suffix(err_cert, suffix_match, 0)) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001887 wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found",
1888 suffix_match);
1889 preverify_ok = 0;
1890 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1891 "Domain suffix mismatch",
1892 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001893 } else if (depth == 0 && domain_match &&
1894 !tls_match_suffix(err_cert, domain_match, 1)) {
1895 wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found",
1896 domain_match);
1897 preverify_ok = 0;
1898 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1899 "Domain mismatch",
1900 TLS_FAIL_DOMAIN_MISMATCH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001901 } else
1902 openssl_tls_cert_event(conn, err_cert, depth, buf);
1903
1904 if (conn->cert_probe && preverify_ok && depth == 0) {
1905 wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate "
1906 "on probe-only run");
1907 preverify_ok = 0;
1908 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1909 "Server certificate chain probe",
1910 TLS_FAIL_SERVER_CHAIN_PROBE);
1911 }
1912
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001913#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001914 if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) &&
1915 preverify_ok) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001916 enum ocsp_result res;
1917
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001918 res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert,
1919 conn->peer_issuer,
1920 conn->peer_issuer_issuer);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001921 if (res == OCSP_REVOKED) {
1922 preverify_ok = 0;
1923 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1924 "certificate revoked",
1925 TLS_FAIL_REVOKED);
1926 if (err == X509_V_OK)
1927 X509_STORE_CTX_set_error(
1928 x509_ctx, X509_V_ERR_CERT_REVOKED);
1929 } else if (res != OCSP_GOOD &&
1930 (conn->flags & TLS_CONN_REQUIRE_OCSP)) {
1931 preverify_ok = 0;
1932 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1933 "bad certificate status response",
1934 TLS_FAIL_UNSPECIFIED);
1935 }
1936 }
1937#endif /* OPENSSL_IS_BORINGSSL */
1938
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001939 if (depth == 0 && preverify_ok && context->event_cb != NULL)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001940 context->event_cb(context->cb_ctx,
1941 TLS_CERT_CHAIN_SUCCESS, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001942
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001943 return preverify_ok;
1944}
1945
1946
1947#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001948static int tls_load_ca_der(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001950 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001951 X509_LOOKUP *lookup;
1952 int ret = 0;
1953
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001954 lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955 X509_LOOKUP_file());
1956 if (lookup == NULL) {
1957 tls_show_errors(MSG_WARNING, __func__,
1958 "Failed add lookup for X509 store");
1959 return -1;
1960 }
1961
1962 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
1963 unsigned long err = ERR_peek_error();
1964 tls_show_errors(MSG_WARNING, __func__,
1965 "Failed load CA in DER format");
1966 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
1967 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1968 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
1969 "cert already in hash table error",
1970 __func__);
1971 } else
1972 ret = -1;
1973 }
1974
1975 return ret;
1976}
1977#endif /* OPENSSL_NO_STDIO */
1978
1979
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001980static int tls_connection_ca_cert(struct tls_data *data,
1981 struct tls_connection *conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982 const char *ca_cert, const u8 *ca_cert_blob,
1983 size_t ca_cert_blob_len, const char *ca_path)
1984{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001985 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001986 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001987
1988 /*
1989 * Remove previously configured trusted CA certificates before adding
1990 * new ones.
1991 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001992 store = X509_STORE_new();
1993 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
1995 "certificate store", __func__);
1996 return -1;
1997 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001998 SSL_CTX_set_cert_store(ssl_ctx, store);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999
2000 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2001 conn->ca_cert_verify = 1;
2002
2003 if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
2004 wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate "
2005 "chain");
2006 conn->cert_probe = 1;
2007 conn->ca_cert_verify = 0;
2008 return 0;
2009 }
2010
2011 if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
2012#ifdef CONFIG_SHA256
2013 const char *pos = ca_cert + 7;
2014 if (os_strncmp(pos, "server/sha256/", 14) != 0) {
2015 wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert "
2016 "hash value '%s'", ca_cert);
2017 return -1;
2018 }
2019 pos += 14;
2020 if (os_strlen(pos) != 32 * 2) {
2021 wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 "
2022 "hash length in ca_cert '%s'", ca_cert);
2023 return -1;
2024 }
2025 if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
2026 wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash "
2027 "value in ca_cert '%s'", ca_cert);
2028 return -1;
2029 }
2030 conn->server_cert_only = 1;
2031 wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server "
2032 "certificate match");
2033 return 0;
2034#else /* CONFIG_SHA256 */
2035 wpa_printf(MSG_INFO, "No SHA256 included in the build - "
2036 "cannot validate server certificate hash");
2037 return -1;
2038#endif /* CONFIG_SHA256 */
2039 }
2040
2041 if (ca_cert_blob) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002042 X509 *cert = d2i_X509(NULL,
2043 (const unsigned char **) &ca_cert_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044 ca_cert_blob_len);
2045 if (cert == NULL) {
2046 tls_show_errors(MSG_WARNING, __func__,
2047 "Failed to parse ca_cert_blob");
2048 return -1;
2049 }
2050
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002051 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
2052 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053 unsigned long err = ERR_peek_error();
2054 tls_show_errors(MSG_WARNING, __func__,
2055 "Failed to add ca_cert_blob to "
2056 "certificate store");
2057 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2058 ERR_GET_REASON(err) ==
2059 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2060 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2061 "cert already in hash table error",
2062 __func__);
2063 } else {
2064 X509_free(cert);
2065 return -1;
2066 }
2067 }
2068 X509_free(cert);
2069 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
2070 "to certificate store", __func__);
2071 return 0;
2072 }
2073
2074#ifdef ANDROID
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002075 /* Single alias */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002076 if (ca_cert && os_strncmp("keystore://", ca_cert, 11) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002077 if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002078 &ca_cert[11]) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002080 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2081 return 0;
2082 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002083
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002084 /* Multiple aliases separated by space */
2085 if (ca_cert && os_strncmp("keystores://", ca_cert, 12) == 0) {
2086 char *aliases = os_strdup(&ca_cert[12]);
2087 const char *delim = " ";
2088 int rc = 0;
2089 char *savedptr;
2090 char *alias;
2091
2092 if (!aliases)
2093 return -1;
2094 alias = strtok_r(aliases, delim, &savedptr);
2095 for (; alias; alias = strtok_r(NULL, delim, &savedptr)) {
2096 if (tls_add_ca_from_keystore_encoded(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002097 SSL_CTX_get_cert_store(ssl_ctx), alias)) {
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002098 wpa_printf(MSG_WARNING,
2099 "OpenSSL: %s - Failed to add ca_cert %s from keystore",
2100 __func__, alias);
2101 rc = -1;
2102 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103 }
2104 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002105 os_free(aliases);
2106 if (rc)
2107 return rc;
2108
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2110 return 0;
2111 }
2112#endif /* ANDROID */
2113
2114#ifdef CONFIG_NATIVE_WINDOWS
2115 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
2116 0) {
2117 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
2118 "system certificate store");
2119 return 0;
2120 }
2121#endif /* CONFIG_NATIVE_WINDOWS */
2122
2123 if (ca_cert || ca_path) {
2124#ifndef OPENSSL_NO_STDIO
2125 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
2126 1) {
2127 tls_show_errors(MSG_WARNING, __func__,
2128 "Failed to load root certificates");
2129 if (ca_cert &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002130 tls_load_ca_der(data, ca_cert) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002131 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
2132 "DER format CA certificate",
2133 __func__);
2134 } else
2135 return -1;
2136 } else {
2137 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2138 "certificate(s) loaded");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002139 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002140 }
2141#else /* OPENSSL_NO_STDIO */
2142 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2143 __func__);
2144 return -1;
2145#endif /* OPENSSL_NO_STDIO */
2146 } else {
2147 /* No ca_cert configured - do not try to verify server
2148 * certificate */
2149 conn->ca_cert_verify = 0;
2150 }
2151
2152 return 0;
2153}
2154
2155
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002156static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002157{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002158 SSL_CTX *ssl_ctx = data->ssl;
2159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160 if (ca_cert) {
2161 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
2162 {
2163 tls_show_errors(MSG_WARNING, __func__,
2164 "Failed to load root certificates");
2165 return -1;
2166 }
2167
2168 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2169 "certificate(s) loaded");
2170
2171#ifndef OPENSSL_NO_STDIO
2172 /* Add the same CAs to the client certificate requests */
2173 SSL_CTX_set_client_CA_list(ssl_ctx,
2174 SSL_load_client_CA_file(ca_cert));
2175#endif /* OPENSSL_NO_STDIO */
2176 }
2177
2178 return 0;
2179}
2180
2181
2182int tls_global_set_verify(void *ssl_ctx, int check_crl)
2183{
2184 int flags;
2185
2186 if (check_crl) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002187 struct tls_data *data = ssl_ctx;
2188 X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002189 if (cs == NULL) {
2190 tls_show_errors(MSG_INFO, __func__, "Failed to get "
2191 "certificate store when enabling "
2192 "check_crl");
2193 return -1;
2194 }
2195 flags = X509_V_FLAG_CRL_CHECK;
2196 if (check_crl == 2)
2197 flags |= X509_V_FLAG_CRL_CHECK_ALL;
2198 X509_STORE_set_flags(cs, flags);
2199 }
2200 return 0;
2201}
2202
2203
2204static int tls_connection_set_subject_match(struct tls_connection *conn,
2205 const char *subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07002206 const char *altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002207 const char *suffix_match,
2208 const char *domain_match)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002209{
2210 os_free(conn->subject_match);
2211 conn->subject_match = NULL;
2212 if (subject_match) {
2213 conn->subject_match = os_strdup(subject_match);
2214 if (conn->subject_match == NULL)
2215 return -1;
2216 }
2217
2218 os_free(conn->altsubject_match);
2219 conn->altsubject_match = NULL;
2220 if (altsubject_match) {
2221 conn->altsubject_match = os_strdup(altsubject_match);
2222 if (conn->altsubject_match == NULL)
2223 return -1;
2224 }
2225
Dmitry Shmidt051af732013-10-22 13:52:46 -07002226 os_free(conn->suffix_match);
2227 conn->suffix_match = NULL;
2228 if (suffix_match) {
2229 conn->suffix_match = os_strdup(suffix_match);
2230 if (conn->suffix_match == NULL)
2231 return -1;
2232 }
2233
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002234 os_free(conn->domain_match);
2235 conn->domain_match = NULL;
2236 if (domain_match) {
2237 conn->domain_match = os_strdup(domain_match);
2238 if (conn->domain_match == NULL)
2239 return -1;
2240 }
2241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002242 return 0;
2243}
2244
2245
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002246static void tls_set_conn_flags(SSL *ssl, unsigned int flags)
2247{
2248#ifdef SSL_OP_NO_TICKET
2249 if (flags & TLS_CONN_DISABLE_SESSION_TICKET)
2250 SSL_set_options(ssl, SSL_OP_NO_TICKET);
2251#ifdef SSL_clear_options
2252 else
2253 SSL_clear_options(ssl, SSL_OP_NO_TICKET);
2254#endif /* SSL_clear_options */
2255#endif /* SSL_OP_NO_TICKET */
2256
2257#ifdef SSL_OP_NO_TLSv1
2258 if (flags & TLS_CONN_DISABLE_TLSv1_0)
2259 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
2260 else
2261 SSL_clear_options(ssl, SSL_OP_NO_TLSv1);
2262#endif /* SSL_OP_NO_TLSv1 */
2263#ifdef SSL_OP_NO_TLSv1_1
2264 if (flags & TLS_CONN_DISABLE_TLSv1_1)
2265 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
2266 else
2267 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1);
2268#endif /* SSL_OP_NO_TLSv1_1 */
2269#ifdef SSL_OP_NO_TLSv1_2
2270 if (flags & TLS_CONN_DISABLE_TLSv1_2)
2271 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
2272 else
2273 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2);
2274#endif /* SSL_OP_NO_TLSv1_2 */
2275}
2276
2277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002278int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002279 int verify_peer, unsigned int flags,
2280 const u8 *session_ctx, size_t session_ctx_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002281{
2282 static int counter = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002283 struct tls_data *data = ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002284
2285 if (conn == NULL)
2286 return -1;
2287
2288 if (verify_peer) {
2289 conn->ca_cert_verify = 1;
2290 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
2291 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
2292 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
2293 } else {
2294 conn->ca_cert_verify = 0;
2295 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
2296 }
2297
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002298 tls_set_conn_flags(conn->ssl, flags);
2299 conn->flags = flags;
2300
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002301 SSL_set_accept_state(conn->ssl);
2302
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002303 if (data->tls_session_lifetime == 0) {
2304 /*
2305 * Set session id context to a unique value to make sure
2306 * session resumption cannot be used either through session
2307 * caching or TLS ticket extension.
2308 */
2309 counter++;
2310 SSL_set_session_id_context(conn->ssl,
2311 (const unsigned char *) &counter,
2312 sizeof(counter));
2313 } else if (session_ctx) {
2314 SSL_set_session_id_context(conn->ssl, session_ctx,
2315 session_ctx_len);
2316 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002317
2318 return 0;
2319}
2320
2321
2322static int tls_connection_client_cert(struct tls_connection *conn,
2323 const char *client_cert,
2324 const u8 *client_cert_blob,
2325 size_t client_cert_blob_len)
2326{
2327 if (client_cert == NULL && client_cert_blob == NULL)
2328 return 0;
2329
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002330#ifdef PKCS12_FUNCS
2331#if OPENSSL_VERSION_NUMBER < 0x10002000L
2332 /*
2333 * Clear previously set extra chain certificates, if any, from PKCS#12
2334 * processing in tls_parse_pkcs12() to allow OpenSSL to build a new
2335 * chain properly.
2336 */
2337 SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx);
2338#endif /* OPENSSL_VERSION_NUMBER < 0x10002000L */
2339#endif /* PKCS12_FUNCS */
2340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002341 if (client_cert_blob &&
2342 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
2343 client_cert_blob_len) == 1) {
2344 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
2345 "OK");
2346 return 0;
2347 } else if (client_cert_blob) {
2348 tls_show_errors(MSG_DEBUG, __func__,
2349 "SSL_use_certificate_ASN1 failed");
2350 }
2351
2352 if (client_cert == NULL)
2353 return -1;
2354
2355#ifdef ANDROID
2356 if (os_strncmp("keystore://", client_cert, 11) == 0) {
2357 BIO *bio = BIO_from_keystore(&client_cert[11]);
2358 X509 *x509 = NULL;
2359 int ret = -1;
2360 if (bio) {
2361 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
2362 BIO_free(bio);
2363 }
2364 if (x509) {
2365 if (SSL_use_certificate(conn->ssl, x509) == 1)
2366 ret = 0;
2367 X509_free(x509);
2368 }
2369 return ret;
2370 }
2371#endif /* ANDROID */
2372
2373#ifndef OPENSSL_NO_STDIO
2374 if (SSL_use_certificate_file(conn->ssl, client_cert,
2375 SSL_FILETYPE_ASN1) == 1) {
2376 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
2377 " --> OK");
2378 return 0;
2379 }
2380
2381 if (SSL_use_certificate_file(conn->ssl, client_cert,
2382 SSL_FILETYPE_PEM) == 1) {
2383 ERR_clear_error();
2384 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
2385 " --> OK");
2386 return 0;
2387 }
2388
2389 tls_show_errors(MSG_DEBUG, __func__,
2390 "SSL_use_certificate_file failed");
2391#else /* OPENSSL_NO_STDIO */
2392 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
2393#endif /* OPENSSL_NO_STDIO */
2394
2395 return -1;
2396}
2397
2398
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002399static int tls_global_client_cert(struct tls_data *data,
2400 const char *client_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002401{
2402#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002403 SSL_CTX *ssl_ctx = data->ssl;
2404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002405 if (client_cert == NULL)
2406 return 0;
2407
2408 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
2409 SSL_FILETYPE_ASN1) != 1 &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002410 SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002411 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
2412 SSL_FILETYPE_PEM) != 1) {
2413 tls_show_errors(MSG_INFO, __func__,
2414 "Failed to load client certificate");
2415 return -1;
2416 }
2417 return 0;
2418#else /* OPENSSL_NO_STDIO */
2419 if (client_cert == NULL)
2420 return 0;
2421 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
2422 return -1;
2423#endif /* OPENSSL_NO_STDIO */
2424}
2425
2426
2427static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
2428{
2429 if (password == NULL) {
2430 return 0;
2431 }
2432 os_strlcpy(buf, (char *) password, size);
2433 return os_strlen(buf);
2434}
2435
2436
2437#ifdef PKCS12_FUNCS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002438static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002439 const char *passwd)
2440{
2441 EVP_PKEY *pkey;
2442 X509 *cert;
2443 STACK_OF(X509) *certs;
2444 int res = 0;
2445 char buf[256];
2446
2447 pkey = NULL;
2448 cert = NULL;
2449 certs = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002450 if (!passwd)
2451 passwd = "";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002452 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
2453 tls_show_errors(MSG_DEBUG, __func__,
2454 "Failed to parse PKCS12 file");
2455 PKCS12_free(p12);
2456 return -1;
2457 }
2458 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
2459
2460 if (cert) {
2461 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2462 sizeof(buf));
2463 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
2464 "subject='%s'", buf);
2465 if (ssl) {
2466 if (SSL_use_certificate(ssl, cert) != 1)
2467 res = -1;
2468 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002469 if (SSL_CTX_use_certificate(data->ssl, cert) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002470 res = -1;
2471 }
2472 X509_free(cert);
2473 }
2474
2475 if (pkey) {
2476 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
2477 if (ssl) {
2478 if (SSL_use_PrivateKey(ssl, pkey) != 1)
2479 res = -1;
2480 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002481 if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482 res = -1;
2483 }
2484 EVP_PKEY_free(pkey);
2485 }
2486
2487 if (certs) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002488#if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002489 if (ssl)
2490 SSL_clear_chain_certs(ssl);
2491 else
2492 SSL_CTX_clear_chain_certs(data->ssl);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002493 while ((cert = sk_X509_pop(certs)) != NULL) {
2494 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2495 sizeof(buf));
2496 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
2497 " from PKCS12: subject='%s'", buf);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002498 if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) ||
2499 (!ssl && SSL_CTX_add1_chain_cert(data->ssl,
2500 cert) != 1)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002501 tls_show_errors(MSG_DEBUG, __func__,
2502 "Failed to add additional certificate");
2503 res = -1;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002504 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002505 break;
2506 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002507 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002508 }
2509 if (!res) {
2510 /* Try to continue anyway */
2511 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002512 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002513#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002514 if (ssl)
2515 res = SSL_build_cert_chain(
2516 ssl,
2517 SSL_BUILD_CHAIN_FLAG_CHECK |
2518 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
2519 else
2520 res = SSL_CTX_build_cert_chain(
2521 data->ssl,
2522 SSL_BUILD_CHAIN_FLAG_CHECK |
2523 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002524 if (!res) {
2525 tls_show_errors(MSG_DEBUG, __func__,
2526 "Failed to build certificate chain");
2527 } else if (res == 2) {
2528 wpa_printf(MSG_DEBUG,
2529 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
2530 }
2531#endif /* OPENSSL_IS_BORINGSSL */
2532 /*
2533 * Try to continue regardless of result since it is possible for
2534 * the extra certificates not to be required.
2535 */
2536 res = 0;
2537#else /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002538 SSL_CTX_clear_extra_chain_certs(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539 while ((cert = sk_X509_pop(certs)) != NULL) {
2540 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2541 sizeof(buf));
2542 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
2543 " from PKCS12: subject='%s'", buf);
2544 /*
2545 * There is no SSL equivalent for the chain cert - so
2546 * always add it to the context...
2547 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002548 if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1)
2549 {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002550 X509_free(cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002551 res = -1;
2552 break;
2553 }
2554 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002555 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002556#endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557 }
2558
2559 PKCS12_free(p12);
2560
2561 if (res < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002562 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563
2564 return res;
2565}
2566#endif /* PKCS12_FUNCS */
2567
2568
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002569static int tls_read_pkcs12(struct tls_data *data, SSL *ssl,
2570 const char *private_key, const char *passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571{
2572#ifdef PKCS12_FUNCS
2573 FILE *f;
2574 PKCS12 *p12;
2575
2576 f = fopen(private_key, "rb");
2577 if (f == NULL)
2578 return -1;
2579
2580 p12 = d2i_PKCS12_fp(f, NULL);
2581 fclose(f);
2582
2583 if (p12 == NULL) {
2584 tls_show_errors(MSG_INFO, __func__,
2585 "Failed to use PKCS#12 file");
2586 return -1;
2587 }
2588
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002589 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590
2591#else /* PKCS12_FUNCS */
2592 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
2593 "p12/pfx files");
2594 return -1;
2595#endif /* PKCS12_FUNCS */
2596}
2597
2598
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002599static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600 const u8 *blob, size_t len, const char *passwd)
2601{
2602#ifdef PKCS12_FUNCS
2603 PKCS12 *p12;
2604
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002605 p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002606 if (p12 == NULL) {
2607 tls_show_errors(MSG_INFO, __func__,
2608 "Failed to use PKCS#12 blob");
2609 return -1;
2610 }
2611
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002612 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002613
2614#else /* PKCS12_FUNCS */
2615 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
2616 "p12/pfx blobs");
2617 return -1;
2618#endif /* PKCS12_FUNCS */
2619}
2620
2621
2622#ifndef OPENSSL_NO_ENGINE
2623static int tls_engine_get_cert(struct tls_connection *conn,
2624 const char *cert_id,
2625 X509 **cert)
2626{
2627 /* this runs after the private key is loaded so no PIN is required */
2628 struct {
2629 const char *cert_id;
2630 X509 *cert;
2631 } params;
2632 params.cert_id = cert_id;
2633 params.cert = NULL;
2634
2635 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
2636 0, &params, NULL, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002637 unsigned long err = ERR_get_error();
2638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
2640 " '%s' [%s]", cert_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002641 ERR_error_string(err, NULL));
2642 if (tls_is_pin_error(err))
2643 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
2645 }
2646 if (!params.cert) {
2647 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
2648 " '%s'", cert_id);
2649 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
2650 }
2651 *cert = params.cert;
2652 return 0;
2653}
2654#endif /* OPENSSL_NO_ENGINE */
2655
2656
2657static int tls_connection_engine_client_cert(struct tls_connection *conn,
2658 const char *cert_id)
2659{
2660#ifndef OPENSSL_NO_ENGINE
2661 X509 *cert;
2662
2663 if (tls_engine_get_cert(conn, cert_id, &cert))
2664 return -1;
2665
2666 if (!SSL_use_certificate(conn->ssl, cert)) {
2667 tls_show_errors(MSG_ERROR, __func__,
2668 "SSL_use_certificate failed");
2669 X509_free(cert);
2670 return -1;
2671 }
2672 X509_free(cert);
2673 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
2674 "OK");
2675 return 0;
2676
2677#else /* OPENSSL_NO_ENGINE */
2678 return -1;
2679#endif /* OPENSSL_NO_ENGINE */
2680}
2681
2682
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002683static int tls_connection_engine_ca_cert(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684 struct tls_connection *conn,
2685 const char *ca_cert_id)
2686{
2687#ifndef OPENSSL_NO_ENGINE
2688 X509 *cert;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002689 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002690 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002691
2692 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
2693 return -1;
2694
2695 /* start off the same as tls_connection_ca_cert */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002696 store = X509_STORE_new();
2697 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002698 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
2699 "certificate store", __func__);
2700 X509_free(cert);
2701 return -1;
2702 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002703 SSL_CTX_set_cert_store(ssl_ctx, store);
2704 if (!X509_STORE_add_cert(store, cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002705 unsigned long err = ERR_peek_error();
2706 tls_show_errors(MSG_WARNING, __func__,
2707 "Failed to add CA certificate from engine "
2708 "to certificate store");
2709 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2710 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2711 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
2712 " already in hash table error",
2713 __func__);
2714 } else {
2715 X509_free(cert);
2716 return -1;
2717 }
2718 }
2719 X509_free(cert);
2720 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
2721 "to certificate store", __func__);
2722 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002723 conn->ca_cert_verify = 1;
2724
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002725 return 0;
2726
2727#else /* OPENSSL_NO_ENGINE */
2728 return -1;
2729#endif /* OPENSSL_NO_ENGINE */
2730}
2731
2732
2733static int tls_connection_engine_private_key(struct tls_connection *conn)
2734{
Adam Langley1eb02ed2015-04-21 19:00:05 -07002735#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002736 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
2737 tls_show_errors(MSG_ERROR, __func__,
2738 "ENGINE: cannot use private key for TLS");
2739 return -1;
2740 }
2741 if (!SSL_check_private_key(conn->ssl)) {
2742 tls_show_errors(MSG_INFO, __func__,
2743 "Private key failed verification");
2744 return -1;
2745 }
2746 return 0;
2747#else /* OPENSSL_NO_ENGINE */
2748 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
2749 "engine support was not compiled in");
2750 return -1;
2751#endif /* OPENSSL_NO_ENGINE */
2752}
2753
2754
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002755static int tls_connection_private_key(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002756 struct tls_connection *conn,
2757 const char *private_key,
2758 const char *private_key_passwd,
2759 const u8 *private_key_blob,
2760 size_t private_key_blob_len)
2761{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002762 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002763 char *passwd;
2764 int ok;
2765
2766 if (private_key == NULL && private_key_blob == NULL)
2767 return 0;
2768
2769 if (private_key_passwd) {
2770 passwd = os_strdup(private_key_passwd);
2771 if (passwd == NULL)
2772 return -1;
2773 } else
2774 passwd = NULL;
2775
2776 SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
2777 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
2778
2779 ok = 0;
2780 while (private_key_blob) {
2781 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
2782 (u8 *) private_key_blob,
2783 private_key_blob_len) == 1) {
2784 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
2785 "ASN1(EVP_PKEY_RSA) --> OK");
2786 ok = 1;
2787 break;
2788 }
2789
2790 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
2791 (u8 *) private_key_blob,
2792 private_key_blob_len) == 1) {
2793 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
2794 "ASN1(EVP_PKEY_DSA) --> OK");
2795 ok = 1;
2796 break;
2797 }
2798
2799 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
2800 (u8 *) private_key_blob,
2801 private_key_blob_len) == 1) {
2802 wpa_printf(MSG_DEBUG, "OpenSSL: "
2803 "SSL_use_RSAPrivateKey_ASN1 --> OK");
2804 ok = 1;
2805 break;
2806 }
2807
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002808 if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809 private_key_blob_len, passwd) == 0) {
2810 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
2811 "OK");
2812 ok = 1;
2813 break;
2814 }
2815
2816 break;
2817 }
2818
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002819 while (!ok && private_key) {
2820#ifndef OPENSSL_NO_STDIO
2821 if (SSL_use_PrivateKey_file(conn->ssl, private_key,
2822 SSL_FILETYPE_ASN1) == 1) {
2823 wpa_printf(MSG_DEBUG, "OpenSSL: "
2824 "SSL_use_PrivateKey_File (DER) --> OK");
2825 ok = 1;
2826 break;
2827 }
2828
2829 if (SSL_use_PrivateKey_file(conn->ssl, private_key,
2830 SSL_FILETYPE_PEM) == 1) {
2831 wpa_printf(MSG_DEBUG, "OpenSSL: "
2832 "SSL_use_PrivateKey_File (PEM) --> OK");
2833 ok = 1;
2834 break;
2835 }
2836#else /* OPENSSL_NO_STDIO */
2837 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2838 __func__);
2839#endif /* OPENSSL_NO_STDIO */
2840
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002841 if (tls_read_pkcs12(data, conn->ssl, private_key, passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002842 == 0) {
2843 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
2844 "--> OK");
2845 ok = 1;
2846 break;
2847 }
2848
2849 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
2850 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
2851 "access certificate store --> OK");
2852 ok = 1;
2853 break;
2854 }
2855
2856 break;
2857 }
2858
2859 if (!ok) {
2860 tls_show_errors(MSG_INFO, __func__,
2861 "Failed to load private key");
2862 os_free(passwd);
2863 return -1;
2864 }
2865 ERR_clear_error();
2866 SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
2867 os_free(passwd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002869 if (!SSL_check_private_key(conn->ssl)) {
2870 tls_show_errors(MSG_INFO, __func__, "Private key failed "
2871 "verification");
2872 return -1;
2873 }
2874
2875 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
2876 return 0;
2877}
2878
2879
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002880static int tls_global_private_key(struct tls_data *data,
2881 const char *private_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002882 const char *private_key_passwd)
2883{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002884 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885 char *passwd;
2886
2887 if (private_key == NULL)
2888 return 0;
2889
2890 if (private_key_passwd) {
2891 passwd = os_strdup(private_key_passwd);
2892 if (passwd == NULL)
2893 return -1;
2894 } else
2895 passwd = NULL;
2896
2897 SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
2898 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
2899 if (
2900#ifndef OPENSSL_NO_STDIO
2901 SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
2902 SSL_FILETYPE_ASN1) != 1 &&
2903 SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
2904 SSL_FILETYPE_PEM) != 1 &&
2905#endif /* OPENSSL_NO_STDIO */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002906 tls_read_pkcs12(data, NULL, private_key, passwd)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002907 tls_show_errors(MSG_INFO, __func__,
2908 "Failed to load private key");
2909 os_free(passwd);
2910 ERR_clear_error();
2911 return -1;
2912 }
2913 os_free(passwd);
2914 ERR_clear_error();
2915 SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002916
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002917 if (!SSL_CTX_check_private_key(ssl_ctx)) {
2918 tls_show_errors(MSG_INFO, __func__,
2919 "Private key failed verification");
2920 return -1;
2921 }
2922
2923 return 0;
2924}
2925
2926
2927static int tls_connection_dh(struct tls_connection *conn, const char *dh_file)
2928{
2929#ifdef OPENSSL_NO_DH
2930 if (dh_file == NULL)
2931 return 0;
2932 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
2933 "dh_file specified");
2934 return -1;
2935#else /* OPENSSL_NO_DH */
2936 DH *dh;
2937 BIO *bio;
2938
2939 /* TODO: add support for dh_blob */
2940 if (dh_file == NULL)
2941 return 0;
2942 if (conn == NULL)
2943 return -1;
2944
2945 bio = BIO_new_file(dh_file, "r");
2946 if (bio == NULL) {
2947 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
2948 dh_file, ERR_error_string(ERR_get_error(), NULL));
2949 return -1;
2950 }
2951 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
2952 BIO_free(bio);
2953#ifndef OPENSSL_NO_DSA
2954 while (dh == NULL) {
2955 DSA *dsa;
2956 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
2957 " trying to parse as DSA params", dh_file,
2958 ERR_error_string(ERR_get_error(), NULL));
2959 bio = BIO_new_file(dh_file, "r");
2960 if (bio == NULL)
2961 break;
2962 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
2963 BIO_free(bio);
2964 if (!dsa) {
2965 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
2966 "'%s': %s", dh_file,
2967 ERR_error_string(ERR_get_error(), NULL));
2968 break;
2969 }
2970
2971 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
2972 dh = DSA_dup_DH(dsa);
2973 DSA_free(dsa);
2974 if (dh == NULL) {
2975 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
2976 "params into DH params");
2977 break;
2978 }
2979 break;
2980 }
2981#endif /* !OPENSSL_NO_DSA */
2982 if (dh == NULL) {
2983 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
2984 "'%s'", dh_file);
2985 return -1;
2986 }
2987
2988 if (SSL_set_tmp_dh(conn->ssl, dh) != 1) {
2989 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
2990 "%s", dh_file,
2991 ERR_error_string(ERR_get_error(), NULL));
2992 DH_free(dh);
2993 return -1;
2994 }
2995 DH_free(dh);
2996 return 0;
2997#endif /* OPENSSL_NO_DH */
2998}
2999
3000
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003001static int tls_global_dh(struct tls_data *data, const char *dh_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003002{
3003#ifdef OPENSSL_NO_DH
3004 if (dh_file == NULL)
3005 return 0;
3006 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
3007 "dh_file specified");
3008 return -1;
3009#else /* OPENSSL_NO_DH */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003010 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003011 DH *dh;
3012 BIO *bio;
3013
3014 /* TODO: add support for dh_blob */
3015 if (dh_file == NULL)
3016 return 0;
3017 if (ssl_ctx == NULL)
3018 return -1;
3019
3020 bio = BIO_new_file(dh_file, "r");
3021 if (bio == NULL) {
3022 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
3023 dh_file, ERR_error_string(ERR_get_error(), NULL));
3024 return -1;
3025 }
3026 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
3027 BIO_free(bio);
3028#ifndef OPENSSL_NO_DSA
3029 while (dh == NULL) {
3030 DSA *dsa;
3031 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
3032 " trying to parse as DSA params", dh_file,
3033 ERR_error_string(ERR_get_error(), NULL));
3034 bio = BIO_new_file(dh_file, "r");
3035 if (bio == NULL)
3036 break;
3037 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
3038 BIO_free(bio);
3039 if (!dsa) {
3040 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
3041 "'%s': %s", dh_file,
3042 ERR_error_string(ERR_get_error(), NULL));
3043 break;
3044 }
3045
3046 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
3047 dh = DSA_dup_DH(dsa);
3048 DSA_free(dsa);
3049 if (dh == NULL) {
3050 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
3051 "params into DH params");
3052 break;
3053 }
3054 break;
3055 }
3056#endif /* !OPENSSL_NO_DSA */
3057 if (dh == NULL) {
3058 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
3059 "'%s'", dh_file);
3060 return -1;
3061 }
3062
3063 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
3064 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
3065 "%s", dh_file,
3066 ERR_error_string(ERR_get_error(), NULL));
3067 DH_free(dh);
3068 return -1;
3069 }
3070 DH_free(dh);
3071 return 0;
3072#endif /* OPENSSL_NO_DH */
3073}
3074
3075
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003076int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
3077 struct tls_random *keys)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003078{
3079 SSL *ssl;
3080
3081 if (conn == NULL || keys == NULL)
3082 return -1;
3083 ssl = conn->ssl;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003084 if (ssl == NULL)
3085 return -1;
3086
3087 os_memset(keys, 0, sizeof(*keys));
3088 keys->client_random = conn->client_random;
3089 keys->client_random_len = SSL_get_client_random(
3090 ssl, conn->client_random, sizeof(conn->client_random));
3091 keys->server_random = conn->server_random;
3092 keys->server_random_len = SSL_get_server_random(
3093 ssl, conn->server_random, sizeof(conn->server_random));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003094
3095 return 0;
3096}
3097
3098
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003099#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003100static int openssl_get_keyblock_size(SSL *ssl)
3101{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003102#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003103 const EVP_CIPHER *c;
3104 const EVP_MD *h;
3105 int md_size;
3106
3107 if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL ||
3108 ssl->read_hash == NULL)
3109 return -1;
3110
3111 c = ssl->enc_read_ctx->cipher;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003112 h = EVP_MD_CTX_md(ssl->read_hash);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003113 if (h)
3114 md_size = EVP_MD_size(h);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003115 else if (ssl->s3)
3116 md_size = ssl->s3->tmp.new_mac_secret_size;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003117 else
3118 return -1;
3119
3120 wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
3121 "IV_len=%d", EVP_CIPHER_key_length(c), md_size,
3122 EVP_CIPHER_iv_length(c));
3123 return 2 * (EVP_CIPHER_key_length(c) +
3124 md_size +
3125 EVP_CIPHER_iv_length(c));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003126#else
3127 const SSL_CIPHER *ssl_cipher;
3128 int cipher, digest;
3129 const EVP_CIPHER *c;
3130 const EVP_MD *h;
3131
3132 ssl_cipher = SSL_get_current_cipher(ssl);
3133 if (!ssl_cipher)
3134 return -1;
3135 cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
3136 digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
3137 wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d",
3138 cipher, digest);
3139 if (cipher < 0 || digest < 0)
3140 return -1;
3141 c = EVP_get_cipherbynid(cipher);
3142 h = EVP_get_digestbynid(digest);
3143 if (!c || !h)
3144 return -1;
3145
3146 wpa_printf(MSG_DEBUG,
3147 "OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d",
3148 EVP_CIPHER_key_length(c), EVP_MD_size(h),
3149 EVP_CIPHER_iv_length(c));
3150 return 2 * (EVP_CIPHER_key_length(c) + EVP_MD_size(h) +
3151 EVP_CIPHER_iv_length(c));
3152#endif
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003153}
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003154#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003155
3156
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003157int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
3158 const char *label, u8 *out, size_t out_len)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003159{
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003160 if (!conn ||
3161 SSL_export_keying_material(conn->ssl, out, out_len, label,
3162 os_strlen(label), NULL, 0, 0) != 1)
3163 return -1;
3164 return 0;
3165}
3166
3167
3168int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
3169 u8 *out, size_t out_len)
3170{
3171#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003172 SSL *ssl;
3173 SSL_SESSION *sess;
3174 u8 *rnd;
3175 int ret = -1;
3176 int skip = 0;
3177 u8 *tmp_out = NULL;
3178 u8 *_out = out;
3179 unsigned char client_random[SSL3_RANDOM_SIZE];
3180 unsigned char server_random[SSL3_RANDOM_SIZE];
3181 unsigned char master_key[64];
3182 size_t master_key_len;
3183 const char *ver;
3184
3185 /*
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003186 * TLS library did not support EAP-FAST key generation, so get the
3187 * needed TLS session parameters and use an internal implementation of
3188 * TLS PRF to derive the key.
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003189 */
3190
3191 if (conn == NULL)
3192 return -1;
3193 ssl = conn->ssl;
3194 if (ssl == NULL)
3195 return -1;
3196 ver = SSL_get_version(ssl);
3197 sess = SSL_get_session(ssl);
3198 if (!ver || !sess)
3199 return -1;
3200
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003201 skip = openssl_get_keyblock_size(ssl);
3202 if (skip < 0)
3203 return -1;
3204 tmp_out = os_malloc(skip + out_len);
3205 if (!tmp_out)
3206 return -1;
3207 _out = tmp_out;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003208
3209 rnd = os_malloc(2 * SSL3_RANDOM_SIZE);
3210 if (!rnd) {
3211 os_free(tmp_out);
3212 return -1;
3213 }
3214
3215 SSL_get_client_random(ssl, client_random, sizeof(client_random));
3216 SSL_get_server_random(ssl, server_random, sizeof(server_random));
3217 master_key_len = SSL_SESSION_get_master_key(sess, master_key,
3218 sizeof(master_key));
3219
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003220 os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE);
3221 os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003222
3223 if (os_strcmp(ver, "TLSv1.2") == 0) {
3224 tls_prf_sha256(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003225 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003226 _out, skip + out_len);
3227 ret = 0;
3228 } else if (tls_prf_sha1_md5(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003229 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003230 _out, skip + out_len) == 0) {
3231 ret = 0;
3232 }
3233 os_memset(master_key, 0, sizeof(master_key));
3234 os_free(rnd);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003235 if (ret == 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003236 os_memcpy(out, _out + skip, out_len);
3237 bin_clear_free(tmp_out, skip);
3238
3239 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003240#else /* OPENSSL_NEED_EAP_FAST_PRF */
3241 wpa_printf(MSG_ERROR,
3242 "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode");
3243 return -1;
3244#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245}
3246
3247
3248static struct wpabuf *
3249openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data,
3250 int server)
3251{
3252 int res;
3253 struct wpabuf *out_data;
3254
3255 /*
3256 * Give TLS handshake data from the server (if available) to OpenSSL
3257 * for processing.
3258 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003259 if (in_data && wpabuf_len(in_data) > 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003260 BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
3261 < 0) {
3262 tls_show_errors(MSG_INFO, __func__,
3263 "Handshake failed - BIO_write");
3264 return NULL;
3265 }
3266
3267 /* Initiate TLS handshake or continue the existing handshake */
3268 if (server)
3269 res = SSL_accept(conn->ssl);
3270 else
3271 res = SSL_connect(conn->ssl);
3272 if (res != 1) {
3273 int err = SSL_get_error(conn->ssl, res);
3274 if (err == SSL_ERROR_WANT_READ)
3275 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
3276 "more data");
3277 else if (err == SSL_ERROR_WANT_WRITE)
3278 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
3279 "write");
3280 else {
3281 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
3282 conn->failed++;
3283 }
3284 }
3285
3286 /* Get the TLS handshake data to be sent to the server */
3287 res = BIO_ctrl_pending(conn->ssl_out);
3288 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
3289 out_data = wpabuf_alloc(res);
3290 if (out_data == NULL) {
3291 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
3292 "handshake output (%d bytes)", res);
3293 if (BIO_reset(conn->ssl_out) < 0) {
3294 tls_show_errors(MSG_INFO, __func__,
3295 "BIO_reset failed");
3296 }
3297 return NULL;
3298 }
3299 res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
3300 res);
3301 if (res < 0) {
3302 tls_show_errors(MSG_INFO, __func__,
3303 "Handshake failed - BIO_read");
3304 if (BIO_reset(conn->ssl_out) < 0) {
3305 tls_show_errors(MSG_INFO, __func__,
3306 "BIO_reset failed");
3307 }
3308 wpabuf_free(out_data);
3309 return NULL;
3310 }
3311 wpabuf_put(out_data, res);
3312
3313 return out_data;
3314}
3315
3316
3317static struct wpabuf *
3318openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
3319{
3320 struct wpabuf *appl_data;
3321 int res;
3322
3323 appl_data = wpabuf_alloc(max_len + 100);
3324 if (appl_data == NULL)
3325 return NULL;
3326
3327 res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
3328 wpabuf_size(appl_data));
3329 if (res < 0) {
3330 int err = SSL_get_error(conn->ssl, res);
3331 if (err == SSL_ERROR_WANT_READ ||
3332 err == SSL_ERROR_WANT_WRITE) {
3333 wpa_printf(MSG_DEBUG, "SSL: No Application Data "
3334 "included");
3335 } else {
3336 tls_show_errors(MSG_INFO, __func__,
3337 "Failed to read possible "
3338 "Application Data");
3339 }
3340 wpabuf_free(appl_data);
3341 return NULL;
3342 }
3343
3344 wpabuf_put(appl_data, res);
3345 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
3346 "message", appl_data);
3347
3348 return appl_data;
3349}
3350
3351
3352static struct wpabuf *
3353openssl_connection_handshake(struct tls_connection *conn,
3354 const struct wpabuf *in_data,
3355 struct wpabuf **appl_data, int server)
3356{
3357 struct wpabuf *out_data;
3358
3359 if (appl_data)
3360 *appl_data = NULL;
3361
3362 out_data = openssl_handshake(conn, in_data, server);
3363 if (out_data == NULL)
3364 return NULL;
Jouni Malinen26af48b2014-04-09 13:02:53 +03003365 if (conn->invalid_hb_used) {
3366 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3367 wpabuf_free(out_data);
3368 return NULL;
3369 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003370
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003371 if (SSL_is_init_finished(conn->ssl)) {
3372 wpa_printf(MSG_DEBUG,
3373 "OpenSSL: Handshake finished - resumed=%d",
3374 tls_connection_resumed(conn->ssl_ctx, conn));
3375 if (appl_data && in_data)
3376 *appl_data = openssl_get_appl_data(conn,
3377 wpabuf_len(in_data));
3378 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003379
Jouni Malinen26af48b2014-04-09 13:02:53 +03003380 if (conn->invalid_hb_used) {
3381 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3382 if (appl_data) {
3383 wpabuf_free(*appl_data);
3384 *appl_data = NULL;
3385 }
3386 wpabuf_free(out_data);
3387 return NULL;
3388 }
3389
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003390 return out_data;
3391}
3392
3393
3394struct wpabuf *
3395tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
3396 const struct wpabuf *in_data,
3397 struct wpabuf **appl_data)
3398{
3399 return openssl_connection_handshake(conn, in_data, appl_data, 0);
3400}
3401
3402
3403struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
3404 struct tls_connection *conn,
3405 const struct wpabuf *in_data,
3406 struct wpabuf **appl_data)
3407{
3408 return openssl_connection_handshake(conn, in_data, appl_data, 1);
3409}
3410
3411
3412struct wpabuf * tls_connection_encrypt(void *tls_ctx,
3413 struct tls_connection *conn,
3414 const struct wpabuf *in_data)
3415{
3416 int res;
3417 struct wpabuf *buf;
3418
3419 if (conn == NULL)
3420 return NULL;
3421
3422 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
3423 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
3424 (res = BIO_reset(conn->ssl_out)) < 0) {
3425 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
3426 return NULL;
3427 }
3428 res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
3429 if (res < 0) {
3430 tls_show_errors(MSG_INFO, __func__,
3431 "Encryption failed - SSL_write");
3432 return NULL;
3433 }
3434
3435 /* Read encrypted data to be sent to the server */
3436 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
3437 if (buf == NULL)
3438 return NULL;
3439 res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
3440 if (res < 0) {
3441 tls_show_errors(MSG_INFO, __func__,
3442 "Encryption failed - BIO_read");
3443 wpabuf_free(buf);
3444 return NULL;
3445 }
3446 wpabuf_put(buf, res);
3447
3448 return buf;
3449}
3450
3451
3452struct wpabuf * tls_connection_decrypt(void *tls_ctx,
3453 struct tls_connection *conn,
3454 const struct wpabuf *in_data)
3455{
3456 int res;
3457 struct wpabuf *buf;
3458
3459 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
3460 res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
3461 wpabuf_len(in_data));
3462 if (res < 0) {
3463 tls_show_errors(MSG_INFO, __func__,
3464 "Decryption failed - BIO_write");
3465 return NULL;
3466 }
3467 if (BIO_reset(conn->ssl_out) < 0) {
3468 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
3469 return NULL;
3470 }
3471
3472 /* Read decrypted data for further processing */
3473 /*
3474 * Even though we try to disable TLS compression, it is possible that
3475 * this cannot be done with all TLS libraries. Add extra buffer space
3476 * to handle the possibility of the decrypted data being longer than
3477 * input data.
3478 */
3479 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
3480 if (buf == NULL)
3481 return NULL;
3482 res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
3483 if (res < 0) {
3484 tls_show_errors(MSG_INFO, __func__,
3485 "Decryption failed - SSL_read");
3486 wpabuf_free(buf);
3487 return NULL;
3488 }
3489 wpabuf_put(buf, res);
3490
Jouni Malinen26af48b2014-04-09 13:02:53 +03003491 if (conn->invalid_hb_used) {
3492 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3493 wpabuf_free(buf);
3494 return NULL;
3495 }
3496
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003497 return buf;
3498}
3499
3500
3501int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
3502{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003503 return conn ? SSL_cache_hit(conn->ssl) : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003504}
3505
3506
3507int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
3508 u8 *ciphers)
3509{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003510 char buf[500], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003511 u8 *c;
3512 int ret;
3513
3514 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
3515 return -1;
3516
3517 buf[0] = '\0';
3518 pos = buf;
3519 end = pos + sizeof(buf);
3520
3521 c = ciphers;
3522 while (*c != TLS_CIPHER_NONE) {
3523 const char *suite;
3524
3525 switch (*c) {
3526 case TLS_CIPHER_RC4_SHA:
3527 suite = "RC4-SHA";
3528 break;
3529 case TLS_CIPHER_AES128_SHA:
3530 suite = "AES128-SHA";
3531 break;
3532 case TLS_CIPHER_RSA_DHE_AES128_SHA:
3533 suite = "DHE-RSA-AES128-SHA";
3534 break;
3535 case TLS_CIPHER_ANON_DH_AES128_SHA:
3536 suite = "ADH-AES128-SHA";
3537 break;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003538 case TLS_CIPHER_RSA_DHE_AES256_SHA:
3539 suite = "DHE-RSA-AES256-SHA";
3540 break;
3541 case TLS_CIPHER_AES256_SHA:
3542 suite = "AES256-SHA";
3543 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003544 default:
3545 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
3546 "cipher selection: %d", *c);
3547 return -1;
3548 }
3549 ret = os_snprintf(pos, end - pos, ":%s", suite);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003550 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551 break;
3552 pos += ret;
3553
3554 c++;
3555 }
3556
3557 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
3558
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003559#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003560#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3561 if (os_strstr(buf, ":ADH-")) {
3562 /*
3563 * Need to drop to security level 0 to allow anonymous
3564 * cipher suites for EAP-FAST.
3565 */
3566 SSL_set_security_level(conn->ssl, 0);
3567 } else if (SSL_get_security_level(conn->ssl) == 0) {
3568 /* Force at least security level 1 */
3569 SSL_set_security_level(conn->ssl, 1);
3570 }
3571#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3572#endif
3573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003574 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
3575 tls_show_errors(MSG_INFO, __func__,
3576 "Cipher suite configuration failed");
3577 return -1;
3578 }
3579
3580 return 0;
3581}
3582
3583
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003584int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
3585 char *buf, size_t buflen)
3586{
3587 const char *name;
3588 if (conn == NULL || conn->ssl == NULL)
3589 return -1;
3590
3591 name = SSL_get_version(conn->ssl);
3592 if (name == NULL)
3593 return -1;
3594
3595 os_strlcpy(buf, name, buflen);
3596 return 0;
3597}
3598
3599
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003600int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
3601 char *buf, size_t buflen)
3602{
3603 const char *name;
3604 if (conn == NULL || conn->ssl == NULL)
3605 return -1;
3606
3607 name = SSL_get_cipher(conn->ssl);
3608 if (name == NULL)
3609 return -1;
3610
3611 os_strlcpy(buf, name, buflen);
3612 return 0;
3613}
3614
3615
3616int tls_connection_enable_workaround(void *ssl_ctx,
3617 struct tls_connection *conn)
3618{
3619 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
3620
3621 return 0;
3622}
3623
3624
3625#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3626/* ClientHello TLS extensions require a patch to openssl, so this function is
3627 * commented out unless explicitly needed for EAP-FAST in order to be able to
3628 * build this file with unmodified openssl. */
3629int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
3630 int ext_type, const u8 *data,
3631 size_t data_len)
3632{
3633 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
3634 return -1;
3635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
3637 data_len) != 1)
3638 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003639
3640 return 0;
3641}
3642#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3643
3644
3645int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
3646{
3647 if (conn == NULL)
3648 return -1;
3649 return conn->failed;
3650}
3651
3652
3653int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
3654{
3655 if (conn == NULL)
3656 return -1;
3657 return conn->read_alerts;
3658}
3659
3660
3661int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
3662{
3663 if (conn == NULL)
3664 return -1;
3665 return conn->write_alerts;
3666}
3667
3668
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003669#ifdef HAVE_OCSP
3670
3671static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
3672{
3673#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003674 BIO *out;
3675 size_t rlen;
3676 char *txt;
3677 int res;
3678
3679 if (wpa_debug_level > MSG_DEBUG)
3680 return;
3681
3682 out = BIO_new(BIO_s_mem());
3683 if (!out)
3684 return;
3685
3686 OCSP_RESPONSE_print(out, rsp, 0);
3687 rlen = BIO_ctrl_pending(out);
3688 txt = os_malloc(rlen + 1);
3689 if (!txt) {
3690 BIO_free(out);
3691 return;
3692 }
3693
3694 res = BIO_read(out, txt, rlen);
3695 if (res > 0) {
3696 txt[res] = '\0';
3697 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt);
3698 }
3699 os_free(txt);
3700 BIO_free(out);
3701#endif /* CONFIG_NO_STDOUT_DEBUG */
3702}
3703
3704
Dmitry Shmidt71757432014-06-02 13:50:35 -07003705static void debug_print_cert(X509 *cert, const char *title)
3706{
3707#ifndef CONFIG_NO_STDOUT_DEBUG
3708 BIO *out;
3709 size_t rlen;
3710 char *txt;
3711 int res;
3712
3713 if (wpa_debug_level > MSG_DEBUG)
3714 return;
3715
3716 out = BIO_new(BIO_s_mem());
3717 if (!out)
3718 return;
3719
3720 X509_print(out, cert);
3721 rlen = BIO_ctrl_pending(out);
3722 txt = os_malloc(rlen + 1);
3723 if (!txt) {
3724 BIO_free(out);
3725 return;
3726 }
3727
3728 res = BIO_read(out, txt, rlen);
3729 if (res > 0) {
3730 txt[res] = '\0';
3731 wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt);
3732 }
3733 os_free(txt);
3734
3735 BIO_free(out);
3736#endif /* CONFIG_NO_STDOUT_DEBUG */
3737}
3738
3739
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003740static int ocsp_resp_cb(SSL *s, void *arg)
3741{
3742 struct tls_connection *conn = arg;
3743 const unsigned char *p;
3744 int len, status, reason;
3745 OCSP_RESPONSE *rsp;
3746 OCSP_BASICRESP *basic;
3747 OCSP_CERTID *id;
3748 ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003749 X509_STORE *store;
3750 STACK_OF(X509) *certs = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003751
3752 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
3753 if (!p) {
3754 wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
3755 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
3756 }
3757
3758 wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
3759
3760 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
3761 if (!rsp) {
3762 wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
3763 return 0;
3764 }
3765
3766 ocsp_debug_print_resp(rsp);
3767
3768 status = OCSP_response_status(rsp);
3769 if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
3770 wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
3771 status, OCSP_response_status_str(status));
3772 return 0;
3773 }
3774
3775 basic = OCSP_response_get1_basic(rsp);
3776 if (!basic) {
3777 wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
3778 return 0;
3779 }
3780
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003781 store = SSL_CTX_get_cert_store(conn->ssl_ctx);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003782 if (conn->peer_issuer) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07003783 debug_print_cert(conn->peer_issuer, "Add OCSP issuer");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003784
3785 if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) {
3786 tls_show_errors(MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003787 "OpenSSL: Could not add issuer to certificate store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003788 }
3789 certs = sk_X509_new_null();
3790 if (certs) {
3791 X509 *cert;
3792 cert = X509_dup(conn->peer_issuer);
3793 if (cert && !sk_X509_push(certs, cert)) {
3794 tls_show_errors(
3795 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003796 "OpenSSL: Could not add issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003797 X509_free(cert);
3798 sk_X509_free(certs);
3799 certs = NULL;
3800 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003801 if (certs && conn->peer_issuer_issuer) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003802 cert = X509_dup(conn->peer_issuer_issuer);
3803 if (cert && !sk_X509_push(certs, cert)) {
3804 tls_show_errors(
3805 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003806 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003807 X509_free(cert);
3808 }
3809 }
3810 }
3811 }
3812
3813 status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
3814 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003815 if (status <= 0) {
3816 tls_show_errors(MSG_INFO, __func__,
3817 "OpenSSL: OCSP response failed verification");
3818 OCSP_BASICRESP_free(basic);
3819 OCSP_RESPONSE_free(rsp);
3820 return 0;
3821 }
3822
3823 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
3824
Dmitry Shmidt56052862013-10-04 10:23:25 -07003825 if (!conn->peer_cert) {
3826 wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
3827 OCSP_BASICRESP_free(basic);
3828 OCSP_RESPONSE_free(rsp);
3829 return 0;
3830 }
3831
3832 if (!conn->peer_issuer) {
3833 wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003834 OCSP_BASICRESP_free(basic);
3835 OCSP_RESPONSE_free(rsp);
3836 return 0;
3837 }
3838
3839 id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer);
3840 if (!id) {
3841 wpa_printf(MSG_DEBUG, "OpenSSL: Could not create OCSP certificate identifier");
3842 OCSP_BASICRESP_free(basic);
3843 OCSP_RESPONSE_free(rsp);
3844 return 0;
3845 }
3846
3847 if (!OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
3848 &this_update, &next_update)) {
3849 wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
3850 (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" :
3851 " (OCSP not required)");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003852 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003853 OCSP_BASICRESP_free(basic);
3854 OCSP_RESPONSE_free(rsp);
3855 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
3856 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003857 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003858
3859 if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
3860 tls_show_errors(MSG_INFO, __func__,
3861 "OpenSSL: OCSP status times invalid");
3862 OCSP_BASICRESP_free(basic);
3863 OCSP_RESPONSE_free(rsp);
3864 return 0;
3865 }
3866
3867 OCSP_BASICRESP_free(basic);
3868 OCSP_RESPONSE_free(rsp);
3869
3870 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
3871 OCSP_cert_status_str(status));
3872
3873 if (status == V_OCSP_CERTSTATUS_GOOD)
3874 return 1;
3875 if (status == V_OCSP_CERTSTATUS_REVOKED)
3876 return 0;
3877 if (conn->flags & TLS_CONN_REQUIRE_OCSP) {
3878 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
3879 return 0;
3880 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07003881 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 -07003882 return 1;
3883}
3884
3885
3886static int ocsp_status_cb(SSL *s, void *arg)
3887{
3888 char *tmp;
3889 char *resp;
3890 size_t len;
3891
3892 if (tls_global->ocsp_stapling_response == NULL) {
3893 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured");
3894 return SSL_TLSEXT_ERR_OK;
3895 }
3896
3897 resp = os_readfile(tls_global->ocsp_stapling_response, &len);
3898 if (resp == NULL) {
3899 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file");
3900 /* TODO: Build OCSPResponse with responseStatus = internalError
3901 */
3902 return SSL_TLSEXT_ERR_OK;
3903 }
3904 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response");
3905 tmp = OPENSSL_malloc(len);
3906 if (tmp == NULL) {
3907 os_free(resp);
3908 return SSL_TLSEXT_ERR_ALERT_FATAL;
3909 }
3910
3911 os_memcpy(tmp, resp, len);
3912 os_free(resp);
3913 SSL_set_tlsext_status_ocsp_resp(s, tmp, len);
3914
3915 return SSL_TLSEXT_ERR_OK;
3916}
3917
3918#endif /* HAVE_OCSP */
3919
3920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003921int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
3922 const struct tls_connection_params *params)
3923{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003924 struct tls_data *data = tls_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003925 int ret;
3926 unsigned long err;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003927 int can_pkcs11 = 0;
3928 const char *key_id = params->key_id;
3929 const char *cert_id = params->cert_id;
3930 const char *ca_cert_id = params->ca_cert_id;
3931 const char *engine_id = params->engine ? params->engine_id : NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003932
3933 if (conn == NULL)
3934 return -1;
3935
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08003936 if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
3937 wpa_printf(MSG_INFO,
3938 "OpenSSL: ocsp=3 not supported");
3939 return -1;
3940 }
3941
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003942 /*
3943 * If the engine isn't explicitly configured, and any of the
3944 * cert/key fields are actually PKCS#11 URIs, then automatically
3945 * use the PKCS#11 ENGINE.
3946 */
3947 if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0)
3948 can_pkcs11 = 1;
3949
3950 if (!key_id && params->private_key && can_pkcs11 &&
3951 os_strncmp(params->private_key, "pkcs11:", 7) == 0) {
3952 can_pkcs11 = 2;
3953 key_id = params->private_key;
3954 }
3955
3956 if (!cert_id && params->client_cert && can_pkcs11 &&
3957 os_strncmp(params->client_cert, "pkcs11:", 7) == 0) {
3958 can_pkcs11 = 2;
3959 cert_id = params->client_cert;
3960 }
3961
3962 if (!ca_cert_id && params->ca_cert && can_pkcs11 &&
3963 os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) {
3964 can_pkcs11 = 2;
3965 ca_cert_id = params->ca_cert;
3966 }
3967
3968 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
3969 if (can_pkcs11 == 2 && !engine_id)
3970 engine_id = "pkcs11";
3971
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003972#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3973#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003974 if (params->flags & TLS_CONN_EAP_FAST) {
3975 wpa_printf(MSG_DEBUG,
3976 "OpenSSL: Use TLSv1_method() for EAP-FAST");
3977 if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) {
3978 tls_show_errors(MSG_INFO, __func__,
3979 "Failed to set TLSv1_method() for EAP-FAST");
3980 return -1;
3981 }
3982 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003983#endif
3984#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003986 while ((err = ERR_get_error())) {
3987 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
3988 __func__, ERR_error_string(err, NULL));
3989 }
3990
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003991 if (engine_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003992 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003993 ret = tls_engine_init(conn, engine_id, params->pin,
3994 key_id, cert_id, ca_cert_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003995 if (ret)
3996 return ret;
3997 }
3998 if (tls_connection_set_subject_match(conn,
3999 params->subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07004000 params->altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004001 params->suffix_match,
4002 params->domain_match))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004003 return -1;
4004
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004005 if (engine_id && ca_cert_id) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004006 if (tls_connection_engine_ca_cert(data, conn, ca_cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004007 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004008 } else if (tls_connection_ca_cert(data, conn, params->ca_cert,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004009 params->ca_cert_blob,
4010 params->ca_cert_blob_len,
4011 params->ca_path))
4012 return -1;
4013
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004014 if (engine_id && cert_id) {
4015 if (tls_connection_engine_client_cert(conn, cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004016 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
4017 } else if (tls_connection_client_cert(conn, params->client_cert,
4018 params->client_cert_blob,
4019 params->client_cert_blob_len))
4020 return -1;
4021
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004022 if (engine_id && key_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004023 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
4024 if (tls_connection_engine_private_key(conn))
4025 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004026 } else if (tls_connection_private_key(data, conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004027 params->private_key,
4028 params->private_key_passwd,
4029 params->private_key_blob,
4030 params->private_key_blob_len)) {
4031 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
4032 params->private_key);
4033 return -1;
4034 }
4035
4036 if (tls_connection_dh(conn, params->dh_file)) {
4037 wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
4038 params->dh_file);
4039 return -1;
4040 }
4041
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004042 if (params->openssl_ciphers &&
4043 SSL_set_cipher_list(conn->ssl, params->openssl_ciphers) != 1) {
4044 wpa_printf(MSG_INFO,
4045 "OpenSSL: Failed to set cipher string '%s'",
4046 params->openssl_ciphers);
4047 return -1;
4048 }
4049
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004050 tls_set_conn_flags(conn->ssl, params->flags);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004051
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004052#ifdef OPENSSL_IS_BORINGSSL
4053 if (params->flags & TLS_CONN_REQUEST_OCSP) {
4054 SSL_enable_ocsp_stapling(conn->ssl);
4055 }
4056#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004057#ifdef HAVE_OCSP
4058 if (params->flags & TLS_CONN_REQUEST_OCSP) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004059 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004060 SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp);
4061 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb);
4062 SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn);
4063 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004064#else /* HAVE_OCSP */
4065 if (params->flags & TLS_CONN_REQUIRE_OCSP) {
4066 wpa_printf(MSG_INFO,
4067 "OpenSSL: No OCSP support included - reject configuration");
4068 return -1;
4069 }
4070 if (params->flags & TLS_CONN_REQUEST_OCSP) {
4071 wpa_printf(MSG_DEBUG,
4072 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
4073 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004074#endif /* HAVE_OCSP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004075#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004076
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07004077 conn->flags = params->flags;
4078
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004079 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004080
4081 return 0;
4082}
4083
4084
4085int tls_global_set_params(void *tls_ctx,
4086 const struct tls_connection_params *params)
4087{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004088 struct tls_data *data = tls_ctx;
4089 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004090 unsigned long err;
4091
4092 while ((err = ERR_get_error())) {
4093 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
4094 __func__, ERR_error_string(err, NULL));
4095 }
4096
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004097 if (tls_global_ca_cert(data, params->ca_cert) ||
4098 tls_global_client_cert(data, params->client_cert) ||
4099 tls_global_private_key(data, params->private_key,
4100 params->private_key_passwd) ||
4101 tls_global_dh(data, params->dh_file)) {
4102 wpa_printf(MSG_INFO, "TLS: Failed to set global parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004103 return -1;
4104 }
4105
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004106 if (params->openssl_ciphers &&
4107 SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) {
4108 wpa_printf(MSG_INFO,
4109 "OpenSSL: Failed to set cipher string '%s'",
4110 params->openssl_ciphers);
4111 return -1;
4112 }
4113
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004114#ifdef SSL_OP_NO_TICKET
4115 if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET)
4116 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004117#ifdef SSL_CTX_clear_options
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004118 else
4119 SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004120#endif /* SSL_clear_options */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004121#endif /* SSL_OP_NO_TICKET */
4122
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004123#ifdef HAVE_OCSP
4124 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb);
4125 SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx);
4126 os_free(tls_global->ocsp_stapling_response);
4127 if (params->ocsp_stapling_response)
4128 tls_global->ocsp_stapling_response =
4129 os_strdup(params->ocsp_stapling_response);
4130 else
4131 tls_global->ocsp_stapling_response = NULL;
4132#endif /* HAVE_OCSP */
4133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004134 return 0;
4135}
4136
4137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004138#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4139/* Pre-shared secred requires a patch to openssl, so this function is
4140 * commented out unless explicitly needed for EAP-FAST in order to be able to
4141 * build this file with unmodified openssl. */
4142
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004143#if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004144static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
4145 STACK_OF(SSL_CIPHER) *peer_ciphers,
4146 const SSL_CIPHER **cipher, void *arg)
4147#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004148static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
4149 STACK_OF(SSL_CIPHER) *peer_ciphers,
4150 SSL_CIPHER **cipher, void *arg)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004151#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004152{
4153 struct tls_connection *conn = arg;
4154 int ret;
4155
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004156#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004157 if (conn == NULL || conn->session_ticket_cb == NULL)
4158 return 0;
4159
4160 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
4161 conn->session_ticket,
4162 conn->session_ticket_len,
4163 s->s3->client_random,
4164 s->s3->server_random, secret);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004165#else
4166 unsigned char client_random[SSL3_RANDOM_SIZE];
4167 unsigned char server_random[SSL3_RANDOM_SIZE];
4168
4169 if (conn == NULL || conn->session_ticket_cb == NULL)
4170 return 0;
4171
4172 SSL_get_client_random(s, client_random, sizeof(client_random));
4173 SSL_get_server_random(s, server_random, sizeof(server_random));
4174
4175 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
4176 conn->session_ticket,
4177 conn->session_ticket_len,
4178 client_random,
4179 server_random, secret);
4180#endif
4181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004182 os_free(conn->session_ticket);
4183 conn->session_ticket = NULL;
4184
4185 if (ret <= 0)
4186 return 0;
4187
4188 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
4189 return 1;
4190}
4191
4192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004193static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
4194 int len, void *arg)
4195{
4196 struct tls_connection *conn = arg;
4197
4198 if (conn == NULL || conn->session_ticket_cb == NULL)
4199 return 0;
4200
4201 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
4202
4203 os_free(conn->session_ticket);
4204 conn->session_ticket = NULL;
4205
4206 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
4207 "extension", data, len);
4208
4209 conn->session_ticket = os_malloc(len);
4210 if (conn->session_ticket == NULL)
4211 return 0;
4212
4213 os_memcpy(conn->session_ticket, data, len);
4214 conn->session_ticket_len = len;
4215
4216 return 1;
4217}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004218#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4219
4220
4221int tls_connection_set_session_ticket_cb(void *tls_ctx,
4222 struct tls_connection *conn,
4223 tls_session_ticket_cb cb,
4224 void *ctx)
4225{
4226#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4227 conn->session_ticket_cb = cb;
4228 conn->session_ticket_cb_ctx = ctx;
4229
4230 if (cb) {
4231 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
4232 conn) != 1)
4233 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234 SSL_set_session_ticket_ext_cb(conn->ssl,
4235 tls_session_ticket_ext_cb, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236 } else {
4237 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
4238 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004239 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004240 }
4241
4242 return 0;
4243#else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4244 return -1;
4245#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4246}
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004247
4248
4249int tls_get_library_version(char *buf, size_t buf_len)
4250{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004251#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004252 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
4253 OPENSSL_VERSION_TEXT,
4254 OpenSSL_version(OPENSSL_VERSION));
4255#else
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004256 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
4257 OPENSSL_VERSION_TEXT,
4258 SSLeay_version(SSLEAY_VERSION));
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004259#endif
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004260}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004261
4262
4263void tls_connection_set_success_data(struct tls_connection *conn,
4264 struct wpabuf *data)
4265{
4266 SSL_SESSION *sess;
4267 struct wpabuf *old;
4268
4269 if (tls_ex_idx_session < 0)
4270 goto fail;
4271 sess = SSL_get_session(conn->ssl);
4272 if (!sess)
4273 goto fail;
4274 old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
4275 if (old) {
4276 wpa_printf(MSG_DEBUG, "OpenSSL: Replacing old success data %p",
4277 old);
4278 wpabuf_free(old);
4279 }
4280 if (SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1)
4281 goto fail;
4282
4283 wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p", data);
4284 conn->success_data = 1;
4285 return;
4286
4287fail:
4288 wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data");
4289 wpabuf_free(data);
4290}
4291
4292
4293void tls_connection_set_success_data_resumed(struct tls_connection *conn)
4294{
4295 wpa_printf(MSG_DEBUG,
4296 "OpenSSL: Success data accepted for resumed session");
4297 conn->success_data = 1;
4298}
4299
4300
4301const struct wpabuf *
4302tls_connection_get_success_data(struct tls_connection *conn)
4303{
4304 SSL_SESSION *sess;
4305
4306 if (tls_ex_idx_session < 0 ||
4307 !(sess = SSL_get_session(conn->ssl)))
4308 return NULL;
4309 return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
4310}
4311
4312
4313void tls_connection_remove_session(struct tls_connection *conn)
4314{
4315 SSL_SESSION *sess;
4316
4317 sess = SSL_get_session(conn->ssl);
4318 if (!sess)
4319 return;
4320
4321 if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1)
4322 wpa_printf(MSG_DEBUG,
4323 "OpenSSL: Session was not cached");
4324 else
4325 wpa_printf(MSG_DEBUG,
4326 "OpenSSL: Removed cached session to disable session resumption");
4327}