3
0
mirror of https://git.kernel.org/pub/scm/network/wireless/iwd.git synced 2024-11-22 14:39:39 +01:00

mschaputil: Add MS CHAP utilities

This commit is contained in:
Tim Kourt 2018-10-23 16:33:51 -07:00 committed by Denis Kenzior
parent a2df686d97
commit b46376980a
3 changed files with 140 additions and 2 deletions

View File

@ -109,7 +109,8 @@ eap_sources = src/eap.c src/eap.h src/eap-private.h \
src/simutil.h src/simutil.c \
src/simauth.h src/simauth.c \
src/watchlist.h src/watchlist.c \
src/eap-tls-common.h src/eap-tls-common.c
src/eap-tls-common.h src/eap-tls-common.c \
src/mschaputil.h src/mschaputil.c
if DAEMON
libexec_PROGRAMS += src/iwd
@ -332,7 +333,8 @@ unit_test_eapol_SOURCES = unit/test-eapol.c \
src/eap.h src/eap.c src/eap-private.h \
src/eap-tls.c src/eap-ttls.c \
src/eap-md5.c src/util.c \
src/eap-tls-common.h src/eap-tls-common.c
src/eap-tls-common.h src/eap-tls-common.c \
src/mschaputil.h src/mschaputil.c
unit_test_eapol_LDADD = $(ell_ldadd)
unit_test_ssid_to_utf8_SOURCES = src/util.h src/util.c \

108
src/mschaputil.c Normal file
View File

@ -0,0 +1,108 @@
/*
*
* Wireless daemon for Linux
*
* Copyright (C) 2018 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ell/ell.h>
#include "mschaputil.h"
static bool mschap_des_encrypt(const uint8_t *challenge, const uint8_t *key,
uint8_t *cipher_text)
{
uint8_t pkey[8], tmp;
int i;
struct l_cipher *cipher;
uint8_t next;
for (i = 0, next = 0; i < 7; ++i) {
tmp = key[i];
pkey[i] = (tmp >> i) | next | 1;
next = tmp << (7 - i);
}
pkey[i] = next | 1;
cipher = l_cipher_new(L_CIPHER_DES, pkey, 8);
if (!cipher)
return false;
l_cipher_encrypt(cipher, challenge, cipher_text, 8);
l_cipher_free(cipher);
return true;
}
bool mschap_challenge_response(const uint8_t *challenge,
const uint8_t *password_hash, uint8_t *response)
{
uint8_t buf[21];
memset(buf, 0, sizeof(buf));
memcpy(buf, password_hash, 16);
if (!mschap_des_encrypt(challenge, buf + 0, response + 0))
return false;
if (!mschap_des_encrypt(challenge, buf + 7, response + 8))
return false;
if (!mschap_des_encrypt(challenge, buf + 14, response + 16))
return false;
return true;
}
bool mschap_nt_password_hash(const char *password, uint8_t *password_hash)
{
size_t size = l_utf8_strlen(password);
size_t bsize = strlen(password);
uint16_t buffer[size];
unsigned int i, pos;
struct l_checksum *check;
for (i = 0, pos = 0; i < size; ++i) {
wchar_t val;
pos += l_utf8_get_codepoint(password + pos, bsize - pos, &val);
if (val > 0xFFFF) {
l_error("Encountered password with value not valid in "
"ucs-2");
return false;
}
buffer[i] = L_CPU_TO_LE16(val);
}
check = l_checksum_new(L_CHECKSUM_MD4);
if (!check)
return false;
l_checksum_update(check, (uint8_t *) buffer, size * 2);
l_checksum_get_digest(check, password_hash, 16);
l_checksum_free(check);
return true;
}

28
src/mschaputil.h Normal file
View File

@ -0,0 +1,28 @@
/*
*
* Wireless daemon for Linux
*
* Copyright (C) 2018 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#define NT_CHALLENGE_RESPONSE_LEN 24
bool mschap_challenge_response(const uint8_t *challenge,
const uint8_t *password_hash,
uint8_t *response);
bool mschap_nt_password_hash(const char *password, uint8_t *password_hash);