~ubuntu-branches/ubuntu/gutsy/wpasupplicant/gutsy

« back to all changes in this revision

Viewing changes to src/crypto/tls_openssl.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler, Alexander Sack
  • Date: 2007-08-26 16:06:57 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20070826160657-2m8pxoweuxe8f93t
Tags: 0.6.0+0.5.8-0ubuntu1
* New upstream release
* remove patch 11_erroneous_manpage_ref, applied upstream
* remove patch 25_wpas_dbus_unregister_iface_fix, applied upstream

[ Alexander Sack ]
* bumping upstream version to replace development version 0.6.0 with
  this package from stable release branch.
* attempt to fix wierd timeout and high latency issues by going
  back to stable upstream version (0.5.9) (LP: #140763,
  LP: #141233).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * WPA Supplicant / SSL/TLS interface functions for openssl
3
 
 * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi>
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License version 2 as
7
 
 * published by the Free Software Foundation.
8
 
 *
9
 
 * Alternatively, this software may be distributed under the terms of BSD
10
 
 * license.
11
 
 *
12
 
 * See README and COPYING for more details.
13
 
 */
14
 
 
15
 
#include "includes.h"
16
 
 
17
 
#ifndef CONFIG_SMARTCARD
18
 
#ifndef OPENSSL_NO_ENGINE
19
 
#define OPENSSL_NO_ENGINE
20
 
#endif
21
 
#endif
22
 
 
23
 
#include <openssl/ssl.h>
24
 
#include <openssl/err.h>
25
 
#include <openssl/pkcs12.h>
26
 
#include <openssl/x509v3.h>
27
 
#ifndef OPENSSL_NO_ENGINE
28
 
#include <openssl/engine.h>
29
 
#endif /* OPENSSL_NO_ENGINE */
30
 
 
31
 
#include "common.h"
32
 
#include "tls.h"
33
 
 
34
 
#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
35
 
#define OPENSSL_d2i_TYPE const unsigned char **
36
 
#else
37
 
#define OPENSSL_d2i_TYPE unsigned char **
38
 
#endif
39
 
 
40
 
static int tls_openssl_ref_count = 0;
41
 
 
42
 
struct tls_connection {
43
 
        SSL *ssl;
44
 
        BIO *ssl_in, *ssl_out;
45
 
#ifndef OPENSSL_NO_ENGINE
46
 
        ENGINE *engine;        /* functional reference to the engine */
47
 
        EVP_PKEY *private_key; /* the private key if using engine */
48
 
#endif /* OPENSSL_NO_ENGINE */
49
 
        char *subject_match, *altsubject_match;
50
 
        int read_alerts, write_alerts, failed;
51
 
 
52
 
        u8 *pre_shared_secret;
53
 
        size_t pre_shared_secret_len;
54
 
};
55
 
 
56
 
 
57
 
#ifdef CONFIG_NO_STDOUT_DEBUG
58
 
 
59
 
static void _tls_show_errors(void)
60
 
{
61
 
        unsigned long err;
62
 
 
63
 
        while ((err = ERR_get_error())) {
64
 
                /* Just ignore the errors, since stdout is disabled */
65
 
        }
66
 
}
67
 
#define tls_show_errors(l, f, t) _tls_show_errors()
68
 
 
69
 
#else /* CONFIG_NO_STDOUT_DEBUG */
70
 
 
71
 
static void tls_show_errors(int level, const char *func, const char *txt)
72
 
{
73
 
        unsigned long err;
74
 
 
75
 
        wpa_printf(level, "OpenSSL: %s - %s %s",
76
 
                   func, txt, ERR_error_string(ERR_get_error(), NULL));
77
 
 
78
 
        while ((err = ERR_get_error())) {
79
 
                wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
80
 
                           ERR_error_string(err, NULL));
81
 
        }
82
 
}
83
 
 
84
 
#endif /* CONFIG_NO_STDOUT_DEBUG */
85
 
 
86
 
 
87
 
#ifdef CONFIG_NATIVE_WINDOWS
88
 
 
89
 
/* Windows CryptoAPI and access to certificate stores */
90
 
#include <wincrypt.h>
91
 
 
92
 
#ifdef __MINGW32_VERSION
93
 
/*
94
 
 * MinGW does not yet include all the needed definitions for CryptoAPI, so
95
 
 * define here whatever extra is needed.
96
 
 */
97
 
#define CALG_SSL3_SHAMD5 (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SSL3SHAMD5)
98
 
#define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
99
 
#define CERT_STORE_READONLY_FLAG 0x00008000
100
 
#define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
101
 
#define CRYPT_ACQUIRE_COMPARE_KEY_FLAG 0x00000004
102
 
 
103
 
static BOOL WINAPI
104
 
(*CryptAcquireCertificatePrivateKey)(PCCERT_CONTEXT pCert, DWORD dwFlags,
105
 
                                     void *pvReserved, HCRYPTPROV *phCryptProv,
106
 
                                     DWORD *pdwKeySpec, BOOL *pfCallerFreeProv)
107
 
= NULL; /* to be loaded from crypt32.dll */
108
 
 
109
 
static PCCERT_CONTEXT WINAPI
110
 
(*CertEnumCertificatesInStore)(HCERTSTORE hCertStore,
111
 
                               PCCERT_CONTEXT pPrevCertContext)
112
 
= NULL; /* to be loaded from crypt32.dll */
113
 
 
114
 
static int mingw_load_crypto_func(void)
115
 
{
116
 
        HINSTANCE dll;
117
 
 
118
 
        /* MinGW does not yet have full CryptoAPI support, so load the needed
119
 
         * function here. */
120
 
 
121
 
        if (CryptAcquireCertificatePrivateKey)
122
 
                return 0;
123
 
 
124
 
        dll = LoadLibrary("crypt32");
125
 
        if (dll == NULL) {
126
 
                wpa_printf(MSG_DEBUG, "CryptoAPI: Could not load crypt32 "
127
 
                           "library");
128
 
                return -1;
129
 
        }
130
 
 
131
 
        CryptAcquireCertificatePrivateKey = GetProcAddress(
132
 
                dll, "CryptAcquireCertificatePrivateKey");
133
 
        if (CryptAcquireCertificatePrivateKey == NULL) {
134
 
                wpa_printf(MSG_DEBUG, "CryptoAPI: Could not get "
135
 
                           "CryptAcquireCertificatePrivateKey() address from "
136
 
                           "crypt32 library");
137
 
                return -1;
138
 
        }
139
 
 
140
 
        CertEnumCertificatesInStore = (void *) GetProcAddress(
141
 
                dll, "CertEnumCertificatesInStore");
142
 
        if (CertEnumCertificatesInStore == NULL) {
143
 
                wpa_printf(MSG_DEBUG, "CryptoAPI: Could not get "
144
 
                           "CertEnumCertificatesInStore() address from "
145
 
                           "crypt32 library");
146
 
                return -1;
147
 
        }
148
 
 
149
 
        return 0;
150
 
}
151
 
 
152
 
#else /* __MINGW32_VERSION */
153
 
 
154
 
static int mingw_load_crypto_func(void)
155
 
{
156
 
        return 0;
157
 
}
158
 
 
159
 
#endif /* __MINGW32_VERSION */
160
 
 
161
 
 
162
 
struct cryptoapi_rsa_data {
163
 
        const CERT_CONTEXT *cert;
164
 
        HCRYPTPROV crypt_prov;
165
 
        DWORD key_spec;
166
 
        BOOL free_crypt_prov;
167
 
};
168
 
 
169
 
 
170
 
static void cryptoapi_error(const char *msg)
171
 
{
172
 
        wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u",
173
 
                   msg, (unsigned int) GetLastError());
174
 
}
175
 
 
176
 
 
177
 
static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from,
178
 
                                 unsigned char *to, RSA *rsa, int padding)
179
 
{
180
 
        wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
181
 
        return 0;
182
 
}
183
 
 
184
 
 
185
 
static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from,
186
 
                                 unsigned char *to, RSA *rsa, int padding)
187
 
{
188
 
        wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
189
 
        return 0;
190
 
}
191
 
 
192
 
 
193
 
static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from,
194
 
                                  unsigned char *to, RSA *rsa, int padding)
195
 
{
196
 
        struct cryptoapi_rsa_data *priv =
197
 
                (struct cryptoapi_rsa_data *) rsa->meth->app_data;
198
 
        HCRYPTHASH hash;
199
 
        DWORD hash_size, len, i;
200
 
        unsigned char *buf = NULL;
201
 
        int ret = 0;
202
 
 
203
 
        if (priv == NULL) {
204
 
                RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
205
 
                       ERR_R_PASSED_NULL_PARAMETER);
206
 
                return 0;
207
 
        }
208
 
 
209
 
        if (padding != RSA_PKCS1_PADDING) {
210
 
                RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
211
 
                       RSA_R_UNKNOWN_PADDING_TYPE);
212
 
                return 0;
213
 
        }
214
 
 
215
 
        if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) {
216
 
                wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported",
217
 
                           __func__);
218
 
                RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
219
 
                       RSA_R_INVALID_MESSAGE_LENGTH);
220
 
                return 0;
221
 
        }
222
 
 
223
 
        if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash))
224
 
        {
225
 
                cryptoapi_error("CryptCreateHash failed");
226
 
                return 0;
227
 
        }
228
 
 
229
 
        len = sizeof(hash_size);
230
 
        if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len,
231
 
                               0)) {
232
 
                cryptoapi_error("CryptGetHashParam failed");
233
 
                goto err;
234
 
        }
235
 
 
236
 
        if ((int) hash_size != flen) {
237
 
                wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)",
238
 
                           (unsigned) hash_size, flen);
239
 
                RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
240
 
                       RSA_R_INVALID_MESSAGE_LENGTH);
241
 
                goto err;
242
 
        }
243
 
        if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) {
244
 
                cryptoapi_error("CryptSetHashParam failed");
245
 
                goto err;
246
 
        }
247
 
 
248
 
        len = RSA_size(rsa);
249
 
        buf = os_malloc(len);
250
 
        if (buf == NULL) {
251
 
                RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
252
 
                goto err;
253
 
        }
254
 
 
255
 
        if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) {
256
 
                cryptoapi_error("CryptSignHash failed");
257
 
                goto err;
258
 
        }
259
 
 
260
 
        for (i = 0; i < len; i++)
261
 
                to[i] = buf[len - i - 1];
262
 
        ret = len;
263
 
 
264
 
err:
265
 
        os_free(buf);
266
 
        CryptDestroyHash(hash);
267
 
 
268
 
        return ret;
269
 
}
270
 
 
271
 
 
272
 
static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from,
273
 
                                  unsigned char *to, RSA *rsa, int padding)
274
 
{
275
 
        wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
276
 
        return 0;
277
 
}
278
 
 
279
 
 
280
 
static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv)
281
 
{
282
 
        if (priv == NULL)
283
 
                return;
284
 
        if (priv->crypt_prov && priv->free_crypt_prov)
285
 
                CryptReleaseContext(priv->crypt_prov, 0);
286
 
        if (priv->cert)
287
 
                CertFreeCertificateContext(priv->cert);
288
 
        os_free(priv);
289
 
}
290
 
 
291
 
 
292
 
