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 #include "lib/crypto/gnutls_helpers.h"
32 #include <gnutls/gnutls.h>
33 #include <gnutls/crypto.h>
35 void SMBencrypt_hash(const uint8_t lm_hash[16], const uint8_t *c8, uint8_t p24[24])
40 memcpy(p21, lm_hash, 16);
42 SMBOWFencrypt(p21, c8, p24);
45 DEBUG(100,("SMBencrypt_hash: lm#, challenge, response\n"));
46 dump_data(100, p21, 16);
47 dump_data(100, c8, 8);
48 dump_data(100, p24, 24);
53 This implements the X/Open SMB password encryption
54 It takes a password ('unix' string), a 8 byte "crypt key"
55 and puts 24 bytes of encrypted password into p24
57 Returns False if password must have been truncated to create LM hash
60 bool SMBencrypt(const char *passwd, const uint8_t *c8, uint8_t p24[24])
65 ret = E_deshash(passwd, lm_hash);
66 SMBencrypt_hash(lm_hash, c8, p24);
71 * Creates the MD4 Hash of the users password in NT UNICODE.
72 * @param passwd password in 'unix' charset.
73 * @param p16 return password hashed with md4, caller allocated 16 byte buffer
76 bool E_md4hash(const char *passwd, uint8_t p16[16])
82 ret = push_ucs2_talloc(NULL, &wpwd, passwd, &len);
83 if (!ret || len < 2) {
84 /* We don't want to return fixed data, as most callers
86 mdfour(p16, (const uint8_t *)passwd, strlen(passwd));
91 mdfour(p16, (const uint8_t *)wpwd, len);
98 * Creates the MD5 Hash of a combination of 16 byte salt and 16 byte NT hash.
99 * @param 16 byte salt.
100 * @param 16 byte NT hash.
101 * @param 16 byte return hashed with md5, caller allocated 16 byte buffer
104 void E_md5hash(const uint8_t salt[16], const uint8_t nthash[16], uint8_t hash_out[16])
106 gnutls_hash_hd_t hash_hnd = NULL;
109 rc = gnutls_hash_init(&hash_hnd, GNUTLS_DIG_MD5);
114 rc = gnutls_hash(hash_hnd, salt, 16);
116 gnutls_hash_deinit(hash_hnd, NULL);
119 rc = gnutls_hash(hash_hnd, nthash, 16);
121 gnutls_hash_deinit(hash_hnd, NULL);
124 gnutls_hash_deinit(hash_hnd, hash_out);
131 * Creates the DES forward-only Hash of the users password in DOS ASCII charset
132 * @param passwd password in 'unix' charset.
133 * @param p16 return password hashed with DES, caller allocated 16 byte buffer
134 * @return false if password was > 14 characters, and therefore may be incorrect, otherwise true
135 * @note p16 is filled in regardless
138 bool E_deshash(const char *passwd, uint8_t p16[16])
142 TALLOC_CTX *frame = talloc_stackframe();
144 size_t converted_size;
150 tmpbuf = strupper_talloc(frame, passwd);
151 if (tmpbuf == NULL) {
152 /* Too many callers don't check this result, we need to fill in the buffer with something */
153 strlcpy((char *)dospwd, passwd ? passwd : "", sizeof(dospwd));
161 ret = convert_string_error(CH_UNIX, CH_DOS, tmpbuf, strlen(tmpbuf), dospwd, sizeof(dospwd), &converted_size);
164 /* Only the first 14 chars are considered, password need not
165 * be null terminated. We do this in the error and success
166 * case to avoid returning a fixed 'password' buffer, but
167 * callers should not use it when E_deshash returns false */
169 E_P16((const uint8_t *)dospwd, p16);
177 * Creates the MD4 and DES (LM) Hash of the users password.
178 * MD4 is of the NT Unicode, DES is of the DOS UPPERCASE password.
179 * @param passwd password in 'unix' charset.
180 * @param nt_p16 return password hashed with md4, caller allocated 16 byte buffer
181 * @param p16 return password hashed with des, caller allocated 16 byte buffer
184 /* Does both the NT and LM owfs of a user's password */
185 void nt_lm_owf_gen(const char *pwd, uint8_t nt_p16[16], uint8_t p16[16])
187 /* Calculate the MD4 hash (NT compatible) of the password */
188 memset(nt_p16, '\0', 16);
189 E_md4hash(pwd, nt_p16);
191 #ifdef DEBUG_PASSWORD
192 DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));
193 dump_data(120, (const uint8_t *)pwd, strlen(pwd));
194 dump_data(100, nt_p16, 16);
197 E_deshash(pwd, (uint8_t *)p16);
199 #ifdef DEBUG_PASSWORD
200 DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
201 dump_data(120, (const uint8_t *)pwd, strlen(pwd));
202 dump_data(100, p16, 16);
206 /* Does both the NTLMv2 owfs of a user's password */
207 bool ntv2_owf_gen(const uint8_t owf[16],
208 const char *user_in, const char *domain_in,
213 size_t user_byte_len;
214 size_t domain_byte_len;
215 gnutls_hmac_hd_t hmac_hnd = NULL;
218 TALLOC_CTX *mem_ctx = talloc_init("ntv2_owf_gen for %s\\%s", domain_in, user_in);
232 user_in = strupper_talloc(mem_ctx, user_in);
233 if (user_in == NULL) {
234 talloc_free(mem_ctx);
238 ok = push_ucs2_talloc(mem_ctx, &user, user_in, &user_byte_len );
240 DEBUG(0, ("push_uss2_talloc() for user failed)\n"));
241 talloc_free(mem_ctx);
245 ok = push_ucs2_talloc(mem_ctx, &domain, domain_in, &domain_byte_len);
247 DEBUG(0, ("push_ucs2_talloc() for domain failed\n"));
248 talloc_free(mem_ctx);
252 SMB_ASSERT(user_byte_len >= 2);
253 SMB_ASSERT(domain_byte_len >= 2);
255 /* We don't want null termination */
256 user_byte_len = user_byte_len - 2;
257 domain_byte_len = domain_byte_len - 2;
259 rc = gnutls_hmac_init(&hmac_hnd,
268 rc = gnutls_hmac(hmac_hnd, user, user_byte_len);
270 gnutls_hmac_deinit(hmac_hnd, NULL);
274 rc = gnutls_hmac(hmac_hnd, domain, domain_byte_len);
276 gnutls_hmac_deinit(hmac_hnd, NULL);
281 gnutls_hmac_deinit(hmac_hnd, kr_buf);
283 #ifdef DEBUG_PASSWORD
284 DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
285 dump_data(100, (uint8_t *)user, user_byte_len);
286 dump_data(100, (uint8_t *)domain, domain_byte_len);
287 dump_data(100, owf, 16);
288 dump_data(100, kr_buf, 16);
293 talloc_free(mem_ctx);
297 /* Does the des encryption from the NT or LM MD4 hash. */
298 void SMBOWFencrypt(const uint8_t passwd[16], const uint8_t *c8, uint8_t p24[24])
304 memcpy(p21, passwd, 16);
308 /* Does the des encryption. */
310 void SMBNTencrypt_hash(const uint8_t nt_hash[16], const uint8_t *c8, uint8_t *p24)
315 memcpy(p21, nt_hash, 16);
316 SMBOWFencrypt(p21, c8, p24);
318 #ifdef DEBUG_PASSWORD
319 DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
320 dump_data(100, p21, 16);
321 dump_data(100, c8, 8);
322 dump_data(100, p24, 24);
326 /* Does the NT MD4 hash then des encryption. Plaintext version of the above. */
328 void SMBNTencrypt(const char *passwd, const uint8_t *c8, uint8_t *p24)
331 E_md4hash(passwd, nt_hash);
332 SMBNTencrypt_hash(nt_hash, c8, p24);
336 /* Does the md5 encryption from the Key Response for NTLMv2. */
337 void SMBOWFencrypt_ntv2(const uint8_t kr[16],
338 const DATA_BLOB *srv_chal,
339 const DATA_BLOB *smbcli_chal,
340 uint8_t resp_buf[16])
342 gnutls_hmac_hd_t hmac_hnd = NULL;
345 rc = gnutls_hmac_init(&hmac_hnd,
353 rc = gnutls_hmac(hmac_hnd, srv_chal->data, srv_chal->length);
357 rc = gnutls_hmac(hmac_hnd, smbcli_chal->data, smbcli_chal->length);
359 gnutls_hmac_deinit(hmac_hnd, NULL);
363 gnutls_hmac_deinit(hmac_hnd, resp_buf);
365 #ifdef DEBUG_PASSWORD
366 DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, smbcli_chal, resp_buf\n"));
367 dump_data(100, srv_chal->data, srv_chal->length);
368 dump_data(100, smbcli_chal->data, smbcli_chal->length);
369 dump_data(100, resp_buf, 16);
373 void SMBsesskeygen_ntv2(const uint8_t kr[16],
374 const uint8_t * nt_resp, uint8_t sess_key[16])
376 /* a very nice, 128 bit, variable session key */
377 gnutls_hmac_fast(GNUTLS_MAC_MD5,
384 #ifdef DEBUG_PASSWORD
385 DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
386 dump_data(100, sess_key, 16);
390 void SMBsesskeygen_ntv1(const uint8_t kr[16], uint8_t sess_key[16])
392 /* yes, this session key does not change - yes, this
393 is a problem - but it is 128 bits */
395 mdfour((uint8_t *)sess_key, kr, 16);
397 #ifdef DEBUG_PASSWORD
398 DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
399 dump_data(100, sess_key, 16);
403 void SMBsesskeygen_lm_sess_key(const uint8_t lm_hash[16],
404 const uint8_t lm_resp[24], /* only uses 8 */
405 uint8_t sess_key[16])
407 /* Calculate the LM session key (effective length 40 bits,
408 but changes with each session) */
410 uint8_t partial_lm_hash[14];
412 memcpy(partial_lm_hash, lm_hash, 8);
413 memset(partial_lm_hash + 8, 0xbd, 6);
415 des_crypt56(p24, lm_resp, partial_lm_hash, 1);
416 des_crypt56(p24+8, lm_resp, partial_lm_hash + 7, 1);
418 memcpy(sess_key, p24, 16);
420 #ifdef DEBUG_PASSWORD
421 DEBUG(100, ("SMBsesskeygen_lm_sess_key: \n"));
422 dump_data(100, sess_key, 16);
426 DATA_BLOB NTLMv2_generate_names_blob(TALLOC_CTX *mem_ctx,
427 const char *hostname,
430 DATA_BLOB names_blob = data_blob_talloc(mem_ctx, NULL, 0);
432 /* Deliberately ignore return here.. */
433 if (hostname != NULL) {
434 (void)msrpc_gen(mem_ctx, &names_blob,
436 MsvAvNbDomainName, domain,
437 MsvAvNbComputerName, hostname,
440 (void)msrpc_gen(mem_ctx, &names_blob,
442 MsvAvNbDomainName, domain,
448 static DATA_BLOB NTLMv2_generate_client_data(TALLOC_CTX *mem_ctx,
450 const DATA_BLOB *names_blob)
452 uint8_t client_chal[8];
453 DATA_BLOB response = data_blob(NULL, 0);
454 uint8_t long_date[8];
456 generate_random_buffer(client_chal, sizeof(client_chal));
458 push_nttime(long_date, 0, nttime);
460 /* See http://www.ubiqx.org/cifs/SMB.html#SMB.8.5 */
462 /* Deliberately ignore return here.. */
463 (void)msrpc_gen(mem_ctx, &response, "ddbbdb",
464 0x00000101, /* Header */
466 long_date, 8, /* Timestamp */
467 client_chal, 8, /* client challenge */
469 names_blob->data, names_blob->length); /* End of name list */
474 static DATA_BLOB NTLMv2_generate_response(TALLOC_CTX *out_mem_ctx,
475 const uint8_t ntlm_v2_hash[16],
476 const DATA_BLOB *server_chal,
478 const DATA_BLOB *names_blob)
480 uint8_t ntlmv2_response[16];
481 DATA_BLOB ntlmv2_client_data;
482 DATA_BLOB final_response;
484 TALLOC_CTX *mem_ctx = talloc_named(out_mem_ctx, 0,
485 "NTLMv2_generate_response internal context");
488 return data_blob(NULL, 0);
492 /* generate some data to pass into the response function - including
493 the hostname and domain name of the server */
494 ntlmv2_client_data = NTLMv2_generate_client_data(mem_ctx, nttime, names_blob);
496 /* Given that data, and the challenge from the server, generate a response */
497 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &ntlmv2_client_data, ntlmv2_response);
499 final_response = data_blob_talloc(out_mem_ctx, NULL, sizeof(ntlmv2_response) + ntlmv2_client_data.length);
501 memcpy(final_response.data, ntlmv2_response, sizeof(ntlmv2_response));
503 memcpy(final_response.data+sizeof(ntlmv2_response),
504 ntlmv2_client_data.data, ntlmv2_client_data.length);
506 talloc_free(mem_ctx);
508 return final_response;
511 static DATA_BLOB LMv2_generate_response(TALLOC_CTX *mem_ctx,
512 const uint8_t ntlm_v2_hash[16],
513 const DATA_BLOB *server_chal)
515 uint8_t lmv2_response[16];
516 DATA_BLOB lmv2_client_data = data_blob_talloc(mem_ctx, NULL, 8);
517 DATA_BLOB final_response = data_blob_talloc(mem_ctx, NULL,24);
520 /* client-supplied random data */
521 generate_random_buffer(lmv2_client_data.data, lmv2_client_data.length);
523 /* Given that data, and the challenge from the server, generate a response */
524 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &lmv2_client_data, lmv2_response);
525 memcpy(final_response.data, lmv2_response, sizeof(lmv2_response));
527 /* after the first 16 bytes is the random data we generated above,
528 so the server can verify us with it */
529 memcpy(final_response.data+sizeof(lmv2_response),
530 lmv2_client_data.data, lmv2_client_data.length);
532 data_blob_free(&lmv2_client_data);
534 return final_response;
537 bool SMBNTLMv2encrypt_hash(TALLOC_CTX *mem_ctx,
538 const char *user, const char *domain, const uint8_t nt_hash[16],
539 const DATA_BLOB *server_chal,
540 const NTTIME *server_timestamp,
541 const DATA_BLOB *names_blob,
542 DATA_BLOB *lm_response, DATA_BLOB *nt_response,
543 DATA_BLOB *lm_session_key, DATA_BLOB *user_session_key)
545 uint8_t ntlm_v2_hash[16];
547 /* We don't use the NT# directly. Instead we use it mashed up with
548 the username and domain.
549 This prevents username swapping during the auth exchange
551 if (!ntv2_owf_gen(nt_hash, user, domain, ntlm_v2_hash)) {
556 const NTTIME *nttime = server_timestamp;
559 if (nttime == NULL) {
560 struct timeval tv_now = timeval_current();
561 _now = timeval_to_nttime(&tv_now);
565 *nt_response = NTLMv2_generate_response(mem_ctx,
570 if (user_session_key) {
571 *user_session_key = data_blob_talloc(mem_ctx, NULL, 16);
573 /* The NTLMv2 calculations also provide a session key, for signing etc later */
574 /* use only the first 16 bytes of nt_response for session key */
575 SMBsesskeygen_ntv2(ntlm_v2_hash, nt_response->data, user_session_key->data);
582 if (server_timestamp != NULL) {
583 *lm_response = data_blob_talloc_zero(mem_ctx, 24);
585 *lm_response = LMv2_generate_response(mem_ctx,
589 if (lm_session_key) {
590 *lm_session_key = data_blob_talloc(mem_ctx, NULL, 16);
592 /* The NTLMv2 calculations also provide a session key, for signing etc later */
593 /* use only the first 16 bytes of lm_response for session key */
594 SMBsesskeygen_ntv2(ntlm_v2_hash, lm_response->data, lm_session_key->data);
601 bool SMBNTLMv2encrypt(TALLOC_CTX *mem_ctx,
602 const char *user, const char *domain,
603 const char *password,
604 const DATA_BLOB *server_chal,
605 const DATA_BLOB *names_blob,
606 DATA_BLOB *lm_response, DATA_BLOB *nt_response,
607 DATA_BLOB *lm_session_key, DATA_BLOB *user_session_key)
610 E_md4hash(password, nt_hash);
612 return SMBNTLMv2encrypt_hash(mem_ctx,
613 user, domain, nt_hash,
614 server_chal, NULL, names_blob,
615 lm_response, nt_response, lm_session_key, user_session_key);
618 NTSTATUS NTLMv2_RESPONSE_verify_netlogon_creds(const char *account_name,
619 const char *account_domain,
620 const DATA_BLOB response,
621 const struct netlogon_creds_CredentialState *creds,
622 const char *workgroup)
624 TALLOC_CTX *frame = NULL;
625 /* RespType + HiRespType */
626 static const char *magic = "\x01\x01";
628 struct NTLMv2_RESPONSE v2_resp;
629 enum ndr_err_code err;
630 const struct AV_PAIR *av_nb_cn = NULL;
631 const struct AV_PAIR *av_nb_dn = NULL;
633 if (response.length < 48) {
635 * NTLMv2_RESPONSE has at least 48 bytes.
640 cmp = memcmp(response.data + 16, magic, 2);
643 * It doesn't look like a valid NTLMv2_RESPONSE
648 frame = talloc_stackframe();
650 err = ndr_pull_struct_blob(&response, frame, &v2_resp,
651 (ndr_pull_flags_fn_t)ndr_pull_NTLMv2_RESPONSE);
652 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
654 status = ndr_map_error2ntstatus(err);
655 DEBUG(2,("Failed to parse NTLMv2_RESPONSE "
656 "length %u - %s - %s\n",
657 (unsigned)response.length,
658 ndr_map_error2string(err),
660 dump_data(2, response.data, response.length);
666 NDR_PRINT_DEBUG(NTLMv2_RESPONSE, &v2_resp);
670 * Make sure the netbios computer name in the
671 * NTLMv2_RESPONSE matches the computer name
672 * in the secure channel credentials for workstation
675 * And the netbios domain name matches our
678 * This prevents workstations from requesting
679 * the session key of NTLMSSP sessions of clients
682 if (creds->secure_channel_type == SEC_CHAN_WKSTA) {
683 av_nb_cn = ndr_ntlmssp_find_av(&v2_resp.Challenge.AvPairs,
684 MsvAvNbComputerName);
685 av_nb_dn = ndr_ntlmssp_find_av(&v2_resp.Challenge.AvPairs,
689 if (av_nb_cn != NULL) {
690 const char *v = NULL;
694 v = av_nb_cn->Value.AvNbComputerName;
696 a = talloc_strdup(frame, creds->account_name);
699 return NT_STATUS_NO_MEMORY;
702 if (len > 0 && a[len - 1] == '$') {
706 cmp = strcasecmp_m(a, v);
708 DEBUG(2,("%s: NTLMv2_RESPONSE with "
709 "NbComputerName[%s] rejected "
711 "against SEC_CHAN_WKSTA[%s/%s] "
712 "in workgroup[%s]\n",
716 creds->computer_name,
720 return NT_STATUS_LOGON_FAILURE;
723 if (av_nb_dn != NULL) {
724 const char *v = NULL;
726 v = av_nb_dn->Value.AvNbDomainName;
728 cmp = strcasecmp_m(workgroup, v);
730 DEBUG(2,("%s: NTLMv2_RESPONSE with "
731 "NbDomainName[%s] rejected "
733 "against SEC_CHAN_WKSTA[%s/%s] "
734 "in workgroup[%s]\n",
738 creds->computer_name,
742 return NT_STATUS_LOGON_FAILURE;
750 /***********************************************************
751 encode a password buffer with a unicode password. The buffer
752 is filled with random data to make it harder to attack.
753 ************************************************************/
754 bool encode_pw_buffer(uint8_t buffer[516], const char *password, int string_flags)
759 /* the incoming buffer can be any alignment. */
760 string_flags |= STR_NOALIGN;
762 new_pw_len = push_string(new_pw,
764 sizeof(new_pw), string_flags);
765 if (new_pw_len == -1) {
769 memcpy(&buffer[512 - new_pw_len], new_pw, new_pw_len);
771 generate_random_buffer(buffer, 512 - new_pw_len);
774 * The length of the new password is in the last 4 bytes of
777 SIVAL(buffer, 512, new_pw_len);
783 /***********************************************************
784 decode a password buffer
785 *new_pw_len is the length in bytes of the possibly mulitbyte
786 returned password including termination.
787 ************************************************************/
789 bool decode_pw_buffer(TALLOC_CTX *ctx,
790 uint8_t in_buffer[516],
793 charset_t string_charset)
801 Warning !!! : This function is called from some rpc call.
802 The password IN the buffer may be a UNICODE string.
803 The password IN new_pwrd is an ASCII string
804 If you reuse that code somewhere else check first.
807 /* The length of the new password is in the last 4 bytes of the data buffer. */
809 byte_len = IVAL(in_buffer, 512);
811 #ifdef DEBUG_PASSWORD
812 dump_data(100, in_buffer, 516);
815 /* Password cannot be longer than the size of the password buffer */
816 if ( (byte_len < 0) || (byte_len > 512)) {
817 DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len));
818 DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n"));
822 /* decode into the return buffer. */
823 if (!convert_string_talloc(ctx, string_charset, CH_UNIX,
824 &in_buffer[512 - byte_len],
828 DEBUG(0, ("decode_pw_buffer: failed to convert incoming password\n"));
832 #ifdef DEBUG_PASSWORD
833 DEBUG(100,("decode_pw_buffer: new_pwrd: "));
834 dump_data(100, (uint8_t *)*pp_new_pwrd, *new_pw_len);
835 DEBUG(100,("multibyte len:%lu\n", (unsigned long int)*new_pw_len));
836 DEBUG(100,("original char len:%d\n", byte_len/2));
842 /***********************************************************
843 Decode an arc4 encrypted password change buffer.
844 ************************************************************/
846 NTSTATUS encode_or_decode_arc4_passwd_buffer(unsigned char pw_buf[532],
847 const DATA_BLOB *psession_key)
849 gnutls_hash_hd_t hash_hnd = NULL;
850 unsigned char key_out[16];
854 /* Confounder is last 16 bytes. */
856 rc = gnutls_hash_init(&hash_hnd, GNUTLS_DIG_MD5);
858 status = gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED);
862 rc = gnutls_hash(hash_hnd, &pw_buf[516], 16);
864 gnutls_hash_deinit(hash_hnd, NULL);
865 status = gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED);
868 rc = gnutls_hash(hash_hnd, psession_key->data, psession_key->length);
870 gnutls_hash_deinit(hash_hnd, NULL);
871 status = gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED);
874 gnutls_hash_deinit(hash_hnd, key_out);
876 /* arc4 with key_out. */
877 arcfour_crypt(pw_buf, key_out, 516);
881 status = NT_STATUS_OK;
886 /***********************************************************
887 encode a password buffer with an already unicode password. The
888 rest of the buffer is filled with random data to make it harder to attack.
889 ************************************************************/
890 bool set_pw_in_buffer(uint8_t buffer[516], const DATA_BLOB *password)
892 if (password->length > 512) {
896 memcpy(&buffer[512 - password->length], password->data, password->length);
898 generate_random_buffer(buffer, 512 - password->length);
901 * The length of the new password is in the last 4 bytes of
904 SIVAL(buffer, 512, password->length);
908 /***********************************************************
909 decode a password buffer
910 *new_pw_size is the length in bytes of the extracted unicode password
911 ************************************************************/
912 bool extract_pw_from_buffer(TALLOC_CTX *mem_ctx,
913 uint8_t in_buffer[516], DATA_BLOB *new_pass)
917 /* The length of the new password is in the last 4 bytes of the data buffer. */
919 byte_len = IVAL(in_buffer, 512);
921 #ifdef DEBUG_PASSWORD
922 dump_data(100, in_buffer, 516);
925 /* Password cannot be longer than the size of the password buffer */
926 if ( (byte_len < 0) || (byte_len > 512)) {
930 *new_pass = data_blob_talloc(mem_ctx, &in_buffer[512 - byte_len], byte_len);
932 if (!new_pass->data) {
940 /* encode a wkssvc_PasswordBuffer:
942 * similar to samr_CryptPasswordEx. Different: 8byte confounder (instead of
943 * 16byte), confounder in front of the 516 byte buffer (instead of after that
944 * buffer), calling MD5Update() first with session_key and then with confounder
945 * (vice versa in samr) - Guenther */
947 void encode_wkssvc_join_password_buffer(TALLOC_CTX *mem_ctx,
949 DATA_BLOB *session_key,
950 struct wkssvc_PasswordBuffer **pwd_buf)
953 gnutls_hash_hd_t hash_hnd = NULL;
954 struct wkssvc_PasswordBuffer *my_pwd_buf = NULL;
955 DATA_BLOB confounded_session_key;
956 int confounder_len = 8;
957 uint8_t confounder[8];
960 my_pwd_buf = talloc_zero(mem_ctx, struct wkssvc_PasswordBuffer);
965 confounded_session_key = data_blob_talloc(mem_ctx, NULL, 16);
967 encode_pw_buffer(buffer, pwd, STR_UNICODE);
969 generate_random_buffer((uint8_t *)confounder, confounder_len);
971 rc = gnutls_hash_init(&hash_hnd, GNUTLS_DIG_MD5);
976 rc = gnutls_hash(hash_hnd, session_key->data, session_key->length);
978 gnutls_hash_deinit(hash_hnd, NULL);
981 rc = gnutls_hash(hash_hnd, confounder, confounder_len);
983 gnutls_hash_deinit(hash_hnd, NULL);
986 gnutls_hash_deinit(hash_hnd, confounded_session_key.data);
988 arcfour_crypt_blob(buffer, 516, &confounded_session_key);
990 memcpy(&my_pwd_buf->data[0], confounder, confounder_len);
991 ZERO_ARRAY(confounder);
992 memcpy(&my_pwd_buf->data[8], buffer, 516);
995 data_blob_clear_free(&confounded_session_key);
997 *pwd_buf = my_pwd_buf;
1003 WERROR decode_wkssvc_join_password_buffer(TALLOC_CTX *mem_ctx,
1004 struct wkssvc_PasswordBuffer *pwd_buf,
1005 DATA_BLOB *session_key,
1008 gnutls_hash_hd_t hash_hnd = NULL;
1009 uint8_t buffer[516];
1015 DATA_BLOB confounded_session_key;
1017 int confounder_len = 8;
1018 uint8_t confounder[8];
1023 return WERR_INVALID_PASSWORD;
1026 if (session_key->length != 16) {
1027 DEBUG(10,("invalid session key\n"));
1028 return WERR_INVALID_PASSWORD;
1031 confounded_session_key = data_blob_talloc(mem_ctx, NULL, 16);
1033 memcpy(&confounder, &pwd_buf->data[0], confounder_len);
1034 memcpy(&buffer, &pwd_buf->data[8], 516);
1036 rc = gnutls_hash_init(&hash_hnd, GNUTLS_DIG_MD5);
1038 result = gnutls_error_to_werror(rc, WERR_CONTENT_BLOCKED);
1042 rc = gnutls_hash(hash_hnd, session_key->data, session_key->length);
1044 gnutls_hash_deinit(hash_hnd, NULL);
1045 result = gnutls_error_to_werror(rc, WERR_CONTENT_BLOCKED);
1048 rc = gnutls_hash(hash_hnd, confounder, confounder_len);
1050 gnutls_hash_deinit(hash_hnd, NULL);
1051 result = gnutls_error_to_werror(rc, WERR_CONTENT_BLOCKED);
1054 gnutls_hash_deinit(hash_hnd, confounded_session_key.data);
1056 arcfour_crypt_blob(buffer, 516, &confounded_session_key);
1058 ok = decode_pw_buffer(mem_ctx, buffer, pwd, &pwd_len, CH_UTF16);
1060 ZERO_ARRAY(confounder);
1063 data_blob_clear_free(&confounded_session_key);
1066 result = WERR_INVALID_PASSWORD;