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"
30 #include "lib/util/bytearray.h"
32 #include "lib/crypto/gnutls_helpers.h"
33 #include <gnutls/gnutls.h>
34 #include <gnutls/crypto.h>
36 int SMBencrypt_hash(const uint8_t lm_hash
[16], const uint8_t *c8
, uint8_t p24
[24])
42 memcpy(p21
, lm_hash
, 16);
44 rc
= SMBOWFencrypt(p21
, c8
, p24
);
47 DEBUG(100,("SMBencrypt_hash: lm#, challenge, response\n"));
48 dump_data(100, p21
, 16);
49 dump_data(100, c8
, 8);
50 dump_data(100, p24
, 24);
57 This implements the X/Open SMB password encryption
58 It takes a password ('unix' string), a 8 byte "crypt key"
59 and puts 24 bytes of encrypted password into p24
61 Returns False if password must have been truncated to create LM hash
64 bool SMBencrypt(const char *passwd
, const uint8_t *c8
, uint8_t p24
[24])
70 ret
= E_deshash(passwd
, lm_hash
);
71 rc
= SMBencrypt_hash(lm_hash
, c8
, p24
);
79 * Creates the MD4 Hash of the users password in NT UNICODE.
80 * @param passwd password in 'unix' charset.
81 * @param p16 return password hashed with md4, caller allocated 16 byte buffer
84 bool E_md4hash(const char *passwd
, uint8_t p16
[16])
90 ret
= push_ucs2_talloc(NULL
, &wpwd
, passwd
, &len
);
91 if (!ret
|| len
< 2) {
92 /* We don't want to return fixed data, as most callers
94 mdfour(p16
, (const uint8_t *)passwd
, strlen(passwd
));
99 mdfour(p16
, (const uint8_t *)wpwd
, len
);
106 * Creates the DES forward-only Hash of the users password in DOS ASCII charset
107 * @param passwd password in 'unix' charset.
108 * @param p16 return password hashed with DES, caller allocated 16 byte buffer
109 * @return false if password was > 14 characters, and therefore may be incorrect, otherwise true
110 * @note p16 is filled in regardless
113 bool E_deshash(const char *passwd
, uint8_t p16
[16])
118 TALLOC_CTX
*frame
= talloc_stackframe();
120 size_t converted_size
;
126 tmpbuf
= strupper_talloc(frame
, passwd
);
127 if (tmpbuf
== NULL
) {
128 /* Too many callers don't check this result, we need to fill in the buffer with something */
129 strlcpy((char *)dospwd
, passwd
? passwd
: "", sizeof(dospwd
));
137 ret
= convert_string_error(CH_UNIX
, CH_DOS
, tmpbuf
, strlen(tmpbuf
), dospwd
, sizeof(dospwd
), &converted_size
);
140 /* Only the first 14 chars are considered, password need not
141 * be null terminated. We do this in the error and success
142 * case to avoid returning a fixed 'password' buffer, but
143 * callers should not use it when E_deshash returns false */
145 rc
= 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
;
194 gnutls_hmac_hd_t hmac_hnd
= NULL
;
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 ok
= 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 ok
= 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 rc
= gnutls_hmac_init(&hmac_hnd
,
247 rc
= gnutls_hmac(hmac_hnd
, user
, user_byte_len
);
249 gnutls_hmac_deinit(hmac_hnd
, NULL
);
253 rc
= gnutls_hmac(hmac_hnd
, domain
, domain_byte_len
);
255 gnutls_hmac_deinit(hmac_hnd
, NULL
);
260 gnutls_hmac_deinit(hmac_hnd
, kr_buf
);
262 #ifdef DEBUG_PASSWORD
263 DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
264 dump_data(100, (uint8_t *)user
, user_byte_len
);
265 dump_data(100, (uint8_t *)domain
, domain_byte_len
);
266 dump_data(100, owf
, 16);
267 dump_data(100, kr_buf
, 16);
272 talloc_free(mem_ctx
);
276 /* Does the des encryption from the NT or LM MD4 hash. */
277 int SMBOWFencrypt(const uint8_t passwd
[16], const uint8_t *c8
, uint8_t p24
[24])
283 memcpy(p21
, passwd
, 16);
284 return E_P24(p21
, c8
, p24
);
287 /* Does the des encryption. */
289 int SMBNTencrypt_hash(const uint8_t nt_hash
[16], const uint8_t *c8
, uint8_t *p24
)
295 memcpy(p21
, nt_hash
, 16);
296 rc
= SMBOWFencrypt(p21
, c8
, p24
);
298 #ifdef DEBUG_PASSWORD
299 DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
300 dump_data(100, p21
, 16);
301 dump_data(100, c8
, 8);
302 dump_data(100, p24
, 24);
308 /* Does the NT MD4 hash then des encryption. Plaintext version of the above. */
310 int SMBNTencrypt(const char *passwd
, const uint8_t *c8
, uint8_t *p24
)
313 E_md4hash(passwd
, nt_hash
);
314 return SMBNTencrypt_hash(nt_hash
, c8
, p24
);
318 /* Does the md5 encryption from the Key Response for NTLMv2. */
319 NTSTATUS
SMBOWFencrypt_ntv2(const uint8_t kr
[16],
320 const DATA_BLOB
*srv_chal
,
321 const DATA_BLOB
*smbcli_chal
,
322 uint8_t resp_buf
[16])
324 gnutls_hmac_hd_t hmac_hnd
= NULL
;
328 rc
= gnutls_hmac_init(&hmac_hnd
,
333 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
336 rc
= gnutls_hmac(hmac_hnd
, srv_chal
->data
, srv_chal
->length
);
338 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
341 rc
= gnutls_hmac(hmac_hnd
, smbcli_chal
->data
, smbcli_chal
->length
);
343 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
348 status
= NT_STATUS_OK
;
350 gnutls_hmac_deinit(hmac_hnd
, resp_buf
);
351 #ifdef DEBUG_PASSWORD
352 DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, smbcli_chal, resp_buf: %s\n",
354 dump_data(100, srv_chal
->data
, srv_chal
->length
);
355 dump_data(100, smbcli_chal
->data
, smbcli_chal
->length
);
356 dump_data(100, resp_buf
, 16);
361 NTSTATUS
SMBsesskeygen_ntv2(const uint8_t kr
[16],
362 const uint8_t *nt_resp
,
363 uint8_t sess_key
[16])
367 /* a very nice, 128 bit, variable session key */
368 rc
= gnutls_hmac_fast(GNUTLS_MAC_MD5
,
375 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HMAC_NOT_SUPPORTED
);
378 #ifdef DEBUG_PASSWORD
379 DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
380 dump_data(100, sess_key
, 16);
386 void SMBsesskeygen_ntv1(const uint8_t kr
[16], uint8_t sess_key
[16])
388 /* yes, this session key does not change - yes, this
389 is a problem - but it is 128 bits */
391 mdfour((uint8_t *)sess_key
, kr
, 16);
393 #ifdef DEBUG_PASSWORD
394 DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
395 dump_data(100, sess_key
, 16);
399 NTSTATUS
SMBsesskeygen_lm_sess_key(const uint8_t lm_hash
[16],
400 const uint8_t lm_resp
[24], /* only uses 8 */
401 uint8_t sess_key
[16])
403 /* Calculate the LM session key (effective length 40 bits,
404 but changes with each session) */
406 uint8_t partial_lm_hash
[14];
409 memcpy(partial_lm_hash
, lm_hash
, 8);
410 memset(partial_lm_hash
+ 8, 0xbd, 6);
412 rc
= des_crypt56_gnutls(p24
, lm_resp
, partial_lm_hash
, SAMBA_GNUTLS_ENCRYPT
);
414 return gnutls_error_to_ntstatus(rc
, NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER
);
416 rc
= des_crypt56_gnutls(p24
+8, lm_resp
, partial_lm_hash
+ 7, SAMBA_GNUTLS_ENCRYPT
);
418 return gnutls_error_to_ntstatus(rc
, NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER
);
421 memcpy(sess_key
, p24
, 16);
423 #ifdef DEBUG_PASSWORD
424 DEBUG(100, ("SMBsesskeygen_lm_sess_key: \n"));
425 dump_data(100, sess_key
, 16);
431 DATA_BLOB
NTLMv2_generate_names_blob(TALLOC_CTX
*mem_ctx
,
432 const char *hostname
,
435 DATA_BLOB names_blob
= data_blob_talloc(mem_ctx
, NULL
, 0);
437 /* Deliberately ignore return here.. */
438 if (hostname
!= NULL
) {
439 (void)msrpc_gen(mem_ctx
, &names_blob
,
441 MsvAvNbDomainName
, domain
,
442 MsvAvNbComputerName
, hostname
,
445 (void)msrpc_gen(mem_ctx
, &names_blob
,
447 MsvAvNbDomainName
, domain
,
453 static DATA_BLOB
NTLMv2_generate_client_data(TALLOC_CTX
*mem_ctx
,
455 const DATA_BLOB
*names_blob
)
457 uint8_t client_chal
[8];
458 DATA_BLOB response
= data_blob(NULL
, 0);
459 uint8_t long_date
[8];
461 generate_random_buffer(client_chal
, sizeof(client_chal
));
463 push_nttime(long_date
, 0, nttime
);
465 /* See http://www.ubiqx.org/cifs/SMB.html#SMB.8.5 */
467 /* Deliberately ignore return here.. */
468 (void)msrpc_gen(mem_ctx
, &response
, "ddbbdb",
469 0x00000101, /* Header */
471 long_date
, 8, /* Timestamp */
472 client_chal
, 8, /* client challenge */
474 names_blob
->data
, names_blob
->length
); /* End of name list */
479 static DATA_BLOB
NTLMv2_generate_response(TALLOC_CTX
*out_mem_ctx
,
480 const uint8_t ntlm_v2_hash
[16],
481 const DATA_BLOB
*server_chal
,
483 const DATA_BLOB
*names_blob
)
485 uint8_t ntlmv2_response
[16];
486 DATA_BLOB ntlmv2_client_data
;
487 DATA_BLOB final_response
;
490 TALLOC_CTX
*mem_ctx
= talloc_named(out_mem_ctx
, 0,
491 "NTLMv2_generate_response internal context");
494 return data_blob(NULL
, 0);
498 /* generate some data to pass into the response function - including
499 the hostname and domain name of the server */
500 ntlmv2_client_data
= NTLMv2_generate_client_data(mem_ctx
, nttime
, names_blob
);
502 /* Given that data, and the challenge from the server, generate a response */
503 status
= SMBOWFencrypt_ntv2(ntlm_v2_hash
,
507 if (!NT_STATUS_IS_OK(status
)) {
508 talloc_free(mem_ctx
);
509 return data_blob(NULL
, 0);
512 final_response
= data_blob_talloc(out_mem_ctx
, NULL
, sizeof(ntlmv2_response
) + ntlmv2_client_data
.length
);
514 memcpy(final_response
.data
, ntlmv2_response
, sizeof(ntlmv2_response
));
516 memcpy(final_response
.data
+sizeof(ntlmv2_response
),
517 ntlmv2_client_data
.data
, ntlmv2_client_data
.length
);
519 talloc_free(mem_ctx
);
521 return final_response
;
524 static DATA_BLOB
LMv2_generate_response(TALLOC_CTX
*mem_ctx
,
525 const uint8_t ntlm_v2_hash
[16],
526 const DATA_BLOB
*server_chal
)
528 uint8_t lmv2_response
[16];
529 DATA_BLOB lmv2_client_data
= data_blob_talloc(mem_ctx
, NULL
, 8);
530 DATA_BLOB final_response
= data_blob_talloc(mem_ctx
, NULL
,24);
534 /* client-supplied random data */
535 generate_random_buffer(lmv2_client_data
.data
, lmv2_client_data
.length
);
537 /* Given that data, and the challenge from the server, generate a response */
538 status
= SMBOWFencrypt_ntv2(ntlm_v2_hash
,
542 if (!NT_STATUS_IS_OK(status
)) {
543 data_blob_free(&lmv2_client_data
);
544 return data_blob(NULL
, 0);
546 memcpy(final_response
.data
, lmv2_response
, sizeof(lmv2_response
));
548 /* after the first 16 bytes is the random data we generated above,
549 so the server can verify us with it */
550 memcpy(final_response
.data
+sizeof(lmv2_response
),
551 lmv2_client_data
.data
, lmv2_client_data
.length
);
553 data_blob_free(&lmv2_client_data
);
555 return final_response
;
558 bool SMBNTLMv2encrypt_hash(TALLOC_CTX
*mem_ctx
,
559 const char *user
, const char *domain
, const uint8_t nt_hash
[16],
560 const DATA_BLOB
*server_chal
,
561 const NTTIME
*server_timestamp
,
562 const DATA_BLOB
*names_blob
,
563 DATA_BLOB
*lm_response
, DATA_BLOB
*nt_response
,
564 DATA_BLOB
*lm_session_key
, DATA_BLOB
*user_session_key
)
566 uint8_t ntlm_v2_hash
[16];
569 /* We don't use the NT# directly. Instead we use it mashed up with
570 the username and domain.
571 This prevents username swapping during the auth exchange
573 if (!ntv2_owf_gen(nt_hash
, user
, domain
, ntlm_v2_hash
)) {
578 const NTTIME
*nttime
= server_timestamp
;
581 if (nttime
== NULL
) {
582 struct timeval tv_now
= timeval_current();
583 _now
= timeval_to_nttime(&tv_now
);
587 *nt_response
= NTLMv2_generate_response(mem_ctx
,
592 if (user_session_key
) {
593 *user_session_key
= data_blob_talloc(mem_ctx
, NULL
, 16);
595 /* The NTLMv2 calculations also provide a session key, for signing etc later */
596 /* use only the first 16 bytes of nt_response for session key */
597 status
= SMBsesskeygen_ntv2(ntlm_v2_hash
,
599 user_session_key
->data
);
600 if (!NT_STATUS_IS_OK(status
)) {
609 if (server_timestamp
!= NULL
) {
610 *lm_response
= data_blob_talloc_zero(mem_ctx
, 24);
612 *lm_response
= LMv2_generate_response(mem_ctx
,
616 if (lm_session_key
) {
617 *lm_session_key
= data_blob_talloc(mem_ctx
, NULL
, 16);
619 /* The NTLMv2 calculations also provide a session key, for signing etc later */
620 /* use only the first 16 bytes of lm_response for session key */
621 status
= SMBsesskeygen_ntv2(ntlm_v2_hash
,
623 lm_session_key
->data
);
624 if (!NT_STATUS_IS_OK(status
)) {
633 bool SMBNTLMv2encrypt(TALLOC_CTX
*mem_ctx
,
634 const char *user
, const char *domain
,
635 const char *password
,
636 const DATA_BLOB
*server_chal
,
637 const DATA_BLOB
*names_blob
,
638 DATA_BLOB
*lm_response
, DATA_BLOB
*nt_response
,
639 DATA_BLOB
*lm_session_key
, DATA_BLOB
*user_session_key
)
642 E_md4hash(password
, nt_hash
);
644 return SMBNTLMv2encrypt_hash(mem_ctx
,
645 user
, domain
, nt_hash
,
646 server_chal
, NULL
, names_blob
,
647 lm_response
, nt_response
, lm_session_key
, user_session_key
);
650 NTSTATUS
NTLMv2_RESPONSE_verify_netlogon_creds(const char *account_name
,
651 const char *account_domain
,
652 const DATA_BLOB response
,
653 const struct netlogon_creds_CredentialState
*creds
,
654 const char *workgroup
)
656 TALLOC_CTX
*frame
= NULL
;
657 /* RespType + HiRespType */
658 static const char *magic
= "\x01\x01";
660 struct NTLMv2_RESPONSE v2_resp
;
661 enum ndr_err_code err
;
662 const struct AV_PAIR
*av_nb_cn
= NULL
;
663 const struct AV_PAIR
*av_nb_dn
= NULL
;
665 if (response
.length
< 48) {
667 * NTLMv2_RESPONSE has at least 48 bytes.
672 cmp
= memcmp(response
.data
+ 16, magic
, 2);
675 * It doesn't look like a valid NTLMv2_RESPONSE
680 if (response
.length
== 95) {
682 * ndr_pull_NTLMv2_RESPONSE() fails on this strange blob,
683 * because the AvPairs content is not valid
684 * as AvLen of the first pair is 33032 (0x8108).
686 * I saw a single machine sending the following 3 times
687 * in a row, but I'm not sure if everything is static.
689 * Note this is NTLMv2_CLIENT_CHALLENGE only, not
690 * the full NTLMv2_RESPONSE (which has Response of 16 bytes
691 * before the NTLMv2_CLIENT_CHALLENGE).
693 * Note this code only prevents
694 * ndr_pull_error(Buffer Size Error): Pull bytes 39016
695 * debug message for a known case, the actual
696 * bug is also handled below in a generic way to
697 * map NT_STATUS_BUFFER_TOO_SMALL to NT_STATUS_OK.
699 * See https://bugzilla.samba.org/show_bug.cgi?id=14932
701 static const char *netapp_magic
=
702 "\x01\x01\x00\x00\x00\x00\x00\x00"
703 "\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f"
704 "\xb8\x82\x3a\xf1\xb3\xdd\x08\x15"
705 "\x00\x00\x00\x00\x11\xa2\x08\x81"
706 "\x50\x38\x22\x78\x2b\x94\x47\xfe"
707 "\x54\x94\x7b\xff\x17\x27\x5a\xb4"
708 "\xf4\x18\xba\xdc\x2c\x38\xfd\x5b"
709 "\xfb\x0e\xc1\x85\x1e\xcc\x92\xbb"
710 "\x9b\xb1\xc4\xd5\x53\x14\xff\x8c"
711 "\x76\x49\xf5\x45\x90\x19\xa2";
713 * First we check the initial bytes
714 * and the 0x3F timestamp.
716 cmp
= memcmp(response
.data
+ 16,
721 * Then check everything after the
724 cmp
= memcmp(response
.data
+ 40,
726 response
.length
- 40);
728 DBG_DEBUG("Invalid NETAPP NTLMv2_RESPONSE "
729 "for user[%s\\%s] against "
730 "SEC_CHAN(%u)[%s/%s] "
731 "in workgroup[%s]\n",
734 creds
->secure_channel_type
,
735 creds
->computer_name
,
743 frame
= talloc_stackframe();
745 err
= ndr_pull_struct_blob(&response
, frame
, &v2_resp
,
746 (ndr_pull_flags_fn_t
)ndr_pull_NTLMv2_RESPONSE
);
747 if (!NDR_ERR_CODE_IS_SUCCESS(err
)) {
749 status
= ndr_map_error2ntstatus(err
);
750 if (NT_STATUS_EQUAL(status
, NT_STATUS_BUFFER_TOO_SMALL
)) {
752 * We are supposed to ignore invalid buffers,
753 * see https://bugzilla.samba.org/show_bug.cgi?id=14932
755 status
= NT_STATUS_OK
;
757 DEBUG(2,("%s: Failed to parse NTLMv2_RESPONSE length=%u "
758 "for user[%s\\%s] against SEC_CHAN(%u)[%s/%s] "
759 "in workgroup[%s] - %s %s %s\n",
761 (unsigned)response
.length
,
764 creds
->secure_channel_type
,
765 creds
->computer_name
,
768 ndr_map_error2string(err
),
769 NT_STATUS_IS_OK(status
) ? "(ignoring) =>" : "=>",
771 dump_data(2, response
.data
, response
.length
);
777 NDR_PRINT_DEBUG(NTLMv2_RESPONSE
, &v2_resp
);
781 * Make sure the netbios computer name in the
782 * NTLMv2_RESPONSE matches the computer name
783 * in the secure channel credentials for workstation
786 * And the netbios domain name matches our
789 * This prevents workstations from requesting
790 * the session key of NTLMSSP sessions of clients
793 if (creds
->secure_channel_type
== SEC_CHAN_WKSTA
) {
794 av_nb_cn
= ndr_ntlmssp_find_av(&v2_resp
.Challenge
.AvPairs
,
795 MsvAvNbComputerName
);
796 av_nb_dn
= ndr_ntlmssp_find_av(&v2_resp
.Challenge
.AvPairs
,
800 if (av_nb_cn
!= NULL
) {
801 const char *v
= NULL
;
805 v
= av_nb_cn
->Value
.AvNbComputerName
;
807 a
= talloc_strdup(frame
, creds
->account_name
);
810 return NT_STATUS_NO_MEMORY
;
813 if (len
> 0 && a
[len
- 1] == '$') {
817 cmp
= strcasecmp_m(a
, v
);
819 DEBUG(2,("%s: NTLMv2_RESPONSE with "
820 "NbComputerName[%s] rejected "
822 "against SEC_CHAN_WKSTA[%s/%s] "
823 "in workgroup[%s]\n",
827 creds
->computer_name
,
831 return NT_STATUS_LOGON_FAILURE
;
834 if (av_nb_dn
!= NULL
) {
835 const char *v
= NULL
;
837 v
= av_nb_dn
->Value
.AvNbDomainName
;
839 cmp
= strcasecmp_m(workgroup
, v
);
841 DEBUG(2,("%s: NTLMv2_RESPONSE with "
842 "NbDomainName[%s] rejected "
844 "against SEC_CHAN_WKSTA[%s/%s] "
845 "in workgroup[%s]\n",
849 creds
->computer_name
,
853 return NT_STATUS_LOGON_FAILURE
;
862 ENCODE_ORDER_PASSWORD_FIRST
,
863 ENCODE_ORDER_PASSWORD_LAST
,
866 #define PASSWORD_BUFFER_LEN 512
868 static ssize_t
_encode_pwd_buffer_from_str(uint8_t buf
[PASSWORD_BUFFER_LEN
],
869 const char *password
,
871 enum encode_order order
)
875 size_t random_pos
= 0;
876 size_t random_len
= 0;
878 /* The incoming buffer can be any alignment. */
879 string_flags
|= STR_NOALIGN
;
881 new_pw_len
= push_string(buf
,
885 if (new_pw_len
< 0) {
886 BURN_DATA_SIZE(buf
, PASSWORD_BUFFER_LEN
);
890 if (new_pw_len
== PASSWORD_BUFFER_LEN
) {
895 case ENCODE_ORDER_PASSWORD_FIRST
:
897 random_pos
= new_pw_len
;
898 random_len
= PASSWORD_BUFFER_LEN
- random_pos
;
900 case ENCODE_ORDER_PASSWORD_LAST
:
901 pw_pos
= PASSWORD_BUFFER_LEN
- new_pw_len
;
904 memmove(buf
+ pw_pos
, buf
, new_pw_len
);
908 generate_random_buffer(buf
+ random_pos
, random_len
);
913 /***********************************************************
914 encode a password buffer with a unicode password. The buffer
915 is filled with random data to make it harder to attack.
916 ************************************************************/
917 bool encode_pw_buffer(uint8_t buffer
[516], const char *password
, int string_flags
)
921 pw_len
= _encode_pwd_buffer_from_str(buffer
,
924 ENCODE_ORDER_PASSWORD_LAST
);
925 if (pw_len
< 0 || pw_len
> PASSWORD_BUFFER_LEN
) {
929 PUSH_LE_U32(buffer
, PASSWORD_BUFFER_LEN
, pw_len
);
935 /***********************************************************
936 decode a password buffer
937 *new_pw_len is the length in bytes of the possibly mulitbyte
938 returned password including termination.
939 ************************************************************/
941 bool decode_pw_buffer(TALLOC_CTX
*ctx
,
942 uint8_t in_buffer
[516],
945 charset_t string_charset
)
947 DATA_BLOB new_password
;
954 ok
= extract_pw_from_buffer(ctx
, in_buffer
, &new_password
);
960 Warning !!! : This function is called from some rpc call.
961 The password IN the buffer may be a UNICODE string.
962 The password IN new_pwrd is an ASCII string
963 If you reuse that code somewhere else check first.
966 /* decode into the return buffer. */
967 ok
= convert_string_talloc(ctx
,
974 data_blob_free(&new_password
);
976 DBG_ERR("Failed to convert incoming password\n");
979 talloc_keep_secret(*pp_new_pwrd
);
981 #ifdef DEBUG_PASSWORD
982 DEBUG(100,("decode_pw_buffer: new_pwrd: "));
983 dump_data(100, (uint8_t *)*pp_new_pwrd
, *new_pw_len
);
984 DEBUG(100,("multibyte len:%lu\n", (unsigned long int)*new_pw_len
));
985 DEBUG(100,("original char len:%d\n", byte_len
/2));
991 #define MAX_PASSWORD_LEN 256
994 * [MS-SAMR] 2.2.6.32 This creates the buffer to be sent. It is of type
995 * SAMPR_USER_PASSWORD_AES.
997 bool encode_pwd_buffer514_from_str(uint8_t buffer
[514],
998 const char *password
,
999 uint32_t string_flags
)
1003 pw_len
= _encode_pwd_buffer_from_str(buffer
+ 2,
1006 ENCODE_ORDER_PASSWORD_FIRST
);
1011 PUSH_LE_U16(buffer
, 0, pw_len
);
1016 bool extract_pwd_blob_from_buffer514(TALLOC_CTX
*mem_ctx
,
1017 const uint8_t in_buffer
[514],
1018 DATA_BLOB
*new_password
)
1020 #ifdef DEBUG_PASSWORD
1021 DEBUG(100, ("in_buffer: "));
1022 dump_data(100, in_buffer
, 514);
1025 new_password
->length
= PULL_LE_U16(in_buffer
, 0);
1026 if (new_password
->length
== 0 || new_password
->length
> 512) {
1030 new_password
->data
=
1031 talloc_memdup(mem_ctx
, in_buffer
+ 2, new_password
->length
);
1032 if (new_password
->data
== NULL
) {
1035 talloc_keep_secret(new_password
->data
);
1037 #ifdef DEBUG_PASSWORD
1038 DEBUG(100, ("new_pwd_len: %zu\n", new_password
->length
));
1039 DEBUG(100, ("new_pwd: "));
1040 dump_data(100, new_password
->data
, new_password
->length
);
1046 bool decode_pwd_string_from_buffer514(TALLOC_CTX
*mem_ctx
,
1047 const uint8_t in_buffer
[514],
1048 charset_t string_charset
,
1049 DATA_BLOB
*decoded_password
)
1051 DATA_BLOB new_password
= {
1056 ok
= extract_pwd_blob_from_buffer514(mem_ctx
, in_buffer
, &new_password
);
1061 ok
= convert_string_talloc(mem_ctx
,
1065 new_password
.length
,
1066 &decoded_password
->data
,
1067 &decoded_password
->length
);
1068 data_blob_free(&new_password
);
1072 talloc_keep_secret(decoded_password
->data
);
1077 /***********************************************************
1078 Encode an arc4 password change buffer.
1079 ************************************************************/
1080 NTSTATUS
encode_rc4_passwd_buffer(const char *passwd
,
1081 const DATA_BLOB
*session_key
,
1082 struct samr_CryptPasswordEx
*out_crypt_pwd
)
1084 uint8_t _confounder
[16] = {0};
1085 DATA_BLOB confounder
= data_blob_const(_confounder
, 16);
1086 DATA_BLOB pw_data
= data_blob_const(out_crypt_pwd
->data
, 516);
1090 ok
= encode_pw_buffer(pw_data
.data
, passwd
, STR_UNICODE
);
1092 return NT_STATUS_INVALID_PARAMETER
;
1095 generate_random_buffer(confounder
.data
, confounder
.length
);
1097 rc
= samba_gnutls_arcfour_confounded_md5(&confounder
,
1100 SAMBA_GNUTLS_ENCRYPT
);
1102 ZERO_ARRAY(_confounder
);
1103 data_blob_clear(&pw_data
);
1104 return gnutls_error_to_ntstatus(rc
, NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER
);
1108 * The packet format is the 516 byte RC4 encrypted
1109 * password followed by the 16 byte confounder
1110 * The confounder is a salt to prevent pre-computed hash attacks on the
1113 memcpy(&out_crypt_pwd
->data
[516], confounder
.data
, confounder
.length
);
1114 ZERO_ARRAY(_confounder
);
1116 return NT_STATUS_OK
;
1119 /***********************************************************
1120 Decode an arc4 encrypted password change buffer.
1121 ************************************************************/
1123 NTSTATUS
decode_rc4_passwd_buffer(const DATA_BLOB
*psession_key
,
1124 struct samr_CryptPasswordEx
*inout_crypt_pwd
)
1126 /* Confounder is last 16 bytes. */
1127 DATA_BLOB confounder
= data_blob_const(&inout_crypt_pwd
->data
[516], 16);
1128 DATA_BLOB pw_data
= data_blob_const(&inout_crypt_pwd
->data
, 516);
1131 rc
= samba_gnutls_arcfour_confounded_md5(&confounder
,
1134 SAMBA_GNUTLS_DECRYPT
);
1136 return gnutls_error_to_ntstatus(rc
, NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER
);
1139 return NT_STATUS_OK
;
1142 /***********************************************************
1143 encode a password buffer with an already unicode password. The
1144 rest of the buffer is filled with random data to make it harder to attack.
1145 ************************************************************/
1147 static bool create_pw_buffer_from_blob(uint8_t buffer
[512],
1148 const DATA_BLOB
*in_password
,
1149 enum encode_order order
)
1152 size_t random_pos
= 0;
1153 size_t random_len
= 0;
1155 if (in_password
->length
> 512) {
1160 case ENCODE_ORDER_PASSWORD_FIRST
:
1162 random_pos
= in_password
->length
;
1164 case ENCODE_ORDER_PASSWORD_LAST
:
1165 pwd_pos
= PASSWORD_BUFFER_LEN
- in_password
->length
;
1169 random_len
= PASSWORD_BUFFER_LEN
- in_password
->length
;
1171 memcpy(buffer
+ pwd_pos
, in_password
->data
, in_password
->length
);
1172 generate_random_buffer(buffer
+ random_pos
, random_len
);
1177 bool set_pw_in_buffer(uint8_t buffer
[516], const DATA_BLOB
*password
)
1181 ok
= create_pw_buffer_from_blob(buffer
,
1183 ENCODE_ORDER_PASSWORD_LAST
);
1189 * The length of the new password is in the last 4 bytes of
1192 PUSH_LE_U32(buffer
, PASSWORD_BUFFER_LEN
, password
->length
);
1197 /***********************************************************
1198 decode a password buffer
1199 *new_pw_size is the length in bytes of the extracted unicode password
1200 ************************************************************/
1201 bool extract_pw_from_buffer(TALLOC_CTX
*mem_ctx
,
1202 uint8_t in_buffer
[516], DATA_BLOB
*new_pass
)
1206 /* The length of the new password is in the last 4 bytes of the data buffer. */
1208 byte_len
= IVAL(in_buffer
, 512);
1210 #ifdef DEBUG_PASSWORD
1211 dump_data(100, in_buffer
, 516);
1214 /* Password cannot be longer than the size of the password buffer */
1215 if ( (byte_len
< 0) || (byte_len
> 512)) {
1219 *new_pass
= data_blob_talloc(mem_ctx
, &in_buffer
[512 - byte_len
], byte_len
);
1221 if (!new_pass
->data
) {
1224 talloc_keep_secret(new_pass
->data
);
1230 /* encode a wkssvc_PasswordBuffer:
1232 * similar to samr_CryptPasswordEx. Different: 8byte confounder (instead of
1233 * 16byte), confounder in front of the 516 byte buffer (instead of after that
1234 * buffer), calling MD5Update() first with session_key and then with confounder
1235 * (vice versa in samr) - Guenther */
1237 WERROR
encode_wkssvc_join_password_buffer(TALLOC_CTX
*mem_ctx
,
1239 DATA_BLOB
*session_key
,
1240 struct wkssvc_PasswordBuffer
**out_pwd_buf
)
1242 struct wkssvc_PasswordBuffer
*pwd_buf
= NULL
;
1243 uint8_t _confounder
[8] = {0};
1244 DATA_BLOB confounder
= data_blob_const(_confounder
, 8);
1245 uint8_t pwbuf
[516] = {0};
1246 DATA_BLOB encrypt_pwbuf
= data_blob_const(pwbuf
, 516);
1249 pwd_buf
= talloc_zero(mem_ctx
, struct wkssvc_PasswordBuffer
);
1250 if (pwd_buf
== NULL
) {
1251 return WERR_NOT_ENOUGH_MEMORY
;
1254 encode_pw_buffer(pwbuf
, pwd
, STR_UNICODE
);
1256 generate_random_buffer(_confounder
, sizeof(_confounder
));
1258 rc
= samba_gnutls_arcfour_confounded_md5(session_key
,
1261 SAMBA_GNUTLS_ENCRYPT
);
1263 ZERO_ARRAY(_confounder
);
1264 TALLOC_FREE(pwd_buf
);
1265 return gnutls_error_to_werror(rc
, WERR_CONTENT_BLOCKED
);
1268 memcpy(&pwd_buf
->data
[0], confounder
.data
, confounder
.length
);
1269 ZERO_ARRAY(_confounder
);
1270 memcpy(&pwd_buf
->data
[8], encrypt_pwbuf
.data
, encrypt_pwbuf
.length
);
1273 *out_pwd_buf
= pwd_buf
;
1278 WERROR
decode_wkssvc_join_password_buffer(TALLOC_CTX
*mem_ctx
,
1279 struct wkssvc_PasswordBuffer
*pwd_buf
,
1280 DATA_BLOB
*session_key
,
1283 uint8_t _confounder
[8] = { 0 };
1284 DATA_BLOB confounder
= data_blob_const(_confounder
, 8);
1285 uint8_t pwbuf
[516] = {0};
1286 DATA_BLOB decrypt_pwbuf
= data_blob_const(pwbuf
, 516);
1290 if (pwd_buf
== NULL
) {
1291 return WERR_INVALID_PASSWORD
;
1296 if (session_key
->length
!= 16) {
1297 DEBUG(10,("invalid session key\n"));
1298 return WERR_INVALID_PASSWORD
;
1301 confounder
= data_blob_const(&pwd_buf
->data
[0], 8);
1302 memcpy(&pwbuf
, &pwd_buf
->data
[8], 516);
1304 rc
= samba_gnutls_arcfour_confounded_md5(session_key
,
1307 SAMBA_GNUTLS_ENCRYPT
);
1309 ZERO_ARRAY(_confounder
);
1310 TALLOC_FREE(pwd_buf
);
1311 return gnutls_error_to_werror(rc
, WERR_CONTENT_BLOCKED
);
1314 ok
= decode_pw_buffer(mem_ctx
,
1317 &decrypt_pwbuf
.length
,
1322 return WERR_INVALID_PASSWORD
;