static int cryptoapi_finish(RSA *rsa)
293
 
{
294
 
        cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data);
295
 
        os_free((void *) rsa->meth);
296
 
        rsa->meth = NULL;
297
 
        return 1;
298
 
}
299
 
 
300
 
 
301
 
static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store)
302
 
{
303
 
        HCERTSTORE cs;
304
 
        const CERT_CONTEXT *ret = NULL;
305
 
 
306
 
        cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0,
307
 
                           store | CERT_STORE_OPEN_EXISTING_FLAG |
308
 
                           CERT_STORE_READONLY_FLAG, L"MY");
309
 
        if (cs == NULL) {
310
 
                cryptoapi_error("Failed to open 'My system store'");
311
 
                return NULL;
312
 
        }
313
 
 
314
 
        if (strncmp(name, "cert://", 7) == 0) {
315
 
                unsigned short wbuf[255];
316
 
                MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255);
317
 
                ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING |
318
 
                                                 PKCS_7_ASN_ENCODING,
319
 
                                                 0, CERT_FIND_SUBJECT_STR,
320
 
                                                 wbuf, NULL);
321
 
        } else if (strncmp(name, "hash://", 7) == 0) {
322
 
                CRYPT_HASH_BLOB blob;
323
 
                int len;
324
 
                const char *hash = name + 7;
325
 
                unsigned char *buf;
326
 
 
327
 
                len = os_strlen(hash) / 2;
328
 
                buf = os_malloc(len);
329
 
                if (buf && hexstr2bin(hash, buf, len) == 0) {
330
 
                        blob.cbData = len;
331
 
                        blob.pbData = buf;
332
 
                        ret = CertFindCertificateInStore(cs,
333
 
                                                         X509_ASN_ENCODING |
334
 
                                                         PKCS_7_ASN_ENCODING,
335
 
                                                         0, CERT_FIND_HASH,
336
 
                                                         &blob, NULL);
337
 
                }
338
 
                os_free(buf);
339
 
        }
340
 
 
341
 
        CertCloseStore(cs, 0);
342
 
 
343
 
        return ret;
344
 
}
345
 
 
346
 
 
347
 
static int tls_cryptoapi_cert(SSL *ssl, const char *name)
348
 
{
349
 
        X509 *cert = NULL;
350
 
        RSA *rsa = NULL, *pub_rsa;
351
 
        struct cryptoapi_rsa_data *priv;
352
 
        RSA_METHOD *rsa_meth;
353
 
 
354
 
        if (name == NULL ||
355
 
            (strncmp(name, "cert://", 7) != 0 &&
356
 
             strncmp(name, "hash://", 7) != 0))
357
 
                return -1;
358
 
 
359
 
        priv = os_zalloc(sizeof(*priv));
360
 
        rsa_meth = os_zalloc(sizeof(*rsa_meth));
361
 
        if (priv == NULL || rsa_meth == NULL) {
362
 
                wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory "
363
 
                           "for CryptoAPI RSA method");
364
 
                os_free(priv);
365
 
                os_free(rsa_meth);
366
 
                return -1;
367
 
        }
368
 
 
369
 
        priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER);
370
 
        if (priv->cert == NULL) {
371
 
                priv->cert = cryptoapi_find_cert(
372
 
                        name, CERT_SYSTEM_STORE_LOCAL_MACHINE);
373
 
        }
374
 
        if (priv->cert == NULL) {
375
 
                wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate "
376
 
                           "'%s'", name);
377
 
                goto err;
378
 
        }
379
 
 
380
 
        cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &priv->cert->pbCertEncoded,
381
 
                        priv->cert->cbCertEncoded);
382
 
        if (cert == NULL) {
383
 
                wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER "
384
 
                           "encoding");
385
 
                goto err;
386
 
        }
387
 
 
388
 
        if (mingw_load_crypto_func())
389
 
                goto err;
390
 
 
391
 
        if (!CryptAcquireCertificatePrivateKey(priv->cert,
392
 
                                               CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
393
 
                                               NULL, &priv->crypt_prov,
394
 
                                               &priv->key_spec,
395
 
                                               &priv->free_crypt_prov)) {
396
 
                cryptoapi_error("Failed to acquire a private key for the "
397
 
                                "certificate");
398
 
                goto err;
399
 
        }
400
 
 
401
 
        rsa_meth->name = "Microsoft CryptoAPI RSA Method";
402
 
        rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc;
403
 
        rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec;
404
 
        rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc;
405
 
        rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec;
406
 
        rsa_meth->finish = cryptoapi_finish;
407
 
        rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK;
408
 
        rsa_meth->app_data = (char *) priv;
409
 
 
410
 
        rsa = RSA_new();
411
 
        if (rsa == NULL) {
412
 
                SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,
413
 
                       ERR_R_MALLOC_FAILURE);
414
 
                goto err;
415
 
        }
416
 
 
417
 
        if (!SSL_use_certificate(ssl, cert)) {
418
 
                RSA_free(rsa);
419
 
                rsa = NULL;
420
 
                goto err;
421
 
        }
422
 
        pub_rsa = cert->cert_info->key->pkey->pkey.rsa;
423
 
        X509_free(cert);
424
 
        cert = NULL;
425
 
 
426
 
        rsa->n = BN_dup(pub_rsa->n);
427
 
        rsa->e = BN_dup(pub_rsa->e);
428
 
        if (!RSA_set_method(rsa, rsa_meth))
429
 
                goto err;
430
 
 
431
 
        if (!SSL_use_RSAPrivateKey(ssl, rsa))
432
 
                goto err;
433
 
        RSA_free(rsa);
434
 
 
435
 
        return 0;
436
 
 
437
 
err:
438
 
        if (cert)
439
 
                X509_free(cert);
440
 
        if (rsa)
441
 
                RSA_free(rsa);
442
 
        else {
443
 
                os_free(rsa_meth);
444
 
                cryptoapi_free_data(priv);
445
 
        }
446
 
        return -1;
447
 
}
448
 
 
449
 
 
450
 
static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name)
451
 
{
452
 
        HCERTSTORE cs;
453
 
        PCCERT_CONTEXT ctx = NULL;
454
 
        X509 *cert;
455
 
        char buf[128];
456
 
        const char *store;
457
 
#ifdef UNICODE
458
 
        WCHAR *wstore;
459
 
#endif /* UNICODE */
460
 
 
461
 
        if (mingw_load_crypto_func())
462
 
                return -1;
463
 
 
464
 
        if (name == NULL || strncmp(name, "cert_store://", 13) != 0)
465
 
                return -1;
466
 
 
467
 
        store = name + 13;
468
 
#ifdef UNICODE
469
 
        wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR));
470
 
        if (wstore == NULL)
471
 
                return -1;
472
 
        wsprintf(wstore, L"%S", store);
473
 
        cs = CertOpenSystemStore(0, wstore);
474
 
        os_free(wstore);
475
 
#else /* UNICODE */
476
 
        cs = CertOpenSystemStore(0, store);
477
 
#endif /* UNICODE */
478
 
        if (cs == NULL) {
479
 
                wpa_printf(MSG_DEBUG, "%s: failed to open system cert store "
480
 
                           "'%s': error=%d", __func__, store,
481
 
                           (int) GetLastError());
482
 
                return -1;
483
 
        }
484
 
 
485
 
        while ((ctx = CertEnumCertificatesInStore(cs, ctx))) {
486
 
                cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &ctx->pbCertEncoded,
487
 
                                ctx->cbCertEncoded);
488
 
                if (cert == NULL) {
489
 
                        wpa_printf(MSG_INFO, "CryptoAPI: Could not process "
490
 
                                   "X509 DER encoding for CA cert");
491
 
                        continue;
492
 
                }
493
 
 
494
 
                X509_NAME_oneline(X509_get_subject_name(cert), buf,
495
 
                                  sizeof(buf));
496
 
                wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for "
497
 
                           "system certificate store: subject='%s'", buf);
498
 
 
499
 
                if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) {
500
 
                        tls_show_errors(MSG_WARNING, __func__,
501
 
                                        "Failed to add ca_cert to OpenSSL "
502
 
                                        "certificate store");
503
 
                }
504
 
 
505
 
                X509_free(cert);
506
 
        }
507
 
 
508
 
        if (!CertCloseStore(cs, 0)) {
509
 
                wpa_printf(MSG_DEBUG, "%s: failed to close system cert store "
510
 
                           "'%s': error=%d", __func__, name + 13,
511
 
                           (int) GetLastError());
512
 
        }
513
 
 
514
 
        return 0;
515
 
}
516
 
 
517
 
 
518
 
#else /* CONFIG_NATIVE_WINDOWS */
519
 
 
520
 
static int tls_cryptoapi_cert(SSL *ssl, const char *name)
521
 
{
522
 
        return -1;
523
 
}
524
 
 
525
 
#endif /* CONFIG_NATIVE_WINDOWS */
526
 
 
527
 
 
528
 
static void ssl_info_cb(const SSL *ssl, int where, int ret)
529
 
{
530
 
        const char *str;
531
 
        int w;
532
 
 
533
 
        wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret);
534
 
        w = where & ~SSL_ST_MASK;
535
 
        if (w & SSL_ST_CONNECT)
536
 
                str = "SSL_connect";
537
 
        else if (w & SSL_ST_ACCEPT)
538
 
                str = "SSL_accept";
539
 
        else
540
 
                str = "undefined";
541
 
 
542
 
        if (where & SSL_CB_LOOP) {
543
 
                wpa_printf(MSG_DEBUG, "SSL: %s:%s",
544
 
                           str, SSL_state_string_long(ssl));
545
 
        } else if (where & SSL_CB_ALERT) {
546
 
                wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s",
547
 
                           where & SSL_CB_READ ?
548
 
                           "read (remote end reported an error)" :
549
 
                           "write (local SSL3 detected an error)",
550
 
                           SSL_alert_type_string_long(ret),
551
 
                           SSL_alert_desc_string_long(ret));
552
 
                if ((ret >> 8) == SSL3_AL_FATAL) {
553
 
                        struct tls_connection *conn =
554
 
                                SSL_get_app_data((SSL *) ssl);
555
 
                        if (where & SSL_CB_READ)
556
 
                                conn->read_alerts++;
557
 
                        else
558
 
                                conn->write_alerts++;
559
 
                }
560
 
        } else if (where & SSL_CB_EXIT && ret <= 0) {
561
 
                wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s",
562
 
                           str, ret == 0 ? "failed" : "error",
563
 
                           SSL_state_string_long(ssl));
564
 
        }
565
 
}
566
 
 
567
 
 
568
 
#ifndef OPENSSL_NO_ENGINE
569
 
/**
570
 
 * tls_engine_load_dynamic_generic - load any openssl engine
571
 
 * @pre: an array of commands and values that load an engine initialized
572
 
 *       in the engine specific function
573
 
 * @post: an array of commands and values that initialize an already loaded
574
 
 *        engine (or %NULL if not required)
575
 
 * @id: the engine id of the engine to load (only required if post is not %NULL
576
 
 *
577
 
 * This function is a generic function that loads any openssl engine.
578
 
 *
579
 
 * Returns: 0 on success, -1 on failure
580
 
 */
