-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Removing hard-coded OpenSSL dependencies from IdAllAuthentications,…
… IdAuthenticationNTLM, and IdNTLM units (#376). Will remove them from IdRegister and IdDsnRegister units when the new IndyTLSOpenSSL package has been released. - Adding additional function pointers in IdFIPS unit to load an external Hashing library and to create NTLM challenge responses. - Moving functions that use OpenSSL to create NTLM challenge responses into IdSSLOpenSSLHeaders unit.
- Loading branch information
Showing
6 changed files
with
248 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ | |
{ | ||
Implementation of the NTLM authentication as specified in | ||
http://www.innovation.ch/java/ntlm.html with some fixes | ||
Author: Doychin Bondzhev ([email protected]) | ||
Copyright: (c) Chad Z. Hower and The Winshoes Working Group. | ||
} | ||
|
@@ -285,21 +285,13 @@ implementation | |
IdHash, | ||
IdHashMessageDigest, | ||
IdCoderMIME | ||
{$IFNDEF DOTNET} | ||
{.$IFDEF USE_OPENSSL} | ||
, IdSSLOpenSSLHeaders | ||
{.$ENDIF} | ||
{$ENDIF} | ||
{$IFDEF HAS_GENERICS_TArray_Copy} | ||
{$IFDEF HAS_UNIT_Generics_Collections} | ||
, System.Generics.Collections | ||
{$ENDIF} | ||
{$ENDIF} | ||
; | ||
|
||
type | ||
Pdes_key_schedule = ^des_key_schedule; | ||
|
||
const | ||
cProtocolStr: array[1..8] of Byte = (Ord('N'),Ord('T'),Ord('L'),Ord('M'),Ord('S'),Ord('S'),Ord('P'),$0); {Do not Localize} | ||
|
||
|
@@ -328,109 +320,15 @@ function NTLMFunctionsLoaded : Boolean; | |
end; | ||
{$ELSE} | ||
function NTLMFunctionsLoaded : Boolean; | ||
//{$IFNDEF USE_OPENSSL}{$IFDEF USE_INLINE} inline; {$ENDIF}{$ENDIF} | ||
begin | ||
{.$IFDEF USE_OPENSSL} | ||
Result := IdSSLOpenSSLHeaders.Load; | ||
Result := LoadNTLMLibrary; | ||
if Result then begin | ||
Result := Assigned(DES_set_odd_parity) and | ||
Assigned(DES_set_key) and | ||
Assigned(DES_ecb_encrypt); | ||
Result := IsNTLMFuncsAvail; | ||
end; | ||
{.$ELSE} | ||
//Result := False; | ||
{.$ENDIF} | ||
end; | ||
{$ENDIF} | ||
|
||
{$IFNDEF DOTNET} | ||
{/* | ||
* turns a 56 bit key into the 64 bit, odd parity key and sets the key. | ||
* The key schedule ks is also set. | ||
*/} | ||
procedure setup_des_key(key_56: des_cblock; Var ks: des_key_schedule); | ||
Var | ||
key: des_cblock; | ||
begin | ||
key[0] := key_56[0]; | ||
|
||
key[1] := ((key_56[0] SHL 7) and $FF) or (key_56[1] SHR 1); | ||
key[2] := ((key_56[1] SHL 6) and $FF) or (key_56[2] SHR 2); | ||
key[3] := ((key_56[2] SHL 5) and $FF) or (key_56[3] SHR 3); | ||
key[4] := ((key_56[3] SHL 4) and $FF) or (key_56[4] SHR 4); | ||
key[5] := ((key_56[4] SHL 3) and $FF) or (key_56[5] SHR 5); | ||
key[6] := ((key_56[5] SHL 2) and $FF) or (key_56[6] SHR 6); | ||
key[7] := (key_56[6] SHL 1) and $FF; | ||
|
||
DES_set_odd_parity(@key); | ||
DES_set_key(@key, ks); | ||
end; | ||
|
||
{/* | ||
* takes a 21 byte array and treats it as 3 56-bit DES keys. The | ||
* 8 byte plaintext is encrypted with each key and the resulting 24 | ||
* bytes are stored in the results array. | ||
*/} | ||
procedure calc_resp(keys: PDES_cblock; const ANonce: TIdBytes; results: Pdes_key_schedule); | ||
Var | ||
ks: des_key_schedule; | ||
nonce: des_cblock; | ||
begin | ||
setup_des_key(keys^, ks); | ||
Move(ANonce[0], nonce, 8); | ||
des_ecb_encrypt(@nonce, Pconst_DES_cblock(results), ks, DES_ENCRYPT); | ||
|
||
setup_des_key(PDES_cblock(PtrUInt(keys) + 7)^, ks); | ||
des_ecb_encrypt(@nonce, Pconst_DES_cblock(PtrUInt(results) + 8), ks, DES_ENCRYPT); | ||
|
||
setup_des_key(PDES_cblock(PtrUInt(keys) + 14)^, ks); | ||
des_ecb_encrypt(@nonce, Pconst_DES_cblock(PtrUInt(results) + 16), ks, DES_ENCRYPT); | ||
end; | ||
|
||
Const | ||
Magic: des_cblock = ($4B, $47, $53, $21, $40, $23, $24, $25 ); | ||
|
||
//* setup LanManager password */ | ||
function SetupLanManagerPassword(const APassword: String; const ANonce: TIdBytes): TIdBytes; | ||
var | ||
lm_hpw: array[0..20] of Byte; | ||
lm_pw: array[0..13] of Byte; | ||
idx, len: Integer; | ||
ks: des_key_schedule; | ||
lm_resp: array [0..23] of Byte; | ||
lPassword: {$IFDEF STRING_IS_UNICODE}TIdBytes{$ELSE}AnsiString{$ENDIF}; | ||
begin | ||
{$IFDEF STRING_IS_UNICODE} | ||
lPassword := IndyTextEncoding_OSDefault.GetBytes(UpperCase(APassword)); | ||
{$ELSE} | ||
lPassword := UpperCase(APassword); | ||
{$ENDIF} | ||
|
||
len := IndyMin(Length(lPassword), 14); | ||
if len > 0 then begin | ||
Move(lPassword[{$IFDEF STRING_IS_UNICODE}0{$ELSE}1{$ENDIF}], lm_pw[0], len); | ||
end; | ||
if len < 14 then begin | ||
for idx := len to 13 do begin | ||
lm_pw[idx] := $0; | ||
end; | ||
end; | ||
|
||
//* create LanManager hashed password */ | ||
|
||
setup_des_key(pdes_cblock(@lm_pw[0])^, ks); | ||
des_ecb_encrypt(@magic, Pconst_DES_cblock(@lm_hpw[0]), ks, DES_ENCRYPT); | ||
|
||
setup_des_key(pdes_cblock(PtrUInt(@lm_pw[0]) + 7)^, ks); | ||
des_ecb_encrypt(@magic, Pconst_DES_cblock(PtrUInt(@lm_hpw[0]) + 8), ks, DES_ENCRYPT); | ||
|
||
FillChar(lm_hpw[16], 5, 0); | ||
|
||
calc_resp(PDes_cblock(@lm_hpw[0]), ANonce, Pdes_key_schedule(@lm_resp[0])); | ||
|
||
SetLength(Result, SizeOf(lm_resp)); | ||
Move(lm_resp[0], Result[0], SizeOf(lm_resp)); | ||
end; | ||
|
||
function BuildUnicode(const S: String): TIdBytes; | ||
{$IFDEF STRING_IS_UNICODE} | ||
|
@@ -443,7 +341,7 @@ function BuildUnicode(const S: String): TIdBytes; | |
{$IFDEF STRING_IS_UNICODE} | ||
Result := IndyTextEncoding_UTF16LE.GetBytes(S); | ||
{$ELSE} | ||
// RLebeau: TODO - should this use encUTF16LE as well? This logic will | ||
// RLebeau: TODO - should this use UTF-16 as well? This logic will | ||
// not produce a valid Unicode string if non-ASCII characters are present! | ||
SetLength(Result, Length(S) * SizeOf(WideChar)); | ||
for i := 0 to Length(S)-1 do begin | ||
|
@@ -453,35 +351,6 @@ function BuildUnicode(const S: String): TIdBytes; | |
{$ENDIF} | ||
end; | ||
|
||
//* create NT hashed password */ | ||
function CreateNTPassword(const APassword: String; const ANonce: TIdBytes): TIdBytes; | ||
var | ||
nt_hpw: array [1..21] of Byte; | ||
nt_hpw128: TIdBytes; | ||
nt_resp: array [1..24] of Byte; | ||
LMD4: TIdHashMessageDigest4; | ||
begin | ||
CheckMD4Permitted; | ||
LMD4 := TIdHashMessageDigest4.Create; | ||
try | ||
{$IFDEF STRING_IS_UNICODE} | ||
nt_hpw128 := LMD4.HashString(APassword, IndyTextEncoding_UTF16LE); | ||
{$ELSE} | ||
nt_hpw128 := LMD4.HashBytes(BuildUnicode(APassword)); | ||
{$ENDIF} | ||
finally | ||
LMD4.Free; | ||
end; | ||
|
||
Move(nt_hpw128[0], nt_hpw[1], 16); | ||
FillChar(nt_hpw[17], 5, 0); | ||
|
||
calc_resp(pdes_cblock(@nt_hpw[1]), ANonce, Pdes_key_schedule(@nt_resp[1])); | ||
|
||
SetLength(Result, SizeOf(nt_resp)); | ||
Move(nt_resp[1], Result[0], SizeOf(nt_resp)); | ||
end; | ||
|
||
function BuildType1Message(const ADomain, AHost: String): String; | ||
var | ||
LEncoding: IIdTextEncoding; | ||
|
@@ -547,8 +416,8 @@ function BuildType3Message(const ADomain, AHost, AUsername: TIdUnicodeString; | |
lHost: TIdBytes; | ||
lUsername: TIdBytes; | ||
begin | ||
lm_password := SetupLanManagerPassword(APassword, ANonce); | ||
nt_password := CreateNTPassword(APassword, ANonce); | ||
lm_password := IdFIPS.NTLMGetLmChallengeResponse(APassword, ANonce); | ||
nt_password := IdFIPS.NTLMGetNtChallengeResponse(APassword, ANonce); | ||
|
||
lDomain := BuildUnicode(UpperCase(ADomain)); | ||
lHost := BuildUnicode(UpperCase(AHost)); | ||
|
Oops, something went wrong.