r38: Fix caching of name->sid lookups
[Samba/gebeck_regimport.git] / source3 / libsmb / smbencrypt.c
blob270a659e57fba818c8e0889c5af43c3856f8d3c5
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
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.
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.
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.
25 #include "includes.h"
26 #include "byteorder.h"
29 This implements the X/Open SMB password encryption
30 It takes a password ('unix' string), a 8 byte "crypt key"
31 and puts 24 bytes of encrypted password into p24 */
32 void SMBencrypt(const char *passwd, const uchar *c8, uchar p24[24])
34 uchar p21[21];
36 memset(p21,'\0',21);
37 E_deshash(passwd, p21);
39 SMBOWFencrypt(p21, c8, p24);
41 #ifdef DEBUG_PASSWORD
42 DEBUG(100,("SMBencrypt: lm#, challenge, response\n"));
43 dump_data(100, (char *)p21, 16);
44 dump_data(100, (const char *)c8, 8);
45 dump_data(100, (char *)p24, 24);
46 #endif
49 /**
50 * Creates the MD4 Hash of the users password in NT UNICODE.
51 * @param passwd password in 'unix' charset.
52 * @param p16 return password hashed with md4, caller allocated 16 byte buffer
55 void E_md4hash(const char *passwd, uchar p16[16])
57 int len;
58 smb_ucs2_t wpwd[129];
60 /* Password must be converted to NT unicode - null terminated. */
61 push_ucs2(NULL, wpwd, (const char *)passwd, 256, STR_UNICODE|STR_NOALIGN|STR_TERMINATE);
62 /* Calculate length in bytes */
63 len = strlen_w(wpwd) * sizeof(int16);
65 mdfour(p16, (unsigned char *)wpwd, len);
66 ZERO_STRUCT(wpwd);
69 /**
70 * Creates the DES forward-only Hash of the users password in DOS ASCII charset
71 * @param passwd password in 'unix' charset.
72 * @param p16 return password hashed with DES, caller allocated 16 byte buffer
73 * @return False if password was > 14 characters, and therefore may be incorrect, otherwise True
74 * @note p16 is filled in regardless
77 BOOL E_deshash(const char *passwd, uchar p16[16])
79 BOOL ret = True;
80 fstring dospwd;
81 ZERO_STRUCT(dospwd);
83 /* Password must be converted to DOS charset - null terminated, uppercase. */
84 push_ascii(dospwd, passwd, sizeof(dospwd), STR_UPPER|STR_TERMINATE);
86 /* Only the fisrt 14 chars are considered, password need not be null terminated. */
87 E_P16((const unsigned char *)dospwd, p16);
89 if (strlen(dospwd) > 14) {
90 ret = False;
93 ZERO_STRUCT(dospwd);
95 return ret;
98 /**
99 * Creates the MD4 and DES (LM) Hash of the users password.
100 * MD4 is of the NT Unicode, DES is of the DOS UPPERCASE password.
101 * @param passwd password in 'unix' charset.
102 * @param nt_p16 return password hashed with md4, caller allocated 16 byte buffer
103 * @param p16 return password hashed with des, caller allocated 16 byte buffer
106 /* Does both the NT and LM owfs of a user's password */
107 void nt_lm_owf_gen(const char *pwd, uchar nt_p16[16], uchar p16[16])
109 /* Calculate the MD4 hash (NT compatible) of the password */
110 memset(nt_p16, '\0', 16);
111 E_md4hash(pwd, nt_p16);
113 #ifdef DEBUG_PASSWORD
114 DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));
115 dump_data(120, pwd, strlen(pwd));
116 dump_data(100, (char *)nt_p16, 16);
117 #endif
119 E_deshash(pwd, (uchar *)p16);
121 #ifdef DEBUG_PASSWORD
122 DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
123 dump_data(120, pwd, strlen(pwd));
124 dump_data(100, (char *)p16, 16);
125 #endif
128 /* Does both the NTLMv2 owfs of a user's password */
129 BOOL ntv2_owf_gen(const uchar owf[16],
130 const char *user_in, const char *domain_in,
131 BOOL upper_case_domain, /* Transform the domain into UPPER case */
132 uchar kr_buf[16])
134 smb_ucs2_t *user;
135 smb_ucs2_t *domain;
137 size_t user_byte_len;
138 size_t domain_byte_len;
140 HMACMD5Context ctx;
142 user_byte_len = push_ucs2_allocate(&user, user_in);
143 if (user_byte_len == (size_t)-1) {
144 DEBUG(0, ("push_uss2_allocate() for user returned -1 (probably malloc() failure)\n"));
145 return False;
148 domain_byte_len = push_ucs2_allocate(&domain, domain_in);
149 if (domain_byte_len == (size_t)-1) {
150 DEBUG(0, ("push_uss2_allocate() for domain returned -1 (probably malloc() failure)\n"));
151 return False;
154 strupper_w(user);
156 if (upper_case_domain)
157 strupper_w(domain);
159 SMB_ASSERT(user_byte_len >= 2);
160 SMB_ASSERT(domain_byte_len >= 2);
162 /* We don't want null termination */
163 user_byte_len = user_byte_len - 2;
164 domain_byte_len = domain_byte_len - 2;
166 hmac_md5_init_limK_to_64(owf, 16, &ctx);
167 hmac_md5_update((const unsigned char *)user, user_byte_len, &ctx);
168 hmac_md5_update((const unsigned char *)domain, domain_byte_len, &ctx);
169 hmac_md5_final(kr_buf, &ctx);
171 #ifdef DEBUG_PASSWORD
172 DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
173 dump_data(100, (const char *)user, user_byte_len);
174 dump_data(100, (const char *)domain, domain_byte_len);
175 dump_data(100, owf, 16);
176 dump_data(100, kr_buf, 16);
177 #endif
179 SAFE_FREE(user);
180 SAFE_FREE(domain);
181 return True;
184 /* Does the des encryption from the NT or LM MD4 hash. */
185 void SMBOWFencrypt(const uchar passwd[16], const uchar *c8, uchar p24[24])
187 uchar p21[21];
189 ZERO_STRUCT(p21);
191 memcpy(p21, passwd, 16);
192 E_P24(p21, c8, p24);
195 /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
196 void NTLMSSPOWFencrypt(const uchar passwd[8], const uchar *ntlmchalresp, uchar p24[24])
198 uchar p21[21];
200 memset(p21,'\0',21);
201 memcpy(p21, passwd, 8);
202 memset(p21 + 8, 0xbd, 8);
204 E_P24(p21, ntlmchalresp, p24);
205 #ifdef DEBUG_PASSWORD
206 DEBUG(100,("NTLMSSPOWFencrypt: p21, c8, p24\n"));
207 dump_data(100, (char *)p21, 21);
208 dump_data(100, (const char *)ntlmchalresp, 8);
209 dump_data(100, (char *)p24, 24);
210 #endif
214 /* Does the NT MD4 hash then des encryption. */
216 void SMBNTencrypt(const char *passwd, uchar *c8, uchar *p24)
218 uchar p21[21];
220 memset(p21,'\0',21);
222 E_md4hash(passwd, p21);
223 SMBOWFencrypt(p21, c8, p24);
225 #ifdef DEBUG_PASSWORD
226 DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
227 dump_data(100, (char *)p21, 16);
228 dump_data(100, (char *)c8, 8);
229 dump_data(100, (char *)p24, 24);
230 #endif
233 /* Does the md5 encryption from the Key Response for NTLMv2. */
234 void SMBOWFencrypt_ntv2(const uchar kr[16],
235 const DATA_BLOB *srv_chal,
236 const DATA_BLOB *cli_chal,
237 uchar resp_buf[16])
239 HMACMD5Context ctx;
241 hmac_md5_init_limK_to_64(kr, 16, &ctx);
242 hmac_md5_update(srv_chal->data, srv_chal->length, &ctx);
243 hmac_md5_update(cli_chal->data, cli_chal->length, &ctx);
244 hmac_md5_final(resp_buf, &ctx);
246 #ifdef DEBUG_PASSWORD
247 DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, cli_chal, resp_buf\n"));
248 dump_data(100, srv_chal->data, srv_chal->length);
249 dump_data(100, cli_chal->data, cli_chal->length);
250 dump_data(100, resp_buf, 16);
251 #endif
254 void SMBsesskeygen_ntv2(const uchar kr[16],
255 const uchar * nt_resp, uint8 sess_key[16])
257 /* a very nice, 128 bit, variable session key */
259 HMACMD5Context ctx;
261 hmac_md5_init_limK_to_64(kr, 16, &ctx);
262 hmac_md5_update(nt_resp, 16, &ctx);
263 hmac_md5_final((unsigned char *)sess_key, &ctx);
265 #ifdef DEBUG_PASSWORD
266 DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
267 dump_data(100, sess_key, 16);
268 #endif
271 void SMBsesskeygen_ntv1(const uchar kr[16],
272 const uchar * nt_resp, uint8 sess_key[16])
274 /* yes, this session key does not change - yes, this
275 is a problem - but it is 128 bits */
277 mdfour((unsigned char *)sess_key, kr, 16);
279 #ifdef DEBUG_PASSWORD
280 DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
281 dump_data(100, sess_key, 16);
282 #endif
285 void SMBsesskeygen_lmv1(const uchar lm_hash[16],
286 const uchar lm_resp[24], /* only uses 8 */
287 uint8 sess_key[16])
289 /* Calculate the LM session key (effective length 40 bits,
290 but changes with each session) */
292 uchar p24[24];
293 uchar partial_lm_hash[16];
295 memcpy(partial_lm_hash, lm_hash, 8);
296 memset(partial_lm_hash + 8, 0xbd, 8);
298 SMBOWFencrypt(lm_hash, lm_resp, p24);
300 memcpy(sess_key, p24, 16);
301 sess_key[5] = 0xe5;
302 sess_key[6] = 0x38;
303 sess_key[7] = 0xb0;
305 #ifdef DEBUG_PASSWORD
306 DEBUG(100, ("SMBsesskeygen_lmv1:\n"));
307 dump_data(100, sess_key, 16);
308 #endif
311 void SMBsesskeygen_lm_sess_key(const uchar lm_hash[16],
312 const uchar lm_resp[24], /* only uses 8 */
313 uint8 sess_key[16])
315 uchar p24[24];
316 uchar partial_lm_hash[16];
318 memcpy(partial_lm_hash, lm_hash, 8);
319 memset(partial_lm_hash + 8, 0xbd, 8);
321 SMBOWFencrypt(partial_lm_hash, lm_resp, p24);
323 memcpy(sess_key, p24, 16);
325 #ifdef DEBUG_PASSWORD
326 DEBUG(100, ("SMBsesskeygen_lmv1_jerry:\n"));
327 dump_data(100, sess_key, 16);
328 #endif
331 DATA_BLOB NTLMv2_generate_names_blob(const char *hostname,
332 const char *domain)
334 DATA_BLOB names_blob = data_blob(NULL, 0);
336 msrpc_gen(&names_blob, "aaa",
337 NTLMSSP_NAME_TYPE_DOMAIN, domain,
338 NTLMSSP_NAME_TYPE_SERVER, hostname,
339 0, "");
340 return names_blob;
343 static DATA_BLOB NTLMv2_generate_client_data(const DATA_BLOB *names_blob)
345 uchar client_chal[8];
346 DATA_BLOB response = data_blob(NULL, 0);
347 char long_date[8];
349 generate_random_buffer(client_chal, sizeof(client_chal), False);
351 put_long_date(long_date, time(NULL));
353 /* See http://www.ubiqx.org/cifs/SMB.html#SMB.8.5 */
355 msrpc_gen(&response, "ddbbdb",
356 0x00000101, /* Header */
357 0, /* 'Reserved' */
358 long_date, 8, /* Timestamp */
359 client_chal, 8, /* client challenge */
360 0, /* Unknown */
361 names_blob->data, names_blob->length); /* End of name list */
363 return response;
366 static DATA_BLOB NTLMv2_generate_response(const uchar ntlm_v2_hash[16],
367 const DATA_BLOB *server_chal,
368 const DATA_BLOB *names_blob)
370 uchar ntlmv2_response[16];
371 DATA_BLOB ntlmv2_client_data;
372 DATA_BLOB final_response;
374 /* NTLMv2 */
375 /* generate some data to pass into the response function - including
376 the hostname and domain name of the server */
377 ntlmv2_client_data = NTLMv2_generate_client_data(names_blob);
379 /* Given that data, and the challenge from the server, generate a response */
380 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &ntlmv2_client_data, ntlmv2_response);
382 final_response = data_blob(NULL, sizeof(ntlmv2_response) + ntlmv2_client_data.length);
384 memcpy(final_response.data, ntlmv2_response, sizeof(ntlmv2_response));
386 memcpy(final_response.data+sizeof(ntlmv2_response),
387 ntlmv2_client_data.data, ntlmv2_client_data.length);
389 data_blob_free(&ntlmv2_client_data);
391 return final_response;
394 static DATA_BLOB LMv2_generate_response(const uchar ntlm_v2_hash[16],
395 const DATA_BLOB *server_chal)
397 uchar lmv2_response[16];
398 DATA_BLOB lmv2_client_data = data_blob(NULL, 8);
399 DATA_BLOB final_response = data_blob(NULL, 24);
401 /* LMv2 */
402 /* client-supplied random data */
403 generate_random_buffer(lmv2_client_data.data, lmv2_client_data.length, False);
405 /* Given that data, and the challenge from the server, generate a response */
406 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &lmv2_client_data, lmv2_response);
407 memcpy(final_response.data, lmv2_response, sizeof(lmv2_response));
409 /* after the first 16 bytes is the random data we generated above,
410 so the server can verify us with it */
411 memcpy(final_response.data+sizeof(lmv2_response),
412 lmv2_client_data.data, lmv2_client_data.length);
414 data_blob_free(&lmv2_client_data);
416 return final_response;
419 BOOL SMBNTLMv2encrypt(const char *user, const char *domain, const char *password,
420 const DATA_BLOB *server_chal,
421 const DATA_BLOB *names_blob,
422 DATA_BLOB *lm_response, DATA_BLOB *nt_response,
423 DATA_BLOB *nt_session_key)
425 uchar nt_hash[16];
426 uchar ntlm_v2_hash[16];
427 E_md4hash(password, nt_hash);
429 /* We don't use the NT# directly. Instead we use it mashed up with
430 the username and domain.
431 This prevents username swapping during the auth exchange
433 if (!ntv2_owf_gen(nt_hash, user, domain, True, ntlm_v2_hash)) {
434 return False;
437 if (nt_response) {
438 *nt_response = NTLMv2_generate_response(ntlm_v2_hash, server_chal,
439 names_blob);
440 if (nt_session_key) {
441 *nt_session_key = data_blob(NULL, 16);
443 /* The NTLMv2 calculations also provide a session key, for signing etc later */
444 /* use only the first 16 bytes of nt_response for session key */
445 SMBsesskeygen_ntv2(ntlm_v2_hash, nt_response->data, nt_session_key->data);
449 /* LMv2 */
451 if (lm_response) {
452 *lm_response = LMv2_generate_response(ntlm_v2_hash, server_chal);
455 return True;
458 /***********************************************************
459 encode a password buffer with a unicode password. The buffer
460 is filled with random data to make it harder to attack.
461 ************************************************************/
462 BOOL encode_pw_buffer(char buffer[516], const char *password, int string_flags)
464 uchar new_pw[512];
465 size_t new_pw_len;
467 new_pw_len = push_string(NULL, new_pw,
468 password,
469 sizeof(new_pw), string_flags);
471 memcpy(&buffer[512 - new_pw_len], new_pw, new_pw_len);
473 generate_random_buffer((unsigned char *)buffer, 512 - new_pw_len, True);
476 * The length of the new password is in the last 4 bytes of
477 * the data buffer.
479 SIVAL(buffer, 512, new_pw_len);
480 ZERO_STRUCT(new_pw);
481 return True;
485 /***********************************************************
486 decode a password buffer
487 *new_pw_len is the length in bytes of the possibly mulitbyte
488 returned password including termination.
489 ************************************************************/
490 BOOL decode_pw_buffer(char in_buffer[516], char *new_pwrd,
491 int new_pwrd_size, uint32 *new_pw_len,
492 int string_flags)
494 int byte_len=0;
497 Warning !!! : This function is called from some rpc call.
498 The password IN the buffer may be a UNICODE string.
499 The password IN new_pwrd is an ASCII string
500 If you reuse that code somewhere else check first.
503 /* The length of the new password is in the last 4 bytes of the data buffer. */
505 byte_len = IVAL(in_buffer, 512);
507 #ifdef DEBUG_PASSWORD
508 dump_data(100, in_buffer, 516);
509 #endif
511 /* Password cannot be longer than the size of the password buffer */
512 if ( (byte_len < 0) || (byte_len > 512)) {
513 DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len));
514 DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n"));
515 return False;
518 /* decode into the return buffer. Buffer length supplied */
519 *new_pw_len = pull_string(NULL, new_pwrd, &in_buffer[512 - byte_len], new_pwrd_size,
520 byte_len, string_flags);
522 #ifdef DEBUG_PASSWORD
523 DEBUG(100,("decode_pw_buffer: new_pwrd: "));
524 dump_data(100, (char *)new_pwrd, *new_pw_len);
525 DEBUG(100,("multibyte len:%d\n", *new_pw_len));
526 DEBUG(100,("original char len:%d\n", byte_len/2));
527 #endif
529 return True;