581
 
static int tls_engine_load_dynamic_generic(const char *pre[],
582
 
                                           const char *post[], const char *id)
583
 
{
584
 
        ENGINE *engine;
585
 
        const char *dynamic_id = "dynamic";
586
 
 
587
 
        engine = ENGINE_by_id(id);
588
 
        if (engine) {
589
 
                ENGINE_free(engine);
590
 
                wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
591
 
                           "available", id);
592
 
                return 0;
593
 
        }
594
 
        ERR_clear_error();
595
 
 
596
 
        engine = ENGINE_by_id(dynamic_id);
597
 
        if (engine == NULL) {
598
 
                wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
599
 
                           dynamic_id,
600
 
                           ERR_error_string(ERR_get_error(), NULL));
601
 
                return -1;
602
 
        }
603
 
 
604
 
        /* Perform the pre commands. This will load the engine. */
605
 
        while (pre && pre[0]) {
606
 
                wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
607
 
                if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
608
 
                        wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
609
 
                                   "%s %s [%s]", pre[0], pre[1],
610
 
                                   ERR_error_string(ERR_get_error(), NULL));
611
 
                        ENGINE_free(engine);
612
 
                        return -1;
613
 
                }
614
 
                pre += 2;
615
 
        }
616
 
 
617
 
        /*
618
 
         * Free the reference to the "dynamic" engine. The loaded engine can
619
 
         * now be looked up using ENGINE_by_id().
620
 
         */
621
 
        ENGINE_free(engine);
622
 
 
623
 
        engine = ENGINE_by_id(id);
624
 
        if (engine == NULL) {
625
 
                wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
626
 
                           id, ERR_error_string(ERR_get_error(), NULL));
627
 
                return -1;
628
 
        }
629
 
 
630
 
        while (post && post[0]) {
631
 
                wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
632
 
                if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
633
 
                        wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
634
 
                                " %s %s [%s]", post[0], post[1],
635
 
                                   ERR_error_string(ERR_get_error(), NULL));
636
 
                        ENGINE_remove(engine);
637
 
                        ENGINE_free(engine);
638
 
                        return -1;
639
 
                }
640
 
                post += 2;
641
 
        }
642
 
        ENGINE_free(engine);
643
 
 
644
 
        return 0;
645
 
}
646
 
 
647
 
 
648
 
/**
649
 
 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
650
 
 * @pkcs11_so_path: pksc11_so_path from the configuration
651
 
 * @pcks11_module_path: pkcs11_module_path from the configuration
652
 
 */
653
 
static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
654
 
                                          const char *pkcs11_module_path)
655
 
{
656
 
        char *engine_id = "pkcs11";
657
 
        const char *pre_cmd[] = {
658
 
                "SO_PATH", NULL /* pkcs11_so_path */,
659
 
                "ID", NULL /* engine_id */,
660
 
                "LIST_ADD", "1",
661
 
                /* "NO_VCHECK", "1", */
662
 
                "LOAD", NULL,
663
 
                NULL, NULL
664
 
        };
665
 
        const char *post_cmd[] = {
666
 
                "MODULE_PATH", NULL /* pkcs11_module_path */,
667
 
                NULL, NULL
668
 
        };
669
 
 
670
 
        if (!pkcs11_so_path || !pkcs11_module_path)
671
 
                return 0;
672
 
 
673
 
        pre_cmd[1] = pkcs11_so_path;
674
 
        pre_cmd[3] = engine_id;
675
 
        post_cmd[1] = pkcs11_module_path;
676
 
 
677
 
        wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
678
 
                   pkcs11_so_path);
679
 
 
680
 
        return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
681
 
}
682
 
 
683
 
 
684
 
/**
685
 
 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
686
 
 * @opensc_so_path: opensc_so_path from the configuration
687
 
 */
688
 
static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
689
 
{
690
 
        char *engine_id = "opensc";
691
 
        const char *pre_cmd[] = {
692
 
                "SO_PATH", NULL /* opensc_so_path */,
693
 
                "ID", NULL /* engine_id */,
694
 
                "LIST_ADD", "1",
695
 
                "LOAD", NULL,
696
 
                NULL, NULL
697
 
        };
698
 
 
699
 
        if (!opensc_so_path)
700
 
                return 0;
701
 
 
702
 
        pre_cmd[1] = opensc_so_path;
703
 
        pre_cmd[3] = engine_id;
704
 
 
705
 
        wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
706
 
                   opensc_so_path);
707
 
 
708
 
        return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
709
 
}
710
 
#endif /* OPENSSL_NO_ENGINE */
711
 
 
712
 
 
713
 
void * tls_init(const struct tls_config *conf)
714
 
{
715
 
        SSL_CTX *ssl;
716
 
 
717
 
        if (tls_openssl_ref_count == 0) {
718
 
                SSL_load_error_strings();
719
 
                SSL_library_init();
720
 
                /* TODO: if /dev/urandom is available, PRNG is seeded
721
 
                 * automatically. If this is not the case, random data should
722
 
                 * be added here. */
723
 
 
724
 
#ifdef PKCS12_FUNCS
725
 
                PKCS12_PBE_add();
726
 
#endif  /* PKCS12_FUNCS */
727
 
        }
728
 
        tls_openssl_ref_count++;
729
 
 
730
 
        ssl = SSL_CTX_new(TLSv1_method());
731
 
        if (ssl == NULL)
732
 
                return NULL;
733
 
 
734
 
        SSL_CTX_set_info_callback(ssl, ssl_info_cb);
735
 
 
736
 
#ifndef OPENSSL_NO_ENGINE
737
 
        if (conf &&
738
 
            (conf->opensc_engine_path || conf->pkcs11_engine_path ||
739
 
             conf->pkcs11_module_path)) {
740
 
                wpa_printf(MSG_DEBUG, "ENGINE: Loading dynamic engine");
741
 
                ERR_load_ENGINE_strings();
742
 
                ENGINE_load_dynamic();
743
 
 
744
 
                if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
745
 
                    tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
746
 
                                                   conf->pkcs11_module_path)) {
747
 
                        tls_deinit(ssl);
748
 
                        return NULL;
749
 
                }
750
 
        }
751
 
#endif /* OPENSSL_NO_ENGINE */
752
 
 
753
 
        return ssl;
754
 
}
755
 
 
756
 
 
757
 
void tls_deinit(void *ssl_ctx)
758
 
{
759
 
        SSL_CTX *ssl = ssl_ctx;
760
 
        SSL_CTX_free(ssl);
761
 
 
762
 
        tls_openssl_ref_count--;
763
 
        if (tls_openssl_ref_count == 0) {
764
 
#ifndef OPENSSL_NO_ENGINE
765
 
                ENGINE_cleanup();
766
 
#endif /* OPENSSL_NO_ENGINE */
767
 
                CRYPTO_cleanup_all_ex_data();
768
 
                ERR_remove_state(0);
769
 
                ERR_free_strings();
770
 
                EVP_cleanup();
771
 
        }
772
 
}
773
 
 
774
 
 
775
 
static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
776
 
                           const char *pin, const char *key_id)
777
 
{
778
 
#ifndef OPENSSL_NO_ENGINE
779
 
        int ret = -1;
780
 
        if (engine_id == NULL) {
781
 
                wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
782
 
                return -1;
783
 
        }
784
 
        if (pin == NULL) {
785
 
                wpa_printf(MSG_ERROR, "ENGINE: Smartcard PIN not set");
786
 
                return -1;
787
 
        }
788
 
        if (key_id == NULL) {
789
 
                wpa_printf(MSG_ERROR, "ENGINE: Key Id not set");
790
 
                return -1;
791
 
        }
792
 
 
793
 
        ERR_clear_error();
794
 
        conn->engine = ENGINE_by_id(engine_id);
795
 
        if (!conn->engine) {
796
 
                wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
797
 
                           engine_id, ERR_error_string(ERR_get_error(), NULL));
798
 
                goto err;
799
 
        }
800
 
        if (ENGINE_init(conn->engine) != 1) {
801
 
                wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
802
 
                           "(engine: %s) [%s]", engine_id,
803
 
                           ERR_error_string(ERR_get_error(), NULL));
804
 
                goto err;
805
 
        }
806
 
        wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
807
 
 
808
 
        if (ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
809
 
                wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
810
 
                           ERR_error_string(ERR_get_error(), NULL));
811
 
                goto err;
812
 
        }
813
 
        conn->private_key = ENGINE_load_private_key(conn->engine,
814
 
                                                    key_id, NULL, NULL);
815
 
        if (!conn->private_key) {
816
 
                wpa_printf(MSG_ERROR, "ENGINE: cannot load private key with id"
817
 
                                " '%s' [%s]", key_id,
818
 
                           ERR_error_string(ERR_get_error(), NULL));
819
 
                ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
820
 
                goto err;
821
 
        }
822
 
        return 0;
823
 
 
824
 
err:
825
 
        if (conn->engine) {
826
 
                ENGINE_free(conn->engine);
827
 
                conn->engine = NULL;
828
 
        }
829
 
 
830
 
        if (conn->private_key) {
831
 
                EVP_PKEY_free(conn->private_key);
832
 
                conn->private_key = NULL;
833
 
        }
834
 
 
835
 
        return ret;
836
 
#else /* OPENSSL_NO_ENGINE */
837
 
        return 0;
838
 
#endif /* OPENSSL_NO_ENGINE */
839
 
}
840
 
 
841
 
 
842
 
static void tls_engine_deinit(struct tls_connection *conn)
843
 
{
844
 
#ifndef OPENSSL_NO_ENGINE
845
 
        wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
846
 
        if (conn->private_key) {
847
 
                EVP_PKEY_free(conn->private_key);
848
 
                conn->private_key = NULL;
849
 
        }
850
 
        if (conn->engine) {
851
 
                ENGINE_finish(conn->engine);
852
 
                conn->engine = NULL;
853
 
        }
854
 
#endif /* OPENSSL_NO_ENGINE */
855
 
}
856
 
 
857
 
 
858
 
int tls_get_errors(void *ssl_ctx)
859
 
{
860
 
        int count = 0;
861
 
        unsigned long err;
862
 
 
863
 
        while ((err = ERR_get_error())) {
864
 
                wpa_printf(MSG_INFO, "TLS - SSL error: %s",
865
 
                           ERR_error_string(err, NULL));
866
 
                count++;
867
 
        }
868
 
 
869
 
        return count;
870
 
}
871
 
 
872
 
struct tls_connection * tls_connection_init(void *ssl_ctx)
873
 
