]>
Commit | Line | Data |
---|---|---|
6fc6879b | 1 | /* |
81c85c06 | 2 | * SSL/TLS interface functions for OpenSSL |
080585c0 | 3 | * Copyright (c) 2004-2013, 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 | |
13 | #define OPENSSL_NO_ENGINE | |
14 | #endif | |
15 | #endif | |
16 | ||
17 | #include <openssl/ssl.h> | |
18 | #include <openssl/err.h> | |
19 | #include <openssl/pkcs12.h> | |
20 | #include <openssl/x509v3.h> | |
21 | #ifndef OPENSSL_NO_ENGINE | |
22 | #include <openssl/engine.h> | |
23 | #endif /* OPENSSL_NO_ENGINE */ | |
24 | ||
2a0cd067 DS |
25 | #ifdef ANDROID |
26 | #include <openssl/pem.h> | |
27 | #include "keystore_get.h" | |
28 | #endif /* ANDROID */ | |
29 | ||
6fc6879b | 30 | #include "common.h" |
00468b46 | 31 | #include "crypto.h" |
6fc6879b JM |
32 | #include "tls.h" |
33 | ||
34 | #if OPENSSL_VERSION_NUMBER >= 0x0090800fL | |
35 | #define OPENSSL_d2i_TYPE const unsigned char ** | |
36 | #else | |
37 | #define OPENSSL_d2i_TYPE unsigned char ** | |
38 | #endif | |
39 | ||
7c0e1e27 PS |
40 | #if defined(SSL_CTX_get_app_data) && defined(SSL_CTX_set_app_data) |
41 | #define OPENSSL_SUPPORTS_CTX_APP_DATA | |
42 | #endif | |
43 | ||
191a8ad7 | 44 | #ifdef SSL_F_SSL_SET_SESSION_TICKET_EXT |
0cf03892 JM |
45 | #ifdef SSL_OP_NO_TICKET |
46 | /* | |
47 | * Session ticket override patch was merged into OpenSSL 0.9.9 tree on | |
48 | * 2008-11-15. This version uses a bit different API compared to the old patch. | |
49 | */ | |
50 | #define CONFIG_OPENSSL_TICKET_OVERRIDE | |
51 | #endif | |
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 | ||
6fc6879b JM |
61 | static int tls_openssl_ref_count = 0; |
62 | ||
7c0e1e27 | 63 | struct tls_context { |
00468b46 JM |
64 | void (*event_cb)(void *ctx, enum tls_event ev, |
65 | union tls_event_data *data); | |
66 | void *cb_ctx; | |
1b414f59 | 67 | int cert_in_cb; |
080585c0 | 68 | char *ocsp_stapling_response; |
00468b46 JM |
69 | }; |
70 | ||
7c0e1e27 | 71 | static struct tls_context *tls_global = NULL; |
00468b46 JM |
72 | |
73 | ||
6fc6879b | 74 | struct tls_connection { |
7c0e1e27 | 75 | struct tls_context *context; |
6fc6879b JM |
76 | SSL *ssl; |
77 | BIO *ssl_in, *ssl_out; | |
78 | #ifndef OPENSSL_NO_ENGINE | |
79 | ENGINE *engine; /* functional reference to the engine */ | |
80 | EVP_PKEY *private_key; /* the private key if using engine */ | |
81 | #endif /* OPENSSL_NO_ENGINE */ | |
01f809c7 | 82 | char *subject_match, *altsubject_match, *suffix_match; |
6fc6879b JM |
83 | int read_alerts, write_alerts, failed; |
84 | ||
85 | tls_session_ticket_cb session_ticket_cb; | |
86 | void *session_ticket_cb_ctx; | |
87 | ||
88 | /* SessionTicket received from OpenSSL hello_extension_cb (server) */ | |
89 | u8 *session_ticket; | |
90 | size_t session_ticket_len; | |
00468b46 | 91 | |
8d6399e4 JM |
92 | unsigned int ca_cert_verify:1; |
93 | unsigned int cert_probe:1; | |
94 | unsigned int server_cert_only:1; | |
00468b46 JM |
95 | |
96 | u8 srv_cert_hash[32]; | |
235279e7 JM |
97 | |
98 | unsigned int flags; | |
080585c0 JM |
99 | |
100 | X509 *peer_cert; | |
101 | X509 *peer_issuer; | |
6fc6879b JM |
102 | }; |
103 | ||
104 | ||
7c0e1e27 PS |
105 | static struct tls_context * tls_context_new(const struct tls_config *conf) |
106 | { | |
107 | struct tls_context *context = os_zalloc(sizeof(*context)); | |
108 | if (context == NULL) | |
109 | return NULL; | |
110 | if (conf) { | |
111 | context->event_cb = conf->event_cb; | |
112 | context->cb_ctx = conf->cb_ctx; | |
113 | context->cert_in_cb = conf->cert_in_cb; | |
114 | } | |
115 | return context; | |
116 | } | |
117 | ||
118 | ||
6fc6879b JM |
119 | #ifdef CONFIG_NO_STDOUT_DEBUG |
120 | ||
121 | static void _tls_show_errors(void) | |
122 | { | |
123 | unsigned long err; | |
124 | ||
125 | while ((err = ERR_get_error())) { | |
126 | /* Just ignore the errors, since stdout is disabled */ | |
127 | } | |
128 | } | |
129 | #define tls_show_errors(l, f, t) _tls_show_errors() | |
130 | ||
131 | #else /* CONFIG_NO_STDOUT_DEBUG */ | |
132 | ||
133 | static void tls_show_errors(int level, const char *func, const char *txt) | |
134 | { | |
135 | unsigned long err; | |
136 | ||
137 | wpa_printf(level, "OpenSSL: %s - %s %s", | |
138 | func, txt, ERR_error_string(ERR_get_error(), NULL)); | |
139 | ||
140 | while ((err = ERR_get_error())) { | |
141 | wpa_printf(MSG_INFO, "OpenSSL: pending error: %s", | |
142 | ERR_error_string(err, NULL)); | |
143 | } | |
144 | } | |
145 | ||
146 | #endif /* CONFIG_NO_STDOUT_DEBUG */ | |
147 | ||
148 | ||
149 | #ifdef CONFIG_NATIVE_WINDOWS | |
150 | ||
151 | /* Windows CryptoAPI and access to certificate stores */ | |
152 | #include <wincrypt.h> | |
153 | ||
154 | #ifdef __MINGW32_VERSION | |
155 | /* | |
156 | * MinGW does not yet include all the needed definitions for CryptoAPI, so | |
157 | * define here whatever extra is needed. | |
158 | */ | |
6fc6879b JM |
159 | #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16) |
160 | #define CERT_STORE_READONLY_FLAG 0x00008000 | |
161 | #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000 | |
6fc6879b JM |
162 | |
163 | #endif /* __MINGW32_VERSION */ | |
164 | ||
165 | ||
166 | struct cryptoapi_rsa_data { | |
167 | const CERT_CONTEXT *cert; | |
168 | HCRYPTPROV crypt_prov; | |
169 | DWORD key_spec; | |
170 | BOOL free_crypt_prov; | |
171 | }; | |
172 | ||
173 | ||
174 | static void cryptoapi_error(const char *msg) | |
175 | { | |
176 | wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u", | |
177 | msg, (unsigned int) GetLastError()); | |
178 | } | |
179 | ||
180 | ||
181 | static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from, | |
182 | unsigned char *to, RSA *rsa, int padding) | |
183 | { | |
184 | wpa_printf(MSG_DEBUG, "%s - not implemented", __func__); | |
185 | return 0; | |
186 | } | |
187 | ||
188 | ||
189 | static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from, | |
190 | unsigned char *to, RSA *rsa, int padding) | |
191 | { | |
192 | wpa_printf(MSG_DEBUG, "%s - not implemented", __func__); | |
193 | return 0; | |
194 | } | |
195 | ||
196 | ||
197 | static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from, | |
198 | unsigned char *to, RSA *rsa, int padding) | |
199 | { | |
200 | struct cryptoapi_rsa_data *priv = | |
201 | (struct cryptoapi_rsa_data *) rsa->meth->app_data; | |
202 | HCRYPTHASH hash; | |
203 | DWORD hash_size, len, i; | |
204 | unsigned char *buf = NULL; | |
205 | int ret = 0; | |
206 | ||
207 | if (priv == NULL) { | |
208 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, | |
209 | ERR_R_PASSED_NULL_PARAMETER); | |
210 | return 0; | |
211 | } | |
212 | ||
213 | if (padding != RSA_PKCS1_PADDING) { | |
214 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, | |
215 | RSA_R_UNKNOWN_PADDING_TYPE); | |
216 | return 0; | |
217 | } | |
218 | ||
219 | if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) { | |
220 | wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported", | |
221 | __func__); | |
222 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, | |
223 | RSA_R_INVALID_MESSAGE_LENGTH); | |
224 | return 0; | |
225 | } | |
226 | ||
227 | if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash)) | |
228 | { | |
229 | cryptoapi_error("CryptCreateHash failed"); | |
230 | return 0; | |
231 | } | |
232 | ||
233 | len = sizeof(hash_size); | |
234 | if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len, | |
235 | 0)) { | |
236 | cryptoapi_error("CryptGetHashParam failed"); | |
237 | goto err; | |
238 | } | |
239 | ||
240 | if ((int) hash_size != flen) { | |
241 | wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)", | |
242 | (unsigned) hash_size, flen); | |
243 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, | |
244 | RSA_R_INVALID_MESSAGE_LENGTH); | |
245 | goto err; | |
246 | } | |
247 | if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) { | |
248 | cryptoapi_error("CryptSetHashParam failed"); | |
249 | goto err; | |
250 | } | |
251 | ||
252 | len = RSA_size(rsa); | |
253 | buf = os_malloc(len); | |
254 | if (buf == NULL) { | |
255 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE); | |
256 | goto err; | |
257 | } | |
258 | ||
259 | if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) { | |
260 | cryptoapi_error("CryptSignHash failed"); | |
261 | goto err; | |
262 | } | |
263 | ||
264 | for (i = 0; i < len; i++) | |
265 | to[i] = buf[len - i - 1]; | |
266 | ret = len; | |
267 | ||
268 | err: | |
269 | os_free(buf); | |
270 | CryptDestroyHash(hash); | |
271 | ||
272 | return ret; | |
273 | } | |
274 | ||
275 | ||
276 | static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from, | |
277 | unsigned char *to, RSA *rsa, int padding) | |
278 | { | |
279 | wpa_printf(MSG_DEBUG, "%s - not implemented", __func__); | |
280 | return 0; | |
281 | } | |
282 | ||
283 | ||
284 | static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv) | |
285 | { | |
286 | if (priv == NULL) | |
287 | return; | |
288 | if (priv->crypt_prov && priv->free_crypt_prov) | |
289 | CryptReleaseContext(priv->crypt_prov, 0); | |
290 | if (priv->cert) | |
291 | CertFreeCertificateContext(priv->cert); | |
292 | os_free(priv); | |
293 | } | |
294 | ||
295 | ||
296 | static int cryptoapi_finish(RSA *rsa) | |
297 | { | |
298 | cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data); | |
299 | os_free((void *) rsa->meth); | |
300 | rsa->meth = NULL; | |
301 | return 1; | |
302 | } | |
303 | ||
304 | ||
305 | static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store) | |
306 | { | |
307 | HCERTSTORE cs; | |
308 | const CERT_CONTEXT *ret = NULL; | |
309 | ||
310 | cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0, | |
311 | store | CERT_STORE_OPEN_EXISTING_FLAG | | |
312 | CERT_STORE_READONLY_FLAG, L"MY"); | |
313 | if (cs == NULL) { | |
314 | cryptoapi_error("Failed to open 'My system store'"); | |
315 | return NULL; | |
316 | } | |
317 | ||
318 | if (strncmp(name, "cert://", 7) == 0) { | |
319 | unsigned short wbuf[255]; | |
320 | MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255); | |
321 | ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING | | |
322 | PKCS_7_ASN_ENCODING, | |
323 | 0, CERT_FIND_SUBJECT_STR, | |
324 | wbuf, NULL); | |
325 | } else if (strncmp(name, "hash://", 7) == 0) { | |
326 | CRYPT_HASH_BLOB blob; | |
327 | int len; | |
328 | const char *hash = name + 7; | |
329 | unsigned char *buf; | |
330 | ||
331 | len = os_strlen(hash) / 2; | |
332 | buf = os_malloc(len); | |
333 | if (buf && hexstr2bin(hash, buf, len) == 0) { | |
334 | blob.cbData = len; | |
335 | blob.pbData = buf; | |
336 | ret = CertFindCertificateInStore(cs, | |
337 | X509_ASN_ENCODING | | |
338 | PKCS_7_ASN_ENCODING, | |
339 | 0, CERT_FIND_HASH, | |
340 | &blob, NULL); | |
341 | } | |
342 | os_free(buf); | |
343 | } | |
344 | ||
345 | CertCloseStore(cs, 0); | |
346 | ||
347 | return ret; | |
348 | } | |
349 | ||
350 | ||
351 | static int tls_cryptoapi_cert(SSL *ssl, const char *name) | |
352 | { | |
353 | X509 *cert = NULL; | |
354 | RSA *rsa = NULL, *pub_rsa; | |
355 | struct cryptoapi_rsa_data *priv; | |
356 | RSA_METHOD *rsa_meth; | |
357 | ||
358 | if (name == NULL || | |
359 | (strncmp(name, "cert://", 7) != 0 && | |
360 | strncmp(name, "hash://", 7) != 0)) | |
361 | return -1; | |
362 | ||
363 | priv = os_zalloc(sizeof(*priv)); | |
364 | rsa_meth = os_zalloc(sizeof(*rsa_meth)); | |
365 | if (priv == NULL || rsa_meth == NULL) { | |
366 | wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory " | |
367 | "for CryptoAPI RSA method"); | |
368 | os_free(priv); | |
369 | os_free(rsa_meth); | |
370 | return -1; | |
371 | } | |
372 | ||
373 | priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER); | |
374 | if (priv->cert == NULL) { | |
375 | priv->cert = cryptoapi_find_cert( | |
376 | name, CERT_SYSTEM_STORE_LOCAL_MACHINE); | |
377 | } | |
378 | if (priv->cert == NULL) { | |
379 | wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate " | |
380 | "'%s'", name); | |
381 | goto err; | |
382 | } | |
383 | ||
384 | cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &priv->cert->pbCertEncoded, | |
385 | priv->cert->cbCertEncoded); | |
386 | if (cert == NULL) { | |
387 | wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER " | |
388 | "encoding"); | |
389 | goto err; | |
390 | } | |
391 | ||
6fc6879b JM |
392 | if (!CryptAcquireCertificatePrivateKey(priv->cert, |
393 | CRYPT_ACQUIRE_COMPARE_KEY_FLAG, | |
394 | NULL, &priv->crypt_prov, | |
395 | &priv->key_spec, | |
396 | &priv->free_crypt_prov)) { | |
397 | cryptoapi_error("Failed to acquire a private key for the " | |
398 | "certificate"); | |
399 | goto err; | |
400 | } | |
401 | ||
402 | rsa_meth->name = "Microsoft CryptoAPI RSA Method"; | |
403 | rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc; | |
404 | rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec; | |
405 | rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc; | |
406 | rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec; | |
407 | rsa_meth->finish = cryptoapi_finish; | |
408 | rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK; | |
409 | rsa_meth->app_data = (char *) priv; | |
410 | ||
411 | rsa = RSA_new(); | |
412 | if (rsa == NULL) { | |
413 | SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, | |
414 | ERR_R_MALLOC_FAILURE); | |
415 | goto err; | |
416 | } | |
417 | ||
418 | if (!SSL_use_certificate(ssl, cert)) { | |
419 | RSA_free(rsa); | |
420 | rsa = NULL; | |
421 | goto err; | |
422 | } | |
423 | pub_rsa = cert->cert_info->key->pkey->pkey.rsa; | |
424 | X509_free(cert); | |
425 | cert = NULL; | |
426 | ||
427 | rsa->n = BN_dup(pub_rsa->n); | |
428 | rsa->e = BN_dup(pub_rsa->e); | |
429 | if (!RSA_set_method(rsa, rsa_meth)) | |
430 | goto err; | |
431 | ||
432 | if (!SSL_use_RSAPrivateKey(ssl, rsa)) | |
433 | goto err; | |
434 | RSA_free(rsa); | |
435 | ||
436 | return 0; | |
437 | ||
438 | err: | |
439 | if (cert) | |
440 | X509_free(cert); | |
441 | if (rsa) | |
442 | RSA_free(rsa); | |
443 | else { | |
444 | os_free(rsa_meth); | |
445 | cryptoapi_free_data(priv); | |
446 | } | |
447 | return -1; | |
448 | } | |
449 | ||
450 | ||
451 | static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name) | |
452 | { | |
453 | HCERTSTORE cs; | |
454 | PCCERT_CONTEXT ctx = NULL; | |
455 | X509 *cert; | |
456 | char buf[128]; | |
457 | const char *store; | |
458 | #ifdef UNICODE | |
459 | WCHAR *wstore; | |
460 | #endif /* UNICODE */ | |
461 | ||
6fc6879b JM |
462 | if (name == NULL || strncmp(name, "cert_store://", 13) != 0) |
463 | return -1; | |
464 | ||
465 | store = name + 13; | |
466 | #ifdef UNICODE | |
467 | wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR)); | |
468 | if (wstore == NULL) | |
469 | return -1; | |
470 | wsprintf(wstore, L"%S", store); | |
471 | cs = CertOpenSystemStore(0, wstore); | |
472 | os_free(wstore); | |
473 | #else /* UNICODE */ | |
474 | cs = CertOpenSystemStore(0, store); | |
475 | #endif /* UNICODE */ | |
476 | if (cs == NULL) { | |
477 | wpa_printf(MSG_DEBUG, "%s: failed to open system cert store " | |
478 | "'%s': error=%d", __func__, store, | |
479 | (int) GetLastError()); | |
480 | return -1; | |
481 | } | |
482 | ||
483 | while ((ctx = CertEnumCertificatesInStore(cs, ctx))) { | |
484 | cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &ctx->pbCertEncoded, | |
485 | ctx->cbCertEncoded); | |
486 | if (cert == NULL) { | |
487 | wpa_printf(MSG_INFO, "CryptoAPI: Could not process " | |
488 | "X509 DER encoding for CA cert"); | |
489 | continue; | |
490 | } | |
491 | ||
492 | X509_NAME_oneline(X509_get_subject_name(cert), buf, | |
493 | sizeof(buf)); | |
494 | wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for " | |
495 | "system certificate store: subject='%s'", buf); | |
496 | ||
497 | if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) { | |
498 | tls_show_errors(MSG_WARNING, __func__, | |
499 | "Failed to add ca_cert to OpenSSL " | |
500 | "certificate store"); | |
501 | } | |
502 | ||
503 | X509_free(cert); | |
504 | } | |
505 | ||
506 | if (!CertCloseStore(cs, 0)) { | |
507 | wpa_printf(MSG_DEBUG, "%s: failed to close system cert store " | |
508 | "'%s': error=%d", __func__, name + 13, | |
509 | (int) GetLastError()); | |
510 | } | |
511 | ||
512 | return 0; | |
513 | } | |
514 | ||
515 | ||
516 | #else /* CONFIG_NATIVE_WINDOWS */ | |
517 | ||
518 | static int tls_cryptoapi_cert(SSL *ssl, const char *name) | |
519 | { | |
520 | return -1; | |
521 | } | |
522 | ||
523 | #endif /* CONFIG_NATIVE_WINDOWS */ | |
524 | ||
525 | ||
526 | static void ssl_info_cb(const SSL *ssl, int where, int ret) | |
527 | { | |
528 | const char *str; | |
529 | int w; | |
530 | ||
531 | wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret); | |
532 | w = where & ~SSL_ST_MASK; | |
533 | if (w & SSL_ST_CONNECT) | |
534 | str = "SSL_connect"; | |
535 | else if (w & SSL_ST_ACCEPT) | |
536 | str = "SSL_accept"; | |
537 | else | |
538 | str = "undefined"; | |
539 | ||
540 | if (where & SSL_CB_LOOP) { | |
541 | wpa_printf(MSG_DEBUG, "SSL: %s:%s", | |
542 | str, SSL_state_string_long(ssl)); | |
543 | } else if (where & SSL_CB_ALERT) { | |
7c0e1e27 | 544 | struct tls_connection *conn = SSL_get_app_data((SSL *) ssl); |
6fc6879b JM |
545 | wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s", |
546 | where & SSL_CB_READ ? | |
547 | "read (remote end reported an error)" : | |
548 | "write (local SSL3 detected an error)", | |
549 | SSL_alert_type_string_long(ret), | |
550 | SSL_alert_desc_string_long(ret)); | |
551 | if ((ret >> 8) == SSL3_AL_FATAL) { | |
6fc6879b JM |
552 | if (where & SSL_CB_READ) |
553 | conn->read_alerts++; | |
554 | else | |
555 | conn->write_alerts++; | |
556 | } | |
7c0e1e27 | 557 | if (conn->context->event_cb != NULL) { |
dd7fec1f | 558 | union tls_event_data ev; |
7c0e1e27 | 559 | struct tls_context *context = conn->context; |
dd7fec1f PS |
560 | os_memset(&ev, 0, sizeof(ev)); |
561 | ev.alert.is_local = !(where & SSL_CB_READ); | |
562 | ev.alert.type = SSL_alert_type_string_long(ret); | |
563 | ev.alert.description = SSL_alert_desc_string_long(ret); | |
7c0e1e27 | 564 | context->event_cb(context->cb_ctx, TLS_ALERT, &ev); |
dd7fec1f | 565 | } |
6fc6879b JM |
566 | } else if (where & SSL_CB_EXIT && ret <= 0) { |
567 | wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s", | |
568 | str, ret == 0 ? "failed" : "error", | |
569 | SSL_state_string_long(ssl)); | |
570 | } | |
571 | } | |
572 | ||
573 | ||
574 | #ifndef OPENSSL_NO_ENGINE | |
575 | /** | |
576 | * tls_engine_load_dynamic_generic - load any openssl engine | |
577 | * @pre: an array of commands and values that load an engine initialized | |
578 | * in the engine specific function | |
579 | * @post: an array of commands and values that initialize an already loaded | |
580 | * engine (or %NULL if not required) | |
581 | * @id: the engine id of the engine to load (only required if post is not %NULL | |
582 | * | |
583 | * This function is a generic function that loads any openssl engine. | |
584 | * | |
585 | * Returns: 0 on success, -1 on failure | |
586 | */ | |
587 | static int tls_engine_load_dynamic_generic(const char *pre[], | |
588 | const char *post[], const char *id) | |
589 | { | |
590 | ENGINE *engine; | |
591 | const char *dynamic_id = "dynamic"; | |
592 | ||
593 | engine = ENGINE_by_id(id); | |
594 | if (engine) { | |
595 | ENGINE_free(engine); | |
596 | wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already " | |
597 | "available", id); | |
598 | return 0; | |
599 | } | |
600 | ERR_clear_error(); | |
601 | ||
602 | engine = ENGINE_by_id(dynamic_id); | |
603 | if (engine == NULL) { | |
604 | wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]", | |
605 | dynamic_id, | |
606 | ERR_error_string(ERR_get_error(), NULL)); | |
607 | return -1; | |
608 | } | |
609 | ||
610 | /* Perform the pre commands. This will load the engine. */ | |
611 | while (pre && pre[0]) { | |
612 | wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]); | |
613 | if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) { | |
614 | wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: " | |
615 | "%s %s [%s]", pre[0], pre[1], | |
616 | ERR_error_string(ERR_get_error(), NULL)); | |
617 | ENGINE_free(engine); | |
618 | return -1; | |
619 | } | |
620 | pre += 2; | |
621 | } | |
622 | ||
623 | /* | |
624 | * Free the reference to the "dynamic" engine. The loaded engine can | |
625 | * now be looked up using ENGINE_by_id(). | |
626 | */ | |
627 | ENGINE_free(engine); | |
628 | ||
629 | engine = ENGINE_by_id(id); | |
630 | if (engine == NULL) { | |
631 | wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]", | |
632 | id, ERR_error_string(ERR_get_error(), NULL)); | |
633 | return -1; | |
634 | } | |
635 | ||
636 | while (post && post[0]) { | |
637 | wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]); | |
638 | if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) { | |
639 | wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:" | |
640 | " %s %s [%s]", post[0], post[1], | |
641 | ERR_error_string(ERR_get_error(), NULL)); | |
642 | ENGINE_remove(engine); | |
643 | ENGINE_free(engine); | |
644 | return -1; | |
645 | } | |
646 | post += 2; | |
647 | } | |
648 | ENGINE_free(engine); | |
649 | ||
650 | return 0; | |
651 | } | |
652 | ||
653 | ||
654 | /** | |
655 | * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc | |
656 | * @pkcs11_so_path: pksc11_so_path from the configuration | |
657 | * @pcks11_module_path: pkcs11_module_path from the configuration | |
658 | */ | |
659 | static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path, | |
660 | const char *pkcs11_module_path) | |
661 | { | |
662 | char *engine_id = "pkcs11"; | |
663 | const char *pre_cmd[] = { | |
664 | "SO_PATH", NULL /* pkcs11_so_path */, | |
665 | "ID", NULL /* engine_id */, | |
666 | "LIST_ADD", "1", | |
667 | /* "NO_VCHECK", "1", */ | |
668 | "LOAD", NULL, | |
669 | NULL, NULL | |
670 | }; | |
671 | const char *post_cmd[] = { | |
672 | "MODULE_PATH", NULL /* pkcs11_module_path */, | |
673 | NULL, NULL | |
674 | }; | |
675 | ||
676 | if (!pkcs11_so_path || !pkcs11_module_path) | |
677 | return 0; | |
678 | ||
679 | pre_cmd[1] = pkcs11_so_path; | |
680 | pre_cmd[3] = engine_id; | |
681 | post_cmd[1] = pkcs11_module_path; | |
682 | ||
683 | wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s", | |
684 | pkcs11_so_path); | |
685 | ||
686 | return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id); | |
687 | } | |
688 | ||
689 | ||
690 | /** | |
691 | * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc | |
692 | * @opensc_so_path: opensc_so_path from the configuration | |
693 | */ | |
694 | static int tls_engine_load_dynamic_opensc(const char *opensc_so_path) | |
695 | { | |
696 | char *engine_id = "opensc"; | |
697 | const char *pre_cmd[] = { | |
698 | "SO_PATH", NULL /* opensc_so_path */, | |
699 | "ID", NULL /* engine_id */, | |
700 | "LIST_ADD", "1", | |
701 | "LOAD", NULL, | |
702 | NULL, NULL | |
703 | }; | |
704 | ||
705 | if (!opensc_so_path) | |
706 | return 0; | |
707 | ||
708 | pre_cmd[1] = opensc_so_path; | |
709 | pre_cmd[3] = engine_id; | |
710 | ||
711 | wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s", | |
712 | opensc_so_path); | |
713 | ||
714 | return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id); | |
715 | } | |
716 | #endif /* OPENSSL_NO_ENGINE */ | |
717 | ||
718 | ||
719 | void * tls_init(const struct tls_config *conf) | |
720 | { | |
721 | SSL_CTX *ssl; | |
7c0e1e27 | 722 | struct tls_context *context; |
6fc6879b JM |
723 | |
724 | if (tls_openssl_ref_count == 0) { | |
7c0e1e27 PS |
725 | tls_global = context = tls_context_new(conf); |
726 | if (context == NULL) | |
00468b46 | 727 | return NULL; |
76f04b38 JM |
728 | #ifdef CONFIG_FIPS |
729 | #ifdef OPENSSL_FIPS | |
cf123d7f | 730 | if (conf && conf->fips_mode) { |
76f04b38 JM |
731 | if (!FIPS_mode_set(1)) { |
732 | wpa_printf(MSG_ERROR, "Failed to enable FIPS " | |
733 | "mode"); | |
734 | ERR_load_crypto_strings(); | |
735 | ERR_print_errors_fp(stderr); | |
b36540db JM |
736 | os_free(tls_global); |
737 | tls_global = NULL; | |
76f04b38 JM |
738 | return NULL; |
739 | } else | |
740 | wpa_printf(MSG_INFO, "Running in FIPS mode"); | |
741 | } | |
742 | #else /* OPENSSL_FIPS */ | |
cf123d7f | 743 | if (conf && conf->fips_mode) { |
76f04b38 JM |
744 | wpa_printf(MSG_ERROR, "FIPS mode requested, but not " |
745 | "supported"); | |
b36540db JM |
746 | os_free(tls_global); |
747 | tls_global = NULL; | |
76f04b38 JM |
748 | return NULL; |
749 | } | |
750 | #endif /* OPENSSL_FIPS */ | |
751 | #endif /* CONFIG_FIPS */ | |
6fc6879b JM |
752 | SSL_load_error_strings(); |
753 | SSL_library_init(); | |
4bb3377b | 754 | #if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && !defined(OPENSSL_NO_SHA256) |
e1ffdfc1 JM |
755 | EVP_add_digest(EVP_sha256()); |
756 | #endif /* OPENSSL_NO_SHA256 */ | |
6fc6879b JM |
757 | /* TODO: if /dev/urandom is available, PRNG is seeded |
758 | * automatically. If this is not the case, random data should | |
759 | * be added here. */ | |
760 | ||
761 | #ifdef PKCS12_FUNCS | |
1056dad7 JM |
762 | #ifndef OPENSSL_NO_RC2 |
763 | /* | |
764 | * 40-bit RC2 is commonly used in PKCS#12 files, so enable it. | |
765 | * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8 | |
766 | * versions, but it looks like OpenSSL 1.0.0 does not do that | |
767 | * anymore. | |
768 | */ | |
769 | EVP_add_cipher(EVP_rc2_40_cbc()); | |
770 | #endif /* OPENSSL_NO_RC2 */ | |
6fc6879b JM |
771 | PKCS12_PBE_add(); |
772 | #endif /* PKCS12_FUNCS */ | |
7c0e1e27 PS |
773 | } else { |
774 | context = tls_global; | |
775 | #ifdef OPENSSL_SUPPORTS_CTX_APP_DATA | |
776 | /* Newer OpenSSL can store app-data per-SSL */ | |
777 | context = tls_context_new(conf); | |
778 | if (context == NULL) | |
779 | return NULL; | |
780 | #endif /* OPENSSL_SUPPORTS_CTX_APP_DATA */ | |
6fc6879b JM |
781 | } |
782 | tls_openssl_ref_count++; | |
783 | ||
784 | ssl = SSL_CTX_new(TLSv1_method()); | |
7c0e1e27 PS |
785 | if (ssl == NULL) { |
786 | tls_openssl_ref_count--; | |
787 | if (tls_openssl_ref_count == 0) { | |
788 | os_free(tls_global); | |
789 | tls_global = NULL; | |
790 | } else if (context != tls_global) { | |
791 | os_free(context); | |
792 | } | |
6fc6879b | 793 | return NULL; |
7c0e1e27 | 794 | } |
6fc6879b JM |
795 | |
796 | SSL_CTX_set_info_callback(ssl, ssl_info_cb); | |
7c0e1e27 PS |
797 | #ifdef OPENSSL_SUPPORTS_CTX_APP_DATA |
798 | SSL_CTX_set_app_data(ssl, context); | |
799 | #endif /* OPENSSL_SUPPORTS_CTX_APP_DATA */ | |
6fc6879b JM |
800 | |
801 | #ifndef OPENSSL_NO_ENGINE | |
802 | if (conf && | |
803 | (conf->opensc_engine_path || conf->pkcs11_engine_path || | |
804 | conf->pkcs11_module_path)) { | |
805 | wpa_printf(MSG_DEBUG, "ENGINE: Loading dynamic engine"); | |
806 | ERR_load_ENGINE_strings(); | |
807 | ENGINE_load_dynamic(); | |
808 | ||
809 | if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) || | |
810 | tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path, | |
811 | conf->pkcs11_module_path)) { | |
812 | tls_deinit(ssl); | |
813 | return NULL; | |
814 | } | |
815 | } | |
816 | #endif /* OPENSSL_NO_ENGINE */ | |
817 | ||
818 | return ssl; | |
819 | } | |
820 | ||
821 | ||
822 | void tls_deinit(void *ssl_ctx) | |
823 | { | |
824 | SSL_CTX *ssl = ssl_ctx; | |
7c0e1e27 PS |
825 | #ifdef OPENSSL_SUPPORTS_CTX_APP_DATA |
826 | struct tls_context *context = SSL_CTX_get_app_data(ssl); | |
827 | if (context != tls_global) | |
828 | os_free(context); | |
829 | #endif /* OPENSSL_SUPPORTS_CTX_APP_DATA */ | |
6fc6879b JM |
830 | SSL_CTX_free(ssl); |
831 | ||
832 | tls_openssl_ref_count--; | |
833 | if (tls_openssl_ref_count == 0) { | |
834 | #ifndef OPENSSL_NO_ENGINE | |
835 | ENGINE_cleanup(); | |
836 | #endif /* OPENSSL_NO_ENGINE */ | |
837 | CRYPTO_cleanup_all_ex_data(); | |
838 | ERR_remove_state(0); | |
839 | ERR_free_strings(); | |
840 | EVP_cleanup(); | |
080585c0 JM |
841 | os_free(tls_global->ocsp_stapling_response); |
842 | tls_global->ocsp_stapling_response = NULL; | |
00468b46 JM |
843 | os_free(tls_global); |
844 | tls_global = NULL; | |
6fc6879b JM |
845 | } |
846 | } | |
847 | ||
848 | ||
849 | static int tls_engine_init(struct tls_connection *conn, const char *engine_id, | |
e59c91af DS |
850 | const char *pin, const char *key_id, |
851 | const char *cert_id, const char *ca_cert_id) | |
6fc6879b JM |
852 | { |
853 | #ifndef OPENSSL_NO_ENGINE | |
854 | int ret = -1; | |
855 | if (engine_id == NULL) { | |
856 | wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set"); | |
857 | return -1; | |
858 | } | |
859 | if (pin == NULL) { | |
860 | wpa_printf(MSG_ERROR, "ENGINE: Smartcard PIN not set"); | |
861 | return -1; | |
862 | } | |
863 | if (key_id == NULL) { | |
864 | wpa_printf(MSG_ERROR, "ENGINE: Key Id not set"); | |
865 | return -1; | |
866 | } | |
867 | ||
868 | ERR_clear_error(); | |
869 | conn->engine = ENGINE_by_id(engine_id); | |
870 | if (!conn->engine) { | |
871 | wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]", | |
872 | engine_id, ERR_error_string(ERR_get_error(), NULL)); | |
873 | goto err; | |
874 | } | |
875 | if (ENGINE_init(conn->engine) != 1) { | |
876 | wpa_printf(MSG_ERROR, "ENGINE: engine init failed " | |
877 | "(engine: %s) [%s]", engine_id, | |
878 | ERR_error_string(ERR_get_error(), NULL)); | |
879 | goto err; | |
880 | } | |
881 | wpa_printf(MSG_DEBUG, "ENGINE: engine initialized"); | |
882 | ||
883 | if (ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) { | |
884 | wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]", | |
885 | ERR_error_string(ERR_get_error(), NULL)); | |
886 | goto err; | |
887 | } | |
e59c91af | 888 | /* load private key first in-case PIN is required for cert */ |
6fc6879b JM |
889 | conn->private_key = ENGINE_load_private_key(conn->engine, |
890 | key_id, NULL, NULL); | |
891 | if (!conn->private_key) { | |
892 | wpa_printf(MSG_ERROR, "ENGINE: cannot load private key with id" | |
893 | " '%s' [%s]", key_id, | |
894 | ERR_error_string(ERR_get_error(), NULL)); | |
895 | ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; | |
896 | goto err; | |
897 | } | |
e59c91af DS |
898 | |
899 | /* handle a certificate and/or CA certificate */ | |
900 | if (cert_id || ca_cert_id) { | |
901 | const char *cmd_name = "LOAD_CERT_CTRL"; | |
902 | ||
903 | /* test if the engine supports a LOAD_CERT_CTRL */ | |
904 | if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME, | |
905 | 0, (void *)cmd_name, NULL)) { | |
906 | wpa_printf(MSG_ERROR, "ENGINE: engine does not support" | |
907 | " loading certificates"); | |
908 | ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; | |
909 | goto err; | |
910 | } | |
911 | } | |
912 | ||
6fc6879b JM |
913 | return 0; |
914 | ||
915 | err: | |
916 | if (conn->engine) { | |
917 | ENGINE_free(conn->engine); | |
918 | conn->engine = NULL; | |
919 | } | |
920 | ||
921 | if (conn->private_key) { | |
922 | EVP_PKEY_free(conn->private_key); | |
923 | conn->private_key = NULL; | |
924 | } | |
925 | ||
926 | return ret; | |
927 | #else /* OPENSSL_NO_ENGINE */ | |
928 | return 0; | |
929 | #endif /* OPENSSL_NO_ENGINE */ | |
930 | } | |
931 | ||
932 | ||
933 | static void tls_engine_deinit(struct tls_connection *conn) | |
934 | { | |
935 | #ifndef OPENSSL_NO_ENGINE | |
936 | wpa_printf(MSG_DEBUG, "ENGINE: engine deinit"); | |
937 | if (conn->private_key) { | |
938 | EVP_PKEY_free(conn->private_key); | |
939 | conn->private_key = NULL; | |
940 | } | |
941 | if (conn->engine) { | |
942 | ENGINE_finish(conn->engine); | |
943 | conn->engine = NULL; | |
944 | } | |
945 | #endif /* OPENSSL_NO_ENGINE */ | |
946 | } | |
947 | ||
948 | ||
949 | int tls_get_errors(void *ssl_ctx) | |
950 | { | |
951 | int count = 0; | |
952 | unsigned long err; | |
953 | ||
954 | while ((err = ERR_get_error())) { | |
955 | wpa_printf(MSG_INFO, "TLS - SSL error: %s", | |
956 | ERR_error_string(err, NULL)); | |
957 | count++; | |
958 | } | |
959 | ||
960 | return count; | |
961 | } | |
962 | ||
963 | struct tls_connection * tls_connection_init(void *ssl_ctx) | |
964 | { | |
965 | SSL_CTX *ssl = ssl_ctx; | |
966 | struct tls_connection *conn; | |
fca25ef4 | 967 | long options; |
7c0e1e27 PS |
968 | struct tls_context *context = tls_global; |
969 | #ifdef OPENSSL_SUPPORTS_CTX_APP_DATA | |
970 | context = SSL_CTX_get_app_data(ssl); | |
971 | #endif /* OPENSSL_SUPPORTS_CTX_APP_DATA */ | |
6fc6879b JM |
972 | |
973 | conn = os_zalloc(sizeof(*conn)); | |
974 | if (conn == NULL) | |
975 | return NULL; | |
976 | conn->ssl = SSL_new(ssl); | |
977 | if (conn->ssl == NULL) { | |
978 | tls_show_errors(MSG_INFO, __func__, | |
979 | "Failed to initialize new SSL connection"); | |
980 | os_free(conn); | |
981 | return NULL; | |
982 | } | |
983 | ||
7c0e1e27 | 984 | conn->context = context; |
6fc6879b | 985 | SSL_set_app_data(conn->ssl, conn); |
fca25ef4 JM |
986 | options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | |
987 | SSL_OP_SINGLE_DH_USE; | |
988 | #ifdef SSL_OP_NO_COMPRESSION | |
989 | options |= SSL_OP_NO_COMPRESSION; | |
990 | #endif /* SSL_OP_NO_COMPRESSION */ | |
991 | SSL_set_options(conn->ssl, options); | |
6fc6879b JM |
992 | |
993 | conn->ssl_in = BIO_new(BIO_s_mem()); | |
994 | if (!conn->ssl_in) { | |
995 | tls_show_errors(MSG_INFO, __func__, | |
996 | "Failed to create a new BIO for ssl_in"); | |
997 | SSL_free(conn->ssl); | |
998 | os_free(conn); | |
999 | return NULL; | |
1000 | } | |
1001 | ||
1002 | conn->ssl_out = BIO_new(BIO_s_mem()); | |
1003 | if (!conn->ssl_out) { | |
1004 | tls_show_errors(MSG_INFO, __func__, | |
1005 | "Failed to create a new BIO for ssl_out"); | |
1006 | SSL_free(conn->ssl); | |
1007 | BIO_free(conn->ssl_in); | |
1008 | os_free(conn); | |
1009 | return NULL; | |
1010 | } | |
1011 | ||
1012 | SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out); | |
1013 | ||
1014 | return conn; | |
1015 | } | |
1016 | ||
1017 | ||
1018 | void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn) | |
1019 | { | |
1020 | if (conn == NULL) | |
1021 | return; | |
1022 | SSL_free(conn->ssl); | |
1023 | tls_engine_deinit(conn); | |
1024 | os_free(conn->subject_match); | |
1025 | os_free(conn->altsubject_match); | |
01f809c7 | 1026 | os_free(conn->suffix_match); |
6fc6879b JM |
1027 | os_free(conn->session_ticket); |
1028 | os_free(conn); | |
1029 | } | |
1030 | ||
1031 | ||
1032 | int tls_connection_established(void *ssl_ctx, struct tls_connection *conn) | |
1033 | { | |
1034 | return conn ? SSL_is_init_finished(conn->ssl) : 0; | |
1035 | } | |
1036 | ||
1037 | ||
1038 | int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn) | |
1039 | { | |
1040 | if (conn == NULL) | |
1041 | return -1; | |
1042 | ||
1043 | /* Shutdown previous TLS connection without notifying the peer | |
1044 | * because the connection was already terminated in practice | |
1045 | * and "close notify" shutdown alert would confuse AS. */ | |
1046 | SSL_set_quiet_shutdown(conn->ssl, 1); | |
1047 | SSL_shutdown(conn->ssl); | |
1048 | return 0; | |
1049 | } | |
1050 | ||
1051 | ||
1052 | static int tls_match_altsubject_component(X509 *cert, int type, | |
1053 | const char *value, size_t len) | |
1054 | { | |
1055 | GENERAL_NAME *gen; | |
1056 | void *ext; | |
1057 | int i, found = 0; | |
1058 | ||
1059 | ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); | |
1060 | ||
1061 | for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) { | |
1062 | gen = sk_GENERAL_NAME_value(ext, i); | |
1063 | if (gen->type != type) | |
1064 | continue; | |
1065 | if (os_strlen((char *) gen->d.ia5->data) == len && | |
1066 | os_memcmp(value, gen->d.ia5->data, len) == 0) | |
1067 | found++; | |
1068 | } | |
1069 | ||
1070 | return found; | |
1071 | } | |
1072 | ||
1073 | ||
1074 | static int tls_match_altsubject(X509 *cert, const char *match) | |
1075 | { | |
1076 | int type; | |
1077 | const char *pos, *end; | |
1078 | size_t len; | |
1079 | ||
1080 | pos = match; | |
1081 | do { | |
1082 | if (os_strncmp(pos, "EMAIL:", 6) == 0) { | |
1083 | type = GEN_EMAIL; | |
1084 | pos += 6; | |
1085 | } else if (os_strncmp(pos, "DNS:", 4) == 0) { | |
1086 | type = GEN_DNS; | |
1087 | pos += 4; | |
1088 | } else if (os_strncmp(pos, "URI:", 4) == 0) { | |
1089 | type = GEN_URI; | |
1090 | pos += 4; | |
1091 | } else { | |
1092 | wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName " | |
1093 | "match '%s'", pos); | |
1094 | return 0; | |
1095 | } | |
1096 | end = os_strchr(pos, ';'); | |
1097 | while (end) { | |
1098 | if (os_strncmp(end + 1, "EMAIL:", 6) == 0 || | |
1099 | os_strncmp(end + 1, "DNS:", 4) == 0 || | |
1100 | os_strncmp(end + 1, "URI:", 4) == 0) | |
1101 | break; | |
1102 | end = os_strchr(end + 1, ';'); | |
1103 | } | |
1104 | if (end) | |
1105 | len = end - pos; | |
1106 | else | |
1107 | len = os_strlen(pos); | |
1108 | if (tls_match_altsubject_component(cert, type, pos, len) > 0) | |
1109 | return 1; | |
1110 | pos = end + 1; | |
1111 | } while (end); | |
1112 | ||
1113 | return 0; | |
1114 | } | |
1115 | ||
1116 | ||
01f809c7 JM |
1117 | static int domain_suffix_match(const u8 *val, size_t len, const char *match) |
1118 | { | |
1119 | size_t i, match_len; | |
1120 | ||
1121 | /* Check for embedded nuls that could mess up suffix matching */ | |
1122 | for (i = 0; i < len; i++) { | |
1123 | if (val[i] == '\0') { | |
1124 | wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject"); | |
1125 | return 0; | |
1126 | } | |
1127 | } | |
1128 | ||
1129 | match_len = os_strlen(match); | |
1130 | if (match_len > len) | |
1131 | return 0; | |
1132 | ||
1133 | if (os_strncasecmp((const char *) val + len - match_len, match, | |
1134 | match_len) != 0) | |
1135 | return 0; /* no match */ | |
1136 | ||
1137 | if (match_len == len) | |
1138 | return 1; /* exact match */ | |
1139 | ||
1140 | if (val[len - match_len - 1] == '.') | |
1141 | return 1; /* full label match completes suffix match */ | |
1142 | ||
1143 | wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match"); | |
1144 | return 0; | |
1145 | } | |
1146 | ||
1147 | ||
1148 | static int tls_match_suffix(X509 *cert, const char *match) | |
1149 | { | |
1150 | GENERAL_NAME *gen; | |
1151 | void *ext; | |
1152 | int i; | |
1153 | int dns_name = 0; | |
1154 | X509_NAME *name; | |
1155 | ||
1156 | wpa_printf(MSG_DEBUG, "TLS: Match domain against suffix %s", match); | |
1157 | ||
1158 | ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); | |
1159 | ||
1160 | for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) { | |
1161 | gen = sk_GENERAL_NAME_value(ext, i); | |
1162 | if (gen->type != GEN_DNS) | |
1163 | continue; | |
1164 | dns_name++; | |
1165 | wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName", | |
1166 | gen->d.dNSName->data, | |
1167 | gen->d.dNSName->length); | |
1168 | if (domain_suffix_match(gen->d.dNSName->data, | |
1169 | gen->d.dNSName->length, match) == 1) { | |
1170 | wpa_printf(MSG_DEBUG, "TLS: Suffix match in dNSName found"); | |
1171 | return 1; | |
1172 | } | |
1173 | } | |
1174 | ||
1175 | if (dns_name) { | |
1176 | wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched"); | |
1177 | return 0; | |
1178 | } | |
1179 | ||
1180 | name = X509_get_subject_name(cert); | |
1181 | i = -1; | |
1182 | for (;;) { | |
1183 | X509_NAME_ENTRY *e; | |
1184 | ASN1_STRING *cn; | |
1185 | ||
1186 | i = X509_NAME_get_index_by_NID(name, NID_commonName, i); | |
1187 | if (i == -1) | |
1188 | break; | |
1189 | e = X509_NAME_get_entry(name, i); | |
1190 | if (e == NULL) | |
1191 | continue; | |
1192 | cn = X509_NAME_ENTRY_get_data(e); | |
1193 | if (cn == NULL) | |
1194 | continue; | |
1195 | wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName", | |
1196 | cn->data, cn->length); | |
1197 | if (domain_suffix_match(cn->data, cn->length, match) == 1) { | |
1198 | wpa_printf(MSG_DEBUG, "TLS: Suffix match in commonName found"); | |
1199 | return 1; | |
1200 | } | |
1201 | } | |
1202 | ||
1203 | wpa_printf(MSG_DEBUG, "TLS: No CommonName suffix match found"); | |
1204 | return 0; | |
1205 | } | |
1206 | ||
1207 | ||
00468b46 JM |
1208 | static enum tls_fail_reason openssl_tls_fail_reason(int err) |
1209 | { | |
1210 | switch (err) { | |
1211 | case X509_V_ERR_CERT_REVOKED: | |
1212 | return TLS_FAIL_REVOKED; | |
1213 | case X509_V_ERR_CERT_NOT_YET_VALID: | |
1214 | case X509_V_ERR_CRL_NOT_YET_VALID: | |
1215 | return TLS_FAIL_NOT_YET_VALID; | |
1216 | case X509_V_ERR_CERT_HAS_EXPIRED: | |
1217 | case X509_V_ERR_CRL_HAS_EXPIRED: | |
1218 | return TLS_FAIL_EXPIRED; | |
1219 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: | |
1220 | case X509_V_ERR_UNABLE_TO_GET_CRL: | |
1221 | case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER: | |
1222 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: | |
1223 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: | |
1224 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: | |
1225 | case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: | |
1226 | case X509_V_ERR_CERT_CHAIN_TOO_LONG: | |
1227 | case X509_V_ERR_PATH_LENGTH_EXCEEDED: | |
1228 | case X509_V_ERR_INVALID_CA: | |
1229 | return TLS_FAIL_UNTRUSTED; | |
1230 | case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: | |
1231 | case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: | |
1232 | case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: | |
1233 | case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: | |
1234 | case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: | |
1235 | case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: | |
1236 | case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: | |
1237 | case X509_V_ERR_CERT_UNTRUSTED: | |
1238 | case X509_V_ERR_CERT_REJECTED: | |
1239 | return TLS_FAIL_BAD_CERTIFICATE; | |
1240 | default: | |
1241 | return TLS_FAIL_UNSPECIFIED; | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | ||
1246 | static struct wpabuf * get_x509_cert(X509 *cert) | |
1247 | { | |
1248 | struct wpabuf *buf; | |
1249 | u8 *tmp; | |
1250 | ||
1251 | int cert_len = i2d_X509(cert, NULL); | |
1252 | if (cert_len <= 0) | |
1253 | return NULL; | |
1254 | ||
1255 | buf = wpabuf_alloc(cert_len); | |
1256 | if (buf == NULL) | |
1257 | return NULL; | |
1258 | ||
1259 | tmp = wpabuf_put(buf, cert_len); | |
1260 | i2d_X509(cert, &tmp); | |
1261 | return buf; | |
1262 | } | |
1263 | ||
1264 | ||
1265 | static void openssl_tls_fail_event(struct tls_connection *conn, | |
1266 | X509 *err_cert, int err, int depth, | |
1267 | const char *subject, const char *err_str, | |
1268 | enum tls_fail_reason reason) | |
1269 | { | |
1270 | union tls_event_data ev; | |
1271 | struct wpabuf *cert = NULL; | |
7c0e1e27 | 1272 | struct tls_context *context = conn->context; |
00468b46 | 1273 | |
7c0e1e27 | 1274 | if (context->event_cb == NULL) |
00468b46 JM |
1275 | return; |
1276 | ||
1277 | cert = get_x509_cert(err_cert); | |
1278 | os_memset(&ev, 0, sizeof(ev)); | |
1279 | ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ? | |
1280 | reason : openssl_tls_fail_reason(err); | |
1281 | ev.cert_fail.depth = depth; | |
1282 | ev.cert_fail.subject = subject; | |
1283 | ev.cert_fail.reason_txt = err_str; | |
1284 | ev.cert_fail.cert = cert; | |
7c0e1e27 | 1285 | context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev); |
00468b46 JM |
1286 | wpabuf_free(cert); |
1287 | } | |
1288 | ||
1289 | ||
1290 | static void openssl_tls_cert_event(struct tls_connection *conn, | |
1291 | X509 *err_cert, int depth, | |
1292 | const char *subject) | |
1293 | { | |
1294 | struct wpabuf *cert = NULL; | |
1295 | union tls_event_data ev; | |
7c0e1e27 | 1296 | struct tls_context *context = conn->context; |
00468b46 JM |
1297 | #ifdef CONFIG_SHA256 |
1298 | u8 hash[32]; | |
1299 | #endif /* CONFIG_SHA256 */ | |
1300 | ||
7c0e1e27 | 1301 | if (context->event_cb == NULL) |
00468b46 JM |
1302 | return; |
1303 | ||
1304 | os_memset(&ev, 0, sizeof(ev)); | |
7c0e1e27 | 1305 | if (conn->cert_probe || context->cert_in_cb) { |
00468b46 JM |
1306 | cert = get_x509_cert(err_cert); |
1307 | ev.peer_cert.cert = cert; | |
1308 | } | |
1309 | #ifdef CONFIG_SHA256 | |
1310 | if (cert) { | |
1311 | const u8 *addr[1]; | |
1312 | size_t len[1]; | |
1313 | addr[0] = wpabuf_head(cert); | |
1314 | len[0] = wpabuf_len(cert); | |
1315 | if (sha256_vector(1, addr, len, hash) == 0) { | |
1316 | ev.peer_cert.hash = hash; | |
1317 | ev.peer_cert.hash_len = sizeof(hash); | |
1318 | } | |
1319 | } | |
1320 | #endif /* CONFIG_SHA256 */ | |
1321 | ev.peer_cert.depth = depth; | |
1322 | ev.peer_cert.subject = subject; | |
7c0e1e27 | 1323 | context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev); |
00468b46 JM |
1324 | wpabuf_free(cert); |
1325 | } | |
1326 | ||
1327 | ||
6fc6879b JM |
1328 | static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx) |
1329 | { | |
1330 | char buf[256]; | |
1331 | X509 *err_cert; | |
1332 | int err, depth; | |
1333 | SSL *ssl; | |
1334 | struct tls_connection *conn; | |
7c0e1e27 | 1335 | struct tls_context *context; |
01f809c7 | 1336 | char *match, *altmatch, *suffix_match; |
00468b46 | 1337 | const char *err_str; |
6fc6879b JM |
1338 | |
1339 | err_cert = X509_STORE_CTX_get_current_cert(x509_ctx); | |
1340 | err = X509_STORE_CTX_get_error(x509_ctx); | |
1341 | depth = X509_STORE_CTX_get_error_depth(x509_ctx); | |
1342 | ssl = X509_STORE_CTX_get_ex_data(x509_ctx, | |
1343 | SSL_get_ex_data_X509_STORE_CTX_idx()); | |
1344 | X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf)); | |
1345 | ||
1346 | conn = SSL_get_app_data(ssl); | |
0bdaa741 JM |
1347 | if (conn == NULL) |
1348 | return 0; | |
080585c0 JM |
1349 | |
1350 | if (depth == 0) | |
1351 | conn->peer_cert = err_cert; | |
1352 | else if (depth == 1) | |
1353 | conn->peer_issuer = err_cert; | |
1354 | ||
7c0e1e27 | 1355 | context = conn->context; |
0bdaa741 JM |
1356 | match = conn->subject_match; |
1357 | altmatch = conn->altsubject_match; | |
01f809c7 | 1358 | suffix_match = conn->suffix_match; |
6fc6879b | 1359 | |
00468b46 JM |
1360 | if (!preverify_ok && !conn->ca_cert_verify) |
1361 | preverify_ok = 1; | |
1362 | if (!preverify_ok && depth > 0 && conn->server_cert_only) | |
1363 | preverify_ok = 1; | |
235279e7 JM |
1364 | if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) && |
1365 | (err == X509_V_ERR_CERT_HAS_EXPIRED || | |
1366 | err == X509_V_ERR_CERT_NOT_YET_VALID)) { | |
1367 | wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity " | |
1368 | "time mismatch"); | |
1369 | preverify_ok = 1; | |
1370 | } | |
00468b46 JM |
1371 | |
1372 | err_str = X509_verify_cert_error_string(err); | |
1373 | ||
1374 | #ifdef CONFIG_SHA256 | |
1375 | if (preverify_ok && depth == 0 && conn->server_cert_only) { | |
1376 | struct wpabuf *cert; | |
1377 | cert = get_x509_cert(err_cert); | |
1378 | if (!cert) { | |
1379 | wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch " | |
1380 | "server certificate data"); | |
6fc6879b | 1381 | preverify_ok = 0; |
00468b46 JM |
1382 | } else { |
1383 | u8 hash[32]; | |
1384 | const u8 *addr[1]; | |
1385 | size_t len[1]; | |
1386 | addr[0] = wpabuf_head(cert); | |
1387 | len[0] = wpabuf_len(cert); | |
1388 | if (sha256_vector(1, addr, len, hash) < 0 || | |
1389 | os_memcmp(conn->srv_cert_hash, hash, 32) != 0) { | |
1390 | err_str = "Server certificate mismatch"; | |
1391 | err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN; | |
1392 | preverify_ok = 0; | |
1393 | } | |
1394 | wpabuf_free(cert); | |
6fc6879b JM |
1395 | } |
1396 | } | |
00468b46 JM |
1397 | #endif /* CONFIG_SHA256 */ |
1398 | ||
1399 | if (!preverify_ok) { | |
1400 | wpa_printf(MSG_WARNING, "TLS: Certificate verification failed," | |
1401 | " error %d (%s) depth %d for '%s'", err, err_str, | |
1402 | depth, buf); | |
1403 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, | |
1404 | err_str, TLS_FAIL_UNSPECIFIED); | |
1405 | return preverify_ok; | |
1406 | } | |
1407 | ||
1408 | wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d " | |
1409 | "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'", | |
1410 | preverify_ok, err, err_str, | |
1411 | conn->ca_cert_verify, depth, buf); | |
1412 | if (depth == 0 && match && os_strstr(buf, match) == NULL) { | |
1413 | wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not " | |
1414 | "match with '%s'", buf, match); | |
1415 | preverify_ok = 0; | |
1416 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, | |
1417 | "Subject mismatch", | |
1418 | TLS_FAIL_SUBJECT_MISMATCH); | |
1419 | } else if (depth == 0 && altmatch && | |
1420 | !tls_match_altsubject(err_cert, altmatch)) { | |
1421 | wpa_printf(MSG_WARNING, "TLS: altSubjectName match " | |
1422 | "'%s' not found", altmatch); | |
1423 | preverify_ok = 0; | |
1424 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, | |
1425 | "AltSubject mismatch", | |
1426 | TLS_FAIL_ALTSUBJECT_MISMATCH); | |
01f809c7 JM |
1427 | } else if (depth == 0 && suffix_match && |
1428 | !tls_match_suffix(err_cert, suffix_match)) { | |
1429 | wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found", | |
1430 | suffix_match); | |
1431 | preverify_ok = 0; | |
1432 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, | |
1433 | "Domain suffix mismatch", | |
1434 | TLS_FAIL_DOMAIN_SUFFIX_MISMATCH); | |
00468b46 JM |
1435 | } else |
1436 | openssl_tls_cert_event(conn, err_cert, depth, buf); | |
1437 | ||
1438 | if (conn->cert_probe && preverify_ok && depth == 0) { | |
1439 | wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate " | |
1440 | "on probe-only run"); | |
1441 | preverify_ok = 0; | |
1442 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, | |
1443 | "Server certificate chain probe", | |
1444 | TLS_FAIL_SERVER_CHAIN_PROBE); | |
1445 | } | |
6fc6879b | 1446 | |
7c0e1e27 PS |
1447 | if (preverify_ok && context->event_cb != NULL) |
1448 | context->event_cb(context->cb_ctx, | |
1449 | TLS_CERT_CHAIN_SUCCESS, NULL); | |
dd7fec1f | 1450 | |
6fc6879b JM |
1451 | return preverify_ok; |
1452 | } | |
1453 | ||
1454 | ||
1455 | #ifndef OPENSSL_NO_STDIO | |
1456 | static int tls_load_ca_der(void *_ssl_ctx, const char *ca_cert) | |
1457 | { | |
1458 | SSL_CTX *ssl_ctx = _ssl_ctx; | |
1459 | X509_LOOKUP *lookup; | |
1460 | int ret = 0; | |
1461 | ||
1462 | lookup = X509_STORE_add_lookup(ssl_ctx->cert_store, | |
1463 | X509_LOOKUP_file()); | |
1464 | if (lookup == NULL) { | |
1465 | tls_show_errors(MSG_WARNING, __func__, | |
1466 | "Failed add lookup for X509 store"); | |
1467 | return -1; | |
1468 | } | |
1469 | ||
1470 | if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) { | |
1471 | unsigned long err = ERR_peek_error(); | |
1472 | tls_show_errors(MSG_WARNING, __func__, | |
1473 | "Failed load CA in DER format"); | |
1474 | if (ERR_GET_LIB(err) == ERR_LIB_X509 && | |
1475 | ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) { | |
1476 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring " | |
1477 | "cert already in hash table error", | |
1478 | __func__); | |
1479 | } else | |
1480 | ret = -1; | |
1481 | } | |
1482 | ||
1483 | return ret; | |
1484 | } | |
1485 | #endif /* OPENSSL_NO_STDIO */ | |
1486 | ||
1487 | ||
2a0cd067 DS |
1488 | #ifdef ANDROID |
1489 | static BIO * BIO_from_keystore(const char *key) | |
1490 | { | |
1491 | BIO *bio = NULL; | |
1492 | char value[KEYSTORE_MESSAGE_SIZE]; | |
1493 | int length = keystore_get(key, strlen(key), value); | |
1494 | if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL) | |
1495 | BIO_write(bio, value, length); | |
1496 | return bio; | |
1497 | } | |
1498 | #endif /* ANDROID */ | |
1499 | ||
1500 | ||
6fc6879b JM |
1501 | static int tls_connection_ca_cert(void *_ssl_ctx, struct tls_connection *conn, |
1502 | const char *ca_cert, const u8 *ca_cert_blob, | |
1503 | size_t ca_cert_blob_len, const char *ca_path) | |
1504 | { | |
1505 | SSL_CTX *ssl_ctx = _ssl_ctx; | |
1506 | ||
1507 | /* | |
1508 | * Remove previously configured trusted CA certificates before adding | |
1509 | * new ones. | |
1510 | */ | |
1511 | X509_STORE_free(ssl_ctx->cert_store); | |
1512 | ssl_ctx->cert_store = X509_STORE_new(); | |
1513 | if (ssl_ctx->cert_store == NULL) { | |
1514 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new " | |
1515 | "certificate store", __func__); | |
1516 | return -1; | |
1517 | } | |
1518 | ||
00468b46 JM |
1519 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb); |
1520 | conn->ca_cert_verify = 1; | |
1521 | ||
1522 | if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) { | |
1523 | wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate " | |
1524 | "chain"); | |
1525 | conn->cert_probe = 1; | |
1526 | conn->ca_cert_verify = 0; | |
1527 | return 0; | |
1528 | } | |
1529 | ||
1530 | if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) { | |
1531 | #ifdef CONFIG_SHA256 | |
1532 | const char *pos = ca_cert + 7; | |
1533 | if (os_strncmp(pos, "server/sha256/", 14) != 0) { | |
1534 | wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert " | |
1535 | "hash value '%s'", ca_cert); | |
1536 | return -1; | |
1537 | } | |
1538 | pos += 14; | |
1539 | if (os_strlen(pos) != 32 * 2) { | |
1540 | wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 " | |
1541 | "hash length in ca_cert '%s'", ca_cert); | |
1542 | return -1; | |
1543 | } | |
1544 | if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) { | |
1545 | wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash " | |
1546 | "value in ca_cert '%s'", ca_cert); | |
1547 | return -1; | |
1548 | } | |
1549 | conn->server_cert_only = 1; | |
1550 | wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server " | |
1551 | "certificate match"); | |
1552 | return 0; | |
1553 | #else /* CONFIG_SHA256 */ | |
1554 | wpa_printf(MSG_INFO, "No SHA256 included in the build - " | |
1555 | "cannot validate server certificate hash"); | |
1556 | return -1; | |
1557 | #endif /* CONFIG_SHA256 */ | |
1558 | } | |
1559 | ||
6fc6879b JM |
1560 | if (ca_cert_blob) { |
1561 | X509 *cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &ca_cert_blob, | |
1562 | ca_cert_blob_len); | |
1563 | if (cert == NULL) { | |
1564 | tls_show_errors(MSG_WARNING, __func__, | |
1565 | "Failed to parse ca_cert_blob"); | |
1566 | return -1; | |
1567 | } | |
1568 | ||
1569 | if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) { | |
1570 | unsigned long err = ERR_peek_error(); | |
1571 | tls_show_errors(MSG_WARNING, __func__, | |
1572 | "Failed to add ca_cert_blob to " | |
1573 | "certificate store"); | |
1574 | if (ERR_GET_LIB(err) == ERR_LIB_X509 && | |
1575 | ERR_GET_REASON(err) == | |
1576 | X509_R_CERT_ALREADY_IN_HASH_TABLE) { | |
1577 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring " | |
1578 | "cert already in hash table error", | |
1579 | __func__); | |
1580 | } else { | |
1581 | X509_free(cert); | |
1582 | return -1; | |
1583 | } | |
1584 | } | |
1585 | X509_free(cert); | |
1586 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob " | |
1587 | "to certificate store", __func__); | |
6fc6879b JM |
1588 | return 0; |
1589 | } | |
1590 | ||
2a0cd067 DS |
1591 | #ifdef ANDROID |
1592 | if (ca_cert && os_strncmp("keystore://", ca_cert, 11) == 0) { | |
1593 | BIO *bio = BIO_from_keystore(&ca_cert[11]); | |
1594 | STACK_OF(X509_INFO) *stack = NULL; | |
1595 | int i; | |
1596 | ||
1597 | if (bio) { | |
1598 | stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL); | |
1599 | BIO_free(bio); | |
1600 | } | |
1601 | if (!stack) | |
1602 | return -1; | |
1603 | ||
1604 | for (i = 0; i < sk_X509_INFO_num(stack); ++i) { | |
1605 | X509_INFO *info = sk_X509_INFO_value(stack, i); | |
1606 | if (info->x509) { | |
1607 | X509_STORE_add_cert(ssl_ctx->cert_store, | |
1608 | info->x509); | |
1609 | } | |
1610 | if (info->crl) { | |
1611 | X509_STORE_add_crl(ssl_ctx->cert_store, | |
1612 | info->crl); | |
1613 | } | |
1614 | } | |
1615 | sk_X509_INFO_pop_free(stack, X509_INFO_free); | |
1616 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb); | |
1617 | return 0; | |
1618 | } | |
1619 | #endif /* ANDROID */ | |
1620 | ||
6fc6879b JM |
1621 | #ifdef CONFIG_NATIVE_WINDOWS |
1622 | if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) == | |
1623 | 0) { | |
1624 | wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from " | |
1625 | "system certificate store"); | |
6fc6879b JM |
1626 | return 0; |
1627 | } | |
1628 | #endif /* CONFIG_NATIVE_WINDOWS */ | |
1629 | ||
1630 | if (ca_cert || ca_path) { | |
1631 | #ifndef OPENSSL_NO_STDIO | |
1632 | if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) != | |
1633 | 1) { | |
1634 | tls_show_errors(MSG_WARNING, __func__, | |
1635 | "Failed to load root certificates"); | |
1636 | if (ca_cert && | |
1637 | tls_load_ca_der(ssl_ctx, ca_cert) == 0) { | |
1638 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded " | |
1639 | "DER format CA certificate", | |
1640 | __func__); | |
1641 | } else | |
1642 | return -1; | |
1643 | } else { | |
1644 | wpa_printf(MSG_DEBUG, "TLS: Trusted root " | |
1645 | "certificate(s) loaded"); | |
1646 | tls_get_errors(ssl_ctx); | |
1647 | } | |
6fc6879b JM |
1648 | #else /* OPENSSL_NO_STDIO */ |
1649 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", | |
1650 | __func__); | |
1651 | return -1; | |
1652 | #endif /* OPENSSL_NO_STDIO */ | |
1653 | } else { | |
1654 | /* No ca_cert configured - do not try to verify server | |
1655 | * certificate */ | |
00468b46 | 1656 | conn->ca_cert_verify = 0; |
6fc6879b JM |
1657 | } |
1658 | ||
1659 | return 0; | |
1660 | } | |
1661 | ||
1662 | ||
1663 | static int tls_global_ca_cert(SSL_CTX *ssl_ctx, const char *ca_cert) | |
1664 | { | |
1665 | if (ca_cert) { | |
1666 | if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1) | |
1667 | { | |
1668 | tls_show_errors(MSG_WARNING, __func__, | |
1669 | "Failed to load root certificates"); | |
1670 | return -1; | |
1671 | } | |
1672 | ||
1673 | wpa_printf(MSG_DEBUG, "TLS: Trusted root " | |
1674 | "certificate(s) loaded"); | |
1675 | ||
1676 | #ifndef OPENSSL_NO_STDIO | |
1677 | /* Add the same CAs to the client certificate requests */ | |
1678 | SSL_CTX_set_client_CA_list(ssl_ctx, | |
1679 | SSL_load_client_CA_file(ca_cert)); | |
1680 | #endif /* OPENSSL_NO_STDIO */ | |
1681 | } | |
1682 | ||
1683 | return 0; | |
1684 | } | |
1685 | ||
1686 | ||
1687 | int tls_global_set_verify(void *ssl_ctx, int check_crl) | |
1688 | { | |
1689 | int flags; | |
1690 | ||
1691 | if (check_crl) { | |
1692 | X509_STORE *cs = SSL_CTX_get_cert_store(ssl_ctx); | |
1693 | if (cs == NULL) { | |
1694 | tls_show_errors(MSG_INFO, __func__, "Failed to get " | |
1695 | "certificate store when enabling " | |
1696 | "check_crl"); | |
1697 | return -1; | |
1698 | } | |
1699 | flags = X509_V_FLAG_CRL_CHECK; | |
1700 | if (check_crl == 2) | |
1701 | flags |= X509_V_FLAG_CRL_CHECK_ALL; | |
1702 | X509_STORE_set_flags(cs, flags); | |
1703 | } | |
1704 | return 0; | |
1705 | } | |
1706 | ||
1707 | ||
1708 | static int tls_connection_set_subject_match(struct tls_connection *conn, | |
1709 | const char *subject_match, | |
01f809c7 JM |
1710 | const char *altsubject_match, |
1711 | const char *suffix_match) | |
6fc6879b JM |
1712 | { |
1713 | os_free(conn->subject_match); | |
1714 | conn->subject_match = NULL; | |
1715 | if (subject_match) { | |
1716 | conn->subject_match = os_strdup(subject_match); | |
1717 | if (conn->subject_match == NULL) | |
1718 | return -1; | |
1719 | } | |
1720 | ||
1721 | os_free(conn->altsubject_match); | |
1722 | conn->altsubject_match = NULL; | |
1723 | if (altsubject_match) { | |
1724 | conn->altsubject_match = os_strdup(altsubject_match); | |
1725 | if (conn->altsubject_match == NULL) | |
1726 | return -1; | |
1727 | } | |
1728 | ||
01f809c7 JM |
1729 | os_free(conn->suffix_match); |
1730 | conn->suffix_match = NULL; | |
1731 | if (suffix_match) { | |
1732 | conn->suffix_match = os_strdup(suffix_match); | |
1733 | if (conn->suffix_match == NULL) | |
1734 | return -1; | |
1735 | } | |
1736 | ||
6fc6879b JM |
1737 | return 0; |
1738 | } | |
1739 | ||
1740 | ||
1741 | int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn, | |
1742 | int verify_peer) | |
1743 | { | |
bf206cad JM |
1744 | static int counter = 0; |
1745 | ||
6fc6879b JM |
1746 | if (conn == NULL) |
1747 | return -1; | |
1748 | ||
1749 | if (verify_peer) { | |
00468b46 | 1750 | conn->ca_cert_verify = 1; |
6fc6879b JM |
1751 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER | |
1752 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | | |
1753 | SSL_VERIFY_CLIENT_ONCE, tls_verify_cb); | |
1754 | } else { | |
00468b46 | 1755 | conn->ca_cert_verify = 0; |
6fc6879b JM |
1756 | SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL); |
1757 | } | |
1758 | ||
1759 | SSL_set_accept_state(conn->ssl); | |
1760 | ||
bf206cad JM |
1761 | /* |
1762 | * Set session id context in order to avoid fatal errors when client | |
1763 | * tries to resume a session. However, set the context to a unique | |
1764 | * value in order to effectively disable session resumption for now | |
1765 | * since not all areas of the server code are ready for it (e.g., | |
1766 | * EAP-TTLS needs special handling for Phase 2 after abbreviated TLS | |
1767 | * handshake). | |
1768 | */ | |
1769 | counter++; | |
1770 | SSL_set_session_id_context(conn->ssl, | |
1771 | (const unsigned char *) &counter, | |
1772 | sizeof(counter)); | |
1773 | ||
6fc6879b JM |
1774 | return 0; |
1775 | } | |
1776 | ||
1777 | ||
1778 | static int tls_connection_client_cert(struct tls_connection *conn, | |
1779 | const char *client_cert, | |
1780 | const u8 *client_cert_blob, | |
1781 | size_t client_cert_blob_len) | |
1782 | { | |
1783 | if (client_cert == NULL && client_cert_blob == NULL) | |
1784 | return 0; | |
1785 | ||
1786 | if (client_cert_blob && | |
1787 | SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob, | |
1788 | client_cert_blob_len) == 1) { | |
1789 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> " | |
1790 | "OK"); | |
1791 | return 0; | |
1792 | } else if (client_cert_blob) { | |
1793 | tls_show_errors(MSG_DEBUG, __func__, | |
1794 | "SSL_use_certificate_ASN1 failed"); | |
1795 | } | |
1796 | ||
1797 | if (client_cert == NULL) | |
1798 | return -1; | |
1799 | ||
2a0cd067 DS |
1800 | #ifdef ANDROID |
1801 | if (os_strncmp("keystore://", client_cert, 11) == 0) { | |
1802 | BIO *bio = BIO_from_keystore(&client_cert[11]); | |
1803 | X509 *x509 = NULL; | |
1804 | int ret = -1; | |
1805 | if (bio) { | |
1806 | x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL); | |
1807 | BIO_free(bio); | |
1808 | } | |
1809 | if (x509) { | |
1810 | if (SSL_use_certificate(conn->ssl, x509) == 1) | |
1811 | ret = 0; | |
1812 | X509_free(x509); | |
1813 | } | |
1814 | return ret; | |
1815 | } | |
1816 | #endif /* ANDROID */ | |
1817 | ||
6fc6879b JM |
1818 | #ifndef OPENSSL_NO_STDIO |
1819 | if (SSL_use_certificate_file(conn->ssl, client_cert, | |
1820 | SSL_FILETYPE_ASN1) == 1) { | |
1821 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)" | |
1822 | " --> OK"); | |
1823 | return 0; | |
6fc6879b JM |
1824 | } |
1825 | ||
1826 | if (SSL_use_certificate_file(conn->ssl, client_cert, | |
1827 | SSL_FILETYPE_PEM) == 1) { | |
effab86f | 1828 | ERR_clear_error(); |
6fc6879b JM |
1829 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)" |
1830 | " --> OK"); | |
1831 | return 0; | |
6fc6879b | 1832 | } |
effab86f JM |
1833 | |
1834 | tls_show_errors(MSG_DEBUG, __func__, | |
1835 | "SSL_use_certificate_file failed"); | |
6fc6879b JM |
1836 | #else /* OPENSSL_NO_STDIO */ |
1837 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__); | |
1838 | #endif /* OPENSSL_NO_STDIO */ | |
1839 | ||
1840 | return -1; | |
1841 | } | |
1842 | ||
1843 | ||
1844 | static int tls_global_client_cert(SSL_CTX *ssl_ctx, const char *client_cert) | |
1845 | { | |
1846 | #ifndef OPENSSL_NO_STDIO | |
1847 | if (client_cert == NULL) | |
1848 | return 0; | |
1849 | ||
1850 | if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert, | |
1851 | SSL_FILETYPE_ASN1) != 1 && | |
65897747 | 1852 | SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 && |
6fc6879b JM |
1853 | SSL_CTX_use_certificate_file(ssl_ctx, client_cert, |
1854 | SSL_FILETYPE_PEM) != 1) { | |
1855 | tls_show_errors(MSG_INFO, __func__, | |
1856 | "Failed to load client certificate"); | |
1857 | return -1; | |
1858 | } | |
1859 | return 0; | |
1860 | #else /* OPENSSL_NO_STDIO */ | |
1861 | if (client_cert == NULL) | |
1862 | return 0; | |
1863 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__); | |
1864 | return -1; | |
1865 | #endif /* OPENSSL_NO_STDIO */ | |
1866 | } | |
1867 | ||
1868 | ||
1869 | static int tls_passwd_cb(char *buf, int size, int rwflag, void *password) | |
1870 | { | |
1871 | if (password == NULL) { | |
1872 | return 0; | |
1873 | } | |
1874 | os_strlcpy(buf, (char *) password, size); | |
1875 | return os_strlen(buf); | |
1876 | } | |
1877 | ||
1878 | ||
1879 | #ifdef PKCS12_FUNCS | |
1880 | static int tls_parse_pkcs12(SSL_CTX *ssl_ctx, SSL *ssl, PKCS12 *p12, | |
1881 | const char *passwd) | |
1882 | { | |
1883 | EVP_PKEY *pkey; | |
1884 | X509 *cert; | |
1885 | STACK_OF(X509) *certs; | |
1886 | int res = 0; | |
1887 | char buf[256]; | |
1888 | ||
1889 | pkey = NULL; | |
1890 | cert = NULL; | |
1891 | certs = NULL; | |
1892 | if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) { | |
1893 | tls_show_errors(MSG_DEBUG, __func__, | |
1894 | "Failed to parse PKCS12 file"); | |
1895 | PKCS12_free(p12); | |
1896 | return -1; | |
1897 | } | |
1898 | wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data"); | |
1899 | ||
1900 | if (cert) { | |
1901 | X509_NAME_oneline(X509_get_subject_name(cert), buf, | |
1902 | sizeof(buf)); | |
1903 | wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: " | |
1904 | "subject='%s'", buf); | |
1905 | if (ssl) { | |
1906 | if (SSL_use_certificate(ssl, cert) != 1) | |
1907 | res = -1; | |
1908 | } else { | |
1909 | if (SSL_CTX_use_certificate(ssl_ctx, cert) != 1) | |
1910 | res = -1; | |
1911 | } | |
1912 | X509_free(cert); | |
1913 | } | |
1914 | ||
1915 | if (pkey) { | |
1916 | wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12"); | |
1917 | if (ssl) { | |
1918 | if (SSL_use_PrivateKey(ssl, pkey) != 1) | |
1919 | res = -1; | |
1920 | } else { | |
1921 | if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) != 1) | |
1922 | res = -1; | |
1923 | } | |
1924 | EVP_PKEY_free(pkey); | |
1925 | } | |
1926 | ||
1927 | if (certs) { | |
1928 | while ((cert = sk_X509_pop(certs)) != NULL) { | |
1929 | X509_NAME_oneline(X509_get_subject_name(cert), buf, | |
1930 | sizeof(buf)); | |
1931 | wpa_printf(MSG_DEBUG, "TLS: additional certificate" | |
1932 | " from PKCS12: subject='%s'", buf); | |
1933 | /* | |
1934 | * There is no SSL equivalent for the chain cert - so | |
1935 | * always add it to the context... | |
1936 | */ | |
1937 | if (SSL_CTX_add_extra_chain_cert(ssl_ctx, cert) != 1) { | |
1938 | res = -1; | |
1939 | break; | |
1940 | } | |
1941 | } | |
1942 | sk_X509_free(certs); | |
1943 | } | |
1944 | ||
1945 | PKCS12_free(p12); | |
1946 | ||
1947 | if (res < 0) | |
1948 | tls_get_errors(ssl_ctx); | |
1949 | ||
1950 | return res; | |
1951 | } | |
1952 | #endif /* PKCS12_FUNCS */ | |
1953 | ||
1954 | ||
1955 | static int tls_read_pkcs12(SSL_CTX *ssl_ctx, SSL *ssl, const char *private_key, | |
1956 | const char *passwd) | |
1957 | { | |
1958 | #ifdef PKCS12_FUNCS | |
1959 | FILE *f; | |
1960 | PKCS12 *p12; | |
1961 | ||
1962 | f = fopen(private_key, "rb"); | |
1963 | if (f == NULL) | |
1964 | return -1; | |
1965 | ||
1966 | p12 = d2i_PKCS12_fp(f, NULL); | |
1967 | fclose(f); | |
1968 | ||
1969 | if (p12 == NULL) { | |
1970 | tls_show_errors(MSG_INFO, __func__, | |
1971 | "Failed to use PKCS#12 file"); | |
1972 | return -1; | |
1973 | } | |
1974 | ||
1975 | return tls_parse_pkcs12(ssl_ctx, ssl, p12, passwd); | |
1976 | ||
1977 | #else /* PKCS12_FUNCS */ | |
1978 | wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read " | |
1979 | "p12/pfx files"); | |
1980 | return -1; | |
1981 | #endif /* PKCS12_FUNCS */ | |
1982 | } | |
1983 | ||
1984 | ||
1985 | static int tls_read_pkcs12_blob(SSL_CTX *ssl_ctx, SSL *ssl, | |
1986 | const u8 *blob, size_t len, const char *passwd) | |
1987 | { | |
1988 | #ifdef PKCS12_FUNCS | |
1989 | PKCS12 *p12; | |
1990 | ||
1991 | p12 = d2i_PKCS12(NULL, (OPENSSL_d2i_TYPE) &blob, len); | |
1992 | if (p12 == NULL) { | |
1993 | tls_show_errors(MSG_INFO, __func__, | |
1994 | "Failed to use PKCS#12 blob"); | |
1995 | return -1; | |
1996 | } | |
1997 | ||
1998 | return tls_parse_pkcs12(ssl_ctx, ssl, p12, passwd); | |
1999 | ||
2000 | #else /* PKCS12_FUNCS */ | |
2001 | wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse " | |
2002 | "p12/pfx blobs"); | |
2003 | return -1; | |
2004 | #endif /* PKCS12_FUNCS */ | |
2005 | } | |
2006 | ||
2007 | ||
e572cb63 | 2008 | #ifndef OPENSSL_NO_ENGINE |
e59c91af DS |
2009 | static int tls_engine_get_cert(struct tls_connection *conn, |
2010 | const char *cert_id, | |
2011 | X509 **cert) | |
2012 | { | |
e59c91af DS |
2013 | /* this runs after the private key is loaded so no PIN is required */ |
2014 | struct { | |
2015 | const char *cert_id; | |
2016 | X509 *cert; | |
2017 | } params; | |
2018 | params.cert_id = cert_id; | |
2019 | params.cert = NULL; | |
2020 | ||
2021 | if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL", | |
2022 | 0, ¶ms, NULL, 1)) { | |
2023 | wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id" | |
2024 | " '%s' [%s]", cert_id, | |
2025 | ERR_error_string(ERR_get_error(), NULL)); | |
2026 | return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; | |
2027 | } | |
2028 | if (!params.cert) { | |
2029 | wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id" | |
2030 | " '%s'", cert_id); | |
2031 | return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; | |
2032 | } | |
2033 | *cert = params.cert; | |
2034 | return 0; | |
e59c91af | 2035 | } |
e572cb63 | 2036 | #endif /* OPENSSL_NO_ENGINE */ |
e59c91af DS |
2037 | |
2038 | ||
2039 | static int tls_connection_engine_client_cert(struct tls_connection *conn, | |
2040 | const char *cert_id) | |
2041 | { | |
2042 | #ifndef OPENSSL_NO_ENGINE | |
2043 | X509 *cert; | |
2044 | ||
2045 | if (tls_engine_get_cert(conn, cert_id, &cert)) | |
2046 | return -1; | |
2047 | ||
2048 | if (!SSL_use_certificate(conn->ssl, cert)) { | |
2049 | tls_show_errors(MSG_ERROR, __func__, | |
2050 | "SSL_use_certificate failed"); | |
2051 | X509_free(cert); | |
2052 | return -1; | |
2053 | } | |
2054 | X509_free(cert); | |
2055 | wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> " | |
2056 | "OK"); | |
2057 | return 0; | |
2058 | ||
2059 | #else /* OPENSSL_NO_ENGINE */ | |
2060 | return -1; | |
2061 | #endif /* OPENSSL_NO_ENGINE */ | |
2062 | } | |
2063 | ||
2064 | ||
2065 | static int tls_connection_engine_ca_cert(void *_ssl_ctx, | |
2066 | struct tls_connection *conn, | |
2067 | const char *ca_cert_id) | |
2068 | { | |
2069 | #ifndef OPENSSL_NO_ENGINE | |
2070 | X509 *cert; | |
2071 | SSL_CTX *ssl_ctx = _ssl_ctx; | |
2072 | ||
2073 | if (tls_engine_get_cert(conn, ca_cert_id, &cert)) | |
2074 | return -1; | |
2075 | ||
2076 | /* start off the same as tls_connection_ca_cert */ | |
2077 | X509_STORE_free(ssl_ctx->cert_store); | |
2078 | ssl_ctx->cert_store = X509_STORE_new(); | |
2079 | if (ssl_ctx->cert_store == NULL) { | |
2080 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new " | |
2081 | "certificate store", __func__); | |
2082 | X509_free(cert); | |
2083 | return -1; | |
2084 | } | |
2085 | if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) { | |
2086 | unsigned long err = ERR_peek_error(); | |
2087 | tls_show_errors(MSG_WARNING, __func__, | |
2088 | "Failed to add CA certificate from engine " | |
2089 | "to certificate store"); | |
2090 | if (ERR_GET_LIB(err) == ERR_LIB_X509 && | |
2091 | ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) { | |
2092 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert" | |
2093 | " already in hash table error", | |
2094 | __func__); | |
2095 | } else { | |
2096 | X509_free(cert); | |
2097 | return -1; | |
2098 | } | |
2099 | } | |
2100 | X509_free(cert); | |
2101 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine " | |
2102 | "to certificate store", __func__); | |
2103 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb); | |
d8858cad CW |
2104 | conn->ca_cert_verify = 1; |
2105 | ||
e59c91af DS |
2106 | return 0; |
2107 | ||
2108 | #else /* OPENSSL_NO_ENGINE */ | |
2109 | return -1; | |
2110 | #endif /* OPENSSL_NO_ENGINE */ | |
2111 | } | |
2112 | ||
2113 | ||
6fc6879b JM |
2114 | static int tls_connection_engine_private_key(struct tls_connection *conn) |
2115 | { | |
2116 | #ifndef OPENSSL_NO_ENGINE | |
2117 | if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) { | |
2118 | tls_show_errors(MSG_ERROR, __func__, | |
2119 | "ENGINE: cannot use private key for TLS"); | |
2120 | return -1; | |
2121 | } | |
2122 | if (!SSL_check_private_key(conn->ssl)) { | |
2123 | tls_show_errors(MSG_INFO, __func__, | |
2124 | "Private key failed verification"); | |
2125 | return -1; | |
2126 | } | |
2127 | return 0; | |
2128 | #else /* OPENSSL_NO_ENGINE */ | |
2129 | wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but " | |
2130 | "engine support was not compiled in"); | |
2131 | return -1; | |
2132 | #endif /* OPENSSL_NO_ENGINE */ | |
2133 | } | |
2134 | ||
2135 | ||
2136 | static int tls_connection_private_key(void *_ssl_ctx, | |
2137 | struct tls_connection *conn, | |
2138 | const char *private_key, | |
2139 | const char *private_key_passwd, | |
2140 | const u8 *private_key_blob, | |
2141 | size_t private_key_blob_len) | |
2142 | { | |
2143 | SSL_CTX *ssl_ctx = _ssl_ctx; | |
2144 | char *passwd; | |
2145 | int ok; | |
2146 | ||
2147 | if (private_key == NULL && private_key_blob == NULL) | |
2148 | return 0; | |
2149 | ||
2150 | if (private_key_passwd) { | |
2151 | passwd = os_strdup(private_key_passwd); | |
2152 | if (passwd == NULL) | |
2153 | return -1; | |
2154 | } else | |
2155 | passwd = NULL; | |
2156 | ||
2157 | SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb); | |
2158 | SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd); | |
2159 | ||
2160 | ok = 0; | |
2161 | while (private_key_blob) { | |
2162 | if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl, | |
2163 | (u8 *) private_key_blob, | |
2164 | private_key_blob_len) == 1) { | |
2165 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_" | |
2166 | "ASN1(EVP_PKEY_RSA) --> OK"); | |
2167 | ok = 1; | |
2168 | break; | |
6fc6879b JM |
2169 | } |
2170 | ||
2171 | if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl, | |
2172 | (u8 *) private_key_blob, | |
2173 | private_key_blob_len) == 1) { | |
2174 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_" | |
2175 | "ASN1(EVP_PKEY_DSA) --> OK"); | |
2176 | ok = 1; | |
2177 | break; | |
6fc6879b JM |
2178 | } |
2179 | ||
2180 | if (SSL_use_RSAPrivateKey_ASN1(conn->ssl, | |
2181 | (u8 *) private_key_blob, | |
2182 | private_key_blob_len) == 1) { | |
2183 | wpa_printf(MSG_DEBUG, "OpenSSL: " | |
2184 | "SSL_use_RSAPrivateKey_ASN1 --> OK"); | |
2185 | ok = 1; | |
2186 | break; | |
6fc6879b JM |
2187 | } |
2188 | ||
2189 | if (tls_read_pkcs12_blob(ssl_ctx, conn->ssl, private_key_blob, | |
2190 | private_key_blob_len, passwd) == 0) { | |
2191 | wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> " | |
2192 | "OK"); | |
2193 | ok = 1; | |
2194 | break; | |
2195 | } | |
2196 | ||
2197 | break; | |
2198 | } | |
2199 | ||
2a0cd067 DS |
2200 | #ifdef ANDROID |
2201 | if (!ok && private_key && | |
2202 | os_strncmp("keystore://", private_key, 11) == 0) { | |
2203 | BIO *bio = BIO_from_keystore(&private_key[11]); | |
2204 | EVP_PKEY *pkey = NULL; | |
2205 | if (bio) { | |
2206 | pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); | |
2207 | BIO_free(bio); | |
2208 | } | |
2209 | if (pkey) { | |
2210 | if (SSL_use_PrivateKey(conn->ssl, pkey) == 1) { | |
2211 | wpa_printf(MSG_DEBUG, "OpenSSL: Private key " | |
2212 | "from keystore"); | |
2213 | ok = 1; | |
2214 | } | |
2215 | EVP_PKEY_free(pkey); | |
2216 | } | |
2217 | } | |
2218 | #endif /* ANDROID */ | |
2219 | ||
6fc6879b JM |
2220 | while (!ok && private_key) { |
2221 | #ifndef OPENSSL_NO_STDIO | |
2222 | if (SSL_use_PrivateKey_file(conn->ssl, private_key, | |
2223 | SSL_FILETYPE_ASN1) == 1) { | |
2224 | wpa_printf(MSG_DEBUG, "OpenSSL: " | |
2225 | "SSL_use_PrivateKey_File (DER) --> OK"); | |
2226 | ok = 1; | |
2227 | break; | |
6fc6879b JM |
2228 | } |
2229 | ||
2230 | if (SSL_use_PrivateKey_file(conn->ssl, private_key, | |
2231 | SSL_FILETYPE_PEM) == 1) { | |
2232 | wpa_printf(MSG_DEBUG, "OpenSSL: " | |
2233 | "SSL_use_PrivateKey_File (PEM) --> OK"); | |
2234 | ok = 1; | |
2235 | break; | |
6fc6879b JM |
2236 | } |
2237 | #else /* OPENSSL_NO_STDIO */ | |
2238 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", | |
2239 | __func__); | |
2240 | #endif /* OPENSSL_NO_STDIO */ | |
2241 | ||
2242 | if (tls_read_pkcs12(ssl_ctx, conn->ssl, private_key, passwd) | |
2243 | == 0) { | |
2244 | wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file " | |
2245 | "--> OK"); | |
2246 | ok = 1; | |
2247 | break; | |
2248 | } | |
2249 | ||
2250 | if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) { | |
2251 | wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to " | |
2252 | "access certificate store --> OK"); | |
2253 | ok = 1; | |
2254 | break; | |
2255 | } | |
2256 | ||
2257 | break; | |
2258 | } | |
2259 | ||
2260 | if (!ok) { | |
effab86f JM |
2261 | tls_show_errors(MSG_INFO, __func__, |
2262 | "Failed to load private key"); | |
6fc6879b | 2263 | os_free(passwd); |
6fc6879b JM |
2264 | return -1; |
2265 | } | |
2266 | ERR_clear_error(); | |
2267 | SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL); | |
2268 | os_free(passwd); | |
55651a4b | 2269 | |
6fc6879b JM |
2270 | if (!SSL_check_private_key(conn->ssl)) { |
2271 | tls_show_errors(MSG_INFO, __func__, "Private key failed " | |
2272 | "verification"); | |
2273 | return -1; | |
2274 | } | |
2275 | ||
2276 | wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully"); | |
2277 | return 0; | |
2278 | } | |
2279 | ||
2280 | ||
2281 | static int tls_global_private_key(SSL_CTX *ssl_ctx, const char *private_key, | |
2282 | const char *private_key_passwd) | |
2283 | { | |
2284 | char *passwd; | |
2285 | ||
2286 | if (private_key == NULL) | |
2287 | return 0; | |
2288 | ||
2289 | if (private_key_passwd) { | |
2290 | passwd = os_strdup(private_key_passwd); | |
2291 | if (passwd == NULL) | |
2292 | return -1; | |
2293 | } else | |
2294 | passwd = NULL; | |
2295 | ||
2296 | SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb); | |
2297 | SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd); | |
2298 | if ( | |
2299 | #ifndef OPENSSL_NO_STDIO | |
2300 | SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key, | |
2301 | SSL_FILETYPE_ASN1) != 1 && | |
2302 | SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key, | |
2303 | SSL_FILETYPE_PEM) != 1 && | |
2304 | #endif /* OPENSSL_NO_STDIO */ | |
2305 | tls_read_pkcs12(ssl_ctx, NULL, private_key, passwd)) { | |
2306 | tls_show_errors(MSG_INFO, __func__, | |
2307 | "Failed to load private key"); | |
2308 | os_free(passwd); | |
2309 | ERR_clear_error(); | |
2310 | return -1; | |
2311 | } | |
2312 | os_free(passwd); | |
2313 | ERR_clear_error(); | |
2314 | SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL); | |
55651a4b | 2315 | |
6fc6879b JM |
2316 | if (!SSL_CTX_check_private_key(ssl_ctx)) { |
2317 | tls_show_errors(MSG_INFO, __func__, | |
2318 | "Private key failed verification"); | |
2319 | return -1; | |
2320 | } | |
2321 | ||
2322 | return 0; | |
2323 | } | |
2324 | ||
2325 | ||
2326 | static int tls_connection_dh(struct tls_connection *conn, const char *dh_file) | |
2327 | { | |
2328 | #ifdef OPENSSL_NO_DH | |
2329 | if (dh_file == NULL) | |
2330 | return 0; | |
2331 | wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but " | |
2332 | "dh_file specified"); | |
2333 | return -1; | |
2334 | #else /* OPENSSL_NO_DH */ | |
2335 | DH *dh; | |
2336 | BIO *bio; | |
2337 | ||
2338 | /* TODO: add support for dh_blob */ | |
2339 | if (dh_file == NULL) | |
2340 | return 0; | |
2341 | if (conn == NULL) | |
2342 | return -1; | |
2343 | ||
2344 | bio = BIO_new_file(dh_file, "r"); | |
2345 | if (bio == NULL) { | |
2346 | wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s", | |
2347 | dh_file, ERR_error_string(ERR_get_error(), NULL)); | |
2348 | return -1; | |
2349 | } | |
2350 | dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); | |
2351 | BIO_free(bio); | |
2352 | #ifndef OPENSSL_NO_DSA | |
2353 | while (dh == NULL) { | |
2354 | DSA *dsa; | |
2355 | wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -" | |
2356 | " trying to parse as DSA params", dh_file, | |
2357 | ERR_error_string(ERR_get_error(), NULL)); | |
2358 | bio = BIO_new_file(dh_file, "r"); | |
2359 | if (bio == NULL) | |
2360 | break; | |
2361 | dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL); | |
2362 | BIO_free(bio); | |
2363 | if (!dsa) { | |
2364 | wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file " | |
2365 | "'%s': %s", dh_file, | |
2366 | ERR_error_string(ERR_get_error(), NULL)); | |
2367 | break; | |
2368 | } | |
2369 | ||
2370 | wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format"); | |
2371 | dh = DSA_dup_DH(dsa); | |
2372 | DSA_free(dsa); | |
2373 | if (dh == NULL) { | |
2374 | wpa_printf(MSG_INFO, "TLS: Failed to convert DSA " | |
2375 | "params into DH params"); | |
2376 | break; | |
2377 | } | |
2378 | break; | |
2379 | } | |
2380 | #endif /* !OPENSSL_NO_DSA */ | |
2381 | if (dh == NULL) { | |
2382 | wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file " | |
2383 | "'%s'", dh_file); | |
2384 | return -1; | |
2385 | } | |
2386 | ||
2387 | if (SSL_set_tmp_dh(conn->ssl, dh) != 1) { | |
2388 | wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': " | |
2389 | "%s", dh_file, | |
2390 | ERR_error_string(ERR_get_error(), NULL)); | |
2391 | DH_free(dh); | |
2392 | return -1; | |
2393 | } | |
2394 | DH_free(dh); | |
2395 | return 0; | |
2396 | #endif /* OPENSSL_NO_DH */ | |
2397 | } | |
2398 | ||
2399 | ||
2400 | static int tls_global_dh(SSL_CTX *ssl_ctx, const char *dh_file) | |
2401 | { | |
2402 | #ifdef OPENSSL_NO_DH | |
2403 | if (dh_file == NULL) | |
2404 | return 0; | |
2405 | wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but " | |
2406 | "dh_file specified"); | |
2407 | return -1; | |
2408 | #else /* OPENSSL_NO_DH */ | |
2409 | DH *dh; | |
2410 | BIO *bio; | |
2411 | ||
2412 | /* TODO: add support for dh_blob */ | |
2413 | if (dh_file == NULL) | |
2414 | return 0; | |
2415 | if (ssl_ctx == NULL) | |
2416 | return -1; | |
2417 | ||
2418 | bio = BIO_new_file(dh_file, "r"); | |
2419 | if (bio == NULL) { | |
2420 | wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s", | |
2421 | dh_file, ERR_error_string(ERR_get_error(), NULL)); | |
2422 | return -1; | |
2423 | } | |
2424 | dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); | |
2425 | BIO_free(bio); | |
2426 | #ifndef OPENSSL_NO_DSA | |
2427 | while (dh == NULL) { | |
2428 | DSA *dsa; | |
2429 | wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -" | |
2430 | " trying to parse as DSA params", dh_file, | |
2431 | ERR_error_string(ERR_get_error(), NULL)); | |
2432 | bio = BIO_new_file(dh_file, "r"); | |
2433 | if (bio == NULL) | |
2434 | break; | |
2435 | dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL); | |
2436 | BIO_free(bio); | |
2437 | if (!dsa) { | |
2438 | wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file " | |
2439 | "'%s': %s", dh_file, | |
2440 | ERR_error_string(ERR_get_error(), NULL)); | |
2441 | break; | |
2442 | } | |
2443 | ||
2444 | wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format"); | |
2445 | dh = DSA_dup_DH(dsa); | |
2446 | DSA_free(dsa); | |
2447 | if (dh == NULL) { | |
2448 | wpa_printf(MSG_INFO, "TLS: Failed to convert DSA " | |
2449 | "params into DH params"); | |
2450 | break; | |
2451 | } | |
2452 | break; | |
2453 | } | |
2454 | #endif /* !OPENSSL_NO_DSA */ | |
2455 | if (dh == NULL) { | |
2456 | wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file " | |
2457 | "'%s'", dh_file); | |
2458 | return -1; | |
2459 | } | |
2460 | ||
2461 | if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) { | |
2462 | wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': " | |
2463 | "%s", dh_file, | |
2464 | ERR_error_string(ERR_get_error(), NULL)); | |
2465 | DH_free(dh); | |
2466 | return -1; | |
2467 | } | |
2468 | DH_free(dh); | |
2469 | return 0; | |
2470 | #endif /* OPENSSL_NO_DH */ | |
2471 | } | |
2472 | ||
2473 | ||
2474 | int tls_connection_get_keys(void *ssl_ctx, struct tls_connection *conn, | |
2475 | struct tls_keys *keys) | |
2476 | { | |
c9e08af2 JM |
2477 | #ifdef CONFIG_FIPS |
2478 | wpa_printf(MSG_ERROR, "OpenSSL: TLS keys cannot be exported in FIPS " | |
2479 | "mode"); | |
2480 | return -1; | |
2481 | #else /* CONFIG_FIPS */ | |
6fc6879b JM |
2482 | SSL *ssl; |
2483 | ||
2484 | if (conn == NULL || keys == NULL) | |
2485 | return -1; | |
2486 | ssl = conn->ssl; | |
2487 | if (ssl == NULL || ssl->s3 == NULL || ssl->session == NULL) | |
2488 | return -1; | |
2489 | ||
2490 | os_memset(keys, 0, sizeof(*keys)); | |
2491 | keys->master_key = ssl->session->master_key; | |
2492 | keys->master_key_len = ssl->session->master_key_length; | |
2493 | keys->client_random = ssl->s3->client_random; | |
2494 | keys->client_random_len = SSL3_RANDOM_SIZE; | |
2495 | keys->server_random = ssl->s3->server_random; | |
2496 | keys->server_random_len = SSL3_RANDOM_SIZE; | |
2497 | ||
2498 | return 0; | |
c9e08af2 | 2499 | #endif /* CONFIG_FIPS */ |
6fc6879b JM |
2500 | } |
2501 | ||
2502 | ||
2503 | int tls_connection_prf(void *tls_ctx, struct tls_connection *conn, | |
2504 | const char *label, int server_random_first, | |
2505 | u8 *out, size_t out_len) | |
2506 | { | |
68770ccd JM |
2507 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L |
2508 | SSL *ssl; | |
2509 | if (conn == NULL) | |
2510 | return -1; | |
2511 | if (server_random_first) | |
2512 | return -1; | |
2513 | ssl = conn->ssl; | |
2514 | if (SSL_export_keying_material(ssl, out, out_len, label, | |
2515 | os_strlen(label), NULL, 0, 0) == 1) { | |
2516 | wpa_printf(MSG_DEBUG, "OpenSSL: Using internal PRF"); | |
2517 | return 0; | |
2518 | } | |
2519 | #endif | |
6fc6879b JM |
2520 | return -1; |
2521 | } | |
2522 | ||
2523 | ||
81c85c06 JM |
2524 | static struct wpabuf * |
2525 | openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data, | |
2526 | int server) | |
6fc6879b JM |
2527 | { |
2528 | int res; | |
81c85c06 | 2529 | struct wpabuf *out_data; |
6fc6879b JM |
2530 | |
2531 | /* | |
2532 | * Give TLS handshake data from the server (if available) to OpenSSL | |
2533 | * for processing. | |
2534 | */ | |
2535 | if (in_data && | |
81c85c06 JM |
2536 | BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data)) |
2537 | < 0) { | |
6fc6879b JM |
2538 | tls_show_errors(MSG_INFO, __func__, |
2539 | "Handshake failed - BIO_write"); | |
2540 | return NULL; | |
2541 | } | |
2542 | ||
2543 | /* Initiate TLS handshake or continue the existing handshake */ | |
81c85c06 JM |
2544 | if (server) |
2545 | res = SSL_accept(conn->ssl); | |
2546 | else | |
2547 | res = SSL_connect(conn->ssl); | |
6fc6879b JM |
2548 | if (res != 1) { |
2549 | int err = SSL_get_error(conn->ssl, res); | |
2550 | if (err == SSL_ERROR_WANT_READ) | |
2551 | wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want " | |
2552 | "more data"); | |
2553 | else if (err == SSL_ERROR_WANT_WRITE) | |
2554 | wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to " | |
2555 | "write"); | |
2556 | else { | |
2557 | tls_show_errors(MSG_INFO, __func__, "SSL_connect"); | |
2558 | conn->failed++; | |
2559 | } | |
2560 | } | |
2561 | ||
2562 | /* Get the TLS handshake data to be sent to the server */ | |
2563 | res = BIO_ctrl_pending(conn->ssl_out); | |
2564 | wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res); | |
81c85c06 | 2565 | out_data = wpabuf_alloc(res); |
6fc6879b JM |
2566 | if (out_data == NULL) { |
2567 | wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for " | |
2568 | "handshake output (%d bytes)", res); | |
2569 | if (BIO_reset(conn->ssl_out) < 0) { | |
2570 | tls_show_errors(MSG_INFO, __func__, | |
2571 | "BIO_reset failed"); | |
2572 | } | |
6fc6879b JM |
2573 | return NULL; |
2574 | } | |
81c85c06 JM |
2575 | res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data), |
2576 | res); | |
6fc6879b JM |
2577 | if (res < 0) { |
2578 | tls_show_errors(MSG_INFO, __func__, | |
2579 | "Handshake failed - BIO_read"); | |
2580 | if (BIO_reset(conn->ssl_out) < 0) { | |
2581 | tls_show_errors(MSG_INFO, __func__, | |
2582 | "BIO_reset failed"); | |
2583 | } | |
81c85c06 | 2584 | wpabuf_free(out_data); |
6fc6879b JM |
2585 | return NULL; |
2586 | } | |
81c85c06 | 2587 | wpabuf_put(out_data, res); |
6fc6879b JM |
2588 | |
2589 | return out_data; | |
2590 | } | |
2591 | ||
2592 | ||
81c85c06 JM |
2593 | static struct wpabuf * |
2594 | openssl_get_appl_data(struct tls_connection *conn, size_t max_len) | |
6fc6879b | 2595 | { |
81c85c06 | 2596 | struct wpabuf *appl_data; |
6fc6879b | 2597 | int res; |
6fc6879b | 2598 | |
81c85c06 JM |
2599 | appl_data = wpabuf_alloc(max_len + 100); |
2600 | if (appl_data == NULL) | |
6fc6879b | 2601 | return NULL; |
6fc6879b | 2602 | |
81c85c06 JM |
2603 | res = SSL_read(conn->ssl, wpabuf_mhead(appl_data), |
2604 | wpabuf_size(appl_data)); | |
2605 | if (res < 0) { | |
bf206cad | 2606 | int err = SSL_get_error(conn->ssl, res); |
81c85c06 JM |
2607 | if (err == SSL_ERROR_WANT_READ || |
2608 | err == SSL_ERROR_WANT_WRITE) { | |
2609 | wpa_printf(MSG_DEBUG, "SSL: No Application Data " | |
2610 | "included"); | |
2611 | } else { | |
6fc6879b | 2612 | tls_show_errors(MSG_INFO, __func__, |
81c85c06 JM |
2613 | "Failed to read possible " |
2614 | "Application Data"); | |
6fc6879b | 2615 | } |
81c85c06 | 2616 | wpabuf_free(appl_data); |
6fc6879b JM |
2617 | return NULL; |
2618 | } | |
81c85c06 JM |
2619 | |
2620 | wpabuf_put(appl_data, res); | |
2621 | wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished " | |
2622 | "message", appl_data); | |
2623 | ||
2624 | return appl_data; | |
2625 | } | |
2626 | ||
2627 | ||
2628 | static struct wpabuf * | |
2629 | openssl_connection_handshake(struct tls_connection *conn, | |
2630 | const struct wpabuf *in_data, | |
2631 | struct wpabuf **appl_data, int server) | |
2632 | { | |
2633 | struct wpabuf *out_data; | |
2634 | ||
2635 | if (appl_data) | |
2636 | *appl_data = NULL; | |
2637 | ||
2638 | out_data = openssl_handshake(conn, in_data, server); | |
2639 | if (out_data == NULL) | |
6fc6879b | 2640 | return NULL; |
81c85c06 JM |
2641 | |
2642 | if (SSL_is_init_finished(conn->ssl) && appl_data && in_data) | |
2643 | *appl_data = openssl_get_appl_data(conn, wpabuf_len(in_data)); | |
2644 | ||
6fc6879b JM |
2645 | return out_data; |
2646 | } | |
2647 | ||
2648 | ||
81c85c06 JM |
2649 | struct wpabuf * |
2650 | tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn, | |
2651 | const struct wpabuf *in_data, | |
2652 | struct wpabuf **appl_data) | |
2653 | { | |
2654 | return openssl_connection_handshake(conn, in_data, appl_data, 0); | |
2655 | } | |
2656 | ||
2657 | ||
2658 | struct wpabuf * tls_connection_server_handshake(void *tls_ctx, | |
2659 | struct tls_connection *conn, | |
2660 | const struct wpabuf *in_data, | |
2661 | struct wpabuf **appl_data) | |
2662 | { | |
2663 | return openssl_connection_handshake(conn, in_data, appl_data, 1); | |
2664 | } | |
2665 | ||
2666 | ||
2667 | struct wpabuf * tls_connection_encrypt(void *tls_ctx, | |
2668 | struct tls_connection *conn, | |
2669 | const struct wpabuf *in_data) | |
6fc6879b JM |
2670 | { |
2671 | int res; | |
81c85c06 | 2672 | struct wpabuf *buf; |
6fc6879b JM |
2673 | |
2674 | if (conn == NULL) | |
81c85c06 | 2675 | return NULL; |
6fc6879b JM |
2676 | |
2677 | /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */ | |
2678 | if ((res = BIO_reset(conn->ssl_in)) < 0 || | |
2679 | (res = BIO_reset(conn->ssl_out)) < 0) { | |
2680 | tls_show_errors(MSG_INFO, __func__, "BIO_reset failed"); | |
81c85c06 | 2681 | return NULL; |
6fc6879b | 2682 | } |
81c85c06 | 2683 | res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data)); |
6fc6879b JM |
2684 | if (res < 0) { |
2685 | tls_show_errors(MSG_INFO, __func__, | |
2686 | "Encryption failed - SSL_write"); | |
81c85c06 | 2687 | return NULL; |
6fc6879b JM |
2688 | } |
2689 | ||
2690 | /* Read encrypted data to be sent to the server */ | |
81c85c06 JM |
2691 | buf = wpabuf_alloc(wpabuf_len(in_data) + 300); |
2692 | if (buf == NULL) | |
2693 | return NULL; | |
2694 | res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf)); | |
6fc6879b JM |
2695 | if (res < 0) { |
2696 | tls_show_errors(MSG_INFO, __func__, | |
2697 | "Encryption failed - BIO_read"); | |
81c85c06 JM |
2698 | wpabuf_free(buf); |
2699 | return NULL; | |
6fc6879b | 2700 | } |
81c85c06 | 2701 | wpabuf_put(buf, res); |
6fc6879b | 2702 | |
81c85c06 | 2703 | return buf; |
6fc6879b JM |
2704 | } |
2705 | ||
2706 | ||
81c85c06 JM |
2707 | struct wpabuf * tls_connection_decrypt(void *tls_ctx, |
2708 | struct tls_connection *conn, | |
2709 | const struct wpabuf *in_data) | |
6fc6879b JM |
2710 | { |
2711 | int res; | |
81c85c06 | 2712 | struct wpabuf *buf; |
6fc6879b JM |
2713 | |
2714 | /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */ | |
81c85c06 JM |
2715 | res = BIO_write(conn->ssl_in, wpabuf_head(in_data), |
2716 | wpabuf_len(in_data)); | |
6fc6879b JM |
2717 | if (res < 0) { |
2718 | tls_show_errors(MSG_INFO, __func__, | |
2719 | "Decryption failed - BIO_write"); | |
81c85c06 | 2720 | return NULL; |
6fc6879b JM |
2721 | } |
2722 | if (BIO_reset(conn->ssl_out) < 0) { | |
2723 | tls_show_errors(MSG_INFO, __func__, "BIO_reset failed"); | |
81c85c06 | 2724 | return NULL; |
6fc6879b JM |
2725 | } |
2726 | ||
2727 | /* Read decrypted data for further processing */ | |
81c85c06 JM |
2728 | /* |
2729 | * Even though we try to disable TLS compression, it is possible that | |
2730 | * this cannot be done with all TLS libraries. Add extra buffer space | |
2731 | * to handle the possibility of the decrypted data being longer than | |
2732 | * input data. | |
2733 | */ | |
2734 | buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3); | |
2735 | if (buf == NULL) | |
2736 | return NULL; | |
2737 | res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf)); | |
6fc6879b JM |
2738 | if (res < 0) { |
2739 | tls_show_errors(MSG_INFO, __func__, | |
2740 | "Decryption failed - SSL_read"); | |
a86a7316 | 2741 | wpabuf_free(buf); |
81c85c06 | 2742 | return NULL; |
6fc6879b | 2743 | } |
81c85c06 | 2744 | wpabuf_put(buf, res); |
6fc6879b | 2745 | |
81c85c06 | 2746 | return buf; |
6fc6879b JM |
2747 | } |
2748 | ||
2749 | ||
2750 | int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn) | |
2751 | { | |
2752 | return conn ? conn->ssl->hit : 0; | |
2753 | } | |
2754 | ||
2755 | ||
2756 | int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn, | |
2757 | u8 *ciphers) | |
2758 | { | |
2759 | char buf[100], *pos, *end; | |
2760 | u8 *c; | |
2761 | int ret; | |
2762 | ||
2763 | if (conn == NULL || conn->ssl == NULL || ciphers == NULL) | |
2764 | return -1; | |
2765 | ||
2766 | buf[0] = '\0'; | |
2767 | pos = buf; | |
2768 | end = pos + sizeof(buf); | |
2769 | ||
2770 | c = ciphers; | |
2771 | while (*c != TLS_CIPHER_NONE) { | |
2772 | const char *suite; | |
2773 | ||
2774 | switch (*c) { | |
2775 | case TLS_CIPHER_RC4_SHA: | |
2776 | suite = "RC4-SHA"; | |
2777 | break; | |
2778 | case TLS_CIPHER_AES128_SHA: | |
2779 | suite = "AES128-SHA"; | |
2780 | break; | |
2781 | case TLS_CIPHER_RSA_DHE_AES128_SHA: | |
2782 | suite = "DHE-RSA-AES128-SHA"; | |
2783 | break; | |
2784 | case TLS_CIPHER_ANON_DH_AES128_SHA: | |
2785 | suite = "ADH-AES128-SHA"; | |
2786 | break; | |
2787 | default: | |
2788 | wpa_printf(MSG_DEBUG, "TLS: Unsupported " | |
2789 | "cipher selection: %d", *c); | |
2790 | return -1; | |
2791 | } | |
2792 | ret = os_snprintf(pos, end - pos, ":%s", suite); | |
2793 | if (ret < 0 || ret >= end - pos) | |
2794 | break; | |
2795 | pos += ret; | |
2796 | ||
2797 | c++; | |
2798 | } | |
2799 | ||
2800 | wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1); | |
2801 | ||
2802 | if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) { | |
2803 | tls_show_errors(MSG_INFO, __func__, | |
2804 | "Cipher suite configuration failed"); | |
2805 | return -1; | |
2806 | } | |
2807 | ||
2808 | return 0; | |
2809 | } | |
2810 | ||
2811 | ||
2812 | int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn, | |
2813 | char *buf, size_t buflen) | |
2814 | { | |
2815 | const char *name; | |
2816 | if (conn == NULL || conn->ssl == NULL) | |
2817 | return -1; | |
2818 | ||
2819 | name = SSL_get_cipher(conn->ssl); | |
2820 | if (name == NULL) | |
2821 | return -1; | |
2822 | ||
2823 | os_strlcpy(buf, name, buflen); | |
2824 | return 0; | |
2825 | } | |
2826 | ||
2827 | ||
2828 | int tls_connection_enable_workaround(void *ssl_ctx, | |
2829 | struct tls_connection *conn) | |
2830 | { | |
2831 | SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); | |
2832 | ||
2833 | return 0; | |
2834 | } | |
2835 | ||
2836 | ||
1e5839e0 | 2837 | #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) |
6fc6879b JM |
2838 | /* ClientHello TLS extensions require a patch to openssl, so this function is |
2839 | * commented out unless explicitly needed for EAP-FAST in order to be able to | |
2840 | * build this file with unmodified openssl. */ | |
2841 | int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn, | |
2842 | int ext_type, const u8 *data, | |
2843 | size_t data_len) | |
2844 | { | |
0cf03892 | 2845 | if (conn == NULL || conn->ssl == NULL || ext_type != 35) |
6fc6879b JM |
2846 | return -1; |
2847 | ||
0cf03892 JM |
2848 | #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE |
2849 | if (SSL_set_session_ticket_ext(conn->ssl, (void *) data, | |
2850 | data_len) != 1) | |
2851 | return -1; | |
2852 | #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */ | |
6fc6879b JM |
2853 | if (SSL_set_hello_extension(conn->ssl, ext_type, (void *) data, |
2854 | data_len) != 1) | |
2855 | return -1; | |
0cf03892 | 2856 | #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */ |
6fc6879b JM |
2857 | |
2858 | return 0; | |
2859 | } | |
1e5839e0 | 2860 | #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
6fc6879b JM |
2861 | |
2862 | ||
2863 | int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn) | |
2864 | { | |
2865 | if (conn == NULL) | |
2866 | return -1; | |
2867 | return conn->failed; | |
2868 | } | |
2869 | ||
2870 | ||
2871 | int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn) | |
2872 | { | |
2873 | if (conn == NULL) | |
2874 | return -1; | |
2875 | return conn->read_alerts; | |
2876 | } | |
2877 | ||
2878 | ||
2879 | int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn) | |
2880 | { | |
2881 | if (conn == NULL) | |
2882 | return -1; | |
2883 | return conn->write_alerts; | |
2884 | } | |
2885 | ||
2886 | ||
080585c0 JM |
2887 | #ifdef HAVE_OCSP |
2888 | ||
2889 | static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp) | |
2890 | { | |
2891 | #ifndef CONFIG_NO_STDOUT_DEBUG | |
2892 | extern int wpa_debug_level; | |
2893 | BIO *out; | |
2894 | size_t rlen; | |
2895 | char *txt; | |
2896 | int res; | |
2897 | ||
2898 | if (wpa_debug_level > MSG_DEBUG) | |
2899 | return; | |
2900 | ||
2901 | out = BIO_new(BIO_s_mem()); | |
2902 | if (!out) | |
2903 | return; | |
2904 | ||
2905 | OCSP_RESPONSE_print(out, rsp, 0); | |
2906 | rlen = BIO_ctrl_pending(out); | |
2907 | txt = os_malloc(rlen + 1); | |
2908 | if (!txt) { | |
2909 | BIO_free(out); | |
2910 | return; | |
2911 | } | |
2912 | ||
2913 | res = BIO_read(out, txt, rlen); | |
2914 | if (res > 0) { | |
2915 | txt[res] = '\0'; | |
2916 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt); | |
2917 | } | |
2918 | os_free(txt); | |
2919 | BIO_free(out); | |
2920 | #endif /* CONFIG_NO_STDOUT_DEBUG */ | |
2921 | } | |
2922 | ||
2923 | ||
2924 | static int ocsp_resp_cb(SSL *s, void *arg) | |
2925 | { | |
2926 | struct tls_connection *conn = arg; | |
2927 | const unsigned char *p; | |
2928 | int len, status, reason; | |
2929 | OCSP_RESPONSE *rsp; | |
2930 | OCSP_BASICRESP *basic; | |
2931 | OCSP_CERTID *id; | |
2932 | ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update; | |
2933 | ||
2934 | len = SSL_get_tlsext_status_ocsp_resp(s, &p); | |
2935 | if (!p) { | |
2936 | wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received"); | |
2937 | return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1; | |
2938 | } | |
2939 | ||
2940 | wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len); | |
2941 | ||
2942 | rsp = d2i_OCSP_RESPONSE(NULL, &p, len); | |
2943 | if (!rsp) { | |
2944 | wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response"); | |
2945 | return 0; | |
2946 | } | |
2947 | ||
2948 | ocsp_debug_print_resp(rsp); | |
2949 | ||
2950 | status = OCSP_response_status(rsp); | |
2951 | if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) { | |
2952 | wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)", | |
2953 | status, OCSP_response_status_str(status)); | |
2954 | return 0; | |
2955 | } | |
2956 | ||
2957 | basic = OCSP_response_get1_basic(rsp); | |
2958 | if (!basic) { | |
2959 | wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse"); | |
2960 | return 0; | |
2961 | } | |
2962 | ||
2963 | status = OCSP_basic_verify(basic, NULL, SSL_CTX_get_cert_store(s->ctx), | |
2964 | 0); | |
2965 | if (status <= 0) { | |
2966 | tls_show_errors(MSG_INFO, __func__, | |
2967 | "OpenSSL: OCSP response failed verification"); | |
2968 | OCSP_BASICRESP_free(basic); | |
2969 | OCSP_RESPONSE_free(rsp); | |
2970 | return 0; | |
2971 | } | |
2972 | ||
2973 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded"); | |
2974 | ||
762c92a4 JM |
2975 | if (!conn->peer_cert) { |
2976 | wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check"); | |
2977 | OCSP_BASICRESP_free(basic); | |
2978 | OCSP_RESPONSE_free(rsp); | |
2979 | return 0; | |
2980 | } | |
2981 | ||
2982 | if (!conn->peer_issuer) { | |
2983 | wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check"); | |
080585c0 JM |
2984 | OCSP_BASICRESP_free(basic); |
2985 | OCSP_RESPONSE_free(rsp); | |
2986 | return 0; | |
2987 | } | |
2988 | ||
2989 | id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer); | |
2990 | if (!id) { | |
2991 | wpa_printf(MSG_DEBUG, "OpenSSL: Could not create OCSP certificate identifier"); | |
2992 | OCSP_BASICRESP_free(basic); | |
2993 | OCSP_RESPONSE_free(rsp); | |
2994 | return 0; | |
2995 | } | |
2996 | ||
2997 | if (!OCSP_resp_find_status(basic, id, &status, &reason, &produced_at, | |
2998 | &this_update, &next_update)) { | |
2999 | wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s", | |
3000 | (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" : | |
3001 | " (OCSP not required)"); | |
3002 | OCSP_BASICRESP_free(basic); | |
3003 | OCSP_RESPONSE_free(rsp); | |
3004 | return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1; | |
3005 | } | |
3006 | ||
3007 | if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) { | |
3008 | tls_show_errors(MSG_INFO, __func__, | |
3009 | "OpenSSL: OCSP status times invalid"); | |
3010 | OCSP_BASICRESP_free(basic); | |
3011 | OCSP_RESPONSE_free(rsp); | |
3012 | return 0; | |
3013 | } | |
3014 | ||
3015 | OCSP_BASICRESP_free(basic); | |
3016 | OCSP_RESPONSE_free(rsp); | |
3017 | ||
3018 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s", | |
3019 | OCSP_cert_status_str(status)); | |
3020 | ||
3021 | if (status == V_OCSP_CERTSTATUS_GOOD) | |
3022 | return 1; | |
3023 | if (status == V_OCSP_CERTSTATUS_REVOKED) | |
3024 | return 0; | |
3025 | if (conn->flags & TLS_CONN_REQUIRE_OCSP) { | |
3026 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required"); | |
3027 | return 0; | |
3028 | } | |
be7963b3 | 3029 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue"); |
080585c0 JM |
3030 | return 1; |
3031 | } | |
3032 | ||
3033 | ||
3034 | static int ocsp_status_cb(SSL *s, void *arg) | |
3035 | { | |
3036 | char *tmp; | |
3037 | char *resp; | |
3038 | size_t len; | |
3039 | ||
3040 | if (tls_global->ocsp_stapling_response == NULL) { | |
3041 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured"); | |
3042 | return SSL_TLSEXT_ERR_OK; | |
3043 | } | |
3044 | ||
3045 | resp = os_readfile(tls_global->ocsp_stapling_response, &len); | |
3046 | if (resp == NULL) { | |
3047 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file"); | |
3048 | /* TODO: Build OCSPResponse with responseStatus = internalError | |
3049 | */ | |
3050 | return SSL_TLSEXT_ERR_OK; | |
3051 | } | |
3052 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response"); | |
3053 | tmp = OPENSSL_malloc(len); | |
3054 | if (tmp == NULL) { | |
3055 | os_free(resp); | |
3056 | return SSL_TLSEXT_ERR_ALERT_FATAL; | |
3057 | } | |
3058 | ||
3059 | os_memcpy(tmp, resp, len); | |
3060 | os_free(resp); | |
3061 | SSL_set_tlsext_status_ocsp_resp(s, tmp, len); | |
3062 | ||
3063 | return SSL_TLSEXT_ERR_OK; | |
3064 | } | |
3065 | ||
3066 | #endif /* HAVE_OCSP */ | |
3067 | ||
3068 | ||
6fc6879b JM |
3069 | int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn, |
3070 | const struct tls_connection_params *params) | |
3071 | { | |
3072 | int ret; | |
3073 | unsigned long err; | |
080585c0 | 3074 | SSL_CTX *ssl_ctx = tls_ctx; |
6fc6879b JM |
3075 | |
3076 | if (conn == NULL) | |
3077 | return -1; | |
3078 | ||
3079 | while ((err = ERR_get_error())) { | |
3080 | wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s", | |
3081 | __func__, ERR_error_string(err, NULL)); | |
3082 | } | |
3083 | ||
e59c91af DS |
3084 | if (params->engine) { |
3085 | wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine"); | |
3086 | ret = tls_engine_init(conn, params->engine_id, params->pin, | |
3087 | params->key_id, params->cert_id, | |
3088 | params->ca_cert_id); | |
3089 | if (ret) | |
3090 | return ret; | |
3091 | } | |
6fc6879b JM |
3092 | if (tls_connection_set_subject_match(conn, |
3093 | params->subject_match, | |
01f809c7 JM |
3094 | params->altsubject_match, |
3095 | params->suffix_match)) | |
6fc6879b | 3096 | return -1; |
e59c91af DS |
3097 | |
3098 | if (params->engine && params->ca_cert_id) { | |
3099 | if (tls_connection_engine_ca_cert(tls_ctx, conn, | |
3100 | params->ca_cert_id)) | |
3101 | return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED; | |
3102 | } else if (tls_connection_ca_cert(tls_ctx, conn, params->ca_cert, | |
3103 | params->ca_cert_blob, | |
3104 | params->ca_cert_blob_len, | |
3105 | params->ca_path)) | |
6fc6879b | 3106 | return -1; |
e59c91af DS |
3107 | |
3108 | if (params->engine && params->cert_id) { | |
3109 | if (tls_connection_engine_client_cert(conn, params->cert_id)) | |
3110 | return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED; | |
3111 | } else if (tls_connection_client_cert(conn, params->client_cert, | |
3112 | params->client_cert_blob, | |
3113 | params->client_cert_blob_len)) | |
6fc6879b JM |
3114 | return -1; |
3115 | ||
e59c91af DS |
3116 | if (params->engine && params->key_id) { |
3117 | wpa_printf(MSG_DEBUG, "TLS: Using private key from engine"); | |
6fc6879b JM |
3118 | if (tls_connection_engine_private_key(conn)) |
3119 | return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED; | |
3120 | } else if (tls_connection_private_key(tls_ctx, conn, | |
3121 | params->private_key, | |
3122 | params->private_key_passwd, | |
3123 | params->private_key_blob, | |
3124 | params->private_key_blob_len)) { | |
3125 | wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'", | |
3126 | params->private_key); | |
3127 | return -1; | |
3128 | } | |
3129 | ||
3130 | if (tls_connection_dh(conn, params->dh_file)) { | |
3131 | wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'", | |
3132 | params->dh_file); | |
3133 | return -1; | |
3134 | } | |
3135 | ||
e866f39f JM |
3136 | #ifdef SSL_OP_NO_TICKET |
3137 | if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET) | |
3138 | SSL_set_options(conn->ssl, SSL_OP_NO_TICKET); | |
d53d2596 | 3139 | #ifdef SSL_clear_options |
e866f39f JM |
3140 | else |
3141 | SSL_clear_options(conn->ssl, SSL_OP_NO_TICKET); | |
d53d2596 | 3142 | #endif /* SSL_clear_options */ |
e866f39f JM |
3143 | #endif /* SSL_OP_NO_TICKET */ |
3144 | ||
080585c0 JM |
3145 | #ifdef HAVE_OCSP |
3146 | if (params->flags & TLS_CONN_REQUEST_OCSP) { | |
3147 | SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp); | |
3148 | SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb); | |
3149 | SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn); | |
3150 | } | |
3151 | #endif /* HAVE_OCSP */ | |
3152 | ||
235279e7 JM |
3153 | conn->flags = params->flags; |
3154 | ||
6fc6879b JM |
3155 | tls_get_errors(tls_ctx); |
3156 | ||
3157 | return 0; | |
3158 | } | |
3159 | ||
3160 | ||
3161 | int tls_global_set_params(void *tls_ctx, | |
3162 | const struct tls_connection_params *params) | |
3163 | { | |
3164 | SSL_CTX *ssl_ctx = tls_ctx; | |
3165 | unsigned long err; | |
3166 | ||
3167 | while ((err = ERR_get_error())) { | |
3168 | wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s", | |
3169 | __func__, ERR_error_string(err, NULL)); | |
3170 | } | |
3171 | ||
3172 | if (tls_global_ca_cert(ssl_ctx, params->ca_cert)) | |
3173 | return -1; | |
3174 | ||
3175 | if (tls_global_client_cert(ssl_ctx, params->client_cert)) | |
3176 | return -1; | |
3177 | ||
3178 | if (tls_global_private_key(ssl_ctx, params->private_key, | |
3179 | params->private_key_passwd)) | |
3180 | return -1; | |
3181 | ||
3182 | if (tls_global_dh(ssl_ctx, params->dh_file)) { | |
3183 | wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'", | |
3184 | params->dh_file); | |
3185 | return -1; | |
3186 | } | |
3187 | ||
e866f39f JM |
3188 | #ifdef SSL_OP_NO_TICKET |
3189 | if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET) | |
3190 | SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET); | |
d53d2596 | 3191 | #ifdef SSL_CTX_clear_options |
e866f39f JM |
3192 | else |
3193 | SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET); | |
d53d2596 | 3194 | #endif /* SSL_clear_options */ |
e866f39f JM |
3195 | #endif /* SSL_OP_NO_TICKET */ |
3196 | ||
080585c0 JM |
3197 | #ifdef HAVE_OCSP |
3198 | SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb); | |
3199 | SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx); | |
3200 | os_free(tls_global->ocsp_stapling_response); | |
3201 | if (params->ocsp_stapling_response) | |
3202 | tls_global->ocsp_stapling_response = | |
3203 | os_strdup(params->ocsp_stapling_response); | |
3204 | else | |
3205 | tls_global->ocsp_stapling_response = NULL; | |
3206 | #endif /* HAVE_OCSP */ | |
3207 | ||
6fc6879b JM |
3208 | return 0; |
3209 | } | |
3210 | ||
3211 | ||
3212 | int tls_connection_get_keyblock_size(void *tls_ctx, | |
3213 | struct tls_connection *conn) | |
3214 | { | |
3215 | const EVP_CIPHER *c; | |
3216 | const EVP_MD *h; | |
7f996409 | 3217 | int md_size; |
6fc6879b JM |
3218 | |
3219 | if (conn == NULL || conn->ssl == NULL || | |
3220 | conn->ssl->enc_read_ctx == NULL || | |
3221 | conn->ssl->enc_read_ctx->cipher == NULL || | |
3222 | conn->ssl->read_hash == NULL) | |
3223 | return -1; | |
3224 | ||
3225 | c = conn->ssl->enc_read_ctx->cipher; | |
3226 | #if OPENSSL_VERSION_NUMBER >= 0x00909000L | |
3227 | h = EVP_MD_CTX_md(conn->ssl->read_hash); | |
3228 | #else | |
3229 | h = conn->ssl->read_hash; | |
3230 | #endif | |
7f996409 JM |
3231 | if (h) |
3232 | md_size = EVP_MD_size(h); | |
3233 | #if OPENSSL_VERSION_NUMBER >= 0x10000000L | |
3234 | else if (conn->ssl->s3) | |
3235 | md_size = conn->ssl->s3->tmp.new_mac_secret_size; | |
3236 | #endif | |
3237 | else | |
3238 | return -1; | |
6fc6879b | 3239 | |
7f996409 JM |
3240 | wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d " |
3241 | "IV_len=%d", EVP_CIPHER_key_length(c), md_size, | |
3242 | EVP_CIPHER_iv_length(c)); | |
6fc6879b | 3243 | return 2 * (EVP_CIPHER_key_length(c) + |
7f996409 | 3244 | md_size + |
6fc6879b JM |
3245 | EVP_CIPHER_iv_length(c)); |
3246 | } | |
3247 | ||
3248 | ||
3249 | unsigned int tls_capabilities(void *tls_ctx) | |
3250 | { | |
3251 | return 0; | |
3252 | } | |
3253 | ||
3254 | ||
1e5839e0 | 3255 | #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) |
6fc6879b JM |
3256 | /* Pre-shared secred requires a patch to openssl, so this function is |
3257 | * commented out unless explicitly needed for EAP-FAST in order to be able to | |
3258 | * build this file with unmodified openssl. */ | |
3259 | ||
3260 | static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len, | |
3261 | STACK_OF(SSL_CIPHER) *peer_ciphers, | |
3262 | SSL_CIPHER **cipher, void *arg) | |
3263 | { | |
3264 | struct tls_connection *conn = arg; | |
3265 | int ret; | |
3266 | ||
3267 | if (conn == NULL || conn->session_ticket_cb == NULL) | |
3268 | return 0; | |
3269 | ||
3270 | ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx, | |
3271 | conn->session_ticket, | |
3272 | conn->session_ticket_len, | |
3273 | s->s3->client_random, | |
3274 | s->s3->server_random, secret); | |
3275 | os_free(conn->session_ticket); | |
3276 | conn->session_ticket = NULL; | |
3277 | ||
3278 | if (ret <= 0) | |
3279 | return 0; | |
3280 | ||
3281 | *secret_len = SSL_MAX_MASTER_KEY_LENGTH; | |
3282 | return 1; | |
3283 | } | |
3284 | ||
3285 | ||
0cf03892 JM |
3286 | #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE |
3287 | static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data, | |
3288 | int len, void *arg) | |
3289 | { | |
3290 | struct tls_connection *conn = arg; | |
3291 | ||
3292 | if (conn == NULL || conn->session_ticket_cb == NULL) | |
3293 | return 0; | |
3294 | ||
3295 | wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len); | |
3296 | ||
3297 | os_free(conn->session_ticket); | |
3298 | conn->session_ticket = NULL; | |
3299 | ||
3300 | wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket " | |
3301 | "extension", data, len); | |
3302 | ||
3303 | conn->session_ticket = os_malloc(len); | |
3304 | if (conn->session_ticket == NULL) | |
3305 | return 0; | |
3306 | ||
3307 | os_memcpy(conn->session_ticket, data, len); | |
3308 | conn->session_ticket_len = len; | |
3309 | ||
3310 | return 1; | |
3311 | } | |
3312 | #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */ | |
6fc6879b JM |
3313 | #ifdef SSL_OP_NO_TICKET |
3314 | static void tls_hello_ext_cb(SSL *s, int client_server, int type, | |
3315 | unsigned char *data, int len, void *arg) | |
3316 | { | |
3317 | struct tls_connection *conn = arg; | |
3318 | ||
3319 | if (conn == NULL || conn->session_ticket_cb == NULL) | |
3320 | return; | |
3321 | ||
3322 | wpa_printf(MSG_DEBUG, "OpenSSL: %s: type=%d length=%d", __func__, | |
3323 | type, len); | |
3324 | ||
3325 | if (type == TLSEXT_TYPE_session_ticket && !client_server) { | |
3326 | os_free(conn->session_ticket); | |
3327 | conn->session_ticket = NULL; | |
3328 | ||
3329 | wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket " | |
3330 | "extension", data, len); | |
3331 | conn->session_ticket = os_malloc(len); | |
3332 | if (conn->session_ticket == NULL) | |
3333 | return; | |
3334 | ||
3335 | os_memcpy(conn->session_ticket, data, len); | |
3336 | conn->session_ticket_len = len; | |
3337 | } | |
3338 | } | |
3339 | #else /* SSL_OP_NO_TICKET */ | |
3340 | static int tls_hello_ext_cb(SSL *s, TLS_EXTENSION *ext, void *arg) | |
3341 | { | |
3342 | struct tls_connection *conn = arg; | |
3343 | ||
3344 | if (conn == NULL || conn->session_ticket_cb == NULL) | |
3345 | return 0; | |
3346 | ||
3347 | wpa_printf(MSG_DEBUG, "OpenSSL: %s: type=%d length=%d", __func__, | |
3348 | ext->type, ext->length); | |
3349 | ||
3350 | os_free(conn->session_ticket); | |
3351 | conn->session_ticket = NULL; | |
3352 | ||
3353 | if (ext->type == 35) { | |
3354 | wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket " | |
3355 | "extension", ext->data, ext->length); | |
3356 | conn->session_ticket = os_malloc(ext->length); | |
3357 | if (conn->session_ticket == NULL) | |
3358 | return SSL_AD_INTERNAL_ERROR; | |
3359 | ||
3360 | os_memcpy(conn->session_ticket, ext->data, ext->length); | |
3361 | conn->session_ticket_len = ext->length; | |
3362 | } | |
3363 | ||
3364 | return 0; | |
3365 | } | |
3366 | #endif /* SSL_OP_NO_TICKET */ | |
0cf03892 | 3367 | #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */ |
1e5839e0 | 3368 | #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
6fc6879b JM |
3369 | |
3370 | ||
3371 | int tls_connection_set_session_ticket_cb(void *tls_ctx, | |
3372 | struct tls_connection *conn, | |
3373 | tls_session_ticket_cb cb, | |
3374 | void *ctx) | |
3375 | { | |
1e5839e0 | 3376 | #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) |
6fc6879b JM |
3377 | conn->session_ticket_cb = cb; |
3378 | conn->session_ticket_cb_ctx = ctx; | |
3379 | ||
3380 | if (cb) { | |
3381 | if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb, | |
3382 | conn) != 1) | |
3383 | return -1; | |
0cf03892 JM |
3384 | #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE |
3385 | SSL_set_session_ticket_ext_cb(conn->ssl, | |
3386 | tls_session_ticket_ext_cb, conn); | |
3387 | #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */ | |
6fc6879b JM |
3388 | #ifdef SSL_OP_NO_TICKET |
3389 | SSL_set_tlsext_debug_callback(conn->ssl, tls_hello_ext_cb); | |
3390 | SSL_set_tlsext_debug_arg(conn->ssl, conn); | |
3391 | #else /* SSL_OP_NO_TICKET */ | |
3392 | if (SSL_set_hello_extension_cb(conn->ssl, tls_hello_ext_cb, | |
3393 | conn) != 1) | |
3394 | return -1; | |
3395 | #endif /* SSL_OP_NO_TICKET */ | |
0cf03892 | 3396 | #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */ |
6fc6879b JM |
3397 | } else { |
3398 | if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1) | |
3399 | return -1; | |
0cf03892 JM |
3400 | #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE |
3401 | SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL); | |
3402 | #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */ | |
6fc6879b JM |
3403 | #ifdef SSL_OP_NO_TICKET |
3404 | SSL_set_tlsext_debug_callback(conn->ssl, NULL); | |
3405 | SSL_set_tlsext_debug_arg(conn->ssl, conn); | |
3406 | #else /* SSL_OP_NO_TICKET */ | |
3407 | if (SSL_set_hello_extension_cb(conn->ssl, NULL, NULL) != 1) | |
3408 | return -1; | |
3409 | #endif /* SSL_OP_NO_TICKET */ | |
0cf03892 | 3410 | #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */ |
6fc6879b JM |
3411 | } |
3412 | ||
3413 | return 0; | |
1e5839e0 | 3414 | #else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
6fc6879b | 3415 | return -1; |
1e5839e0 | 3416 | #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
6fc6879b | 3417 | } |