source: trunk/server/libcli/auth/smbencrypt.c@ 752

Last change on this file since 752 was 752, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.9 2nd part

File size: 21.7 KB
Line 
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
9
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 3 of the License, or
13 (at your option) any later version.
14
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.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include "includes.h"
25#include "system/time.h"
26#include "../libcli/auth/msrpc_parse.h"
27#include "../lib/crypto/crypto.h"
28#include "../libcli/auth/libcli_auth.h"
29#include "../librpc/gen_ndr/ntlmssp.h"
30
31void SMBencrypt_hash(const uint8_t lm_hash[16], const uint8_t *c8, uint8_t p24[24])
32{
33 uint8_t p21[21];
34
35 memset(p21,'\0',21);
36 memcpy(p21, lm_hash, 16);
37
38 SMBOWFencrypt(p21, c8, p24);
39
40#ifdef DEBUG_PASSWORD
41 DEBUG(100,("SMBencrypt_hash: lm#, challenge, response\n"));
42 dump_data(100, p21, 16);
43 dump_data(100, c8, 8);
44 dump_data(100, p24, 24);
45#endif
46}
47
48/*
49 This implements the X/Open SMB password encryption
50 It takes a password ('unix' string), a 8 byte "crypt key"
51 and puts 24 bytes of encrypted password into p24
52
53 Returns False if password must have been truncated to create LM hash
54*/
55
56bool SMBencrypt(const char *passwd, const uint8_t *c8, uint8_t p24[24])
57{
58 bool ret;
59 uint8_t lm_hash[16];
60
61 ret = E_deshash(passwd, lm_hash);
62 SMBencrypt_hash(lm_hash, c8, p24);
63 return ret;
64}
65
66/**
67 * Creates the MD4 Hash of the users password in NT UNICODE.
68 * @param passwd password in 'unix' charset.
69 * @param p16 return password hashed with md4, caller allocated 16 byte buffer
70 */
71
72bool E_md4hash(const char *passwd, uint8_t p16[16])
73{
74 size_t len;
75 smb_ucs2_t *wpwd;
76 bool ret;
77
78 ret = push_ucs2_talloc(NULL, &wpwd, passwd, &len);
79 if (!ret || len < 2) {
80 /* We don't want to return fixed data, as most callers
81 * don't check */
82 mdfour(p16, (const uint8_t *)passwd, strlen(passwd));
83 return false;
84 }
85
86 len -= 2;
87 mdfour(p16, (const uint8_t *)wpwd, len);
88
89 talloc_free(wpwd);
90 return true;
91}
92
93/**
94 * Creates the MD5 Hash of a combination of 16 byte salt and 16 byte NT hash.
95 * @param 16 byte salt.
96 * @param 16 byte NT hash.
97 * @param 16 byte return hashed with md5, caller allocated 16 byte buffer
98 */
99
100void E_md5hash(const uint8_t salt[16], const uint8_t nthash[16], uint8_t hash_out[16])
101{
102 MD5_CTX tctx;
103 MD5Init(&tctx);
104 MD5Update(&tctx, salt, 16);
105 MD5Update(&tctx, nthash, 16);
106 MD5Final(hash_out, &tctx);
107}
108
109/**
110 * Creates the DES forward-only Hash of the users password in DOS ASCII charset
111 * @param passwd password in 'unix' charset.
112 * @param p16 return password hashed with DES, caller allocated 16 byte buffer
113 * @return false if password was > 14 characters, and therefore may be incorrect, otherwise true
114 * @note p16 is filled in regardless
115 */
116
117bool E_deshash(const char *passwd, uint8_t p16[16])
118{
119 bool ret = true;
120 char dospwd[256];
121 ZERO_STRUCT(dospwd);
122
123 /* Password must be converted to DOS charset - null terminated, uppercase. */
124 push_string(dospwd, passwd, sizeof(dospwd), STR_ASCII|STR_UPPER|STR_TERMINATE);
125
126 /* Only the first 14 chars are considered, password need not be null terminated. */
127 E_P16((const uint8_t *)dospwd, p16);
128
129 if (strlen(dospwd) > 14) {
130 ret = false;
131 }
132
133 ZERO_STRUCT(dospwd);
134
135 return ret;
136}
137
138/**
139 * Creates the MD4 and DES (LM) Hash of the users password.
140 * MD4 is of the NT Unicode, DES is of the DOS UPPERCASE password.
141 * @param passwd password in 'unix' charset.
142 * @param nt_p16 return password hashed with md4, caller allocated 16 byte buffer
143 * @param p16 return password hashed with des, caller allocated 16 byte buffer
144 */
145
146/* Does both the NT and LM owfs of a user's password */
147void nt_lm_owf_gen(const char *pwd, uint8_t nt_p16[16], uint8_t p16[16])
148{
149 /* Calculate the MD4 hash (NT compatible) of the password */
150 memset(nt_p16, '\0', 16);
151 E_md4hash(pwd, nt_p16);
152
153#ifdef DEBUG_PASSWORD
154 DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));
155 dump_data(120, (const uint8_t *)pwd, strlen(pwd));
156 dump_data(100, nt_p16, 16);
157#endif
158
159 E_deshash(pwd, (uint8_t *)p16);
160
161#ifdef DEBUG_PASSWORD
162 DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
163 dump_data(120, (const uint8_t *)pwd, strlen(pwd));
164 dump_data(100, p16, 16);
165#endif
166}
167
168/* Does both the NTLMv2 owfs of a user's password */
169bool ntv2_owf_gen(const uint8_t owf[16],
170 const char *user_in, const char *domain_in,
171 uint8_t kr_buf[16])
172{
173 smb_ucs2_t *user;
174 smb_ucs2_t *domain;
175 size_t user_byte_len;
176 size_t domain_byte_len;
177 bool ret;
178
179 HMACMD5Context ctx;
180 TALLOC_CTX *mem_ctx = talloc_init("ntv2_owf_gen for %s\\%s", domain_in, user_in);
181
182 if (!mem_ctx) {
183 return false;
184 }
185
186 if (!user_in) {
187 user_in = "";
188 }
189
190 if (!domain_in) {
191 domain_in = "";
192 }
193
194 user_in = strupper_talloc(mem_ctx, user_in);
195 if (user_in == NULL) {
196 talloc_free(mem_ctx);
197 return false;
198 }
199
200 ret = push_ucs2_talloc(mem_ctx, &user, user_in, &user_byte_len );
201 if (!ret) {
202 DEBUG(0, ("push_uss2_talloc() for user failed)\n"));
203 talloc_free(mem_ctx);
204 return false;
205 }
206
207 ret = push_ucs2_talloc(mem_ctx, &domain, domain_in, &domain_byte_len);
208 if (!ret) {
209 DEBUG(0, ("push_ucs2_talloc() for domain failed\n"));
210 talloc_free(mem_ctx);
211 return false;
212 }
213
214 SMB_ASSERT(user_byte_len >= 2);
215 SMB_ASSERT(domain_byte_len >= 2);
216
217 /* We don't want null termination */
218 user_byte_len = user_byte_len - 2;
219 domain_byte_len = domain_byte_len - 2;
220
221 hmac_md5_init_limK_to_64(owf, 16, &ctx);
222 hmac_md5_update((uint8_t *)user, user_byte_len, &ctx);
223 hmac_md5_update((uint8_t *)domain, domain_byte_len, &ctx);
224 hmac_md5_final(kr_buf, &ctx);
225
226#ifdef DEBUG_PASSWORD
227 DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
228 dump_data(100, (uint8_t *)user, user_byte_len);
229 dump_data(100, (uint8_t *)domain, domain_byte_len);
230 dump_data(100, owf, 16);
231 dump_data(100, kr_buf, 16);
232#endif
233
234 talloc_free(mem_ctx);
235 return true;
236}
237
238/* Does the des encryption from the NT or LM MD4 hash. */
239void SMBOWFencrypt(const uint8_t passwd[16], const uint8_t *c8, uint8_t p24[24])
240{
241 uint8_t p21[21];
242
243 ZERO_STRUCT(p21);
244
245 memcpy(p21, passwd, 16);
246 E_P24(p21, c8, p24);
247}
248
249/* Does the des encryption. */
250
251void SMBNTencrypt_hash(const uint8_t nt_hash[16], uint8_t *c8, uint8_t *p24)
252{
253 uint8_t p21[21];
254
255 memset(p21,'\0',21);
256 memcpy(p21, nt_hash, 16);
257 SMBOWFencrypt(p21, c8, p24);
258
259#ifdef DEBUG_PASSWORD
260 DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
261 dump_data(100, p21, 16);
262 dump_data(100, c8, 8);
263 dump_data(100, p24, 24);
264#endif
265}
266
267/* Does the NT MD4 hash then des encryption. Plaintext version of the above. */
268
269void SMBNTencrypt(const char *passwd, uint8_t *c8, uint8_t *p24)
270{
271 uint8_t nt_hash[16];
272 E_md4hash(passwd, nt_hash);
273 SMBNTencrypt_hash(nt_hash, c8, p24);
274}
275
276
277/* Does the md5 encryption from the Key Response for NTLMv2. */
278void SMBOWFencrypt_ntv2(const uint8_t kr[16],
279 const DATA_BLOB *srv_chal,
280 const DATA_BLOB *smbcli_chal,
281 uint8_t resp_buf[16])
282{
283 HMACMD5Context ctx;
284
285 hmac_md5_init_limK_to_64(kr, 16, &ctx);
286 hmac_md5_update(srv_chal->data, srv_chal->length, &ctx);
287 hmac_md5_update(smbcli_chal->data, smbcli_chal->length, &ctx);
288 hmac_md5_final(resp_buf, &ctx);
289
290#ifdef DEBUG_PASSWORD
291 DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, smbcli_chal, resp_buf\n"));
292 dump_data(100, srv_chal->data, srv_chal->length);
293 dump_data(100, smbcli_chal->data, smbcli_chal->length);
294 dump_data(100, resp_buf, 16);
295#endif
296}
297
298void SMBsesskeygen_ntv2(const uint8_t kr[16],
299 const uint8_t * nt_resp, uint8_t sess_key[16])
300{
301 /* a very nice, 128 bit, variable session key */
302
303 HMACMD5Context ctx;
304
305 hmac_md5_init_limK_to_64(kr, 16, &ctx);
306 hmac_md5_update(nt_resp, 16, &ctx);
307 hmac_md5_final((uint8_t *)sess_key, &ctx);
308
309#ifdef DEBUG_PASSWORD
310 DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
311 dump_data(100, sess_key, 16);
312#endif
313}
314
315void SMBsesskeygen_ntv1(const uint8_t kr[16], uint8_t sess_key[16])
316{
317 /* yes, this session key does not change - yes, this
318 is a problem - but it is 128 bits */
319
320 mdfour((uint8_t *)sess_key, kr, 16);
321
322#ifdef DEBUG_PASSWORD
323 DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
324 dump_data(100, sess_key, 16);
325#endif
326}
327
328void SMBsesskeygen_lm_sess_key(const uint8_t lm_hash[16],
329 const uint8_t lm_resp[24], /* only uses 8 */
330 uint8_t sess_key[16])
331{
332 /* Calculate the LM session key (effective length 40 bits,
333 but changes with each session) */
334 uint8_t p24[24];
335 uint8_t partial_lm_hash[14];
336
337 memcpy(partial_lm_hash, lm_hash, 8);
338 memset(partial_lm_hash + 8, 0xbd, 6);
339
340 des_crypt56(p24, lm_resp, partial_lm_hash, 1);
341 des_crypt56(p24+8, lm_resp, partial_lm_hash + 7, 1);
342
343 memcpy(sess_key, p24, 16);
344
345#ifdef DEBUG_PASSWORD
346 DEBUG(100, ("SMBsesskeygen_lm_sess_key: \n"));
347 dump_data(100, sess_key, 16);
348#endif
349}
350
351DATA_BLOB NTLMv2_generate_names_blob(TALLOC_CTX *mem_ctx,
352 const char *hostname,
353 const char *domain)
354{
355 DATA_BLOB names_blob = data_blob_talloc(mem_ctx, NULL, 0);
356
357 /* Deliberately ignore return here.. */
358 (void)msrpc_gen(mem_ctx, &names_blob,
359 "aaa",
360 MsvAvNbDomainName, domain,
361 MsvAvNbComputerName, hostname,
362 MsvAvEOL, "");
363 return names_blob;
364}
365
366static DATA_BLOB NTLMv2_generate_client_data(TALLOC_CTX *mem_ctx, const DATA_BLOB *names_blob)
367{
368 uint8_t client_chal[8];
369 DATA_BLOB response = data_blob(NULL, 0);
370 uint8_t long_date[8];
371 NTTIME nttime;
372
373 unix_to_nt_time(&nttime, time(NULL));
374
375 generate_random_buffer(client_chal, sizeof(client_chal));
376
377 push_nttime(long_date, 0, nttime);
378
379 /* See http://www.ubiqx.org/cifs/SMB.html#SMB.8.5 */
380
381 /* Deliberately ignore return here.. */
382 (void)msrpc_gen(mem_ctx, &response, "ddbbdb",
383 0x00000101, /* Header */
384 0, /* 'Reserved' */
385 long_date, 8, /* Timestamp */
386 client_chal, 8, /* client challenge */
387 0, /* Unknown */
388 names_blob->data, names_blob->length); /* End of name list */
389
390 return response;
391}
392
393static DATA_BLOB NTLMv2_generate_response(TALLOC_CTX *out_mem_ctx,
394 const uint8_t ntlm_v2_hash[16],
395 const DATA_BLOB *server_chal,
396 const DATA_BLOB *names_blob)
397{
398 uint8_t ntlmv2_response[16];
399 DATA_BLOB ntlmv2_client_data;
400 DATA_BLOB final_response;
401
402 TALLOC_CTX *mem_ctx = talloc_named(out_mem_ctx, 0,
403 "NTLMv2_generate_response internal context");
404
405 if (!mem_ctx) {
406 return data_blob(NULL, 0);
407 }
408
409 /* NTLMv2 */
410 /* generate some data to pass into the response function - including
411 the hostname and domain name of the server */
412 ntlmv2_client_data = NTLMv2_generate_client_data(mem_ctx, names_blob);
413
414 /* Given that data, and the challenge from the server, generate a response */
415 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &ntlmv2_client_data, ntlmv2_response);
416
417 final_response = data_blob_talloc(out_mem_ctx, NULL, sizeof(ntlmv2_response) + ntlmv2_client_data.length);
418
419 memcpy(final_response.data, ntlmv2_response, sizeof(ntlmv2_response));
420
421 memcpy(final_response.data+sizeof(ntlmv2_response),
422 ntlmv2_client_data.data, ntlmv2_client_data.length);
423
424 talloc_free(mem_ctx);
425
426 return final_response;
427}
428
429static DATA_BLOB LMv2_generate_response(TALLOC_CTX *mem_ctx,
430 const uint8_t ntlm_v2_hash[16],
431 const DATA_BLOB *server_chal)
432{
433 uint8_t lmv2_response[16];
434 DATA_BLOB lmv2_client_data = data_blob_talloc(mem_ctx, NULL, 8);
435 DATA_BLOB final_response = data_blob_talloc(mem_ctx, NULL,24);
436
437 /* LMv2 */
438 /* client-supplied random data */
439 generate_random_buffer(lmv2_client_data.data, lmv2_client_data.length);
440
441 /* Given that data, and the challenge from the server, generate a response */
442 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &lmv2_client_data, lmv2_response);
443 memcpy(final_response.data, lmv2_response, sizeof(lmv2_response));
444
445 /* after the first 16 bytes is the random data we generated above,
446 so the server can verify us with it */
447 memcpy(final_response.data+sizeof(lmv2_response),
448 lmv2_client_data.data, lmv2_client_data.length);
449
450 data_blob_free(&lmv2_client_data);
451
452 return final_response;
453}
454
455bool SMBNTLMv2encrypt_hash(TALLOC_CTX *mem_ctx,
456 const char *user, const char *domain, const uint8_t nt_hash[16],
457 const DATA_BLOB *server_chal,
458 const DATA_BLOB *names_blob,
459 DATA_BLOB *lm_response, DATA_BLOB *nt_response,
460 DATA_BLOB *lm_session_key, DATA_BLOB *user_session_key)
461{
462 uint8_t ntlm_v2_hash[16];
463
464 /* We don't use the NT# directly. Instead we use it mashed up with
465 the username and domain.
466 This prevents username swapping during the auth exchange
467 */
468 if (!ntv2_owf_gen(nt_hash, user, domain, ntlm_v2_hash)) {
469 return false;
470 }
471
472 if (nt_response) {
473 *nt_response = NTLMv2_generate_response(mem_ctx,
474 ntlm_v2_hash, server_chal,
475 names_blob);
476 if (user_session_key) {
477 *user_session_key = data_blob_talloc(mem_ctx, NULL, 16);
478
479 /* The NTLMv2 calculations also provide a session key, for signing etc later */
480 /* use only the first 16 bytes of nt_response for session key */
481 SMBsesskeygen_ntv2(ntlm_v2_hash, nt_response->data, user_session_key->data);
482 }
483 }
484
485 /* LMv2 */
486
487 if (lm_response) {
488 *lm_response = LMv2_generate_response(mem_ctx,
489 ntlm_v2_hash, server_chal);
490 if (lm_session_key) {
491 *lm_session_key = data_blob_talloc(mem_ctx, NULL, 16);
492
493 /* The NTLMv2 calculations also provide a session key, for signing etc later */
494 /* use only the first 16 bytes of lm_response for session key */
495 SMBsesskeygen_ntv2(ntlm_v2_hash, lm_response->data, lm_session_key->data);
496 }
497 }
498
499 return true;
500}
501
502bool SMBNTLMv2encrypt(TALLOC_CTX *mem_ctx,
503 const char *user, const char *domain,
504 const char *password,
505 const DATA_BLOB *server_chal,
506 const DATA_BLOB *names_blob,
507 DATA_BLOB *lm_response, DATA_BLOB *nt_response,
508 DATA_BLOB *lm_session_key, DATA_BLOB *user_session_key)
509{
510 uint8_t nt_hash[16];
511 E_md4hash(password, nt_hash);
512
513 return SMBNTLMv2encrypt_hash(mem_ctx,
514 user, domain, nt_hash, server_chal, names_blob,
515 lm_response, nt_response, lm_session_key, user_session_key);
516}
517
518/***********************************************************
519 encode a password buffer with a unicode password. The buffer
520 is filled with random data to make it harder to attack.
521************************************************************/
522bool encode_pw_buffer(uint8_t buffer[516], const char *password, int string_flags)
523{
524 uint8_t new_pw[512];
525 ssize_t new_pw_len;
526
527 /* the incoming buffer can be any alignment. */
528 string_flags |= STR_NOALIGN;
529
530 new_pw_len = push_string(new_pw,
531 password,
532 sizeof(new_pw), string_flags);
533 if (new_pw_len == -1) {
534 return false;
535 }
536
537 memcpy(&buffer[512 - new_pw_len], new_pw, new_pw_len);
538
539 generate_random_buffer(buffer, 512 - new_pw_len);
540
541 /*
542 * The length of the new password is in the last 4 bytes of
543 * the data buffer.
544 */
545 SIVAL(buffer, 512, new_pw_len);
546 ZERO_STRUCT(new_pw);
547 return true;
548}
549
550
551/***********************************************************
552 decode a password buffer
553 *new_pw_len is the length in bytes of the possibly mulitbyte
554 returned password including termination.
555************************************************************/
556
557bool decode_pw_buffer(TALLOC_CTX *ctx,
558 uint8_t in_buffer[516],
559 char **pp_new_pwrd,
560 size_t *new_pw_len,
561 charset_t string_charset)
562{
563 int byte_len=0;
564
565 *pp_new_pwrd = NULL;
566 *new_pw_len = 0;
567
568 /*
569 Warning !!! : This function is called from some rpc call.
570 The password IN the buffer may be a UNICODE string.
571 The password IN new_pwrd is an ASCII string
572 If you reuse that code somewhere else check first.
573 */
574
575 /* The length of the new password is in the last 4 bytes of the data buffer. */
576
577 byte_len = IVAL(in_buffer, 512);
578
579#ifdef DEBUG_PASSWORD
580 dump_data(100, in_buffer, 516);
581#endif
582
583 /* Password cannot be longer than the size of the password buffer */
584 if ( (byte_len < 0) || (byte_len > 512)) {
585 DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len));
586 DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n"));
587 return false;
588 }
589
590 /* decode into the return buffer. */
591 if (!convert_string_talloc(ctx, string_charset, CH_UNIX,
592 &in_buffer[512 - byte_len],
593 byte_len,
594 (void *)pp_new_pwrd,
595 new_pw_len,
596 false)) {
597 DEBUG(0, ("decode_pw_buffer: failed to convert incoming password\n"));
598 return false;
599 }
600
601#ifdef DEBUG_PASSWORD
602 DEBUG(100,("decode_pw_buffer: new_pwrd: "));
603 dump_data(100, (uint8_t *)*pp_new_pwrd, *new_pw_len);
604 DEBUG(100,("multibyte len:%lu\n", (unsigned long int)*new_pw_len));
605 DEBUG(100,("original char len:%d\n", byte_len/2));
606#endif
607
608 return true;
609}
610
611/***********************************************************
612 Decode an arc4 encrypted password change buffer.
613************************************************************/
614
615void encode_or_decode_arc4_passwd_buffer(unsigned char pw_buf[532], const DATA_BLOB *psession_key)
616{
617 MD5_CTX tctx;
618 unsigned char key_out[16];
619
620 /* Confounder is last 16 bytes. */
621
622 MD5Init(&tctx);
623 MD5Update(&tctx, &pw_buf[516], 16);
624 MD5Update(&tctx, psession_key->data, psession_key->length);
625 MD5Final(key_out, &tctx);
626 /* arc4 with key_out. */
627 arcfour_crypt(pw_buf, key_out, 516);
628}
629
630/***********************************************************
631 encode a password buffer with an already unicode password. The
632 rest of the buffer is filled with random data to make it harder to attack.
633************************************************************/
634bool set_pw_in_buffer(uint8_t buffer[516], DATA_BLOB *password)
635{
636 if (password->length > 512) {
637 return false;
638 }
639
640 memcpy(&buffer[512 - password->length], password->data, password->length);
641
642 generate_random_buffer(buffer, 512 - password->length);
643
644 /*
645 * The length of the new password is in the last 4 bytes of
646 * the data buffer.
647 */
648 SIVAL(buffer, 512, password->length);
649 return true;
650}
651
652/***********************************************************
653 decode a password buffer
654 *new_pw_size is the length in bytes of the extracted unicode password
655************************************************************/
656bool extract_pw_from_buffer(TALLOC_CTX *mem_ctx,
657 uint8_t in_buffer[516], DATA_BLOB *new_pass)
658{
659 int byte_len=0;
660
661 /* The length of the new password is in the last 4 bytes of the data buffer. */
662
663 byte_len = IVAL(in_buffer, 512);
664
665#ifdef DEBUG_PASSWORD
666 dump_data(100, in_buffer, 516);
667#endif
668
669 /* Password cannot be longer than the size of the password buffer */
670 if ( (byte_len < 0) || (byte_len > 512)) {
671 return false;
672 }
673
674 *new_pass = data_blob_talloc(mem_ctx, &in_buffer[512 - byte_len], byte_len);
675
676 if (!new_pass->data) {
677 return false;
678 }
679
680 return true;
681}
682
683
684/* encode a wkssvc_PasswordBuffer:
685 *
686 * similar to samr_CryptPasswordEx. Different: 8byte confounder (instead of
687 * 16byte), confounder in front of the 516 byte buffer (instead of after that
688 * buffer), calling MD5Update() first with session_key and then with confounder
689 * (vice versa in samr) - Guenther */
690
691void encode_wkssvc_join_password_buffer(TALLOC_CTX *mem_ctx,
692 const char *pwd,
693 DATA_BLOB *session_key,
694 struct wkssvc_PasswordBuffer **pwd_buf)
695{
696 uint8_t buffer[516];
697 MD5_CTX ctx;
698 struct wkssvc_PasswordBuffer *my_pwd_buf = NULL;
699 DATA_BLOB confounded_session_key;
700 int confounder_len = 8;
701 uint8_t confounder[8];
702
703 my_pwd_buf = talloc_zero(mem_ctx, struct wkssvc_PasswordBuffer);
704 if (!my_pwd_buf) {
705 return;
706 }
707
708 confounded_session_key = data_blob_talloc(mem_ctx, NULL, 16);
709
710 encode_pw_buffer(buffer, pwd, STR_UNICODE);
711
712 generate_random_buffer((uint8_t *)confounder, confounder_len);
713
714 MD5Init(&ctx);
715 MD5Update(&ctx, session_key->data, session_key->length);
716 MD5Update(&ctx, confounder, confounder_len);
717 MD5Final(confounded_session_key.data, &ctx);
718
719 arcfour_crypt_blob(buffer, 516, &confounded_session_key);
720
721 memcpy(&my_pwd_buf->data[0], confounder, confounder_len);
722 memcpy(&my_pwd_buf->data[8], buffer, 516);
723
724 data_blob_free(&confounded_session_key);
725
726 *pwd_buf = my_pwd_buf;
727}
728
729WERROR decode_wkssvc_join_password_buffer(TALLOC_CTX *mem_ctx,
730 struct wkssvc_PasswordBuffer *pwd_buf,
731 DATA_BLOB *session_key,
732 char **pwd)
733{
734 uint8_t buffer[516];
735 MD5_CTX ctx;
736 size_t pwd_len;
737
738 DATA_BLOB confounded_session_key;
739
740 int confounder_len = 8;
741 uint8_t confounder[8];
742
743 *pwd = NULL;
744
745 if (!pwd_buf) {
746 return WERR_BAD_PASSWORD;
747 }
748
749 if (session_key->length != 16) {
750 DEBUG(10,("invalid session key\n"));
751 return WERR_BAD_PASSWORD;
752 }
753
754 confounded_session_key = data_blob_talloc(mem_ctx, NULL, 16);
755
756 memcpy(&confounder, &pwd_buf->data[0], confounder_len);
757 memcpy(&buffer, &pwd_buf->data[8], 516);
758
759 MD5Init(&ctx);
760 MD5Update(&ctx, session_key->data, session_key->length);
761 MD5Update(&ctx, confounder, confounder_len);
762 MD5Final(confounded_session_key.data, &ctx);
763
764 arcfour_crypt_blob(buffer, 516, &confounded_session_key);
765
766 if (!decode_pw_buffer(mem_ctx, buffer, pwd, &pwd_len, CH_UTF16)) {
767 data_blob_free(&confounded_session_key);
768 return WERR_BAD_PASSWORD;
769 }
770
771 data_blob_free(&confounded_session_key);
772
773 return WERR_OK;
774}
775
Note: See TracBrowser for help on using the repository browser.