{
874
 
        SSL_CTX *ssl = ssl_ctx;
875
 
        struct tls_connection *conn;
876
 
 
877
 
        conn = os_zalloc(sizeof(*conn));
878
 
        if (conn == NULL)
879
 
                return NULL;
880
 
        conn->ssl = SSL_new(ssl);
881
 
        if (conn->ssl == NULL) {
882
 
                tls_show_errors(MSG_INFO, __func__,
883
 
                                "Failed to initialize new SSL connection");
884
 
                os_free(conn);
885
 
                return NULL;
886
 
        }
887
 
 
888
 
        SSL_set_app_data(conn->ssl, conn);
889
 
        SSL_set_options(conn->ssl,
890
 
                        SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
891
 
                        SSL_OP_SINGLE_DH_USE);
892
 
 
893
 
        conn->ssl_in = BIO_new(BIO_s_mem());
894
 
        if (!conn->ssl_in) {
895
 
                tls_show_errors(MSG_INFO, __func__,
896
 
                                "Failed to create a new BIO for ssl_in");
897
 
                SSL_free(conn->ssl);
898
 
                os_free(conn);
899
 
                return NULL;
900
 
        }
901
 
 
902
 
        conn->ssl_out = BIO_new(BIO_s_mem());
903
 
        if (!conn->ssl_out) {
904
 
                tls_show_errors(MSG_INFO, __func__,
905
 
                                "Failed to create a new BIO for ssl_out");
906
 
                SSL_free(conn->ssl);
907
 
                BIO_free(conn->ssl_in);
908
 
                os_free(conn);
909
 
                return NULL;
910
 
        }
911
 
 
912
 
        SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
913
 
 
914
 
        return conn;
915
 
}
916
 
 
917
 
 
918
 
void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
919
 
{
920
 
        if (conn == NULL)
921
 
                return;
922
 
        os_free(conn->pre_shared_secret);
923
 
        SSL_free(conn->ssl);
924
 
        tls_engine_deinit(conn);
925
 
        os_free(conn->subject_match);
926
 
        os_free(conn->altsubject_match);
927
 
        os_free(conn);
928
 
}
929
 
 
930
 
 
931
 
int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
932
 
{
933
 
        return conn ? SSL_is_init_finished(conn->ssl) : 0;
934
 
}
935
 
 
936
 
 
937
 
int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
938
 
{
939
 
        if (conn == NULL)
940
 
                return -1;
941
 
 
942
 
        /* Shutdown previous TLS connection without notifying the peer
943
 
         * because the connection was already terminated in practice
944
 
         * and "close notify" shutdown alert would confuse AS. */
945
 
        SSL_set_quiet_shutdown(conn->ssl, 1);
946
 
        SSL_shutdown(conn->ssl);
947
 
        return 0;
948
 
}
949
 
 
950
 
 
951
 
static int tls_match_altsubject_component(X509 *cert, int type,
952
 
                                          const char *value, size_t len)
953
 
{
954
 
        GENERAL_NAME *gen;
955
 
        void *ext;
956
 
        int i, found = 0;
957
 
 
958
 
        ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
959
 
 
960
 
        for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
961
 
                gen = sk_GENERAL_NAME_value(ext, i);
962
 
                if (gen->type != type)
963
 
                        continue;
964
 
                if (os_strlen((char *) gen->d.ia5->data) == len &&
965
 
                    os_memcmp(value, gen->d.ia5->data, len) == 0)
966
 
                        found++;
967
 
        }
968
 
 
969
 
        return found;
970
 
}
971
 
 
972
 
 
973
 
static int tls_match_altsubject(X509 *cert, const char *match)
974
 
{
975
 
        int type;
976
 
        const char *pos, *end;
977
 
        size_t len;
978
 
 
979
 
        pos = match;
980
 
        do {
981
 
                if (os_strncmp(pos, "EMAIL:", 6) == 0) {
982
 
                        type = GEN_EMAIL;
983
 
                        pos += 6;
984
 
                } else if (os_strncmp(pos, "DNS:", 4) == 0) {
985
 
                        type = GEN_DNS;
986
 
                        pos += 4;
987
 
                } else if (os_strncmp(pos, "URI:", 4) == 0) {
988
 
                        type = GEN_URI;
989
 
                        pos += 4;
990
 
                } else {
991
 
                        wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
992
 
                                   "match '%s'", pos);
993
 
                        return 0;
994
 
                }
995
 
                end = os_strchr(pos, ';');
996
 
                while (end) {
997
 
                        if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
998
 
                            os_strncmp(end + 1, "DNS:", 4) == 0 ||
999
 
                            os_strncmp(end + 1, "URI:", 4) == 0)
1000
 
                                break;
1001
 
                        end = os_strchr(end + 1, ';');
1002
 
                }
1003
 
                if (end)
1004
 
                        len = end - pos;
1005
 
                else
1006
 
                        len = os_strlen(pos);
1007
 
                if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1008
 
                        return 1;
1009
 
                pos = end + 1;
1010
 
        } while (end);
1011
 
 
1012
 
        return 0;
1013
 
}
1014
 
 
1015
 
 
1016
 
static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
1017
 
{
1018
 
        char buf[256];
1019
 
        X509 *err_cert;
1020
 
        int err, depth;
1021
 
        SSL *ssl;
1022
 
        struct tls_connection *conn;
1023
 
        char *match, *altmatch;
1024
 
 
1025
 
        err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
1026
 
        err = X509_STORE_CTX_get_error(x509_ctx);
1027
 
        depth = X509_STORE_CTX_get_error_depth(x509_ctx);
1028
 
        ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1029
 
                                         SSL_get_ex_data_X509_STORE_CTX_idx());
1030
 
        X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
1031
 
 
1032
 
        conn = SSL_get_app_data(ssl);
1033
 
        match = conn ? conn->subject_match : NULL;
1034
 
        altmatch = conn ? conn->altsubject_match : NULL;
1035
 
 
1036
 
        if (!preverify_ok) {
1037
 
                wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
1038
 
                           " error %d (%s) depth %d for '%s'", err,
1039
 
                           X509_verify_cert_error_string(err), depth, buf);
1040
 
        } else {
1041
 
                wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - "
1042
 
                           "preverify_ok=%d err=%d (%s) depth=%d buf='%s'",
1043
 
                           preverify_ok, err,
1044
 
                           X509_verify_cert_error_string(err), depth, buf);
1045
 
                if (depth == 0 && match && os_strstr(buf, match) == NULL) {
1046
 
                        wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
1047
 
                                   "match with '%s'", buf, match);
1048
 
                        preverify_ok = 0;
1049
 
                } else if (depth == 0 && altmatch &&
1050
 
                           !tls_match_altsubject(err_cert, altmatch)) {
1051
 
                        wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
1052
 
                                   "'%s' not found", altmatch);
1053
 
                        preverify_ok = 0;
1054
 
                }
1055
 
        }
1056
 
 
1057
 
        return preverify_ok;
1058
 
}
1059
 
 
1060
 
 
1061
 
#ifndef OPENSSL_NO_STDIO
1062
 
static int tls_load_ca_der(void *_ssl_ctx, const char *ca_cert)
1063
 
{
1064
 
        SSL_CTX *ssl_ctx = _ssl_ctx;
1065
 
        X509_LOOKUP *lookup;
1066
 
        int ret = 0;
1067
 
 
1068
 
        lookup = X509_STORE_add_lookup(ssl_ctx->cert_store,
1069
 
                                       X509_LOOKUP_file());
1070
 
        if (lookup == NULL) {
1071
 
                tls_show_errors(MSG_WARNING, __func__,
1072
 
                                "Failed add lookup for X509 store");
1073
 
                return -1;
1074
 
        }
1075
 
 
1076
 
        if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
1077
 
                unsigned long err = ERR_peek_error();
1078
 
                tls_show_errors(MSG_WARNING, __func__,
1079
 
                                "Failed load CA in DER format");
1080
 
                if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
1081
 
                    ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1082
 
                        wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
1083
 
                                   "cert already in hash table error",
1084
 
                                   __func__);
1085
 
                } else
1086
 
                        ret = -1;
1087
 
        }
1088
 
 
1089
 
        return ret;
1090
 
}
1091
 
#endif /* OPENSSL_NO_STDIO */
1092
 
 
1093
 
 
1094
 
static int tls_connection_ca_cert(void *_ssl_ctx, struct tls_connection *conn,
1095
 
                                  const char *ca_cert, const u8 *ca_cert_blob,
1096
 
                                  size_t ca_cert_blob_len, const char *ca_path)
1097
 
{
1098
 
        SSL_CTX *ssl_ctx = _ssl_ctx;
1099
 
 
1100
 
        if (ca_cert_blob) {
1101
 
                X509 *cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &ca_cert_blob,
1102
 
                                      ca_cert_blob_len);
1103
 
                if (cert == NULL) {
1104
 
                        tls_show_errors(MSG_WARNING, __func__,
1105
 
                                        "Failed to parse ca_cert_blob");
1106
 
                        return -1;
1107
 
                }
1108
 
 
1109
 
                if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) {
1110
 
                        unsigned long err = ERR_peek_error();
1111
 
                        tls_show_errors(MSG_WARNING, __func__,
1112
 
                                        "Failed to add ca_cert_blob to "
1113
 
                                        "certificate store");
1114
 
                        if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
1115
 
                            ERR_GET_REASON(err) ==
1116
 
                            X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1117
 
                                wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
1118
 
                                           "cert already in hash table error",
1119
 
                                           __func__);
1120
 
                        } else {
1121
 
                                X509_free(cert);
1122
 
                                return -1;
1123
 
                        }
1124
 
                }
1125
 
                X509_free(cert);
1126
 
                wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
1127
 
                           "to certificate store", __func__);
1128
 
                SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1129
 
                return 0;
1130
 
        }
1131
 
 
1132
 
#ifdef CONFIG_NATIVE_WINDOWS
1133
 
        if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
1134
 
            0) {
1135
 
                wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
1136
 
                           "system certificate store");
1137
 
                SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1138
 
                return 0;
1139
 
        }
1140
 
#endif /* CONFIG_NATIVE_WINDOWS */
1141
 
 
1142
 
        if (ca_cert || ca_path) {
1143
 
#ifndef OPENSSL_NO_STDIO
1144
 
                if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
1145
 
                    1) {
1146
 
                        tls_show_errors(MSG_WARNING, __func__,
1147
 
                                        "Failed to load root certificates");
1148
 
                        if (ca_cert &&
1149
 
                            tls_load_ca_der(ssl_ctx, ca_cert) == 0) {
1150
 
                                wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
1151
 
                                           "DER format CA certificate",
1152
 
                                           __func__);
1153
 
                        } else
1154
 
                                return -1;
1155
 
                } else {
1156
 
                        wpa_printf(MSG_DEBUG, "TLS: Trusted root "
1157
 
                                   "certificate(s) loaded");
1158
 
                        tls_get_errors(ssl_ctx);
1159
 
                }
1160
 
                SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1161
 
#else /* OPENSSL_NO_STDIO */
1162
 
                wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
1163
 
                           __func__);
1164
 
                return -1;
1165
 
#endif /* OPENSSL_NO_STDIO */
1166
 
        } else {
1167
 
                /* No ca_cert configured - do not try to verify server
1168
 
                 * certificate */
1169
 
                SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
1170
 
        }
1171
 
 
1172
 
        return 0;
1173
 
}
1174
 
 
1175
 
 
1176
 
static int tls_global_ca_cert(SSL_CTX *ssl_ctx, const char *ca_cert)
1177
 
