mschaputil.c (8646B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2018-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 30 #include <ell/ell.h> 31 32 #include "src/missing.h" 33 #include "src/mschaputil.h" 34 35 /** 36 * Internal function for generate_nt_response. 37 * The DES keys specified for generate_nt_response are 56bit, while the api we 38 * use takes 64bit keys, so we have to generate the parity bits. 39 **/ 40 static bool mschap_des_encrypt(const uint8_t challenge[static 8], 41 const uint8_t key[static 7], 42 uint8_t cipher_text[static 8]) 43 { 44 uint8_t pkey[8], tmp; 45 int i; 46 struct l_cipher *cipher; 47 uint8_t next; 48 49 for (i = 0, next = 0; i < 7; ++i) { 50 tmp = key[i]; 51 pkey[i] = (tmp >> i) | next | 1; 52 next = tmp << (7 - i); 53 } 54 55 pkey[i] = next | 1; 56 57 cipher = l_cipher_new(L_CIPHER_DES, pkey, 8); 58 explicit_bzero(pkey, 8); 59 60 if (!cipher) 61 return false; 62 63 l_cipher_encrypt(cipher, challenge, cipher_text, 8); 64 l_cipher_free(cipher); 65 66 return true; 67 } 68 69 bool mschap_challenge_response(const uint8_t *challenge, 70 const uint8_t *password_hash, uint8_t *response) 71 { 72 uint8_t buf[21]; 73 bool r; 74 75 memset(buf, 0, sizeof(buf)); 76 memcpy(buf, password_hash, 16); 77 78 r = mschap_des_encrypt(challenge, buf + 0, response + 0) && 79 mschap_des_encrypt(challenge, buf + 7, response + 8) && 80 mschap_des_encrypt(challenge, buf + 14, response + 16); 81 82 explicit_bzero(buf, sizeof(buf)); 83 return r; 84 } 85 86 /** 87 * Hash the utf8 encoded nt password. 88 * It is assumed, that the password is valid utf8! 89 * The rfc says "unicode-char", but never specifies which encoding. 90 * This function converts the password to ucs-2. 91 * The example in the code uses LE for the unicode chars, so it is forced here. 92 * https://tools.ietf.org/html/draft-ietf-pppext-mschap-00#ref-8 93 */ 94 bool mschap_nt_password_hash(const char *password, uint8_t *password_hash) 95 { 96 size_t size = l_utf8_strlen(password); 97 size_t bsize = strlen(password); 98 uint16_t buffer[size]; 99 unsigned int i, pos; 100 struct l_checksum *check; 101 bool r = false; 102 103 for (i = 0, pos = 0; i < size; ++i) { 104 wchar_t val; 105 106 pos += l_utf8_get_codepoint(password + pos, bsize - pos, &val); 107 108 if (val > 0xFFFF) { 109 l_error("Encountered password with value not valid in " 110 "ucs-2"); 111 goto cleanup; 112 } 113 114 buffer[i] = L_CPU_TO_LE16(val); 115 } 116 117 check = l_checksum_new(L_CHECKSUM_MD4); 118 if (!check) 119 goto cleanup; 120 121 l_checksum_update(check, (uint8_t *) buffer, size * 2); 122 l_checksum_get_digest(check, password_hash, 16); 123 l_checksum_free(check); 124 r = true; 125 126 cleanup: 127 explicit_bzero(buffer, size * 2); 128 return r; 129 } 130 131 static const char *mschapv2_exlude_domain_name(const char *username) 132 { 133 const char *c; 134 135 for (c = username; *c; c++) { 136 if (*c != '\\') 137 continue; 138 139 return c + 1; 140 } 141 142 return username; 143 } 144 145 /** 146 * Internal function to generate the challenge used in nt_response 147 * https://tools.ietf.org/html/rfc2759 148 * 149 * @peer_challenge: The challenge generated by the peer (us) 150 * @server_challenge: The challenge generated by the authenticator 151 * @user: The username utf8 encoded 152 * @challenge: The destination 153 * 154 * Returns: true on success, false if hash/encrypt couldn't be done 155 **/ 156 static bool mschapv2_challenge_hash(const uint8_t *peer_challenge, 157 const uint8_t *server_challenge, 158 const char *username, 159 uint8_t challenge[static 8]) 160 { 161 struct l_checksum *check; 162 163 check = l_checksum_new(L_CHECKSUM_SHA1); 164 if (!check) 165 return false; 166 167 username = mschapv2_exlude_domain_name(username); 168 169 l_checksum_update(check, peer_challenge, 16); 170 l_checksum_update(check, server_challenge, 16); 171 l_checksum_update(check, username, strlen(username)); 172 173 l_checksum_get_digest(check, challenge, 8); 174 l_checksum_free(check); 175 176 return true; 177 } 178 179 /** 180 * Generate the nt_response for mschapv2. 181 * This function is specified in: 182 * https://tools.ietf.org/html/rfc2759 183 * 184 * @password_hash: The MD4 hash of the ucs2 encoded user password 185 * @peer_challenge: the challenge generated by the peer (us) 186 * @server_challenge: the challenge generated by the authenticator 187 * @user: The username, utf8 encoded 188 * @nt_response: The destination 189 * 190 * Returns: true on success, false if hash/encrypt couldn't be done 191 **/ 192 bool mschapv2_generate_nt_response(const uint8_t password_hash[static 16], 193 const uint8_t peer_challenge[static 16], 194 const uint8_t server_challenge[static 16], 195 const char *user, uint8_t response[static 24]) 196 { 197 uint8_t challenge[8]; 198 uint8_t buffer[21]; 199 bool r; 200 201 if (!mschapv2_challenge_hash(peer_challenge, server_challenge, user, 202 challenge)) 203 return false; 204 205 memset(buffer, 0, sizeof(buffer)); 206 memcpy(buffer, password_hash, 16); 207 208 r = mschap_des_encrypt(challenge, buffer + 0, response + 0) && 209 mschap_des_encrypt(challenge, buffer + 7, response + 8) && 210 mschap_des_encrypt(challenge, buffer + 14, response + 16); 211 212 explicit_bzero(buffer, sizeof(buffer)); 213 return r; 214 } 215 216 /** 217 * Generate the hash of the password hash 218 * 219 * @password_hash: The hash of the password 220 * @password_hash_hash: The MD4 hash of the password hash 221 * 222 * Returns: true on success, false if hash/encrypt couldn't be done 223 **/ 224 bool mschapv2_hash_nt_password_hash(const uint8_t password_hash[static 16], 225 uint8_t password_hash_hash[static 16]) 226 { 227 struct l_checksum *check; 228 229 check = l_checksum_new(L_CHECKSUM_MD4); 230 if (!check) 231 return false; 232 233 l_checksum_update(check, password_hash, 16); 234 l_checksum_get_digest(check, password_hash_hash, 16); 235 l_checksum_free(check); 236 237 return true; 238 } 239 240 /** 241 * Generate the mschapv2 authenticator response for verifying authenticator 242 * This function is specified in: 243 * https://tools.ietf.org/html/rfc2759 244 * 245 * @password_hash_hash: The MD4 hash of the password hash 246 * @nt_response: The nt_response generated for this exchange 247 * @peer_challenge: The challenge generated by the peer (us) 248 * @server_challenge: The challenge generated by the authenticator 249 * @user: The username utf8 encoded 250 * @response: The destination 251 * 252 * Returns: true on success, false if hash/encrypt couldn't be done 253 **/ 254 bool mschapv2_generate_authenticator_response( 255 const uint8_t pw_hash_hash[static 16], 256 const uint8_t nt_response[static 24], 257 const uint8_t peer_challenge[static 16], 258 const uint8_t server_challenge[static 16], 259 const char *user, char response[static 42]) 260 { 261 static const uint8_t magic1[] = { 262 0x4D, 0x61, 0x67, 0x69, 0x63, 0x20, 0x73, 0x65, 0x72, 0x76, 263 0x65, 0x72, 0x20, 0x74, 0x6F, 0x20, 0x63, 0x6C, 0x69, 0x65, 264 0x6E, 0x74, 0x20, 0x73, 0x69, 0x67, 0x6E, 0x69, 0x6E, 0x67, 265 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x74, 0x61, 0x6E, 0x74 266 }; 267 268 static const uint8_t magic2[] = { 269 0x50, 0x61, 0x64, 0x20, 0x74, 0x6F, 0x20, 0x6D, 0x61, 0x6B, 270 0x65, 0x20, 0x69, 0x74, 0x20, 0x64, 0x6F, 0x20, 0x6D, 0x6F, 271 0x72, 0x65, 0x20, 0x74, 0x68, 0x61, 0x6E, 0x20, 0x6F, 0x6E, 272 0x65, 0x20, 0x69, 0x74, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6F, 273 0x6E 274 }; 275 276 uint8_t digest[20]; 277 uint8_t challenge[8]; 278 char *ascii; 279 struct l_checksum *check; 280 281 check = l_checksum_new(L_CHECKSUM_SHA1); 282 if (!check) 283 return false; 284 285 l_checksum_update(check, pw_hash_hash, 16); 286 l_checksum_update(check, nt_response, 24); 287 l_checksum_update(check, magic1, sizeof(magic1)); 288 l_checksum_get_digest(check, digest, sizeof(digest)); 289 l_checksum_free(check); 290 291 if (!mschapv2_challenge_hash(peer_challenge, server_challenge, user, 292 challenge)) 293 return false; 294 295 check = l_checksum_new(L_CHECKSUM_SHA1); 296 if (!check) 297 return false; 298 299 l_checksum_update(check, digest, sizeof(digest)); 300 l_checksum_update(check, challenge, sizeof(challenge)); 301 l_checksum_update(check, magic2, sizeof(magic2)); 302 l_checksum_get_digest(check, digest, sizeof(digest)); 303 l_checksum_free(check); 304 305 response[0] = 'S'; 306 response[1] = '='; 307 308 ascii = l_util_hexstring_upper(digest, sizeof(digest)); 309 if (!ascii) 310 return false; 311 312 memcpy(response + 2, ascii, 40); 313 l_free(ascii); 314 315 return true; 316 }