eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

eap-mschapv2.c (15928B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2016  Markus Ongyerth. 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 <ctype.h>
     29 #include <stdio.h>
     30 #include <errno.h>
     31 #include <string.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-mschapv2.h"
     38 #include "src/mschaputil.h"
     39 
     40 #define MSCHAPV2_CHAL_LEN 16
     41 #define MSCHAPV2_NT_RESPONSE_LEN 24
     42 #define MSCHAPV2_AUTH_RESPONSE_LEN 20
     43 #define MSCHAPV2_MASTER_KEY_LEN 16
     44 
     45 #define MSCHAPV2_OP_CHALLENGE 1
     46 #define MSCHAPV2_OP_RESPONSE 2
     47 #define MSCHAPV2_OP_SUCCESS 3
     48 #define MSCHAPV2_OP_FAILURE 4
     49 
     50 struct eap_mschapv2_state {
     51 	uint8_t password_hash[16];
     52 	char *user;
     53 	size_t user_len;
     54 	uint8_t current_id;
     55 
     56 	uint8_t peer_challenge[MSCHAPV2_CHAL_LEN];
     57 	uint8_t server_challenge[MSCHAPV2_CHAL_LEN];
     58 };
     59 
     60 struct mschapv2_header {
     61 	uint8_t op_code;
     62 	uint8_t mschap_id;
     63 	uint16_t mschap_len;
     64 } __attribute__((packed));
     65 
     66 struct mschapv2_value {
     67 	uint8_t peer_challenge[MSCHAPV2_CHAL_LEN];
     68 	uint8_t reserved[8];
     69 	uint8_t nt_response[MSCHAPV2_NT_RESPONSE_LEN];
     70 	uint8_t flags;
     71 } __attribute__((packed));
     72 
     73 struct mschapv2_response {
     74 	struct mschapv2_header hdr;
     75 	/* This will always be sizeof(value) */
     76 	uint8_t val_length;
     77 	struct mschapv2_value value;
     78 	char name[];
     79 } __attribute__((packed));
     80 
     81 /**
     82  * Generate the asymmetric start keys from our mschapv2 master key for MPPE
     83  * This function is specified in:
     84  * https://tools.ietf.org/html/draft-ietf-pppext-mschapv2-keys-02
     85  *
     86  * @master_key: The master key
     87  * @session_key: the destination
     88  * @session_len: The length of the requested key in octets (<= 20)
     89  * @server: if the key should be generated for server side
     90  * @send:  if the send or the receive key should be generated
     91  *
     92  * Returns: true on success, false if hash/encrypt couldn't be done
     93  **/
     94 bool mschapv2_get_asymmetric_start_key(const uint8_t master_key[static 16],
     95 				uint8_t *session_key, size_t session_len,
     96 				bool server, bool send)
     97 {
     98 	static const uint8_t magic2[] = {
     99 		0x4f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x69,
    100 		0x65, 0x6e, 0x74, 0x20, 0x73, 0x69, 0x64, 0x65, 0x2c, 0x20,
    101 		0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
    102 		0x65, 0x20, 0x73, 0x65, 0x6e, 0x64, 0x20, 0x6b, 0x65, 0x79,
    103 		0x3b, 0x20, 0x6f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73,
    104 		0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x73, 0x69, 0x64, 0x65,
    105 		0x2c, 0x20, 0x69, 0x74, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
    106 		0x65, 0x20, 0x72, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x20,
    107 		0x6b, 0x65, 0x79, 0x2e
    108 	};
    109 	static const uint8_t magic3[] = {
    110 		0x4f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x69,
    111 		0x65, 0x6e, 0x74, 0x20, 0x73, 0x69, 0x64, 0x65, 0x2c, 0x20,
    112 		0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
    113 		0x65, 0x20, 0x72, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x20,
    114 		0x6b, 0x65, 0x79, 0x3b, 0x20, 0x6f, 0x6e, 0x20, 0x74, 0x68,
    115 		0x65, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x73,
    116 		0x69, 0x64, 0x65, 0x2c, 0x20, 0x69, 0x74, 0x20, 0x69, 0x73,
    117 		0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x65, 0x6e, 0x64, 0x20,
    118 		0x6b, 0x65, 0x79, 0x2e
    119 	};
    120 	static const uint8_t shs_pad1[] = {
    121 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    122 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    123 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    124 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    125 	};
    126 
    127 	static const uint8_t shs_pad2[] = {
    128 		0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2,
    129 		0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2,
    130 		0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2,
    131 		0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2, 0xf2
    132 	};
    133 	const uint8_t *magic;
    134 	struct l_checksum *check;
    135 
    136 	if (send == server)
    137 		magic = magic3;
    138 	else
    139 		magic = magic2;
    140 
    141 	check = l_checksum_new(L_CHECKSUM_SHA1);
    142 	if (!check)
    143 		return false;
    144 
    145 	l_checksum_update(check, master_key, 16);
    146 	l_checksum_update(check, shs_pad1, 40);
    147 	l_checksum_update(check, magic, 84);
    148 	l_checksum_update(check, shs_pad2, 40);
    149 	l_checksum_get_digest(check, session_key, session_len);
    150 
    151 	l_checksum_free(check);
    152 
    153 	return true;
    154 }
    155 
    156 /**
    157  * Generate the master key for MPPE from mschapv2
    158  * This function is specified in:
    159  * https://tools.ietf.org/html/draft-ietf-pppext-mschapv2-keys-02
    160  *
    161  * @pw_hash_hash: The MD4 hash of the password hash
    162  * @nt_response: The nt_response generated for mschapv2
    163  * @master_key: The destination
    164  *
    165  * Returns: true on success, false if hash/encrypt couldn't be done
    166  **/
    167 bool mschapv2_get_master_key(const uint8_t pw_hash_hash[static 16],
    168 					const uint8_t nt_response[static 24],
    169 					uint8_t master_key[static 16])
    170 {
    171 	static const uint8_t magic[] = {
    172 		0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
    173 		0x68, 0x65, 0x20, 0x4d, 0x50, 0x50, 0x45, 0x20, 0x4d,
    174 		0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x4b, 0x65, 0x79
    175 	};
    176 	struct l_checksum *check;
    177 
    178 	check = l_checksum_new(L_CHECKSUM_SHA1);
    179 	if (!check)
    180 		return false;
    181 
    182 	l_checksum_update(check, pw_hash_hash, 16);
    183 	l_checksum_update(check, nt_response, 24);
    184 	l_checksum_update(check, magic, sizeof(magic));
    185 
    186 	l_checksum_get_digest(check, master_key, 16);
    187 	l_checksum_free(check);
    188 
    189 	return true;
    190 }
    191 
    192 static bool eap_mschapv2_reset_state(struct eap_state *eap)
    193 {
    194 	struct eap_mschapv2_state *state = eap_get_data(eap);
    195 
    196 	memset(state->peer_challenge, 0, sizeof(state->peer_challenge));
    197 	memset(state->server_challenge, 0, sizeof(state->server_challenge));
    198 
    199 	return true;
    200 }
    201 
    202 static void eap_mschapv2_state_free(struct eap_mschapv2_state *state)
    203 {
    204 	explicit_bzero(state->password_hash, sizeof(state->password_hash));
    205 
    206 	explicit_bzero(state->user, state->user_len);
    207 	l_free(state->user);
    208 	state->user_len = 0;
    209 
    210 	l_free(state);
    211 }
    212 
    213 static void eap_mschapv2_free(struct eap_state *eap)
    214 {
    215 	struct eap_mschapv2_state *state;
    216 
    217 	eap_mschapv2_reset_state(eap);
    218 
    219 	state = eap_get_data(eap);
    220 	eap_set_data(eap, NULL);
    221 
    222 	eap_mschapv2_state_free(state);
    223 }
    224 
    225 static bool eap_mschapv2_send_response(struct eap_state *eap)
    226 {
    227 	struct eap_mschapv2_state *state = eap_get_data(eap);
    228 	size_t size = sizeof(struct mschapv2_response) + state->user_len;
    229 	uint8_t output[size + 5];
    230 	struct mschapv2_response *response =
    231 				(struct mschapv2_response *) (output + 5);
    232 	bool ret;
    233 
    234 	/*
    235 	 * Make sure to initialize the response structure to 0 since
    236 	 * we're not filling in some of the reserved or optional fields
    237 	 */
    238 	memset(response, 0, size);
    239 
    240 	ret = mschapv2_generate_nt_response(state->password_hash,
    241 						state->peer_challenge,
    242 						state->server_challenge,
    243 						state->user,
    244 						response->value.nt_response);
    245 
    246 	if (!ret)
    247 		return false;
    248 
    249 	response->hdr.op_code = MSCHAPV2_OP_RESPONSE;
    250 	response->hdr.mschap_id = state->current_id;
    251 	response->hdr.mschap_len = L_BE16_TO_CPU(size);
    252 	response->val_length = sizeof(struct mschapv2_value);
    253 
    254 	memcpy(response->value.peer_challenge, state->peer_challenge,
    255 							MSCHAPV2_CHAL_LEN);
    256 	memcpy(response->name, state->user, state->user_len);
    257 
    258 	eap_method_respond(eap, output, sizeof(output));
    259 
    260 	return true;
    261 }
    262 
    263 static void eap_mschapv2_handle_challenge(struct eap_state *eap,
    264 						const uint8_t *pkt, size_t len)
    265 {
    266 	struct eap_mschapv2_state *state = eap_get_data(eap);
    267 
    268 	if (pkt[0] != MSCHAPV2_CHAL_LEN) {
    269 		l_error("MSCHAPV2-Challenge had unexpected length: %x",
    270 								pkt[0]);
    271 		goto err;
    272 	}
    273 
    274 	if (len - 1 < MSCHAPV2_CHAL_LEN) {
    275 		l_error("MSCHAPV2-Challenge packet was to short for challenge");
    276 		goto err;
    277 	}
    278 
    279 	memcpy(state->server_challenge, pkt + 1, MSCHAPV2_CHAL_LEN);
    280 	l_getrandom(state->peer_challenge, MSCHAPV2_CHAL_LEN);
    281 
    282 	if (eap_mschapv2_send_response(eap))
    283 		return;
    284 
    285 err:
    286 	eap_method_error(eap);
    287 }
    288 
    289 /*
    290  * We need to verify the authenticator response from the server
    291  * and generate the master session key.
    292  */
    293 static void eap_mschapv2_handle_success(struct eap_state *eap,
    294 					const uint8_t *pkt, size_t len)
    295 {
    296 	struct eap_mschapv2_state *state = eap_get_data(eap);
    297 	uint8_t nt_response[24];
    298 	uint8_t password_hash_hash[16];
    299 	uint8_t master_key[16];
    300 	uint8_t session_key[32];
    301 	char authenticator_resp[42];
    302 	bool ret;
    303 
    304 	uint8_t buffer[5 + 1];
    305 
    306 	mschapv2_hash_nt_password_hash(state->password_hash,
    307 							password_hash_hash);
    308 
    309 	ret = mschapv2_generate_nt_response(state->password_hash,
    310 						state->peer_challenge,
    311 						state->server_challenge,
    312 						state->user, nt_response);
    313 
    314 	if (!ret)
    315 		goto done;
    316 
    317 	ret = mschapv2_generate_authenticator_response(password_hash_hash,
    318 						nt_response,
    319 						state->peer_challenge,
    320 						state->server_challenge,
    321 						state->user,
    322 						authenticator_resp);
    323 
    324 	if (!ret)
    325 		goto done;
    326 
    327 	/*
    328 	 * For iwd timing attacks are unlikely because media access will
    329 	 * influence timing. If this code is ever taken out of iwd, memcmp
    330 	 * should be replaced by a constant time memcmp
    331 	 */
    332 	if (len < 42 || memcmp(authenticator_resp, pkt, 42)) {
    333 		l_warn("Authenticator response didn't match");
    334 		ret = false;
    335 		goto done;
    336 	}
    337 
    338 	ret = mschapv2_get_master_key(password_hash_hash, nt_response,
    339 								master_key);
    340 	ret &= mschapv2_get_asymmetric_start_key(master_key, session_key,
    341 							16, false, true);
    342 	ret &= mschapv2_get_asymmetric_start_key(master_key, session_key + 16,
    343 							16, false, false);
    344 
    345 	if (!ret)
    346 		goto done;
    347 
    348 	eap_method_success(eap);
    349 
    350 	buffer[5] = MSCHAPV2_OP_SUCCESS;
    351 	eap_method_respond(eap, buffer, sizeof(buffer));
    352 
    353 	/* The eapol set_key_material only needs msk, and that's all we got */
    354 	eap_set_key_material(eap, session_key, 32, NULL, 0, NULL, 0, NULL, 0);
    355 
    356 done:
    357 	if (!ret)
    358 		eap_method_error(eap);
    359 
    360 	explicit_bzero(master_key, sizeof(master_key));
    361 	explicit_bzero(session_key, sizeof(session_key));
    362 	explicit_bzero(password_hash_hash, sizeof(password_hash_hash));
    363 }
    364 
    365 static void eap_mschapv2_handle_failure(struct eap_state *eap,
    366 					const uint8_t *pkt, size_t len)
    367 {
    368 	/*
    369 	 * From what I have seen, we can't prompt the user in any useful way
    370 	 * yet, so we can't do any proper error handling.
    371 	 * The values we can read from this are defined in:
    372 	 * https://tools.ietf.org/html/draft-ietf-pppext-mschap-v2-01
    373 	 * Section 9
    374 	 *
    375 	 * At the current point, this will be a fail.
    376 	 */
    377 	l_debug("");
    378 	eap_method_error(eap);
    379 }
    380 
    381 static void eap_mschapv2_handle_request(struct eap_state *eap,
    382 					const uint8_t *pkt, size_t len)
    383 {
    384 	struct eap_mschapv2_state *state = eap_get_data(eap);
    385 	const struct mschapv2_header *hdr = (struct mschapv2_header *) pkt;
    386 	size_t size = sizeof(*hdr);
    387 
    388 	if (len < sizeof(struct mschapv2_header) + 1) {
    389 		l_error("EAP-MSCHAPV2 packet too short");
    390 		goto err;
    391 	}
    392 
    393 	state->current_id = hdr->mschap_id;
    394 
    395 	if (L_BE16_TO_CPU(hdr->mschap_len) != len) {
    396 		l_error("EAP-MSCHAPV2 packet contains invalid length");
    397 		goto err;
    398 	}
    399 
    400 	switch (hdr->op_code) {
    401 	case MSCHAPV2_OP_CHALLENGE:
    402 		eap_mschapv2_handle_challenge(eap, pkt + size, len - size);
    403 		break;
    404 	case MSCHAPV2_OP_SUCCESS:
    405 		eap_mschapv2_handle_success(eap, pkt + size, len - size);
    406 		break;
    407 	case MSCHAPV2_OP_FAILURE:
    408 		eap_mschapv2_handle_failure(eap, pkt + size, len - size);
    409 		break;
    410 	default:
    411 		l_error("Got unknown OP-Code in MSCHPV2 packet: %x",
    412 							hdr->op_code);
    413 		goto err;
    414 	}
    415 
    416 	return;
    417 
    418 err:
    419 	eap_method_error(eap);
    420 }
    421 
    422 static bool set_password_from_string(struct eap_mschapv2_state *state,
    423 						const char *password)
    424 {
    425 	return mschap_nt_password_hash(password, state->password_hash);
    426 }
    427 
    428 static int eap_mschapv2_check_settings(struct l_settings *settings,
    429 					struct l_queue *secrets,
    430 					const char *prefix,
    431 					struct l_queue **out_missing)
    432 {
    433 	L_AUTO_FREE_VAR(uint8_t *, password_hash) = NULL;
    434 	L_AUTO_FREE_VAR(char *, password) = NULL;
    435 	L_AUTO_FREE_VAR(char *, identity);
    436 	const struct eap_secret_info *secret;
    437 	char setting[64], setting2[64];
    438 	uint8_t hash[16];
    439 	int r = 0;
    440 	size_t hash_len;
    441 
    442 	snprintf(setting, sizeof(setting), "%sIdentity", prefix);
    443 	identity = l_settings_get_string(settings, "Security", setting);
    444 
    445 	snprintf(setting2, sizeof(setting2), "%sPassword", prefix);
    446 
    447 	if (!identity) {
    448 		secret = l_queue_find(secrets, eap_secret_info_match, setting);
    449 		if (secret) {
    450 			password = l_strdup(secret->value +
    451 						strlen(secret->value) + 1);
    452 
    453 			goto validate;
    454 		}
    455 
    456 		eap_append_secret(out_missing, EAP_SECRET_REMOTE_USER_PASSWORD,
    457 					setting, setting2, NULL,
    458 					EAP_CACHE_TEMPORARY);
    459 		return 0;
    460 	}
    461 
    462 	password = l_settings_get_string(settings, "Security", setting2);
    463 
    464 	snprintf(setting, sizeof(setting), "%sPassword-Hash", prefix);
    465 	password_hash = l_settings_get_bytes(settings, "Security",
    466 						setting, &hash_len);
    467 
    468 	if (password && password_hash) {
    469 		l_error("Exactly one of (%s, %s) must be present",
    470 			setting, setting2);
    471 		r = -EEXIST;
    472 		goto cleanup;
    473 	}
    474 
    475 	if (password_hash) {
    476 		if (hash_len != 16) {
    477 			l_error("Property %s is not a 16-byte hexstring",
    478 				setting);
    479 			return -EINVAL;
    480 		}
    481 
    482 		return 0;
    483 	} else if (password)
    484 		goto validate;
    485 
    486 	secret = l_queue_find(secrets, eap_secret_info_match, setting2);
    487 	if (!secret) {
    488 		eap_append_secret(out_missing, EAP_SECRET_REMOTE_PASSWORD,
    489 					setting2, NULL, identity,
    490 					EAP_CACHE_TEMPORARY);
    491 		return 0;
    492 	}
    493 
    494 	password = l_strdup(secret->value);
    495 
    496 validate:
    497 	if (!mschap_nt_password_hash(password, hash))
    498 		r = -EINVAL;
    499 
    500 cleanup:
    501 	explicit_bzero(password, strlen(password));
    502 	return r;
    503 }
    504 
    505 static bool eap_mschapv2_load_settings(struct eap_state *eap,
    506 					struct l_settings *settings,
    507 					const char *prefix)
    508 {
    509 	struct eap_mschapv2_state *state;
    510 	L_AUTO_FREE_VAR(char *, password) = NULL;
    511 	char setting[64];
    512 
    513 	state = l_new(struct eap_mschapv2_state, 1);
    514 
    515 	snprintf(setting, sizeof(setting), "%sIdentity", prefix);
    516 	state->user = l_settings_get_string(settings, "Security", setting);
    517 	if (!state->user)
    518 		goto error;
    519 
    520 	state->user_len = strlen(state->user);
    521 
    522 	/* Either read the password-hash from hexdump or password and hash it */
    523 	snprintf(setting, sizeof(setting), "%sPassword", prefix);
    524 	password = l_settings_get_string(settings, "Security", setting);
    525 
    526 	if (password) {
    527 		set_password_from_string(state, password);
    528 		explicit_bzero(password, strlen(password));
    529 	} else {
    530 		size_t hash_len;
    531 		uint8_t *hash;
    532 
    533 		snprintf(setting, sizeof(setting), "%sPassword-Hash", prefix);
    534 		hash = l_settings_get_bytes(settings, "Security", setting,
    535 						&hash_len);
    536 		if (!hash)
    537 			goto error;
    538 
    539 		memcpy(state->password_hash, hash, 16);
    540 		explicit_bzero(hash, 16);
    541 		l_free(hash);
    542 	}
    543 
    544 	eap_set_data(eap, state);
    545 
    546 	return true;
    547 
    548 error:
    549 	l_free(state->user);
    550 	l_free(state);
    551 	return false;
    552 }
    553 
    554 static struct eap_method eap_mschapv2 = {
    555 	.request_type = EAP_TYPE_MSCHAPV2,
    556 	.exports_msk = true,
    557 	.name = "MSCHAPV2",
    558 
    559 	.free = eap_mschapv2_free,
    560 	.handle_request = eap_mschapv2_handle_request,
    561 	.check_settings = eap_mschapv2_check_settings,
    562 	.load_settings = eap_mschapv2_load_settings,
    563 	.reset_state = eap_mschapv2_reset_state,
    564 };
    565 
    566 static int eap_mschapv2_init(void)
    567 {
    568 	l_debug("");
    569 
    570 	if (!l_checksum_is_supported(L_CHECKSUM_MD4, false)) {
    571 		l_warn("EAP_MSCHAPv2 init: MD4 support not found, skipping");
    572 		l_warn("Ensure that CONFIG_CRYPTO_MD4 is enabled");
    573 		return -ENOTSUP;
    574 	}
    575 
    576 	return eap_register_method(&eap_mschapv2);
    577 }
    578 
    579 static void eap_mschapv2_exit(void)
    580 {
    581 	l_debug("");
    582 	eap_unregister_method(&eap_mschapv2);
    583 }
    584 
    585 EAP_METHOD_BUILTIN(eap_mschapv2, eap_mschapv2_init, eap_mschapv2_exit)