{
1178
 
        if (ca_cert) {
1179
 
                if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
1180
 
                {
1181
 
                        tls_show_errors(MSG_WARNING, __func__,
1182
 
                                        "Failed to load root certificates");
1183
 
                        return -1;
1184
 
                }
1185
 
 
1186
 
                wpa_printf(MSG_DEBUG, "TLS: Trusted root "
1187
 
                           "certificate(s) loaded");
1188
 
 
1189
 
#ifndef OPENSSL_NO_STDIO
1190
 
                /* Add the same CAs to the client certificate requests */
1191
 
                SSL_CTX_set_client_CA_list(ssl_ctx,
1192
 
                                           SSL_load_client_CA_file(ca_cert));
1193
 
#endif /* OPENSSL_NO_STDIO */
1194
 
        }
1195
 
 
1196
 
        return 0;
1197
 
}
1198
 
 
1199
 
 
1200
 
int tls_global_set_verify(void *ssl_ctx, int check_crl)
1201
 
{
1202
 
        int flags;
1203
 
 
1204
 
        if (check_crl) {
1205
 
                X509_STORE *cs = SSL_CTX_get_cert_store(ssl_ctx);
1206
 
                if (cs == NULL) {
1207
 
                        tls_show_errors(MSG_INFO, __func__, "Failed to get "
1208
 
                                        "certificate store when enabling "
1209
 
                                        "check_crl");
1210
 
                        return -1;
1211
 
                }
1212
 
                flags = X509_V_FLAG_CRL_CHECK;
1213
 
                if (check_crl == 2)
1214
 
                        flags |= X509_V_FLAG_CRL_CHECK_ALL;
1215
 
                X509_STORE_set_flags(cs, flags);
1216
 
        }
1217
 
        return 0;
1218
 
}
1219
 
 
1220
 
 
1221
 
static int tls_connection_set_subject_match(struct tls_connection *conn,
1222
 
                                            const char *subject_match,
1223
 
                                            const char *altsubject_match)
1224
 
{
1225
 
        os_free(conn->subject_match);
1226
 
        conn->subject_match = NULL;
1227
 
        if (subject_match) {
1228
 
                conn->subject_match = os_strdup(subject_match);
1229
 
                if (conn->subject_match == NULL)
1230
 
                        return -1;
1231
 
        }
1232
 
 
1233
 
        os_free(conn->altsubject_match);
1234
 
        conn->altsubject_match = NULL;
1235
 
        if (altsubject_match) {
1236
 
                conn->altsubject_match = os_strdup(altsubject_match);
1237
 
                if (conn->altsubject_match == NULL)
1238
 
                        return -1;
1239
 
        }
1240
 
 
1241
 
        return 0;
1242
 
}
1243
 
 
1244
 
 
1245
 
int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
1246
 
                              int verify_peer)
1247
 
{
1248
 
        if (conn == NULL)
1249
 
                return -1;
1250
 
 
1251
 
        if (verify_peer) {
1252
 
                SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
1253
 
                               SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
1254
 
                               SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
1255
 
        } else {
1256
 
                SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
1257
 
        }
1258
 
 
1259
 
        SSL_set_accept_state(conn->ssl);
1260
 
 
1261
 
        return 0;
1262
 
}
1263
 
 
1264
 
 
1265
 
static int tls_connection_client_cert(struct tls_connection *conn,
1266
 
                                      const char *client_cert,
1267
 
                                      const u8 *client_cert_blob,
1268
 
                                      size_t client_cert_blob_len)
1269
 
{
1270
 
        if (client_cert == NULL && client_cert_blob == NULL)
1271
 
                return 0;
1272
 
 
1273
 
        if (client_cert_blob &&
1274
 
            SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
1275
 
                                     client_cert_blob_len) == 1) {
1276
 
                wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
1277
 
                           "OK");
1278
 
                return 0;
1279
 
        } else if (client_cert_blob) {
1280
 
                tls_show_errors(MSG_DEBUG, __func__,
1281
 
                                "SSL_use_certificate_ASN1 failed");
1282
 
        }
1283
 
 
1284
 
        if (client_cert == NULL)
1285
 
                return -1;
1286
 
 
1287
 
#ifndef OPENSSL_NO_STDIO
1288
 
        if (SSL_use_certificate_file(conn->ssl, client_cert,
1289
 
                                     SSL_FILETYPE_ASN1) == 1) {
1290
 
                wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
1291
 
                           " --> OK");
1292
 
                return 0;
1293
 
        } else {
1294
 
                tls_show_errors(MSG_DEBUG, __func__,
1295
 
                                "SSL_use_certificate_file (DER) failed");
1296
 
        }
1297
 
 
1298
 
        if (SSL_use_certificate_file(conn->ssl, client_cert,
1299
 
                                     SSL_FILETYPE_PEM) == 1) {
1300
 
                wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
1301
 
                           " --> OK");
1302
 
                return 0;
1303
 
        } else {
1304
 
                tls_show_errors(MSG_DEBUG, __func__,
1305
 
                                "SSL_use_certificate_file (PEM) failed");
1306
 
        }
1307
 
#else /* OPENSSL_NO_STDIO */
1308
 
        wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
1309
 
#endif /* OPENSSL_NO_STDIO */
1310
 
 
1311
 
        return -1;
1312
 
}
1313
 
 
1314
 
 
1315
 
static int tls_global_client_cert(SSL_CTX *ssl_ctx, const char *client_cert)
1316
 
{
1317
 
#ifndef OPENSSL_NO_STDIO
1318
 
        if (client_cert == NULL)
1319
 
                return 0;
1320
 
 
1321
 
        if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
1322
 
                                         SSL_FILETYPE_ASN1) != 1 &&
1323
 
            SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
1324
 
                                         SSL_FILETYPE_PEM) != 1) {
1325
 
                tls_show_errors(MSG_INFO, __func__,
1326
 
                                "Failed to load client certificate");
1327
 
                return -1;
1328
 
        }
1329
 
        return 0;
1330
 
#else /* OPENSSL_NO_STDIO */
1331
 
        if (client_cert == NULL)
1332
 
                return 0;
1333
 
        wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
1334
 
        return -1;
1335
 
#endif /* OPENSSL_NO_STDIO */
1336
 
}
1337
 
 
1338
 
 
1339
 
static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
1340
 
{
1341
 
        if (password == NULL) {
1342
 
                return 0;
1343
 
        }
1344
 
        os_strlcpy(buf, (char *) password, size);
1345
 
        return os_strlen(buf);
1346
 
}
1347
 
 
1348
 
 
1349
 
#ifdef PKCS12_FUNCS
1350
 
static int tls_parse_pkcs12(SSL_CTX *ssl_ctx, SSL *ssl, PKCS12 *p12,
1351
 
                            const char *passwd)
1352
 
{
1353
 
        EVP_PKEY *pkey;
1354
 
        X509 *cert;
1355
 
        STACK_OF(X509) *certs;
1356
 
        int res = 0;
1357
 
        char buf[256];
1358
 
 
1359
 
        pkey = NULL;
1360
 
        cert = NULL;
1361
 
        certs = NULL;
1362
 
        if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
1363
 
                tls_show_errors(MSG_DEBUG, __func__,
1364
 
                                "Failed to parse PKCS12 file");
1365
 
                PKCS12_free(p12);
1366
 
                return -1;
1367
 
        }
1368
 
        wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
1369
 
 
1370
 
        if (cert) {
1371
 
                X509_NAME_oneline(X509_get_subject_name(cert), buf,
1372
 
                                  sizeof(buf));
1373
 
                wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
1374
 
                           "subject='%s'", buf);
1375
 
                if (ssl) {
1376
 
                        if (SSL_use_certificate(ssl, cert) != 1)
1377
 
                                res = -1;
1378
 
                } else {
1379
 
                        if (SSL_CTX_use_certificate(ssl_ctx, cert) != 1)
1380
 
                                res = -1;
1381
 
                }
1382
 
                X509_free(cert);
1383
 
        }
1384
 
 
1385
 
        if (pkey) {
1386
 
                wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
1387
 
                if (ssl) {
1388
 
                        if (SSL_use_PrivateKey(ssl, pkey) != 1)
1389
 
                                res = -1;
1390
 
                } else {
1391
 
                        if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) != 1)
1392
 
                                res = -1;
1393
 
                }
1394
 
                EVP_PKEY_free(pkey);
1395
 
        }
1396
 
 
1397
 
        if (certs) {
1398
 
                while ((cert = sk_X509_pop(certs)) != NULL) {
1399
 
                        X509_NAME_oneline(X509_get_subject_name(cert), buf,
1400
 
                                          sizeof(buf));
1401
 
                        wpa_printf(MSG_DEBUG, "TLS: additional certificate"
1402
 
                                   " from PKCS12: subject='%s'", buf);
1403
 
                        /*
1404
 
                         * There is no SSL equivalent for the chain cert - so
1405
 
                         * always add it to the context...
1406
 
                         */
1407
 
                        if (SSL_CTX_add_extra_chain_cert(ssl_ctx, cert) != 1) {
1408
 
                                res = -1;
1409
 
                                break;
1410
 
                        }
1411
 
                }
1412
 
                sk_X509_free(certs);
1413
 
        }
1414
 
 
1415
 
        PKCS12_free(p12);
1416
 
 
1417
 
        if (res < 0)
1418
 
                tls_get_errors(ssl_ctx);
1419
 
 
1420
 
        return res;
1421
 
}
1422
 
#endif  /* PKCS12_FUNCS */
1423
 
 
1424
 
 
1425
 
static int tls_read_pkcs12(SSL_CTX *ssl_ctx, SSL *ssl, const char *private_key,
1426
 
                           const char *passwd)
1427
 
{
1428
 
#ifdef PKCS12_FUNCS
1429
 
        FILE *f;
1430
 
        PKCS12 *p12;
1431
 
 
1432
 
        f = fopen(private_key, "rb");
1433
 
        if (f == NULL)
1434
 
                return -1;
1435
 
 
1436
 
        p12 = d2i_PKCS12_fp(f, NULL);
1437
 
        fclose(f);
1438
 
 
1439
 
        if (p12 == NULL) {
1440
 
                tls_show_errors(MSG_INFO, __func__,
1441
 
                                "Failed to use PKCS#12 file");
1442
 
                return -1;
1443
 
        }
1444
 
 
1445
 
        return tls_parse_pkcs12(ssl_ctx, ssl, p12, passwd);
1446
 
 
1447
 
#else /* PKCS12_FUNCS */
1448
 
        wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
1449
 
                   "p12/pfx files");
1450
 
        return -1;
1451
 
#endif  /* PKCS12_FUNCS */
1452
 
}
1453
 
 
1454
 
 
1455
 
static int tls_read_pkcs12_blob(SSL_CTX *ssl_ctx, SSL *ssl,
1456
 
                                const u8 *blob, size_t len, const char *passwd)
1457
 
