1996-05-04 11:50:46 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1996-05-04 11:50:46 +04:00
SMB parameters and setup
1998-01-22 16:27:43 +03:00
Copyright ( C ) Andrew Tridgell 1992 - 1998
1996-05-04 11:50:46 +04:00
Modified by Jeremy Allison 1995.
2001-07-07 11:00:15 +04:00
Copyright ( C ) Jeremy Allison 1995 - 2000.
Copyright ( C ) Luke Kennethc Casson Leighton 1996 - 2000.
2003-02-24 05:55:00 +03:00
Copyright ( C ) Andrew Bartlett < abartlet @ samba . org > 2002 - 2003
1996-05-04 11:50:46 +04:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 2 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "includes.h"
1999-12-13 16:27:58 +03:00
# include "byteorder.h"
1996-05-04 11:50:46 +04:00
/*
This implements the X / Open SMB password encryption
2002-07-15 14:35:28 +04:00
It takes a password ( ' unix ' string ) , a 8 byte " crypt key "
and puts 24 bytes of encrypted password into p24 */
2002-08-17 21:00:51 +04:00
void SMBencrypt ( const char * passwd , const uchar * c8 , uchar p24 [ 24 ] )
1996-05-04 11:50:46 +04:00
{
2002-07-15 14:35:28 +04:00
uchar p21 [ 21 ] ;
1996-05-04 11:50:46 +04:00
1999-12-13 16:27:58 +03:00
memset ( p21 , ' \0 ' , 21 ) ;
2002-07-15 14:35:28 +04:00
E_deshash ( passwd , p21 ) ;
1998-10-02 22:45:07 +04:00
SMBOWFencrypt ( p21 , c8 , p24 ) ;
1996-05-04 11:50:46 +04:00
1998-10-02 22:45:07 +04:00
# ifdef DEBUG_PASSWORD
1999-12-13 16:27:58 +03:00
DEBUG ( 100 , ( " SMBencrypt: lm#, challenge, response \n " ) ) ;
dump_data ( 100 , ( char * ) p21 , 16 ) ;
This commit is number 4 of 4.
In particular this commit focuses on:
Actually adding the 'const' to the passdb interface, and the flow-on changes.
Also kill off the 'disp_info' stuff, as its no longer used.
While these changes have been mildly tested, and are pretty small, any
assistance in this is appreciated.
----
These changes introduces a large dose of 'const' to the Samba tree.
There are a number of good reasons to do this:
- I want to allow the SAM_ACCOUNT structure to move from wasteful
pstrings and fstrings to allocated strings. We can't do that if
people are modifying these outputs, as they may well make
assumptions about getting pstrings and fstrings
- I want --with-pam_smbpass to compile with a slightly sane
volume of warnings, currently its pretty bad, even in 2.2
where is compiles at all.
- Tridge assures me that he no longer opposes 'const religion'
based on the ability to #define const the problem away.
- Changed Get_Pwnam(x,y) into two variants (so that the const
parameter can work correctly): - Get_Pwnam(const x) and
Get_Pwnam_Modify(x).
- Reworked smbd/chgpasswd.c to work with these mods, passing
around a 'struct passwd' rather than the modified username
---
This finishes this line of commits off, your tree should now compile again :-)
Andrew Bartlett
(This used to be commit c95f5aeb9327347674589ae313b75bee3bf8e317)
2001-10-29 10:35:11 +03:00
dump_data ( 100 , ( const char * ) c8 , 8 ) ;
1999-12-13 16:27:58 +03:00
dump_data ( 100 , ( char * ) p24 , 24 ) ;
1998-10-02 22:45:07 +04:00
# endif
1996-05-04 11:50:46 +04:00
}
2002-07-15 14:35:28 +04:00
/**
1996-05-04 11:50:46 +04:00
* Creates the MD4 Hash of the users password in NT UNICODE .
2002-07-15 14:35:28 +04:00
* @ param passwd password in ' unix ' charset .
* @ param p16 return password hashed with md4 , caller allocated 16 byte buffer
1996-05-04 11:50:46 +04:00
*/
2002-07-15 14:35:28 +04:00
void E_md4hash ( const char * passwd , uchar p16 [ 16 ] )
1996-05-04 11:50:46 +04:00
{
1997-09-15 06:49:38 +04:00
int len ;
2001-07-31 00:25:35 +04:00
smb_ucs2_t wpwd [ 129 ] ;
1997-09-15 06:49:38 +04:00
2001-07-31 00:25:35 +04:00
/* Password must be converted to NT unicode - null terminated. */
2001-08-25 00:32:01 +04:00
push_ucs2 ( NULL , wpwd , ( const char * ) passwd , 256 , STR_UNICODE | STR_NOALIGN | STR_TERMINATE ) ;
1996-05-04 11:50:46 +04:00
/* Calculate length in bytes */
2001-07-31 00:25:35 +04:00
len = strlen_w ( wpwd ) * sizeof ( int16 ) ;
1997-09-15 06:49:38 +04:00
mdfour ( p16 , ( unsigned char * ) wpwd , len ) ;
2002-07-15 14:35:28 +04:00
ZERO_STRUCT ( wpwd ) ;
1996-05-04 11:50:46 +04:00
}
2002-07-15 14:35:28 +04:00
/**
2002-08-17 21:00:51 +04:00
* Creates the DES forward - only Hash of the users password in DOS ASCII charset
2002-07-15 14:35:28 +04:00
* @ param passwd password in ' unix ' charset .
2002-08-17 21:00:51 +04:00
* @ param p16 return password hashed with DES , caller allocated 16 byte buffer
2002-07-15 14:35:28 +04:00
*/
void E_deshash ( const char * passwd , uchar p16 [ 16 ] )
1999-11-19 04:37:16 +03:00
{
2003-01-14 11:26:54 +03:00
fstring dospwd ;
2002-07-15 14:35:28 +04:00
ZERO_STRUCT ( dospwd ) ;
2002-08-17 21:00:51 +04:00
/* Password must be converted to DOS charset - null terminated, uppercase. */
2003-05-05 09:15:54 +04:00
push_ascii ( dospwd , passwd , sizeof ( dospwd ) , STR_UPPER | STR_TERMINATE ) ;
1999-11-19 04:37:16 +03:00
2003-01-14 11:26:54 +03:00
/* Only the fisrt 14 chars are considered, password need not be null terminated. */
2002-07-15 14:35:28 +04:00
E_P16 ( dospwd , p16 ) ;
ZERO_STRUCT ( dospwd ) ;
}
1999-11-19 04:37:16 +03:00
2002-07-15 14:35:28 +04:00
/**
* Creates the MD4 and DES ( LM ) Hash of the users password .
* MD4 is of the NT Unicode , DES is of the DOS UPPERCASE password .
* @ param passwd password in ' unix ' charset .
* @ param nt_p16 return password hashed with md4 , caller allocated 16 byte buffer
* @ param p16 return password hashed with des , caller allocated 16 byte buffer
*/
/* Does both the NT and LM owfs of a user's password */
void nt_lm_owf_gen ( const char * pwd , uchar nt_p16 [ 16 ] , uchar p16 [ 16 ] )
{
1999-12-13 16:27:58 +03:00
/* Calculate the MD4 hash (NT compatible) of the password */
memset ( nt_p16 , ' \0 ' , 16 ) ;
2002-07-15 14:35:28 +04:00
E_md4hash ( pwd , nt_p16 ) ;
1999-11-19 04:37:16 +03:00
# ifdef DEBUG_PASSWORD
1999-12-13 16:27:58 +03:00
DEBUG ( 100 , ( " nt_lm_owf_gen: pwd, nt# \n " ) ) ;
2002-07-15 14:35:28 +04:00
dump_data ( 120 , pwd , strlen ( pwd ) ) ;
1999-12-13 16:27:58 +03:00
dump_data ( 100 , ( char * ) nt_p16 , 16 ) ;
1999-11-19 04:37:16 +03:00
# endif
1998-09-26 01:01:52 +04:00
2002-07-15 14:35:28 +04:00
E_deshash ( pwd , ( uchar * ) p16 ) ;
1998-09-26 01:01:52 +04:00
1998-10-02 22:45:07 +04:00
# ifdef DEBUG_PASSWORD
DEBUG ( 100 , ( " nt_lm_owf_gen: pwd, lm# \n " ) ) ;
2002-07-15 14:35:28 +04:00
dump_data ( 120 , pwd , strlen ( pwd ) ) ;
1999-12-13 16:27:58 +03:00
dump_data ( 100 , ( char * ) p16 , 16 ) ;
1999-06-29 22:47:06 +04:00
# endif
}
2001-07-07 11:00:15 +04:00
/* Does both the NTLMv2 owfs of a user's password */
2002-09-25 19:19:00 +04:00
BOOL ntv2_owf_gen ( const uchar owf [ 16 ] ,
const char * user_in , const char * domain_in , uchar kr_buf [ 16 ] )
2001-07-07 11:00:15 +04:00
{
2002-09-25 19:19:00 +04:00
smb_ucs2_t * user ;
smb_ucs2_t * domain ;
2003-01-14 11:26:54 +03:00
size_t user_byte_len ;
size_t domain_byte_len ;
2002-09-25 19:19:00 +04:00
2001-07-07 11:00:15 +04:00
HMACMD5Context ctx ;
2002-09-25 19:19:00 +04:00
user_byte_len = push_ucs2_allocate ( & user , user_in ) ;
2003-01-14 11:26:54 +03:00
if ( user_byte_len = = ( size_t ) - 1 ) {
DEBUG ( 0 , ( " push_uss2_allocate() for user returned -1 (probably malloc() failure) \n " ) ) ;
2002-09-25 19:19:00 +04:00
return False ;
}
2001-07-07 11:00:15 +04:00
2002-09-25 19:19:00 +04:00
domain_byte_len = push_ucs2_allocate ( & domain , domain_in ) ;
2003-01-14 11:26:54 +03:00
if ( domain_byte_len = = ( size_t ) - 1 ) {
DEBUG ( 0 , ( " push_uss2_allocate() for domain returned -1 (probably malloc() failure) \n " ) ) ;
2002-09-25 19:19:00 +04:00
return False ;
}
strupper_w ( user ) ;
strupper_w ( domain ) ;
2003-01-14 11:26:54 +03:00
SMB_ASSERT ( user_byte_len > = 2 ) ;
SMB_ASSERT ( domain_byte_len > = 2 ) ;
2002-09-25 19:19:00 +04:00
/* We don't want null termination */
user_byte_len = user_byte_len - 2 ;
domain_byte_len = domain_byte_len - 2 ;
2001-07-07 11:00:15 +04:00
hmac_md5_init_limK_to_64 ( owf , 16 , & ctx ) ;
2002-09-25 19:19:00 +04:00
hmac_md5_update ( ( const unsigned char * ) user , user_byte_len , & ctx ) ;
hmac_md5_update ( ( const unsigned char * ) domain , domain_byte_len , & ctx ) ;
2001-07-07 11:00:15 +04:00
hmac_md5_final ( kr_buf , & ctx ) ;
# ifdef DEBUG_PASSWORD
DEBUG ( 100 , ( " ntv2_owf_gen: user, domain, owfkey, kr \n " ) ) ;
2002-09-25 19:19:00 +04:00
dump_data ( 100 , ( const char * ) user , user_byte_len ) ;
dump_data ( 100 , ( const char * ) domain , domain_byte_len ) ;
2001-07-07 11:00:15 +04:00
dump_data ( 100 , owf , 16 ) ;
dump_data ( 100 , kr_buf , 16 ) ;
# endif
2002-09-25 19:19:00 +04:00
SAFE_FREE ( user ) ;
SAFE_FREE ( domain ) ;
return True ;
2001-07-07 11:00:15 +04:00
}
1998-09-26 01:01:52 +04:00
/* Does the des encryption from the NT or LM MD4 hash. */
2001-07-07 11:00:15 +04:00
void SMBOWFencrypt ( const uchar passwd [ 16 ] , const uchar * c8 , uchar p24 [ 24 ] )
1998-09-26 01:01:52 +04:00
{
uchar p21 [ 21 ] ;
2002-09-25 19:19:00 +04:00
ZERO_STRUCT ( p21 ) ;
1998-09-26 01:01:52 +04:00
1999-12-13 16:27:58 +03:00
memcpy ( p21 , passwd , 16 ) ;
1998-09-26 01:01:52 +04:00
E_P24 ( p21 , c8 , p24 ) ;
}
1999-12-13 16:27:58 +03:00
/* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
This commit is number 4 of 4.
In particular this commit focuses on:
Actually adding the 'const' to the passdb interface, and the flow-on changes.
Also kill off the 'disp_info' stuff, as its no longer used.
While these changes have been mildly tested, and are pretty small, any
assistance in this is appreciated.
----
These changes introduces a large dose of 'const' to the Samba tree.
There are a number of good reasons to do this:
- I want to allow the SAM_ACCOUNT structure to move from wasteful
pstrings and fstrings to allocated strings. We can't do that if
people are modifying these outputs, as they may well make
assumptions about getting pstrings and fstrings
- I want --with-pam_smbpass to compile with a slightly sane
volume of warnings, currently its pretty bad, even in 2.2
where is compiles at all.
- Tridge assures me that he no longer opposes 'const religion'
based on the ability to #define const the problem away.
- Changed Get_Pwnam(x,y) into two variants (so that the const
parameter can work correctly): - Get_Pwnam(const x) and
Get_Pwnam_Modify(x).
- Reworked smbd/chgpasswd.c to work with these mods, passing
around a 'struct passwd' rather than the modified username
---
This finishes this line of commits off, your tree should now compile again :-)
Andrew Bartlett
(This used to be commit c95f5aeb9327347674589ae313b75bee3bf8e317)
2001-10-29 10:35:11 +03:00
void NTLMSSPOWFencrypt ( const uchar passwd [ 8 ] , const uchar * ntlmchalresp , uchar p24 [ 24 ] )
1999-11-21 22:59:56 +03:00
{
1999-12-13 16:27:58 +03:00
uchar p21 [ 21 ] ;
memset ( p21 , ' \0 ' , 21 ) ;
memcpy ( p21 , passwd , 8 ) ;
memset ( p21 + 8 , 0xbd , 8 ) ;
1999-11-21 22:59:56 +03:00
1999-12-13 16:27:58 +03:00
E_P24 ( p21 , ntlmchalresp , p24 ) ;
1999-11-21 22:59:56 +03:00
# ifdef DEBUG_PASSWORD
1999-12-13 16:27:58 +03:00
DEBUG ( 100 , ( " NTLMSSPOWFencrypt: p21, c8, p24 \n " ) ) ;
dump_data ( 100 , ( char * ) p21 , 21 ) ;
This commit is number 4 of 4.
In particular this commit focuses on:
Actually adding the 'const' to the passdb interface, and the flow-on changes.
Also kill off the 'disp_info' stuff, as its no longer used.
While these changes have been mildly tested, and are pretty small, any
assistance in this is appreciated.
----
These changes introduces a large dose of 'const' to the Samba tree.
There are a number of good reasons to do this:
- I want to allow the SAM_ACCOUNT structure to move from wasteful
pstrings and fstrings to allocated strings. We can't do that if
people are modifying these outputs, as they may well make
assumptions about getting pstrings and fstrings
- I want --with-pam_smbpass to compile with a slightly sane
volume of warnings, currently its pretty bad, even in 2.2
where is compiles at all.
- Tridge assures me that he no longer opposes 'const religion'
based on the ability to #define const the problem away.
- Changed Get_Pwnam(x,y) into two variants (so that the const
parameter can work correctly): - Get_Pwnam(const x) and
Get_Pwnam_Modify(x).
- Reworked smbd/chgpasswd.c to work with these mods, passing
around a 'struct passwd' rather than the modified username
---
This finishes this line of commits off, your tree should now compile again :-)
Andrew Bartlett
(This used to be commit c95f5aeb9327347674589ae313b75bee3bf8e317)
2001-10-29 10:35:11 +03:00
dump_data ( 100 , ( const char * ) ntlmchalresp , 8 ) ;
1999-12-13 16:27:58 +03:00
dump_data ( 100 , ( char * ) p24 , 24 ) ;
1999-11-21 22:59:56 +03:00
# endif
}
1999-06-29 22:47:06 +04:00
1999-12-13 16:27:58 +03:00
/* Does the NT MD4 hash then des encryption. */
2002-08-17 21:00:51 +04:00
void SMBNTencrypt ( const char * passwd , uchar * c8 , uchar * p24 )
1998-10-08 01:42:24 +04:00
{
uchar p21 [ 21 ] ;
memset ( p21 , ' \0 ' , 21 ) ;
1999-12-13 16:27:58 +03:00
E_md4hash ( passwd , p21 ) ;
SMBOWFencrypt ( p21 , c8 , p24 ) ;
1998-10-08 01:42:24 +04:00
1998-10-09 23:05:19 +04:00
# ifdef DEBUG_PASSWORD
1999-12-13 16:27:58 +03:00
DEBUG ( 100 , ( " SMBNTencrypt: nt#, challenge, response \n " ) ) ;
dump_data ( 100 , ( char * ) p21 , 16 ) ;
dump_data ( 100 , ( char * ) c8 , 8 ) ;
dump_data ( 100 , ( char * ) p24 , 24 ) ;
1998-10-09 23:05:19 +04:00
# endif
1998-10-08 01:42:24 +04:00
}
2001-08-10 10:00:33 +04:00
BOOL make_oem_passwd_hash ( char data [ 516 ] , const char * passwd , uchar old_pw_hash [ 16 ] , BOOL unicode )
1998-10-10 03:31:50 +04:00
{
1999-12-13 16:27:58 +03:00
int new_pw_len = strlen ( passwd ) * ( unicode ? 2 : 1 ) ;
1998-10-10 03:31:50 +04:00
if ( new_pw_len > 512 )
{
1999-12-13 16:27:58 +03:00
DEBUG ( 0 , ( " make_oem_passwd_hash: new password is too long. \n " ) ) ;
1998-10-10 03:31:50 +04:00
return False ;
}
/*
* Now setup the data area .
* We need to generate a random fill
* for this area to make it harder to
* decrypt . JRA .
*/
generate_random_buffer ( ( unsigned char * ) data , 516 , False ) ;
2001-07-04 11:15:53 +04:00
push_string ( NULL , & data [ 512 - new_pw_len ] , passwd , new_pw_len ,
STR_NOALIGN | ( unicode ? STR_UNICODE : STR_ASCII ) ) ;
1998-10-10 03:31:50 +04:00
SIVAL ( data , 512 , new_pw_len ) ;
1998-10-10 04:46:28 +04:00
# ifdef DEBUG_PASSWORD
1999-12-13 16:27:58 +03:00
DEBUG ( 100 , ( " make_oem_passwd_hash \n " ) ) ;
1998-10-10 04:46:28 +04:00
dump_data ( 100 , data , 516 ) ;
# endif
2001-06-20 23:55:59 +04:00
SamOEMhash ( ( unsigned char * ) data , ( unsigned char * ) old_pw_hash , 516 ) ;
1998-10-14 11:00:00 +04:00
return True ;
1998-10-10 03:31:50 +04:00
}
2001-07-07 11:00:15 +04:00
/* Does the md5 encryption from the NT hash for NTLMv2. */
void SMBOWFencrypt_ntv2 ( const uchar kr [ 16 ] ,
2003-05-09 18:42:20 +04:00
const DATA_BLOB * srv_chal ,
const DATA_BLOB * cli_chal ,
2002-08-17 21:00:51 +04:00
uchar resp_buf [ 16 ] )
2001-07-07 11:00:15 +04:00
{
HMACMD5Context ctx ;
hmac_md5_init_limK_to_64 ( kr , 16 , & ctx ) ;
2003-05-09 18:42:20 +04:00
hmac_md5_update ( srv_chal - > data , srv_chal - > length , & ctx ) ;
hmac_md5_update ( cli_chal - > data , cli_chal - > length , & ctx ) ;
2002-08-17 21:00:51 +04:00
hmac_md5_final ( resp_buf , & ctx ) ;
2001-07-07 11:00:15 +04:00
# ifdef DEBUG_PASSWORD
DEBUG ( 100 , ( " SMBOWFencrypt_ntv2: srv_chal, cli_chal, resp_buf \n " ) ) ;
2003-05-09 18:42:20 +04:00
dump_data ( 100 , srv_chal - > data , srv_chal - > length ) ;
dump_data ( 100 , cli_chal - > data , cli_chal - > length ) ;
2001-07-07 11:00:15 +04:00
dump_data ( 100 , resp_buf , 16 ) ;
# endif
}
void SMBsesskeygen_ntv2 ( const uchar kr [ 16 ] ,
2001-10-31 13:46:25 +03:00
const uchar * nt_resp , uint8 sess_key [ 16 ] )
2001-07-07 11:00:15 +04:00
{
Jeremy requested that I get my NTLMSSP patch into CVS. He didn't request
the schannel code, but I've included that anyway. :-)
This patch revives the client-side NTLMSSP support for RPC named pipes
in Samba, and cleans up the client and server schannel code. The use of the
new code is enabled by the 'sign', 'seal' and 'schannel' commands in
rpcclient.
The aim was to prove that our separate NTLMSSP client library actually
implements NTLMSSP signing and sealing as per Microsoft's NTLMv1 implementation,
in the hope that knowing this will assist us in correctly implementing
NTLMSSP signing for SMB packets. (Still not yet functional)
This patch replaces the NTLMSSP implementation in rpc_client/cli_pipe.c with
calls to libsmb/ntlmssp.c. In the process, we have gained the ability to
use the more secure NT password, and the ability to sign-only, instead of
having to seal the pipe connection. (Previously we were limited to sealing,
and could only use the LM-password derived key).
Our new client-side NTLMSSP code also needed alteration to cope with our
comparatively simple server-side implementation. A future step is to replace
it with calls to the same NTLMSSP library.
Also included in this patch is the schannel 'sign only' patch I submitted to
the team earlier. While not enabled (and not functional, at this stage) the
work in this patch makes the code paths *much* easier to follow. I have also
included similar hooks in rpccleint to allow the use of schannel on *any* pipe.
rpcclient now defaults to not using schannel (or any other extra per-pipe
authenticiation) for any connection. The 'schannel' command enables schannel
for all pipes until disabled.
This code is also much more secure than the previous code, as changes to our
cli_pipe routines ensure that the authentication footer cannot be removed
by an attacker, and more error states are correctly handled.
(The same needs to be done to our server)
Andrew Bartlett
(This used to be commit 5472ddc9eaf4e79c5b2e1c8ee8c7f190dc285f19)
2003-07-14 12:46:32 +04:00
/* a very nice, 128 bit, variable session key */
2001-07-07 11:00:15 +04:00
HMACMD5Context ctx ;
hmac_md5_init_limK_to_64 ( kr , 16 , & ctx ) ;
hmac_md5_update ( nt_resp , 16 , & ctx ) ;
2001-08-25 00:32:01 +04:00
hmac_md5_final ( ( unsigned char * ) sess_key , & ctx ) ;
2001-07-07 11:00:15 +04:00
# ifdef DEBUG_PASSWORD
DEBUG ( 100 , ( " SMBsesskeygen_ntv2: \n " ) ) ;
dump_data ( 100 , sess_key , 16 ) ;
# endif
}
void SMBsesskeygen_ntv1 ( const uchar kr [ 16 ] ,
2001-10-31 13:46:25 +03:00
const uchar * nt_resp , uint8 sess_key [ 16 ] )
2001-07-07 11:00:15 +04:00
{
Jeremy requested that I get my NTLMSSP patch into CVS. He didn't request
the schannel code, but I've included that anyway. :-)
This patch revives the client-side NTLMSSP support for RPC named pipes
in Samba, and cleans up the client and server schannel code. The use of the
new code is enabled by the 'sign', 'seal' and 'schannel' commands in
rpcclient.
The aim was to prove that our separate NTLMSSP client library actually
implements NTLMSSP signing and sealing as per Microsoft's NTLMv1 implementation,
in the hope that knowing this will assist us in correctly implementing
NTLMSSP signing for SMB packets. (Still not yet functional)
This patch replaces the NTLMSSP implementation in rpc_client/cli_pipe.c with
calls to libsmb/ntlmssp.c. In the process, we have gained the ability to
use the more secure NT password, and the ability to sign-only, instead of
having to seal the pipe connection. (Previously we were limited to sealing,
and could only use the LM-password derived key).
Our new client-side NTLMSSP code also needed alteration to cope with our
comparatively simple server-side implementation. A future step is to replace
it with calls to the same NTLMSSP library.
Also included in this patch is the schannel 'sign only' patch I submitted to
the team earlier. While not enabled (and not functional, at this stage) the
work in this patch makes the code paths *much* easier to follow. I have also
included similar hooks in rpccleint to allow the use of schannel on *any* pipe.
rpcclient now defaults to not using schannel (or any other extra per-pipe
authenticiation) for any connection. The 'schannel' command enables schannel
for all pipes until disabled.
This code is also much more secure than the previous code, as changes to our
cli_pipe routines ensure that the authentication footer cannot be removed
by an attacker, and more error states are correctly handled.
(The same needs to be done to our server)
Andrew Bartlett
(This used to be commit 5472ddc9eaf4e79c5b2e1c8ee8c7f190dc285f19)
2003-07-14 12:46:32 +04:00
/* yes, this session key does not change - yes, this
is a problem - but it is 128 bits */
2001-08-25 00:32:01 +04:00
mdfour ( ( unsigned char * ) sess_key , kr , 16 ) ;
2001-07-07 11:00:15 +04:00
# ifdef DEBUG_PASSWORD
DEBUG ( 100 , ( " SMBsesskeygen_ntv1: \n " ) ) ;
dump_data ( 100 , sess_key , 16 ) ;
# endif
}
Jeremy requested that I get my NTLMSSP patch into CVS. He didn't request
the schannel code, but I've included that anyway. :-)
This patch revives the client-side NTLMSSP support for RPC named pipes
in Samba, and cleans up the client and server schannel code. The use of the
new code is enabled by the 'sign', 'seal' and 'schannel' commands in
rpcclient.
The aim was to prove that our separate NTLMSSP client library actually
implements NTLMSSP signing and sealing as per Microsoft's NTLMv1 implementation,
in the hope that knowing this will assist us in correctly implementing
NTLMSSP signing for SMB packets. (Still not yet functional)
This patch replaces the NTLMSSP implementation in rpc_client/cli_pipe.c with
calls to libsmb/ntlmssp.c. In the process, we have gained the ability to
use the more secure NT password, and the ability to sign-only, instead of
having to seal the pipe connection. (Previously we were limited to sealing,
and could only use the LM-password derived key).
Our new client-side NTLMSSP code also needed alteration to cope with our
comparatively simple server-side implementation. A future step is to replace
it with calls to the same NTLMSSP library.
Also included in this patch is the schannel 'sign only' patch I submitted to
the team earlier. While not enabled (and not functional, at this stage) the
work in this patch makes the code paths *much* easier to follow. I have also
included similar hooks in rpccleint to allow the use of schannel on *any* pipe.
rpcclient now defaults to not using schannel (or any other extra per-pipe
authenticiation) for any connection. The 'schannel' command enables schannel
for all pipes until disabled.
This code is also much more secure than the previous code, as changes to our
cli_pipe routines ensure that the authentication footer cannot be removed
by an attacker, and more error states are correctly handled.
(The same needs to be done to our server)
Andrew Bartlett
(This used to be commit 5472ddc9eaf4e79c5b2e1c8ee8c7f190dc285f19)
2003-07-14 12:46:32 +04:00
void SMBsesskeygen_lmv1 ( const uchar lm_hash [ 16 ] ,
const uchar lm_resp [ 24 ] , /* only uses 8 */
uint8 sess_key [ 16 ] )
{
/* Calculate the LM session key (effective length 40 bits,
but changes with each session ) */
uchar p24 [ 24 ] ;
uchar partial_lm_hash [ 16 ] ;
memcpy ( partial_lm_hash , lm_hash , 8 ) ;
memset ( partial_lm_hash + 8 , 0xbd , 8 ) ;
SMBOWFencrypt ( lm_hash , lm_resp , p24 ) ;
memcpy ( sess_key , p24 , 16 ) ;
sess_key [ 5 ] = 0xe5 ;
sess_key [ 6 ] = 0x38 ;
sess_key [ 7 ] = 0xb0 ;
# ifdef DEBUG_PASSWORD
DEBUG ( 100 , ( " SMBsesskeygen_lmv1: \n " ) ) ;
dump_data ( 100 , sess_key , 16 ) ;
# endif
}
2003-05-09 18:42:20 +04:00
DATA_BLOB NTLMv2_generate_names_blob ( const char * hostname ,
const char * domain )
{
DATA_BLOB names_blob = data_blob ( NULL , 0 ) ;
msrpc_gen ( & names_blob , " aaa " ,
True , NTLMSSP_NAME_TYPE_DOMAIN , domain ,
True , NTLMSSP_NAME_TYPE_SERVER , hostname ,
True , 0 , " " ) ;
return names_blob ;
}
static DATA_BLOB NTLMv2_generate_client_data ( const DATA_BLOB * names_blob )
{
uchar client_chal [ 8 ] ;
DATA_BLOB response = data_blob ( NULL , 0 ) ;
char long_date [ 8 ] ;
generate_random_buffer ( client_chal , sizeof ( client_chal ) , False ) ;
put_long_date ( long_date , time ( NULL ) ) ;
/* See http://www.ubiqx.org/cifs/SMB.html#SMB.8.5 */
msrpc_gen ( & response , " ddbbdb " ,
0x00000101 , /* Header */
0 , /* 'Reserved' */
long_date , 8 , /* Timestamp */
client_chal , 8 , /* client challenge */
0 , /* Unknown */
names_blob - > data , names_blob - > length ) ; /* End of name list */
return response ;
}
static DATA_BLOB NTLMv2_generate_response ( const uchar ntlm_v2_hash [ 16 ] ,
const DATA_BLOB * server_chal ,
const DATA_BLOB * names_blob )
2003-02-24 05:55:00 +03:00
{
uchar ntlmv2_response [ 16 ] ;
DATA_BLOB ntlmv2_client_data ;
DATA_BLOB final_response ;
/* NTLMv2 */
2003-05-09 18:42:20 +04:00
/* generate some data to pass into the response function - including
the hostname and domain name of the server */
ntlmv2_client_data = NTLMv2_generate_client_data ( names_blob ) ;
2003-02-24 05:55:00 +03:00
/* Given that data, and the challenge from the server, generate a response */
2003-05-09 18:42:20 +04:00
SMBOWFencrypt_ntv2 ( ntlm_v2_hash , server_chal , & ntlmv2_client_data , ntlmv2_response ) ;
2003-02-24 05:55:00 +03:00
2003-05-09 18:42:20 +04:00
final_response = data_blob ( NULL , sizeof ( ntlmv2_response ) + ntlmv2_client_data . length ) ;
2003-02-24 05:55:00 +03:00
memcpy ( final_response . data , ntlmv2_response , sizeof ( ntlmv2_response ) ) ;
2003-05-09 18:42:20 +04:00
memcpy ( final_response . data + sizeof ( ntlmv2_response ) ,
ntlmv2_client_data . data , ntlmv2_client_data . length ) ;
2003-02-24 05:55:00 +03:00
data_blob_free ( & ntlmv2_client_data ) ;
return final_response ;
}
2003-05-09 18:42:20 +04:00
static DATA_BLOB LMv2_generate_response ( const uchar ntlm_v2_hash [ 16 ] ,
const DATA_BLOB * server_chal )
{
uchar lmv2_response [ 16 ] ;
DATA_BLOB lmv2_client_data = data_blob ( NULL , 8 ) ;
DATA_BLOB final_response = data_blob ( NULL , 24 ) ;
/* LMv2 */
/* client-supplied random data */
generate_random_buffer ( lmv2_client_data . data , lmv2_client_data . length , False ) ;
/* Given that data, and the challenge from the server, generate a response */
SMBOWFencrypt_ntv2 ( ntlm_v2_hash , server_chal , & lmv2_client_data , lmv2_response ) ;
memcpy ( final_response . data , lmv2_response , sizeof ( lmv2_response ) ) ;
/* after the first 16 bytes is the random data we generated above,
so the server can verify us with it */
memcpy ( final_response . data + sizeof ( lmv2_response ) ,
lmv2_client_data . data , lmv2_client_data . length ) ;
data_blob_free ( & lmv2_client_data ) ;
return final_response ;
}
2003-02-24 05:55:00 +03:00
BOOL SMBNTLMv2encrypt ( const char * user , const char * domain , const char * password ,
2003-05-09 18:42:20 +04:00
const DATA_BLOB * server_chal ,
const DATA_BLOB * names_blob ,
2003-02-24 05:55:00 +03:00
DATA_BLOB * lm_response , DATA_BLOB * nt_response ,
2003-05-05 09:15:54 +04:00
DATA_BLOB * nt_session_key )
2003-02-24 05:55:00 +03:00
{
uchar nt_hash [ 16 ] ;
uchar ntlm_v2_hash [ 16 ] ;
E_md4hash ( password , nt_hash ) ;
/* We don't use the NT# directly. Instead we use it mashed up with
the username and domain .
This prevents username swapping during the auth exchange
*/
if ( ! ntv2_owf_gen ( nt_hash , user , domain , ntlm_v2_hash ) ) {
return False ;
}
2003-05-05 09:15:54 +04:00
if ( nt_response ) {
2003-05-09 18:42:20 +04:00
* nt_response = NTLMv2_generate_response ( ntlm_v2_hash , server_chal ,
names_blob ) ;
2003-05-05 09:15:54 +04:00
if ( nt_session_key ) {
* nt_session_key = data_blob ( NULL , 16 ) ;
/* The NTLMv2 calculations also provide a session key, for signing etc later */
/* use only the first 16 bytes of nt_response for session key */
SMBsesskeygen_ntv2 ( ntlm_v2_hash , nt_response - > data , nt_session_key - > data ) ;
}
}
2003-02-24 05:55:00 +03:00
/* LMv2 */
2003-05-05 09:15:54 +04:00
if ( lm_response ) {
2003-05-09 18:42:20 +04:00
* lm_response = LMv2_generate_response ( ntlm_v2_hash , server_chal ) ;
2003-05-05 09:15:54 +04:00
}
2003-02-24 05:55:00 +03:00
return True ;
}
2001-06-15 08:47:05 +04:00
/***********************************************************
2001-12-04 08:03:03 +03:00
encode a password buffer . The caller gets to figure out
what to put in it .
2001-06-15 08:47:05 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-12-04 08:03:03 +03:00
BOOL encode_pw_buffer ( char buffer [ 516 ] , char * new_pw , int new_pw_length )
2001-06-15 08:47:05 +04:00
{
2001-08-25 00:32:01 +04:00
generate_random_buffer ( ( unsigned char * ) buffer , 516 , True ) ;
2001-06-15 08:47:05 +04:00
2001-12-04 08:03:03 +03:00
memcpy ( & buffer [ 512 - new_pw_length ] , new_pw , new_pw_length ) ;
2001-06-15 08:47:05 +04:00
/*
* The length of the new password is in the last 4 bytes of
* the data buffer .
*/
2001-12-04 08:03:03 +03:00
SIVAL ( buffer , 512 , new_pw_length ) ;
2001-06-15 08:47:05 +04:00
return True ;
}
2000-10-07 19:56:36 +04:00
/***********************************************************
decode a password buffer
2001-09-26 15:51:25 +04:00
* new_pw_len is the length in bytes of the possibly mulitbyte
returned password including termination .
2000-10-07 19:56:36 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-04-22 06:54:04 +04:00
BOOL decode_pw_buffer ( char in_buffer [ 516 ] , char * new_pwrd ,
2001-09-26 15:51:25 +04:00
int new_pwrd_size , uint32 * new_pw_len )
2000-10-07 19:56:36 +04:00
{
2001-04-22 06:54:04 +04:00
int byte_len = 0 ;
2000-10-07 19:56:36 +04:00
/*
Warning ! ! ! : This function is called from some rpc call .
The password IN the buffer is a UNICODE string .
The password IN new_pwrd is an ASCII string
If you reuse that code somewhere else check first .
*/
2001-04-22 06:54:04 +04:00
/* The length of the new password is in the last 4 bytes of the data buffer. */
byte_len = IVAL ( in_buffer , 512 ) ;
2000-10-07 19:56:36 +04:00
# ifdef DEBUG_PASSWORD
2001-04-22 06:54:04 +04:00
dump_data ( 100 , in_buffer , 516 ) ;
2000-10-07 19:56:36 +04:00
# endif
2001-04-22 06:54:04 +04:00
/* Password cannot be longer than 128 characters */
if ( ( byte_len < 0 ) | | ( byte_len > new_pwrd_size - 1 ) ) {
DEBUG ( 0 , ( " decode_pw_buffer: incorrect password length (%d). \n " , byte_len ) ) ;
2001-09-26 15:51:25 +04:00
DEBUG ( 0 , ( " decode_pw_buffer: check that 'encrypt passwords = yes' \n " ) ) ;
2000-10-07 19:56:36 +04:00
return False ;
}
2001-09-26 15:51:25 +04:00
/* decode into the return buffer. Buffer must be a pstring */
* new_pw_len = pull_string ( NULL , new_pwrd , & in_buffer [ 512 - byte_len ] , new_pwrd_size , byte_len , STR_UNICODE ) ;
2000-10-07 19:56:36 +04:00
# ifdef DEBUG_PASSWORD
2001-09-26 15:51:25 +04:00
DEBUG ( 100 , ( " decode_pw_buffer: new_pwrd: " ) ) ;
dump_data ( 100 , ( char * ) new_pwrd , * new_pw_len ) ;
DEBUG ( 100 , ( " multibyte len:%d \n " , * new_pw_len ) ) ;
DEBUG ( 100 , ( " original char len:%d \n " , byte_len / 2 ) ) ;
2000-10-07 19:56:36 +04:00
# endif
2001-04-22 06:54:04 +04:00
2000-10-07 19:56:36 +04:00
return True ;
2002-07-15 14:35:28 +04:00
}