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.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "byteorder.h"
28 This implements the X/Open SMB password encryption
29 It takes a password ('unix' string), a 8 byte "crypt key"
30 and puts 24 bytes of encrypted password into p24 */
31 void SMBencrypt(const char *passwd, const uchar *c8, uchar p24[24])
36 E_deshash(passwd, p21);
38 SMBOWFencrypt(p21, c8, p24);
41 DEBUG(100,("SMBencrypt: lm#, challenge, response\n"));
42 dump_data(100, (char *)p21, 16);
43 dump_data(100, (const char *)c8, 8);
44 dump_data(100, (char *)p24, 24);
49 * Creates the MD4 Hash of the users password in NT UNICODE.
50 * @param passwd password in 'unix' charset.
51 * @param p16 return password hashed with md4, caller allocated 16 byte buffer
54 void E_md4hash(const char *passwd, uchar p16[16])
59 /* Password must be converted to NT unicode - null terminated. */
60 push_ucs2(NULL, wpwd, (const char *)passwd, 256, STR_UNICODE|STR_NOALIGN|STR_TERMINATE);
61 /* Calculate length in bytes */
62 len = strlen_w(wpwd) * sizeof(int16);
64 mdfour(p16, (unsigned char *)wpwd, len);
69 * Creates the DES forward-only Hash of the users password in DOS ASCII charset
70 * @param passwd password in 'unix' charset.
71 * @param p16 return password hashed with DES, caller allocated 16 byte buffer
74 void E_deshash(const char *passwd, uchar p16[16])
76 uchar dospwd[15]; /* Password must not be > 14 chars long. */
80 /* Password must be converted to DOS charset - null terminated, uppercase. */
81 push_ascii(dospwd, (const char *)passwd, sizeof(dospwd), STR_UPPER|STR_TERMINATE);
89 * Creates the MD4 and DES (LM) Hash of the users password.
90 * MD4 is of the NT Unicode, DES is of the DOS UPPERCASE password.
91 * @param passwd password in 'unix' charset.
92 * @param nt_p16 return password hashed with md4, caller allocated 16 byte buffer
93 * @param p16 return password hashed with des, caller allocated 16 byte buffer
96 /* Does both the NT and LM owfs of a user's password */
97 void nt_lm_owf_gen(const char *pwd, uchar nt_p16[16], uchar p16[16])
99 /* Calculate the MD4 hash (NT compatible) of the password */
100 memset(nt_p16, '\0', 16);
101 E_md4hash(pwd, nt_p16);
103 #ifdef DEBUG_PASSWORD
104 DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));
105 dump_data(120, pwd, strlen(pwd));
106 dump_data(100, (char *)nt_p16, 16);
109 E_deshash(pwd, (uchar *)p16);
111 #ifdef DEBUG_PASSWORD
112 DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
113 dump_data(120, pwd, strlen(pwd));
114 dump_data(100, (char *)p16, 16);
118 /* Does both the NTLMv2 owfs of a user's password */
119 BOOL ntv2_owf_gen(const uchar owf[16],
120 const char *user_in, const char *domain_in, uchar kr_buf[16])
130 user_byte_len = push_ucs2_allocate(&user, user_in);
131 if (user_byte_len < 0) {
132 DEBUG(0, ("push_uss2_allocate() for user returned %d (probably malloc() failure)\n", user_byte_len));
136 domain_byte_len = push_ucs2_allocate(&domain, domain_in);
137 if (domain_byte_len < 0) {
138 DEBUG(0, ("push_uss2_allocate() for domain returned %d (probably malloc() failure)\n", user_byte_len));
145 /* We don't want null termination */
146 user_byte_len = user_byte_len - 2;
147 domain_byte_len = domain_byte_len - 2;
149 SMB_ASSERT(user_byte_len >= 0);
150 SMB_ASSERT(domain_byte_len >= 0);
152 hmac_md5_init_limK_to_64(owf, 16, &ctx);
153 hmac_md5_update((const unsigned char *)user, user_byte_len, &ctx);
154 hmac_md5_update((const unsigned char *)domain, domain_byte_len, &ctx);
155 hmac_md5_final(kr_buf, &ctx);
157 #ifdef DEBUG_PASSWORD
158 DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
159 dump_data(100, (const char *)user, user_byte_len);
160 dump_data(100, (const char *)domain, domain_byte_len);
161 dump_data(100, owf, 16);
162 dump_data(100, kr_buf, 16);
170 /* Does the des encryption from the NT or LM MD4 hash. */
171 void SMBOWFencrypt(const uchar passwd[16], const uchar *c8, uchar p24[24])
177 memcpy(p21, passwd, 16);
181 /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
182 void NTLMSSPOWFencrypt(const uchar passwd[8], const uchar *ntlmchalresp, uchar p24[24])
187 memcpy(p21, passwd, 8);
188 memset(p21 + 8, 0xbd, 8);
190 E_P24(p21, ntlmchalresp, p24);
191 #ifdef DEBUG_PASSWORD
192 DEBUG(100,("NTLMSSPOWFencrypt: p21, c8, p24\n"));
193 dump_data(100, (char *)p21, 21);
194 dump_data(100, (const char *)ntlmchalresp, 8);
195 dump_data(100, (char *)p24, 24);
200 /* Does the NT MD4 hash then des encryption. */
202 void SMBNTencrypt(const char *passwd, uchar *c8, uchar *p24)
208 E_md4hash(passwd, p21);
209 SMBOWFencrypt(p21, c8, p24);
211 #ifdef DEBUG_PASSWORD
212 DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
213 dump_data(100, (char *)p21, 16);
214 dump_data(100, (char *)c8, 8);
215 dump_data(100, (char *)p24, 24);
219 BOOL make_oem_passwd_hash(char data[516], const char *passwd, uchar old_pw_hash[16], BOOL unicode)
221 int new_pw_len = strlen(passwd) * (unicode ? 2 : 1);
223 if (new_pw_len > 512)
225 DEBUG(0,("make_oem_passwd_hash: new password is too long.\n"));
230 * Now setup the data area.
231 * We need to generate a random fill
232 * for this area to make it harder to
235 generate_random_buffer((unsigned char *)data, 516, False);
236 push_string(NULL, &data[512 - new_pw_len], passwd, new_pw_len,
237 STR_NOALIGN | (unicode?STR_UNICODE:STR_ASCII));
238 SIVAL(data, 512, new_pw_len);
240 #ifdef DEBUG_PASSWORD
241 DEBUG(100,("make_oem_passwd_hash\n"));
242 dump_data(100, data, 516);
244 SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
249 /* Does the md5 encryption from the NT hash for NTLMv2. */
250 void SMBOWFencrypt_ntv2(const uchar kr[16],
251 const DATA_BLOB srv_chal,
252 const DATA_BLOB cli_chal,
257 hmac_md5_init_limK_to_64(kr, 16, &ctx);
258 hmac_md5_update(srv_chal.data, srv_chal.length, &ctx);
259 hmac_md5_update(cli_chal.data, cli_chal.length, &ctx);
260 hmac_md5_final(resp_buf, &ctx);
262 #ifdef DEBUG_PASSWORD
263 DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, cli_chal, resp_buf\n"));
264 dump_data(100, srv_chal.data, srv_chal.length);
265 dump_data(100, cli_chal.data, cli_chal.length);
266 dump_data(100, resp_buf, 16);
270 void SMBsesskeygen_ntv2(const uchar kr[16],
271 const uchar * nt_resp, uint8 sess_key[16])
275 hmac_md5_init_limK_to_64(kr, 16, &ctx);
276 hmac_md5_update(nt_resp, 16, &ctx);
277 hmac_md5_final((unsigned char *)sess_key, &ctx);
279 #ifdef DEBUG_PASSWORD
280 DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
281 dump_data(100, sess_key, 16);
285 void SMBsesskeygen_ntv1(const uchar kr[16],
286 const uchar * nt_resp, uint8 sess_key[16])
288 mdfour((unsigned char *)sess_key, kr, 16);
290 #ifdef DEBUG_PASSWORD
291 DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
292 dump_data(100, sess_key, 16);
296 /***********************************************************
297 encode a password buffer. The caller gets to figure out
299 ************************************************************/
300 BOOL encode_pw_buffer(char buffer[516], char *new_pw, int new_pw_length)
302 generate_random_buffer((unsigned char *)buffer, 516, True);
304 memcpy(&buffer[512 - new_pw_length], new_pw, new_pw_length);
307 * The length of the new password is in the last 4 bytes of
310 SIVAL(buffer, 512, new_pw_length);
315 /***********************************************************
316 decode a password buffer
317 *new_pw_len is the length in bytes of the possibly mulitbyte
318 returned password including termination.
319 ************************************************************/
320 BOOL decode_pw_buffer(char in_buffer[516], char *new_pwrd,
321 int new_pwrd_size, uint32 *new_pw_len)
326 Warning !!! : This function is called from some rpc call.
327 The password IN the buffer is a UNICODE string.
328 The password IN new_pwrd is an ASCII string
329 If you reuse that code somewhere else check first.
332 /* The length of the new password is in the last 4 bytes of the data buffer. */
334 byte_len = IVAL(in_buffer, 512);
336 #ifdef DEBUG_PASSWORD
337 dump_data(100, in_buffer, 516);
340 /* Password cannot be longer than 128 characters */
341 if ( (byte_len < 0) || (byte_len > new_pwrd_size - 1)) {
342 DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len));
343 DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n"));
347 /* decode into the return buffer. Buffer must be a pstring */
348 *new_pw_len = pull_string(NULL, new_pwrd, &in_buffer[512 - byte_len], new_pwrd_size, byte_len, STR_UNICODE);
350 #ifdef DEBUG_PASSWORD
351 DEBUG(100,("decode_pw_buffer: new_pwrd: "));
352 dump_data(100, (char *)new_pwrd, *new_pw_len);
353 DEBUG(100,("multibyte len:%d\n", *new_pw_len));
354 DEBUG(100,("original char len:%d\n", byte_len/2));
360 /***********************************************************
361 SMB signing - setup the MAC key.
362 ************************************************************/
364 void cli_calculate_mac_key(struct cli_state *cli, const char *ntpasswd, const uchar resp[24])
366 /* Get first 16 bytes. */
367 E_md4hash(ntpasswd,&cli->sign_info.mac_key[0]);
368 memcpy(&cli->sign_info.mac_key[16],resp,24);
369 cli->sign_info.mac_key_len = 40;
370 cli->sign_info.use_smb_signing = True;
372 /* These calls are INCONPATIBLE with SMB signing */
373 cli->readbraw_supported = False;
374 cli->writebraw_supported = False;
376 /* Reset the sequence number in case we had a previous (aborted) attempt */
377 cli->sign_info.send_seq_num = 0;
380 /***********************************************************
381 SMB signing - calculate a MAC to send.
382 ************************************************************/
384 void cli_caclulate_sign_mac(struct cli_state *cli)
386 unsigned char calc_md5_mac[16];
387 struct MD5Context md5_ctx;
389 if (cli->sign_info.temp_smb_signing) {
390 memcpy(&cli->outbuf[smb_ss_field], "SignRequest", 8);
391 cli->sign_info.temp_smb_signing = False;
395 if (!cli->sign_info.use_smb_signing) {
400 * Firstly put the sequence number into the first 4 bytes.
401 * and zero out the next 4 bytes.
403 SIVAL(cli->outbuf, smb_ss_field, cli->sign_info.send_seq_num);
404 SIVAL(cli->outbuf, smb_ss_field + 4, 0);
406 /* Calculate the 16 byte MAC and place first 8 bytes into the field. */
408 MD5Update(&md5_ctx, cli->sign_info.mac_key, cli->sign_info.mac_key_len);
409 MD5Update(&md5_ctx, cli->outbuf + 4, smb_len(cli->outbuf));
410 MD5Final(calc_md5_mac, &md5_ctx);
412 memcpy(&cli->outbuf[smb_ss_field], calc_md5_mac, 8);
413 /* cli->outbuf[smb_ss_field+2]=0;
414 Uncomment this to test if the remote server actually verifies signitures...*/
415 cli->sign_info.send_seq_num++;
416 cli->sign_info.reply_seq_num = cli->sign_info.send_seq_num;
417 cli->sign_info.send_seq_num++;