{
1458
 
#ifdef PKCS12_FUNCS
1459
 
        PKCS12 *p12;
1460
 
 
1461
 
        p12 = d2i_PKCS12(NULL, (OPENSSL_d2i_TYPE) &blob, len);
1462
 
        if (p12 == NULL) {
1463
 
                tls_show_errors(MSG_INFO, __func__,
1464
 
                                "Failed to use PKCS#12 blob");
1465
 
                return -1;
1466
 
        }
1467
 
 
1468
 
        return tls_parse_pkcs12(ssl_ctx, ssl, p12, passwd);
1469
 
 
1470
 
#else /* PKCS12_FUNCS */
1471
 
        wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
1472
 
                   "p12/pfx blobs");
1473
 
        return -1;
1474
 
#endif  /* PKCS12_FUNCS */
1475
 
}
1476
 
 
1477
 
 
1478
 
static int tls_connection_engine_private_key(struct tls_connection *conn)
1479
 
{
1480
 
#ifndef OPENSSL_NO_ENGINE
1481
 
        if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
1482
 
                tls_show_errors(MSG_ERROR, __func__,
1483
 
                                "ENGINE: cannot use private key for TLS");
1484
 
                return -1;
1485
 
        }
1486
 
        if (!SSL_check_private_key(conn->ssl)) {
1487
 
                tls_show_errors(MSG_INFO, __func__,
1488
 
                                "Private key failed verification");
1489
 
                return -1;
1490
 
        }
1491
 
        return 0;
1492
 
#else /* OPENSSL_NO_ENGINE */
1493
 
        wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
1494
 
                   "engine support was not compiled in");
1495
 
        return -1;
1496
 
#endif /* OPENSSL_NO_ENGINE */
1497
 
}
1498
 
 
1499
 
 
1500
 
static int tls_connection_private_key(void *_ssl_ctx,
1501
 
                                      struct tls_connection *conn,
1502
 
                                      const char *private_key,
1503
 
                                      const char *private_key_passwd,
1504
 
                                      const u8 *private_key_blob,
1505
 
                                      size_t private_key_blob_len)
1506
 
{
1507
 
        SSL_CTX *ssl_ctx = _ssl_ctx;
1508
 
        char *passwd;
1509
 
        int ok;
1510
 
 
1511
 
        if (private_key == NULL && private_key_blob == NULL)
1512
 
                return 0;
1513
 
 
1514
 
        if (private_key_passwd) {
1515
 
                passwd = os_strdup(private_key_passwd);
1516
 
                if (passwd == NULL)
1517
 
                        return -1;
1518
 
        } else
1519
 
                passwd = NULL;
1520
 
 
1521
 
        SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
1522
 
        SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
1523
 
 
1524
 
        ok = 0;
1525
 
        while (private_key_blob) {
1526
 
                if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
1527
 
                                            (u8 *) private_key_blob,
1528
 
                                            private_key_blob_len) == 1) {
1529
 
                        wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
1530
 
                                   "ASN1(EVP_PKEY_RSA) --> OK");
1531
 
                        ok = 1;
1532
 
                        break;
1533
 
                } else {
1534
 
                        tls_show_errors(MSG_DEBUG, __func__,
1535
 
                                        "SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA)"
1536
 
                                        " failed");
1537
 
                }
1538
 
 
1539
 
                if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
1540
 
                                            (u8 *) private_key_blob,
1541
 
                                            private_key_blob_len) == 1) {
1542
 
                        wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
1543
 
                                   "ASN1(EVP_PKEY_DSA) --> OK");
1544
 
                        ok = 1;
1545
 
                        break;
1546
 
                } else {
1547
 
                        tls_show_errors(MSG_DEBUG, __func__,
1548
 
                                        "SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA)"
1549
 
                                        " failed");
1550
 
                }
1551
 
 
1552
 
                if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
1553
 
                                               (u8 *) private_key_blob,
1554
 
                                               private_key_blob_len) == 1) {
1555
 
                        wpa_printf(MSG_DEBUG, "OpenSSL: "
1556
 
                                   "SSL_use_RSAPrivateKey_ASN1 --> OK");
1557
 
                        ok = 1;
1558
 
                        break;
1559
 
                } else {
1560
 
                        tls_show_errors(MSG_DEBUG, __func__,
1561
 
                                        "SSL_use_RSAPrivateKey_ASN1 failed");
1562
 
                }
1563
 
 
1564
 
                if (tls_read_pkcs12_blob(ssl_ctx, conn->ssl, private_key_blob,
1565
 
                                         private_key_blob_len, passwd) == 0) {
1566
 
                        wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
1567
 
                                   "OK");
1568
 
                        ok = 1;
1569
 
                        break;
1570
 
                }
1571
 
 
1572
 
                break;
1573
 
        }
1574
 
 
1575
 
        while (!ok && private_key) {
1576
 
#ifndef OPENSSL_NO_STDIO
1577
 
                if (SSL_use_PrivateKey_file(conn->ssl, private_key,
1578
 
                                            SSL_FILETYPE_ASN1) == 1) {
1579
 
                        wpa_printf(MSG_DEBUG, "OpenSSL: "
1580
 
                                   "SSL_use_PrivateKey_File (DER) --> OK");
1581
 
                        ok = 1;
1582
 
                        break;
1583
 
                } else {
1584
 
                        tls_show_errors(MSG_DEBUG, __func__,
1585
 
                                        "SSL_use_PrivateKey_File (DER) "
1586
 
                                        "failed");
1587
 
                }
1588
 
 
1589
 
                if (SSL_use_PrivateKey_file(conn->ssl, private_key,
1590
 
                                            SSL_FILETYPE_PEM) == 1) {
1591
 
                        wpa_printf(MSG_DEBUG, "OpenSSL: "
1592
 
                                   "SSL_use_PrivateKey_File (PEM) --> OK");
1593
 
                        ok = 1;
1594
 
                        break;
1595
 
                } else {
1596
 
                        tls_show_errors(MSG_DEBUG, __func__,
1597
 
                                        "SSL_use_PrivateKey_File (PEM) "
1598
 
                                        "failed");
1599
 
                }
1600
 
#else /* OPENSSL_NO_STDIO */
1601
 
                wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
1602
 
                           __func__);
1603
 
#endif /* OPENSSL_NO_STDIO */
1604
 
 
1605
 
                if (tls_read_pkcs12(ssl_ctx, conn->ssl, private_key, passwd)
1606
 
                    == 0) {
1607
 
                        wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
1608
 
                                   "--> OK");
1609
 
                        ok = 1;
1610
 
                        break;
1611
 
                }
1612
 
 
1613
 
                if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
1614
 
                        wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
1615
 
                                   "access certificate store --> OK");
1616
 
                        ok = 1;
1617
 
                        break;
1618
 
                }
1619
 
 
1620
 
                break;
1621
 
        }
1622
 
 
1623
 
        if (!ok) {
1624
 
                wpa_printf(MSG_INFO, "OpenSSL: Failed to load private key");
1625
 
                os_free(passwd);
1626
 
                ERR_clear_error();
1627
 
                return -1;
1628
 
        }
1629
 
        ERR_clear_error();
1630
 
        SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
1631
 
        os_free(passwd);
1632
 
        
1633
 
        if (!SSL_check_private_key(conn->ssl)) {
1634
 
                tls_show_errors(MSG_INFO, __func__, "Private key failed "
1635
 
                                "verification");
1636
 
                return -1;
1637
 
        }
1638
 
 
1639
 
        wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
1640
 
        return 0;
1641
 
}
1642
 
 
1643
 
 
1644
 
static int tls_global_private_key(SSL_CTX *ssl_ctx, const char *private_key,
1645
 
                                  const char *private_key_passwd)
1646
 
{
1647
 
        char *passwd;
1648
 
 
1649
 
        if (private_key == NULL)
1650
 
                return 0;
1651
 
 
1652
 
        if (private_key_passwd) {
1653
 
                passwd = os_strdup(private_key_passwd);
1654
 
                if (passwd == NULL)
1655
 
                        return -1;
1656
 
        } else
1657
 
                passwd = NULL;
1658
 
 
1659
 
        SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
1660
 
        SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
1661
 
        if (
1662
 
#ifndef OPENSSL_NO_STDIO
1663
 
            SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
1664
 
                                        SSL_FILETYPE_ASN1) != 1 &&
1665
 
            SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
1666
 
                                        SSL_FILETYPE_PEM) != 1 &&
1667
 
#endif /* OPENSSL_NO_STDIO */
1668
 
            tls_read_pkcs12(ssl_ctx, NULL, private_key, passwd)) {
1669
 
                tls_show_errors(MSG_INFO, __func__,
1670
 
                                "Failed to load private key");
1671
 
                os_free(passwd);
1672
 
                ERR_clear_error();
1673
 
                return -1;
1674
 
        }
1675
 
        os_free(passwd);
1676
 
        ERR_clear_error();
1677
 
        SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
1678
 
        
1679
 
        if (!SSL_CTX_check_private_key(ssl_ctx)) {
1680
 
                tls_show_errors(MSG_INFO, __func__,
1681
 
                                "Private key failed verification");
1682
 
                return -1;
1683
 
        }
1684
 
 
1685
 
        return 0;
1686
 
}
1687
 
 
1688
 
 
1689
 
static int tls_connection_dh(struct tls_connection *conn, const char *dh_file)
1690
 
{
1691
 
#ifdef OPENSSL_NO_DH
1692
 
        if (dh_file == NULL)
1693
 
                return 0;
1694
 
        wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
1695
 
                   "dh_file specified");
1696
 
        return -1;
1697
 
#else /* OPENSSL_NO_DH */
1698
 
        DH *dh;
1699
 
        BIO *bio;
1700
 
 
1701
 
        /* TODO: add support for dh_blob */
1702
 
        if (dh_file == NULL)
1703
 
                return 0;
1704
 
        if (conn == NULL)
1705
 
                return -1;
1706
 
 
1707
 
        bio = BIO_new_file(dh_file, "r");
1708
 
        if (bio == NULL) {
1709
 
                wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
1710
 
                           dh_file, ERR_error_string(ERR_get_error(), NULL));
1711
 
                return -1;
1712
 
        }
1713
 
        dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1714
 
        BIO_free(bio);
1715
 
#ifndef OPENSSL_NO_DSA
1716
 
        while (dh == NULL) {
1717
 
                DSA *dsa;
1718
 
                wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
1719
 
                           " trying to parse as DSA params", dh_file,
1720
 
                           ERR_error_string(ERR_get_error(), NULL));
1721
 
                bio = BIO_new_file(dh_file, "r");
1722
 
                if (bio == NULL)
1723
 
                        break;
1724
 
                dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
1725
 
                BIO_free(bio);
1726
 
                if (!dsa) {
1727
 
                        wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
1728
 
                                   "'%s': %s", dh_file,
1729
 
                                   ERR_error_string(ERR_get_error(), NULL));
1730
 
                        break;
1731
 
                }
1732
 
 
1733
 
                wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
1734
 
                dh = DSA_dup_DH(dsa);
1735
 
                DSA_free(dsa);
1736
 
                if (dh == NULL) {
1737
 
                        wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
1738
 
                                   "params into DH params");
1739
 
                        break;
1740
 
                }
1741
 
                break;
1742
 
        }
