IdSSL.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.11 2004.02.03 5:45:36 PM czhower
  18. Name changes
  19. Rev 1.10 10/5/2003 11:44:06 PM GGrieve
  20. Remove IdContainers
  21. Rev 1.9 9/18/2003 10:20:28 AM JPMugaas
  22. Updated for new API.
  23. Rev 1.8 3/30/2003 12:38:56 AM BGooijen
  24. Removed warning
  25. Rev 1.7 3/30/2003 12:15:12 AM BGooijen
  26. Added MakeFTPSvrPort/MakeFTPSvrPasv
  27. Rev 1.6 3/23/2003 11:44:24 PM BGooijen
  28. Added MakeClientIOHandler(ATheThread:TIdThreadHandle ):...
  29. Rev 1.5 3/14/2003 10:00:36 PM BGooijen
  30. Removed TIdServerIOHandlerSSLBase.PeerPassthrough, the ssl is now enabled in
  31. the server-protocol-files
  32. Rev 1.3 3/13/2003 09:14:44 PM JPMugaas
  33. Added property suggested by Henrick Hellström (StreamSec) for checking a
  34. certificate against a URL provided by a user.
  35. Rev 1.2 3/13/2003 11:55:44 AM JPMugaas
  36. Updated registration framework to give more information.
  37. Rev 1.1 3/13/2003 4:08:42 PM BGooijen
  38. classes -> Classes
  39. Rev 1.0 3/13/2003 09:51:18 AM JPMugaas
  40. Abstract SSL class to permit the clients and servers to use OpenSSL or
  41. third-party components SSL IOHandler.
  42. }
  43. unit IdSSL;
  44. interface
  45. {$i IdCompilerDefines.inc}
  46. uses
  47. Classes,
  48. IdGlobal,
  49. IdIOHandler,
  50. IdIOHandlerSocket,
  51. IdIOHandlerStack,
  52. IdServerIOHandler,
  53. IdYarn;
  54. type
  55. TIdSSLVersion = (sslvSSLv2, sslvSSLv23, sslvSSLv3, sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2);
  56. TIdSSLVersions = set of TIdSSLVersion;
  57. //client
  58. TIdSSLIOHandlerSocketBase = class(TIdIOHandlerStack)
  59. protected
  60. fPassThrough: Boolean;
  61. fIsPeer : Boolean;
  62. FURIToCheck : String;
  63. function GetProxyTargetHost: string;
  64. function GetURIHost : string;
  65. function RecvEnc(var ABuffer: TIdBytes): Integer; virtual; abstract;
  66. function SendEnc(const ABuffer: TIdBytes; const AOffset, ALength: Integer): Integer; virtual; abstract;
  67. function ReadDataFromSource(var VBuffer: TIdBytes): Integer; override;
  68. function WriteDataToTarget(const ABuffer: TIdBytes; const AOffset, ALength: Integer): Integer; override;
  69. procedure SetPassThrough(const AValue: Boolean); virtual;
  70. procedure SetURIToCheck(const AValue: String); virtual;
  71. public
  72. constructor Create(AOwner: TComponent); override;
  73. // TODO: add an AOwner parameter
  74. function Clone : TIdSSLIOHandlerSocketBase; virtual; abstract;
  75. procedure StartSSL; virtual; abstract;
  76. property PassThrough: Boolean read fPassThrough write SetPassThrough;
  77. property IsPeer : Boolean read fIsPeer write fIsPeer;
  78. {
  79. Pasted from private corresponance from Henrick Hellström - StreamSec http://www.streamsec.com
  80. This property should be set to the exact value of the URI passed to e.g.
  81. TIdHTTP.Get and should not be used or modified by any code outside of
  82. the SSL handler implementation units. The reason for this is that the
  83. SSL/TLS handler should verify that the URI entered by the client user
  84. matches the identity information present in the server certificate.
  85. }
  86. property URIToCheck : String read FURIToCheck write SetURIToCheck;
  87. end;
  88. //server
  89. TIdServerIOHandlerSSLBase = class(TIdServerIOHandler)
  90. protected
  91. public
  92. //this is for the FTP Server to make a client IOHandler for it's data connection's IOHandler
  93. function MakeClientIOHandler(ATheThread:TIdYarn ): TIdIOHandler; overload; override;
  94. function MakeClientIOHandler : TIdSSLIOHandlerSocketBase; reintroduce; overload; virtual; abstract;
  95. function MakeFTPSvrPort : TIdSSLIOHandlerSocketBase; virtual; abstract;
  96. function MakeFTPSvrPasv : TIdSSLIOHandlerSocketBase; virtual; abstract;
  97. end;
  98. TIdClientSSLClass = class of TIdSSLIOHandlerSocketBase;
  99. TIdServerSSLClass = class of TIdServerIOHandlerSSLBase;
  100. implementation
  101. uses
  102. IdCustomTransparentProxy, IdURI;
  103. { TIdSSLIOHandlerSocketBase }
  104. constructor TIdSSLIOHandlerSocketBase.Create(AOwner: TComponent);
  105. begin
  106. inherited Create(AOwner);
  107. fPassThrough := True;
  108. end;
  109. function TIdSSLIOHandlerSocketBase.GetProxyTargetHost: string;
  110. var
  111. // under ARC, convert a weak reference to a strong reference before working with it
  112. LTransparentProxy, LNextTransparentProxy: TIdCustomTransparentProxy;
  113. begin
  114. Result := '';
  115. // RLebeau: not reading from the property as it will create a
  116. // default Proxy object if one is not already assigned...
  117. LTransparentProxy := FTransparentProxy;
  118. if Assigned(LTransparentProxy) then
  119. begin
  120. if LTransparentProxy.Enabled then
  121. begin
  122. repeat
  123. LNextTransparentProxy := LTransparentProxy.ChainedProxy;
  124. if not Assigned(LNextTransparentProxy) then
  125. Break;
  126. if not LNextTransparentProxy.Enabled then
  127. Break;
  128. LTransparentProxy := LNextTransparentProxy;
  129. until False;
  130. Result := LTransparentProxy.Host;
  131. end;
  132. end;
  133. end;
  134. function TIdSSLIOHandlerSocketBase.GetURIHost : string;
  135. var
  136. LURI: TIdURI;
  137. begin
  138. Result := '';
  139. if URIToCheck <> '' then
  140. begin
  141. LURI := TIdURI.Create(URIToCheck);
  142. try
  143. Result := LURI.Host;
  144. finally
  145. LURI.Free;
  146. end;
  147. end;
  148. end;
  149. function TIdSSLIOHandlerSocketBase.ReadDataFromSource(var VBuffer: TIdBytes): Integer;
  150. begin
  151. if PassThrough then begin
  152. Result := inherited ReadDataFromSource(VBuffer);
  153. end else begin
  154. Result := RecvEnc(VBuffer);
  155. end;
  156. end;
  157. function TIdSSLIOHandlerSocketBase.WriteDataToTarget(const ABuffer: TIdBytes;
  158. const AOffset, ALength: Integer): Integer;
  159. begin
  160. if PassThrough then begin
  161. Result := inherited WriteDataToTarget(ABuffer, AOffset, ALength);
  162. end else begin
  163. Result := SendEnc(ABuffer, AOffset, ALength);
  164. end;
  165. end;
  166. procedure TIdSSLIOHandlerSocketBase.SetPassThrough(const AValue: Boolean);
  167. begin
  168. fPassThrough := AValue;
  169. end;
  170. procedure TIdSSLIOHandlerSocketBase.SetURIToCheck(const AValue: String);
  171. begin
  172. FURIToCheck := AValue;
  173. end;
  174. { TIdServerIOHandlerSSLBase }
  175. function TIdServerIOHandlerSSLBase.MakeClientIOHandler(ATheThread: TIdYarn): TIdIOHandler;
  176. begin
  177. Result := MakeClientIOHandler;
  178. end;
  179. end.