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