1743
 
#endif /* !OPENSSL_NO_DSA */
1744
 
        if (dh == NULL) {
1745
 
                wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
1746
 
                           "'%s'", dh_file);
1747
 
                return -1;
1748
 
        }
1749
 
 
1750
 
        if (SSL_set_tmp_dh(conn->ssl, dh) != 1) {
1751
 
                wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
1752
 
                           "%s", dh_file,
1753
 
                           ERR_error_string(ERR_get_error(), NULL));
1754
 
                DH_free(dh);
1755
 
                return -1;
1756
 
        }
1757
 
        DH_free(dh);
1758
 
        return 0;
1759
 
#endif /* OPENSSL_NO_DH */
1760
 
}
1761
 
 
1762
 
 
1763
 
int tls_connection_get_keys(void *ssl_ctx, struct tls_connection *conn,
1764
 
                            struct tls_keys *keys)
1765
 
{
1766
 
        SSL *ssl;
1767
 
 
1768
 
        if (conn == NULL || keys == NULL)
1769
 
                return -1;
1770
 
        ssl = conn->ssl;
1771
 
        if (ssl == NULL || ssl->s3 == NULL || ssl->session == NULL)
1772
 
                return -1;
1773
 
 
1774
 
        os_memset(keys, 0, sizeof(*keys));
1775
 
        keys->master_key = ssl->session->master_key;
1776
 
        keys->master_key_len = ssl->session->master_key_length;
1777
 
        keys->client_random = ssl->s3->client_random;
1778
 
        keys->client_random_len = SSL3_RANDOM_SIZE;
1779
 
        keys->server_random = ssl->s3->server_random;
1780
 
        keys->server_random_len = SSL3_RANDOM_SIZE;
1781
 
 
1782
 
        return 0;
1783
 
}
1784
 
 
1785
 
 
1786
 
int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
1787
 
                       const char *label, int server_random_first,
1788
 
                       u8 *out, size_t out_len)
1789
 
{
1790
 
        return -1;
1791
 
}
1792
 
 
1793
 
 
1794
 
u8 * tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
1795
 
                              const u8 *in_data, size_t in_len,
1796
 
                              size_t *out_len, u8 **appl_data,
1797
 
                              size_t *appl_data_len)
1798
 
{
1799
 
        int res;
1800
 
        u8 *out_data;
1801
 
 
1802
 
        if (appl_data)
1803
 
                *appl_data = NULL;
1804
 
 
1805
 
        /*
1806
 
         * Give TLS handshake data from the server (if available) to OpenSSL
1807
 
         * for processing.
1808
 
         */
1809
 
        if (in_data &&
1810
 
            BIO_write(conn->ssl_in, in_data, in_len) < 0) {
1811
 
                tls_show_errors(MSG_INFO, __func__,
1812
 
                                "Handshake failed - BIO_write");
1813
 
                return NULL;
1814
 
        }
1815
 
 
1816
 
        /* Initiate TLS handshake or continue the existing handshake */
1817
 
        res = SSL_connect(conn->ssl);
1818
 
        if (res != 1) {
1819
 
                int err = SSL_get_error(conn->ssl, res);
1820
 
                if (err == SSL_ERROR_WANT_READ)
1821
 
                        wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
1822
 
                                   "more data");
1823
 
                else if (err == SSL_ERROR_WANT_WRITE)
1824
 
                        wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
1825
 
                                   "write");
1826
 
                else {
1827
 
                        tls_show_errors(MSG_INFO, __func__, "SSL_connect");
1828
 
                        conn->failed++;
1829
 
                }
1830
 
        }
1831
 
 
1832
 
        /* Get the TLS handshake data to be sent to the server */
1833
 
        res = BIO_ctrl_pending(conn->ssl_out);
1834
 
        wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
1835
 
        out_data = os_malloc(res == 0 ? 1 : res);
1836
 
        if (out_data == NULL) {
1837
 
                wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
1838
 
                           "handshake output (%d bytes)", res);
1839
 
                if (BIO_reset(conn->ssl_out) < 0) {
1840
 
                        tls_show_errors(MSG_INFO, __func__,
1841
 
                                        "BIO_reset failed");
1842
 
                }
1843
 
                *out_len = 0;
1844
 
                return NULL;
1845
 
        }
1846
 
        res = res == 0 ? 0 : BIO_read(conn->ssl_out, out_data, res);
1847
 
        if (res < 0) {
1848
 
                tls_show_errors(MSG_INFO, __func__,
1849
 
                                "Handshake failed - BIO_read");
1850
 
                if (BIO_reset(conn->ssl_out) < 0) {
1851
 
                        tls_show_errors(MSG_INFO, __func__,
1852
 
                                        "BIO_reset failed");
1853
 
                }
1854
 
                *out_len = 0;
1855
 
                return NULL;
1856
 
        }
1857
 
        *out_len = res;
1858
 
 
1859
 
        if (SSL_is_init_finished(conn->ssl) && appl_data) {
1860
 
                *appl_data = os_malloc(in_len);
1861
 
                if (*appl_data) {
1862
 
                        res = SSL_read(conn->ssl, *appl_data, in_len);
1863
 
                        if (res < 0) {
1864
 
                                tls_show_errors(MSG_INFO, __func__,
1865
 
                                                "Failed to read possible "
1866
 
                                                "Application Data");
1867
 
                                os_free(*appl_data);
1868
 
                                *appl_data = NULL;
1869
 
                        } else {
1870
 
                                *appl_data_len = res;
1871
 
                                wpa_hexdump_key(MSG_MSGDUMP, "SSL: Application"
1872
 
                                                " Data in Finish message",
1873
 
                                                *appl_data, *appl_data_len);
1874
 
                        }
1875
 
                }
1876
 
        }
1877
 
 
1878
 
        return out_data;
1879
 
}
1880
 
 
1881
 
 
1882
 
u8 * tls_connection_server_handshake(void *ssl_ctx,
1883
 
                                     struct tls_connection *conn,
1884
 
                                     const u8 *in_data, size_t in_len,
1885
 
                                     size_t *out_len)
1886
 
{
1887
 
        int res;
1888
 
        u8 *out_data;
1889
 
        char buf[10];
1890
 
 
1891
 
        if (in_data &&
1892
 
            BIO_write(conn->ssl_in, in_data, in_len) < 0) {
1893
 
                tls_show_errors(MSG_INFO, __func__,
1894
 
                                "Handshake failed - BIO_write");
1895
 
                return NULL;
1896
 
        }
1897
 
 
1898
 
        res = SSL_read(conn->ssl, buf, sizeof(buf));
1899
 
        if (res >= 0) {
1900
 
                wpa_printf(MSG_DEBUG, "SSL: Unexpected data from SSL_read "
1901
 
                           "(res=%d)", res);
1902
 
        }
1903
 
 
1904
 
        res = BIO_ctrl_pending(conn->ssl_out);
1905
 
        wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
1906
 
        out_data = os_malloc(res == 0 ? 1 : res);
1907
 
        if (out_data == NULL) {
1908
 
                wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
1909
 
                           "handshake output (%d bytes)", res);
1910
 
                if (BIO_reset(conn->ssl_out) < 0) {
1911
 
                        tls_show_errors(MSG_INFO, __func__,
1912
 
                                        "BIO_reset failed");
1913
 
                }
1914
 
                *out_len = 0;
1915
 
                return NULL;
1916
 
        }
1917
 
        res = res == 0 ? 0 : BIO_read(conn->ssl_out, out_data, res);
1918
 
        if (res < 0) {
1919
 
                tls_show_errors(MSG_INFO, __func__,
1920
 
                                "Handshake failed - BIO_read");
1921
 
                if (BIO_reset(conn->ssl_out) < 0) {
1922
 
                        tls_show_errors(MSG_INFO, __func__,
1923
 
                                        "BIO_reset failed");
1924
 
                }
1925
 
                *out_len = 0;
1926
 
                return NULL;
1927
 
        }
1928
 
        *out_len = res;
1929
 
        return out_data;
1930
 
}
1931
 
 
1932
 
 
1933
 
int tls_connection_encrypt(void *ssl_ctx, struct tls_connection *conn,
1934
 
                           const u8 *in_data, size_t in_len,
1935
 
                           u8 *out_data, size_t out_len)
1936
 
{
1937
 
        int res;
1938
 
 
1939
 
        if (conn == NULL)
1940
 
                return -1;
1941
 
 
1942
 
        /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
1943
 
        if ((res = BIO_reset(conn->ssl_in)) < 0 ||
1944
 
            (res = BIO_reset(conn->ssl_out)) < 0) {
1945
 
                tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
1946
 
                return res;
1947
 
        }
1948
 
        res = SSL_write(conn->ssl, in_data, in_len);
1949
 
        if (res < 0) {
1950
 
                tls_show_errors(MSG_INFO, __func__,
1951
 
                                "Encryption failed - SSL_write");
1952
 
                return res;
1953
 
        }
1954
 
 
1955
 
        /* Read encrypted data to be sent to the server */
1956
 
        res = BIO_read(conn->ssl_out, out_data, out_len);
1957
 
        if (res < 0) {
1958
 
                tls_show_errors(MSG_INFO, __func__,
1959
 
                                "Encryption failed - BIO_read");
1960
 
                return res;
1961
 
        }
1962
 
 
1963
 
        return res;
1964
 
}
1965
 
 
1966
 
 
1967
 
int tls_connection_decrypt(void *ssl_ctx, struct tls_connection *conn,
1968
 
                           const u8 *in_data, size_t in_len,
1969
 
                           u8 *out_data, size_t out_len)
1970
 
{
1971
 
        int res;
1972
 
 
1973
 
        /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
1974
 
        res = BIO_write(conn->ssl_in, in_data, in_len);
1975
 
        if (res < 0) {
1976
 
                tls_show_errors(MSG_INFO, __func__,
1977
 
                                "Decryption failed - BIO_write");
1978
 
                return res;
1979
 
        }
1980
 
        if (BIO_reset(conn->ssl_out) < 0) {
1981
 
                tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
1982
 
                return res;
1983
 
        }
1984
 
 
1985
 
        /* Read decrypted data for further processing */
1986
 
        res = SSL_read(conn->ssl, out_data, out_len);
1987
 
        if (res < 0) {
1988
 
                tls_show_errors(MSG_INFO, __func__,
1989
 
                                "Decryption failed - SSL_read");
1990
 
                return res;
1991
 
        }
1992
 
 
1993
 
        return res;
1994
 
}
1995
 
 
1996
 
 
1997
 
int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
1998
 
{
1999
 
        return conn ? conn->ssl->hit : 0;
2000
 
}
2001
 
 
2002
 
 
2003
 
#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC)
2004
 
/* Pre-shared secred requires a patch to openssl, so this function is
2005
 
 * commented out unless explicitly needed for EAP-FAST in order to be able to
2006
 
 * build this file with unmodified openssl. */
2007
 
 
2008
 
static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
2009
 
                           STACK_OF(SSL_CIPHER) *peer_ciphers,
2010
 
                           SSL_CIPHER **cipher, void *arg)
2011
 
