(merge from 3.0)
[Samba/gebeck_regimport.git] / source / libsmb / smbencrypt.c
blob1d192b816a61d031f5c2cdc3923513808283004f
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, uchar kr_buf[16])
132 smb_ucs2_t *user;
133 smb_ucs2_t *domain;
135 size_t user_byte_len;
136 size_t domain_byte_len;
138 HMACMD5Context ctx;
140 user_byte_len = push_ucs2_allocate(&user, user_in);
141 if (user_byte_len == (size_t)-1) {
142 DEBUG(0, ("push_uss2_allocate() for user returned -1 (probably malloc() failure)\n"));
143 return False;
146 domain_byte_len = push_ucs2_allocate(&domain, domain_in);
147 if (domain_byte_len == (size_t)-1) {
148 DEBUG(0, ("push_uss2_allocate() for domain returned -1 (probably malloc() failure)\n"));
149 return False;
152 strupper_w(user);
153 strupper_w(domain);
155 SMB_ASSERT(user_byte_len >= 2);
156 SMB_ASSERT(domain_byte_len >= 2);
158 /* We don't want null termination */
159 user_byte_len = user_byte_len - 2;
160 domain_byte_len = domain_byte_len - 2;
162 hmac_md5_init_limK_to_64(owf, 16, &ctx);
163 hmac_md5_update((const unsigned char *)user, user_byte_len, &ctx);
164 hmac_md5_update((const unsigned char *)domain, domain_byte_len, &ctx);
165 hmac_md5_final(kr_buf, &ctx);
167 #ifdef DEBUG_PASSWORD
168 DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
169 dump_data(100, (const char *)user, user_byte_len);
170 dump_data(100, (const char *)domain, domain_byte_len);
171 dump_data(100, owf, 16);
172 dump_data(100, kr_buf, 16);
173 #endif
175 SAFE_FREE(user);
176 SAFE_FREE(domain);
177 return True;
180 /* Does the des encryption from the NT or LM MD4 hash. */
181 void SMBOWFencrypt(const uchar passwd[16], const uchar *c8, uchar p24[24])
183 uchar p21[21];
185 ZERO_STRUCT(p21);
187 memcpy(p21, passwd, 16);
188 E_P24(p21, c8, p24);
191 /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
192 void NTLMSSPOWFencrypt(const uchar passwd[8], const uchar *ntlmchalresp, uchar p24[24])
194 uchar p21[21];
196 memset(p21,'\0',21);
197 memcpy(p21, passwd, 8);
198 memset(p21 + 8, 0xbd, 8);
200 E_P24(p21, ntlmchalresp, p24);
201 #ifdef DEBUG_PASSWORD
202 DEBUG(100,("NTLMSSPOWFencrypt: p21, c8, p24\n"));
203 dump_data(100, (char *)p21, 21);
204 dump_data(100, (const char *)ntlmchalresp, 8);
205 dump_data(100, (char *)p24, 24);
206 #endif
210 /* Does the NT MD4 hash then des encryption. */
212 void SMBNTencrypt(const char *passwd, uchar *c8, uchar *p24)
214 uchar p21[21];
216 memset(p21,'\0',21);
218 E_md4hash(passwd, p21);
219 SMBOWFencrypt(p21, c8, p24);
221 #ifdef DEBUG_PASSWORD
222 DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
223 dump_data(100, (char *)p21, 16);
224 dump_data(100, (char *)c8, 8);
225 dump_data(100, (char *)p24, 24);
226 #endif
229 BOOL make_oem_passwd_hash(char data[516], const char *passwd, uchar old_pw_hash[16], BOOL unicode)
231 encode_pw_buffer(data, passwd, (unicode?STR_UNICODE:STR_ASCII));
233 #ifdef DEBUG_PASSWORD
234 DEBUG(100,("make_oem_passwd_hash\n"));
235 dump_data(100, data, 516);
236 #endif
237 SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
239 return True;
242 /* Does the md5 encryption from the Key Response for NTLMv2. */
243 void SMBOWFencrypt_ntv2(const uchar kr[16],
244 const DATA_BLOB *srv_chal,
245 const DATA_BLOB *cli_chal,
246 uchar resp_buf[16])
248 HMACMD5Context ctx;
250 hmac_md5_init_limK_to_64(kr, 16, &ctx);
251 hmac_md5_update(srv_chal->data, srv_chal->length, &ctx);
252 hmac_md5_update(cli_chal->data, cli_chal->length, &ctx);
253 hmac_md5_final(resp_buf, &ctx);
255 #ifdef DEBUG_PASSWORD
256 DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, cli_chal, resp_buf\n"));
257 dump_data(100, srv_chal->data, srv_chal->length);
258 dump_data(100, cli_chal->data, cli_chal->length);
259 dump_data(100, resp_buf, 16);
260 #endif
263 void SMBsesskeygen_ntv2(const uchar kr[16],
264 const uchar * nt_resp, uint8 sess_key[16])
266 /* a very nice, 128 bit, variable session key */
268 HMACMD5Context ctx;
270 hmac_md5_init_limK_to_64(kr, 16, &ctx);
271 hmac_md5_update(nt_resp, 16, &ctx);
272 hmac_md5_final((unsigned char *)sess_key, &ctx);
274 #ifdef DEBUG_PASSWORD
275 DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
276 dump_data(100, sess_key, 16);
277 #endif
280 void SMBsesskeygen_ntv1(const uchar kr[16],
281 const uchar * nt_resp, uint8 sess_key[16])
283 /* yes, this session key does not change - yes, this
284 is a problem - but it is 128 bits */
286 mdfour((unsigned char *)sess_key, kr, 16);
288 #ifdef DEBUG_PASSWORD
289 DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
290 dump_data(100, sess_key, 16);
291 #endif
294 void SMBsesskeygen_lmv1(const uchar lm_hash[16],
295 const uchar lm_resp[24], /* only uses 8 */
296 uint8 sess_key[16])
298 /* Calculate the LM session key (effective length 40 bits,
299 but changes with each session) */
301 uchar p24[24];
302 uchar partial_lm_hash[16];
304 memcpy(partial_lm_hash, lm_hash, 8);
305 memset(partial_lm_hash + 8, 0xbd, 8);
307 SMBOWFencrypt(lm_hash, lm_resp, p24);
309 memcpy(sess_key, p24, 16);
310 sess_key[5] = 0xe5;
311 sess_key[6] = 0x38;
312 sess_key[7] = 0xb0;
314 #ifdef DEBUG_PASSWORD
315 DEBUG(100, ("SMBsesskeygen_lmv1:\n"));
316 dump_data(100, sess_key, 16);
317 #endif
320 void SMBsesskeygen_lm_sess_key(const uchar lm_hash[16],
321 const uchar lm_resp[24], /* only uses 8 */
322 uint8 sess_key[16])
324 uchar p24[24];
325 uchar partial_lm_hash[16];
327 memcpy(partial_lm_hash, lm_hash, 8);
328 memset(partial_lm_hash + 8, 0xbd, 8);
330 SMBOWFencrypt(partial_lm_hash, lm_resp, p24);
332 memcpy(sess_key, p24, 16);
334 #ifdef DEBUG_PASSWORD
335 DEBUG(100, ("SMBsesskeygen_lmv1_jerry:\n"));
336 dump_data(100, sess_key, 16);
337 #endif
340 DATA_BLOB NTLMv2_generate_names_blob(const char *hostname,
341 const char *domain)
343 DATA_BLOB names_blob = data_blob(NULL, 0);
345 msrpc_gen(&names_blob, "aaa",
346 NTLMSSP_NAME_TYPE_DOMAIN, domain,
347 NTLMSSP_NAME_TYPE_SERVER, hostname,
348 0, "");
349 return names_blob;
352 static DATA_BLOB NTLMv2_generate_client_data(const DATA_BLOB *names_blob)
354 uchar client_chal[8];
355 DATA_BLOB response = data_blob(NULL, 0);
356 char long_date[8];
358 generate_random_buffer(client_chal, sizeof(client_chal), False);
360 put_long_date(long_date, time(NULL));
362 /* See http://www.ubiqx.org/cifs/SMB.html#SMB.8.5 */
364 msrpc_gen(&response, "ddbbdb",
365 0x00000101, /* Header */
366 0, /* 'Reserved' */
367 long_date, 8, /* Timestamp */
368 client_chal, 8, /* client challenge */
369 0, /* Unknown */
370 names_blob->data, names_blob->length); /* End of name list */
372 return response;
375 static DATA_BLOB NTLMv2_generate_response(const uchar ntlm_v2_hash[16],
376 const DATA_BLOB *server_chal,
377 const DATA_BLOB *names_blob)
379 uchar ntlmv2_response[16];
380 DATA_BLOB ntlmv2_client_data;
381 DATA_BLOB final_response;
383 /* NTLMv2 */
384 /* generate some data to pass into the response function - including
385 the hostname and domain name of the server */
386 ntlmv2_client_data = NTLMv2_generate_client_data(names_blob);
388 /* Given that data, and the challenge from the server, generate a response */
389 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &ntlmv2_client_data, ntlmv2_response);
391 final_response = data_blob(NULL, sizeof(ntlmv2_response) + ntlmv2_client_data.length);
393 memcpy(final_response.data, ntlmv2_response, sizeof(ntlmv2_response));
395 memcpy(final_response.data+sizeof(ntlmv2_response),
396 ntlmv2_client_data.data, ntlmv2_client_data.length);
398 data_blob_free(&ntlmv2_client_data);
400 return final_response;
403 static DATA_BLOB LMv2_generate_response(const uchar ntlm_v2_hash[16],
404 const DATA_BLOB *server_chal)
406 uchar lmv2_response[16];
407 DATA_BLOB lmv2_client_data = data_blob(NULL, 8);
408 DATA_BLOB final_response = data_blob(NULL, 24);
410 /* LMv2 */
411 /* client-supplied random data */
412 generate_random_buffer(lmv2_client_data.data, lmv2_client_data.length, False);
414 /* Given that data, and the challenge from the server, generate a response */
415 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &lmv2_client_data, lmv2_response);
416 memcpy(final_response.data, lmv2_response, sizeof(lmv2_response));
418 /* after the first 16 bytes is the random data we generated above,
419 so the server can verify us with it */
420 memcpy(final_response.data+sizeof(lmv2_response),
421 lmv2_client_data.data, lmv2_client_data.length);
423 data_blob_free(&lmv2_client_data);
425 return final_response;
428 BOOL SMBNTLMv2encrypt(const char *user, const char *domain, const char *password,
429 const DATA_BLOB *server_chal,
430 const DATA_BLOB *names_blob,
431 DATA_BLOB *lm_response, DATA_BLOB *nt_response,
432 DATA_BLOB *nt_session_key)
434 uchar nt_hash[16];
435 uchar ntlm_v2_hash[16];
436 E_md4hash(password, nt_hash);
438 /* We don't use the NT# directly. Instead we use it mashed up with
439 the username and domain.
440 This prevents username swapping during the auth exchange
442 if (!ntv2_owf_gen(nt_hash, user, domain, ntlm_v2_hash)) {
443 return False;
446 if (nt_response) {
447 *nt_response = NTLMv2_generate_response(ntlm_v2_hash, server_chal,
448 names_blob);
449 if (nt_session_key) {
450 *nt_session_key = data_blob(NULL, 16);
452 /* The NTLMv2 calculations also provide a session key, for signing etc later */
453 /* use only the first 16 bytes of nt_response for session key */
454 SMBsesskeygen_ntv2(ntlm_v2_hash, nt_response->data, nt_session_key->data);
458 /* LMv2 */
460 if (lm_response) {
461 *lm_response = LMv2_generate_response(ntlm_v2_hash, server_chal);
464 return True;
467 /***********************************************************
468 encode a password buffer with a unicode password. The buffer
469 is filled with random data to make it harder to attack.
470 ************************************************************/
471 BOOL encode_pw_buffer(char buffer[516], const char *password, int string_flags)
473 uchar new_pw[512];
474 size_t new_pw_len;
476 new_pw_len = push_string(NULL, new_pw,
477 password,
478 sizeof(new_pw), string_flags);
480 memcpy(&buffer[512 - new_pw_len], new_pw, new_pw_len);
482 generate_random_buffer((unsigned char *)buffer, 512 - new_pw_len, True);
485 * The length of the new password is in the last 4 bytes of
486 * the data buffer.
488 SIVAL(buffer, 512, new_pw_len);
489 ZERO_STRUCT(new_pw);
490 return True;
494 /***********************************************************
495 decode a password buffer
496 *new_pw_len is the length in bytes of the possibly mulitbyte
497 returned password including termination.
498 ************************************************************/
499 BOOL decode_pw_buffer(char in_buffer[516], char *new_pwrd,
500 int new_pwrd_size, uint32 *new_pw_len,
501 int string_flags)
503 int byte_len=0;
506 Warning !!! : This function is called from some rpc call.
507 The password IN the buffer may be a UNICODE string.
508 The password IN new_pwrd is an ASCII string
509 If you reuse that code somewhere else check first.
512 /* The length of the new password is in the last 4 bytes of the data buffer. */
514 byte_len = IVAL(in_buffer, 512);
516 #ifdef DEBUG_PASSWORD
517 dump_data(100, in_buffer, 516);
518 #endif
520 /* Password cannot be longer than the size of the password buffer */
521 if ( (byte_len < 0) || (byte_len > 512)) {
522 DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len));
523 DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n"));
524 return False;
527 /* decode into the return buffer. Buffer length supplied */
528 *new_pw_len = pull_string(NULL, new_pwrd, &in_buffer[512 - byte_len], new_pwrd_size,
529 byte_len, string_flags);
531 #ifdef DEBUG_PASSWORD
532 DEBUG(100,("decode_pw_buffer: new_pwrd: "));
533 dump_data(100, (char *)new_pwrd, *new_pw_len);
534 DEBUG(100,("multibyte len:%d\n", *new_pw_len));
535 DEBUG(100,("original char len:%d\n", byte_len/2));
536 #endif
538 return True;