IdSASLDigest.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. unit IdSASLDigest;
  2. interface
  3. {$i IdCompilerDefines.inc}
  4. uses
  5. Classes,
  6. SysUtils, //here to facilitate inline expansion
  7. IdSASL, IdSASLUserPass, IdException;
  8. type
  9. TIdSASLDigest = class(TIdSASLUserPass)
  10. protected
  11. Fauthzid : String;
  12. public
  13. function StartAuthenticate(const AChallenge, AHost, AProtocolName:string) : String; override;
  14. function ContinueAuthenticate(const ALastResponse, AHost, AProtocolName : string): string; override;
  15. class function ServiceName: TIdSASLServiceName; override;
  16. function IsReadyToStart: Boolean; override;
  17. published
  18. property authzid : String read Fauthzid write Fauthzid;
  19. end;
  20. EIdSASLDigestException = class(EIdException);
  21. EIdSASLDigestChallException = class(EIdSASLDigestException);
  22. EIdSASLDigestChallNoAlgorithm = class(EIdSASLDigestChallException);
  23. EIdSASLDigestChallInvalidAlg = class(EIdSASLDigestChallException);
  24. EIdSASLDigestAuthConfNotSupported = class(EIdSASLDigestException);
  25. //done this way so we can use testboxes
  26. function CalcDigestResponse(const AUserName, APassword, ARealm, ANonce, ACNonce : String;
  27. const ANC : Integer;
  28. const AQop, ADigestURI : String; const AAuthzid : String = '') : String;
  29. implementation
  30. uses
  31. IdFIPS, IdGlobal, IdGlobalProtocols, IdHash, IdHashMessageDigest, IdResourceStringsProtocols;
  32. function NCToStr(const AValue : Integer):String;
  33. {$IFDEF USE_INLINE} inline; {$ENDIF}
  34. begin
  35. Result := IntToHex(AValue,8);
  36. end;
  37. function Unquote(var S: String): String;
  38. {$IFDEF USE_INLINE} inline; {$ENDIF}
  39. var
  40. I, Len: Integer;
  41. begin
  42. Len := Length(S);
  43. I := 2; // skip first quote
  44. while I <= Len do
  45. begin
  46. if S[I] = '"' then begin
  47. Break;
  48. end;
  49. if S[I] = '\' then begin
  50. Inc(I);
  51. end;
  52. Inc(I);
  53. end;
  54. Result := Copy(S, 2, I-2);
  55. S := Copy(S, I+1, MaxInt);
  56. end;
  57. //
  58. function HashResult(const AStr : String): TIdBytes;
  59. {$IFDEF USE_INLINE} inline; {$ENDIF}
  60. var
  61. LMD5: TIdHashMessageDigest5;
  62. begin
  63. LMD5 := TIdHashMessageDigest5.Create;
  64. try
  65. Result := LMD5.HashString(AStr);
  66. finally
  67. LMD5.Free;
  68. end;
  69. end;
  70. function HashResultAsHex(const ABytes : TIdBytes) : String; overload;
  71. {$IFDEF USE_INLINE} inline; {$ENDIF}
  72. var
  73. LMD5: TIdHashMessageDigest5;
  74. begin
  75. LMD5 := TIdHashMessageDigest5.Create;
  76. try
  77. Result := LowerCase(LMD5.HashBytesAsHex(ABytes));
  78. finally
  79. LMD5.Free;
  80. end;
  81. end;
  82. function HashResultAsHex(const AStr : String) : String; overload;
  83. {$IFDEF USE_INLINE} inline; {$ENDIF}
  84. var
  85. LMD5: TIdHashMessageDigest5;
  86. begin
  87. LMD5 := TIdHashMessageDigest5.Create;
  88. try
  89. Result := LowerCase(LMD5.HashStringAsHex(AStr));
  90. finally
  91. LMD5.Free;
  92. end;
  93. end;
  94. function CalcDigestResponse(const AUserName, APassword, ARealm, ANonce, ACNonce : String;
  95. const ANC : Integer; const AQop, ADigestURI : String; const AAuthzid : String = '') : String;
  96. var
  97. LA1 : TIdBytes;
  98. LA2: TIdBytes;
  99. LA1_P : TIdBytes;
  100. begin
  101. LA1_P := IdGlobal.ToBytes(':' + ANonce + ':' + ACNonce);
  102. LA1 := HashResult(AUserName + ':' + ARealm + ':' +
  103. APassword);
  104. IdGlobal.AppendBytes(LA1,LA1_P);
  105. If AAuthzid <> '' then begin
  106. IdGlobal.AppendBytes(LA1,IdGlobal.ToBytes(AAuthzid));
  107. end;
  108. if AQop = 'auth' then begin
  109. LA2 := ToBytes('AUTHENTICATE:' + ADigestURI);
  110. end
  111. else if (AQop = 'auth-int') or (AQop = 'auth-conf') then begin
  112. LA2 := ToBytes('AUTHENTICATE:' + ADigestURI + ':00000000000000000000000000000000');
  113. end else begin
  114. SetLength(LA2,0);
  115. end;
  116. Result := HashResultAsHex(HashResultAsHex(LA1) + ':' + ANonce + ':' +
  117. NCToStr(ANC) + ':' + ACNonce + ':' + AQop +':' + HashResultAsHex(LA2));
  118. end;
  119. //
  120. { TIdSASLDigest }
  121. function TIdSASLDigest.ContinueAuthenticate(const ALastResponse, AHost,
  122. AProtocolName: string): string;
  123. begin
  124. Result := '';
  125. end;
  126. function TIdSASLDigest.IsReadyToStart: Boolean;
  127. begin
  128. Result := not GetFIPSMode;
  129. end;
  130. class function TIdSASLDigest.ServiceName: TIdSASLServiceName;
  131. begin
  132. Result := 'DIGEST-MD5';
  133. end;
  134. function TIdSASLDigest.StartAuthenticate(const AChallenge, AHost, AProtocolName: string): String;
  135. var
  136. LBuf : String;
  137. LChallange: TStringList;
  138. LReply : TStringList;
  139. Lqop : String;
  140. LstrCNonce : String;
  141. LstrResponse : String;
  142. LURL : String;
  143. LCharset : String;
  144. LQopOptions: TStrings;
  145. LAlgorithm : String;
  146. LNonce : String;
  147. LRealm: String;
  148. LName, LValue: String;
  149. begin
  150. LURL := AProtocolName+'/'+AHost;
  151. LReply := TStringList.Create;
  152. LChallange := TStringList.Create;
  153. LQopOptions:= TStringList.Create;
  154. try
  155. LBuf := AChallenge;
  156. while Length(LBuf) > 0 do begin
  157. LName := Trim(Fetch(LBuf, '=')); {do not localize}
  158. LBuf := TrimLeft(LBuf);
  159. if TextStartsWith(LBuf, '"') then begin {do not localize}
  160. LValue := Unquote(LBuf); {do not localize}
  161. Fetch(LBuf, ','); {do not localize}
  162. end else begin
  163. LValue := Trim(Fetch(LBuf, ','));
  164. end;
  165. IndyAddPair(LChallange, LName, LValue);
  166. LBuf := TrimLeft(LBuf);
  167. end;
  168. LQopOptions.CommaText := LChallange.Values['qop'];
  169. if LQopOptions.IndexOf('auth-int') > -1 then begin
  170. Lqop := 'auth-int';
  171. end else begin
  172. Lqop := 'auth';
  173. end;
  174. if LQopOptions.IndexOf('auth-conf') > -1 then begin
  175. if LQopOptions.IndexOf('auth') = -1 then begin
  176. raise EIdSASLDigestAuthConfNotSupported.Create(RSSASLDigestAuthConfNotSupported);
  177. end;
  178. end;
  179. LNonce := LChallange.Values['nonce'];
  180. LRealm := LChallange.Values['realm'];
  181. LAlgorithm := LChallange.Values['algorithm'];
  182. if LAlgorithm = '' then begin
  183. raise EIdSASLDigestChallNoAlgorithm.Create(RSSASLDigestMissingAlgorithm);
  184. end;
  185. {
  186. if LAlgorithm <> 'md5-sess' then begin
  187. raise EIdSASLDigestChallInvalidAlg.Create(RSSASLDigestInvalidAlgorithm);
  188. end;
  189. }
  190. //Commented out for case test mentioned in RFC 2831
  191. LstrCNonce := HashResultAsHex(DateTimeToStr(Now));
  192. LCharset := LChallange.Values['charset'];
  193. LstrResponse := CalcDigestResponse(GetUserName,Self.GetPassword,LRealm,LNonce,LstrCNonce,
  194. 1, Lqop, LURL,Fauthzid);
  195. // if LQopOptions.IndexOf('auth-conf') > -1 then begin
  196. // if LQopOptions.IndexOf('auth') = -1 then begin
  197. // raise EIdSASLDigestAuthConfNotSupported.Create(RSSASLDigestAuthConfNotSupported);
  198. // end;
  199. // end;
  200. if LCharset = '' then begin
  201. Result := '';
  202. end else begin
  203. Result := 'charset='+LCharset+',';
  204. end;
  205. {
  206. #( username | realm | nonce | cnonce |
  207. nonce-count | qop | digest-uri | response |
  208. maxbuf | charset | cipher | authzid |
  209. auth-param )
  210. }
  211. Result := Result + 'username="'+GetUsername+'"'+
  212. ',realm="'+LRealm+'"'+ {Do not localize}
  213. ',nonce="'+ LNonce+'"'+
  214. ',nc='+NCToStr(1)+
  215. ',cnonce="'+LstrCNonce+'"'+
  216. ',digest-uri="'+LURL+'"'+
  217. ',response='+LstrResponse+
  218. ',qop='+Lqop;
  219. finally
  220. FreeAndNil(LQopOptions);
  221. FreeAndNil(LChallange);
  222. FreeAndNil(LReply);
  223. end;
  224. end;
  225. end.