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 3 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, see <http://www.gnu.org/licenses/>.
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/ndr_ntlmssp.h"
31 void SMBencrypt_hash(const uint8_t lm_hash[16], const uint8_t *c8, uint8_t p24[24])
36 memcpy(p21, lm_hash, 16);
38 SMBOWFencrypt(p21, c8, p24);
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);
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
53 Returns False if password must have been truncated to create LM hash
56 bool SMBencrypt(const char *passwd, const uint8_t *c8, uint8_t p24[24])
61 ret = E_deshash(passwd, lm_hash);
62 SMBencrypt_hash(lm_hash, c8, p24);
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
72 bool E_md4hash(const char *passwd, uint8_t p16[16])
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
82 mdfour(p16, (const uint8_t *)passwd, strlen(passwd));
87 mdfour(p16, (const uint8_t *)wpwd, len);
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
100 void E_md5hash(const uint8_t salt[16], const uint8_t nthash[16], uint8_t hash_out[16])
104 MD5Update(&tctx, salt, 16);
105 MD5Update(&tctx, nthash, 16);
106 MD5Final(hash_out, &tctx);
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
117 bool E_deshash(const char *passwd, uint8_t p16[16])
121 TALLOC_CTX *frame = talloc_stackframe();
123 size_t converted_size;
129 tmpbuf = strupper_talloc(frame, passwd);
130 if (tmpbuf == NULL) {
131 /* Too many callers don't check this result, we need to fill in the buffer with something */
132 strlcpy((char *)dospwd, passwd ? passwd : "", sizeof(dospwd));
140 ret = convert_string_error(CH_UNIX, CH_DOS, tmpbuf, strlen(tmpbuf), dospwd, sizeof(dospwd), &converted_size);
143 /* Only the first 14 chars are considered, password need not
144 * be null terminated. We do this in the error and success
145 * case to avoid returning a fixed 'password' buffer, but
146 * callers should not use it when E_deshash returns false */
148 E_P16((const uint8_t *)dospwd, p16);
156 * Creates the MD4 and DES (LM) Hash of the users password.
157 * MD4 is of the NT Unicode, DES is of the DOS UPPERCASE password.
158 * @param passwd password in 'unix' charset.
159 * @param nt_p16 return password hashed with md4, caller allocated 16 byte buffer
160 * @param p16 return password hashed with des, caller allocated 16 byte buffer
163 /* Does both the NT and LM owfs of a user's password */
164 void nt_lm_owf_gen(const char *pwd, uint8_t nt_p16[16], uint8_t p16[16])
166 /* Calculate the MD4 hash (NT compatible) of the password */
167 memset(nt_p16, '\0', 16);
168 E_md4hash(pwd, nt_p16);
170 #ifdef DEBUG_PASSWORD
171 DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));
172 dump_data(120, (const uint8_t *)pwd, strlen(pwd));
173 dump_data(100, nt_p16, 16);
176 E_deshash(pwd, (uint8_t *)p16);
178 #ifdef DEBUG_PASSWORD
179 DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
180 dump_data(120, (const uint8_t *)pwd, strlen(pwd));
181 dump_data(100, p16, 16);
185 /* Does both the NTLMv2 owfs of a user's password */
186 bool ntv2_owf_gen(const uint8_t owf[16],
187 const char *user_in, const char *domain_in,
192 size_t user_byte_len;
193 size_t domain_byte_len;
197 TALLOC_CTX *mem_ctx = talloc_init("ntv2_owf_gen for %s\\%s", domain_in, user_in);
211 user_in = strupper_talloc(mem_ctx, user_in);
212 if (user_in == NULL) {
213 talloc_free(mem_ctx);
217 ret = push_ucs2_talloc(mem_ctx, &user, user_in, &user_byte_len );
219 DEBUG(0, ("push_uss2_talloc() for user failed)\n"));
220 talloc_free(mem_ctx);
224 ret = push_ucs2_talloc(mem_ctx, &domain, domain_in, &domain_byte_len);
226 DEBUG(0, ("push_ucs2_talloc() for domain failed\n"));
227 talloc_free(mem_ctx);
231 SMB_ASSERT(user_byte_len >= 2);
232 SMB_ASSERT(domain_byte_len >= 2);
234 /* We don't want null termination */
235 user_byte_len = user_byte_len - 2;
236 domain_byte_len = domain_byte_len - 2;
238 hmac_md5_init_limK_to_64(owf, 16, &ctx);
239 hmac_md5_update((uint8_t *)user, user_byte_len, &ctx);
240 hmac_md5_update((uint8_t *)domain, domain_byte_len, &ctx);
241 hmac_md5_final(kr_buf, &ctx);
243 #ifdef DEBUG_PASSWORD
244 DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
245 dump_data(100, (uint8_t *)user, user_byte_len);
246 dump_data(100, (uint8_t *)domain, domain_byte_len);
247 dump_data(100, owf, 16);
248 dump_data(100, kr_buf, 16);
251 talloc_free(mem_ctx);
255 /* Does the des encryption from the NT or LM MD4 hash. */
256 void SMBOWFencrypt(const uint8_t passwd[16], const uint8_t *c8, uint8_t p24[24])
262 memcpy(p21, passwd, 16);
266 /* Does the des encryption. */
268 void SMBNTencrypt_hash(const uint8_t nt_hash[16], const uint8_t *c8, uint8_t *p24)
273 memcpy(p21, nt_hash, 16);
274 SMBOWFencrypt(p21, c8, p24);
276 #ifdef DEBUG_PASSWORD
277 DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
278 dump_data(100, p21, 16);
279 dump_data(100, c8, 8);
280 dump_data(100, p24, 24);
284 /* Does the NT MD4 hash then des encryption. Plaintext version of the above. */
286 void SMBNTencrypt(const char *passwd, const uint8_t *c8, uint8_t *p24)
289 E_md4hash(passwd, nt_hash);
290 SMBNTencrypt_hash(nt_hash, c8, p24);
294 /* Does the md5 encryption from the Key Response for NTLMv2. */
295 void SMBOWFencrypt_ntv2(const uint8_t kr[16],
296 const DATA_BLOB *srv_chal,
297 const DATA_BLOB *smbcli_chal,
298 uint8_t resp_buf[16])
302 hmac_md5_init_limK_to_64(kr, 16, &ctx);
303 hmac_md5_update(srv_chal->data, srv_chal->length, &ctx);
304 hmac_md5_update(smbcli_chal->data, smbcli_chal->length, &ctx);
305 hmac_md5_final(resp_buf, &ctx);
307 #ifdef DEBUG_PASSWORD
308 DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, smbcli_chal, resp_buf\n"));
309 dump_data(100, srv_chal->data, srv_chal->length);
310 dump_data(100, smbcli_chal->data, smbcli_chal->length);
311 dump_data(100, resp_buf, 16);
315 void SMBsesskeygen_ntv2(const uint8_t kr[16],
316 const uint8_t * nt_resp, uint8_t sess_key[16])
318 /* a very nice, 128 bit, variable session key */
322 hmac_md5_init_limK_to_64(kr, 16, &ctx);
323 hmac_md5_update(nt_resp, 16, &ctx);
324 hmac_md5_final((uint8_t *)sess_key, &ctx);
326 #ifdef DEBUG_PASSWORD
327 DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
328 dump_data(100, sess_key, 16);
332 void SMBsesskeygen_ntv1(const uint8_t kr[16], uint8_t sess_key[16])
334 /* yes, this session key does not change - yes, this
335 is a problem - but it is 128 bits */
337 mdfour((uint8_t *)sess_key, kr, 16);
339 #ifdef DEBUG_PASSWORD
340 DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
341 dump_data(100, sess_key, 16);
345 void SMBsesskeygen_lm_sess_key(const uint8_t lm_hash[16],
346 const uint8_t lm_resp[24], /* only uses 8 */
347 uint8_t sess_key[16])
349 /* Calculate the LM session key (effective length 40 bits,
350 but changes with each session) */
352 uint8_t partial_lm_hash[14];
354 memcpy(partial_lm_hash, lm_hash, 8);
355 memset(partial_lm_hash + 8, 0xbd, 6);
357 des_crypt56(p24, lm_resp, partial_lm_hash, 1);
358 des_crypt56(p24+8, lm_resp, partial_lm_hash + 7, 1);
360 memcpy(sess_key, p24, 16);
362 #ifdef DEBUG_PASSWORD
363 DEBUG(100, ("SMBsesskeygen_lm_sess_key: \n"));
364 dump_data(100, sess_key, 16);
368 DATA_BLOB NTLMv2_generate_names_blob(TALLOC_CTX *mem_ctx,
369 const char *hostname,
372 DATA_BLOB names_blob = data_blob_talloc(mem_ctx, NULL, 0);
374 /* Deliberately ignore return here.. */
375 if (hostname != NULL) {
376 (void)msrpc_gen(mem_ctx, &names_blob,
378 MsvAvNbDomainName, domain,
379 MsvAvNbComputerName, hostname,
382 (void)msrpc_gen(mem_ctx, &names_blob,
384 MsvAvNbDomainName, domain,
390 static DATA_BLOB NTLMv2_generate_client_data(TALLOC_CTX *mem_ctx,
392 const DATA_BLOB *names_blob)
394 uint8_t client_chal[8];
395 DATA_BLOB response = data_blob(NULL, 0);
396 uint8_t long_date[8];
398 generate_random_buffer(client_chal, sizeof(client_chal));
400 push_nttime(long_date, 0, nttime);
402 /* See http://www.ubiqx.org/cifs/SMB.html#SMB.8.5 */
404 /* Deliberately ignore return here.. */
405 (void)msrpc_gen(mem_ctx, &response, "ddbbdb",
406 0x00000101, /* Header */
408 long_date, 8, /* Timestamp */
409 client_chal, 8, /* client challenge */
411 names_blob->data, names_blob->length); /* End of name list */
416 static DATA_BLOB NTLMv2_generate_response(TALLOC_CTX *out_mem_ctx,
417 const uint8_t ntlm_v2_hash[16],
418 const DATA_BLOB *server_chal,
420 const DATA_BLOB *names_blob)
422 uint8_t ntlmv2_response[16];
423 DATA_BLOB ntlmv2_client_data;
424 DATA_BLOB final_response;
426 TALLOC_CTX *mem_ctx = talloc_named(out_mem_ctx, 0,
427 "NTLMv2_generate_response internal context");
430 return data_blob(NULL, 0);
434 /* generate some data to pass into the response function - including
435 the hostname and domain name of the server */
436 ntlmv2_client_data = NTLMv2_generate_client_data(mem_ctx, nttime, names_blob);
438 /* Given that data, and the challenge from the server, generate a response */
439 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &ntlmv2_client_data, ntlmv2_response);
441 final_response = data_blob_talloc(out_mem_ctx, NULL, sizeof(ntlmv2_response) + ntlmv2_client_data.length);
443 memcpy(final_response.data, ntlmv2_response, sizeof(ntlmv2_response));
445 memcpy(final_response.data+sizeof(ntlmv2_response),
446 ntlmv2_client_data.data, ntlmv2_client_data.length);
448 talloc_free(mem_ctx);
450 return final_response;
453 static DATA_BLOB LMv2_generate_response(TALLOC_CTX *mem_ctx,
454 const uint8_t ntlm_v2_hash[16],
455 const DATA_BLOB *server_chal)
457 uint8_t lmv2_response[16];
458 DATA_BLOB lmv2_client_data = data_blob_talloc(mem_ctx, NULL, 8);
459 DATA_BLOB final_response = data_blob_talloc(mem_ctx, NULL,24);
462 /* client-supplied random data */
463 generate_random_buffer(lmv2_client_data.data, lmv2_client_data.length);
465 /* Given that data, and the challenge from the server, generate a response */
466 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &lmv2_client_data, lmv2_response);
467 memcpy(final_response.data, lmv2_response, sizeof(lmv2_response));
469 /* after the first 16 bytes is the random data we generated above,
470 so the server can verify us with it */
471 memcpy(final_response.data+sizeof(lmv2_response),
472 lmv2_client_data.data, lmv2_client_data.length);
474 data_blob_free(&lmv2_client_data);
476 return final_response;
479 bool SMBNTLMv2encrypt_hash(TALLOC_CTX *mem_ctx,
480 const char *user, const char *domain, const uint8_t nt_hash[16],
481 const DATA_BLOB *server_chal,
482 const NTTIME *server_timestamp,
483 const DATA_BLOB *names_blob,
484 DATA_BLOB *lm_response, DATA_BLOB *nt_response,
485 DATA_BLOB *lm_session_key, DATA_BLOB *user_session_key)
487 uint8_t ntlm_v2_hash[16];
489 /* We don't use the NT# directly. Instead we use it mashed up with
490 the username and domain.
491 This prevents username swapping during the auth exchange
493 if (!ntv2_owf_gen(nt_hash, user, domain, ntlm_v2_hash)) {
498 const NTTIME *nttime = server_timestamp;
501 if (nttime == NULL) {
502 struct timeval tv_now = timeval_current();
503 _now = timeval_to_nttime(&tv_now);
507 *nt_response = NTLMv2_generate_response(mem_ctx,
512 if (user_session_key) {
513 *user_session_key = data_blob_talloc(mem_ctx, NULL, 16);
515 /* The NTLMv2 calculations also provide a session key, for signing etc later */
516 /* use only the first 16 bytes of nt_response for session key */
517 SMBsesskeygen_ntv2(ntlm_v2_hash, nt_response->data, user_session_key->data);
524 if (server_timestamp != NULL) {
525 *lm_response = data_blob_talloc_zero(mem_ctx, 24);
527 *lm_response = LMv2_generate_response(mem_ctx,
531 if (lm_session_key) {
532 *lm_session_key = data_blob_talloc(mem_ctx, NULL, 16);
534 /* The NTLMv2 calculations also provide a session key, for signing etc later */
535 /* use only the first 16 bytes of lm_response for session key */
536 SMBsesskeygen_ntv2(ntlm_v2_hash, lm_response->data, lm_session_key->data);
543 bool SMBNTLMv2encrypt(TALLOC_CTX *mem_ctx,
544 const char *user, const char *domain,
545 const char *password,
546 const DATA_BLOB *server_chal,
547 const DATA_BLOB *names_blob,
548 DATA_BLOB *lm_response, DATA_BLOB *nt_response,
549 DATA_BLOB *lm_session_key, DATA_BLOB *user_session_key)
552 E_md4hash(password, nt_hash);
554 return SMBNTLMv2encrypt_hash(mem_ctx,
555 user, domain, nt_hash,
556 server_chal, NULL, names_blob,
557 lm_response, nt_response, lm_session_key, user_session_key);
560 NTSTATUS NTLMv2_RESPONSE_verify_netlogon_creds(const char *account_name,
561 const char *account_domain,
562 const DATA_BLOB response,
563 const struct netlogon_creds_CredentialState *creds,
564 const char *workgroup)
566 TALLOC_CTX *frame = NULL;
567 /* RespType + HiRespType */
568 static const char *magic = "\x01\x01";
570 struct NTLMv2_RESPONSE v2_resp;
571 enum ndr_err_code err;
572 const struct AV_PAIR *av_nb_cn = NULL;
573 const struct AV_PAIR *av_nb_dn = NULL;
575 if (response.length < 48) {
577 * NTLMv2_RESPONSE has at least 48 bytes.
582 cmp = memcmp(response.data + 16, magic, 2);
585 * It doesn't look like a valid NTLMv2_RESPONSE
590 frame = talloc_stackframe();
592 err = ndr_pull_struct_blob(&response, frame, &v2_resp,
593 (ndr_pull_flags_fn_t)ndr_pull_NTLMv2_RESPONSE);
594 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
596 status = ndr_map_error2ntstatus(err);
597 DEBUG(2,("Failed to parse NTLMv2_RESPONSE "
598 "length %u - %s - %s\n",
599 (unsigned)response.length,
600 ndr_map_error2string(err),
602 dump_data(2, response.data, response.length);
608 NDR_PRINT_DEBUG(NTLMv2_RESPONSE, &v2_resp);
612 * Make sure the netbios computer name in the
613 * NTLMv2_RESPONSE matches the computer name
614 * in the secure channel credentials for workstation
617 * And the netbios domain name matches our
620 * This prevents workstations from requesting
621 * the session key of NTLMSSP sessions of clients
624 if (creds->secure_channel_type == SEC_CHAN_WKSTA) {
625 av_nb_cn = ndr_ntlmssp_find_av(&v2_resp.Challenge.AvPairs,
626 MsvAvNbComputerName);
627 av_nb_dn = ndr_ntlmssp_find_av(&v2_resp.Challenge.AvPairs,
631 if (av_nb_cn != NULL) {
632 const char *v = NULL;
636 v = av_nb_cn->Value.AvNbComputerName;
638 a = talloc_strdup(frame, creds->account_name);
641 return NT_STATUS_NO_MEMORY;
644 if (len > 0 && a[len - 1] == '$') {
648 cmp = strcasecmp_m(a, v);
650 DEBUG(2,("%s: NTLMv2_RESPONSE with "
651 "NbComputerName[%s] rejected "
653 "against SEC_CHAN_WKSTA[%s/%s] "
654 "in workgroup[%s]\n",
658 creds->computer_name,
662 return NT_STATUS_LOGON_FAILURE;
665 if (av_nb_dn != NULL) {
666 const char *v = NULL;
668 v = av_nb_dn->Value.AvNbDomainName;
670 cmp = strcasecmp_m(workgroup, v);
672 DEBUG(2,("%s: NTLMv2_RESPONSE with "
673 "NbDomainName[%s] rejected "
675 "against SEC_CHAN_WKSTA[%s/%s] "
676 "in workgroup[%s]\n",
680 creds->computer_name,
684 return NT_STATUS_LOGON_FAILURE;
692 /***********************************************************
693 encode a password buffer with a unicode password. The buffer
694 is filled with random data to make it harder to attack.
695 ************************************************************/
696 bool encode_pw_buffer(uint8_t buffer[516], const char *password, int string_flags)
701 /* the incoming buffer can be any alignment. */
702 string_flags |= STR_NOALIGN;
704 new_pw_len = push_string(new_pw,
706 sizeof(new_pw), string_flags);
707 if (new_pw_len == -1) {
711 memcpy(&buffer[512 - new_pw_len], new_pw, new_pw_len);
713 generate_random_buffer(buffer, 512 - new_pw_len);
716 * The length of the new password is in the last 4 bytes of
719 SIVAL(buffer, 512, new_pw_len);
725 /***********************************************************
726 decode a password buffer
727 *new_pw_len is the length in bytes of the possibly mulitbyte
728 returned password including termination.
729 ************************************************************/
731 bool decode_pw_buffer(TALLOC_CTX *ctx,
732 uint8_t in_buffer[516],
735 charset_t string_charset)
743 Warning !!! : This function is called from some rpc call.
744 The password IN the buffer may be a UNICODE string.
745 The password IN new_pwrd is an ASCII string
746 If you reuse that code somewhere else check first.
749 /* The length of the new password is in the last 4 bytes of the data buffer. */
751 byte_len = IVAL(in_buffer, 512);
753 #ifdef DEBUG_PASSWORD
754 dump_data(100, in_buffer, 516);
757 /* Password cannot be longer than the size of the password buffer */
758 if ( (byte_len < 0) || (byte_len > 512)) {
759 DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len));
760 DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n"));
764 /* decode into the return buffer. */
765 if (!convert_string_talloc(ctx, string_charset, CH_UNIX,
766 &in_buffer[512 - byte_len],
770 DEBUG(0, ("decode_pw_buffer: failed to convert incoming password\n"));
774 #ifdef DEBUG_PASSWORD
775 DEBUG(100,("decode_pw_buffer: new_pwrd: "));
776 dump_data(100, (uint8_t *)*pp_new_pwrd, *new_pw_len);
777 DEBUG(100,("multibyte len:%lu\n", (unsigned long int)*new_pw_len));
778 DEBUG(100,("original char len:%d\n", byte_len/2));
784 /***********************************************************
785 Decode an arc4 encrypted password change buffer.
786 ************************************************************/
788 void encode_or_decode_arc4_passwd_buffer(unsigned char pw_buf[532], const DATA_BLOB *psession_key)
791 unsigned char key_out[16];
793 /* Confounder is last 16 bytes. */
796 MD5Update(&tctx, &pw_buf[516], 16);
797 MD5Update(&tctx, psession_key->data, psession_key->length);
798 MD5Final(key_out, &tctx);
799 /* arc4 with key_out. */
800 arcfour_crypt(pw_buf, key_out, 516);
803 /***********************************************************
804 encode a password buffer with an already unicode password. The
805 rest of the buffer is filled with random data to make it harder to attack.
806 ************************************************************/
807 bool set_pw_in_buffer(uint8_t buffer[516], const DATA_BLOB *password)
809 if (password->length > 512) {
813 memcpy(&buffer[512 - password->length], password->data, password->length);
815 generate_random_buffer(buffer, 512 - password->length);
818 * The length of the new password is in the last 4 bytes of
821 SIVAL(buffer, 512, password->length);
825 /***********************************************************
826 decode a password buffer
827 *new_pw_size is the length in bytes of the extracted unicode password
828 ************************************************************/
829 bool extract_pw_from_buffer(TALLOC_CTX *mem_ctx,
830 uint8_t in_buffer[516], DATA_BLOB *new_pass)
834 /* The length of the new password is in the last 4 bytes of the data buffer. */
836 byte_len = IVAL(in_buffer, 512);
838 #ifdef DEBUG_PASSWORD
839 dump_data(100, in_buffer, 516);
842 /* Password cannot be longer than the size of the password buffer */
843 if ( (byte_len < 0) || (byte_len > 512)) {
847 *new_pass = data_blob_talloc(mem_ctx, &in_buffer[512 - byte_len], byte_len);
849 if (!new_pass->data) {
857 /* encode a wkssvc_PasswordBuffer:
859 * similar to samr_CryptPasswordEx. Different: 8byte confounder (instead of
860 * 16byte), confounder in front of the 516 byte buffer (instead of after that
861 * buffer), calling MD5Update() first with session_key and then with confounder
862 * (vice versa in samr) - Guenther */
864 void encode_wkssvc_join_password_buffer(TALLOC_CTX *mem_ctx,
866 DATA_BLOB *session_key,
867 struct wkssvc_PasswordBuffer **pwd_buf)
871 struct wkssvc_PasswordBuffer *my_pwd_buf = NULL;
872 DATA_BLOB confounded_session_key;
873 int confounder_len = 8;
874 uint8_t confounder[8];
876 my_pwd_buf = talloc_zero(mem_ctx, struct wkssvc_PasswordBuffer);
881 confounded_session_key = data_blob_talloc(mem_ctx, NULL, 16);
883 encode_pw_buffer(buffer, pwd, STR_UNICODE);
885 generate_random_buffer((uint8_t *)confounder, confounder_len);
888 MD5Update(&ctx, session_key->data, session_key->length);
889 MD5Update(&ctx, confounder, confounder_len);
890 MD5Final(confounded_session_key.data, &ctx);
892 arcfour_crypt_blob(buffer, 516, &confounded_session_key);
894 memcpy(&my_pwd_buf->data[0], confounder, confounder_len);
895 memcpy(&my_pwd_buf->data[8], buffer, 516);
897 data_blob_free(&confounded_session_key);
899 *pwd_buf = my_pwd_buf;
902 WERROR decode_wkssvc_join_password_buffer(TALLOC_CTX *mem_ctx,
903 struct wkssvc_PasswordBuffer *pwd_buf,
904 DATA_BLOB *session_key,
911 DATA_BLOB confounded_session_key;
913 int confounder_len = 8;
914 uint8_t confounder[8];
919 return WERR_INVALID_PASSWORD;
922 if (session_key->length != 16) {
923 DEBUG(10,("invalid session key\n"));
924 return WERR_INVALID_PASSWORD;
927 confounded_session_key = data_blob_talloc(mem_ctx, NULL, 16);
929 memcpy(&confounder, &pwd_buf->data[0], confounder_len);
930 memcpy(&buffer, &pwd_buf->data[8], 516);
933 MD5Update(&ctx, session_key->data, session_key->length);
934 MD5Update(&ctx, confounder, confounder_len);
935 MD5Final(confounded_session_key.data, &ctx);
937 arcfour_crypt_blob(buffer, 516, &confounded_session_key);
939 if (!decode_pw_buffer(mem_ctx, buffer, pwd, &pwd_len, CH_UTF16)) {
940 data_blob_free(&confounded_session_key);
941 return WERR_INVALID_PASSWORD;
944 data_blob_free(&confounded_session_key);