eap-tls.c (12165B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2013-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #define _GNU_SOURCE 28 #include <string.h> 29 #include <stdio.h> 30 #include <errno.h> 31 #include <alloca.h> 32 #include <ell/ell.h> 33 34 #include "src/missing.h" 35 #include "src/eap.h" 36 #include "src/eap-private.h" 37 #include "src/eap-tls-common.h" 38 39 static bool eap_tls_tunnel_ready(struct eap_state *eap, 40 const char *peer_identity, 41 bool resumed) 42 { 43 uint8_t msk_emsk[128]; 44 uint8_t iv[64]; 45 46 eap_method_success(eap); 47 eap_tls_common_set_completed(eap); 48 49 /* MSK, EMSK and IV derivation */ 50 eap_tls_common_tunnel_prf_get_bytes(eap, true, "client EAP encryption", 51 msk_emsk, 128); 52 eap_tls_common_tunnel_prf_get_bytes(eap, false, "client EAP encryption", 53 iv, 64); 54 55 /* TODO: Derive Session-ID */ 56 eap_set_key_material(eap, msk_emsk + 0, 64, msk_emsk + 64, 64, iv, 64, 57 NULL, 0); 58 explicit_bzero(msk_emsk, sizeof(msk_emsk)); 59 explicit_bzero(iv, sizeof(iv)); 60 61 eap_tls_common_send_empty_response(eap); 62 63 return true; 64 } 65 66 static int eap_tls_check_keys_match(struct l_key *priv_key, struct l_cert *cert, 67 const char *key_name, 68 const char *cert_name) 69 { 70 bool is_public; 71 size_t size; 72 ssize_t result; 73 uint8_t *encrypted, *decrypted; 74 struct l_key *pub_key; 75 76 if (!l_key_get_info(priv_key, L_KEY_RSA_PKCS1_V1_5, L_CHECKSUM_NONE, 77 &size, &is_public) || is_public) { 78 l_error("%s is not a private key or l_key_get_info fails", 79 key_name); 80 return -EINVAL; 81 } 82 83 size /= 8; 84 encrypted = alloca(size); 85 decrypted = alloca(size); 86 87 pub_key = l_cert_get_pubkey(cert); 88 if (!pub_key) { 89 l_error("l_cert_get_pubkey fails for %s", cert_name); 90 return -EIO; 91 } 92 93 result = l_key_encrypt(pub_key, L_KEY_RSA_PKCS1_V1_5, L_CHECKSUM_NONE, 94 "", encrypted, 1, size); 95 l_key_free(pub_key); 96 97 if (result != (ssize_t) size) { 98 l_error("l_key_encrypt fails with %s: %s", cert_name, 99 strerror(-result)); 100 return result; 101 } 102 103 result = l_key_decrypt(priv_key, L_KEY_RSA_PKCS1_V1_5, L_CHECKSUM_NONE, 104 encrypted, decrypted, size, size); 105 if (result < 0) { 106 l_error("l_key_decrypt fails with %s: %s", key_name, 107 strerror(-result)); 108 return result; 109 } 110 111 if (result != 1 || decrypted[0] != 0) { 112 l_error("Private key %s does not match certificate %s", key_name, 113 cert_name); 114 return -EINVAL; 115 } 116 117 return 0; 118 } 119 120 static int eap_tls_settings_check(struct l_settings *settings, 121 struct l_queue *secrets, 122 const char *prefix, 123 struct l_queue **out_missing) 124 { 125 char tls_prefix[32]; 126 char passphrase_setting[72]; 127 char client_cert_setting[72]; 128 char priv_key_setting[72]; 129 char bundle_setting[72]; 130 L_AUTO_FREE_VAR(char *, passphrase) = NULL; 131 L_AUTO_FREE_VAR(char *, client_cert_value) = NULL; 132 L_AUTO_FREE_VAR(char *, priv_key_value) = NULL; 133 L_AUTO_FREE_VAR(char *, bundle_value) = NULL; 134 struct l_certchain *client_cert = NULL; 135 struct l_key *priv_key = NULL; 136 const char *error_str; 137 bool priv_key_encrypted, cert_encrypted; 138 int ret; 139 140 snprintf(tls_prefix, sizeof(tls_prefix), "%sTLS-", prefix); 141 142 ret = eap_tls_common_settings_check(settings, secrets, tls_prefix, 143 out_missing); 144 if (ret < 0) 145 goto done; 146 147 snprintf(client_cert_setting, sizeof(client_cert_setting), 148 "%sClientCert", tls_prefix); 149 client_cert_value = l_settings_get_string(settings, "Security", 150 client_cert_setting); 151 152 snprintf(priv_key_setting, sizeof(priv_key_setting), "%sClientKey", 153 tls_prefix); 154 priv_key_value = l_settings_get_string(settings, "Security", 155 priv_key_setting); 156 157 snprintf(bundle_setting, sizeof(bundle_setting), 158 "%sClientKeyBundle", tls_prefix); 159 bundle_value = l_settings_get_string(settings, "Security", 160 bundle_setting); 161 162 snprintf(passphrase_setting, sizeof(passphrase_setting), 163 "%sClientKeyPassphrase", tls_prefix); 164 passphrase = l_settings_get_string(settings, "Security", 165 passphrase_setting); 166 167 if (!passphrase) { 168 const struct eap_secret_info *secret; 169 170 secret = l_queue_find(secrets, eap_secret_info_match, 171 passphrase_setting); 172 if (secret) 173 passphrase = l_strdup(secret->value); 174 } 175 176 /* 177 * Check whether the combination of settings that are present/missing 178 * makes sense before validating each setting. 179 */ 180 if (bundle_value && (priv_key_value || client_cert_value)) { 181 l_error("Either %s or %s/%s can be used, not both", 182 bundle_setting, priv_key_setting, client_cert_setting); 183 ret = -EEXIST; 184 goto done; 185 } else if (priv_key_value && !client_cert_value) { 186 l_error("%s present but no client certificate (%s)", 187 priv_key_setting, client_cert_setting); 188 ret = -ENOENT; 189 goto done; 190 } else if (!priv_key_value && client_cert_value) { 191 l_error("%s present but no client private key (%s)", 192 client_cert_setting, priv_key_setting); 193 ret = -ENOENT; 194 goto done; 195 } 196 197 if (!priv_key_value && !bundle_value) { 198 if (passphrase) { 199 l_error("%s present but no client keys set (%s)", 200 passphrase_setting, priv_key_setting); 201 ret = -ENOENT; 202 goto done; 203 } 204 205 ret = 0; 206 goto done; 207 } 208 209 if (bundle_value && 210 (!l_cert_load_container_file(bundle_value, passphrase, 211 &client_cert, &priv_key, 212 &priv_key_encrypted) || 213 !client_cert || !priv_key)) { 214 if (client_cert) { 215 l_error("No private key loaded from %s", bundle_value); 216 ret = -ENOKEY; 217 goto done; 218 } else if (priv_key) { 219 l_error("No certificates loaded from %s", bundle_value); 220 ret = -ENOKEY; 221 goto done; 222 } else if (!priv_key_encrypted) { 223 l_error("Error loading %s", bundle_value); 224 ret = -EIO; 225 goto done; 226 } else if (passphrase) { 227 l_error("Error loading key pair from encrypted file %s", 228 bundle_value); 229 ret = -EACCES; 230 goto done; 231 } 232 233 /* 234 * We've got an encrypted file and passphrase was not saved 235 * in the network settings, need to request the passphrase. 236 */ 237 eap_append_secret(out_missing, 238 EAP_SECRET_LOCAL_PKEY_PASSPHRASE, 239 passphrase_setting, NULL, 240 bundle_value, EAP_CACHE_TEMPORARY); 241 ret = 0; 242 goto done; 243 } 244 245 if (bundle_value) 246 goto validate_keys; 247 248 client_cert = eap_tls_load_client_cert(settings, client_cert_value, 249 passphrase, &cert_encrypted); 250 if (!client_cert) { 251 if (!cert_encrypted) { 252 l_error("Failed to load %s", client_cert_value); 253 ret = -EIO; 254 goto done; 255 } 256 257 if (passphrase) { 258 l_error("Error loading certificate from encrypted " 259 "file %s", client_cert_value); 260 ret = -EACCES; 261 goto done; 262 } 263 264 /* 265 * We've got an encrypted file and passphrase was not saved 266 * in the network settings, need to request the passphrase. 267 */ 268 eap_append_secret(out_missing, 269 EAP_SECRET_LOCAL_PKEY_PASSPHRASE, 270 passphrase_setting, NULL, 271 client_cert_value, EAP_CACHE_TEMPORARY); 272 ret = 0; 273 goto done; 274 } 275 276 priv_key = eap_tls_load_priv_key(settings, priv_key_value, passphrase, 277 &priv_key_encrypted); 278 if (!priv_key) { 279 if (!priv_key_encrypted) { 280 l_error("Error loading client private key %s", 281 priv_key_value); 282 ret = -EIO; 283 goto done; 284 } 285 286 if (passphrase) { 287 l_error("Error loading encrypted client private key %s", 288 priv_key_value); 289 ret = -EACCES; 290 goto done; 291 } 292 293 /* 294 * We've got an encrypted key and passphrase was not saved 295 * in the network settings, need to request the passphrase. 296 */ 297 eap_append_secret(out_missing, 298 EAP_SECRET_LOCAL_PKEY_PASSPHRASE, 299 passphrase_setting, NULL, 300 priv_key_value, EAP_CACHE_TEMPORARY); 301 ret = 0; 302 goto done; 303 } 304 305 validate_keys: 306 if (passphrase && !priv_key_encrypted && !cert_encrypted) { 307 l_error("%s present but keys are not encrypted", 308 passphrase_setting); 309 ret = -ENOENT; 310 goto done; 311 } 312 313 /* 314 * Sanity check that certchain provided is valid. We do not verify 315 * the certchain against the provided CA since the CA that issued 316 * user certificates might be different from the one that is used 317 * to verify the peer. 318 */ 319 if (!l_certchain_verify(client_cert, NULL, &error_str)) { 320 l_error("Certificate chain %s fails verification: %s", 321 client_cert_value, error_str); 322 ret = -EINVAL; 323 goto done; 324 } 325 326 ret = eap_tls_check_keys_match(priv_key, 327 l_certchain_get_leaf(client_cert), 328 priv_key_value, client_cert_value); 329 330 done: 331 l_certchain_free(client_cert); 332 l_key_free(priv_key); 333 334 if (passphrase) 335 explicit_bzero(passphrase, strlen(passphrase)); 336 337 return ret; 338 } 339 340 static const struct eap_tls_variant_ops eap_tls_ops = { 341 .tunnel_ready = eap_tls_tunnel_ready, 342 }; 343 344 static bool eap_tls_settings_load(struct eap_state *eap, 345 struct l_settings *settings, 346 const char *prefix) 347 { 348 char tls_prefix[32]; 349 char setting_key[72]; 350 struct l_certchain *client_cert = NULL; 351 struct l_key *client_key = NULL; 352 353 L_AUTO_FREE_VAR(char *, value) = NULL; 354 L_AUTO_FREE_VAR(char *, passphrase) = NULL; 355 356 snprintf(tls_prefix, sizeof(tls_prefix), "%sTLS-", prefix); 357 358 if (!eap_tls_common_settings_load(eap, settings, tls_prefix, 359 &eap_tls_ops, NULL)) 360 return false; 361 362 snprintf(setting_key, sizeof(setting_key), "%sClientKeyPassphrase", 363 tls_prefix); 364 passphrase = l_settings_get_string(settings, "Security", setting_key); 365 366 snprintf(setting_key, sizeof(setting_key), "%sClientCert", tls_prefix); 367 value = l_settings_get_string(settings, "Security", setting_key); 368 if (value) { 369 client_cert = eap_tls_load_client_cert(settings, value, 370 passphrase, NULL); 371 if (!client_cert) 372 goto bad_client_cert; 373 } 374 375 l_free(value); 376 377 snprintf(setting_key, sizeof(setting_key), "%sClientKey", tls_prefix); 378 value = l_settings_get_string(settings, "Security", setting_key); 379 if (value) { 380 client_key = eap_tls_load_priv_key(settings, value, 381 passphrase, NULL); 382 if (!client_key) 383 goto bad_client_key; 384 } 385 386 l_free(value); 387 388 snprintf(setting_key, sizeof(setting_key), "%sClientKeyBundle", 389 tls_prefix); 390 value = l_settings_get_string(settings, "Security", setting_key); 391 if (value && !client_cert && !client_key && 392 (!l_cert_load_container_file(value, passphrase, 393 &client_cert, 394 &client_key, NULL) || 395 !client_cert || !client_key)) { 396 goto bad_bundle; 397 } 398 399 eap_tls_common_set_keys(eap, client_cert, client_key); 400 return true; 401 402 bad_bundle: 403 l_key_free(client_key); 404 bad_client_key: 405 l_certchain_free(client_cert); 406 bad_client_cert: 407 eap_tls_common_state_free(eap); 408 return false; 409 } 410 411 static struct eap_method eap_tls = { 412 .request_type = EAP_TYPE_TLS, 413 .exports_msk = true, 414 .name = "TLS", 415 416 .handle_request = eap_tls_common_handle_request, 417 .handle_retransmit = eap_tls_common_handle_retransmit, 418 .reset_state = eap_tls_common_state_reset, 419 .free = eap_tls_common_state_free, 420 421 .check_settings = eap_tls_settings_check, 422 .load_settings = eap_tls_settings_load, 423 }; 424 425 static struct eap_method eap_wfa_tls = { 426 .request_type = EAP_TYPE_EXPANDED, 427 .exports_msk = true, 428 .name = "WFA-TLS", 429 430 .handle_request = eap_tls_common_handle_request, 431 .handle_retransmit = eap_tls_common_handle_retransmit, 432 .reset_state = eap_tls_common_state_reset, 433 .free = eap_tls_common_state_free, 434 435 .check_settings = eap_tls_settings_check, 436 .load_settings = eap_tls_settings_load, 437 .vendor_id = { 0x00, 0x9f, 0x68 }, 438 .vendor_type = 0x0000000d, 439 }; 440 441 static int eap_tls_init(void) 442 { 443 l_debug(""); 444 445 if (eap_register_method(&eap_tls) < 0) 446 return -EPERM; 447 448 return eap_register_method(&eap_wfa_tls); 449 } 450 451 static void eap_tls_exit(void) 452 { 453 l_debug(""); 454 eap_unregister_method(&eap_tls); 455 eap_unregister_method(&eap_wfa_tls); 456 } 457 458 EAP_METHOD_BUILTIN(eap_tls, eap_tls_init, eap_tls_exit)