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 void ntv2_owf_gen(const uchar owf
[16],
120 const char *user_n
, const char *domain_n
, uchar kr_buf
[16])
126 int user_l
= strlen(user_n
);
127 int domain_l
= strlen(domain_n
);
129 push_ucs2(NULL
, user_u
, user_n
, (user_l
+1)*2, STR_UNICODE
|STR_NOALIGN
|STR_TERMINATE
|STR_UPPER
);
130 push_ucs2(NULL
, dom_u
, domain_n
, (domain_l
+1)*2, STR_UNICODE
|STR_NOALIGN
|STR_TERMINATE
|STR_UPPER
);
132 hmac_md5_init_limK_to_64(owf
, 16, &ctx
);
133 hmac_md5_update((const unsigned char *)user_u
, user_l
* 2, &ctx
);
134 hmac_md5_update((const unsigned char *)dom_u
, domain_l
* 2, &ctx
);
135 hmac_md5_final(kr_buf
, &ctx
);
137 #ifdef DEBUG_PASSWORD
138 DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
139 dump_data(100, user_u
, user_l
* 2);
140 dump_data(100, dom_u
, domain_l
* 2);
141 dump_data(100, owf
, 16);
142 dump_data(100, kr_buf
, 16);
146 /* Does the des encryption from the NT or LM MD4 hash. */
147 void SMBOWFencrypt(const uchar passwd
[16], const uchar
*c8
, uchar p24
[24])
153 memcpy(p21
, passwd
, 16);
157 /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
158 void NTLMSSPOWFencrypt(const uchar passwd
[8], const uchar
*ntlmchalresp
, uchar p24
[24])
163 memcpy(p21
, passwd
, 8);
164 memset(p21
+ 8, 0xbd, 8);
166 E_P24(p21
, ntlmchalresp
, p24
);
167 #ifdef DEBUG_PASSWORD
168 DEBUG(100,("NTLMSSPOWFencrypt: p21, c8, p24\n"));
169 dump_data(100, (char *)p21
, 21);
170 dump_data(100, (const char *)ntlmchalresp
, 8);
171 dump_data(100, (char *)p24
, 24);
176 /* Does the NT MD4 hash then des encryption. */
178 void SMBNTencrypt(const char *passwd
, uchar
*c8
, uchar
*p24
)
184 E_md4hash(passwd
, p21
);
185 SMBOWFencrypt(p21
, c8
, p24
);
187 #ifdef DEBUG_PASSWORD
188 DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
189 dump_data(100, (char *)p21
, 16);
190 dump_data(100, (char *)c8
, 8);
191 dump_data(100, (char *)p24
, 24);
195 BOOL
make_oem_passwd_hash(char data
[516], const char *passwd
, uchar old_pw_hash
[16], BOOL unicode
)
197 int new_pw_len
= strlen(passwd
) * (unicode
? 2 : 1);
199 if (new_pw_len
> 512)
201 DEBUG(0,("make_oem_passwd_hash: new password is too long.\n"));
206 * Now setup the data area.
207 * We need to generate a random fill
208 * for this area to make it harder to
211 generate_random_buffer((unsigned char *)data
, 516, False
);
212 push_string(NULL
, &data
[512 - new_pw_len
], passwd
, new_pw_len
,
213 STR_NOALIGN
| (unicode
?STR_UNICODE
:STR_ASCII
));
214 SIVAL(data
, 512, new_pw_len
);
216 #ifdef DEBUG_PASSWORD
217 DEBUG(100,("make_oem_passwd_hash\n"));
218 dump_data(100, data
, 516);
220 SamOEMhash( (unsigned char *)data
, (unsigned char *)old_pw_hash
, 516);
225 /* Does the md5 encryption from the NT hash for NTLMv2. */
226 void SMBOWFencrypt_ntv2(const uchar kr
[16],
227 const DATA_BLOB srv_chal
,
228 const DATA_BLOB cli_chal
,
233 hmac_md5_init_limK_to_64(kr
, 16, &ctx
);
234 hmac_md5_update(srv_chal
.data
, srv_chal
.length
, &ctx
);
235 hmac_md5_update(cli_chal
.data
, cli_chal
.length
, &ctx
);
236 hmac_md5_final(resp_buf
, &ctx
);
238 #ifdef DEBUG_PASSWORD
239 DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, cli_chal, resp_buf\n"));
240 dump_data(100, srv_chal
.data
, srv_chal
.length
);
241 dump_data(100, cli_chal
.data
, cli_chal
.length
);
242 dump_data(100, resp_buf
, 16);
246 void SMBsesskeygen_ntv2(const uchar kr
[16],
247 const uchar
* nt_resp
, uint8 sess_key
[16])
251 hmac_md5_init_limK_to_64(kr
, 16, &ctx
);
252 hmac_md5_update(nt_resp
, 16, &ctx
);
253 hmac_md5_final((unsigned char *)sess_key
, &ctx
);
255 #ifdef DEBUG_PASSWORD
256 DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
257 dump_data(100, sess_key
, 16);
261 void SMBsesskeygen_ntv1(const uchar kr
[16],
262 const uchar
* nt_resp
, uint8 sess_key
[16])
264 mdfour((unsigned char *)sess_key
, kr
, 16);
266 #ifdef DEBUG_PASSWORD
267 DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
268 dump_data(100, sess_key
, 16);
272 /***********************************************************
273 encode a password buffer. The caller gets to figure out
275 ************************************************************/
276 BOOL
encode_pw_buffer(char buffer
[516], char *new_pw
, int new_pw_length
)
278 generate_random_buffer((unsigned char *)buffer
, 516, True
);
280 memcpy(&buffer
[512 - new_pw_length
], new_pw
, new_pw_length
);
283 * The length of the new password is in the last 4 bytes of
286 SIVAL(buffer
, 512, new_pw_length
);
291 /***********************************************************
292 decode a password buffer
293 *new_pw_len is the length in bytes of the possibly mulitbyte
294 returned password including termination.
295 ************************************************************/
296 BOOL
decode_pw_buffer(char in_buffer
[516], char *new_pwrd
,
297 int new_pwrd_size
, uint32
*new_pw_len
)
302 Warning !!! : This function is called from some rpc call.
303 The password IN the buffer is a UNICODE string.
304 The password IN new_pwrd is an ASCII string
305 If you reuse that code somewhere else check first.
308 /* The length of the new password is in the last 4 bytes of the data buffer. */
310 byte_len
= IVAL(in_buffer
, 512);
312 #ifdef DEBUG_PASSWORD
313 dump_data(100, in_buffer
, 516);
316 /* Password cannot be longer than 128 characters */
317 if ( (byte_len
< 0) || (byte_len
> new_pwrd_size
- 1)) {
318 DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len
));
319 DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n"));
323 /* decode into the return buffer. Buffer must be a pstring */
324 *new_pw_len
= pull_string(NULL
, new_pwrd
, &in_buffer
[512 - byte_len
], new_pwrd_size
, byte_len
, STR_UNICODE
);
326 #ifdef DEBUG_PASSWORD
327 DEBUG(100,("decode_pw_buffer: new_pwrd: "));
328 dump_data(100, (char *)new_pwrd
, *new_pw_len
);
329 DEBUG(100,("multibyte len:%d\n", *new_pw_len
));
330 DEBUG(100,("original char len:%d\n", byte_len
/2));
336 /***********************************************************
337 SMB signing - setup the MAC key.
338 ************************************************************/
340 void cli_calculate_mac_key(struct cli_state
*cli
, const char *ntpasswd
, const uchar resp
[24])
342 /* Get first 16 bytes. */
343 E_md4hash(ntpasswd
,&cli
->sign_info
.mac_key
[0]);
344 memcpy(&cli
->sign_info
.mac_key
[16],resp
,24);
345 cli
->sign_info
.mac_key_len
= 40;
346 cli
->sign_info
.use_smb_signing
= True
;
348 /* These calls are INCONPATIBLE with SMB signing */
349 cli
->readbraw_supported
= False
;
350 cli
->writebraw_supported
= False
;
352 /* Reset the sequence number in case we had a previous (aborted) attempt */
353 cli
->sign_info
.send_seq_num
= 0;
356 /***********************************************************
357 SMB signing - calculate a MAC to send.
358 ************************************************************/
360 void cli_caclulate_sign_mac(struct cli_state
*cli
)
362 unsigned char calc_md5_mac
[16];
363 struct MD5Context md5_ctx
;
365 if (!cli
->sign_info
.use_smb_signing
) {
370 * Firstly put the sequence number into the first 4 bytes.
371 * and zero out the next 4 bytes.
373 SIVAL(cli
->outbuf
, smb_ss_field
, cli
->sign_info
.send_seq_num
);
374 SIVAL(cli
->outbuf
, smb_ss_field
+ 4, 0);
376 /* Calculate the 16 byte MAC and place first 8 bytes into the field. */
378 MD5Update(&md5_ctx
, cli
->sign_info
.mac_key
, cli
->sign_info
.mac_key_len
);
379 MD5Update(&md5_ctx
, cli
->outbuf
+ 4, smb_len(cli
->outbuf
));
380 MD5Final(calc_md5_mac
, &md5_ctx
);
382 memcpy(&cli
->outbuf
[smb_ss_field
], calc_md5_mac
, 8);
383 cli
->sign_info
.send_seq_num
++;
384 cli
->sign_info
.reply_seq_num
= cli
->sign_info
.send_seq_num
;
385 cli
->sign_info
.send_seq_num
++;