{
2012
 
        struct tls_connection *conn = arg;
2013
 
 
2014
 
        if (conn == NULL || conn->pre_shared_secret == 0)
2015
 
                return 0;
2016
 
 
2017
 
        os_memcpy(secret, conn->pre_shared_secret,
2018
 
                  conn->pre_shared_secret_len);
2019
 
        *secret_len = conn->pre_shared_secret_len;
2020
 
 
2021
 
        return 1;
2022
 
}
2023
 
 
2024
 
 
2025
 
int tls_connection_set_master_key(void *ssl_ctx, struct tls_connection *conn,
2026
 
                                  const u8 *key, size_t key_len)
2027
 
{
2028
 
        if (conn == NULL || key_len > SSL_MAX_MASTER_KEY_LENGTH)
2029
 
                return -1;
2030
 
 
2031
 
        os_free(conn->pre_shared_secret);
2032
 
        conn->pre_shared_secret = NULL;
2033
 
        conn->pre_shared_secret_len = 0;
2034
 
 
2035
 
        if (key) {
2036
 
                conn->pre_shared_secret = os_malloc(key_len);
2037
 
                if (conn->pre_shared_secret) {
2038
 
                        os_memcpy(conn->pre_shared_secret, key, key_len);
2039
 
                        conn->pre_shared_secret_len = key_len;
2040
 
                }
2041
 
                if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
2042
 
                                              conn) != 1)
2043
 
                        return -1;
2044
 
        } else {
2045
 
                if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
2046
 
                        return -1;
2047
 
        }
2048
 
 
2049
 
        return 0;
2050
 
}
2051
 
#endif /* EAP_FAST || EAP_FAST_DYNAMIC */
2052
 
 
2053
 
 
2054
 
int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
2055
 
                                   u8 *ciphers)
2056
 
{
2057
 
        char buf[100], *pos, *end;
2058
 
        u8 *c;
2059
 
        int ret;
2060
 
 
2061
 
        if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
2062
 
                return -1;
2063
 
 
2064
 
        buf[0] = '\0';
2065
 
        pos = buf;
2066
 
        end = pos + sizeof(buf);
2067
 
 
2068
 
        c = ciphers;
2069
 
        while (*c != TLS_CIPHER_NONE) {
2070
 
                const char *suite;
2071
 
 
2072
 
                switch (*c) {
2073
 
                case TLS_CIPHER_RC4_SHA:
2074
 
                        suite = "RC4-SHA";
2075
 
                        break;
2076
 
                case TLS_CIPHER_AES128_SHA:
2077
 
                        suite = "AES128-SHA";
2078
 
                        break;
2079
 
                case TLS_CIPHER_RSA_DHE_AES128_SHA:
2080
 
                        suite = "DHE-RSA-AES128-SHA";
2081
 
                        break;
2082
 
                case TLS_CIPHER_ANON_DH_AES128_SHA:
2083
 
                        suite = "ADH-AES128-SHA";
2084
 
                        break;
2085
 
                default:
2086
 
                        wpa_printf(MSG_DEBUG, "TLS: Unsupported "
2087
 
                                   "cipher selection: %d", *c);
2088
 
                        return -1;
2089
 
                }
2090
 
                ret = os_snprintf(pos, end - pos, ":%s", suite);
2091
 
                if (ret < 0 || ret >= end - pos)
2092
 
                        break;
2093
 
                pos += ret;
2094
 
 
2095
 
                c++;
2096
 
        }
2097
 
 
2098
 
        wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
2099
 
 
2100
 
        if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
2101
 
                tls_show_errors(MSG_INFO, __func__,
2102
 
                                "Cipher suite configuration failed");
2103
 
                return -1;
2104
 
        }
2105
 
 
2106
 
        return 0;
2107
 
}
2108
 
 
2109
 
 
2110
 
int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
2111
 
                   char *buf, size_t buflen)
2112
 
{
2113
 
        const char *name;
2114
 
        if (conn == NULL || conn->ssl == NULL)
2115
 
                return -1;
2116
 
 
2117
 
        name = SSL_get_cipher(conn->ssl);
2118
 
        if (name == NULL)
2119
 
                return -1;
2120
 
 
2121
 
        os_strlcpy(buf, name, buflen);
2122
 
        return 0;
2123
 
}
2124
 
 
2125
 
 
2126
 
int tls_connection_enable_workaround(void *ssl_ctx,
2127
 
                                     struct tls_connection *conn)
2128
 
{
2129
 
        SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
2130
 
 
2131
 
        return 0;
2132
 
}
2133
 
 
2134
 
 
2135
 
#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC)
2136
 
/* ClientHello TLS extensions require a patch to openssl, so this function is
2137
 
 * commented out unless explicitly needed for EAP-FAST in order to be able to
2138
 
 * build this file with unmodified openssl. */
2139
 
int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
2140
 
                                    int ext_type, const u8 *data,
2141
 
                                    size_t data_len)
2142
 
{
2143
 
        if (conn == NULL || conn->ssl == NULL)
2144
 
                return -1;
2145
 
 
2146
 
        if (SSL_set_hello_extension(conn->ssl, ext_type, (void *) data,
2147
 
                                    data_len) != 1)
2148
 
                return -1;
2149
 
 
2150
 
        return 0;
2151
 
}
2152
 
#endif /* EAP_FAST || EAP_FAST_DYNAMIC */
2153
 
 
2154
 
 
2155
 
int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
2156
 
{
2157
 
        if (conn == NULL)
2158
 
                return -1;
2159
 
        return conn->failed;
2160
 
}
2161
 
 
2162
 
 
2163
 
int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
2164
 
{
2165
 
        if (conn == NULL)
2166
 
                return -1;
2167
 
        return conn->read_alerts;
2168
 
}
2169
 
 
2170
 
 
2171
 
int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
2172
 
{
2173
 
        if (conn == NULL)
2174
 
                return -1;
2175
 
        return conn->write_alerts;
2176
 
}
2177
 
 
2178
 
 
2179
 
int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
2180
 
                              const struct tls_connection_params *params)
2181
 
{
2182
 
        int ret;
2183
 
        unsigned long err;
2184
 
 
2185
 
        if (conn == NULL)
2186
 
                return -1;
2187
 
 
2188
 
        while ((err = ERR_get_error())) {
2189
 
                wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
2190
 
                           __func__, ERR_error_string(err, NULL));
2191
 
        }
2192
 
 
2193
 
        if (tls_connection_set_subject_match(conn,
2194
 
                                             params->subject_match,
2195
 
                                             params->altsubject_match))
2196
 
                return -1;
2197
 
        if (tls_connection_ca_cert(tls_ctx, conn, params->ca_cert,
2198
 
                                   params->ca_cert_blob,
2199
 
                                   params->ca_cert_blob_len,
2200
 
                                   params->ca_path))
2201
 
                return -1;
2202
 
        if (tls_connection_client_cert(conn, params->client_cert,
2203
 
                                       params->client_cert_blob,
2204
 
                                       params->client_cert_blob_len))
2205
 
                return -1;
2206
 
 
2207
 
        if (params->engine) {
2208
 
                wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine");
2209
 
                ret = tls_engine_init(conn, params->engine_id, params->pin,
2210
 
                                      params->key_id);
2211
 
                if (ret)
2212
 
                        return ret;
2213
 
                if (tls_connection_engine_private_key(conn))
2214
 
                        return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
2215
 
        } else if (tls_connection_private_key(tls_ctx, conn,
2216
 
                                              params->private_key,
2217
 
                                              params->private_key_passwd,
2218
 
                                              params->private_key_blob,
2219
 
                                              params->private_key_blob_len)) {
2220
 
                wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
2221
 
                           params->private_key);
2222
 
                return -1;
2223
 
        }
2224
 
 
2225
 
        if (tls_connection_dh(conn, params->dh_file)) {
2226
 
                wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
2227
 
                           params->dh_file);
2228
 
                return -1;
2229
 
        }
2230
 
 
2231
 
        tls_get_errors(tls_ctx);
2232
 
 
2233
 
        return 0;
2234
 
}
2235
 
 
2236
 
 
2237
 
int tls_global_set_params(void *tls_ctx,
2238
 
                          const struct tls_connection_params *params)
2239
 
{
2240
 
        SSL_CTX *ssl_ctx = tls_ctx;
2241
 
        unsigned long err;
2242
 
 
2243
 
        while ((err = ERR_get_error())) {
2244
 
                wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
2245
 
                           __func__, ERR_error_string(err, NULL));
2246
 
        }
2247
 
 
2248
 
        if (tls_global_ca_cert(ssl_ctx, params->ca_cert))
2249
 
                return -1;
2250
 
 
2251
 
        if (tls_global_client_cert(ssl_ctx, params->client_cert))
2252
 
                return -1;
2253
 
 
2254
 
        if (tls_global_private_key(ssl_ctx, params->private_key,
2255
 
                                   params->private_key_passwd))
2256
 
                return -1;
2257
 
 
2258
 
        return 0;
2259
 
}
2260
 
 
2261
 
 
2262
 
int tls_connection_get_keyblock_size(void *tls_ctx,
2263
 
                                     struct tls_connection *conn)
2264
 
{
2265
 
        const EVP_CIPHER *c;
2266
 
        const EVP_MD *h;
2267
 
 
2268
 
        if (conn == NULL || conn->ssl == NULL ||
2269
 
            conn->ssl->enc_read_ctx == NULL ||
2270
 
            conn->ssl->enc_read_ctx->cipher == NULL ||
2271
 
            conn->ssl->read_hash == NULL)
2272
 
                return -1;
2273
 
 
2274
 
        c = conn->ssl->enc_read_ctx->cipher;
2275
 
        h = conn->ssl->read_hash;
2276
 
 
2277
 
        return 2 * (EVP_CIPHER_key_length(c) +
2278
 
                    EVP_MD_size(h) +
2279
 
                    EVP_CIPHER_iv_length(c));
2280
 
}
2281
 
 
2282
 
 
2283
 
unsigned int tls_capabilities(void *tls_ctx)
2284
 
{
2285
 
        return 0;
2286
 
}
2287
 
 
2288
 
 
2289
 
int tls_connection_set_ia(void *tls_ctx, struct tls_connection *conn,
2290
 
                          int tls_ia)
2291
 
{
2292
 
        return -1;
2293
 
}
2294
 
 
2295
 
 
2296
 
int tls_connection_ia_send_phase_finished(void *tls_ctx,
2297
 
                                          struct tls_connection *conn,
2298
 
                                          int final,
2299
 
                                          u8 *out_data, size_t out_len)
2300
 
{
2301
 
        return -1;
2302
 
}
2303
 
 
2304
 
 
2305
 
int tls_connection_ia_final_phase_finished(void *tls_ctx,
2306
 
                                           struct tls_connection *conn)
2307
 
{
2308
 
        return -1;
2309
 
}
2310
 
 
2311
 
 
2312
 
int tls_connection_ia_permute_inner_secret(void *tls_ctx,
2313
 
                                           struct tls_connection *conn,
2314
 
                                           const u8 *key, size_t key_len)
2315
 
{
2316
 
        return -1;
2317
 
}