Changes to make gss-spnego ntlmssp client work against W2k AD.
[Samba/gebeck_regimport.git] / source / libsmb / smbencrypt.c
blob7a1a2d7d1894d84cd4064fb2447b490a670c9bc2
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
75 void E_deshash(const char *passwd, uchar p16[16])
77 fstring dospwd;
78 ZERO_STRUCT(dospwd);
80 /* Password must be converted to DOS charset - null terminated, uppercase. */
81 push_ascii(dospwd, passwd, sizeof(dospwd), STR_UPPER|STR_TERMINATE);
83 /* Only the fisrt 14 chars are considered, password need not be null terminated. */
84 E_P16(dospwd, p16);
86 ZERO_STRUCT(dospwd);
89 /**
90 * Creates the MD4 and DES (LM) Hash of the users password.
91 * MD4 is of the NT Unicode, DES is of the DOS UPPERCASE password.
92 * @param passwd password in 'unix' charset.
93 * @param nt_p16 return password hashed with md4, caller allocated 16 byte buffer
94 * @param p16 return password hashed with des, caller allocated 16 byte buffer
97 /* Does both the NT and LM owfs of a user's password */
98 void nt_lm_owf_gen(const char *pwd, uchar nt_p16[16], uchar p16[16])
100 /* Calculate the MD4 hash (NT compatible) of the password */
101 memset(nt_p16, '\0', 16);
102 E_md4hash(pwd, nt_p16);
104 #ifdef DEBUG_PASSWORD
105 DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));
106 dump_data(120, pwd, strlen(pwd));
107 dump_data(100, (char *)nt_p16, 16);
108 #endif
110 E_deshash(pwd, (uchar *)p16);
112 #ifdef DEBUG_PASSWORD
113 DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
114 dump_data(120, pwd, strlen(pwd));
115 dump_data(100, (char *)p16, 16);
116 #endif
119 /* Does both the NTLMv2 owfs of a user's password */
120 BOOL ntv2_owf_gen(const uchar owf[16],
121 const char *user_in, const char *domain_in, uchar kr_buf[16])
123 smb_ucs2_t *user;
124 smb_ucs2_t *domain;
126 size_t user_byte_len;
127 size_t domain_byte_len;
129 HMACMD5Context ctx;
131 user_byte_len = push_ucs2_allocate(&user, user_in);
132 if (user_byte_len == (size_t)-1) {
133 DEBUG(0, ("push_uss2_allocate() for user returned -1 (probably malloc() failure)\n"));
134 return False;
137 domain_byte_len = push_ucs2_allocate(&domain, domain_in);
138 if (domain_byte_len == (size_t)-1) {
139 DEBUG(0, ("push_uss2_allocate() for domain returned -1 (probably malloc() failure)\n"));
140 return False;
143 strupper_w(user);
144 strupper_w(domain);
146 SMB_ASSERT(user_byte_len >= 2);
147 SMB_ASSERT(domain_byte_len >= 2);
149 /* We don't want null termination */
150 user_byte_len = user_byte_len - 2;
151 domain_byte_len = domain_byte_len - 2;
153 hmac_md5_init_limK_to_64(owf, 16, &ctx);
154 hmac_md5_update((const unsigned char *)user, user_byte_len, &ctx);
155 hmac_md5_update((const unsigned char *)domain, domain_byte_len, &ctx);
156 hmac_md5_final(kr_buf, &ctx);
158 #ifdef DEBUG_PASSWORD
159 DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
160 dump_data(100, (const char *)user, user_byte_len);
161 dump_data(100, (const char *)domain, domain_byte_len);
162 dump_data(100, owf, 16);
163 dump_data(100, kr_buf, 16);
164 #endif
166 SAFE_FREE(user);
167 SAFE_FREE(domain);
168 return True;
171 /* Does the des encryption from the NT or LM MD4 hash. */
172 void SMBOWFencrypt(const uchar passwd[16], const uchar *c8, uchar p24[24])
174 uchar p21[21];
176 ZERO_STRUCT(p21);
178 memcpy(p21, passwd, 16);
179 E_P24(p21, c8, p24);
182 /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
183 void NTLMSSPOWFencrypt(const uchar passwd[8], const uchar *ntlmchalresp, uchar p24[24])
185 uchar p21[21];
187 memset(p21,'\0',21);
188 memcpy(p21, passwd, 8);
189 memset(p21 + 8, 0xbd, 8);
191 E_P24(p21, ntlmchalresp, p24);
192 #ifdef DEBUG_PASSWORD
193 DEBUG(100,("NTLMSSPOWFencrypt: p21, c8, p24\n"));
194 dump_data(100, (char *)p21, 21);
195 dump_data(100, (const char *)ntlmchalresp, 8);
196 dump_data(100, (char *)p24, 24);
197 #endif
201 /* Does the NT MD4 hash then des encryption. */
203 void SMBNTencrypt(const char *passwd, uchar *c8, uchar *p24)
205 uchar p21[21];
207 memset(p21,'\0',21);
209 E_md4hash(passwd, p21);
210 SMBOWFencrypt(p21, c8, p24);
212 #ifdef DEBUG_PASSWORD
213 DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
214 dump_data(100, (char *)p21, 16);
215 dump_data(100, (char *)c8, 8);
216 dump_data(100, (char *)p24, 24);
217 #endif
220 BOOL make_oem_passwd_hash(char data[516], const char *passwd, uchar old_pw_hash[16], BOOL unicode)
222 int new_pw_len = strlen(passwd) * (unicode ? 2 : 1);
224 if (new_pw_len > 512)
226 DEBUG(0,("make_oem_passwd_hash: new password is too long.\n"));
227 return False;
231 * Now setup the data area.
232 * We need to generate a random fill
233 * for this area to make it harder to
234 * decrypt. JRA.
236 generate_random_buffer((unsigned char *)data, 516, False);
237 push_string(NULL, &data[512 - new_pw_len], passwd, new_pw_len,
238 STR_NOALIGN | (unicode?STR_UNICODE:STR_ASCII));
239 SIVAL(data, 512, new_pw_len);
241 #ifdef DEBUG_PASSWORD
242 DEBUG(100,("make_oem_passwd_hash\n"));
243 dump_data(100, data, 516);
244 #endif
245 SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
247 return True;
250 /* Does the md5 encryption from the NT hash for NTLMv2. */
251 void SMBOWFencrypt_ntv2(const uchar kr[16],
252 const DATA_BLOB *srv_chal,
253 const DATA_BLOB *cli_chal,
254 uchar resp_buf[16])
256 HMACMD5Context ctx;
258 hmac_md5_init_limK_to_64(kr, 16, &ctx);
259 hmac_md5_update(srv_chal->data, srv_chal->length, &ctx);
260 hmac_md5_update(cli_chal->data, cli_chal->length, &ctx);
261 hmac_md5_final(resp_buf, &ctx);
263 #ifdef DEBUG_PASSWORD
264 DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, cli_chal, resp_buf\n"));
265 dump_data(100, srv_chal->data, srv_chal->length);
266 dump_data(100, cli_chal->data, cli_chal->length);
267 dump_data(100, resp_buf, 16);
268 #endif
271 void SMBsesskeygen_ntv2(const uchar kr[16],
272 const uchar * nt_resp, uint8 sess_key[16])
274 /* a very nice, 128 bit, variable session key */
276 HMACMD5Context ctx;
278 hmac_md5_init_limK_to_64(kr, 16, &ctx);
279 hmac_md5_update(nt_resp, 16, &ctx);
280 hmac_md5_final((unsigned char *)sess_key, &ctx);
282 #ifdef DEBUG_PASSWORD
283 DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
284 dump_data(100, sess_key, 16);
285 #endif
288 void SMBsesskeygen_ntv1(const uchar kr[16],
289 const uchar * nt_resp, uint8 sess_key[16])
291 /* yes, this session key does not change - yes, this
292 is a problem - but it is 128 bits */
294 mdfour((unsigned char *)sess_key, kr, 16);
296 #ifdef DEBUG_PASSWORD
297 DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
298 dump_data(100, sess_key, 16);
299 #endif
302 void SMBsesskeygen_lmv1(const uchar lm_hash[16],
303 const uchar lm_resp[24], /* only uses 8 */
304 uint8 sess_key[16])
306 /* Calculate the LM session key (effective length 40 bits,
307 but changes with each session) */
309 uchar p24[24];
310 uchar partial_lm_hash[16];
312 memcpy(partial_lm_hash, lm_hash, 8);
313 memset(partial_lm_hash + 8, 0xbd, 8);
315 SMBOWFencrypt(lm_hash, lm_resp, p24);
317 memcpy(sess_key, p24, 16);
318 sess_key[5] = 0xe5;
319 sess_key[6] = 0x38;
320 sess_key[7] = 0xb0;
322 #ifdef DEBUG_PASSWORD
323 DEBUG(100, ("SMBsesskeygen_lmv1:\n"));
324 dump_data(100, sess_key, 16);
325 #endif
328 DATA_BLOB NTLMv2_generate_names_blob(const char *hostname,
329 const char *domain)
331 DATA_BLOB names_blob = data_blob(NULL, 0);
333 msrpc_gen(&names_blob, "aaa",
334 True, NTLMSSP_NAME_TYPE_DOMAIN, domain,
335 True, NTLMSSP_NAME_TYPE_SERVER, hostname,
336 True, 0, "");
337 return names_blob;
340 static DATA_BLOB NTLMv2_generate_client_data(const DATA_BLOB *names_blob)
342 uchar client_chal[8];
343 DATA_BLOB response = data_blob(NULL, 0);
344 char long_date[8];
346 generate_random_buffer(client_chal, sizeof(client_chal), False);
348 put_long_date(long_date, time(NULL));
350 /* See http://www.ubiqx.org/cifs/SMB.html#SMB.8.5 */
352 msrpc_gen(&response, "ddbbdb",
353 0x00000101, /* Header */
354 0, /* 'Reserved' */
355 long_date, 8, /* Timestamp */
356 client_chal, 8, /* client challenge */
357 0, /* Unknown */
358 names_blob->data, names_blob->length); /* End of name list */
360 return response;
363 static DATA_BLOB NTLMv2_generate_response(const uchar ntlm_v2_hash[16],
364 const DATA_BLOB *server_chal,
365 const DATA_BLOB *names_blob)
367 uchar ntlmv2_response[16];
368 DATA_BLOB ntlmv2_client_data;
369 DATA_BLOB final_response;
371 /* NTLMv2 */
372 /* generate some data to pass into the response function - including
373 the hostname and domain name of the server */
374 ntlmv2_client_data = NTLMv2_generate_client_data(names_blob);
376 /* Given that data, and the challenge from the server, generate a response */
377 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &ntlmv2_client_data, ntlmv2_response);
379 final_response = data_blob(NULL, sizeof(ntlmv2_response) + ntlmv2_client_data.length);
381 memcpy(final_response.data, ntlmv2_response, sizeof(ntlmv2_response));
383 memcpy(final_response.data+sizeof(ntlmv2_response),
384 ntlmv2_client_data.data, ntlmv2_client_data.length);
386 data_blob_free(&ntlmv2_client_data);
388 return final_response;
391 static DATA_BLOB LMv2_generate_response(const uchar ntlm_v2_hash[16],
392 const DATA_BLOB *server_chal)
394 uchar lmv2_response[16];
395 DATA_BLOB lmv2_client_data = data_blob(NULL, 8);
396 DATA_BLOB final_response = data_blob(NULL, 24);
398 /* LMv2 */
399 /* client-supplied random data */
400 generate_random_buffer(lmv2_client_data.data, lmv2_client_data.length, False);
402 /* Given that data, and the challenge from the server, generate a response */
403 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &lmv2_client_data, lmv2_response);
404 memcpy(final_response.data, lmv2_response, sizeof(lmv2_response));
406 /* after the first 16 bytes is the random data we generated above,
407 so the server can verify us with it */
408 memcpy(final_response.data+sizeof(lmv2_response),
409 lmv2_client_data.data, lmv2_client_data.length);
411 data_blob_free(&lmv2_client_data);
413 return final_response;
416 BOOL SMBNTLMv2encrypt(const char *user, const char *domain, const char *password,
417 const DATA_BLOB *server_chal,
418 const DATA_BLOB *names_blob,
419 DATA_BLOB *lm_response, DATA_BLOB *nt_response,
420 DATA_BLOB *nt_session_key)
422 uchar nt_hash[16];
423 uchar ntlm_v2_hash[16];
424 E_md4hash(password, nt_hash);
426 /* We don't use the NT# directly. Instead we use it mashed up with
427 the username and domain.
428 This prevents username swapping during the auth exchange
430 if (!ntv2_owf_gen(nt_hash, user, domain, ntlm_v2_hash)) {
431 return False;
434 if (nt_response) {
435 *nt_response = NTLMv2_generate_response(ntlm_v2_hash, server_chal,
436 names_blob);
437 if (nt_session_key) {
438 *nt_session_key = data_blob(NULL, 16);
440 /* The NTLMv2 calculations also provide a session key, for signing etc later */
441 /* use only the first 16 bytes of nt_response for session key */
442 SMBsesskeygen_ntv2(ntlm_v2_hash, nt_response->data, nt_session_key->data);
446 /* LMv2 */
448 if (lm_response) {
449 *lm_response = LMv2_generate_response(ntlm_v2_hash, server_chal);
452 return True;
455 /***********************************************************
456 encode a password buffer. The caller gets to figure out
457 what to put in it.
458 ************************************************************/
459 BOOL encode_pw_buffer(char buffer[516], char *new_pw, int new_pw_length)
461 generate_random_buffer((unsigned char *)buffer, 516, True);
463 memcpy(&buffer[512 - new_pw_length], new_pw, new_pw_length);
466 * The length of the new password is in the last 4 bytes of
467 * the data buffer.
469 SIVAL(buffer, 512, new_pw_length);
471 return True;
474 /***********************************************************
475 decode a password buffer
476 *new_pw_len is the length in bytes of the possibly mulitbyte
477 returned password including termination.
478 ************************************************************/
479 BOOL decode_pw_buffer(char in_buffer[516], char *new_pwrd,
480 int new_pwrd_size, uint32 *new_pw_len)
482 int byte_len=0;
485 Warning !!! : This function is called from some rpc call.
486 The password IN the buffer is a UNICODE string.
487 The password IN new_pwrd is an ASCII string
488 If you reuse that code somewhere else check first.
491 /* The length of the new password is in the last 4 bytes of the data buffer. */
493 byte_len = IVAL(in_buffer, 512);
495 #ifdef DEBUG_PASSWORD
496 dump_data(100, in_buffer, 516);
497 #endif
499 /* Password cannot be longer than 128 characters */
500 if ( (byte_len < 0) || (byte_len > new_pwrd_size - 1)) {
501 DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len));
502 DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n"));
503 return False;
506 /* decode into the return buffer. Buffer must be a pstring */
507 *new_pw_len = pull_string(NULL, new_pwrd, &in_buffer[512 - byte_len], new_pwrd_size, byte_len, STR_UNICODE);
509 #ifdef DEBUG_PASSWORD
510 DEBUG(100,("decode_pw_buffer: new_pwrd: "));
511 dump_data(100, (char *)new_pwrd, *new_pw_len);
512 DEBUG(100,("multibyte len:%d\n", *new_pw_len));
513 DEBUG(100,("original char len:%d\n", byte_len/2));
514 #endif
516 return True;