]>
Commit | Line | Data |
---|---|---|
6fc6879b | 1 | /* |
81c85c06 | 2 | * SSL/TLS interface functions for OpenSSL |
16bc3b89 | 3 | * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi> |
6fc6879b | 4 | * |
0f3d578e JM |
5 | * This software may be distributed under the terms of the BSD license. |
6 | * See README for more details. | |
6fc6879b JM |
7 | */ |
8 | ||
9 | #include "includes.h" | |
10 | ||
11 | #ifndef CONFIG_SMARTCARD | |
12 | #ifndef OPENSSL_NO_ENGINE | |
1176ab6d | 13 | #ifndef ANDROID |
6fc6879b JM |
14 | #define OPENSSL_NO_ENGINE |
15 | #endif | |
16 | #endif | |
1176ab6d | 17 | #endif |
6fc6879b JM |
18 | |
19 | #include <openssl/ssl.h> | |
20 | #include <openssl/err.h> | |
e4471338 | 21 | #include <openssl/opensslv.h> |
6fc6879b JM |
22 | #include <openssl/pkcs12.h> |
23 | #include <openssl/x509v3.h> | |
24 | #ifndef OPENSSL_NO_ENGINE | |
25 | #include <openssl/engine.h> | |
26 | #endif /* OPENSSL_NO_ENGINE */ | |
690e543e JM |
27 | #ifndef OPENSSL_NO_DSA |
28 | #include <openssl/dsa.h> | |
29 | #endif | |
30 | #ifndef OPENSSL_NO_DH | |
31 | #include <openssl/dh.h> | |
32 | #endif | |
6fc6879b JM |
33 | |
34 | #include "common.h" | |
00468b46 | 35 | #include "crypto.h" |
fa0e7151 | 36 | #include "sha1.h" |
16bc3b89 | 37 | #include "sha256.h" |
6fc6879b | 38 | #include "tls.h" |
213e158c | 39 | #include "tls_openssl.h" |
6fc6879b | 40 | |
1cece2fa DB |
41 | #if !defined(CONFIG_FIPS) && \ |
42 | (defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || \ | |
43 | defined(EAP_SERVER_FAST)) | |
44 | #define OPENSSL_NEED_EAP_FAST_PRF | |
45 | #endif | |
46 | ||
a8572960 AL |
47 | #if defined(OPENSSL_IS_BORINGSSL) |
48 | /* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */ | |
49 | typedef size_t stack_index_t; | |
50 | #else | |
51 | typedef int stack_index_t; | |
0cf03892 JM |
52 | #endif |
53 | ||
080585c0 JM |
54 | #ifdef SSL_set_tlsext_status_type |
55 | #ifndef OPENSSL_NO_TLSEXT | |
56 | #define HAVE_OCSP | |
57 | #include <openssl/ocsp.h> | |
58 | #endif /* OPENSSL_NO_TLSEXT */ | |
59 | #endif /* SSL_set_tlsext_status_type */ | |
60 | ||
e4471338 DB |
61 | #if (OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
62 | defined(LIBRESSL_VERSION_NUMBER)) && \ | |
63 | !defined(BORINGSSL_API_VERSION) | |
dea20519 JM |
64 | /* |
65 | * SSL_get_client_random() and SSL_get_server_random() were added in OpenSSL | |
e4471338 DB |
66 | * 1.1.0 and newer BoringSSL revisions. Provide compatibility wrappers for |
67 | * older versions. | |
dea20519 JM |
68 | */ |
69 | ||
70 | static size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, | |
71 | size_t outlen) | |
72 | { | |
73 | if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE) | |
74 | return 0; | |
75 | os_memcpy(out, ssl->s3->client_random, SSL3_RANDOM_SIZE); | |
76 | return SSL3_RANDOM_SIZE; | |
77 | } | |
78 | ||
79 | ||
80 | static size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, | |
81 | size_t outlen) | |
82 | { | |
83 | if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE) | |
84 | return 0; | |
85 | os_memcpy(out, ssl->s3->server_random, SSL3_RANDOM_SIZE); | |
86 | return SSL3_RANDOM_SIZE; | |
87 | } | |
88 | ||
1ebb24bb | 89 | |
1cece2fa | 90 | #ifdef OPENSSL_NEED_EAP_FAST_PRF |
1ebb24bb JM |
91 | static size_t SSL_SESSION_get_master_key(const SSL_SESSION *session, |
92 | unsigned char *out, size_t outlen) | |
93 | { | |
94 | if (!session || session->master_key_length < 0 || | |
95 | (size_t) session->master_key_length > outlen) | |
96 | return 0; | |
97 | if ((size_t) session->master_key_length < outlen) | |
98 | outlen = session->master_key_length; | |
99 | os_memcpy(out, session->master_key, outlen); | |
100 | return outlen; | |
101 | } | |
1cece2fa | 102 | #endif /* OPENSSL_NEED_EAP_FAST_PRF */ |
1ebb24bb | 103 | |
dea20519 JM |
104 | #endif |
105 | ||
3cfbd3b0 | 106 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(OPENSSL_IS_BORINGSSL) |
4eb8cfe0 JM |
107 | #ifdef CONFIG_SUITEB |
108 | static int RSA_bits(const RSA *r) | |
109 | { | |
110 | return BN_num_bits(r->n); | |
111 | } | |
112 | #endif /* CONFIG_SUITEB */ | |
113 | #endif | |
114 | ||
1d415f1f KR |
115 | #ifdef ANDROID |
116 | #include <openssl/pem.h> | |
117 | #include <keystore/keystore_get.h> | |
118 | ||
119 | static BIO * BIO_from_keystore(const char *key) | |
120 | { | |
121 | BIO *bio = NULL; | |
122 | uint8_t *value = NULL; | |
123 | int length = keystore_get(key, strlen(key), &value); | |
124 | if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL) | |
125 | BIO_write(bio, value, length); | |
126 | free(value); | |
127 | return bio; | |
128 | } | |
a8ef133f RX |
129 | |
130 | ||
131 | static int tls_add_ca_from_keystore(X509_STORE *ctx, const char *key_alias) | |
132 | { | |
133 | BIO *bio = BIO_from_keystore(key_alias); | |
134 | STACK_OF(X509_INFO) *stack = NULL; | |
135 | stack_index_t i; | |
136 | ||
137 | if (bio) { | |
138 | stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL); | |
139 | BIO_free(bio); | |
140 | } | |
141 | ||
142 | if (!stack) { | |
143 | wpa_printf(MSG_WARNING, "TLS: Failed to parse certificate: %s", | |
144 | key_alias); | |
145 | return -1; | |
146 | } | |
147 | ||
148 | for (i = 0; i < sk_X509_INFO_num(stack); ++i) { | |
149 | X509_INFO *info = sk_X509_INFO_value(stack, i); | |
150 | ||
151 | if (info->x509) | |
152 | X509_STORE_add_cert(ctx, info->x509); | |
153 | if (info->crl) | |
154 | X509_STORE_add_crl(ctx, info->crl); | |
155 | } | |
156 | ||
157 | sk_X509_INFO_pop_free(stack, X509_INFO_free); | |
158 | ||
159 | return 0; | |
160 | } | |
161 | ||
162 | ||
163 | static int tls_add_ca_from_keystore_encoded(X509_STORE *ctx, | |
164 | const char *encoded_key_alias) | |
165 | { | |
166 | int rc = -1; | |
167 | int len = os_strlen(encoded_key_alias); | |
168 | unsigned char *decoded_alias; | |
169 | ||
170 | if (len & 1) { | |
171 | wpa_printf(MSG_WARNING, "Invalid hex-encoded alias: %s", | |
172 | encoded_key_alias); | |
173 | return rc; | |
174 | } | |
175 | ||
176 | decoded_alias = os_malloc(len / 2 + 1); | |
177 | if (decoded_alias) { | |
178 | if (!hexstr2bin(encoded_key_alias, decoded_alias, len / 2)) { | |
179 | decoded_alias[len / 2] = '\0'; | |
180 | rc = tls_add_ca_from_keystore( | |
181 | ctx, (const char *) decoded_alias); | |
182 | } | |
183 | os_free(decoded_alias); | |
184 | } | |
185 | ||
186 | return rc; | |
187 | } | |
188 | ||
1d415f1f KR |
189 | #endif /* ANDROID */ |
190 | ||
6fc6879b | 191 | static int tls_openssl_ref_count = 0; |
acf36f31 | 192 | static int tls_ex_idx_session = -1; |
6fc6879b | 193 | |
7c0e1e27 | 194 | struct tls_context { |
00468b46 JM |
195 | void (*event_cb)(void *ctx, enum tls_event ev, |
196 | union tls_event_data *data); | |
197 | void *cb_ctx; | |
1b414f59 | 198 | int cert_in_cb; |
080585c0 | 199 | char *ocsp_stapling_response; |
00468b46 JM |
200 | }; |
201 | ||
7c0e1e27 | 202 | static struct tls_context *tls_global = NULL; |
00468b46 JM |
203 | |
204 | ||
bd9b8b2b JM |
205 | struct tls_data { |
206 | SSL_CTX *ssl; | |
acf36f31 | 207 | unsigned int tls_session_lifetime; |
bd9b8b2b JM |
208 | }; |
209 | ||
6fc6879b | 210 | struct tls_connection { |
7c0e1e27 | 211 | struct tls_context *context; |
68ae4773 | 212 | SSL_CTX *ssl_ctx; |
6fc6879b JM |
213 | SSL *ssl; |
214 | BIO *ssl_in, *ssl_out; | |
aeeb0bca | 215 | #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE) |
6fc6879b JM |
216 | ENGINE *engine; /* functional reference to the engine */ |
217 | EVP_PKEY *private_key; /* the private key if using engine */ | |
218 | #endif /* OPENSSL_NO_ENGINE */ | |
cebee30f | 219 | char *subject_match, *altsubject_match, *suffix_match, *domain_match; |
6fc6879b JM |
220 | int read_alerts, write_alerts, failed; |
221 | ||
222 | tls_session_ticket_cb session_ticket_cb; | |
223 | void *session_ticket_cb_ctx; | |
224 | ||
225 | /* SessionTicket received from OpenSSL hello_extension_cb (server) */ | |
226 | u8 *session_ticket; | |
227 | size_t session_ticket_len; | |
00468b46 | 228 | |
8d6399e4 JM |
229 | unsigned int ca_cert_verify:1; |
230 | unsigned int cert_probe:1; | |
231 | unsigned int server_cert_only:1; | |
bb52293e | 232 | unsigned int invalid_hb_used:1; |
acf36f31 | 233 | unsigned int success_data:1; |
00468b46 JM |
234 | |
235 | u8 srv_cert_hash[32]; | |
235279e7 JM |
236 | |
237 | unsigned int flags; | |
080585c0 JM |
238 | |
239 | X509 *peer_cert; | |
240 | X509 *peer_issuer; | |
6bf61fb2 | 241 | X509 *peer_issuer_issuer; |
005c5dcf | 242 | |
005c5dcf JM |
243 | unsigned char client_random[SSL3_RANDOM_SIZE]; |
244 | unsigned char server_random[SSL3_RANDOM_SIZE]; | |
1c9663cf JM |
245 | |
246 | u16 cipher_suite; | |
247 | int server_dh_prime_len; | |
6fc6879b JM |
248 | }; |
249 | ||
250 | ||
7c0e1e27 PS |
251 | static struct tls_context * tls_context_new(const struct tls_config *conf) |
252 | { | |
253 | struct tls_context *context = os_zalloc(sizeof(*context)); | |
254 | if (context == NULL) | |
255 | return NULL; | |
256 | if (conf) { | |
257 | context->event_cb = conf->event_cb; | |
258 | context->cb_ctx = conf->cb_ctx; | |
259 | context->cert_in_cb = conf->cert_in_cb; | |
260 | } | |
261 | return context; | |
262 | } | |
263 | ||
264 | ||
6fc6879b JM |
265 | #ifdef CONFIG_NO_STDOUT_DEBUG |
266 | ||
267 | static void _tls_show_errors(void) | |
268 | { | |
269 | unsigned long err; | |
270 | ||
271 | while ((err = ERR_get_error())) { | |
272 | /* Just ignore the errors, since stdout is disabled */ | |
273 | } | |
274 | } | |
275 | #define tls_show_errors(l, f, t) _tls_show_errors() | |
276 | ||
277 | #else /* CONFIG_NO_STDOUT_DEBUG */ | |
278 | ||
279 | static void tls_show_errors(int level, const char *func, const char *txt) | |
280 | { | |
281 | unsigned long err; | |
282 | ||
283 | wpa_printf(level, "OpenSSL: %s - %s %s", | |
284 | func, txt, ERR_error_string(ERR_get_error(), NULL)); | |
285 | ||
286 | while ((err = ERR_get_error())) { | |
287 | wpa_printf(MSG_INFO, "OpenSSL: pending error: %s", | |
288 | ERR_error_string(err, NULL)); | |
289 | } | |
290 | } | |
291 | ||
292 | #endif /* CONFIG_NO_STDOUT_DEBUG */ | |
293 | ||
294 | ||
295 | #ifdef CONFIG_NATIVE_WINDOWS | |
296 | ||
297 | /* Windows CryptoAPI and access to certificate stores */ | |
298 | #include <wincrypt.h> | |
299 | ||
300 | #ifdef __MINGW32_VERSION | |
301 | /* | |
302 | * MinGW does not yet include all the needed definitions for CryptoAPI, so | |
303 | * define here whatever extra is needed. | |
304 | */ | |
6fc6879b JM |
305 | #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16) |
306 | #define CERT_STORE_READONLY_FLAG 0x00008000 | |
307 | #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000 | |
6fc6879b JM |
308 | |
309 | #endif /* __MINGW32_VERSION */ | |
310 | ||
311 | ||
312 | struct cryptoapi_rsa_data { | |
313 | const CERT_CONTEXT *cert; | |
314 | HCRYPTPROV crypt_prov; | |
315 | DWORD key_spec; | |
316 | BOOL free_crypt_prov; | |
317 | }; | |
318 | ||
319 | ||
320 | static void cryptoapi_error(const char *msg) | |
321 | { | |
322 | wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u", | |
323 | msg, (unsigned int) GetLastError()); | |
324 | } | |
325 | ||
326 | ||
327 | static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from, | |
328 | unsigned char *to, RSA *rsa, int padding) | |
329 | { | |
330 | wpa_printf(MSG_DEBUG, "%s - not implemented", __func__); | |
331 | return 0; | |
332 | } | |
333 | ||
334 | ||
335 | static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from, | |
336 | unsigned char *to, RSA *rsa, int padding) | |
337 | { | |
338 | wpa_printf(MSG_DEBUG, "%s - not implemented", __func__); | |
339 | return 0; | |
340 | } | |
341 | ||
342 | ||
343 | static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from, | |
344 | unsigned char *to, RSA *rsa, int padding) | |
345 | { | |
346 | struct cryptoapi_rsa_data *priv = | |
347 | (struct cryptoapi_rsa_data *) rsa->meth->app_data; | |
348 | HCRYPTHASH hash; | |
349 | DWORD hash_size, len, i; | |
350 | unsigned char *buf = NULL; | |
351 | int ret = 0; | |
352 | ||
353 | if (priv == NULL) { | |
354 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, | |
355 | ERR_R_PASSED_NULL_PARAMETER); | |
356 | return 0; | |
357 | } | |
358 | ||
359 | if (padding != RSA_PKCS1_PADDING) { | |
360 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, | |
361 | RSA_R_UNKNOWN_PADDING_TYPE); | |
362 | return 0; | |
363 | } | |
364 | ||
365 | if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) { | |
366 | wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported", | |
367 | __func__); | |
368 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, | |
369 | RSA_R_INVALID_MESSAGE_LENGTH); | |
370 | return 0; | |
371 | } | |
372 | ||
373 | if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash)) | |
374 | { | |
375 | cryptoapi_error("CryptCreateHash failed"); | |
376 | return 0; | |
377 | } | |
378 | ||
379 | len = sizeof(hash_size); | |
380 | if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len, | |
381 | 0)) { | |
382 | cryptoapi_error("CryptGetHashParam failed"); | |
383 | goto err; | |
384 | } | |
385 | ||
386 | if ((int) hash_size != flen) { | |
387 | wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)", | |
388 | (unsigned) hash_size, flen); | |
389 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, | |
390 | RSA_R_INVALID_MESSAGE_LENGTH); | |
391 | goto err; | |
392 | } | |
393 | if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) { | |
394 | cryptoapi_error("CryptSetHashParam failed"); | |
395 | goto err; | |
396 | } | |
397 | ||
398 | len = RSA_size(rsa); | |
399 | buf = os_malloc(len); | |
400 | if (buf == NULL) { | |
401 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE); | |
402 | goto err; | |
403 | } | |
404 | ||
405 | if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) { | |
406 | cryptoapi_error("CryptSignHash failed"); | |
407 | goto err; | |
408 | } | |
409 | ||
410 | for (i = 0; i < len; i++) | |
411 | to[i] = buf[len - i - 1]; | |
412 | ret = len; | |
413 | ||
414 | err: | |
415 | os_free(buf); | |
416 | CryptDestroyHash(hash); | |
417 | ||
418 | return ret; | |
419 | } | |
420 | ||
421 | ||
422 | static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from, | |
423 | unsigned char *to, RSA *rsa, int padding) | |
424 | { | |
425 | wpa_printf(MSG_DEBUG, "%s - not implemented", __func__); | |
426 | return 0; | |
427 | } | |
428 | ||
429 | ||
430 | static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv) | |
431 | { | |
432 | if (priv == NULL) | |
433 | return; | |
434 | if (priv->crypt_prov && priv->free_crypt_prov) | |
435 | CryptReleaseContext(priv->crypt_prov, 0); | |
436 | if (priv->cert) | |
437 | CertFreeCertificateContext(priv->cert); | |
438 | os_free(priv); | |
439 | } | |
440 | ||
441 | ||
442 | static int cryptoapi_finish(RSA *rsa) | |
443 | { | |
444 | cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data); | |
445 | os_free((void *) rsa->meth); | |
446 | rsa->meth = NULL; | |
447 | return 1; | |
448 | } | |
449 | ||
450 | ||
451 | static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store) | |
452 | { | |
453 | HCERTSTORE cs; | |
454 | const CERT_CONTEXT *ret = NULL; | |
455 | ||
456 | cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0, | |
457 | store | CERT_STORE_OPEN_EXISTING_FLAG | | |
458 | CERT_STORE_READONLY_FLAG, L"MY"); | |
459 | if (cs == NULL) { | |
460 | cryptoapi_error("Failed to open 'My system store'"); | |
461 | return NULL; | |
462 | } | |
463 | ||
464 | if (strncmp(name, "cert://", 7) == 0) { | |
465 | unsigned short wbuf[255]; | |
466 | MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255); | |
467 | ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING | | |
468 | PKCS_7_ASN_ENCODING, | |
469 | 0, CERT_FIND_SUBJECT_STR, | |
470 | wbuf, NULL); | |
471 | } else if (strncmp(name, "hash://", 7) == 0) { | |
472 | CRYPT_HASH_BLOB blob; | |
473 | int len; | |
474 | const char *hash = name + 7; | |
475 | unsigned char *buf; | |
476 | ||
477 | len = os_strlen(hash) / 2; | |
478 | buf = os_malloc(len); | |
479 | if (buf && hexstr2bin(hash, buf, len) == 0) { | |
480 | blob.cbData = len; | |
481 | blob.pbData = buf; | |
482 | ret = CertFindCertificateInStore(cs, | |
483 | X509_ASN_ENCODING | | |
484 | PKCS_7_ASN_ENCODING, | |
485 | 0, CERT_FIND_HASH, | |
486 | &blob, NULL); | |
487 | } | |
488 | os_free(buf); | |
489 | } | |
490 | ||
491 | CertCloseStore(cs, 0); | |
492 | ||
493 | return ret; | |
494 | } | |
495 | ||
496 | ||
497 | static int tls_cryptoapi_cert(SSL *ssl, const char *name) | |
498 | { | |
499 | X509 *cert = NULL; | |
500 | RSA *rsa = NULL, *pub_rsa; | |
501 | struct cryptoapi_rsa_data *priv; | |
502 | RSA_METHOD *rsa_meth; | |
503 | ||
504 | if (name == NULL || | |
505 | (strncmp(name, "cert://", 7) != 0 && | |
506 | strncmp(name, "hash://", 7) != 0)) | |
507 | return -1; | |
508 | ||
509 | priv = os_zalloc(sizeof(*priv)); | |
510 | rsa_meth = os_zalloc(sizeof(*rsa_meth)); | |
511 | if (priv == NULL || rsa_meth == NULL) { | |
512 | wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory " | |
513 | "for CryptoAPI RSA method"); | |
514 | os_free(priv); | |
515 | os_free(rsa_meth); | |
516 | return -1; | |
517 | } | |
518 | ||
519 | priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER); | |
520 | if (priv->cert == NULL) { | |
521 | priv->cert = cryptoapi_find_cert( | |
522 | name, CERT_SYSTEM_STORE_LOCAL_MACHINE); | |
523 | } | |
524 | if (priv->cert == NULL) { | |
525 | wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate " | |
526 | "'%s'", name); | |
527 | goto err; | |
528 | } | |
529 | ||
fee31f76 JM |
530 | cert = d2i_X509(NULL, |
531 | (const unsigned char **) &priv->cert->pbCertEncoded, | |
6fc6879b JM |
532 | priv->cert->cbCertEncoded); |
533 | if (cert == NULL) { | |
534 | wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER " | |
535 | "encoding"); | |
536 | goto err; | |
537 | } | |
538 | ||
6fc6879b JM |
539 | if (!CryptAcquireCertificatePrivateKey(priv->cert, |
540 | CRYPT_ACQUIRE_COMPARE_KEY_FLAG, | |
541 | NULL, &priv->crypt_prov, | |
542 | &priv->key_spec, | |
543 | &priv->free_crypt_prov)) { | |
544 | cryptoapi_error("Failed to acquire a private key for the " | |
545 | "certificate"); | |
546 | goto err; | |
547 | } | |
548 | ||
549 | rsa_meth->name = "Microsoft CryptoAPI RSA Method"; | |
550 | rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc; | |
551 | rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec; | |
552 | rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc; | |
553 | rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec; | |
554 | rsa_meth->finish = cryptoapi_finish; | |
555 | rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK; | |
556 | rsa_meth->app_data = (char *) priv; | |
557 | ||
558 | rsa = RSA_new(); | |
559 | if (rsa == NULL) { | |
560 | SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, | |
561 | ERR_R_MALLOC_FAILURE); | |
562 | goto err; | |
563 | } | |
564 | ||
565 | if (!SSL_use_certificate(ssl, cert)) { | |
566 | RSA_free(rsa); | |
567 | rsa = NULL; | |
568 | goto err; | |
569 | } | |
570 | pub_rsa = cert->cert_info->key->pkey->pkey.rsa; | |
571 | X509_free(cert); | |
572 | cert = NULL; | |
573 | ||
574 | rsa->n = BN_dup(pub_rsa->n); | |
575 | rsa->e = BN_dup(pub_rsa->e); | |
576 | if (!RSA_set_method(rsa, rsa_meth)) | |
577 | goto err; | |
578 | ||
579 | if (!SSL_use_RSAPrivateKey(ssl, rsa)) | |
580 | goto err; | |
581 | RSA_free(rsa); | |
582 | ||
583 | return 0; | |
584 | ||
585 | err: | |
586 | if (cert) | |
587 | X509_free(cert); | |
588 | if (rsa) | |
589 | RSA_free(rsa); | |
590 | else { | |
591 | os_free(rsa_meth); | |
592 | cryptoapi_free_data(priv); | |
593 | } | |
594 | return -1; | |
595 | } | |
596 | ||
597 | ||
598 | static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name) | |
599 | { | |
600 | HCERTSTORE cs; | |
601 | PCCERT_CONTEXT ctx = NULL; | |
602 | X509 *cert; | |
603 | char buf[128]; | |
604 | const char *store; | |
605 | #ifdef UNICODE | |
606 | WCHAR *wstore; | |
607 | #endif /* UNICODE */ | |
608 | ||
6fc6879b JM |
609 | if (name == NULL || strncmp(name, "cert_store://", 13) != 0) |
610 | return -1; | |
611 | ||
612 | store = name + 13; | |
613 | #ifdef UNICODE | |
614 | wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR)); | |
615 | if (wstore == NULL) | |
616 | return -1; | |
617 | wsprintf(wstore, L"%S", store); | |
618 | cs = CertOpenSystemStore(0, wstore); | |
619 | os_free(wstore); | |
620 | #else /* UNICODE */ | |
621 | cs = CertOpenSystemStore(0, store); | |
622 | #endif /* UNICODE */ | |
623 | if (cs == NULL) { | |
624 | wpa_printf(MSG_DEBUG, "%s: failed to open system cert store " | |
625 | "'%s': error=%d", __func__, store, | |
626 | (int) GetLastError()); | |
627 | return -1; | |
628 | } | |
629 | ||
630 | while ((ctx = CertEnumCertificatesInStore(cs, ctx))) { | |
fee31f76 JM |
631 | cert = d2i_X509(NULL, |
632 | (const unsigned char **) &ctx->pbCertEncoded, | |
6fc6879b JM |
633 | ctx->cbCertEncoded); |
634 | if (cert == NULL) { | |
635 | wpa_printf(MSG_INFO, "CryptoAPI: Could not process " | |
636 | "X509 DER encoding for CA cert"); | |
637 | continue; | |
638 | } | |
639 | ||
640 | X509_NAME_oneline(X509_get_subject_name(cert), buf, | |
641 | sizeof(buf)); | |
642 | wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for " | |
643 | "system certificate store: subject='%s'", buf); | |
644 | ||
f150db6c DB |
645 | if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx), |
646 | cert)) { | |
6fc6879b JM |
647 | tls_show_errors(MSG_WARNING, __func__, |
648 | "Failed to add ca_cert to OpenSSL " | |
649 | "certificate store"); | |
650 | } | |
651 | ||
652 | X509_free(cert); | |
653 | } | |
654 | ||
655 | if (!CertCloseStore(cs, 0)) { | |
656 | wpa_printf(MSG_DEBUG, "%s: failed to close system cert store " | |
657 | "'%s': error=%d", __func__, name + 13, | |
658 | (int) GetLastError()); | |
659 | } | |
660 | ||
661 | return 0; | |
662 | } | |
663 | ||
664 | ||
665 | #else /* CONFIG_NATIVE_WINDOWS */ | |
666 | ||
667 | static int tls_cryptoapi_cert(SSL *ssl, const char *name) | |
668 | { | |
669 | return -1; | |
670 | } | |
671 | ||
672 | #endif /* CONFIG_NATIVE_WINDOWS */ | |
673 | ||
674 | ||
675 | static void ssl_info_cb(const SSL *ssl, int where, int ret) | |
676 | { | |
677 | const char *str; | |
678 | int w; | |
679 | ||
680 | wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret); | |
681 | w = where & ~SSL_ST_MASK; | |
682 | if (w & SSL_ST_CONNECT) | |
683 | str = "SSL_connect"; | |
684 | else if (w & SSL_ST_ACCEPT) | |
685 | str = "SSL_accept"; | |
686 | else | |
687 | str = "undefined"; | |
688 | ||
689 | if (where & SSL_CB_LOOP) { | |
690 | wpa_printf(MSG_DEBUG, "SSL: %s:%s", | |
691 | str, SSL_state_string_long(ssl)); | |
692 | } else if (where & SSL_CB_ALERT) { | |
7c0e1e27 | 693 | struct tls_connection *conn = SSL_get_app_data((SSL *) ssl); |
6fc6879b JM |
694 | wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s", |
695 | where & SSL_CB_READ ? | |
696 | "read (remote end reported an error)" : | |
697 | "write (local SSL3 detected an error)", | |
698 | SSL_alert_type_string_long(ret), | |
699 | SSL_alert_desc_string_long(ret)); | |
700 | if ((ret >> 8) == SSL3_AL_FATAL) { | |
6fc6879b JM |
701 | if (where & SSL_CB_READ) |
702 | conn->read_alerts++; | |
703 | else | |
704 | conn->write_alerts++; | |
705 | } | |
7c0e1e27 | 706 | if (conn->context->event_cb != NULL) { |
dd7fec1f | 707 | union tls_event_data ev; |
7c0e1e27 | 708 | struct tls_context *context = conn->context; |
dd7fec1f PS |
709 | os_memset(&ev, 0, sizeof(ev)); |
710 | ev.alert.is_local = !(where & SSL_CB_READ); | |
711 | ev.alert.type = SSL_alert_type_string_long(ret); | |
712 | ev.alert.description = SSL_alert_desc_string_long(ret); | |
7c0e1e27 | 713 | context->event_cb(context->cb_ctx, TLS_ALERT, &ev); |
dd7fec1f | 714 | } |
6fc6879b JM |
715 | } else if (where & SSL_CB_EXIT && ret <= 0) { |
716 | wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s", | |
717 | str, ret == 0 ? "failed" : "error", | |
718 | SSL_state_string_long(ssl)); | |
719 | } | |
720 | } | |
721 | ||
722 | ||
723 | #ifndef OPENSSL_NO_ENGINE | |
724 | /** | |
725 | * tls_engine_load_dynamic_generic - load any openssl engine | |
726 | * @pre: an array of commands and values that load an engine initialized | |
727 | * in the engine specific function | |
728 | * @post: an array of commands and values that initialize an already loaded | |
729 | * engine (or %NULL if not required) | |
730 | * @id: the engine id of the engine to load (only required if post is not %NULL | |
731 | * | |
732 | * This function is a generic function that loads any openssl engine. | |
733 | * | |
734 | * Returns: 0 on success, -1 on failure | |
735 | */ | |
736 | static int tls_engine_load_dynamic_generic(const char *pre[], | |
737 | const char *post[], const char *id) | |
738 | { | |
739 | ENGINE *engine; | |
740 | const char *dynamic_id = "dynamic"; | |
741 | ||
742 | engine = ENGINE_by_id(id); | |
743 | if (engine) { | |
6fc6879b JM |
744 | wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already " |
745 | "available", id); | |
c3d7fb7e DW |
746 | /* |
747 | * If it was auto-loaded by ENGINE_by_id() we might still | |
748 | * need to tell it which PKCS#11 module to use in legacy | |
749 | * (non-p11-kit) environments. Do so now; even if it was | |
750 | * properly initialised before, setting it again will be | |
751 | * harmless. | |
752 | */ | |
753 | goto found; | |
6fc6879b JM |
754 | } |
755 | ERR_clear_error(); | |
756 | ||
757 | engine = ENGINE_by_id(dynamic_id); | |
758 | if (engine == NULL) { | |
759 | wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]", | |
760 | dynamic_id, | |
761 | ERR_error_string(ERR_get_error(), NULL)); | |
762 | return -1; | |
763 | } | |
764 | ||
765 | /* Perform the pre commands. This will load the engine. */ | |
766 | while (pre && pre[0]) { | |
767 | wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]); | |
768 | if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) { | |
769 | wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: " | |
770 | "%s %s [%s]", pre[0], pre[1], | |
771 | ERR_error_string(ERR_get_error(), NULL)); | |
772 | ENGINE_free(engine); | |
773 | return -1; | |
774 | } | |
775 | pre += 2; | |
776 | } | |
777 | ||
778 | /* | |
779 | * Free the reference to the "dynamic" engine. The loaded engine can | |
780 | * now be looked up using ENGINE_by_id(). | |
781 | */ | |
782 | ENGINE_free(engine); | |
783 | ||
784 | engine = ENGINE_by_id(id); | |
785 | if (engine == NULL) { | |
786 | wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]", | |
787 | id, ERR_error_string(ERR_get_error(), NULL)); | |
788 | return -1; | |
789 | } | |
c3d7fb7e | 790 | found: |
6fc6879b JM |
791 | while (post && post[0]) { |
792 | wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]); | |
793 | if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) { | |
794 | wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:" | |
795 | " %s %s [%s]", post[0], post[1], | |
796 | ERR_error_string(ERR_get_error(), NULL)); | |
797 | ENGINE_remove(engine); | |
798 | ENGINE_free(engine); | |
799 | return -1; | |
800 | } | |
801 | post += 2; | |
802 | } | |
803 | ENGINE_free(engine); | |
804 | ||
805 | return 0; | |
806 | } | |
807 | ||
808 | ||
809 | /** | |
810 | * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc | |
811 | * @pkcs11_so_path: pksc11_so_path from the configuration | |
812 | * @pcks11_module_path: pkcs11_module_path from the configuration | |
813 | */ | |
814 | static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path, | |
815 | const char *pkcs11_module_path) | |
816 | { | |
817 | char *engine_id = "pkcs11"; | |
818 | const char *pre_cmd[] = { | |
819 | "SO_PATH", NULL /* pkcs11_so_path */, | |
820 | "ID", NULL /* engine_id */, | |
821 | "LIST_ADD", "1", | |
822 | /* "NO_VCHECK", "1", */ | |
823 | "LOAD", NULL, | |
824 | NULL, NULL | |
825 | }; | |
826 | const char *post_cmd[] = { | |
827 | "MODULE_PATH", NULL /* pkcs11_module_path */, | |
828 | NULL, NULL | |
829 | }; | |
830 | ||
5c8ab0d4 | 831 | if (!pkcs11_so_path) |
6fc6879b JM |
832 | return 0; |
833 | ||
834 | pre_cmd[1] = pkcs11_so_path; | |
835 | pre_cmd[3] = engine_id; | |
5c8ab0d4 DW |
836 | if (pkcs11_module_path) |
837 | post_cmd[1] = pkcs11_module_path; | |
838 | else | |
839 | post_cmd[0] = NULL; | |
6fc6879b JM |
840 | |
841 | wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s", | |
842 | pkcs11_so_path); | |
843 | ||
844 | return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id); | |
845 | } | |
846 | ||
847 | ||
848 | /** | |
849 | * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc | |
850 | * @opensc_so_path: opensc_so_path from the configuration | |
851 | */ | |
852 | static int tls_engine_load_dynamic_opensc(const char *opensc_so_path) | |
853 | { | |
854 | char *engine_id = "opensc"; | |
855 | const char *pre_cmd[] = { | |
856 | "SO_PATH", NULL /* opensc_so_path */, | |
857 | "ID", NULL /* engine_id */, | |
858 | "LIST_ADD", "1", | |
859 | "LOAD", NULL, | |
860 | NULL, NULL | |
861 | }; | |
862 | ||
863 | if (!opensc_so_path) | |
864 | return 0; | |
865 | ||
866 | pre_cmd[1] = opensc_so_path; | |
867 | pre_cmd[3] = engine_id; | |
868 | ||
869 | wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s", | |
870 | opensc_so_path); | |
871 | ||
872 | return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id); | |
873 | } | |
874 | #endif /* OPENSSL_NO_ENGINE */ | |
875 | ||
876 | ||
acf36f31 JM |
877 | static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
878 | { | |
879 | struct wpabuf *buf; | |
880 | ||
881 | if (tls_ex_idx_session < 0) | |
882 | return; | |
883 | buf = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session); | |
884 | if (!buf) | |
885 | return; | |
886 | wpa_printf(MSG_DEBUG, | |
887 | "OpenSSL: Free application session data %p (sess %p)", | |
888 | buf, sess); | |
889 | wpabuf_free(buf); | |
890 | ||
891 | SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL); | |
892 | } | |
893 | ||
894 | ||
6fc6879b JM |
895 | void * tls_init(const struct tls_config *conf) |
896 | { | |
bd9b8b2b | 897 | struct tls_data *data; |
6fc6879b | 898 | SSL_CTX *ssl; |
7c0e1e27 | 899 | struct tls_context *context; |
b7328434 | 900 | const char *ciphers; |
6fc6879b JM |
901 | |
902 | if (tls_openssl_ref_count == 0) { | |
7c0e1e27 PS |
903 | tls_global = context = tls_context_new(conf); |
904 | if (context == NULL) | |
00468b46 | 905 | return NULL; |
76f04b38 JM |
906 | #ifdef CONFIG_FIPS |
907 | #ifdef OPENSSL_FIPS | |
cf123d7f | 908 | if (conf && conf->fips_mode) { |
4fc53159 JM |
909 | static int fips_enabled = 0; |
910 | ||
911 | if (!fips_enabled && !FIPS_mode_set(1)) { | |
76f04b38 JM |
912 | wpa_printf(MSG_ERROR, "Failed to enable FIPS " |
913 | "mode"); | |
914 | ERR_load_crypto_strings(); | |
915 | ERR_print_errors_fp(stderr); | |
b36540db JM |
916 | os_free(tls_global); |
917 | tls_global = NULL; | |
76f04b38 | 918 | return NULL; |
4fc53159 | 919 | } else { |
76f04b38 | 920 | wpa_printf(MSG_INFO, "Running in FIPS mode"); |
4fc53159 JM |
921 | fips_enabled = 1; |
922 | } | |
76f04b38 JM |
923 | } |
924 | #else /* OPENSSL_FIPS */ | |
cf123d7f | 925 | if (conf && conf->fips_mode) { |
76f04b38 JM |
926 | wpa_printf(MSG_ERROR, "FIPS mode requested, but not " |
927 | "supported"); | |
b36540db JM |
928 | os_free(tls_global); |
929 | tls_global = NULL; | |
76f04b38 JM |
930 | return NULL; |
931 | } | |
932 | #endif /* OPENSSL_FIPS */ | |
933 | #endif /* CONFIG_FIPS */ | |
df426738 | 934 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
6fc6879b JM |
935 | SSL_load_error_strings(); |
936 | SSL_library_init(); | |
fee31f76 | 937 | #ifndef OPENSSL_NO_SHA256 |
e1ffdfc1 JM |
938 | EVP_add_digest(EVP_sha256()); |
939 | #endif /* OPENSSL_NO_SHA256 */ | |
6fc6879b JM |
940 | /* TODO: if /dev/urandom is available, PRNG is seeded |
941 | * automatically. If this is not the case, random data should | |
942 | * be added here. */ | |
943 | ||
944 | #ifdef PKCS12_FUNCS | |
1056dad7 JM |
945 | #ifndef OPENSSL_NO_RC2 |
946 | /* | |
947 | * 40-bit RC2 is commonly used in PKCS#12 files, so enable it. | |
948 | * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8 | |
949 | * versions, but it looks like OpenSSL 1.0.0 does not do that | |
950 | * anymore. | |
951 | */ | |
952 | EVP_add_cipher(EVP_rc2_40_cbc()); | |
953 | #endif /* OPENSSL_NO_RC2 */ | |
6fc6879b JM |
954 | PKCS12_PBE_add(); |
955 | #endif /* PKCS12_FUNCS */ | |
29bc76e3 | 956 | #endif /* < 1.1.0 */ |
7c0e1e27 | 957 | } else { |
7c0e1e27 PS |
958 | context = tls_context_new(conf); |
959 | if (context == NULL) | |
960 | return NULL; | |
6fc6879b JM |
961 | } |
962 | tls_openssl_ref_count++; | |
963 | ||
bd9b8b2b JM |
964 | data = os_zalloc(sizeof(*data)); |
965 | if (data) | |
966 | ssl = SSL_CTX_new(SSLv23_method()); | |
967 | else | |
968 | ssl = NULL; | |
7c0e1e27 PS |
969 | if (ssl == NULL) { |
970 | tls_openssl_ref_count--; | |
a288da61 JM |
971 | if (context != tls_global) |
972 | os_free(context); | |
7c0e1e27 PS |
973 | if (tls_openssl_ref_count == 0) { |
974 | os_free(tls_global); | |
975 | tls_global = NULL; | |
7c0e1e27 | 976 | } |
1f1e599b | 977 | os_free(data); |
6fc6879b | 978 | return NULL; |
7c0e1e27 | 979 | } |
bd9b8b2b | 980 | data->ssl = ssl; |
acf36f31 JM |
981 | if (conf) |
982 | data->tls_session_lifetime = conf->tls_session_lifetime; | |
6fc6879b | 983 | |
35efa247 JM |
984 | SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv2); |
985 | SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv3); | |
986 | ||
d7f12e4e JM |
987 | #ifdef SSL_MODE_NO_AUTO_CHAIN |
988 | /* Number of deployed use cases assume the default OpenSSL behavior of | |
989 | * auto chaining the local certificate is in use. BoringSSL removed this | |
990 | * functionality by default, so we need to restore it here to avoid | |
991 | * breaking existing use cases. */ | |
992 | SSL_CTX_clear_mode(ssl, SSL_MODE_NO_AUTO_CHAIN); | |
993 | #endif /* SSL_MODE_NO_AUTO_CHAIN */ | |
994 | ||
6fc6879b | 995 | SSL_CTX_set_info_callback(ssl, ssl_info_cb); |
7c0e1e27 | 996 | SSL_CTX_set_app_data(ssl, context); |
acf36f31 JM |
997 | if (data->tls_session_lifetime > 0) { |
998 | SSL_CTX_set_quiet_shutdown(ssl, 1); | |
999 | /* | |
1000 | * Set default context here. In practice, this will be replaced | |
1001 | * by the per-EAP method context in tls_connection_set_verify(). | |
1002 | */ | |
1003 | SSL_CTX_set_session_id_context(ssl, (u8 *) "hostapd", 7); | |
1004 | SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_SERVER); | |
1005 | SSL_CTX_set_timeout(ssl, data->tls_session_lifetime); | |
1006 | SSL_CTX_sess_set_remove_cb(ssl, remove_session_cb); | |
1007 | } else { | |
1008 | SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_OFF); | |
1009 | } | |
1010 | ||
1011 | if (tls_ex_idx_session < 0) { | |
1012 | tls_ex_idx_session = SSL_SESSION_get_ex_new_index( | |
1013 | 0, NULL, NULL, NULL, NULL); | |
1014 | if (tls_ex_idx_session < 0) { | |
1015 | tls_deinit(data); | |
1016 | return NULL; | |
1017 | } | |
1018 | } | |
6fc6879b JM |
1019 | |
1020 | #ifndef OPENSSL_NO_ENGINE | |
ddda6276 DW |
1021 | wpa_printf(MSG_DEBUG, "ENGINE: Loading dynamic engine"); |
1022 | ERR_load_ENGINE_strings(); | |
1023 | ENGINE_load_dynamic(); | |
1024 | ||
6fc6879b JM |
1025 | if (conf && |
1026 | (conf->opensc_engine_path || conf->pkcs11_engine_path || | |
1027 | conf->pkcs11_module_path)) { | |
6fc6879b JM |
1028 | if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) || |
1029 | tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path, | |
1030 | conf->pkcs11_module_path)) { | |
bd9b8b2b | 1031 | tls_deinit(data); |
6fc6879b JM |
1032 | return NULL; |
1033 | } | |
1034 | } | |
1035 | #endif /* OPENSSL_NO_ENGINE */ | |
1036 | ||
b7328434 JM |
1037 | if (conf && conf->openssl_ciphers) |
1038 | ciphers = conf->openssl_ciphers; | |
1039 | else | |
2b9891bd | 1040 | ciphers = TLS_DEFAULT_CIPHERS; |
b7328434 JM |
1041 | if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) { |
1042 | wpa_printf(MSG_ERROR, | |
1043 | "OpenSSL: Failed to set cipher string '%s'", | |
1044 | ciphers); | |
bd9b8b2b | 1045 | tls_deinit(data); |
b7328434 JM |
1046 | return NULL; |
1047 | } | |
1048 | ||
bd9b8b2b | 1049 | return data; |
6fc6879b JM |
1050 | } |
1051 | ||
1052 | ||
1053 | void tls_deinit(void *ssl_ctx) | |
1054 | { | |
bd9b8b2b JM |
1055 | struct tls_data *data = ssl_ctx; |
1056 | SSL_CTX *ssl = data->ssl; | |
7c0e1e27 PS |
1057 | struct tls_context *context = SSL_CTX_get_app_data(ssl); |
1058 | if (context != tls_global) | |
1059 | os_free(context); | |
acf36f31 JM |
1060 | if (data->tls_session_lifetime > 0) |
1061 | SSL_CTX_flush_sessions(ssl, 0); | |
6fc6879b JM |
1062 | SSL_CTX_free(ssl); |
1063 | ||
1064 | tls_openssl_ref_count--; | |
1065 | if (tls_openssl_ref_count == 0) { | |
df426738 | 1066 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
6fc6879b JM |
1067 | #ifndef OPENSSL_NO_ENGINE |
1068 | ENGINE_cleanup(); | |
1069 | #endif /* OPENSSL_NO_ENGINE */ | |
1070 | CRYPTO_cleanup_all_ex_data(); | |
a8572960 | 1071 | ERR_remove_thread_state(NULL); |
6fc6879b JM |
1072 | ERR_free_strings(); |
1073 | EVP_cleanup(); | |
29bc76e3 | 1074 | #endif /* < 1.1.0 */ |
080585c0 JM |
1075 | os_free(tls_global->ocsp_stapling_response); |
1076 | tls_global->ocsp_stapling_response = NULL; | |
00468b46 JM |
1077 | os_free(tls_global); |
1078 | tls_global = NULL; | |
6fc6879b | 1079 | } |
bd9b8b2b JM |
1080 | |
1081 | os_free(data); | |
6fc6879b JM |
1082 | } |
1083 | ||
1084 | ||
fd4fb281 MG |
1085 | #ifndef OPENSSL_NO_ENGINE |
1086 | ||
1087 | /* Cryptoki return values */ | |
1088 | #define CKR_PIN_INCORRECT 0x000000a0 | |
1089 | #define CKR_PIN_INVALID 0x000000a1 | |
1090 | #define CKR_PIN_LEN_RANGE 0x000000a2 | |
1091 | ||
1092 | /* libp11 */ | |
1093 | #define ERR_LIB_PKCS11 ERR_LIB_USER | |
1094 | ||
1095 | static int tls_is_pin_error(unsigned int err) | |
1096 | { | |
1097 | return ERR_GET_LIB(err) == ERR_LIB_PKCS11 && | |
1098 | (ERR_GET_REASON(err) == CKR_PIN_INCORRECT || | |
1099 | ERR_GET_REASON(err) == CKR_PIN_INVALID || | |
1100 | ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE); | |
1101 | } | |
1102 | ||
1103 | #endif /* OPENSSL_NO_ENGINE */ | |
1104 | ||
1105 | ||
aeeb0bca AL |
1106 | #ifdef ANDROID |
1107 | /* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */ | |
1108 | EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id); | |
1109 | #endif /* ANDROID */ | |
1110 | ||
6fc6879b | 1111 | static int tls_engine_init(struct tls_connection *conn, const char *engine_id, |
e59c91af DS |
1112 | const char *pin, const char *key_id, |
1113 | const char *cert_id, const char *ca_cert_id) | |
6fc6879b | 1114 | { |
aeeb0bca AL |
1115 | #if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL) |
1116 | #if !defined(OPENSSL_NO_ENGINE) | |
1117 | #error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL." | |
1118 | #endif | |
1119 | if (!key_id) | |
1120 | return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; | |
1121 | conn->engine = NULL; | |
1122 | conn->private_key = EVP_PKEY_from_keystore(key_id); | |
1123 | if (!conn->private_key) { | |
1124 | wpa_printf(MSG_ERROR, | |
1125 | "ENGINE: cannot load private key with id '%s' [%s]", | |
1126 | key_id, | |
1127 | ERR_error_string(ERR_get_error(), NULL)); | |
1128 | return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; | |
1129 | } | |
1130 | #endif /* ANDROID && OPENSSL_IS_BORINGSSL */ | |
1131 | ||
6fc6879b JM |
1132 | #ifndef OPENSSL_NO_ENGINE |
1133 | int ret = -1; | |
1134 | if (engine_id == NULL) { | |
1135 | wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set"); | |
1136 | return -1; | |
1137 | } | |
6fc6879b JM |
1138 | |
1139 | ERR_clear_error(); | |
1176ab6d KR |
1140 | #ifdef ANDROID |
1141 | ENGINE_load_dynamic(); | |
1142 | #endif | |
6fc6879b JM |
1143 | conn->engine = ENGINE_by_id(engine_id); |
1144 | if (!conn->engine) { | |
1145 | wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]", | |
1146 | engine_id, ERR_error_string(ERR_get_error(), NULL)); | |
1147 | goto err; | |
1148 | } | |
1149 | if (ENGINE_init(conn->engine) != 1) { | |
1150 | wpa_printf(MSG_ERROR, "ENGINE: engine init failed " | |
1151 | "(engine: %s) [%s]", engine_id, | |
1152 | ERR_error_string(ERR_get_error(), NULL)); | |
1153 | goto err; | |
1154 | } | |
1155 | wpa_printf(MSG_DEBUG, "ENGINE: engine initialized"); | |
1156 | ||
1176ab6d | 1157 | #ifndef ANDROID |
a642a52b | 1158 | if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) { |
6fc6879b JM |
1159 | wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]", |
1160 | ERR_error_string(ERR_get_error(), NULL)); | |
1161 | goto err; | |
1162 | } | |
1176ab6d | 1163 | #endif |
3d268b8d | 1164 | if (key_id) { |
a642a52b DW |
1165 | /* |
1166 | * Ensure that the ENGINE does not attempt to use the OpenSSL | |
1167 | * UI system to obtain a PIN, if we didn't provide one. | |
1168 | */ | |
1169 | struct { | |
1170 | const void *password; | |
1171 | const char *prompt_info; | |
1172 | } key_cb = { "", NULL }; | |
1173 | ||
3d268b8d DW |
1174 | /* load private key first in-case PIN is required for cert */ |
1175 | conn->private_key = ENGINE_load_private_key(conn->engine, | |
a642a52b DW |
1176 | key_id, NULL, |
1177 | &key_cb); | |
3d268b8d | 1178 | if (!conn->private_key) { |
fd4fb281 MG |
1179 | unsigned long err = ERR_get_error(); |
1180 | ||
3d268b8d DW |
1181 | wpa_printf(MSG_ERROR, |
1182 | "ENGINE: cannot load private key with id '%s' [%s]", | |
1183 | key_id, | |
fd4fb281 MG |
1184 | ERR_error_string(err, NULL)); |
1185 | if (tls_is_pin_error(err)) | |
1186 | ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN; | |
1187 | else | |
1188 | ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; | |
3d268b8d DW |
1189 | goto err; |
1190 | } | |
6fc6879b | 1191 | } |
e59c91af DS |
1192 | |
1193 | /* handle a certificate and/or CA certificate */ | |
1194 | if (cert_id || ca_cert_id) { | |
1195 | const char *cmd_name = "LOAD_CERT_CTRL"; | |
1196 | ||
1197 | /* test if the engine supports a LOAD_CERT_CTRL */ | |
1198 | if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME, | |
1199 | 0, (void *)cmd_name, NULL)) { | |
1200 | wpa_printf(MSG_ERROR, "ENGINE: engine does not support" | |
1201 | " loading certificates"); | |
1202 | ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; | |
1203 | goto err; | |
1204 | } | |
1205 | } | |
1206 | ||
6fc6879b JM |
1207 | return 0; |
1208 | ||
1209 | err: | |
1210 | if (conn->engine) { | |
1211 | ENGINE_free(conn->engine); | |
1212 | conn->engine = NULL; | |
1213 | } | |
1214 | ||
1215 | if (conn->private_key) { | |
1216 | EVP_PKEY_free(conn->private_key); | |
1217 | conn->private_key = NULL; | |
1218 | } | |
1219 | ||
1220 | return ret; | |
1221 | #else /* OPENSSL_NO_ENGINE */ | |
1222 | return 0; | |
1223 | #endif /* OPENSSL_NO_ENGINE */ | |
1224 | } | |
1225 | ||
1226 | ||
1227 | static void tls_engine_deinit(struct tls_connection *conn) | |
1228 | { | |
aeeb0bca | 1229 | #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE) |
6fc6879b JM |
1230 | wpa_printf(MSG_DEBUG, "ENGINE: engine deinit"); |
1231 | if (conn->private_key) { | |
1232 | EVP_PKEY_free(conn->private_key); | |
1233 | conn->private_key = NULL; | |
1234 | } | |
1235 | if (conn->engine) { | |
aeeb0bca | 1236 | #if !defined(OPENSSL_IS_BORINGSSL) |
6fc6879b | 1237 | ENGINE_finish(conn->engine); |
aeeb0bca | 1238 | #endif /* !OPENSSL_IS_BORINGSSL */ |
6fc6879b JM |
1239 | conn->engine = NULL; |
1240 | } | |
aeeb0bca | 1241 | #endif /* ANDROID || !OPENSSL_NO_ENGINE */ |
6fc6879b JM |
1242 | } |
1243 | ||
1244 | ||
1245 | int tls_get_errors(void *ssl_ctx) | |
1246 | { | |
1247 | int count = 0; | |
1248 | unsigned long err; | |
1249 | ||
1250 | while ((err = ERR_get_error())) { | |
1251 | wpa_printf(MSG_INFO, "TLS - SSL error: %s", | |
1252 | ERR_error_string(err, NULL)); | |
1253 | count++; | |
1254 | } | |
1255 | ||
1256 | return count; | |
1257 | } | |
1258 | ||
bb52293e | 1259 | |
cbb15497 JM |
1260 | static const char * openssl_content_type(int content_type) |
1261 | { | |
1262 | switch (content_type) { | |
1263 | case 20: | |
1264 | return "change cipher spec"; | |
1265 | case 21: | |
1266 | return "alert"; | |
1267 | case 22: | |
1268 | return "handshake"; | |
1269 | case 23: | |
1270 | return "application data"; | |
1271 | case 24: | |
1272 | return "heartbeat"; | |
1273 | case 256: | |
1274 | return "TLS header info"; /* pseudo content type */ | |
1275 | default: | |
1276 | return "?"; | |
1277 | } | |
1278 | } | |
1279 | ||
1280 | ||
1281 | static const char * openssl_handshake_type(int content_type, const u8 *buf, | |
1282 | size_t len) | |
1283 | { | |
1284 | if (content_type != 22 || !buf || len == 0) | |
1285 | return ""; | |
1286 | switch (buf[0]) { | |
1287 | case 0: | |
1288 | return "hello request"; | |
1289 | case 1: | |
1290 | return "client hello"; | |
1291 | case 2: | |
1292 | return "server hello"; | |
1293 | case 4: | |
1294 | return "new session ticket"; | |
1295 | case 11: | |
1296 | return "certificate"; | |
1297 | case 12: | |
1298 | return "server key exchange"; | |
1299 | case 13: | |
1300 | return "certificate request"; | |
1301 | case 14: | |
1302 | return "server hello done"; | |
1303 | case 15: | |
1304 | return "certificate verify"; | |
1305 | case 16: | |
1306 | return "client key exchange"; | |
1307 | case 20: | |
1308 | return "finished"; | |
1309 | case 21: | |
1310 | return "certificate url"; | |
1311 | case 22: | |
1312 | return "certificate status"; | |
1313 | default: | |
1314 | return "?"; | |
1315 | } | |
1316 | } | |
1317 | ||
1318 | ||
1c9663cf JM |
1319 | #ifdef CONFIG_SUITEB |
1320 | ||
1321 | static void check_server_hello(struct tls_connection *conn, | |
1322 | const u8 *pos, const u8 *end) | |
1323 | { | |
1324 | size_t payload_len, id_len; | |
1325 | ||
1326 | /* | |
1327 | * Parse ServerHello to get the selected cipher suite since OpenSSL does | |
1328 | * not make it cleanly available during handshake and we need to know | |
1329 | * whether DHE was selected. | |
1330 | */ | |
1331 | ||
1332 | if (end - pos < 3) | |
1333 | return; | |
1334 | payload_len = WPA_GET_BE24(pos); | |
1335 | pos += 3; | |
1336 | ||
1337 | if ((size_t) (end - pos) < payload_len) | |
1338 | return; | |
1339 | end = pos + payload_len; | |
1340 | ||
1341 | /* Skip Version and Random */ | |
1342 | if (end - pos < 2 + SSL3_RANDOM_SIZE) | |
1343 | return; | |
1344 | pos += 2 + SSL3_RANDOM_SIZE; | |
1345 | ||
1346 | /* Skip Session ID */ | |
1347 | if (end - pos < 1) | |
1348 | return; | |
1349 | id_len = *pos++; | |
1350 | if ((size_t) (end - pos) < id_len) | |
1351 | return; | |
1352 | pos += id_len; | |
1353 | ||
1354 | if (end - pos < 2) | |
1355 | return; | |
1356 | conn->cipher_suite = WPA_GET_BE16(pos); | |
1357 | wpa_printf(MSG_DEBUG, "OpenSSL: Server selected cipher suite 0x%x", | |
1358 | conn->cipher_suite); | |
1359 | } | |
1360 | ||
1361 | ||
1362 | static void check_server_key_exchange(SSL *ssl, struct tls_connection *conn, | |
1363 | const u8 *pos, const u8 *end) | |
1364 | { | |
1365 | size_t payload_len; | |
1366 | u16 dh_len; | |
1367 | BIGNUM *p; | |
1368 | int bits; | |
1369 | ||
1370 | if (!(conn->flags & TLS_CONN_SUITEB)) | |
1371 | return; | |
1372 | ||
1373 | /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */ | |
1374 | if (conn->cipher_suite != 0x9f) | |
1375 | return; | |
1376 | ||
1377 | if (end - pos < 3) | |
1378 | return; | |
1379 | payload_len = WPA_GET_BE24(pos); | |
1380 | pos += 3; | |
1381 | ||
1382 | if ((size_t) (end - pos) < payload_len) | |
1383 | return; | |
1384 | end = pos + payload_len; | |
1385 | ||
1386 | if (end - pos < 2) | |
1387 | return; | |
1388 | dh_len = WPA_GET_BE16(pos); | |
1389 | pos += 2; | |
1390 | ||
1391 | if ((size_t) (end - pos) < dh_len) | |
1392 | return; | |
1393 | p = BN_bin2bn(pos, dh_len, NULL); | |
1394 | if (!p) | |
1395 | return; | |
1396 | ||
1397 | bits = BN_num_bits(p); | |
1398 | BN_free(p); | |
1399 | ||
1400 | conn->server_dh_prime_len = bits; | |
1401 | wpa_printf(MSG_DEBUG, "OpenSSL: Server DH prime length: %d bits", | |
1402 | conn->server_dh_prime_len); | |
1403 | } | |
1404 | ||
1405 | #endif /* CONFIG_SUITEB */ | |
1406 | ||
1407 | ||
bb52293e JM |
1408 | static void tls_msg_cb(int write_p, int version, int content_type, |
1409 | const void *buf, size_t len, SSL *ssl, void *arg) | |
1410 | { | |
1411 | struct tls_connection *conn = arg; | |
1412 | const u8 *pos = buf; | |
1413 | ||
faf8f293 JM |
1414 | if (write_p == 2) { |
1415 | wpa_printf(MSG_DEBUG, | |
1416 | "OpenSSL: session ver=0x%x content_type=%d", | |
1417 | version, content_type); | |
1418 | wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len); | |
1419 | return; | |
1420 | } | |
1421 | ||
cbb15497 JM |
1422 | wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)", |
1423 | write_p ? "TX" : "RX", version, content_type, | |
1424 | openssl_content_type(content_type), | |
1425 | openssl_handshake_type(content_type, buf, len)); | |
bb52293e JM |
1426 | wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len); |
1427 | if (content_type == 24 && len >= 3 && pos[0] == 1) { | |
1428 | size_t payload_len = WPA_GET_BE16(pos + 1); | |
1429 | if (payload_len + 3 > len) { | |
1430 | wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected"); | |
1431 | conn->invalid_hb_used = 1; | |
1432 | } | |
1433 | } | |
1c9663cf JM |
1434 | |
1435 | #ifdef CONFIG_SUITEB | |
1436 | /* | |
1437 | * Need to parse these handshake messages to be able to check DH prime | |
1438 | * length since OpenSSL does not expose the new cipher suite and DH | |
1439 | * parameters during handshake (e.g., for cert_cb() callback). | |
1440 | */ | |
1441 | if (content_type == 22 && pos && len > 0 && pos[0] == 2) | |
1442 | check_server_hello(conn, pos + 1, pos + len); | |
1443 | if (content_type == 22 && pos && len > 0 && pos[0] == 12) | |
1444 | check_server_key_exchange(ssl, conn, pos + 1, pos + len); | |
1445 | #endif /* CONFIG_SUITEB */ | |
bb52293e JM |
1446 | } |
1447 | ||
1448 | ||
6a31a31d | 1449 | struct tls_connection * tls_connection_init(void *ssl_ctx) |
6fc6879b | 1450 | { |
bd9b8b2b JM |
1451 | struct tls_data *data = ssl_ctx; |
1452 | SSL_CTX *ssl = data->ssl; | |
6a31a31d | 1453 | struct tls_connection *conn; |
fca25ef4 | 1454 | long options; |
6a31a31d | 1455 | struct tls_context *context = SSL_CTX_get_app_data(ssl); |
6fc6879b | 1456 | |
6a31a31d JM |
1457 | conn = os_zalloc(sizeof(*conn)); |
1458 | if (conn == NULL) | |
1459 | return NULL; | |
bd9b8b2b | 1460 | conn->ssl_ctx = ssl; |
6a31a31d JM |
1461 | conn->ssl = SSL_new(ssl); |
1462 | if (conn->ssl == NULL) { | |
6fc6879b JM |
1463 | tls_show_errors(MSG_INFO, __func__, |
1464 | "Failed to initialize new SSL connection"); | |
6a31a31d JM |
1465 | os_free(conn); |
1466 | return NULL; | |
6fc6879b JM |
1467 | } |
1468 | ||
6a31a31d JM |
1469 | conn->context = context; |
1470 | SSL_set_app_data(conn->ssl, conn); | |
1471 | SSL_set_msg_callback(conn->ssl, tls_msg_cb); | |
1472 | SSL_set_msg_callback_arg(conn->ssl, conn); | |
fca25ef4 JM |
1473 | options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | |
1474 | SSL_OP_SINGLE_DH_USE; | |
1475 | #ifdef SSL_OP_NO_COMPRESSION | |
1476 | options |= SSL_OP_NO_COMPRESSION; | |
1477 | #endif /* SSL_OP_NO_COMPRESSION */ | |
6a31a31d | 1478 | SSL_set_options(conn->ssl, options); |
6fc6879b | 1479 | |
6a31a31d JM |
1480 | conn->ssl_in = BIO_new(BIO_s_mem()); |
1481 | if (!conn->ssl_in) { | |
6fc6879b JM |
1482 | tls_show_errors(MSG_INFO, __func__, |
1483 | "Failed to create a new BIO for ssl_in"); | |
6a31a31d JM |
1484 | SSL_free(conn->ssl); |
1485 | os_free(conn); | |
1486 | return NULL; | |
6fc6879b JM |
1487 | } |
1488 | ||
6a31a31d JM |
1489 | conn->ssl_out = BIO_new(BIO_s_mem()); |
1490 | if (!conn->ssl_out) { | |
6fc6879b JM |
1491 | tls_show_errors(MSG_INFO, __func__, |
1492 | "Failed to create a new BIO for ssl_out"); | |
1493 | SSL_free(conn->ssl); | |
6a31a31d | 1494 | BIO_free(conn->ssl_in); |
6fc6879b JM |
1495 | os_free(conn); |
1496 | return NULL; | |
1497 | } | |
1498 | ||
6a31a31d JM |
1499 | SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out); |
1500 | ||
6fc6879b JM |
1501 | return conn; |
1502 | } | |
1503 | ||
1504 | ||
1505 | void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn) | |
1506 | { | |
1507 | if (conn == NULL) | |
1508 | return; | |
acf36f31 JM |
1509 | if (conn->success_data) { |
1510 | /* | |
1511 | * Make sure ssl_clear_bad_session() does not remove this | |
1512 | * session. | |
1513 | */ | |
1514 | SSL_set_quiet_shutdown(conn->ssl, 1); | |
1515 | SSL_shutdown(conn->ssl); | |
1516 | } | |
6fc6879b JM |
1517 | SSL_free(conn->ssl); |
1518 | tls_engine_deinit(conn); | |
1519 | os_free(conn->subject_match); | |
1520 | os_free(conn->altsubject_match); | |
01f809c7 | 1521 | os_free(conn->suffix_match); |
cebee30f | 1522 | os_free(conn->domain_match); |
6fc6879b JM |
1523 | os_free(conn->session_ticket); |
1524 | os_free(conn); | |
1525 | } | |
1526 | ||
1527 | ||
1528 | int tls_connection_established(void *ssl_ctx, struct tls_connection *conn) | |
1529 | { | |
1530 | return conn ? SSL_is_init_finished(conn->ssl) : 0; | |
1531 | } | |
1532 | ||
1533 | ||
1534 | int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn) | |
1535 | { | |
1536 | if (conn == NULL) | |
1537 | return -1; | |
1538 | ||
1539 | /* Shutdown previous TLS connection without notifying the peer | |
1540 | * because the connection was already terminated in practice | |
1541 | * and "close notify" shutdown alert would confuse AS. */ | |
1542 | SSL_set_quiet_shutdown(conn->ssl, 1); | |
1543 | SSL_shutdown(conn->ssl); | |
a7803b0c | 1544 | return SSL_clear(conn->ssl) == 1 ? 0 : -1; |
6fc6879b JM |
1545 | } |
1546 | ||
1547 | ||
1548 | static int tls_match_altsubject_component(X509 *cert, int type, | |
1549 | const char *value, size_t len) | |
1550 | { | |
1551 | GENERAL_NAME *gen; | |
1552 | void *ext; | |
a8572960 AL |
1553 | int found = 0; |
1554 | stack_index_t i; | |
6fc6879b JM |
1555 | |
1556 | ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); | |
1557 | ||
1558 | for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) { | |
1559 | gen = sk_GENERAL_NAME_value(ext, i); | |
1560 | if (gen->type != type) | |
1561 | continue; | |
1562 | if (os_strlen((char *) gen->d.ia5->data) == len && | |
1563 | os_memcmp(value, gen->d.ia5->data, len) == 0) | |
1564 | found++; | |
1565 | } | |
1566 | ||
0f096370 JM |
1567 | sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free); |
1568 | ||
6fc6879b JM |
1569 | return found; |
1570 | } | |
1571 | ||
1572 | ||
1573 | static int tls_match_altsubject(X509 *cert, const char *match) | |
1574 | { | |
1575 | int type; | |
1576 | const char *pos, *end; | |
1577 | size_t len; | |
1578 | ||
1579 | pos = match; | |
1580 | do { | |
1581 | if (os_strncmp(pos, "EMAIL:", 6) == 0) { | |
1582 | type = GEN_EMAIL; | |
1583 | pos += 6; | |
1584 | } else if (os_strncmp(pos, "DNS:", 4) == 0) { | |
1585 | type = GEN_DNS; | |
1586 | pos += 4; | |
1587 | } else if (os_strncmp(pos, "URI:", 4) == 0) { | |
1588 | type = GEN_URI; | |
1589 | pos += 4; | |
1590 | } else { | |
1591 | wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName " | |
1592 | "match '%s'", pos); | |
1593 | return 0; | |
1594 | } | |
1595 | end = os_strchr(pos, ';'); | |
1596 | while (end) { | |
1597 | if (os_strncmp(end + 1, "EMAIL:", 6) == 0 || | |
1598 | os_strncmp(end + 1, "DNS:", 4) == 0 || | |
1599 | os_strncmp(end + 1, "URI:", 4) == 0) | |
1600 | break; | |
1601 | end = os_strchr(end + 1, ';'); | |
1602 | } | |
1603 | if (end) | |
1604 | len = end - pos; | |
1605 | else | |
1606 | len = os_strlen(pos); | |
1607 | if (tls_match_altsubject_component(cert, type, pos, len) > 0) | |
1608 | return 1; | |
1609 | pos = end + 1; | |
1610 | } while (end); | |
1611 | ||
1612 | return 0; | |
1613 | } | |
1614 | ||
1615 | ||
461e3ebe | 1616 | #ifndef CONFIG_NATIVE_WINDOWS |
cebee30f JM |
1617 | static int domain_suffix_match(const u8 *val, size_t len, const char *match, |
1618 | int full) | |
01f809c7 JM |
1619 | { |
1620 | size_t i, match_len; | |
1621 | ||
1622 | /* Check for embedded nuls that could mess up suffix matching */ | |
1623 | for (i = 0; i < len; i++) { | |
1624 | if (val[i] == '\0') { | |
1625 | wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject"); | |
1626 | return 0; | |
1627 | } | |
1628 | } | |
1629 | ||
1630 | match_len = os_strlen(match); | |
cebee30f | 1631 | if (match_len > len || (full && match_len != len)) |
01f809c7 JM |
1632 | return 0; |
1633 | ||
1634 | if (os_strncasecmp((const char *) val + len - match_len, match, | |
1635 | match_len) != 0) | |
1636 | return 0; /* no match */ | |
1637 | ||
1638 | if (match_len == len) | |
1639 | return 1; /* exact match */ | |
1640 | ||
1641 | if (val[len - match_len - 1] == '.') | |
1642 | return 1; /* full label match completes suffix match */ | |
1643 | ||
1644 | wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match"); | |
1645 | return 0; | |
1646 | } | |
461e3ebe | 1647 | #endif /* CONFIG_NATIVE_WINDOWS */ |
01f809c7 JM |
1648 | |
1649 | ||
cebee30f | 1650 | static int tls_match_suffix(X509 *cert, const char *match, int full) |
01f809c7 | 1651 | { |
461e3ebe JM |
1652 | #ifdef CONFIG_NATIVE_WINDOWS |
1653 | /* wincrypt.h has conflicting X509_NAME definition */ | |
1654 | return -1; | |
1655 | #else /* CONFIG_NATIVE_WINDOWS */ | |
01f809c7 JM |
1656 | GENERAL_NAME *gen; |
1657 | void *ext; | |
1658 | int i; | |
98a1571d | 1659 | stack_index_t j; |
01f809c7 JM |
1660 | int dns_name = 0; |
1661 | X509_NAME *name; | |
1662 | ||
cebee30f JM |
1663 | wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s", |
1664 | full ? "": "suffix ", match); | |
01f809c7 JM |
1665 | |
1666 | ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); | |
1667 | ||
98a1571d JM |
1668 | for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) { |
1669 | gen = sk_GENERAL_NAME_value(ext, j); | |
01f809c7 JM |
1670 | if (gen->type != GEN_DNS) |
1671 | continue; | |
1672 | dns_name++; | |
1673 | wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName", | |
1674 | gen->d.dNSName->data, | |
1675 | gen->d.dNSName->length); | |
1676 | if (domain_suffix_match(gen->d.dNSName->data, | |
cebee30f JM |
1677 | gen->d.dNSName->length, match, full) == |
1678 | 1) { | |
1679 | wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found", | |
1680 | full ? "Match" : "Suffix match"); | |
0f096370 | 1681 | sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free); |
01f809c7 JM |
1682 | return 1; |
1683 | } | |
1684 | } | |
0f096370 | 1685 | sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free); |
01f809c7 JM |
1686 | |
1687 | if (dns_name) { | |
1688 | wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched"); | |
1689 | return 0; | |
1690 | } | |
1691 | ||
1692 | name = X509_get_subject_name(cert); | |
1693 | i = -1; | |
1694 | for (;;) { | |
1695 | X509_NAME_ENTRY *e; | |
1696 | ASN1_STRING *cn; | |
1697 | ||
1698 | i = X509_NAME_get_index_by_NID(name, NID_commonName, i); | |
1699 | if (i == -1) | |
1700 | break; | |
1701 | e = X509_NAME_get_entry(name, i); | |
1702 | if (e == NULL) | |
1703 | continue; | |
1704 | cn = X509_NAME_ENTRY_get_data(e); | |
1705 | if (cn == NULL) | |
1706 | continue; | |
1707 | wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName", | |
1708 | cn->data, cn->length); | |
cebee30f JM |
1709 | if (domain_suffix_match(cn->data, cn->length, match, full) == 1) |
1710 | { | |
1711 | wpa_printf(MSG_DEBUG, "TLS: %s in commonName found", | |
1712 | full ? "Match" : "Suffix match"); | |
01f809c7 JM |
1713 | return 1; |
1714 | } | |
1715 | } | |
1716 | ||
cebee30f JM |
1717 | wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found", |
1718 | full ? "": "suffix "); | |
01f809c7 | 1719 | return 0; |
461e3ebe | 1720 | #endif /* CONFIG_NATIVE_WINDOWS */ |
01f809c7 JM |
1721 | } |
1722 | ||
1723 | ||
00468b46 JM |
1724 | static enum tls_fail_reason openssl_tls_fail_reason(int err) |
1725 | { | |
1726 | switch (err) { | |
1727 | case X509_V_ERR_CERT_REVOKED: | |
1728 | return TLS_FAIL_REVOKED; | |
1729 | case X509_V_ERR_CERT_NOT_YET_VALID: | |
1730 | case X509_V_ERR_CRL_NOT_YET_VALID: | |
1731 | return TLS_FAIL_NOT_YET_VALID; | |
1732 | case X509_V_ERR_CERT_HAS_EXPIRED: | |
1733 | case X509_V_ERR_CRL_HAS_EXPIRED: | |
1734 | return TLS_FAIL_EXPIRED; | |
1735 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: | |
1736 | case X509_V_ERR_UNABLE_TO_GET_CRL: | |
1737 | case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER: | |
1738 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: | |
1739 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: | |
1740 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: | |
1741 | case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: | |
1742 | case X509_V_ERR_CERT_CHAIN_TOO_LONG: | |
1743 | case X509_V_ERR_PATH_LENGTH_EXCEEDED: | |
1744 | case X509_V_ERR_INVALID_CA: | |
1745 | return TLS_FAIL_UNTRUSTED; | |
1746 | case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: | |
1747 | case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: | |
1748 | case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: | |
1749 | case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: | |
1750 | case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: | |
1751 | case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: | |
1752 | case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: | |
1753 | case X509_V_ERR_CERT_UNTRUSTED: | |
1754 | case X509_V_ERR_CERT_REJECTED: | |
1755 | return TLS_FAIL_BAD_CERTIFICATE; | |
1756 | default: | |
1757 | return TLS_FAIL_UNSPECIFIED; | |
1758 | } | |
1759 | } | |
1760 | ||
1761 | ||
1762 | static struct wpabuf * get_x509_cert(X509 *cert) | |
1763 | { | |
1764 | struct wpabuf *buf; | |
1765 | u8 *tmp; | |
1766 | ||
1767 | int cert_len = i2d_X509(cert, NULL); | |
1768 | if (cert_len <= 0) | |
1769 | return NULL; | |
1770 | ||
1771 | buf = wpabuf_alloc(cert_len); | |
1772 | if (buf == NULL) | |
1773 | return NULL; | |
1774 | ||
1775 | tmp = wpabuf_put(buf, cert_len); | |
1776 | i2d_X509(cert, &tmp); | |
1777 | return buf; | |
1778 | } | |
1779 | ||
1780 | ||
1781 | static void openssl_tls_fail_event(struct tls_connection *conn, | |
1782 | X509 *err_cert, int err, int depth, | |
1783 | const char *subject, const char *err_str, | |
1784 | enum tls_fail_reason reason) | |
1785 | { | |
1786 | union tls_event_data ev; | |
1787 | struct wpabuf *cert = NULL; | |
7c0e1e27 | 1788 | struct tls_context *context = conn->context; |
00468b46 | 1789 | |
7c0e1e27 | 1790 | if (context->event_cb == NULL) |
00468b46 JM |
1791 | return; |
1792 | ||
1793 | cert = get_x509_cert(err_cert); | |
1794 | os_memset(&ev, 0, sizeof(ev)); | |
1795 | ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ? | |
1796 | reason : openssl_tls_fail_reason(err); | |
1797 | ev.cert_fail.depth = depth; | |
1798 | ev.cert_fail.subject = subject; | |
1799 | ev.cert_fail.reason_txt = err_str; | |
1800 | ev.cert_fail.cert = cert; | |
7c0e1e27 | 1801 | context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev); |
00468b46 JM |
1802 | wpabuf_free(cert); |
1803 | } | |
1804 | ||
1805 | ||
1806 | static void openssl_tls_cert_event(struct tls_connection *conn, | |
1807 | X509 *err_cert, int depth, | |
1808 | const char *subject) | |
1809 | { | |
1810 | struct wpabuf *cert = NULL; | |
1811 | union tls_event_data ev; | |
7c0e1e27 | 1812 | struct tls_context *context = conn->context; |
d07d3fbd JM |
1813 | char *altsubject[TLS_MAX_ALT_SUBJECT]; |
1814 | int alt, num_altsubject = 0; | |
1815 | GENERAL_NAME *gen; | |
1816 | void *ext; | |
1817 | stack_index_t i; | |
00468b46 JM |
1818 | #ifdef CONFIG_SHA256 |
1819 | u8 hash[32]; | |
1820 | #endif /* CONFIG_SHA256 */ | |
1821 | ||
7c0e1e27 | 1822 | if (context->event_cb == NULL) |
00468b46 JM |
1823 | return; |
1824 | ||
1825 | os_memset(&ev, 0, sizeof(ev)); | |
3c108b75 JM |
1826 | if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) || |
1827 | context->cert_in_cb) { | |
00468b46 JM |
1828 | cert = get_x509_cert(err_cert); |
1829 | ev.peer_cert.cert = cert; | |
1830 | } | |
1831 | #ifdef CONFIG_SHA256 | |
1832 | if (cert) { | |
1833 | const u8 *addr[1]; | |
1834 | size_t len[1]; | |
1835 | addr[0] = wpabuf_head(cert); | |
1836 | len[0] = wpabuf_len(cert); | |
1837 | if (sha256_vector(1, addr, len, hash) == 0) { | |
1838 | ev.peer_cert.hash = hash; | |
1839 | ev.peer_cert.hash_len = sizeof(hash); | |
1840 | } | |
1841 | } | |
1842 | #endif /* CONFIG_SHA256 */ | |
1843 | ev.peer_cert.depth = depth; | |
1844 | ev.peer_cert.subject = subject; | |
d07d3fbd JM |
1845 | |
1846 | ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL); | |
1847 | for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) { | |
1848 | char *pos; | |
1849 | ||
1850 | if (num_altsubject == TLS_MAX_ALT_SUBJECT) | |
1851 | break; | |
1852 | gen = sk_GENERAL_NAME_value(ext, i); | |
1853 | if (gen->type != GEN_EMAIL && | |
1854 | gen->type != GEN_DNS && | |
1855 | gen->type != GEN_URI) | |
1856 | continue; | |
1857 | ||
1858 | pos = os_malloc(10 + gen->d.ia5->length + 1); | |
1859 | if (pos == NULL) | |
1860 | break; | |
1861 | altsubject[num_altsubject++] = pos; | |
1862 | ||
1863 | switch (gen->type) { | |
1864 | case GEN_EMAIL: | |
1865 | os_memcpy(pos, "EMAIL:", 6); | |
1866 | pos += 6; | |
1867 | break; | |
1868 | case GEN_DNS: | |
1869 | os_memcpy(pos, "DNS:", 4); | |
1870 | pos += 4; | |
1871 | break; | |
1872 | case GEN_URI: | |
1873 | os_memcpy(pos, "URI:", 4); | |
1874 | pos += 4; | |
1875 | break; | |
1876 | } | |
1877 | ||
1878 | os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length); | |
1879 | pos += gen->d.ia5->length; | |
1880 | *pos = '\0'; | |
1881 | } | |
0f096370 | 1882 | sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free); |
d07d3fbd JM |
1883 | |
1884 | for (alt = 0; alt < num_altsubject; alt++) | |
1885 | ev.peer_cert.altsubject[alt] = altsubject[alt]; | |
1886 | ev.peer_cert.num_altsubject = num_altsubject; | |
1887 | ||
7c0e1e27 | 1888 | context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev); |
00468b46 | 1889 | wpabuf_free(cert); |
d07d3fbd JM |
1890 | for (alt = 0; alt < num_altsubject; alt++) |
1891 | os_free(altsubject[alt]); | |
00468b46 JM |
1892 | } |
1893 | ||
1894 | ||
6fc6879b JM |
1895 | static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx) |
1896 | { | |
1897 | char buf[256]; | |
1898 | X509 *err_cert; | |
1899 | int err, depth; | |
1900 | SSL *ssl; | |
1901 | struct tls_connection *conn; | |
7c0e1e27 | 1902 | struct tls_context *context; |
cebee30f | 1903 | char *match, *altmatch, *suffix_match, *domain_match; |
00468b46 | 1904 | const char *err_str; |
6fc6879b JM |
1905 | |
1906 | err_cert = X509_STORE_CTX_get_current_cert(x509_ctx); | |
97efe70b EL |
1907 | if (!err_cert) |
1908 | return 0; | |
1909 | ||
6fc6879b JM |
1910 | err = X509_STORE_CTX_get_error(x509_ctx); |
1911 | depth = X509_STORE_CTX_get_error_depth(x509_ctx); | |
1912 | ssl = X509_STORE_CTX_get_ex_data(x509_ctx, | |
1913 | SSL_get_ex_data_X509_STORE_CTX_idx()); | |
1914 | X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf)); | |
1915 | ||
1916 | conn = SSL_get_app_data(ssl); | |
0bdaa741 JM |
1917 | if (conn == NULL) |
1918 | return 0; | |
080585c0 JM |
1919 | |
1920 | if (depth == 0) | |
1921 | conn->peer_cert = err_cert; | |
1922 | else if (depth == 1) | |
1923 | conn->peer_issuer = err_cert; | |
6bf61fb2 JM |
1924 | else if (depth == 2) |
1925 | conn->peer_issuer_issuer = err_cert; | |
080585c0 | 1926 | |
7c0e1e27 | 1927 | context = conn->context; |
0bdaa741 JM |
1928 | match = conn->subject_match; |
1929 | altmatch = conn->altsubject_match; | |
01f809c7 | 1930 | suffix_match = conn->suffix_match; |
cebee30f | 1931 | domain_match = conn->domain_match; |
6fc6879b | 1932 | |
00468b46 JM |
1933 | if (!preverify_ok && !conn->ca_cert_verify) |
1934 | preverify_ok = 1; | |
1935 | if (!preverify_ok && depth > 0 && conn->server_cert_only) | |
1936 | preverify_ok = 1; | |
235279e7 JM |
1937 | if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) && |
1938 | (err == X509_V_ERR_CERT_HAS_EXPIRED || | |
1939 | err == X509_V_ERR_CERT_NOT_YET_VALID)) { | |
1940 | wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity " | |
1941 | "time mismatch"); | |
1942 | preverify_ok = 1; | |
1943 | } | |
00468b46 JM |
1944 | |
1945 | err_str = X509_verify_cert_error_string(err); | |
1946 | ||
1947 | #ifdef CONFIG_SHA256 | |
00033a09 RA |
1948 | /* |
1949 | * Do not require preverify_ok so we can explicity allow otherwise | |
1950 | * invalid pinned server certificates. | |
1951 | */ | |
1952 | if (depth == 0 && conn->server_cert_only) { | |
00468b46 JM |
1953 | struct wpabuf *cert; |
1954 | cert = get_x509_cert(err_cert); | |
1955 | if (!cert) { | |
1956 | wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch " | |
1957 | "server certificate data"); | |
6fc6879b | 1958 | preverify_ok = 0; |
00468b46 JM |
1959 | } else { |
1960 | u8 hash[32]; | |
1961 | const u8 *addr[1]; | |
1962 | size_t len[1]; | |
1963 | addr[0] = wpabuf_head(cert); | |
1964 | len[0] = wpabuf_len(cert); | |
1965 | if (sha256_vector(1, addr, len, hash) < 0 || | |
1966 | os_memcmp(conn->srv_cert_hash, hash, 32) != 0) { | |
1967 | err_str = "Server certificate mismatch"; | |
1968 | err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN; | |
1969 | preverify_ok = 0; | |
00033a09 RA |
1970 | } else if (!preverify_ok) { |
1971 | /* | |
1972 | * Certificate matches pinned certificate, allow | |
1973 | * regardless of other problems. | |
1974 | */ | |
1975 | wpa_printf(MSG_DEBUG, | |
1976 | "OpenSSL: Ignore validation issues for a pinned server certificate"); | |
1977 | preverify_ok = 1; | |
00468b46 JM |
1978 | } |
1979 | wpabuf_free(cert); | |
6fc6879b JM |
1980 | } |
1981 | } | |
00468b46 JM |
1982 | #endif /* CONFIG_SHA256 */ |
1983 | ||
1984 | if (!preverify_ok) { | |
1985 | wpa_printf(MSG_WARNING, "TLS: Certificate verification failed," | |
1986 | " error %d (%s) depth %d for '%s'", err, err_str, | |
1987 | depth, buf); | |
1988 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, | |
1989 | err_str, TLS_FAIL_UNSPECIFIED); | |
1990 | return preverify_ok; | |
1991 | } | |
1992 | ||
1993 | wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d " | |
1994 | "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'", | |
1995 | preverify_ok, err, err_str, | |
1996 | conn->ca_cert_verify, depth, buf); | |
1997 | if (depth == 0 && match && os_strstr(buf, match) == NULL) { | |
1998 | wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not " | |
1999 | "match with '%s'", buf, match); | |
2000 | preverify_ok = 0; | |
2001 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, | |
2002 | "Subject mismatch", | |
2003 | TLS_FAIL_SUBJECT_MISMATCH); | |
2004 | } else if (depth == 0 && altmatch && | |
2005 | !tls_match_altsubject(err_cert, altmatch)) { | |
2006 | wpa_printf(MSG_WARNING, "TLS: altSubjectName match " | |
2007 | "'%s' not found", altmatch); | |
2008 | preverify_ok = 0; | |
2009 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, | |
2010 | "AltSubject mismatch", | |
2011 | TLS_FAIL_ALTSUBJECT_MISMATCH); | |
01f809c7 | 2012 | } else if (depth == 0 && suffix_match && |
cebee30f | 2013 | !tls_match_suffix(err_cert, suffix_match, 0)) { |
01f809c7 JM |
2014 | wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found", |
2015 | suffix_match); | |
2016 | preverify_ok = 0; | |
2017 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, | |
2018 | "Domain suffix mismatch", | |
2019 | TLS_FAIL_DOMAIN_SUFFIX_MISMATCH); | |
cebee30f JM |
2020 | } else if (depth == 0 && domain_match && |
2021 | !tls_match_suffix(err_cert, domain_match, 1)) { | |
2022 | wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found", | |
2023 | domain_match); | |
2024 | preverify_ok = 0; | |
2025 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, | |
2026 | "Domain mismatch", | |
2027 | TLS_FAIL_DOMAIN_MISMATCH); | |
00468b46 JM |
2028 | } else |
2029 | openssl_tls_cert_event(conn, err_cert, depth, buf); | |
2030 | ||
2031 | if (conn->cert_probe && preverify_ok && depth == 0) { | |
2032 | wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate " | |
2033 | "on probe-only run"); | |
2034 | preverify_ok = 0; | |
2035 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, | |
2036 | "Server certificate chain probe", | |
2037 | TLS_FAIL_SERVER_CHAIN_PROBE); | |
2038 | } | |
6fc6879b | 2039 | |
4eb8cfe0 JM |
2040 | #ifdef CONFIG_SUITEB |
2041 | if (conn->flags & TLS_CONN_SUITEB) { | |
2042 | EVP_PKEY *pk; | |
2043 | RSA *rsa; | |
2044 | int len = -1; | |
2045 | ||
2046 | pk = X509_get_pubkey(err_cert); | |
2047 | if (pk) { | |
2048 | rsa = EVP_PKEY_get1_RSA(pk); | |
2049 | if (rsa) { | |
2050 | len = RSA_bits(rsa); | |
2051 | RSA_free(rsa); | |
2052 | } | |
2053 | EVP_PKEY_free(pk); | |
2054 | } | |
2055 | ||
2056 | if (len >= 0) { | |
2057 | wpa_printf(MSG_DEBUG, | |
2058 | "OpenSSL: RSA modulus size: %d bits", len); | |
2059 | if (len < 3072) { | |
2060 | preverify_ok = 0; | |
2061 | openssl_tls_fail_event( | |
2062 | conn, err_cert, err, | |
2063 | depth, buf, | |
2064 | "Insufficient RSA modulus size", | |
2065 | TLS_FAIL_INSUFFICIENT_KEY_LEN); | |
2066 | } | |
2067 | } | |
2068 | } | |
2069 | #endif /* CONFIG_SUITEB */ | |
2070 | ||
bdee6ca0 | 2071 | #ifdef OPENSSL_IS_BORINGSSL |
213e158c JM |
2072 | if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) && |
2073 | preverify_ok) { | |
bdee6ca0 JM |
2074 | enum ocsp_result res; |
2075 | ||
213e158c JM |
2076 | res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert, |
2077 | conn->peer_issuer, | |
2078 | conn->peer_issuer_issuer); | |
bdee6ca0 JM |
2079 | if (res == OCSP_REVOKED) { |
2080 | preverify_ok = 0; | |
2081 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, | |
2082 | "certificate revoked", | |
2083 | TLS_FAIL_REVOKED); | |
2084 | if (err == X509_V_OK) | |
2085 | X509_STORE_CTX_set_error( | |
2086 | x509_ctx, X509_V_ERR_CERT_REVOKED); | |
2087 | } else if (res != OCSP_GOOD && | |
2088 | (conn->flags & TLS_CONN_REQUIRE_OCSP)) { | |
2089 | preverify_ok = 0; | |
2090 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, | |
2091 | "bad certificate status response", | |
2092 | TLS_FAIL_UNSPECIFIED); | |
2093 | } | |
2094 | } | |
2095 | #endif /* OPENSSL_IS_BORINGSSL */ | |
2096 | ||
3c108b75 | 2097 | if (depth == 0 && preverify_ok && context->event_cb != NULL) |
7c0e1e27 PS |
2098 | context->event_cb(context->cb_ctx, |
2099 | TLS_CERT_CHAIN_SUCCESS, NULL); | |
dd7fec1f | 2100 | |
6fc6879b JM |
2101 | return preverify_ok; |
2102 | } | |
2103 | ||
2104 | ||
2105 | #ifndef OPENSSL_NO_STDIO | |
bd9b8b2b | 2106 | static int tls_load_ca_der(struct tls_data *data, const char *ca_cert) |
6fc6879b | 2107 | { |
bd9b8b2b | 2108 | SSL_CTX *ssl_ctx = data->ssl; |
6fc6879b JM |
2109 | X509_LOOKUP *lookup; |
2110 | int ret = 0; | |
2111 | ||
68ae4773 | 2112 | lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx), |
6fc6879b JM |
2113 | X509_LOOKUP_file()); |
2114 | if (lookup == NULL) { | |
2115 | tls_show_errors(MSG_WARNING, __func__, | |
2116 | "Failed add lookup for X509 store"); | |
2117 | return -1; | |
2118 | } | |
2119 | ||
2120 | if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) { | |
2121 | unsigned long err = ERR_peek_error(); | |
2122 | tls_show_errors(MSG_WARNING, __func__, | |
2123 | "Failed load CA in DER format"); | |
2124 | if (ERR_GET_LIB(err) == ERR_LIB_X509 && | |
2125 | ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) { | |
2126 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring " | |
2127 | "cert already in hash table error", | |
2128 | __func__); | |
2129 | } else | |
2130 | ret = -1; | |
2131 | } | |
2132 | ||
2133 | return ret; | |
2134 | } | |
2135 | #endif /* OPENSSL_NO_STDIO */ | |
2136 | ||
2137 | ||
bd9b8b2b JM |
2138 | static int tls_connection_ca_cert(struct tls_data *data, |
2139 | struct tls_connection *conn, | |
6fc6879b JM |
2140 | const char *ca_cert, const u8 *ca_cert_blob, |
2141 | size_t ca_cert_blob_len, const char *ca_path) | |
2142 | { | |
bd9b8b2b | 2143 | SSL_CTX *ssl_ctx = data->ssl; |
68ae4773 | 2144 | X509_STORE *store; |
6fc6879b JM |
2145 | |
2146 | /* | |
2147 | * Remove previously configured trusted CA certificates before adding | |
2148 | * new ones. | |
2149 | */ | |
68ae4773 JM |
2150 | store = X509_STORE_new(); |
2151 | if (store == NULL) { | |
6fc6879b JM |
2152 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new " |
2153 | "certificate store", __func__); | |
2154 | return -1; | |
2155 | } | |
68ae4773 | 2156 | SSL_CTX_set_cert_store(ssl_ctx, store); |
6fc6879b | 2157 | |
00468b46 JM |
2158 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb); |
2159 | conn->ca_cert_verify = 1; | |
2160 | ||
2161 | if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) { | |
2162 | wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate " | |
2163 | "chain"); | |
2164 | conn->cert_probe = 1; | |
2165 | conn->ca_cert_verify = 0; | |
2166 | return 0; | |
2167 | } | |
2168 | ||
2169 | if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) { | |
2170 | #ifdef CONFIG_SHA256 | |
2171 | const char *pos = ca_cert + 7; | |
2172 | if (os_strncmp(pos, "server/sha256/", 14) != 0) { | |
2173 | wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert " | |
2174 | "hash value '%s'", ca_cert); | |
2175 | return -1; | |
2176 | } | |
2177 | pos += 14; | |
2178 | if (os_strlen(pos) != 32 * 2) { | |
2179 | wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 " | |
2180 | "hash length in ca_cert '%s'", ca_cert); | |
2181 | return -1; | |
2182 | } | |
2183 | if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) { | |
2184 | wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash " | |
2185 | "value in ca_cert '%s'", ca_cert); | |
2186 | return -1; | |
2187 | } | |
2188 | conn->server_cert_only = 1; | |
2189 | wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server " | |
2190 | "certificate match"); | |
2191 | return 0; | |
2192 | #else /* CONFIG_SHA256 */ | |
2193 | wpa_printf(MSG_INFO, "No SHA256 included in the build - " | |
2194 | "cannot validate server certificate hash"); | |
2195 | return -1; | |
2196 | #endif /* CONFIG_SHA256 */ | |
2197 | } | |
2198 | ||
6fc6879b | 2199 | if (ca_cert_blob) { |
fee31f76 JM |
2200 | X509 *cert = d2i_X509(NULL, |
2201 | (const unsigned char **) &ca_cert_blob, | |
6fc6879b JM |
2202 | ca_cert_blob_len); |
2203 | if (cert == NULL) { | |
2204 | tls_show_errors(MSG_WARNING, __func__, | |
2205 | "Failed to parse ca_cert_blob"); | |
2206 | return -1; | |
2207 | } | |
2208 | ||
68ae4773 JM |
2209 | if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx), |
2210 | cert)) { | |
6fc6879b JM |
2211 | unsigned long err = ERR_peek_error(); |
2212 | tls_show_errors(MSG_WARNING, __func__, | |
2213 | "Failed to add ca_cert_blob to " | |
2214 | "certificate store"); | |
2215 | if (ERR_GET_LIB(err) == ERR_LIB_X509 && | |
2216 | ERR_GET_REASON(err) == | |
2217 | X509_R_CERT_ALREADY_IN_HASH_TABLE) { | |
2218 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring " | |
2219 | "cert already in hash table error", | |
2220 | __func__); | |
2221 | } else { | |
2222 | X509_free(cert); | |
2223 | return -1; | |
2224 | } | |
2225 | } | |
2226 | X509_free(cert); | |
2227 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob " | |
2228 | "to certificate store", __func__); | |
6fc6879b JM |
2229 | return 0; |
2230 | } | |
2231 | ||
2a0cd067 | 2232 | #ifdef ANDROID |
a8ef133f | 2233 | /* Single alias */ |
2a0cd067 | 2234 | if (ca_cert && os_strncmp("keystore://", ca_cert, 11) == 0) { |
f150db6c | 2235 | if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx), |
a8ef133f | 2236 | &ca_cert[11]) < 0) |
2a0cd067 | 2237 | return -1; |
a8ef133f RX |
2238 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb); |
2239 | return 0; | |
2240 | } | |
2a0cd067 | 2241 | |
a8ef133f RX |
2242 | /* Multiple aliases separated by space */ |
2243 | if (ca_cert && os_strncmp("keystores://", ca_cert, 12) == 0) { | |
2244 | char *aliases = os_strdup(&ca_cert[12]); | |
2245 | const char *delim = " "; | |
2246 | int rc = 0; | |
2247 | char *savedptr; | |
2248 | char *alias; | |
2249 | ||
2250 | if (!aliases) | |
2251 | return -1; | |
2252 | alias = strtok_r(aliases, delim, &savedptr); | |
2253 | for (; alias; alias = strtok_r(NULL, delim, &savedptr)) { | |
2254 | if (tls_add_ca_from_keystore_encoded( | |
f150db6c | 2255 | SSL_CTX_get_cert_store(ssl_ctx), alias)) { |
a8ef133f RX |
2256 | wpa_printf(MSG_WARNING, |
2257 | "OpenSSL: %s - Failed to add ca_cert %s from keystore", | |
2258 | __func__, alias); | |
2259 | rc = -1; | |
2260 | break; | |
2a0cd067 DS |
2261 | } |
2262 | } | |
a8ef133f RX |
2263 | os_free(aliases); |
2264 | if (rc) | |
2265 | return rc; | |
2266 | ||
2a0cd067 DS |
2267 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb); |
2268 | return 0; | |
2269 | } | |
2270 | #endif /* ANDROID */ | |
2271 | ||
6fc6879b JM |
2272 | #ifdef CONFIG_NATIVE_WINDOWS |
2273 | if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) == | |
2274 | 0) { | |
2275 | wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from " | |
2276 | "system certificate store"); | |
6fc6879b JM |
2277 | return 0; |
2278 | } | |
2279 | #endif /* CONFIG_NATIVE_WINDOWS */ | |
2280 | ||
2281 | if (ca_cert || ca_path) { | |
2282 | #ifndef OPENSSL_NO_STDIO | |
2283 | if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) != | |
2284 | 1) { | |
2285 | tls_show_errors(MSG_WARNING, __func__, | |
2286 | "Failed to load root certificates"); | |
2287 | if (ca_cert && | |
bd9b8b2b | 2288 | tls_load_ca_der(data, ca_cert) == 0) { |
6fc6879b JM |
2289 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded " |
2290 | "DER format CA certificate", | |
2291 | __func__); | |
2292 | } else | |
2293 | return -1; | |
2294 | } else { | |
2295 | wpa_printf(MSG_DEBUG, "TLS: Trusted root " | |
2296 | "certificate(s) loaded"); | |
bd9b8b2b | 2297 | tls_get_errors(data); |
6fc6879b | 2298 | } |
6fc6879b JM |
2299 | #else /* OPENSSL_NO_STDIO */ |
2300 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", | |
2301 | __func__); | |
2302 | return -1; | |
2303 | #endif /* OPENSSL_NO_STDIO */ | |
2304 | } else { | |
2305 | /* No ca_cert configured - do not try to verify server | |
2306 | * certificate */ | |
00468b46 | 2307 | conn->ca_cert_verify = 0; |
6fc6879b JM |
2308 | } |
2309 | ||
2310 | return 0; | |
2311 | } | |
2312 | ||
2313 | ||
bd9b8b2b | 2314 | static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert) |
6fc6879b | 2315 | { |
bd9b8b2b JM |
2316 | SSL_CTX *ssl_ctx = data->ssl; |
2317 | ||
6fc6879b JM |
2318 | if (ca_cert) { |
2319 | if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1) | |
2320 | { | |
2321 | tls_show_errors(MSG_WARNING, __func__, | |
2322 | "Failed to load root certificates"); | |
2323 | return -1; | |
2324 | } | |
2325 | ||
2326 | wpa_printf(MSG_DEBUG, "TLS: Trusted root " | |
2327 | "certificate(s) loaded"); | |
2328 | ||
2329 | #ifndef OPENSSL_NO_STDIO | |
2330 | /* Add the same CAs to the client certificate requests */ | |
2331 | SSL_CTX_set_client_CA_list(ssl_ctx, | |
2332 | SSL_load_client_CA_file(ca_cert)); | |
2333 | #endif /* OPENSSL_NO_STDIO */ | |
2334 | } | |
2335 | ||
2336 | return 0; | |
2337 | } | |
2338 | ||
2339 | ||
2340 | int tls_global_set_verify(void *ssl_ctx, int check_crl) | |
2341 | { | |
2342 | int flags; | |
2343 | ||
2344 | if (check_crl) { | |
bd9b8b2b JM |
2345 | struct tls_data *data = ssl_ctx; |
2346 | X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl); | |
6fc6879b JM |
2347 | if (cs == NULL) { |
2348 | tls_show_errors(MSG_INFO, __func__, "Failed to get " | |
2349 | "certificate store when enabling " | |
2350 | "check_crl"); | |
2351 | return -1; | |
2352 | } | |
2353 | flags = X509_V_FLAG_CRL_CHECK; | |
2354 | if (check_crl == 2) | |
2355 | flags |= X509_V_FLAG_CRL_CHECK_ALL; | |
2356 | X509_STORE_set_flags(cs, flags); | |
2357 | } | |
2358 | return 0; | |
2359 | } | |
2360 | ||
2361 | ||
2362 | static int tls_connection_set_subject_match(struct tls_connection *conn, | |
2363 | const char *subject_match, | |
01f809c7 | 2364 | const char *altsubject_match, |
cebee30f JM |
2365 | const char *suffix_match, |
2366 | const char *domain_match) | |
6fc6879b JM |
2367 | { |
2368 | os_free(conn->subject_match); | |
2369 | conn->subject_match = NULL; | |
2370 | if (subject_match) { | |
2371 | conn->subject_match = os_strdup(subject_match); | |
2372 | if (conn->subject_match == NULL) | |
2373 | return -1; | |
2374 | } | |
2375 | ||
2376 | os_free(conn->altsubject_match); | |
2377 | conn->altsubject_match = NULL; | |
2378 | if (altsubject_match) { | |
2379 | conn->altsubject_match = os_strdup(altsubject_match); | |
2380 | if (conn->altsubject_match == NULL) | |
2381 | return -1; | |
2382 | } | |
2383 | ||
01f809c7 JM |
2384 | os_free(conn->suffix_match); |
2385 | conn->suffix_match = NULL; | |
2386 | if (suffix_match) { | |
2387 | conn->suffix_match = os_strdup(suffix_match); | |
2388 | if (conn->suffix_match == NULL) | |
2389 | return -1; | |
2390 | } | |
2391 | ||
cebee30f JM |
2392 | os_free(conn->domain_match); |
2393 | conn->domain_match = NULL; | |
2394 | if (domain_match) { | |
2395 | conn->domain_match = os_strdup(domain_match); | |
2396 | if (conn->domain_match == NULL) | |
2397 | return -1; | |
2398 | } | |
2399 | ||
6fc6879b JM |
2400 | return 0; |
2401 | } | |
2402 | ||
2403 | ||
1c9663cf JM |
2404 | #ifdef CONFIG_SUITEB |
2405 | #if OPENSSL_VERSION_NUMBER >= 0x10002000L | |
2406 | static int suiteb_cert_cb(SSL *ssl, void *arg) | |
2407 | { | |
2408 | struct tls_connection *conn = arg; | |
2409 | ||
2410 | /* | |
2411 | * This cert_cb() is not really the best location for doing a | |
2412 | * constraint check for the ServerKeyExchange message, but this seems to | |
2413 | * be the only place where the current OpenSSL sequence can be | |
2414 | * terminated cleanly with an TLS alert going out to the server. | |
2415 | */ | |
2416 | ||
2417 | if (!(conn->flags & TLS_CONN_SUITEB)) | |
2418 | return 1; | |
2419 | ||
2420 | /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */ | |
2421 | if (conn->cipher_suite != 0x9f) | |
2422 | return 1; | |
2423 | ||
2424 | if (conn->server_dh_prime_len >= 3072) | |
2425 | return 1; | |
2426 | ||
2427 | wpa_printf(MSG_DEBUG, | |
2428 | "OpenSSL: Server DH prime length (%d bits) not sufficient for Suite B RSA - reject handshake", | |
2429 | conn->server_dh_prime_len); | |
2430 | return 0; | |
2431 | } | |
2432 | #endif /* OPENSSL_VERSION_NUMBER */ | |
2433 | #endif /* CONFIG_SUITEB */ | |
2434 | ||
2435 | ||
2436 | static int tls_set_conn_flags(struct tls_connection *conn, unsigned int flags) | |
93bc6549 | 2437 | { |
1c9663cf JM |
2438 | SSL *ssl = conn->ssl; |
2439 | ||
93bc6549 JM |
2440 | #ifdef SSL_OP_NO_TICKET |
2441 | if (flags & TLS_CONN_DISABLE_SESSION_TICKET) | |
2442 | SSL_set_options(ssl, SSL_OP_NO_TICKET); | |
93bc6549 JM |
2443 | else |
2444 | SSL_clear_options(ssl, SSL_OP_NO_TICKET); | |
93bc6549 JM |
2445 | #endif /* SSL_OP_NO_TICKET */ |
2446 | ||
2447 | #ifdef SSL_OP_NO_TLSv1 | |
2448 | if (flags & TLS_CONN_DISABLE_TLSv1_0) | |
2449 | SSL_set_options(ssl, SSL_OP_NO_TLSv1); | |
2450 | else | |
2451 | SSL_clear_options(ssl, SSL_OP_NO_TLSv1); | |
2452 | #endif /* SSL_OP_NO_TLSv1 */ | |
2453 | #ifdef SSL_OP_NO_TLSv1_1 | |
2454 | if (flags & TLS_CONN_DISABLE_TLSv1_1) | |
2455 | SSL_set_options(ssl, SSL_OP_NO_TLSv1_1); | |
2456 | else | |
2457 | SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1); | |
2458 | #endif /* SSL_OP_NO_TLSv1_1 */ | |
2459 | #ifdef SSL_OP_NO_TLSv1_2 | |
2460 | if (flags & TLS_CONN_DISABLE_TLSv1_2) | |
2461 | SSL_set_options(ssl, SSL_OP_NO_TLSv1_2); | |
2462 | else | |
2463 | SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2); | |
2464 | #endif /* SSL_OP_NO_TLSv1_2 */ | |
60ed2f24 | 2465 | #ifdef CONFIG_SUITEB |
1c9663cf | 2466 | #if OPENSSL_VERSION_NUMBER >= 0x10002000L |
2ed70c75 JM |
2467 | if (flags & TLS_CONN_SUITEB_NO_ECDH) { |
2468 | const char *ciphers = "DHE-RSA-AES256-GCM-SHA384"; | |
2469 | ||
2470 | if (SSL_set_cipher_list(ssl, ciphers) != 1) { | |
2471 | wpa_printf(MSG_INFO, | |
2472 | "OpenSSL: Failed to set Suite B ciphers"); | |
2473 | return -1; | |
2474 | } | |
2475 | } else if (flags & TLS_CONN_SUITEB) { | |
60ed2f24 JM |
2476 | EC_KEY *ecdh; |
2477 | const char *ciphers = | |
2478 | "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384"; | |
2479 | ||
2480 | if (SSL_set_cipher_list(ssl, ciphers) != 1) { | |
2481 | wpa_printf(MSG_INFO, | |
2482 | "OpenSSL: Failed to set Suite B ciphers"); | |
2483 | return -1; | |
2484 | } | |
2485 | ||
2486 | if (SSL_set1_curves_list(ssl, "P-384") != 1) { | |
2487 | wpa_printf(MSG_INFO, | |
2488 | "OpenSSL: Failed to set Suite B curves"); | |
2489 | return -1; | |
2490 | } | |
60ed2f24 JM |
2491 | |
2492 | ecdh = EC_KEY_new_by_curve_name(NID_secp384r1); | |
2493 | if (!ecdh || SSL_set_tmp_ecdh(ssl, ecdh) != 1) { | |
2494 | EC_KEY_free(ecdh); | |
2495 | wpa_printf(MSG_INFO, | |
2496 | "OpenSSL: Failed to set ECDH parameter"); | |
2497 | return -1; | |
2498 | } | |
2499 | EC_KEY_free(ecdh); | |
2ed70c75 JM |
2500 | } |
2501 | if (flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) { | |
b9dc63c2 | 2502 | #ifndef OPENSSL_IS_BORINGSSL |
2ed70c75 JM |
2503 | /* ECDSA+SHA384 if need to add EC support here */ |
2504 | if (SSL_set1_sigalgs_list(ssl, "RSA+SHA384") != 1) { | |
2505 | wpa_printf(MSG_INFO, | |
2506 | "OpenSSL: Failed to set Suite B sigalgs"); | |
2507 | return -1; | |
2508 | } | |
b9dc63c2 | 2509 | #endif /* OPENSSL_IS_BORINGSSL */ |
60ed2f24 JM |
2510 | |
2511 | SSL_set_options(ssl, SSL_OP_NO_TLSv1); | |
2512 | SSL_set_options(ssl, SSL_OP_NO_TLSv1_1); | |
1c9663cf JM |
2513 | SSL_set_cert_cb(ssl, suiteb_cert_cb, conn); |
2514 | } | |
2515 | #else /* OPENSSL_VERSION_NUMBER < 0x10002000L */ | |
2516 | if (flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) { | |
2517 | wpa_printf(MSG_ERROR, | |
2518 | "OpenSSL: Suite B RSA case not supported with this OpenSSL version"); | |
2519 | return -1; | |
60ed2f24 | 2520 | } |
1c9663cf | 2521 | #endif /* OPENSSL_VERSION_NUMBER */ |
60ed2f24 JM |
2522 | #endif /* CONFIG_SUITEB */ |
2523 | ||
2524 | return 0; | |
93bc6549 JM |
2525 | } |
2526 | ||
2527 | ||
6fc6879b | 2528 | int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn, |
bfbebd26 JM |
2529 | int verify_peer, unsigned int flags, |
2530 | const u8 *session_ctx, size_t session_ctx_len) | |
6fc6879b | 2531 | { |
bf206cad | 2532 | static int counter = 0; |
acf36f31 | 2533 | struct tls_data *data = ssl_ctx; |
bf206cad | 2534 | |
6fc6879b JM |
2535 | if (conn == NULL) |
2536 | return -1; | |
2537 | ||
2538 | if (verify_peer) { | |
00468b46 | 2539 | conn->ca_cert_verify = 1; |
6fc6879b JM |
2540 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER | |
2541 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | | |
2542 | SSL_VERIFY_CLIENT_ONCE, tls_verify_cb); | |
2543 | } else { | |
00468b46 | 2544 | conn->ca_cert_verify = 0; |
6fc6879b JM |
2545 | SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL); |
2546 | } | |
2547 | ||
1c9663cf | 2548 | if (tls_set_conn_flags(conn, flags) < 0) |
60ed2f24 | 2549 | return -1; |
93bc6549 JM |
2550 | conn->flags = flags; |
2551 | ||
6fc6879b JM |
2552 | SSL_set_accept_state(conn->ssl); |
2553 | ||
acf36f31 JM |
2554 | if (data->tls_session_lifetime == 0) { |
2555 | /* | |
2556 | * Set session id context to a unique value to make sure | |
2557 | * session resumption cannot be used either through session | |
2558 | * caching or TLS ticket extension. | |
2559 | */ | |
2560 | counter++; | |
2561 | SSL_set_session_id_context(conn->ssl, | |
2562 | (const unsigned char *) &counter, | |
2563 | sizeof(counter)); | |
2564 | } else if (session_ctx) { | |
2565 | SSL_set_session_id_context(conn->ssl, session_ctx, | |
2566 | session_ctx_len); | |
2567 | } | |
bf206cad | 2568 | |
6fc6879b JM |
2569 | return 0; |
2570 | } | |
2571 | ||
2572 | ||
2573 | static int tls_connection_client_cert(struct tls_connection *conn, | |
2574 | const char *client_cert, | |
2575 | const u8 *client_cert_blob, | |
2576 | size_t client_cert_blob_len) | |
2577 | { | |
2578 | if (client_cert == NULL && client_cert_blob == NULL) | |
2579 | return 0; | |
2580 | ||
21cb63ff | 2581 | #ifdef PKCS12_FUNCS |
df426738 | 2582 | #if OPENSSL_VERSION_NUMBER < 0x10002000L || defined(LIBRESSL_VERSION_NUMBER) |
21cb63ff JM |
2583 | /* |
2584 | * Clear previously set extra chain certificates, if any, from PKCS#12 | |
2585 | * processing in tls_parse_pkcs12() to allow OpenSSL to build a new | |
2586 | * chain properly. | |
2587 | */ | |
2588 | SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx); | |
2589 | #endif /* OPENSSL_VERSION_NUMBER < 0x10002000L */ | |
2590 | #endif /* PKCS12_FUNCS */ | |
2591 | ||
6fc6879b JM |
2592 | if (client_cert_blob && |
2593 | SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob, | |
2594 | client_cert_blob_len) == 1) { | |
2595 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> " | |
2596 | "OK"); | |
2597 | return 0; | |
2598 | } else if (client_cert_blob) { | |
2599 | tls_show_errors(MSG_DEBUG, __func__, | |
2600 | "SSL_use_certificate_ASN1 failed"); | |
2601 | } | |
2602 | ||
2603 | if (client_cert == NULL) | |
2604 | return -1; | |
2605 | ||
2a0cd067 DS |
2606 | #ifdef ANDROID |
2607 | if (os_strncmp("keystore://", client_cert, 11) == 0) { | |
2608 | BIO *bio = BIO_from_keystore(&client_cert[11]); | |
2609 | X509 *x509 = NULL; | |
2610 | int ret = -1; | |
2611 | if (bio) { | |
2612 | x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL); | |
2a0cd067 DS |
2613 | } |
2614 | if (x509) { | |
2615 | if (SSL_use_certificate(conn->ssl, x509) == 1) | |
2616 | ret = 0; | |
2617 | X509_free(x509); | |
2618 | } | |
6d08f23f PS |
2619 | |
2620 | /* Read additional certificates into the chain. */ | |
2621 | while (bio) { | |
2622 | x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL); | |
2623 | if (x509) { | |
2624 | /* Takes ownership of x509 */ | |
2625 | SSL_add0_chain_cert(conn->ssl, x509); | |
2626 | } else { | |
2627 | BIO_free(bio); | |
2628 | bio = NULL; | |
2629 | } | |
2630 | } | |
2a0cd067 DS |
2631 | return ret; |
2632 | } | |
2633 | #endif /* ANDROID */ | |
2634 | ||
6fc6879b JM |
2635 | #ifndef OPENSSL_NO_STDIO |
2636 | if (SSL_use_certificate_file(conn->ssl, client_cert, | |
2637 | SSL_FILETYPE_ASN1) == 1) { | |
2638 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)" | |
2639 | " --> OK"); | |
2640 | return 0; | |
6fc6879b JM |
2641 | } |
2642 | ||
2643 | if (SSL_use_certificate_file(conn->ssl, client_cert, | |
2644 | SSL_FILETYPE_PEM) == 1) { | |
effab86f | 2645 | ERR_clear_error(); |
6fc6879b JM |
2646 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)" |
2647 | " --> OK"); | |
2648 | return 0; | |
6fc6879b | 2649 | } |
effab86f JM |
2650 | |
2651 | tls_show_errors(MSG_DEBUG, __func__, | |
2652 | "SSL_use_certificate_file failed"); | |
6fc6879b JM |
2653 | #else /* OPENSSL_NO_STDIO */ |
2654 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__); | |
2655 | #endif /* OPENSSL_NO_STDIO */ | |
2656 | ||
2657 | return -1; | |
2658 | } | |
2659 | ||
2660 | ||
bd9b8b2b JM |
2661 | static int tls_global_client_cert(struct tls_data *data, |
2662 | const char *client_cert) | |
6fc6879b JM |
2663 | { |
2664 | #ifndef OPENSSL_NO_STDIO | |
bd9b8b2b JM |
2665 | SSL_CTX *ssl_ctx = data->ssl; |
2666 | ||
6fc6879b JM |
2667 | if (client_cert == NULL) |
2668 | return 0; | |
2669 | ||
2670 | if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert, | |
2671 | SSL_FILETYPE_ASN1) != 1 && | |
65897747 | 2672 | SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 && |
6fc6879b JM |
2673 | SSL_CTX_use_certificate_file(ssl_ctx, client_cert, |
2674 | SSL_FILETYPE_PEM) != 1) { | |
2675 | tls_show_errors(MSG_INFO, __func__, | |
2676 | "Failed to load client certificate"); | |
2677 | return -1; | |
2678 | } | |
2679 | return 0; | |
2680 | #else /* OPENSSL_NO_STDIO */ | |
2681 | if (client_cert == NULL) | |
2682 | return 0; | |
2683 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__); | |
2684 | return -1; | |
2685 | #endif /* OPENSSL_NO_STDIO */ | |
2686 | } | |
2687 | ||
2688 | ||
2689 | static int tls_passwd_cb(char *buf, int size, int rwflag, void *password) | |
2690 | { | |
2691 | if (password == NULL) { | |
2692 | return 0; | |
2693 | } | |
2694 | os_strlcpy(buf, (char *) password, size); | |
2695 | return os_strlen(buf); | |
2696 | } | |
2697 | ||
2698 | ||
2699 | #ifdef PKCS12_FUNCS | |
bd9b8b2b | 2700 | static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12, |
6fc6879b JM |
2701 | const char *passwd) |
2702 | { | |
2703 | EVP_PKEY *pkey; | |
2704 | X509 *cert; | |
2705 | STACK_OF(X509) *certs; | |
2706 | int res = 0; | |
2707 | char buf[256]; | |
2708 | ||
2709 | pkey = NULL; | |
2710 | cert = NULL; | |
2711 | certs = NULL; | |
c07e7b43 JM |
2712 | if (!passwd) |
2713 | passwd = ""; | |
6fc6879b JM |
2714 | if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) { |
2715 | tls_show_errors(MSG_DEBUG, __func__, | |
2716 | "Failed to parse PKCS12 file"); | |
2717 | PKCS12_free(p12); | |
2718 | return -1; | |
2719 | } | |
2720 | wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data"); | |
2721 | ||
2722 | if (cert) { | |
2723 | X509_NAME_oneline(X509_get_subject_name(cert), buf, | |
2724 | sizeof(buf)); | |
2725 | wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: " | |
2726 | "subject='%s'", buf); | |
2727 | if (ssl) { | |
2728 | if (SSL_use_certificate(ssl, cert) != 1) | |
2729 | res = -1; | |
2730 | } else { | |
bd9b8b2b | 2731 | if (SSL_CTX_use_certificate(data->ssl, cert) != 1) |
6fc6879b JM |
2732 | res = -1; |
2733 | } | |
2734 | X509_free(cert); | |
2735 | } | |
2736 | ||
2737 | if (pkey) { | |
2738 | wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12"); | |
2739 | if (ssl) { | |
2740 | if (SSL_use_PrivateKey(ssl, pkey) != 1) | |
2741 | res = -1; | |
2742 | } else { | |
bd9b8b2b | 2743 | if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1) |
6fc6879b JM |
2744 | res = -1; |
2745 | } | |
2746 | EVP_PKEY_free(pkey); | |
2747 | } | |
2748 | ||
2749 | if (certs) { | |
36e82060 | 2750 | #if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER) |
dda091cf AB |
2751 | if (ssl) |
2752 | SSL_clear_chain_certs(ssl); | |
2753 | else | |
2754 | SSL_CTX_clear_chain_certs(data->ssl); | |
de2a7b79 JM |
2755 | while ((cert = sk_X509_pop(certs)) != NULL) { |
2756 | X509_NAME_oneline(X509_get_subject_name(cert), buf, | |
2757 | sizeof(buf)); | |
2758 | wpa_printf(MSG_DEBUG, "TLS: additional certificate" | |
2759 | " from PKCS12: subject='%s'", buf); | |
dda091cf AB |
2760 | if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) || |
2761 | (!ssl && SSL_CTX_add1_chain_cert(data->ssl, | |
2762 | cert) != 1)) { | |
20f331b7 JM |
2763 | tls_show_errors(MSG_DEBUG, __func__, |
2764 | "Failed to add additional certificate"); | |
de2a7b79 | 2765 | res = -1; |
8bcf8de8 | 2766 | X509_free(cert); |
de2a7b79 JM |
2767 | break; |
2768 | } | |
8bcf8de8 | 2769 | X509_free(cert); |
de2a7b79 | 2770 | } |
20f331b7 JM |
2771 | if (!res) { |
2772 | /* Try to continue anyway */ | |
2773 | } | |
8bcf8de8 | 2774 | sk_X509_pop_free(certs, X509_free); |
226cdea6 | 2775 | #ifndef OPENSSL_IS_BORINGSSL |
dda091cf AB |
2776 | if (ssl) |
2777 | res = SSL_build_cert_chain( | |
2778 | ssl, | |
2779 | SSL_BUILD_CHAIN_FLAG_CHECK | | |
2780 | SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR); | |
2781 | else | |
2782 | res = SSL_CTX_build_cert_chain( | |
2783 | data->ssl, | |
2784 | SSL_BUILD_CHAIN_FLAG_CHECK | | |
2785 | SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR); | |
de2a7b79 JM |
2786 | if (!res) { |
2787 | tls_show_errors(MSG_DEBUG, __func__, | |
2788 | "Failed to build certificate chain"); | |
2789 | } else if (res == 2) { | |
2790 | wpa_printf(MSG_DEBUG, | |
2791 | "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates"); | |
2792 | } | |
226cdea6 | 2793 | #endif /* OPENSSL_IS_BORINGSSL */ |
de2a7b79 JM |
2794 | /* |
2795 | * Try to continue regardless of result since it is possible for | |
2796 | * the extra certificates not to be required. | |
2797 | */ | |
2798 | res = 0; | |
2799 | #else /* OPENSSL_VERSION_NUMBER >= 0x10002000L */ | |
bd9b8b2b | 2800 | SSL_CTX_clear_extra_chain_certs(data->ssl); |
6fc6879b JM |
2801 | while ((cert = sk_X509_pop(certs)) != NULL) { |
2802 | X509_NAME_oneline(X509_get_subject_name(cert), buf, | |
2803 | sizeof(buf)); | |
2804 | wpa_printf(MSG_DEBUG, "TLS: additional certificate" | |
2805 | " from PKCS12: subject='%s'", buf); | |
2806 | /* | |
2807 | * There is no SSL equivalent for the chain cert - so | |
2808 | * always add it to the context... | |
2809 | */ | |
bd9b8b2b JM |
2810 | if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1) |
2811 | { | |
8bcf8de8 | 2812 | X509_free(cert); |
6fc6879b JM |
2813 | res = -1; |
2814 | break; | |
2815 | } | |
2816 | } | |
8bcf8de8 | 2817 | sk_X509_pop_free(certs, X509_free); |
de2a7b79 | 2818 | #endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */ |
6fc6879b JM |
2819 | } |
2820 | ||
2821 | PKCS12_free(p12); | |
2822 | ||
2823 | if (res < 0) | |
bd9b8b2b | 2824 | tls_get_errors(data); |
6fc6879b JM |
2825 | |
2826 | return res; | |
2827 | } | |
2828 | #endif /* PKCS12_FUNCS */ | |
2829 | ||
2830 | ||
bd9b8b2b JM |
2831 | static int tls_read_pkcs12(struct tls_data *data, SSL *ssl, |
2832 | const char *private_key, const char *passwd) | |
6fc6879b JM |
2833 | { |
2834 | #ifdef PKCS12_FUNCS | |
2835 | FILE *f; | |
2836 | PKCS12 *p12; | |
2837 | ||
2838 | f = fopen(private_key, "rb"); | |
2839 | if (f == NULL) | |
2840 | return -1; | |
2841 | ||
2842 | p12 = d2i_PKCS12_fp(f, NULL); | |
2843 | fclose(f); | |
2844 | ||
2845 | if (p12 == NULL) { | |
2846 | tls_show_errors(MSG_INFO, __func__, | |
2847 | "Failed to use PKCS#12 file"); | |
2848 | return -1; | |
2849 | } | |
2850 | ||
bd9b8b2b | 2851 | return tls_parse_pkcs12(data, ssl, p12, passwd); |
6fc6879b JM |
2852 | |
2853 | #else /* PKCS12_FUNCS */ | |
2854 | wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read " | |
2855 | "p12/pfx files"); | |
2856 | return -1; | |
2857 | #endif /* PKCS12_FUNCS */ | |
2858 | } | |
2859 | ||
2860 | ||
bd9b8b2b | 2861 | static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl, |
6fc6879b JM |
2862 | const u8 *blob, size_t len, const char *passwd) |
2863 | { | |
2864 | #ifdef PKCS12_FUNCS | |
2865 | PKCS12 *p12; | |
2866 | ||
fee31f76 | 2867 | p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len); |
6fc6879b JM |
2868 | if (p12 == NULL) { |
2869 | tls_show_errors(MSG_INFO, __func__, | |
2870 | "Failed to use PKCS#12 blob"); | |
2871 | return -1; | |
2872 | } | |
2873 | ||
bd9b8b2b | 2874 | return tls_parse_pkcs12(data, ssl, p12, passwd); |
6fc6879b JM |
2875 | |
2876 | #else /* PKCS12_FUNCS */ | |
2877 | wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse " | |
2878 | "p12/pfx blobs"); | |
2879 | return -1; | |
2880 | #endif /* PKCS12_FUNCS */ | |
2881 | } | |
2882 | ||
2883 | ||
e572cb63 | 2884 | #ifndef OPENSSL_NO_ENGINE |
e59c91af DS |
2885 | static int tls_engine_get_cert(struct tls_connection *conn, |
2886 | const char *cert_id, | |
2887 | X509 **cert) | |
2888 | { | |
e59c91af DS |
2889 | /* this runs after the private key is loaded so no PIN is required */ |
2890 | struct { | |
2891 | const char *cert_id; | |
2892 | X509 *cert; | |
2893 | } params; | |
2894 | params.cert_id = cert_id; | |
2895 | params.cert = NULL; | |
2896 | ||
2897 | if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL", | |
2898 | 0, ¶ms, NULL, 1)) { | |
fd4fb281 MG |
2899 | unsigned long err = ERR_get_error(); |
2900 | ||
e59c91af DS |
2901 | wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id" |
2902 | " '%s' [%s]", cert_id, | |
fd4fb281 MG |
2903 | ERR_error_string(err, NULL)); |
2904 | if (tls_is_pin_error(err)) | |
2905 | return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN; | |
e59c91af DS |
2906 | return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; |
2907 | } | |
2908 | if (!params.cert) { | |
2909 | wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id" | |
2910 | " '%s'", cert_id); | |
2911 | return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; | |
2912 | } | |
2913 | *cert = params.cert; | |
2914 | return 0; | |
e59c91af | 2915 | } |
e572cb63 | 2916 | #endif /* OPENSSL_NO_ENGINE */ |
e59c91af DS |
2917 | |
2918 | ||
2919 | static int tls_connection_engine_client_cert(struct tls_connection *conn, | |
2920 | const char *cert_id) | |
2921 | { | |
2922 | #ifndef OPENSSL_NO_ENGINE | |
2923 | X509 *cert; | |
2924 | ||
2925 | if (tls_engine_get_cert(conn, cert_id, &cert)) | |
2926 | return -1; | |
2927 | ||
2928 | if (!SSL_use_certificate(conn->ssl, cert)) { | |
2929 | tls_show_errors(MSG_ERROR, __func__, | |
2930 | "SSL_use_certificate failed"); | |
2931 | X509_free(cert); | |
2932 | return -1; | |
2933 | } | |
2934 | X509_free(cert); | |
2935 | wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> " | |
2936 | "OK"); | |
2937 | return 0; | |
2938 | ||
2939 | #else /* OPENSSL_NO_ENGINE */ | |
2940 | return -1; | |
2941 | #endif /* OPENSSL_NO_ENGINE */ | |
2942 | } | |
2943 | ||
2944 | ||
bd9b8b2b | 2945 | static int tls_connection_engine_ca_cert(struct tls_data *data, |
e59c91af DS |
2946 | struct tls_connection *conn, |
2947 | const char *ca_cert_id) | |
2948 | { | |
2949 | #ifndef OPENSSL_NO_ENGINE | |
2950 | X509 *cert; | |
bd9b8b2b | 2951 | SSL_CTX *ssl_ctx = data->ssl; |
68ae4773 | 2952 | X509_STORE *store; |
e59c91af DS |
2953 | |
2954 | if (tls_engine_get_cert(conn, ca_cert_id, &cert)) | |
2955 | return -1; | |
2956 | ||
2957 | /* start off the same as tls_connection_ca_cert */ | |
68ae4773 JM |
2958 | store = X509_STORE_new(); |
2959 | if (store == NULL) { | |
e59c91af DS |
2960 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new " |
2961 | "certificate store", __func__); | |
2962 | X509_free(cert); | |
2963 | return -1; | |
2964 | } | |
68ae4773 JM |
2965 | SSL_CTX_set_cert_store(ssl_ctx, store); |
2966 | if (!X509_STORE_add_cert(store, cert)) { | |
e59c91af DS |
2967 | unsigned long err = ERR_peek_error(); |
2968 | tls_show_errors(MSG_WARNING, __func__, | |
2969 | "Failed to add CA certificate from engine " | |
2970 | "to certificate store"); | |
2971 | if (ERR_GET_LIB(err) == ERR_LIB_X509 && | |
2972 | ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) { | |
2973 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert" | |
2974 | " already in hash table error", | |
2975 | __func__); | |
2976 | } else { | |
2977 | X509_free(cert); | |
2978 | return -1; | |
2979 | } | |
2980 | } | |
2981 | X509_free(cert); | |
2982 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine " | |
2983 | "to certificate store", __func__); | |
2984 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb); | |
d8858cad CW |
2985 | conn->ca_cert_verify = 1; |
2986 | ||
e59c91af DS |
2987 | return 0; |
2988 | ||
2989 | #else /* OPENSSL_NO_ENGINE */ | |
2990 | return -1; | |
2991 | #endif /* OPENSSL_NO_ENGINE */ | |
2992 | } | |
2993 | ||
2994 | ||
6fc6879b JM |
2995 | static int tls_connection_engine_private_key(struct tls_connection *conn) |
2996 | { | |
aeeb0bca | 2997 | #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE) |
6fc6879b JM |
2998 | if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) { |
2999 | tls_show_errors(MSG_ERROR, __func__, | |
3000 | "ENGINE: cannot use private key for TLS"); | |
3001 | return -1; | |
3002 | } | |
3003 | if (!SSL_check_private_key(conn->ssl)) { | |
3004 | tls_show_errors(MSG_INFO, __func__, | |
3005 | "Private key failed verification"); | |
3006 | return -1; | |
3007 | } | |
3008 | return 0; | |
3009 | #else /* OPENSSL_NO_ENGINE */ | |
3010 | wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but " | |
3011 | "engine support was not compiled in"); | |
3012 | return -1; | |
3013 | #endif /* OPENSSL_NO_ENGINE */ | |
3014 | } | |
3015 | ||
3016 | ||
89971d8b JM |
3017 | static void tls_clear_default_passwd_cb(SSL_CTX *ssl_ctx, SSL *ssl) |
3018 | { | |
7721fe08 JM |
3019 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L |
3020 | #ifndef LIBRESSL_VERSION_NUMBER | |
3021 | #ifndef OPENSSL_IS_BORINGSSL | |
89971d8b JM |
3022 | if (ssl) { |
3023 | SSL_set_default_passwd_cb(ssl, NULL); | |
3024 | SSL_set_default_passwd_cb_userdata(ssl, NULL); | |
3025 | } | |
7721fe08 JM |
3026 | #endif /* !BoringSSL */ |
3027 | #endif /* !LibreSSL */ | |
3028 | #endif /* >= 1.1.0f */ | |
89971d8b JM |
3029 | SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL); |
3030 | SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, NULL); | |
3031 | } | |
3032 | ||
3033 | ||
bd9b8b2b | 3034 | static int tls_connection_private_key(struct tls_data *data, |
6fc6879b JM |
3035 | struct tls_connection *conn, |
3036 | const char *private_key, | |
3037 | const char *private_key_passwd, | |
3038 | const u8 *private_key_blob, | |
3039 | size_t private_key_blob_len) | |
3040 | { | |
bd9b8b2b | 3041 | SSL_CTX *ssl_ctx = data->ssl; |
6fc6879b JM |
3042 | int ok; |
3043 | ||
3044 | if (private_key == NULL && private_key_blob == NULL) | |
3045 | return 0; | |
3046 | ||
7721fe08 JM |
3047 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L |
3048 | #ifndef LIBRESSL_VERSION_NUMBER | |
3049 | #ifndef OPENSSL_IS_BORINGSSL | |
f665c93e BG |
3050 | /* |
3051 | * In OpenSSL >= 1.1.0f SSL_use_PrivateKey_file() uses the callback | |
3052 | * from the SSL object. See OpenSSL commit d61461a75253. | |
3053 | */ | |
3054 | SSL_set_default_passwd_cb(conn->ssl, tls_passwd_cb); | |
149143e3 DB |
3055 | SSL_set_default_passwd_cb_userdata(conn->ssl, |
3056 | (void *) private_key_passwd); | |
7721fe08 JM |
3057 | #endif /* !BoringSSL */ |
3058 | #endif /* !LibreSSL */ | |
3059 | #endif /* >= 1.1.0f && */ | |
f665c93e | 3060 | /* Keep these for OpenSSL < 1.1.0f */ |
6fc6879b | 3061 | SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb); |
149143e3 DB |
3062 | SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, |
3063 | (void *) private_key_passwd); | |
6fc6879b JM |
3064 | |
3065 | ok = 0; | |
3066 | while (private_key_blob) { | |
3067 | if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl, | |
3068 | (u8 *) private_key_blob, | |
3069 | private_key_blob_len) == 1) { | |
3070 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_" | |
3071 | "ASN1(EVP_PKEY_RSA) --> OK"); | |
3072 | ok = 1; | |
3073 | break; | |
6fc6879b JM |
3074 | } |
3075 | ||
3076 | if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl, | |
3077 | (u8 *) private_key_blob, | |
3078 | private_key_blob_len) == 1) { | |
3079 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_" | |
3080 | "ASN1(EVP_PKEY_DSA) --> OK"); | |
3081 | ok = 1; | |
3082 | break; | |
6fc6879b JM |
3083 | } |
3084 | ||
3085 | if (SSL_use_RSAPrivateKey_ASN1(conn->ssl, | |
3086 | (u8 *) private_key_blob, | |
3087 | private_key_blob_len) == 1) { | |
3088 | wpa_printf(MSG_DEBUG, "OpenSSL: " | |
3089 | "SSL_use_RSAPrivateKey_ASN1 --> OK"); | |
3090 | ok = 1; | |
3091 | break; | |
6fc6879b JM |
3092 | } |
3093 | ||
bd9b8b2b | 3094 | if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob, |
149143e3 DB |
3095 | private_key_blob_len, |
3096 | private_key_passwd) == 0) { | |
6fc6879b JM |
3097 | wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> " |
3098 | "OK"); | |
3099 | ok = 1; | |
3100 | break; | |
3101 | } | |
3102 | ||
3103 | break; | |
3104 | } | |
3105 | ||
3106 | while (!ok && private_key) { | |
3107 | #ifndef OPENSSL_NO_STDIO | |
3108 | if (SSL_use_PrivateKey_file(conn->ssl, private_key, | |
3109 | SSL_FILETYPE_ASN1) == 1) { | |
3110 | wpa_printf(MSG_DEBUG, "OpenSSL: " | |
3111 | "SSL_use_PrivateKey_File (DER) --> OK"); | |
3112 | ok = 1; | |
3113 | break; | |
6fc6879b JM |
3114 | } |
3115 | ||
3116 | if (SSL_use_PrivateKey_file(conn->ssl, private_key, | |
3117 | SSL_FILETYPE_PEM) == 1) { | |
3118 | wpa_printf(MSG_DEBUG, "OpenSSL: " | |
3119 | "SSL_use_PrivateKey_File (PEM) --> OK"); | |
3120 | ok = 1; | |
3121 | break; | |
6fc6879b JM |
3122 | } |
3123 | #else /* OPENSSL_NO_STDIO */ | |
3124 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", | |
3125 | __func__); | |
3126 | #endif /* OPENSSL_NO_STDIO */ | |
3127 | ||
149143e3 DB |
3128 | if (tls_read_pkcs12(data, conn->ssl, private_key, |
3129 | private_key_passwd) == 0) { | |
6fc6879b JM |
3130 | wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file " |
3131 | "--> OK"); | |
3132 | ok = 1; | |
3133 | break; | |
3134 | } | |
3135 | ||
3136 | if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) { | |
3137 | wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to " | |
3138 | "access certificate store --> OK"); | |
3139 | ok = 1; | |
3140 | break; | |
3141 | } | |
3142 | ||
3143 | break; | |
3144 | } | |
3145 | ||
3146 | if (!ok) { | |
effab86f JM |
3147 | tls_show_errors(MSG_INFO, __func__, |
3148 | "Failed to load private key"); | |
89971d8b | 3149 | tls_clear_default_passwd_cb(ssl_ctx, conn->ssl); |
6fc6879b JM |
3150 | return -1; |
3151 | } | |
3152 | ERR_clear_error(); | |
89971d8b | 3153 | tls_clear_default_passwd_cb(ssl_ctx, conn->ssl); |
55651a4b | 3154 | |
6fc6879b JM |
3155 | if (!SSL_check_private_key(conn->ssl)) { |
3156 | tls_show_errors(MSG_INFO, __func__, "Private key failed " | |
3157 | "verification"); | |
3158 | return -1; | |
3159 | } | |
3160 | ||
3161 | wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully"); | |
3162 | return 0; | |
3163 | } | |
3164 | ||
3165 | ||
bd9b8b2b JM |
3166 | static int tls_global_private_key(struct tls_data *data, |
3167 | const char *private_key, | |
6fc6879b JM |
3168 | const char *private_key_passwd) |
3169 | { | |
bd9b8b2b | 3170 | SSL_CTX *ssl_ctx = data->ssl; |
6fc6879b JM |
3171 | |
3172 | if (private_key == NULL) | |
3173 | return 0; | |
3174 | ||
6fc6879b | 3175 | SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb); |
149143e3 DB |
3176 | SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, |
3177 | (void *) private_key_passwd); | |
6fc6879b JM |
3178 | if ( |
3179 | #ifndef OPENSSL_NO_STDIO | |
3180 | SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key, | |
3181 | SSL_FILETYPE_ASN1) != 1 && | |
3182 | SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key, | |
3183 | SSL_FILETYPE_PEM) != 1 && | |
3184 | #endif /* OPENSSL_NO_STDIO */ | |
149143e3 | 3185 | tls_read_pkcs12(data, NULL, private_key, private_key_passwd)) { |
6fc6879b JM |
3186 | tls_show_errors(MSG_INFO, __func__, |
3187 | "Failed to load private key"); | |
89971d8b | 3188 | tls_clear_default_passwd_cb(ssl_ctx, NULL); |
6fc6879b JM |
3189 | ERR_clear_error(); |
3190 | return -1; | |
3191 | } | |
89971d8b | 3192 | tls_clear_default_passwd_cb(ssl_ctx, NULL); |
6fc6879b | 3193 | ERR_clear_error(); |
55651a4b | 3194 | |
6fc6879b JM |
3195 | if (!SSL_CTX_check_private_key(ssl_ctx)) { |
3196 | tls_show_errors(MSG_INFO, __func__, | |
3197 | "Private key failed verification"); | |
3198 | return -1; | |
3199 | } | |
3200 | ||
3201 | return 0; | |
3202 | } | |
3203 | ||
3204 | ||
3205 | static int tls_connection_dh(struct tls_connection *conn, const char *dh_file) | |
3206 | { | |
3207 | #ifdef OPENSSL_NO_DH | |
3208 | if (dh_file == NULL) | |
3209 | return 0; | |
3210 | wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but " | |
3211 | "dh_file specified"); | |
3212 | return -1; | |
3213 | #else /* OPENSSL_NO_DH */ | |
3214 | DH *dh; | |
3215 | BIO *bio; | |
3216 | ||
3217 | /* TODO: add support for dh_blob */ | |
3218 | if (dh_file == NULL) | |
3219 | return 0; | |
3220 | if (conn == NULL) | |
3221 | return -1; | |
3222 | ||
3223 | bio = BIO_new_file(dh_file, "r"); | |
3224 | if (bio == NULL) { | |
3225 | wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s", | |
3226 | dh_file, ERR_error_string(ERR_get_error(), NULL)); | |
3227 | return -1; | |
3228 | } | |
3229 | dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); | |
3230 | BIO_free(bio); | |
3231 | #ifndef OPENSSL_NO_DSA | |
3232 | while (dh == NULL) { | |
3233 | DSA *dsa; | |
3234 | wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -" | |
3235 | " trying to parse as DSA params", dh_file, | |
3236 | ERR_error_string(ERR_get_error(), NULL)); | |
3237 | bio = BIO_new_file(dh_file, "r"); | |
3238 | if (bio == NULL) | |
3239 | break; | |
3240 | dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL); | |
3241 | BIO_free(bio); | |
3242 | if (!dsa) { | |
3243 | wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file " | |
3244 | "'%s': %s", dh_file, | |
3245 | ERR_error_string(ERR_get_error(), NULL)); | |
3246 | break; | |
3247 | } | |
3248 | ||
3249 | wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format"); | |
3250 | dh = DSA_dup_DH(dsa); | |
3251 | DSA_free(dsa); | |
3252 | if (dh == NULL) { | |
3253 | wpa_printf(MSG_INFO, "TLS: Failed to convert DSA " | |
3254 | "params into DH params"); | |
3255 | break; | |
3256 | } | |
3257 | break; | |
3258 | } | |
3259 | #endif /* !OPENSSL_NO_DSA */ | |
3260 | if (dh == NULL) { | |
3261 | wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file " | |
3262 | "'%s'", dh_file); | |
3263 | return -1; | |
3264 | } | |
3265 | ||
3266 | if (SSL_set_tmp_dh(conn->ssl, dh) != 1) { | |
3267 | wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': " | |
3268 | "%s", dh_file, | |
3269 | ERR_error_string(ERR_get_error(), NULL)); | |
3270 | DH_free(dh); | |
3271 | return -1; | |
3272 | } | |
3273 | DH_free(dh); | |
3274 | return 0; | |
3275 | #endif /* OPENSSL_NO_DH */ | |
3276 | } | |
3277 | ||
3278 | ||
bd9b8b2b | 3279 | static int tls_global_dh(struct tls_data *data, const char *dh_file) |
6fc6879b JM |
3280 | { |
3281 | #ifdef OPENSSL_NO_DH | |
3282 | if (dh_file == NULL) | |
3283 | return 0; | |
3284 | wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but " | |
3285 | "dh_file specified"); | |
3286 | return -1; | |
3287 | #else /* OPENSSL_NO_DH */ | |
bd9b8b2b | 3288 | SSL_CTX *ssl_ctx = data->ssl; |
6fc6879b JM |
3289 | DH *dh; |
3290 | BIO *bio; | |
3291 | ||
3292 | /* TODO: add support for dh_blob */ | |
3293 | if (dh_file == NULL) | |
3294 | return 0; | |
3295 | if (ssl_ctx == NULL) | |
3296 | return -1; | |
3297 | ||
3298 | bio = BIO_new_file(dh_file, "r"); | |
3299 | if (bio == NULL) { | |
3300 | wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s", | |
3301 | dh_file, ERR_error_string(ERR_get_error(), NULL)); | |
3302 | return -1; | |
3303 | } | |
3304 | dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); | |
3305 | BIO_free(bio); | |
3306 | #ifndef OPENSSL_NO_DSA | |
3307 | while (dh == NULL) { | |
3308 | DSA *dsa; | |
3309 | wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -" | |
3310 | " trying to parse as DSA params", dh_file, | |
3311 | ERR_error_string(ERR_get_error(), NULL)); | |
3312 | bio = BIO_new_file(dh_file, "r"); | |
3313 | if (bio == NULL) | |
3314 | break; | |
3315 | dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL); | |
3316 | BIO_free(bio); | |
3317 | if (!dsa) { | |
3318 | wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file " | |
3319 | "'%s': %s", dh_file, | |
3320 | ERR_error_string(ERR_get_error(), NULL)); | |
3321 | break; | |
3322 | } | |
3323 | ||
3324 | wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format"); | |
3325 | dh = DSA_dup_DH(dsa); | |
3326 | DSA_free(dsa); | |
3327 | if (dh == NULL) { | |
3328 | wpa_printf(MSG_INFO, "TLS: Failed to convert DSA " | |
3329 | "params into DH params"); | |
3330 | break; | |
3331 | } | |
3332 | break; | |
3333 | } | |
3334 | #endif /* !OPENSSL_NO_DSA */ | |
3335 | if (dh == NULL) { | |
3336 | wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file " | |
3337 | "'%s'", dh_file); | |
3338 | return -1; | |
3339 | } | |
3340 | ||
3341 | if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) { | |
3342 | wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': " | |
3343 | "%s", dh_file, | |
3344 | ERR_error_string(ERR_get_error(), NULL)); | |
3345 | DH_free(dh); | |
3346 | return -1; | |
3347 | } | |
3348 | DH_free(dh); | |
3349 | return 0; | |
3350 | #endif /* OPENSSL_NO_DH */ | |
3351 | } | |
3352 | ||
3353 | ||
1046db8b JM |
3354 | int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn, |
3355 | struct tls_random *keys) | |
6fc6879b JM |
3356 | { |
3357 | SSL *ssl; | |
3358 | ||
3359 | if (conn == NULL || keys == NULL) | |
3360 | return -1; | |
3361 | ssl = conn->ssl; | |
005c5dcf JM |
3362 | if (ssl == NULL) |
3363 | return -1; | |
3364 | ||
3365 | os_memset(keys, 0, sizeof(*keys)); | |
3366 | keys->client_random = conn->client_random; | |
3367 | keys->client_random_len = SSL_get_client_random( | |
3368 | ssl, conn->client_random, sizeof(conn->client_random)); | |
3369 | keys->server_random = conn->server_random; | |
3370 | keys->server_random_len = SSL_get_server_random( | |
3371 | ssl, conn->server_random, sizeof(conn->server_random)); | |
6fc6879b JM |
3372 | |
3373 | return 0; | |
3374 | } | |
3375 | ||
3376 | ||
1cece2fa | 3377 | #ifdef OPENSSL_NEED_EAP_FAST_PRF |
af851914 JM |
3378 | static int openssl_get_keyblock_size(SSL *ssl) |
3379 | { | |
36e82060 | 3380 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
af851914 JM |
3381 | const EVP_CIPHER *c; |
3382 | const EVP_MD *h; | |
3383 | int md_size; | |
3384 | ||
3385 | if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL || | |
3386 | ssl->read_hash == NULL) | |
3387 | return -1; | |
3388 | ||
3389 | c = ssl->enc_read_ctx->cipher; | |
af851914 | 3390 | h = EVP_MD_CTX_md(ssl->read_hash); |
af851914 JM |
3391 | if (h) |
3392 | md_size = EVP_MD_size(h); | |
af851914 JM |
3393 | else if (ssl->s3) |
3394 | md_size = ssl->s3->tmp.new_mac_secret_size; | |
af851914 JM |
3395 | else |
3396 | return -1; | |
3397 | ||
3398 | wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d " | |
3399 | "IV_len=%d", EVP_CIPHER_key_length(c), md_size, | |
3400 | EVP_CIPHER_iv_length(c)); | |
3401 | return 2 * (EVP_CIPHER_key_length(c) + | |
3402 | md_size + | |
3403 | EVP_CIPHER_iv_length(c)); | |
3de28506 JM |
3404 | #else |
3405 | const SSL_CIPHER *ssl_cipher; | |
3406 | int cipher, digest; | |
3407 | const EVP_CIPHER *c; | |
3408 | const EVP_MD *h; | |
3409 | ||
3410 | ssl_cipher = SSL_get_current_cipher(ssl); | |
3411 | if (!ssl_cipher) | |
3412 | return -1; | |
3413 | cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher); | |
3414 | digest = SSL_CIPHER_get_digest_nid(ssl_cipher); | |
3415 | wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d", | |
3416 | cipher, digest); | |
3417 | if (cipher < 0 || digest < 0) | |
3418 | return -1; | |
3419 | c = EVP_get_cipherbynid(cipher); | |
3420 | h = EVP_get_digestbynid(digest); | |
3421 | if (!c || !h) | |
3422 | return -1; | |
3423 | ||
3424 | wpa_printf(MSG_DEBUG, | |
3425 | "OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d", | |
3426 | EVP_CIPHER_key_length(c), EVP_MD_size(h), | |
3427 | EVP_CIPHER_iv_length(c)); | |
3428 | return 2 * (EVP_CIPHER_key_length(c) + EVP_MD_size(h) + | |
3429 | EVP_CIPHER_iv_length(c)); | |
3430 | #endif | |
af851914 | 3431 | } |
1cece2fa | 3432 | #endif /* OPENSSL_NEED_EAP_FAST_PRF */ |
af851914 JM |
3433 | |
3434 | ||
73581707 DB |
3435 | int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn, |
3436 | const char *label, u8 *out, size_t out_len) | |
3437 | { | |
3438 | if (!conn || | |
3439 | SSL_export_keying_material(conn->ssl, out, out_len, label, | |
3440 | os_strlen(label), NULL, 0, 0) != 1) | |
3441 | return -1; | |
3442 | return 0; | |
3443 | } | |
3444 | ||
3445 | ||
3446 | int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn, | |
3447 | u8 *out, size_t out_len) | |
fa0e7151 | 3448 | { |
1cece2fa | 3449 | #ifdef OPENSSL_NEED_EAP_FAST_PRF |
3de28506 JM |
3450 | SSL *ssl; |
3451 | SSL_SESSION *sess; | |
3452 | u8 *rnd; | |
3453 | int ret = -1; | |
3454 | int skip = 0; | |
3455 | u8 *tmp_out = NULL; | |
3456 | u8 *_out = out; | |
3457 | unsigned char client_random[SSL3_RANDOM_SIZE]; | |
3458 | unsigned char server_random[SSL3_RANDOM_SIZE]; | |
3459 | unsigned char master_key[64]; | |
3460 | size_t master_key_len; | |
16bc3b89 | 3461 | const char *ver; |
3de28506 JM |
3462 | |
3463 | /* | |
73581707 DB |
3464 | * TLS library did not support EAP-FAST key generation, so get the |
3465 | * needed TLS session parameters and use an internal implementation of | |
3466 | * TLS PRF to derive the key. | |
3de28506 JM |
3467 | */ |
3468 | ||
3469 | if (conn == NULL) | |
3470 | return -1; | |
3471 | ssl = conn->ssl; | |
3472 | if (ssl == NULL) | |
3473 | return -1; | |
16bc3b89 | 3474 | ver = SSL_get_version(ssl); |
3de28506 | 3475 | sess = SSL_get_session(ssl); |
16bc3b89 | 3476 | if (!ver || !sess) |
3de28506 JM |
3477 | return -1; |
3478 | ||
73581707 DB |
3479 | skip = openssl_get_keyblock_size(ssl); |
3480 | if (skip < 0) | |
3481 | return -1; | |
3482 | tmp_out = os_malloc(skip + out_len); | |
3483 | if (!tmp_out) | |
3484 | return -1; | |
3485 | _out = tmp_out; | |
3de28506 JM |
3486 | |
3487 | rnd = os_malloc(2 * SSL3_RANDOM_SIZE); | |
3488 | if (!rnd) { | |
3489 | os_free(tmp_out); | |
3490 | return -1; | |
3491 | } | |
3492 | ||
3493 | SSL_get_client_random(ssl, client_random, sizeof(client_random)); | |
3494 | SSL_get_server_random(ssl, server_random, sizeof(server_random)); | |
3495 | master_key_len = SSL_SESSION_get_master_key(sess, master_key, | |
3496 | sizeof(master_key)); | |
3497 | ||
73581707 DB |
3498 | os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE); |
3499 | os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE); | |
3de28506 | 3500 | |
16bc3b89 JM |
3501 | if (os_strcmp(ver, "TLSv1.2") == 0) { |
3502 | tls_prf_sha256(master_key, master_key_len, | |
73581707 | 3503 | "key expansion", rnd, 2 * SSL3_RANDOM_SIZE, |
16bc3b89 | 3504 | _out, skip + out_len); |
3de28506 | 3505 | ret = 0; |
16bc3b89 | 3506 | } else if (tls_prf_sha1_md5(master_key, master_key_len, |
73581707 | 3507 | "key expansion", rnd, 2 * SSL3_RANDOM_SIZE, |
16bc3b89 JM |
3508 | _out, skip + out_len) == 0) { |
3509 | ret = 0; | |
3510 | } | |
3de28506 JM |
3511 | os_memset(master_key, 0, sizeof(master_key)); |
3512 | os_free(rnd); | |
73581707 | 3513 | if (ret == 0) |
3de28506 JM |
3514 | os_memcpy(out, _out + skip, out_len); |
3515 | bin_clear_free(tmp_out, skip); | |
3516 | ||
3517 | return ret; | |
1cece2fa DB |
3518 | #else /* OPENSSL_NEED_EAP_FAST_PRF */ |
3519 | wpa_printf(MSG_ERROR, | |
3520 | "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode"); | |
3521 | return -1; | |
3522 | #endif /* OPENSSL_NEED_EAP_FAST_PRF */ | |
fa0e7151 JM |
3523 | } |
3524 | ||
3525 | ||
81c85c06 JM |
3526 | static struct wpabuf * |
3527 | openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data, | |
3528 | int server) | |
6fc6879b JM |
3529 | { |
3530 | int res; | |
81c85c06 | 3531 | struct wpabuf *out_data; |
6fc6879b JM |
3532 | |
3533 | /* | |
3534 | * Give TLS handshake data from the server (if available) to OpenSSL | |
3535 | * for processing. | |
3536 | */ | |
e9690eb7 | 3537 | if (in_data && wpabuf_len(in_data) > 0 && |
81c85c06 JM |
3538 | BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data)) |
3539 | < 0) { | |
6fc6879b JM |
3540 | tls_show_errors(MSG_INFO, __func__, |
3541 | "Handshake failed - BIO_write"); | |
3542 | return NULL; | |
3543 | } | |
3544 | ||
3545 | /* Initiate TLS handshake or continue the existing handshake */ | |
81c85c06 JM |
3546 | if (server) |
3547 | res = SSL_accept(conn->ssl); | |
3548 | else | |
3549 | res = SSL_connect(conn->ssl); | |
6fc6879b JM |
3550 | if (res != 1) { |
3551 | int err = SSL_get_error(conn->ssl, res); | |
3552 | if (err == SSL_ERROR_WANT_READ) | |
3553 | wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want " | |
3554 | "more data"); | |
3555 | else if (err == SSL_ERROR_WANT_WRITE) | |
3556 | wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to " | |
3557 | "write"); | |
3558 | else { | |
3559 | tls_show_errors(MSG_INFO, __func__, "SSL_connect"); | |
3560 | conn->failed++; | |
3561 | } | |
3562 | } | |
3563 | ||
1c9663cf JM |
3564 | #ifdef CONFIG_SUITEB |
3565 | if ((conn->flags & TLS_CONN_SUITEB) && !server && | |
3566 | os_strncmp(SSL_get_cipher(conn->ssl), "DHE-", 4) == 0 && | |
3567 | conn->server_dh_prime_len < 3072) { | |
3568 | struct tls_context *context = conn->context; | |
3569 | ||
3570 | /* | |
3571 | * This should not be reached since earlier cert_cb should have | |
3572 | * terminated the handshake. Keep this check here for extra | |
3573 | * protection if anything goes wrong with the more low-level | |
3574 | * checks based on having to parse the TLS handshake messages. | |
3575 | */ | |
3576 | wpa_printf(MSG_DEBUG, | |
3577 | "OpenSSL: Server DH prime length: %d bits", | |
3578 | conn->server_dh_prime_len); | |
3579 | ||
3580 | if (context->event_cb) { | |
3581 | union tls_event_data ev; | |
3582 | ||
3583 | os_memset(&ev, 0, sizeof(ev)); | |
3584 | ev.alert.is_local = 1; | |
3585 | ev.alert.type = "fatal"; | |
3586 | ev.alert.description = "insufficient security"; | |
3587 | context->event_cb(context->cb_ctx, TLS_ALERT, &ev); | |
3588 | } | |
3589 | /* | |
3590 | * Could send a TLS Alert to the server, but for now, simply | |
3591 | * terminate handshake. | |
3592 | */ | |
3593 | conn->failed++; | |
3594 | conn->write_alerts++; | |
3595 | return NULL; | |
3596 | } | |
3597 | #endif /* CONFIG_SUITEB */ | |
3598 | ||
6fc6879b JM |
3599 | /* Get the TLS handshake data to be sent to the server */ |
3600 | res = BIO_ctrl_pending(conn->ssl_out); | |
3601 | wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res); | |
81c85c06 | 3602 | out_data = wpabuf_alloc(res); |
6fc6879b JM |
3603 | if (out_data == NULL) { |
3604 | wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for " | |
3605 | "handshake output (%d bytes)", res); | |
3606 | if (BIO_reset(conn->ssl_out) < 0) { | |
3607 | tls_show_errors(MSG_INFO, __func__, | |
3608 | "BIO_reset failed"); | |
3609 | } | |
6fc6879b JM |
3610 | return NULL; |
3611 | } | |
81c85c06 JM |
3612 | res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data), |
3613 | res); | |
6fc6879b JM |
3614 | if (res < 0) { |
3615 | tls_show_errors(MSG_INFO, __func__, | |
3616 | "Handshake failed - BIO_read"); | |
3617 | if (BIO_reset(conn->ssl_out) < 0) { | |
3618 | tls_show_errors(MSG_INFO, __func__, | |
3619 | "BIO_reset failed"); | |
3620 | } | |
81c85c06 | 3621 | wpabuf_free(out_data); |
6fc6879b JM |
3622 | return NULL; |
3623 | } | |
81c85c06 | 3624 | wpabuf_put(out_data, res); |
6fc6879b JM |
3625 | |
3626 | return out_data; | |
3627 | } | |
3628 | ||
3629 | ||
81c85c06 JM |
3630 | static struct wpabuf * |
3631 | openssl_get_appl_data(struct tls_connection *conn, size_t max_len) | |
6fc6879b | 3632 | { |
81c85c06 | 3633 | struct wpabuf *appl_data; |
6fc6879b | 3634 | int res; |
6fc6879b | 3635 | |
81c85c06 JM |
3636 | appl_data = wpabuf_alloc(max_len + 100); |
3637 | if (appl_data == NULL) | |
6fc6879b | 3638 | return NULL; |
6fc6879b | 3639 | |
81c85c06 JM |
3640 | res = SSL_read(conn->ssl, wpabuf_mhead(appl_data), |
3641 | wpabuf_size(appl_data)); | |
3642 | if (res < 0) { | |
bf206cad | 3643 | int err = SSL_get_error(conn->ssl, res); |
81c85c06 JM |
3644 | if (err == SSL_ERROR_WANT_READ || |
3645 | err == SSL_ERROR_WANT_WRITE) { | |
3646 | wpa_printf(MSG_DEBUG, "SSL: No Application Data " | |
3647 | "included"); | |
3648 | } else { | |
6fc6879b | 3649 | tls_show_errors(MSG_INFO, __func__, |
81c85c06 JM |
3650 | "Failed to read possible " |
3651 | "Application Data"); | |
6fc6879b | 3652 | } |
81c85c06 | 3653 | wpabuf_free(appl_data); |
6fc6879b JM |
3654 | return NULL; |
3655 | } | |
81c85c06 JM |
3656 | |
3657 | wpabuf_put(appl_data, res); | |
3658 | wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished " | |
3659 | "message", appl_data); | |
3660 | ||
3661 | return appl_data; | |
3662 | } | |
3663 | ||
3664 | ||
3665 | static struct wpabuf * | |
3666 | openssl_connection_handshake(struct tls_connection *conn, | |
3667 | const struct wpabuf *in_data, | |
3668 | struct wpabuf **appl_data, int server) | |
3669 | { | |
3670 | struct wpabuf *out_data; | |
3671 | ||
3672 | if (appl_data) | |
3673 | *appl_data = NULL; | |
3674 | ||
3675 | out_data = openssl_handshake(conn, in_data, server); | |
3676 | if (out_data == NULL) | |
6fc6879b | 3677 | return NULL; |
bb52293e JM |
3678 | if (conn->invalid_hb_used) { |
3679 | wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response"); | |
3680 | wpabuf_free(out_data); | |
3681 | return NULL; | |
3682 | } | |
81c85c06 | 3683 | |
a89beee5 JM |
3684 | if (SSL_is_init_finished(conn->ssl)) { |
3685 | wpa_printf(MSG_DEBUG, | |
3686 | "OpenSSL: Handshake finished - resumed=%d", | |
3687 | tls_connection_resumed(conn->ssl_ctx, conn)); | |
3688 | if (appl_data && in_data) | |
3689 | *appl_data = openssl_get_appl_data(conn, | |
3690 | wpabuf_len(in_data)); | |
3691 | } | |
81c85c06 | 3692 | |
bb52293e JM |
3693 | if (conn->invalid_hb_used) { |
3694 | wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response"); | |
3695 | if (appl_data) { | |
3696 | wpabuf_free(*appl_data); | |
3697 | *appl_data = NULL; | |
3698 | } | |
3699 | wpabuf_free(out_data); | |
3700 | return NULL; | |
3701 | } | |
3702 | ||
6fc6879b JM |
3703 | return out_data; |
3704 | } | |
3705 | ||
3706 | ||
81c85c06 JM |
3707 | struct wpabuf * |
3708 | tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn, | |
3709 | const struct wpabuf *in_data, | |
3710 | struct wpabuf **appl_data) | |
3711 | { | |
3712 | return openssl_connection_handshake(conn, in_data, appl_data, 0); | |
3713 | } | |
3714 | ||
3715 | ||
3716 | struct wpabuf * tls_connection_server_handshake(void *tls_ctx, | |
3717 | struct tls_connection *conn, | |
3718 | const struct wpabuf *in_data, | |
3719 | struct wpabuf **appl_data) | |
3720 | { | |
3721 | return openssl_connection_handshake(conn, in_data, appl_data, 1); | |
3722 | } | |
3723 | ||
3724 | ||
3725 | struct wpabuf * tls_connection_encrypt(void *tls_ctx, | |
3726 | struct tls_connection *conn, | |
3727 | const struct wpabuf *in_data) | |
6fc6879b JM |
3728 | { |
3729 | int res; | |
81c85c06 | 3730 | struct wpabuf *buf; |
6fc6879b JM |
3731 | |
3732 | if (conn == NULL) | |
81c85c06 | 3733 | return NULL; |
6fc6879b JM |
3734 | |
3735 | /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */ | |
3736 | if ((res = BIO_reset(conn->ssl_in)) < 0 || | |
3737 | (res = BIO_reset(conn->ssl_out)) < 0) { | |
3738 | tls_show_errors(MSG_INFO, __func__, "BIO_reset failed"); | |
81c85c06 | 3739 | return NULL; |
6fc6879b | 3740 | } |
81c85c06 | 3741 | res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data)); |
6fc6879b JM |
3742 | if (res < 0) { |
3743 | tls_show_errors(MSG_INFO, __func__, | |
3744 | "Encryption failed - SSL_write"); | |
81c85c06 | 3745 | return NULL; |
6fc6879b JM |
3746 | } |
3747 | ||
3748 | /* Read encrypted data to be sent to the server */ | |
81c85c06 JM |
3749 | buf = wpabuf_alloc(wpabuf_len(in_data) + 300); |
3750 | if (buf == NULL) | |
3751 | return NULL; | |
3752 | res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf)); | |
6fc6879b JM |
3753 | if (res < 0) { |
3754 | tls_show_errors(MSG_INFO, __func__, | |
3755 | "Encryption failed - BIO_read"); | |
81c85c06 JM |
3756 | wpabuf_free(buf); |
3757 | return NULL; | |
6fc6879b | 3758 | } |
81c85c06 | 3759 | wpabuf_put(buf, res); |
6fc6879b | 3760 | |
81c85c06 | 3761 | return buf; |
6fc6879b JM |
3762 | } |
3763 | ||
3764 | ||
81c85c06 JM |
3765 | struct wpabuf * tls_connection_decrypt(void *tls_ctx, |
3766 | struct tls_connection *conn, | |
3767 | const struct wpabuf *in_data) | |
6fc6879b JM |
3768 | { |
3769 | int res; | |
81c85c06 | 3770 | struct wpabuf *buf; |
6fc6879b JM |
3771 | |
3772 | /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */ | |
81c85c06 JM |
3773 | res = BIO_write(conn->ssl_in, wpabuf_head(in_data), |
3774 | wpabuf_len(in_data)); | |
6fc6879b JM |
3775 | if (res < 0) { |
3776 | tls_show_errors(MSG_INFO, __func__, | |
3777 | "Decryption failed - BIO_write"); | |
81c85c06 | 3778 | return NULL; |
6fc6879b JM |
3779 | } |
3780 | if (BIO_reset(conn->ssl_out) < 0) { | |
3781 | tls_show_errors(MSG_INFO, __func__, "BIO_reset failed"); | |
81c85c06 | 3782 | return NULL; |
6fc6879b JM |
3783 | } |
3784 | ||
3785 | /* Read decrypted data for further processing */ | |
81c85c06 JM |
3786 | /* |
3787 | * Even though we try to disable TLS compression, it is possible that | |
3788 | * this cannot be done with all TLS libraries. Add extra buffer space | |
3789 | * to handle the possibility of the decrypted data being longer than | |
3790 | * input data. | |
3791 | */ | |
3792 | buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3); | |
3793 | if (buf == NULL) | |
3794 | return NULL; | |
3795 | res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf)); | |
6fc6879b JM |
3796 | if (res < 0) { |
3797 | tls_show_errors(MSG_INFO, __func__, | |
3798 | "Decryption failed - SSL_read"); | |
a86a7316 | 3799 | wpabuf_free(buf); |
81c85c06 | 3800 | return NULL; |
6fc6879b | 3801 | } |
81c85c06 | 3802 | wpabuf_put(buf, res); |
6fc6879b | 3803 | |
bb52293e JM |
3804 | if (conn->invalid_hb_used) { |
3805 | wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response"); | |
3806 | wpabuf_free(buf); | |
3807 | return NULL; | |
3808 | } | |
3809 | ||
81c85c06 | 3810 | return buf; |
6fc6879b JM |
3811 | } |
3812 | ||
3813 | ||
3814 | int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn) | |
3815 | { | |
db3168d4 | 3816 | return conn ? SSL_cache_hit(conn->ssl) : 0; |
6fc6879b JM |
3817 | } |
3818 | ||
3819 | ||
3820 | int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn, | |
3821 | u8 *ciphers) | |
3822 | { | |
750f5d99 | 3823 | char buf[500], *pos, *end; |
6fc6879b JM |
3824 | u8 *c; |
3825 | int ret; | |
3826 | ||
3827 | if (conn == NULL || conn->ssl == NULL || ciphers == NULL) | |
3828 | return -1; | |
3829 | ||
3830 | buf[0] = '\0'; | |
3831 | pos = buf; | |
3832 | end = pos + sizeof(buf); | |
3833 | ||
3834 | c = ciphers; | |
3835 | while (*c != TLS_CIPHER_NONE) { | |
3836 | const char *suite; | |
3837 | ||
3838 | switch (*c) { | |
3839 | case TLS_CIPHER_RC4_SHA: | |
3840 | suite = "RC4-SHA"; | |
3841 | break; | |
3842 | case TLS_CIPHER_AES128_SHA: | |
3843 | suite = "AES128-SHA"; | |
3844 | break; | |
3845 | case TLS_CIPHER_RSA_DHE_AES128_SHA: | |
3846 | suite = "DHE-RSA-AES128-SHA"; | |
3847 | break; | |
3848 | case TLS_CIPHER_ANON_DH_AES128_SHA: | |
3849 | suite = "ADH-AES128-SHA"; | |
3850 | break; | |
750f5d99 JM |
3851 | case TLS_CIPHER_RSA_DHE_AES256_SHA: |
3852 | suite = "DHE-RSA-AES256-SHA"; | |
3853 | break; | |
3854 | case TLS_CIPHER_AES256_SHA: | |
3855 | suite = "AES256-SHA"; | |
3856 | break; | |
6fc6879b JM |
3857 | default: |
3858 | wpa_printf(MSG_DEBUG, "TLS: Unsupported " | |
3859 | "cipher selection: %d", *c); | |
3860 | return -1; | |
3861 | } | |
3862 | ret = os_snprintf(pos, end - pos, ":%s", suite); | |
d85e1fc8 | 3863 | if (os_snprintf_error(end - pos, ret)) |
6fc6879b JM |
3864 | break; |
3865 | pos += ret; | |
3866 | ||
3867 | c++; | |
3868 | } | |
3869 | ||
3870 | wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1); | |
3871 | ||
36e82060 | 3872 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER) |
c34cd668 JM |
3873 | #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) |
3874 | if (os_strstr(buf, ":ADH-")) { | |
3875 | /* | |
3876 | * Need to drop to security level 0 to allow anonymous | |
3877 | * cipher suites for EAP-FAST. | |
3878 | */ | |
3879 | SSL_set_security_level(conn->ssl, 0); | |
3880 | } else if (SSL_get_security_level(conn->ssl) == 0) { | |
3881 | /* Force at least security level 1 */ | |
3882 | SSL_set_security_level(conn->ssl, 1); | |
3883 | } | |
3884 | #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ | |
3885 | #endif | |
3886 | ||
6fc6879b JM |
3887 | if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) { |
3888 | tls_show_errors(MSG_INFO, __func__, | |
3889 | "Cipher suite configuration failed"); | |
3890 | return -1; | |
3891 | } | |
3892 | ||
3893 | return 0; | |
3894 | } | |
3895 | ||
3896 | ||
fe1bf329 JM |
3897 | int tls_get_version(void *ssl_ctx, struct tls_connection *conn, |
3898 | char *buf, size_t buflen) | |
3899 | { | |
3900 | const char *name; | |
3901 | if (conn == NULL || conn->ssl == NULL) | |
3902 | return -1; | |
3903 | ||
3904 | name = SSL_get_version(conn->ssl); | |
3905 | if (name == NULL) | |
3906 | return -1; | |
3907 | ||
3908 | os_strlcpy(buf, name, buflen); | |
3909 | return 0; | |
3910 | } | |
3911 | ||
3912 | ||
6fc6879b JM |
3913 | int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn, |
3914 | char *buf, size_t buflen) | |
3915 | { | |
3916 | const char *name; | |
3917 | if (conn == NULL || conn->ssl == NULL) | |
3918 | return -1; | |
3919 | ||
3920 | name = SSL_get_cipher(conn->ssl); | |
3921 | if (name == NULL) | |
3922 | return -1; | |
3923 | ||
3924 | os_strlcpy(buf, name, buflen); | |
3925 | return 0; | |
3926 | } | |
3927 | ||
3928 | ||
3929 | int tls_connection_enable_workaround(void *ssl_ctx, | |
3930 | struct tls_connection *conn) | |
3931 | { | |
3932 | SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); | |
3933 | ||
3934 | return 0; | |
3935 | } | |
3936 | ||
3937 | ||
1e5839e0 | 3938 | #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) |
6fc6879b JM |
3939 | /* ClientHello TLS extensions require a patch to openssl, so this function is |
3940 | * commented out unless explicitly needed for EAP-FAST in order to be able to | |
3941 | * build this file with unmodified openssl. */ | |
3942 | int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn, | |
3943 | int ext_type, const u8 *data, | |
3944 | size_t data_len) | |
3945 | { | |
0cf03892 | 3946 | if (conn == NULL || conn->ssl == NULL || ext_type != 35) |
6fc6879b JM |
3947 | return -1; |
3948 | ||
0cf03892 JM |
3949 | if (SSL_set_session_ticket_ext(conn->ssl, (void *) data, |
3950 | data_len) != 1) | |
3951 | return -1; | |
6fc6879b JM |
3952 | |
3953 | return 0; | |
3954 | } | |
1e5839e0 | 3955 | #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
6fc6879b JM |
3956 | |
3957 | ||
3958 | int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn) | |
3959 | { | |
3960 | if (conn == NULL) | |
3961 | return -1; | |
3962 | return conn->failed; | |
3963 | } | |
3964 | ||
3965 | ||
3966 | int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn) | |
3967 | { | |
3968 | if (conn == NULL) | |
3969 | return -1; | |
3970 | return conn->read_alerts; | |
3971 | } | |
3972 | ||
3973 | ||
3974 | int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn) | |
3975 | { | |
3976 | if (conn == NULL) | |
3977 | return -1; | |
3978 | return conn->write_alerts; | |
3979 | } | |
3980 | ||
3981 | ||
080585c0 JM |
3982 | #ifdef HAVE_OCSP |
3983 | ||
3984 | static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp) | |
3985 | { | |
3986 | #ifndef CONFIG_NO_STDOUT_DEBUG | |
080585c0 JM |
3987 | BIO *out; |
3988 | size_t rlen; | |
3989 | char *txt; | |
3990 | int res; | |
3991 | ||
3992 | if (wpa_debug_level > MSG_DEBUG) | |
3993 | return; | |
3994 | ||
3995 | out = BIO_new(BIO_s_mem()); | |
3996 | if (!out) | |
3997 | return; | |
3998 | ||
3999 | OCSP_RESPONSE_print(out, rsp, 0); | |
4000 | rlen = BIO_ctrl_pending(out); | |
4001 | txt = os_malloc(rlen + 1); | |
4002 | if (!txt) { | |
4003 | BIO_free(out); | |
4004 | return; | |
4005 | } | |
4006 | ||
4007 | res = BIO_read(out, txt, rlen); | |
4008 | if (res > 0) { | |
4009 | txt[res] = '\0'; | |
4010 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt); | |
4011 | } | |
4012 | os_free(txt); | |
4013 | BIO_free(out); | |
4014 | #endif /* CONFIG_NO_STDOUT_DEBUG */ | |
4015 | } | |
4016 | ||
4017 | ||
4eb3b76b JM |
4018 | static void debug_print_cert(X509 *cert, const char *title) |
4019 | { | |
4020 | #ifndef CONFIG_NO_STDOUT_DEBUG | |
4021 | BIO *out; | |
4022 | size_t rlen; | |
4023 | char *txt; | |
4024 | int res; | |
4025 | ||
4026 | if (wpa_debug_level > MSG_DEBUG) | |
4027 | return; | |
4028 | ||
4029 | out = BIO_new(BIO_s_mem()); | |
4030 | if (!out) | |
4031 | return; | |
4032 | ||
4033 | X509_print(out, cert); | |
4034 | rlen = BIO_ctrl_pending(out); | |
4035 | txt = os_malloc(rlen + 1); | |
4036 | if (!txt) { | |
4037 | BIO_free(out); | |
4038 | return; | |
4039 | } | |
4040 | ||
4041 | res = BIO_read(out, txt, rlen); | |
4042 | if (res > 0) { | |
4043 | txt[res] = '\0'; | |
4044 | wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt); | |
4045 | } | |
4046 | os_free(txt); | |
4047 | ||
4048 | BIO_free(out); | |
4049 | #endif /* CONFIG_NO_STDOUT_DEBUG */ | |
4050 | } | |
4051 | ||
4052 | ||
080585c0 JM |
4053 | static int ocsp_resp_cb(SSL *s, void *arg) |
4054 | { | |
4055 | struct tls_connection *conn = arg; | |
4056 | const unsigned char *p; | |
85cff4b0 | 4057 | int len, status, reason, res; |
080585c0 JM |
4058 | OCSP_RESPONSE *rsp; |
4059 | OCSP_BASICRESP *basic; | |
4060 | OCSP_CERTID *id; | |
4061 | ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update; | |
6bf61fb2 JM |
4062 | X509_STORE *store; |
4063 | STACK_OF(X509) *certs = NULL; | |
080585c0 JM |
4064 | |
4065 | len = SSL_get_tlsext_status_ocsp_resp(s, &p); | |
4066 | if (!p) { | |
4067 | wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received"); | |
4068 | return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1; | |
4069 | } | |
4070 | ||
4071 | wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len); | |
4072 | ||
4073 | rsp = d2i_OCSP_RESPONSE(NULL, &p, len); | |
4074 | if (!rsp) { | |
4075 | wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response"); | |
4076 | return 0; | |
4077 | } | |
4078 | ||
4079 | ocsp_debug_print_resp(rsp); | |
4080 | ||
4081 | status = OCSP_response_status(rsp); | |
4082 | if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) { | |
4083 | wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)", | |
4084 | status, OCSP_response_status_str(status)); | |
4085 | return 0; | |
4086 | } | |
4087 | ||
4088 | basic = OCSP_response_get1_basic(rsp); | |
4089 | if (!basic) { | |
4090 | wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse"); | |
4091 | return 0; | |
4092 | } | |
4093 | ||
68ae4773 | 4094 | store = SSL_CTX_get_cert_store(conn->ssl_ctx); |
6bf61fb2 | 4095 | if (conn->peer_issuer) { |
4eb3b76b | 4096 | debug_print_cert(conn->peer_issuer, "Add OCSP issuer"); |
6bf61fb2 JM |
4097 | |
4098 | if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) { | |
4099 | tls_show_errors(MSG_INFO, __func__, | |
58d405fc | 4100 | "OpenSSL: Could not add issuer to certificate store"); |
6bf61fb2 JM |
4101 | } |
4102 | certs = sk_X509_new_null(); | |
4103 | if (certs) { | |
4104 | X509 *cert; | |
4105 | cert = X509_dup(conn->peer_issuer); | |
4106 | if (cert && !sk_X509_push(certs, cert)) { | |
4107 | tls_show_errors( | |
4108 | MSG_INFO, __func__, | |
58d405fc | 4109 | "OpenSSL: Could not add issuer to OCSP responder trust store"); |
6bf61fb2 JM |
4110 | X509_free(cert); |
4111 | sk_X509_free(certs); | |
4112 | certs = NULL; | |
4113 | } | |
710dfb4e | 4114 | if (certs && conn->peer_issuer_issuer) { |
6bf61fb2 JM |
4115 | cert = X509_dup(conn->peer_issuer_issuer); |
4116 | if (cert && !sk_X509_push(certs, cert)) { | |
4117 | tls_show_errors( | |
4118 | MSG_INFO, __func__, | |
58d405fc | 4119 | "OpenSSL: Could not add issuer's issuer to OCSP responder trust store"); |
6bf61fb2 JM |
4120 | X509_free(cert); |
4121 | } | |
4122 | } | |
4123 | } | |
4124 | } | |
4125 | ||
4126 | status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER); | |
4127 | sk_X509_pop_free(certs, X509_free); | |
080585c0 JM |
4128 | if (status <= 0) { |
4129 | tls_show_errors(MSG_INFO, __func__, | |
4130 | "OpenSSL: OCSP response failed verification"); | |
4131 | OCSP_BASICRESP_free(basic); | |
4132 | OCSP_RESPONSE_free(rsp); | |
4133 | return 0; | |
4134 | } | |
4135 | ||
4136 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded"); | |
4137 | ||
762c92a4 JM |
4138 | if (!conn->peer_cert) { |
4139 | wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check"); | |
4140 | OCSP_BASICRESP_free(basic); | |
4141 | OCSP_RESPONSE_free(rsp); | |
4142 | return 0; | |
4143 | } | |
4144 | ||
4145 | if (!conn->peer_issuer) { | |
4146 | wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check"); | |
080585c0 JM |
4147 | OCSP_BASICRESP_free(basic); |
4148 | OCSP_RESPONSE_free(rsp); | |
4149 | return 0; | |
4150 | } | |
4151 | ||
85cff4b0 | 4152 | id = OCSP_cert_to_id(EVP_sha256(), conn->peer_cert, conn->peer_issuer); |
080585c0 | 4153 | if (!id) { |
85cff4b0 JM |
4154 | wpa_printf(MSG_DEBUG, |
4155 | "OpenSSL: Could not create OCSP certificate identifier (SHA256)"); | |
080585c0 JM |
4156 | OCSP_BASICRESP_free(basic); |
4157 | OCSP_RESPONSE_free(rsp); | |
4158 | return 0; | |
4159 | } | |
4160 | ||
85cff4b0 JM |
4161 | res = OCSP_resp_find_status(basic, id, &status, &reason, &produced_at, |
4162 | &this_update, &next_update); | |
4163 | if (!res) { | |
4164 | id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer); | |
4165 | if (!id) { | |
4166 | wpa_printf(MSG_DEBUG, | |
4167 | "OpenSSL: Could not create OCSP certificate identifier (SHA1)"); | |
4168 | OCSP_BASICRESP_free(basic); | |
4169 | OCSP_RESPONSE_free(rsp); | |
4170 | return 0; | |
4171 | } | |
4172 | ||
4173 | res = OCSP_resp_find_status(basic, id, &status, &reason, | |
4174 | &produced_at, &this_update, | |
4175 | &next_update); | |
4176 | } | |
4177 | ||
4178 | if (!res) { | |
080585c0 JM |
4179 | wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s", |
4180 | (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" : | |
4181 | " (OCSP not required)"); | |
d9a0f697 | 4182 | OCSP_CERTID_free(id); |
080585c0 JM |
4183 | OCSP_BASICRESP_free(basic); |
4184 | OCSP_RESPONSE_free(rsp); | |
4185 | return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1; | |
4186 | } | |
d9a0f697 | 4187 | OCSP_CERTID_free(id); |
080585c0 JM |
4188 | |
4189 | if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) { | |
4190 | tls_show_errors(MSG_INFO, __func__, | |
4191 | "OpenSSL: OCSP status times invalid"); | |
4192 | OCSP_BASICRESP_free(basic); | |
4193 | OCSP_RESPONSE_free(rsp); | |
4194 | return 0; | |
4195 | } | |
4196 | ||
4197 | OCSP_BASICRESP_free(basic); | |
4198 | OCSP_RESPONSE_free(rsp); | |
4199 | ||
4200 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s", | |
4201 | OCSP_cert_status_str(status)); | |
4202 | ||
4203 | if (status == V_OCSP_CERTSTATUS_GOOD) | |
4204 | return 1; | |
4205 | if (status == V_OCSP_CERTSTATUS_REVOKED) | |
4206 | return 0; | |
4207 | if (conn->flags & TLS_CONN_REQUIRE_OCSP) { | |
4208 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required"); | |
4209 | return 0; | |
4210 | } | |
be7963b3 | 4211 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue"); |
080585c0 JM |
4212 | return 1; |
4213 | } | |
4214 | ||
4215 | ||
4216 | static int ocsp_status_cb(SSL *s, void *arg) | |
4217 | { | |
4218 | char *tmp; | |
4219 | char *resp; | |
4220 | size_t len; | |
4221 | ||
4222 | if (tls_global->ocsp_stapling_response == NULL) { | |
4223 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured"); | |
4224 | return SSL_TLSEXT_ERR_OK; | |
4225 | } | |
4226 | ||
4227 | resp = os_readfile(tls_global->ocsp_stapling_response, &len); | |
4228 | if (resp == NULL) { | |
4229 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file"); | |
4230 | /* TODO: Build OCSPResponse with responseStatus = internalError | |
4231 | */ | |
4232 | return SSL_TLSEXT_ERR_OK; | |
4233 | } | |
4234 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response"); | |
4235 | tmp = OPENSSL_malloc(len); | |
4236 | if (tmp == NULL) { | |
4237 | os_free(resp); | |
4238 | return SSL_TLSEXT_ERR_ALERT_FATAL; | |
4239 | } | |
4240 | ||
4241 | os_memcpy(tmp, resp, len); | |
4242 | os_free(resp); | |
4243 | SSL_set_tlsext_status_ocsp_resp(s, tmp, len); | |
4244 | ||
4245 | return SSL_TLSEXT_ERR_OK; | |
4246 | } | |
4247 | ||
4248 | #endif /* HAVE_OCSP */ | |
4249 | ||
4250 | ||
6fc6879b JM |
4251 | int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn, |
4252 | const struct tls_connection_params *params) | |
4253 | { | |
bd9b8b2b | 4254 | struct tls_data *data = tls_ctx; |
6fc6879b JM |
4255 | int ret; |
4256 | unsigned long err; | |
96955192 | 4257 | int can_pkcs11 = 0; |
01b0d1d5 DW |
4258 | const char *key_id = params->key_id; |
4259 | const char *cert_id = params->cert_id; | |
4260 | const char *ca_cert_id = params->ca_cert_id; | |
96955192 | 4261 | const char *engine_id = params->engine ? params->engine_id : NULL; |
6fc6879b JM |
4262 | |
4263 | if (conn == NULL) | |
4264 | return -1; | |
4265 | ||
d6b536f7 JM |
4266 | if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) { |
4267 | wpa_printf(MSG_INFO, | |
4268 | "OpenSSL: ocsp=3 not supported"); | |
4269 | return -1; | |
4270 | } | |
4271 | ||
01b0d1d5 | 4272 | /* |
96955192 DW |
4273 | * If the engine isn't explicitly configured, and any of the |
4274 | * cert/key fields are actually PKCS#11 URIs, then automatically | |
4275 | * use the PKCS#11 ENGINE. | |
01b0d1d5 | 4276 | */ |
96955192 DW |
4277 | if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0) |
4278 | can_pkcs11 = 1; | |
4279 | ||
4280 | if (!key_id && params->private_key && can_pkcs11 && | |
4281 | os_strncmp(params->private_key, "pkcs11:", 7) == 0) { | |
4282 | can_pkcs11 = 2; | |
01b0d1d5 | 4283 | key_id = params->private_key; |
96955192 | 4284 | } |
01b0d1d5 | 4285 | |
96955192 DW |
4286 | if (!cert_id && params->client_cert && can_pkcs11 && |
4287 | os_strncmp(params->client_cert, "pkcs11:", 7) == 0) { | |
4288 | can_pkcs11 = 2; | |
01b0d1d5 | 4289 | cert_id = params->client_cert; |
96955192 | 4290 | } |
01b0d1d5 | 4291 | |
96955192 DW |
4292 | if (!ca_cert_id && params->ca_cert && can_pkcs11 && |
4293 | os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) { | |
4294 | can_pkcs11 = 2; | |
01b0d1d5 | 4295 | ca_cert_id = params->ca_cert; |
96955192 DW |
4296 | } |
4297 | ||
4298 | /* If we need to automatically enable the PKCS#11 ENGINE, do so. */ | |
4299 | if (can_pkcs11 == 2 && !engine_id) | |
4300 | engine_id = "pkcs11"; | |
01b0d1d5 | 4301 | |
0f56057c | 4302 | #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) |
b70d508c | 4303 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
6a31a31d JM |
4304 | if (params->flags & TLS_CONN_EAP_FAST) { |
4305 | wpa_printf(MSG_DEBUG, | |
4306 | "OpenSSL: Use TLSv1_method() for EAP-FAST"); | |
4307 | if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) { | |
4308 | tls_show_errors(MSG_INFO, __func__, | |
4309 | "Failed to set TLSv1_method() for EAP-FAST"); | |
4310 | return -1; | |
4311 | } | |
4312 | } | |
06836013 | 4313 | #endif |
0f56057c | 4314 | #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
d4913c58 | 4315 | |
6fc6879b JM |
4316 | while ((err = ERR_get_error())) { |
4317 | wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s", | |
4318 | __func__, ERR_error_string(err, NULL)); | |
4319 | } | |
4320 | ||
96955192 | 4321 | if (engine_id) { |
e59c91af | 4322 | wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine"); |
96955192 | 4323 | ret = tls_engine_init(conn, engine_id, params->pin, |
01b0d1d5 | 4324 | key_id, cert_id, ca_cert_id); |
e59c91af DS |
4325 | if (ret) |
4326 | return ret; | |
4327 | } | |
6fc6879b JM |
4328 | if (tls_connection_set_subject_match(conn, |
4329 | params->subject_match, | |
01f809c7 | 4330 | params->altsubject_match, |
cebee30f JM |
4331 | params->suffix_match, |
4332 | params->domain_match)) | |
6fc6879b | 4333 | return -1; |
e59c91af | 4334 | |
96955192 | 4335 | if (engine_id && ca_cert_id) { |
bd9b8b2b | 4336 | if (tls_connection_engine_ca_cert(data, conn, ca_cert_id)) |
e59c91af | 4337 | return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED; |
bd9b8b2b | 4338 | } else if (tls_connection_ca_cert(data, conn, params->ca_cert, |
e59c91af DS |
4339 | params->ca_cert_blob, |
4340 | params->ca_cert_blob_len, | |
4341 | params->ca_path)) | |
6fc6879b | 4342 | return -1; |
e59c91af | 4343 | |
96955192 | 4344 | if (engine_id && cert_id) { |
01b0d1d5 | 4345 | if (tls_connection_engine_client_cert(conn, cert_id)) |
e59c91af DS |
4346 | return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED; |
4347 | } else if (tls_connection_client_cert(conn, params->client_cert, | |
4348 | params->client_cert_blob, | |
4349 | params->client_cert_blob_len)) | |
6fc6879b JM |
4350 | return -1; |
4351 | ||
96955192 | 4352 | if (engine_id && key_id) { |
e59c91af | 4353 | wpa_printf(MSG_DEBUG, "TLS: Using private key from engine"); |
6fc6879b JM |
4354 | if (tls_connection_engine_private_key(conn)) |
4355 | return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED; | |
bd9b8b2b | 4356 | } else if (tls_connection_private_key(data, conn, |
6fc6879b JM |
4357 | params->private_key, |
4358 | params->private_key_passwd, | |
4359 | params->private_key_blob, | |
4360 | params->private_key_blob_len)) { | |
4361 | wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'", | |
4362 | params->private_key); | |
4363 | return -1; | |
4364 | } | |
4365 | ||
4366 | if (tls_connection_dh(conn, params->dh_file)) { | |
4367 | wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'", | |
4368 | params->dh_file); | |
4369 | return -1; | |
4370 | } | |
4371 | ||
b7328434 JM |
4372 | if (params->openssl_ciphers && |
4373 | SSL_set_cipher_list(conn->ssl, params->openssl_ciphers) != 1) { | |
4374 | wpa_printf(MSG_INFO, | |
4375 | "OpenSSL: Failed to set cipher string '%s'", | |
4376 | params->openssl_ciphers); | |
4377 | return -1; | |
4378 | } | |
4379 | ||
1c9663cf | 4380 | if (tls_set_conn_flags(conn, params->flags) < 0) |
60ed2f24 | 4381 | return -1; |
e9a6f183 | 4382 | |
bdee6ca0 JM |
4383 | #ifdef OPENSSL_IS_BORINGSSL |
4384 | if (params->flags & TLS_CONN_REQUEST_OCSP) { | |
4385 | SSL_enable_ocsp_stapling(conn->ssl); | |
4386 | } | |
4387 | #else /* OPENSSL_IS_BORINGSSL */ | |
080585c0 JM |
4388 | #ifdef HAVE_OCSP |
4389 | if (params->flags & TLS_CONN_REQUEST_OCSP) { | |
bd9b8b2b | 4390 | SSL_CTX *ssl_ctx = data->ssl; |
080585c0 JM |
4391 | SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp); |
4392 | SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb); | |
4393 | SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn); | |
4394 | } | |
355a5c8e JM |
4395 | #else /* HAVE_OCSP */ |
4396 | if (params->flags & TLS_CONN_REQUIRE_OCSP) { | |
4397 | wpa_printf(MSG_INFO, | |
4398 | "OpenSSL: No OCSP support included - reject configuration"); | |
4399 | return -1; | |
4400 | } | |
4401 | if (params->flags & TLS_CONN_REQUEST_OCSP) { | |
4402 | wpa_printf(MSG_DEBUG, | |
4403 | "OpenSSL: No OCSP support included - allow optional OCSP case to continue"); | |
4404 | } | |
080585c0 | 4405 | #endif /* HAVE_OCSP */ |
bdee6ca0 | 4406 | #endif /* OPENSSL_IS_BORINGSSL */ |
080585c0 | 4407 | |
235279e7 JM |
4408 | conn->flags = params->flags; |
4409 | ||
bd9b8b2b | 4410 | tls_get_errors(data); |
6fc6879b JM |
4411 | |
4412 | return 0; | |
4413 | } | |
4414 | ||
4415 | ||
4416 | int tls_global_set_params(void *tls_ctx, | |
4417 | const struct tls_connection_params *params) | |
4418 | { | |
bd9b8b2b JM |
4419 | struct tls_data *data = tls_ctx; |
4420 | SSL_CTX *ssl_ctx = data->ssl; | |
6fc6879b JM |
4421 | unsigned long err; |
4422 | ||
4423 | while ((err = ERR_get_error())) { | |
4424 | wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s", | |
4425 | __func__, ERR_error_string(err, NULL)); | |
4426 | } | |
4427 | ||
bd9b8b2b JM |
4428 | if (tls_global_ca_cert(data, params->ca_cert) || |
4429 | tls_global_client_cert(data, params->client_cert) || | |
4430 | tls_global_private_key(data, params->private_key, | |
f24b9797 | 4431 | params->private_key_passwd) || |
bd9b8b2b | 4432 | tls_global_dh(data, params->dh_file)) { |
f24b9797 | 4433 | wpa_printf(MSG_INFO, "TLS: Failed to set global parameters"); |
6fc6879b JM |
4434 | return -1; |
4435 | } | |
4436 | ||
b7328434 JM |
4437 | if (params->openssl_ciphers && |
4438 | SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) { | |
4439 | wpa_printf(MSG_INFO, | |
4440 | "OpenSSL: Failed to set cipher string '%s'", | |
4441 | params->openssl_ciphers); | |
4442 | return -1; | |
4443 | } | |
4444 | ||
e866f39f JM |
4445 | #ifdef SSL_OP_NO_TICKET |
4446 | if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET) | |
4447 | SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET); | |
4448 | else | |
4449 | SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET); | |
4450 | #endif /* SSL_OP_NO_TICKET */ | |
4451 | ||
080585c0 JM |
4452 | #ifdef HAVE_OCSP |
4453 | SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb); | |
4454 | SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx); | |
4455 | os_free(tls_global->ocsp_stapling_response); | |
4456 | if (params->ocsp_stapling_response) | |
4457 | tls_global->ocsp_stapling_response = | |
4458 | os_strdup(params->ocsp_stapling_response); | |
4459 | else | |
4460 | tls_global->ocsp_stapling_response = NULL; | |
4461 | #endif /* HAVE_OCSP */ | |
4462 | ||
6fc6879b JM |
4463 | return 0; |
4464 | } | |
4465 | ||
4466 | ||
1e5839e0 | 4467 | #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) |
6fc6879b JM |
4468 | /* Pre-shared secred requires a patch to openssl, so this function is |
4469 | * commented out unless explicitly needed for EAP-FAST in order to be able to | |
4470 | * build this file with unmodified openssl. */ | |
4471 | ||
587b0457 | 4472 | #if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER) |
a8572960 AL |
4473 | static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len, |
4474 | STACK_OF(SSL_CIPHER) *peer_ciphers, | |
4475 | const SSL_CIPHER **cipher, void *arg) | |
4476 | #else /* OPENSSL_IS_BORINGSSL */ | |
6fc6879b JM |
4477 | static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len, |
4478 | STACK_OF(SSL_CIPHER) *peer_ciphers, | |
4479 | SSL_CIPHER **cipher, void *arg) | |
a8572960 | 4480 | #endif /* OPENSSL_IS_BORINGSSL */ |
6fc6879b JM |
4481 | { |
4482 | struct tls_connection *conn = arg; | |
4483 | int ret; | |
4484 | ||
36e82060 | 4485 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
6fc6879b JM |
4486 | if (conn == NULL || conn->session_ticket_cb == NULL) |
4487 | return 0; | |
4488 | ||
4489 | ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx, | |
4490 | conn->session_ticket, | |
4491 | conn->session_ticket_len, | |
4492 | s->s3->client_random, | |
4493 | s->s3->server_random, secret); | |
4d2a1b4f JM |
4494 | #else |
4495 | unsigned char client_random[SSL3_RANDOM_SIZE]; | |
4496 | unsigned char server_random[SSL3_RANDOM_SIZE]; | |
4497 | ||
4498 | if (conn == NULL || conn->session_ticket_cb == NULL) | |
4499 | return 0; | |
4500 | ||
4501 | SSL_get_client_random(s, client_random, sizeof(client_random)); | |
4502 | SSL_get_server_random(s, server_random, sizeof(server_random)); | |
4503 | ||
4504 | ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx, | |
4505 | conn->session_ticket, | |
4506 | conn->session_ticket_len, | |
4507 | client_random, | |
4508 | server_random, secret); | |
4509 | #endif | |
4510 | ||
6fc6879b JM |
4511 | os_free(conn->session_ticket); |
4512 | conn->session_ticket = NULL; | |
4513 | ||
4514 | if (ret <= 0) | |
4515 | return 0; | |
4516 | ||
4517 | *secret_len = SSL_MAX_MASTER_KEY_LENGTH; | |
4518 | return 1; | |
4519 | } | |
4520 | ||
4521 | ||
0cf03892 JM |
4522 | static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data, |
4523 | int len, void *arg) | |
4524 | { | |
4525 | struct tls_connection *conn = arg; | |
4526 | ||
4527 | if (conn == NULL || conn->session_ticket_cb == NULL) | |
4528 | return 0; | |
4529 | ||
4530 | wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len); | |
4531 | ||
4532 | os_free(conn->session_ticket); | |
4533 | conn->session_ticket = NULL; | |
4534 | ||
4535 | wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket " | |
4536 | "extension", data, len); | |
4537 | ||
a1f11e34 | 4538 | conn->session_ticket = os_memdup(data, len); |
0cf03892 JM |
4539 | if (conn->session_ticket == NULL) |
4540 | return 0; | |
4541 | ||
0cf03892 JM |
4542 | conn->session_ticket_len = len; |
4543 | ||
4544 | return 1; | |
4545 | } | |
1e5839e0 | 4546 | #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
6fc6879b JM |
4547 | |
4548 | ||
4549 | int tls_connection_set_session_ticket_cb(void *tls_ctx, | |
4550 | struct tls_connection *conn, | |
4551 | tls_session_ticket_cb cb, | |
4552 | void *ctx) | |
4553 | { | |
1e5839e0 | 4554 | #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) |
6fc6879b JM |
4555 | conn->session_ticket_cb = cb; |
4556 | conn->session_ticket_cb_ctx = ctx; | |
4557 | ||
4558 | if (cb) { | |
4559 | if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb, | |
4560 | conn) != 1) | |
4561 | return -1; | |
0cf03892 JM |
4562 | SSL_set_session_ticket_ext_cb(conn->ssl, |
4563 | tls_session_ticket_ext_cb, conn); | |
6fc6879b JM |
4564 | } else { |
4565 | if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1) | |
4566 | return -1; | |
0cf03892 | 4567 | SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL); |
6fc6879b JM |
4568 | } |
4569 | ||
4570 | return 0; | |
1e5839e0 | 4571 | #else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
6fc6879b | 4572 | return -1; |
1e5839e0 | 4573 | #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
6fc6879b | 4574 | } |
a1651451 JM |
4575 | |
4576 | ||
4577 | int tls_get_library_version(char *buf, size_t buf_len) | |
4578 | { | |
587b0457 | 4579 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER) |
b34c623c JM |
4580 | return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s", |
4581 | OPENSSL_VERSION_TEXT, | |
4582 | OpenSSL_version(OPENSSL_VERSION)); | |
4583 | #else | |
a1651451 JM |
4584 | return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s", |
4585 | OPENSSL_VERSION_TEXT, | |
4586 | SSLeay_version(SSLEAY_VERSION)); | |
b34c623c | 4587 | #endif |
a1651451 | 4588 | } |
b3b8085a JM |
4589 | |
4590 | ||
4591 | void tls_connection_set_success_data(struct tls_connection *conn, | |
4592 | struct wpabuf *data) | |
4593 | { | |
acf36f31 JM |
4594 | SSL_SESSION *sess; |
4595 | struct wpabuf *old; | |
4596 | ||
4597 | if (tls_ex_idx_session < 0) | |
4598 | goto fail; | |
4599 | sess = SSL_get_session(conn->ssl); | |
4600 | if (!sess) | |
4601 | goto fail; | |
4602 | old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session); | |
4603 | if (old) { | |
4604 | wpa_printf(MSG_DEBUG, "OpenSSL: Replacing old success data %p", | |
4605 | old); | |
4606 | wpabuf_free(old); | |
4607 | } | |
4608 | if (SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1) | |
4609 | goto fail; | |
4610 | ||
4611 | wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p", data); | |
4612 | conn->success_data = 1; | |
4613 | return; | |
4614 | ||
4615 | fail: | |
4616 | wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data"); | |
4617 | wpabuf_free(data); | |
b3b8085a JM |
4618 | } |
4619 | ||
4620 | ||
4621 | void tls_connection_set_success_data_resumed(struct tls_connection *conn) | |
4622 | { | |
acf36f31 JM |
4623 | wpa_printf(MSG_DEBUG, |
4624 | "OpenSSL: Success data accepted for resumed session"); | |
4625 | conn->success_data = 1; | |
b3b8085a JM |
4626 | } |
4627 | ||
4628 | ||
4629 | const struct wpabuf * | |
4630 | tls_connection_get_success_data(struct tls_connection *conn) | |
4631 | { | |
acf36f31 JM |
4632 | SSL_SESSION *sess; |
4633 | ||
4634 | if (tls_ex_idx_session < 0 || | |
4635 | !(sess = SSL_get_session(conn->ssl))) | |
4636 | return NULL; | |
4637 | return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session); | |
b3b8085a JM |
4638 | } |
4639 | ||
4640 | ||
4641 | void tls_connection_remove_session(struct tls_connection *conn) | |
4642 | { | |
acf36f31 JM |
4643 | SSL_SESSION *sess; |
4644 | ||
4645 | sess = SSL_get_session(conn->ssl); | |
4646 | if (!sess) | |
4647 | return; | |
4648 | ||
4649 | if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1) | |
4650 | wpa_printf(MSG_DEBUG, | |
4651 | "OpenSSL: Session was not cached"); | |
4652 | else | |
4653 | wpa_printf(MSG_DEBUG, | |
4654 | "OpenSSL: Removed cached session to disable session resumption"); | |
b3b8085a | 4655 | } |