2 Unix SMB/Netbios implementation.
4 SMB parameters and setup
5 Copyright (C) Andrew Tridgell 1992-1998
6 Modified by Jeremy Allison 1995.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern int DEBUGLEVEL;
28 This implements the X/Open SMB password encryption
29 It takes a password, a 8 byte "crypt key" and puts 24 bytes of
30 encrypted password into p24 */
31 void SMBencrypt(uchar *pwrd, uchar *c8, uchar *p24)
35 lm_owf_gen(pwrd, p21);
36 SMBOWFencrypt(p21, c8, p24);
39 DEBUG(100,("SMBencrypt: lm#, challenge, response\n"));
40 dump_data(100, p21, 16);
41 dump_data(100, c8, 8);
42 dump_data(100, p24, 24);
46 void SMBNTencrypt(uchar *pwrd, uchar *c8, uchar *p24)
52 nt_owf_gen(pwrd, p21);
53 SMBOWFencrypt(p21, c8, p24);
56 DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
57 dump_data(100, p21, 16);
58 dump_data(100, c8, 8);
59 dump_data(100, p24, 24);
63 /* Routines for Windows NT MD4 Hash functions. */
64 static int _my_wcslen(int16 *str)
73 * Convert a string into an NT UNICODE string.
74 * Note that regardless of processor type
75 * this must be in intel (little-endian)
79 static int _my_mbstowcsupper(int16 *dst, const uchar *src, int len)
84 for(i = 0; i < len; i++) {
95 static int _my_mbstowcs(int16 *dst, const uchar *src, int len)
100 for(i = 0; i < len; i++) {
112 * Creates the MD4 Hash of the users password in NT UNICODE.
115 void E_md4hash(uchar *pwrd, uchar *p16)
120 /* Password cannot be longer than 128 characters */
121 len = strlen((char *)pwrd);
124 /* Password must be converted to NT unicode */
125 _my_mbstowcs(wpwd, pwrd, len);
126 wpwd[len] = 0; /* Ensure string is null terminated */
127 /* Calculate length in bytes */
128 len = _my_wcslen(wpwd) * sizeof(int16);
130 mdfour(p16, (unsigned char *)wpwd, len);
133 /* Does the LM owf of a user's password */
134 void lm_owf_genW(const UNISTR2 *pwd, uchar p16[16])
138 memset(pwrd,'\0',15);
141 unistr2_to_ascii( pwrd, pwd, sizeof(pwrd)-1);
144 /* Mangle the passwords into Lanman format */
148 /* Calculate the SMB (lanman) hash functions of the password */
150 memset(p16, '\0', 16);
151 E_P16((uchar *) pwrd, (uchar *)p16);
153 #ifdef DEBUG_PASSWORD
154 DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
155 dump_data(120, pwrd, strlen(pwrd));
156 dump_data(100, p16, 16);
158 /* clear out local copy of user's password (just being paranoid). */
159 bzero(pwrd, sizeof(pwrd));
162 /* Does the LM owf of a user's password */
163 void lm_owf_gen(const char *pwd, uchar p16[16])
167 memset(pwrd,'\0',15);
170 safe_strcpy( pwrd, pwd, sizeof(pwrd)-1);
173 /* Mangle the passwords into Lanman format */
177 /* Calculate the SMB (lanman) hash functions of the password */
179 memset(p16, '\0', 16);
180 E_P16((uchar *) pwrd, (uchar *)p16);
182 #ifdef DEBUG_PASSWORD
183 DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
184 dump_data(120, pwrd, strlen(pwrd));
185 dump_data(100, p16, 16);
187 /* clear out local copy of user's password (just being paranoid). */
188 bzero(pwrd, sizeof(pwrd));
191 /* Does both the NT and LM owfs of a user's password */
192 void nt_owf_genW(const UNISTR2 *pwd, uchar nt_p16[16])
196 memset(&pwrd,'\0',sizeof(pwrd));
199 copy_unistr2(&pwrd, pwd);
202 /* Calculate the MD4 hash (NT compatible) of the password */
203 memset(nt_p16, '\0', 16);
204 mdfour(nt_p16, (unsigned char *)pwrd.buffer, pwrd.uni_str_len * 2);
206 #ifdef DEBUG_PASSWORD
207 DEBUG(100,("nt_owf_gen: pwd, nt#\n"));
208 dump_data(120, (const char*)pwrd.buffer, pwrd.uni_str_len * 2);
209 dump_data(100, nt_p16, 16);
211 /* clear out local copy of user's password (just being paranoid). */
212 memset(&pwrd, 0, sizeof(pwrd));
215 /* Does both the NT and LM owfs of a user's password */
216 void nt_owf_gen(const char *pwd, uchar nt_p16[16])
220 memset(pwrd,'\0',130);
223 safe_strcpy( pwrd, pwd, sizeof(pwrd)-1);
226 /* Calculate the MD4 hash (NT compatible) of the password */
227 memset(nt_p16, '\0', 16);
228 E_md4hash((uchar *)pwrd, nt_p16);
230 #ifdef DEBUG_PASSWORD
231 DEBUG(100,("nt_owf_gen: pwd, nt#\n"));
232 dump_data(120, pwrd, strlen(pwrd));
233 dump_data(100, nt_p16, 16);
235 /* clear out local copy of user's password (just being paranoid). */
236 bzero(pwrd, sizeof(pwrd));
239 /* Does both the NT and LM owfs of a user's UNICODE password */
240 void nt_lm_owf_genW(const UNISTR2 *pwd, uchar nt_p16[16], uchar lm_p16[16])
242 nt_owf_genW(pwd, nt_p16);
243 lm_owf_genW(pwd, lm_p16);
246 /* Does both the NT and LM owfs of a user's password */
247 void nt_lm_owf_gen(const char *pwd, uchar nt_p16[16], uchar lm_p16[16])
249 nt_owf_gen(pwd, nt_p16);
250 lm_owf_gen(pwd, lm_p16);
253 /* Does the des encryption from the NT or LM MD4 hash. */
254 void SMBOWFencrypt(uchar pwrd[16], uchar *c8, uchar p24[24])
260 memcpy(p21, pwrd, 16);
264 void SMBOWFencrypt_ntv2(const uchar kr[16],
265 const uchar *srv_chal, int srv_chal_len,
266 const uchar *cli_chal, int cli_chal_len,
271 hmac_md5_init_limK_to_64(kr, 16, &ctx);
272 hmac_md5_update(srv_chal, srv_chal_len, &ctx);
273 hmac_md5_update(cli_chal, cli_chal_len, &ctx);
274 hmac_md5_final(resp_buf, &ctx);
276 #ifdef DEBUG_PASSWORD
277 DEBUG(100,("SMBOWFencrypt_ntv2: srv_chal, cli_chal, resp_buf\n"));
278 dump_data(100, srv_chal, srv_chal_len);
279 dump_data(100, cli_chal, cli_chal_len);
280 dump_data(100, resp_buf, 16);
284 void SMBsesskeygen_ntv2(const uchar kr[16],
285 const uchar *nt_resp,
290 hmac_md5_init_limK_to_64(kr, 16, &ctx);
291 hmac_md5_update(nt_resp, 16, &ctx);
292 hmac_md5_final(sess_key, &ctx);
294 #ifdef DEBUG_PASSWORD
295 DEBUG(100,("SMBsesskeygen_ntv2:\n"));
296 dump_data(100, sess_key, 16);
300 void SMBsesskeygen_ntv1(const uchar kr[16],
301 const uchar *nt_resp,
304 mdfour(sess_key, kr, 16);
306 #ifdef DEBUG_PASSWORD
307 DEBUG(100,("SMBsesskeygen_ntv2:\n"));
308 dump_data(100, sess_key, 16);
312 void SMBgenclientchals(char *lm_cli_chal,
313 char *nt_cli_chal, int *nt_cli_chal_len,
314 const char *srv, const char *dom)
317 int srv_len = strlen(srv);
318 int dom_len = strlen(dom);
321 fstrcpy(server, srv);
322 fstrcpy(domain, dom);
326 generate_random_buffer(lm_cli_chal, 8, False);
327 unix_to_nt_time(&nt_time, time(NULL));
329 CVAL(nt_cli_chal,0) = 0x1;
330 CVAL(nt_cli_chal,1) = 0x1;
331 SSVAL(nt_cli_chal, 2, 0x0);
332 SIVAL(nt_cli_chal, 4, 0x0);
333 SIVAL(nt_cli_chal, 8, nt_time.low);
334 SIVAL(nt_cli_chal, 12, nt_time.high);
335 memcpy(nt_cli_chal+16, lm_cli_chal, 8);
336 /* fill in offset 24, size of structure, later */
338 *nt_cli_chal_len = 28;
340 SSVAL(nt_cli_chal, *nt_cli_chal_len, 2);
341 *nt_cli_chal_len += 2;
342 SSVAL(nt_cli_chal, *nt_cli_chal_len, dom_len*2);
343 *nt_cli_chal_len += 2;
344 ascii_to_unibuf(nt_cli_chal+(*nt_cli_chal_len), domain, dom_len*2);
345 *nt_cli_chal_len += dom_len*2;
346 *nt_cli_chal_len += 4 - ((*nt_cli_chal_len) % 4);
348 SSVAL(nt_cli_chal, *nt_cli_chal_len, 2);
349 *nt_cli_chal_len += 2;
350 SSVAL(nt_cli_chal, 30, srv_len*2);
351 *nt_cli_chal_len += 2;
352 ascii_to_unibuf(nt_cli_chal+(*nt_cli_chal_len), server, srv_len*2);
353 *nt_cli_chal_len += srv_len*2;
355 SSVAL(nt_cli_chal, 24, (*nt_cli_chal_len)+16);
356 SSVAL(nt_cli_chal, 26, (*nt_cli_chal_len)+15);
358 DEBUG(100,("SMBgenclientchals: srv %s, dom %s\n", server, domain));
359 dump_data(100, nt_cli_chal, *nt_cli_chal_len);
362 void ntv2_owf_gen(const uchar owf[16],
364 const char *domain_n,
371 int user_l = strlen(user_n );
372 int domain_l = strlen(domain_n);
374 _my_mbstowcsupper((int16*)user_u, user_n , user_l*2 );
375 _my_mbstowcsupper((int16*)dom_u , domain_n, domain_l*2);
377 hmac_md5_init_limK_to_64(owf, 16, &ctx);
378 hmac_md5_update(user_u, user_l*2, &ctx);
379 hmac_md5_update(dom_u, domain_l*2, &ctx);
380 hmac_md5_final(kr_buf, &ctx);
382 #ifdef DEBUG_PASSWORD
383 DEBUG(100,("ntv2_owf_gen: user, domain, owfkey, kr\n"));
384 dump_data(100, user_u, user_l*2);
385 dump_data(100, dom_u, domain_l*2);
386 dump_data(100, owf, 16);
387 dump_data(100, kr_buf, 16);
391 /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
392 void NTLMSSPOWFencrypt(uchar pwrd[8], uchar *ntlmchalresp, uchar p24[24])
397 memcpy(p21, pwrd, 8);
398 memset(p21 + 8, 0xbd, 8);
400 E_P24(p21, ntlmchalresp, p24);
401 #ifdef DEBUG_PASSWORD
402 DEBUG(100,("NTLMSSPOWFencrypt: p21, c8, p24\n"));
403 dump_data(100, p21, 21);
404 dump_data(100, ntlmchalresp, 8);
405 dump_data(100, p24, 24);
409 BOOL make_oem_passwd_hash(char data[516], const char *pwrd, uchar old_pw_hash[16], BOOL unicode)
411 int new_pw_len = strlen(pwrd) * (unicode ? 2 : 1);
413 if (new_pw_len > 512)
415 DEBUG(0,("make_oem_pwrd_hash: new password is too long.\n"));
420 * Now setup the data area.
421 * We need to generate a random fill
422 * for this area to make it harder to
425 generate_random_buffer((unsigned char *)data, 516, False);
428 ascii_to_unibuf(&data[512 - new_pw_len], pwrd, new_pw_len);
432 fstrcpy( &data[512 - new_pw_len], pwrd);
434 SIVAL(data, 512, new_pw_len);
436 #ifdef DEBUG_PASSWORD
437 DEBUG(100,("make_oem_pwrd_hash\n"));
438 dump_data(100, data, 516);
440 SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, True);
445 BOOL nt_decrypt_string2(STRING2 *out, const STRING2 *in, const uchar *key)
450 const uchar *keyptr = key;
451 const uchar *keyend = key + 16;
453 uchar *outbuf = (uchar *)out->buffer;
454 const uchar *inbuf = (const uchar *)in->buffer;
455 const uchar *inbufend;
457 smbhash(bufhdr, inbuf, keyptr, 0);
458 datalen = IVAL(bufhdr, 0);
460 if ((datalen > in->str_str_len) || (datalen > MAX_STRINGLEN))
462 DEBUG(0, ("nt_decrypt_string2: failed\n"));
466 out->str_max_len = out->str_str_len = datalen;
468 inbufend = inbuf + datalen;
470 while (inbuf < inbufend)
473 if (keyptr + 7 > keyend)
475 keyptr = (keyend - keyptr) + key;
478 smbhash(outbuf, inbuf, keyptr, 0);
487 /*******************************************************************
488 creates a DCE/RPC bind authentication response
490 - initialises the parse structure.
491 - dynamically allocates the header data structure
492 - caller is expected to free the header data structure once used.
494 ********************************************************************/
495 void create_ntlmssp_resp(struct pwd_info *pwd,
496 char *domain, char *user_name, char *my_name,
497 uint32 ntlmssp_cli_flgs,
498 prs_struct *auth_resp)
500 RPC_AUTH_NTLMSSP_RESP ntlmssp_resp;
501 unsigned char lm_owf[24];
502 unsigned char nt_owf[128];
505 pwd_get_lm_nt_owf(pwd, lm_owf, nt_owf, &nt_owf_len, NULL);
507 make_rpc_auth_ntlmssp_resp(&ntlmssp_resp,
508 lm_owf, nt_owf, nt_owf_len,
509 domain, user_name, my_name,
512 smb_io_rpc_auth_ntlmssp_resp("ntlmssp_resp", &ntlmssp_resp, auth_resp, 0);
513 mem_realloc_data(auth_resp->data, auth_resp->offset);
516 /***********************************************************
517 decode a password buffer
518 ************************************************************/
519 BOOL decode_pw_buffer(const char buffer[516], char *new_pwrd,
520 int new_pwrd_size, uint32 *new_pw_len)
523 * The length of the new password is in the last 4 bytes of
527 (*new_pw_len) = IVAL(buffer, 512);
529 #ifdef DEBUG_PASSWORD
530 dump_data(100, buffer, 516);
533 if ((*new_pw_len) < 0 || (*new_pw_len) > new_pwrd_size - 1)
535 DEBUG(0,("check_oem_password: incorrect password length (%d).\n", (*new_pw_len)));
539 memcpy(new_pwrd, &buffer[512-(*new_pw_len)], (*new_pw_len));
540 new_pwrd[(*new_pw_len)] = '\0';
545 /***********************************************************
546 encode a password buffer
547 ************************************************************/
548 BOOL encode_pw_buffer(char buffer[516], const char *new_pass,
549 int new_pw_len, BOOL nt_pass_set)
551 generate_random_buffer(buffer, 516, True);
556 * nt passwords are in unicode. last char overwrites NULL
557 * in ascii_to_unibuf, so use SIVAL *afterwards*.
560 ascii_to_unibuf(&buffer[512-new_pw_len], new_pass, new_pw_len);
564 memcpy(&buffer[512-new_pw_len], new_pass, new_pw_len);
568 * The length of the new password is in the last 4 bytes of
572 SIVAL(buffer, 512, new_pw_len);
574 #ifdef DEBUG_PASSWORD
575 dump_data(100, buffer, 516);