[124] | 1 | /*
|
---|
| 2 | Unix SMB/CIFS implementation.
|
---|
| 3 | SMB parameters and setup
|
---|
| 4 | Copyright (C) Andrew Tridgell 1992-1998
|
---|
| 5 | Modified by Jeremy Allison 1995.
|
---|
| 6 | Copyright (C) Jeremy Allison 1995-2000.
|
---|
| 7 | Copyright (C) Luke Kennethc Casson Leighton 1996-2000.
|
---|
| 8 | Copyright (C) Andrew Bartlett <abartlet@samba.org> 2002-2003
|
---|
| 9 |
|
---|
| 10 | This program is free software; you can redistribute it and/or modify
|
---|
| 11 | it under the terms of the GNU General Public License as published by
|
---|
| 12 | the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | (at your option) any later version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful,
|
---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | GNU General Public License for more details.
|
---|
| 19 |
|
---|
| 20 | You should have received a copy of the GNU General Public License
|
---|
| 21 | along with this program; if not, write to the Free Software
|
---|
| 22 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | */
|
---|
| 24 |
|
---|
| 25 | #include "includes.h"
|
---|
| 26 | #include "byteorder.h"
|
---|
| 27 |
|
---|
| 28 | void SMBencrypt_hash(const uchar lm_hash[16], const uchar *c8, uchar p24[24])
|
---|
| 29 | {
|
---|
| 30 | uchar p21[21];
|
---|
| 31 |
|
---|
| 32 | memset(p21,'\0',21);
|
---|
| 33 | memcpy(p21, lm_hash, 16);
|
---|
| 34 |
|
---|
| 35 | SMBOWFencrypt(p21, c8, p24);
|
---|
| 36 |
|
---|
| 37 | #ifdef DEBUG_PASSWORD
|
---|
| 38 | DEBUG(100,("SMBencrypt_hash: lm#, challenge, response\n"));
|
---|
| 39 | dump_data(100, (const char *)p21, 16);
|
---|
| 40 | dump_data(100, (const char *)c8, 8);
|
---|
| 41 | dump_data(100, (const char *)p24, 24);
|
---|
| 42 | #endif
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | /*
|
---|
| 46 | This implements the X/Open SMB password encryption
|
---|
| 47 | It takes a password ('unix' string), a 8 byte "crypt key"
|
---|
| 48 | and puts 24 bytes of encrypted password into p24
|
---|
| 49 |
|
---|
| 50 | Returns False if password must have been truncated to create LM hash
|
---|
| 51 | */
|
---|
| 52 |
|
---|
| 53 | BOOL SMBencrypt(const char *passwd, const uchar *c8, uchar p24[24])
|
---|
| 54 | {
|
---|
| 55 | BOOL ret;
|
---|
| 56 | uchar lm_hash[16];
|
---|
| 57 |
|
---|
| 58 | ret = E_deshash(passwd, lm_hash);
|
---|
| 59 | SMBencrypt_hash(lm_hash, c8, p24);
|
---|
| 60 | return ret;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Creates the MD4 Hash of the users password in NT UNICODE.
|
---|
| 65 | * @param passwd password in 'unix' charset.
|
---|
| 66 | * @param p16 return password hashed with md4, caller allocated 16 byte buffer
|
---|
| 67 | */
|
---|
| 68 |
|
---|
| 69 | void E_md4hash(const char *passwd, uchar p16[16])
|
---|
| 70 | {
|
---|
| 71 | int len;
|
---|
| 72 | smb_ucs2_t wpwd[129];
|
---|
| 73 |
|
---|
| 74 | /* Password must be converted to NT unicode - null terminated. */
|
---|
| 75 | push_ucs2(NULL, wpwd, (const char *)passwd, 256, STR_UNICODE|STR_NOALIGN|STR_TERMINATE);
|
---|
| 76 | /* Calculate length in bytes */
|
---|
| 77 | len = strlen_w(wpwd) * sizeof(int16);
|
---|
| 78 |
|
---|
| 79 | mdfour(p16, (unsigned char *)wpwd, len);
|
---|
| 80 | ZERO_STRUCT(wpwd);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | * Creates the MD5 Hash of a combination of 16 byte salt and 16 byte NT hash.
|
---|
| 85 | * @param 16 byte salt.
|
---|
| 86 | * @param 16 byte NT hash.
|
---|
| 87 | * @param 16 byte return hashed with md5, caller allocated 16 byte buffer
|
---|
| 88 | */
|
---|
| 89 |
|
---|
| 90 | void E_md5hash(const uchar salt[16], const uchar nthash[16], uchar hash_out[16])
|
---|
| 91 | {
|
---|
| 92 | struct MD5Context tctx;
|
---|
| 93 | uchar array[32];
|
---|
| 94 |
|
---|
| 95 | memset(hash_out, '\0', 16);
|
---|
| 96 | memcpy(array, salt, 16);
|
---|
| 97 | memcpy(&array[16], nthash, 16);
|
---|
| 98 | MD5Init(&tctx);
|
---|
| 99 | MD5Update(&tctx, array, 32);
|
---|
| 100 | MD5Final(hash_out, &tctx);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
| 104 | * Creates the DES forward-only Hash of the users password in DOS ASCII charset
|
---|
| 105 | * @param passwd password in 'unix' charset.
|
---|
| 106 | * @param p16 return password hashed with DES, caller allocated 16 byte buffer
|
---|
| 107 | * @return False if password was > 14 characters, and therefore may be incorrect, otherwise True
|
---|
| 108 | * @note p16 is filled in regardless
|
---|
| 109 | */
|
---|
| 110 |
|
---|
| 111 | BOOL E_deshash(const char *passwd, uchar p16[16])
|
---|
| 112 | {
|
---|
| 113 | BOOL ret = True;
|
---|
| 114 | fstring dospwd;
|
---|
| 115 | ZERO_STRUCT(dospwd);
|
---|
| 116 |
|
---|
| 117 | /* Password must be converted to DOS charset - null terminated, uppercase. */
|
---|
| 118 | push_ascii(dospwd, passwd, sizeof(dospwd), STR_UPPER|STR_TERMINATE);
|
---|
| 119 |
|
---|
| 120 | /* Only the fisrt 14 chars are considered, password need not be null terminated. */
|
---|
| 121 | E_P16((const unsigned char *)dospwd, p16);
|
---|
| 122 |
|
---|
| 123 | if (strlen(dospwd) > 14) {
|
---|
| 124 | ret = False;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | ZERO_STRUCT(dospwd);
|
---|
| 128 |
|
---|
| 129 | return ret;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
| 133 | * Creates the MD4 and DES (LM) Hash of the users password.
|
---|
| 134 | * MD4 is of the NT Unicode, DES is of the DOS UPPERCASE password.
|
---|
| 135 | * @param passwd password in 'unix' charset.
|
---|
| 136 | * @param nt_p16 return password hashed with md4, caller allocated 16 byte buffer
|
---|
| 137 | * @param p16 return password hashed with des, caller allocated 16 byte buffer
|
---|
| 138 | */
|
---|
| 139 |
|
---|
| 140 | /* Does both the NT and LM owfs of a user's password */
|
---|
| 141 | void nt_lm_owf_gen(const char *pwd, uchar nt_p16[16], uchar p16[16])
|
---|
| 142 | {
|
---|
| 143 | /* Calculate the MD4 hash (NT compatible) of the password */
|
---|
| 144 | memset(nt_p16, '\0', 16);
|
---|
| 145 | E_md4hash(pwd, nt_p16);
|
---|
| 146 |
|
---|
| 147 | #ifdef DEBUG_PASSWORD
|
---|
| 148 | DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));
|
---|
| 149 | dump_data(120, pwd, strlen(pwd));
|
---|
| 150 | dump_data(100, (char *)nt_p16, 16);
|
---|
| 151 | #endif
|
---|
| 152 |
|
---|
| 153 | E_deshash(pwd, (uchar *)p16);
|
---|
| 154 |
|
---|
| 155 | #ifdef DEBUG_PASSWORD
|
---|
| 156 | DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
|
---|
| 157 | dump_data(120, pwd, strlen(pwd));
|
---|
| 158 | dump_data(100, (char *)p16, 16);
|
---|
| 159 | #endif
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /* Does both the NTLMv2 owfs of a user's password */
|
---|
| 163 | BOOL ntv2_owf_gen(const uchar owf[16],
|
---|
| 164 | const char *user_in, const char *domain_in,
|
---|
| 165 | BOOL upper_case_domain, /* Transform the domain into UPPER case */
|
---|
| 166 | uchar kr_buf[16])
|
---|
| 167 | {
|
---|
| 168 | smb_ucs2_t *user;
|
---|
| 169 | smb_ucs2_t *domain;
|
---|
| 170 |
|
---|
| 171 | size_t user_byte_len;
|
---|
| 172 | size_t domain_byte_len;
|
---|
| 173 |
|
---|
| 174 | HMACMD5Context ctx;
|
---|
| 175 |
|
---|
| 176 | user_byte_len = push_ucs2_allocate(&user, user_in);
|
---|
| 177 | if (user_byte_len == (size_t)-1) {
|
---|
| 178 | DEBUG(0, ("push_uss2_allocate() for user returned -1 (probably malloc() failure)\n"));
|
---|
| 179 | return False;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | domain_byte_len = push_ucs2_allocate(&domain, domain_in);
|
---|
| 183 | if (domain_byte_len == (size_t)-1) {
|
---|
| 184 | DEBUG(0, ("push_uss2_allocate() for domain returned -1 (probably malloc() failure)\n"));
|
---|
[140] | 185 | SAFE_FREE(user);
|
---|
[124] | 186 | return False;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | strupper_w(user);
|
---|
| 190 |
|
---|
| 191 | if (upper_case_domain)
|
---|
| 192 | strupper_w(domain);
|
---|
| 193 |
|
---|
| 194 | SMB_ASSERT(user_byte_len >= 2);
|
---|
| 195 | SMB_ASSERT(domain_byte_len >= 2);
|
---|
| 196 |
|
---|
| 197 | /* We don't want null termination */
|
---|
| 198 | user_byte_len = user_byte_len - 2;
|
---|
| 199 | domain_byte_len = domain_byte_len - 2;
|
---|
| 200 |
|
---|
| 201 | hmac_md5_init_limK_to_64(owf, 16, &ctx);
|
---|
| 202 | hmac_md5_update((const unsigned char *)user, user_byte_len, &ctx);
|
---|
| 203 | hmac_md5_update((const unsigned char *)domain, domain_byte_len, &ctx);
|
---|
| 204 | hmac_md5_final(kr_buf, &ctx);
|
---|
| 205 |
|
---|
| 206 | #ifdef DEBUG_PASSWORD
|
---|
| 207 | DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
|
---|
| 208 | dump_data(100, (const char *)user, user_byte_len);
|
---|
| 209 | dump_data(100, (const char *)domain, domain_byte_len);
|
---|
| 210 | dump_data(100, (const char *)owf, 16);
|
---|
| 211 | dump_data(100, (const char *)kr_buf, 16);
|
---|
| 212 | #endif
|
---|
| 213 |
|
---|
| 214 | SAFE_FREE(user);
|
---|
| 215 | SAFE_FREE(domain);
|
---|
| 216 | return True;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /* Does the des encryption from the NT or LM MD4 hash. */
|
---|
| 220 | void SMBOWFencrypt(const uchar passwd[16], const uchar *c8, uchar p24[24])
|
---|
| 221 | {
|
---|
| 222 | uchar p21[21];
|
---|
| 223 |
|
---|
| 224 | ZERO_STRUCT(p21);
|
---|
| 225 |
|
---|
| 226 | memcpy(p21, passwd, 16);
|
---|
| 227 | E_P24(p21, c8, p24);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
|
---|
| 231 | void NTLMSSPOWFencrypt(const uchar passwd[8], const uchar *ntlmchalresp, uchar p24[24])
|
---|
| 232 | {
|
---|
| 233 | uchar p21[21];
|
---|
| 234 |
|
---|
| 235 | memset(p21,'\0',21);
|
---|
| 236 | memcpy(p21, passwd, 8);
|
---|
| 237 | memset(p21 + 8, 0xbd, 8);
|
---|
| 238 |
|
---|
| 239 | E_P24(p21, ntlmchalresp, p24);
|
---|
| 240 | #ifdef DEBUG_PASSWORD
|
---|
| 241 | DEBUG(100,("NTLMSSPOWFencrypt: p21, c8, p24\n"));
|
---|
| 242 | dump_data(100, (char *)p21, 21);
|
---|
| 243 | dump_data(100, (const char *)ntlmchalresp, 8);
|
---|
| 244 | dump_data(100, (char *)p24, 24);
|
---|
| 245 | #endif
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 |
|
---|
| 249 | /* Does the des encryption. */
|
---|
| 250 |
|
---|
| 251 | void SMBNTencrypt_hash(const uchar nt_hash[16], uchar *c8, uchar *p24)
|
---|
| 252 | {
|
---|
| 253 | uchar p21[21];
|
---|
| 254 |
|
---|
| 255 | memset(p21,'\0',21);
|
---|
| 256 | memcpy(p21, nt_hash, 16);
|
---|
| 257 | SMBOWFencrypt(p21, c8, p24);
|
---|
| 258 |
|
---|
| 259 | #ifdef DEBUG_PASSWORD
|
---|
| 260 | DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
|
---|
| 261 | dump_data(100, (char *)p21, 16);
|
---|
| 262 | dump_data(100, (char *)c8, 8);
|
---|
| 263 | dump_data(100, (char *)p24, 24);
|
---|
| 264 | #endif
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | /* Does the NT MD4 hash then des encryption. Plaintext version of the above. */
|
---|
| 268 |
|
---|
| 269 | void SMBNTencrypt(const char *passwd, uchar *c8, uchar *p24)
|
---|
| 270 | {
|
---|
| 271 | uchar nt_hash[16];
|
---|
| 272 | E_md4hash(passwd, nt_hash);
|
---|
| 273 | SMBNTencrypt_hash(nt_hash, c8, p24);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | /* Does the md5 encryption from the Key Response for NTLMv2. */
|
---|
| 277 | void SMBOWFencrypt_ntv2(const uchar kr[16],
|
---|
| 278 | const DATA_BLOB *srv_chal,
|
---|
| 279 | const DATA_BLOB *cli_chal,
|
---|
| 280 | uchar resp_buf[16])
|
---|
| 281 | {
|
---|
| 282 | HMACMD5Context ctx;
|
---|
| 283 |
|
---|
| 284 | hmac_md5_init_limK_to_64(kr, 16, &ctx);
|
---|
| 285 | hmac_md5_update(srv_chal->data, srv_chal->length, &ctx);
|
---|
| 286 | hmac_md5_update(cli_chal->data, cli_chal->length, &ctx);
|
---|
| 287 | hmac_md5_final(resp_buf, &ctx);
|
---|
| 288 |
|
---|
| 289 | #ifdef DEBUG_PASSWORD
|
---|
| 290 | DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, cli_chal, resp_buf\n"));
|
---|
| 291 | dump_data(100, (const char *)srv_chal->data, srv_chal->length);
|
---|
| 292 | dump_data(100, (const char *)cli_chal->data, cli_chal->length);
|
---|
| 293 | dump_data(100, (const char *)resp_buf, 16);
|
---|
| 294 | #endif
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | void SMBsesskeygen_ntv2(const uchar kr[16],
|
---|
| 298 | const uchar * nt_resp, uint8 sess_key[16])
|
---|
| 299 | {
|
---|
| 300 | /* a very nice, 128 bit, variable session key */
|
---|
| 301 |
|
---|
| 302 | HMACMD5Context ctx;
|
---|
| 303 |
|
---|
| 304 | hmac_md5_init_limK_to_64(kr, 16, &ctx);
|
---|
| 305 | hmac_md5_update(nt_resp, 16, &ctx);
|
---|
| 306 | hmac_md5_final((unsigned char *)sess_key, &ctx);
|
---|
| 307 |
|
---|
| 308 | #ifdef DEBUG_PASSWORD
|
---|
| 309 | DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
|
---|
| 310 | dump_data(100, (const char *)sess_key, 16);
|
---|
| 311 | #endif
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | void SMBsesskeygen_ntv1(const uchar kr[16],
|
---|
| 315 | const uchar * nt_resp, uint8 sess_key[16])
|
---|
| 316 | {
|
---|
| 317 | /* yes, this session key does not change - yes, this
|
---|
| 318 | is a problem - but it is 128 bits */
|
---|
| 319 |
|
---|
| 320 | mdfour((unsigned char *)sess_key, kr, 16);
|
---|
| 321 |
|
---|
| 322 | #ifdef DEBUG_PASSWORD
|
---|
| 323 | DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
|
---|
| 324 | dump_data(100, (const char *)sess_key, 16);
|
---|
| 325 | #endif
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | void SMBsesskeygen_lm_sess_key(const uchar lm_hash[16],
|
---|
| 329 | const uchar lm_resp[24], /* only uses 8 */
|
---|
| 330 | uint8 sess_key[16])
|
---|
| 331 | {
|
---|
| 332 | uchar p24[24];
|
---|
| 333 | uchar partial_lm_hash[16];
|
---|
| 334 |
|
---|
| 335 | memcpy(partial_lm_hash, lm_hash, 8);
|
---|
| 336 | memset(partial_lm_hash + 8, 0xbd, 8);
|
---|
| 337 |
|
---|
| 338 | SMBOWFencrypt(partial_lm_hash, lm_resp, p24);
|
---|
| 339 |
|
---|
| 340 | memcpy(sess_key, p24, 16);
|
---|
| 341 |
|
---|
| 342 | #ifdef DEBUG_PASSWORD
|
---|
| 343 | DEBUG(100, ("SMBsesskeygen_lmv1_jerry:\n"));
|
---|
| 344 | dump_data(100, (const char *)sess_key, 16);
|
---|
| 345 | #endif
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | DATA_BLOB NTLMv2_generate_names_blob(const char *hostname,
|
---|
| 349 | const char *domain)
|
---|
| 350 | {
|
---|
| 351 | DATA_BLOB names_blob = data_blob(NULL, 0);
|
---|
| 352 |
|
---|
| 353 | msrpc_gen(&names_blob, "aaa",
|
---|
| 354 | NTLMSSP_NAME_TYPE_DOMAIN, domain,
|
---|
| 355 | NTLMSSP_NAME_TYPE_SERVER, hostname,
|
---|
| 356 | 0, "");
|
---|
| 357 | return names_blob;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | static DATA_BLOB NTLMv2_generate_client_data(const DATA_BLOB *names_blob)
|
---|
| 361 | {
|
---|
| 362 | uchar client_chal[8];
|
---|
| 363 | DATA_BLOB response = data_blob(NULL, 0);
|
---|
| 364 | char long_date[8];
|
---|
| 365 |
|
---|
| 366 | generate_random_buffer(client_chal, sizeof(client_chal));
|
---|
| 367 |
|
---|
| 368 | put_long_date(long_date, time(NULL));
|
---|
| 369 |
|
---|
| 370 | /* See http://www.ubiqx.org/cifs/SMB.html#SMB.8.5 */
|
---|
| 371 |
|
---|
| 372 | msrpc_gen(&response, "ddbbdb",
|
---|
| 373 | 0x00000101, /* Header */
|
---|
| 374 | 0, /* 'Reserved' */
|
---|
| 375 | long_date, 8, /* Timestamp */
|
---|
| 376 | client_chal, 8, /* client challenge */
|
---|
| 377 | 0, /* Unknown */
|
---|
| 378 | names_blob->data, names_blob->length); /* End of name list */
|
---|
| 379 |
|
---|
| 380 | return response;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | static DATA_BLOB NTLMv2_generate_response(const uchar ntlm_v2_hash[16],
|
---|
| 384 | const DATA_BLOB *server_chal,
|
---|
| 385 | const DATA_BLOB *names_blob)
|
---|
| 386 | {
|
---|
| 387 | uchar ntlmv2_response[16];
|
---|
| 388 | DATA_BLOB ntlmv2_client_data;
|
---|
| 389 | DATA_BLOB final_response;
|
---|
| 390 |
|
---|
| 391 | /* NTLMv2 */
|
---|
| 392 | /* generate some data to pass into the response function - including
|
---|
| 393 | the hostname and domain name of the server */
|
---|
| 394 | ntlmv2_client_data = NTLMv2_generate_client_data(names_blob);
|
---|
| 395 |
|
---|
| 396 | /* Given that data, and the challenge from the server, generate a response */
|
---|
| 397 | SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &ntlmv2_client_data, ntlmv2_response);
|
---|
| 398 |
|
---|
| 399 | final_response = data_blob(NULL, sizeof(ntlmv2_response) + ntlmv2_client_data.length);
|
---|
| 400 |
|
---|
| 401 | memcpy(final_response.data, ntlmv2_response, sizeof(ntlmv2_response));
|
---|
| 402 |
|
---|
| 403 | memcpy(final_response.data+sizeof(ntlmv2_response),
|
---|
| 404 | ntlmv2_client_data.data, ntlmv2_client_data.length);
|
---|
| 405 |
|
---|
| 406 | data_blob_free(&ntlmv2_client_data);
|
---|
| 407 |
|
---|
| 408 | return final_response;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | static DATA_BLOB LMv2_generate_response(const uchar ntlm_v2_hash[16],
|
---|
| 412 | const DATA_BLOB *server_chal)
|
---|
| 413 | {
|
---|
| 414 | uchar lmv2_response[16];
|
---|
| 415 | DATA_BLOB lmv2_client_data = data_blob(NULL, 8);
|
---|
| 416 | DATA_BLOB final_response = data_blob(NULL, 24);
|
---|
| 417 |
|
---|
| 418 | /* LMv2 */
|
---|
| 419 | /* client-supplied random data */
|
---|
| 420 | generate_random_buffer(lmv2_client_data.data, lmv2_client_data.length);
|
---|
| 421 |
|
---|
| 422 | /* Given that data, and the challenge from the server, generate a response */
|
---|
| 423 | SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &lmv2_client_data, lmv2_response);
|
---|
| 424 | memcpy(final_response.data, lmv2_response, sizeof(lmv2_response));
|
---|
| 425 |
|
---|
| 426 | /* after the first 16 bytes is the random data we generated above,
|
---|
| 427 | so the server can verify us with it */
|
---|
| 428 | memcpy(final_response.data+sizeof(lmv2_response),
|
---|
| 429 | lmv2_client_data.data, lmv2_client_data.length);
|
---|
| 430 |
|
---|
| 431 | data_blob_free(&lmv2_client_data);
|
---|
| 432 |
|
---|
| 433 | return final_response;
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | BOOL SMBNTLMv2encrypt_hash(const char *user, const char *domain, const uchar nt_hash[16],
|
---|
| 437 | const DATA_BLOB *server_chal,
|
---|
| 438 | const DATA_BLOB *names_blob,
|
---|
| 439 | DATA_BLOB *lm_response, DATA_BLOB *nt_response,
|
---|
| 440 | DATA_BLOB *user_session_key)
|
---|
| 441 | {
|
---|
| 442 | uchar ntlm_v2_hash[16];
|
---|
| 443 |
|
---|
| 444 | /* We don't use the NT# directly. Instead we use it mashed up with
|
---|
| 445 | the username and domain.
|
---|
| 446 | This prevents username swapping during the auth exchange
|
---|
| 447 | */
|
---|
| 448 | if (!ntv2_owf_gen(nt_hash, user, domain, False, ntlm_v2_hash)) {
|
---|
| 449 | return False;
|
---|
| 450 | }
|
---|
| 451 |
|
---|
| 452 | if (nt_response) {
|
---|
| 453 | *nt_response = NTLMv2_generate_response(ntlm_v2_hash, server_chal,
|
---|
| 454 | names_blob);
|
---|
| 455 | if (user_session_key) {
|
---|
| 456 | *user_session_key = data_blob(NULL, 16);
|
---|
| 457 |
|
---|
| 458 | /* The NTLMv2 calculations also provide a session key, for signing etc later */
|
---|
| 459 | /* use only the first 16 bytes of nt_response for session key */
|
---|
| 460 | SMBsesskeygen_ntv2(ntlm_v2_hash, nt_response->data, user_session_key->data);
|
---|
| 461 | }
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | /* LMv2 */
|
---|
| 465 |
|
---|
| 466 | if (lm_response) {
|
---|
| 467 | *lm_response = LMv2_generate_response(ntlm_v2_hash, server_chal);
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | return True;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | /* Plaintext version of the above. */
|
---|
| 474 |
|
---|
| 475 | BOOL SMBNTLMv2encrypt(const char *user, const char *domain, const char *password,
|
---|
| 476 | const DATA_BLOB *server_chal,
|
---|
| 477 | const DATA_BLOB *names_blob,
|
---|
| 478 | DATA_BLOB *lm_response, DATA_BLOB *nt_response,
|
---|
| 479 | DATA_BLOB *user_session_key)
|
---|
| 480 | {
|
---|
| 481 | uchar nt_hash[16];
|
---|
| 482 | E_md4hash(password, nt_hash);
|
---|
| 483 |
|
---|
| 484 | return SMBNTLMv2encrypt_hash(user, domain, nt_hash,
|
---|
| 485 | server_chal,
|
---|
| 486 | names_blob,
|
---|
| 487 | lm_response, nt_response,
|
---|
| 488 | user_session_key);
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 | /***********************************************************
|
---|
| 492 | encode a password buffer with a unicode password. The buffer
|
---|
| 493 | is filled with random data to make it harder to attack.
|
---|
| 494 | ************************************************************/
|
---|
| 495 | BOOL encode_pw_buffer(uint8 buffer[516], const char *password, int string_flags)
|
---|
| 496 | {
|
---|
| 497 | uchar new_pw[512];
|
---|
| 498 | size_t new_pw_len;
|
---|
| 499 |
|
---|
| 500 | /* the incoming buffer can be any alignment. */
|
---|
| 501 | string_flags |= STR_NOALIGN;
|
---|
| 502 |
|
---|
| 503 | new_pw_len = push_string(NULL, new_pw,
|
---|
| 504 | password,
|
---|
| 505 | sizeof(new_pw), string_flags);
|
---|
| 506 |
|
---|
| 507 | memcpy(&buffer[512 - new_pw_len], new_pw, new_pw_len);
|
---|
| 508 |
|
---|
| 509 | generate_random_buffer(buffer, 512 - new_pw_len);
|
---|
| 510 |
|
---|
| 511 | /*
|
---|
| 512 | * The length of the new password is in the last 4 bytes of
|
---|
| 513 | * the data buffer.
|
---|
| 514 | */
|
---|
| 515 | SIVAL(buffer, 512, new_pw_len);
|
---|
| 516 | ZERO_STRUCT(new_pw);
|
---|
| 517 | return True;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 |
|
---|
| 521 | /***********************************************************
|
---|
| 522 | decode a password buffer
|
---|
| 523 | *new_pw_len is the length in bytes of the possibly mulitbyte
|
---|
| 524 | returned password including termination.
|
---|
| 525 | ************************************************************/
|
---|
| 526 |
|
---|
| 527 | BOOL decode_pw_buffer(uint8 in_buffer[516], char *new_pwrd,
|
---|
| 528 | int new_pwrd_size, uint32 *new_pw_len,
|
---|
| 529 | int string_flags)
|
---|
| 530 | {
|
---|
| 531 | int byte_len=0;
|
---|
| 532 |
|
---|
| 533 | /* the incoming buffer can be any alignment. */
|
---|
| 534 | string_flags |= STR_NOALIGN;
|
---|
| 535 |
|
---|
| 536 | /*
|
---|
| 537 | Warning !!! : This function is called from some rpc call.
|
---|
| 538 | The password IN the buffer may be a UNICODE string.
|
---|
| 539 | The password IN new_pwrd is an ASCII string
|
---|
| 540 | If you reuse that code somewhere else check first.
|
---|
| 541 | */
|
---|
| 542 |
|
---|
| 543 | /* The length of the new password is in the last 4 bytes of the data buffer. */
|
---|
| 544 |
|
---|
| 545 | byte_len = IVAL(in_buffer, 512);
|
---|
| 546 |
|
---|
| 547 | #ifdef DEBUG_PASSWORD
|
---|
| 548 | dump_data(100, (const char *)in_buffer, 516);
|
---|
| 549 | #endif
|
---|
| 550 |
|
---|
| 551 | /* Password cannot be longer than the size of the password buffer */
|
---|
| 552 | if ( (byte_len < 0) || (byte_len > 512)) {
|
---|
| 553 | DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len));
|
---|
| 554 | DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n"));
|
---|
| 555 | return False;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | /* decode into the return buffer. Buffer length supplied */
|
---|
| 559 | *new_pw_len = pull_string(NULL, new_pwrd, &in_buffer[512 - byte_len], new_pwrd_size,
|
---|
| 560 | byte_len, string_flags);
|
---|
| 561 |
|
---|
| 562 | #ifdef DEBUG_PASSWORD
|
---|
| 563 | DEBUG(100,("decode_pw_buffer: new_pwrd: "));
|
---|
| 564 | dump_data(100, (const char *)new_pwrd, *new_pw_len);
|
---|
| 565 | DEBUG(100,("multibyte len:%d\n", *new_pw_len));
|
---|
| 566 | DEBUG(100,("original char len:%d\n", byte_len/2));
|
---|
| 567 | #endif
|
---|
| 568 |
|
---|
| 569 | return True;
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 | /***********************************************************
|
---|
| 573 | Decode an arc4 encrypted password change buffer.
|
---|
| 574 | ************************************************************/
|
---|
| 575 |
|
---|
| 576 | void encode_or_decode_arc4_passwd_buffer(unsigned char pw_buf[532], const DATA_BLOB *psession_key)
|
---|
| 577 | {
|
---|
| 578 | struct MD5Context tctx;
|
---|
| 579 | unsigned char key_out[16];
|
---|
| 580 |
|
---|
| 581 | /* Confounder is last 16 bytes. */
|
---|
| 582 |
|
---|
| 583 | MD5Init(&tctx);
|
---|
| 584 | MD5Update(&tctx, &pw_buf[516], 16);
|
---|
| 585 | MD5Update(&tctx, psession_key->data, psession_key->length);
|
---|
| 586 | MD5Final(key_out, &tctx);
|
---|
| 587 | /* arc4 with key_out. */
|
---|
| 588 | SamOEMhash(pw_buf, key_out, 516);
|
---|
| 589 | }
|
---|
| 590 |
|
---|
| 591 | /***********************************************************
|
---|
| 592 | Encrypt/Decrypt used for LSA secrets and trusted domain
|
---|
| 593 | passwords.
|
---|
| 594 | ************************************************************/
|
---|
| 595 |
|
---|
| 596 | void sess_crypt_blob(DATA_BLOB *out, const DATA_BLOB *in, const DATA_BLOB *session_key, int forward)
|
---|
| 597 | {
|
---|
| 598 | int i, k;
|
---|
| 599 |
|
---|
| 600 | for (i=0,k=0;
|
---|
| 601 | i<in->length;
|
---|
| 602 | i += 8, k += 7) {
|
---|
| 603 | uint8 bin[8], bout[8], key[7];
|
---|
| 604 |
|
---|
| 605 | memset(bin, 0, 8);
|
---|
| 606 | memcpy(bin, &in->data[i], MIN(8, in->length-i));
|
---|
| 607 |
|
---|
| 608 | if (k + 7 > session_key->length) {
|
---|
| 609 | k = (session_key->length - k);
|
---|
| 610 | }
|
---|
| 611 | memcpy(key, &session_key->data[k], 7);
|
---|
| 612 |
|
---|
| 613 | des_crypt56(bout, bin, key, forward?1:0);
|
---|
| 614 |
|
---|
| 615 | memcpy(&out->data[i], bout, MIN(8, in->length-i));
|
---|
| 616 | }
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 | /* Decrypts password-blob with session-key
|
---|
| 620 | * @param pass password for session-key
|
---|
| 621 | * @param data_in DATA_BLOB encrypted password
|
---|
| 622 | *
|
---|
| 623 | * Returns cleartext password in CH_UNIX
|
---|
| 624 | * Caller must free the returned string
|
---|
| 625 | */
|
---|
| 626 |
|
---|
| 627 | char *decrypt_trustdom_secret(const char *pass, DATA_BLOB *data_in)
|
---|
| 628 | {
|
---|
| 629 | DATA_BLOB data_out, sess_key;
|
---|
| 630 | uchar nt_hash[16];
|
---|
| 631 | uint32_t length;
|
---|
| 632 | uint32_t version;
|
---|
| 633 | fstring cleartextpwd;
|
---|
| 634 |
|
---|
| 635 | if (!data_in || !pass)
|
---|
| 636 | return NULL;
|
---|
| 637 |
|
---|
| 638 | /* generate md4 password-hash derived from the NT UNICODE password */
|
---|
| 639 | E_md4hash(pass, nt_hash);
|
---|
| 640 |
|
---|
| 641 | /* hashed twice with md4 */
|
---|
| 642 | mdfour(nt_hash, nt_hash, 16);
|
---|
| 643 |
|
---|
| 644 | /* 16-Byte session-key */
|
---|
| 645 | sess_key = data_blob(nt_hash, 16);
|
---|
| 646 | if (sess_key.data == NULL)
|
---|
| 647 | return NULL;
|
---|
| 648 |
|
---|
| 649 | data_out = data_blob(NULL, data_in->length);
|
---|
| 650 | if (data_out.data == NULL)
|
---|
| 651 | return NULL;
|
---|
| 652 |
|
---|
| 653 | /* decrypt with des3 */
|
---|
| 654 | sess_crypt_blob(&data_out, data_in, &sess_key, 0);
|
---|
| 655 |
|
---|
| 656 | /* 4 Byte length, 4 Byte version */
|
---|
| 657 | length = IVAL(data_out.data, 0);
|
---|
| 658 | version = IVAL(data_out.data, 4);
|
---|
| 659 |
|
---|
| 660 | if (length > data_in->length - 8) {
|
---|
| 661 | DEBUG(0,("decrypt_trustdom_secret: invalid length (%d)\n", length));
|
---|
| 662 | return NULL;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | if (version != 1) {
|
---|
| 666 | DEBUG(0,("decrypt_trustdom_secret: unknown version number (%d)\n", version));
|
---|
| 667 | return NULL;
|
---|
| 668 | }
|
---|
| 669 |
|
---|
| 670 | rpcstr_pull(cleartextpwd, data_out.data + 8, sizeof(fstring), length, 0 );
|
---|
| 671 |
|
---|
| 672 | #ifdef DEBUG_PASSWORD
|
---|
| 673 | DEBUG(100,("decrypt_trustdom_secret: length is: %d, version is: %d, password is: %s\n",
|
---|
| 674 | length, version, cleartextpwd));
|
---|
| 675 | #endif
|
---|
| 676 |
|
---|
| 677 | data_blob_free(&data_out);
|
---|
| 678 | data_blob_free(&sess_key);
|
---|
| 679 |
|
---|
| 680 | return SMB_STRDUP(cleartextpwd);
|
---|
| 681 |
|
---|
| 682 | }
|
---|
| 683 |
|
---|