IdExplicitTLSClientServerBase.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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.14 10/26/2004 9:09:36 PM JPMugaas
  18. Updated references.
  19. Rev 1.13 2004.02.03 5:45:36 PM czhower
  20. Name changes
  21. Rev 1.12 1/25/2004 3:52:28 PM JPMugaas
  22. Fixes for abstract SSL interface to work in NET.
  23. Rev 1.11 1/21/2004 1:23:38 PM JPMugaas
  24. InitComponent.
  25. Rev 1.10 5/25/2003 12:06:16 AM JPMugaas
  26. TLS checking code moved into a protected method for reuse in TIdDirectSMTP.
  27. Note that TLS support is different in that component because of the way it
  28. works.
  29. Rev 1.9 5/21/2003 3:36:42 PM BGooijen
  30. Fixed design time bug regarding the Active property
  31. Rev 1.8 5/8/2003 11:27:38 AM JPMugaas
  32. Moved feature negoation properties down to the ExplicitTLSClient level as
  33. feature negotiation goes hand in hand with explicit TLS support.
  34. Rev 1.7 4/13/2003 05:38:02 PM JPMugaas
  35. Fix for SetTLS exception problem with IdMessage.SaveToFile.
  36. Rev 1.6 4/5/2003 02:06:48 PM JPMugaas
  37. TLS handshake itself can now be handled.
  38. Rev 1.5 3/27/2003 05:46:22 AM JPMugaas
  39. Updated framework with an event if the TLS negotiation command fails.
  40. Cleaned up some duplicate code in the clients.
  41. Rev 1.4 3/26/2003 04:19:18 PM JPMugaas
  42. Cleaned-up some code and illiminated some duplicate things.
  43. Rev 1.3 3/23/2003 11:45:02 PM BGooijen
  44. classes -> Classes
  45. Rev 1.2 3/18/2003 04:36:52 PM JPMugaas
  46. Rev 1.1 3/16/2003 06:08:34 PM JPMugaas
  47. Fixed a bug where the wrong port number was being set. I also expanded a few
  48. things for the server.
  49. Rev 1.0 3/16/2003 02:38:08 PM JPMugaas
  50. Base class for some clients that use both implicit and explicit TLS.
  51. }
  52. unit IdExplicitTLSClientServerBase;
  53. interface
  54. {$i IdCompilerDefines.inc}
  55. uses
  56. Classes,
  57. IdCmdTCPServer,
  58. IdException,
  59. IdGlobal,
  60. IdIOHandler,
  61. IdServerIOHandler,
  62. IdTCPClient;
  63. type
  64. TIdUseTLS = (
  65. utNoTLSSupport,
  66. utUseImplicitTLS, // ssl iohandler req, allways tls
  67. utUseRequireTLS, // ssl iohandler req, user command only accepted when in tls
  68. utUseExplicitTLS // < user can choose to use tls
  69. );
  70. const
  71. ExplicitTLSVals = [utUseRequireTLS,utUseExplicitTLS];
  72. DEF_USETLS = utNoTLSSupport; //we can't assume the user wants to use a SSL IOHandler
  73. type
  74. TIdOnTLSNegotiationFailure = procedure(Asender : TObject; var VContinue : Boolean) of object;
  75. TIdExplicitTLSServer = class(TIdCmdTCPServer)
  76. protected
  77. FRegularProtPort : TIdPort;
  78. FImplicitTLSProtPort : TIdPort;
  79. FExplicitTLSProtPort : TIdPort;
  80. FUseTLS : TIdUseTLS;
  81. procedure Loaded; override;
  82. procedure SetIOHandler(const AValue: TIdServerIOHandler); override;
  83. procedure SetUseTLS(AValue : TIdUseTLS); virtual;
  84. property UseTLS : TIdUseTLS read FUseTLS write SetUseTLS default DEF_USETLS;
  85. procedure InitComponent; override;
  86. end;
  87. TIdExplicitTLSClient = class(TIdTCPClientCustom)
  88. protected
  89. FRegularProtPort : TIdPort;
  90. FImplicitTLSProtPort : TIdPort;
  91. FExplicitTLSProtPort : TIdPort;
  92. FUseTLS : TIdUseTLS;
  93. FOnTLSNotAvailable : TIdOnTLSNegotiationFailure;
  94. FOnTLSNegCmdFailed : TIdOnTLSNegotiationFailure;
  95. FOnTLSHandShakeFailed : TIdOnTLSNegotiationFailure;
  96. //feature negotiation stuff
  97. FCapabilities : TStrings;
  98. function GetSupportsTLS : Boolean; virtual;
  99. procedure CheckIfCanUseTLS; virtual;
  100. procedure Loaded; override;
  101. procedure TLSNotAvailable;
  102. procedure DoOnTLSNotAvailable;
  103. procedure ProcessTLSNotAvail;
  104. procedure TLSNegCmdFailed;
  105. procedure DoOnTLSNegCmdFailed;
  106. procedure ProcessTLSNegCmdFailed;
  107. procedure TLSHandShakeFailed;
  108. procedure DoOnTLSHandShakeFailed;
  109. procedure ProcessTLSHandShakeFailed;
  110. procedure SetIOHandler(AValue: TIdIOHandler); override;
  111. procedure SetUseTLS(AValue : TIdUseTLS); virtual;
  112. //Note TLSHandshake should be the ONLY method to do the actual TLS
  113. //or SSL handshake for explicit TLS clients.
  114. procedure TLSHandshake; virtual;
  115. procedure InitComponent; override;
  116. property UseTLS : TIdUseTLS read FUseTLS write SetUseTLS default DEF_USETLS;
  117. public
  118. destructor Destroy; override;
  119. procedure Connect; override;
  120. property SupportsTLS: boolean read GetSupportsTLS;
  121. property Capabilities : TStrings read FCapabilities;
  122. property OnTLSHandShakeFailed : TIdOnTLSNegotiationFailure read FOnTLSHandShakeFailed write FOnTLSHandShakeFailed;
  123. property OnTLSNotAvailable : TIdOnTLSNegotiationFailure read FOnTLSNotAvailable write FOnTLSNotAvailable;
  124. property OnTLSNegCmdFailed : TIdOnTLSNegotiationFailure read FOnTLSNegCmdFailed write FOnTLSNegCmdFailed;
  125. end;
  126. EIdTLSClientException = class(EIdException);
  127. EIdTLSClientSSLIOHandlerRequred = class(EIdTLSClientException);
  128. EIdTLSClientCanNotSetWhileConnected = class(EIdTLSClientException);
  129. EIdTLSClientTLSNotAvailable = class(EIdTLSClientException);
  130. EIdTLSClientTLSNegCmdFailed = class(EIdTLSClientException);
  131. EIdTLSClientTLSHandShakeFailed = class(EIdTLSClientException);
  132. EIdTLSServerException = class(EIdException);
  133. EIdTLSServerSSLIOHandlerRequired = class(EIdTLSServerException);
  134. EIdTLSClientCanNotSetWhileActive = class(EIdTLSClientException);
  135. implementation
  136. uses
  137. IdResourceStringsProtocols, IdSSL, IdBaseComponent, SysUtils;
  138. { TIdExplicitTLSServer }
  139. procedure TIdExplicitTLSServer.InitComponent;
  140. begin
  141. inherited InitComponent;
  142. FUseTLS := DEF_USETLS;
  143. end;
  144. procedure TIdExplicitTLSServer.Loaded;
  145. begin
  146. inherited Loaded;
  147. if not (IOHandler is TIdServerIOHandler) then begin
  148. SetUseTLS(utNoTLSSupport);
  149. end;
  150. end;
  151. procedure TIdExplicitTLSServer.SetIOHandler(const AValue: TIdServerIOHandler);
  152. begin
  153. inherited SetIOHandler(AValue);
  154. if not (IOHandler is TIdServerIOHandlerSSLBase) then begin
  155. SetUseTLS(utNoTLSSupport);
  156. end;
  157. end;
  158. procedure TIdExplicitTLSServer.SetUseTLS(AValue: TIdUseTLS);
  159. begin
  160. if Active and (not IsDesignTime) then begin
  161. raise EIdTLSClientCanNotSetWhileActive.Create(RSTLSSLCanNotSetWhileConnected);
  162. end;
  163. if IsLoading then begin
  164. FUseTLS := AValue;
  165. Exit;
  166. end;
  167. if FUseTLS <> AValue then
  168. begin
  169. if AValue <> utNoTLSSupport then
  170. begin
  171. if not (IOHandler is TIdServerIOHandlerSSLBase) then begin
  172. raise EIdTLSServerSSLIOHandlerRequired.Create(RSTLSSSLIOHandlerRequired);
  173. end;
  174. end;
  175. case AValue of
  176. utUseImplicitTLS: begin
  177. if (DefaultPort = FRegularProtPort) or (DefaultPort = FExplicitTLSProtPort) then begin
  178. DefaultPort := FImplicitTLSProtPort;
  179. end;
  180. end;
  181. utUseExplicitTLS: begin
  182. if (DefaultPort = FRegularProtPort) or (DefaultPort = FImplicitTLSProtPort) then begin
  183. DefaultPort := iif(FExplicitTLSProtPort <> 0, FExplicitTLSProtPort, FRegularProtPort);
  184. end;
  185. end;
  186. else
  187. if (DefaultPort = FImplicitTLSProtPort) or (DefaultPort = FExplicitTLSProtPort) then begin
  188. DefaultPort := FRegularProtPort;
  189. end;
  190. end;
  191. FUseTLS := AValue;
  192. end;
  193. end;
  194. { TIdExplicitTLSClient }
  195. procedure TIdExplicitTLSClient.CheckIfCanUseTLS;
  196. begin
  197. if not (IOHandler is TIdSSLIOHandlerSocketBase) then begin
  198. raise EIdTLSClientSSLIOHandlerRequred.Create(RSTLSSSLIOHandlerRequired);
  199. end;
  200. end;
  201. procedure TIdExplicitTLSClient.Connect;
  202. begin
  203. if UseTLS in ExplicitTLSVals then begin
  204. // TLS only enabled later in this case!
  205. (IOHandler as TIdSSLIOHandlerSocketBase).PassThrough := True;
  206. end;
  207. if (IOHandler is TIdSSLIOHandlerSocketBase) then begin
  208. case FUseTLS of
  209. utNoTLSSupport :
  210. begin
  211. (IOHandler as TIdSSLIOHandlerSocketBase).PassThrough := True;
  212. end;
  213. utUseImplicitTLS :
  214. begin
  215. (IOHandler as TIdSSLIOHandlerSocketBase).PassThrough := False;
  216. end;
  217. else
  218. begin
  219. if FUseTLS <> utUseImplicitTLS then begin
  220. (IOHandler as TIdSSLIOHandlerSocketBase).PassThrough := True;
  221. end;
  222. end;
  223. end;
  224. end;
  225. inherited Connect;
  226. end;
  227. procedure TIdExplicitTLSClient.InitComponent;
  228. begin
  229. inherited InitComponent;
  230. FCapabilities := TStringList.Create;
  231. FUseTLS := DEF_USETLS;
  232. end;
  233. destructor TIdExplicitTLSClient.Destroy;
  234. begin
  235. FreeAndNil(FCapabilities);
  236. inherited Destroy;
  237. end;
  238. //OnTLSHandShakeFailed
  239. procedure TIdExplicitTLSClient.DoOnTLSHandShakeFailed;
  240. var
  241. LContinue : Boolean;
  242. begin
  243. LContinue := False;
  244. if Assigned(OnTLSHandShakeFailed) then begin
  245. FOnTLSHandShakeFailed(Self, LContinue);
  246. end;
  247. if not LContinue then begin
  248. TLSHandShakeFailed;
  249. end;
  250. end;
  251. procedure TIdExplicitTLSClient.DoOnTLSNegCmdFailed;
  252. var
  253. LContinue : Boolean;
  254. begin
  255. LContinue := False;
  256. if Assigned(OnTLSNegCmdFailed) then begin
  257. FOnTLSNegCmdFailed(Self, LContinue);
  258. end;
  259. if not LContinue then begin
  260. TLSNegCmdFailed;
  261. end;
  262. end;
  263. procedure TIdExplicitTLSClient.DoOnTLSNotAvailable;
  264. var
  265. LContinue : Boolean;
  266. begin
  267. LContinue := True;
  268. if Assigned(FOnTLSNotAvailable) then begin
  269. FOnTLSNotAvailable(Self, LContinue);
  270. end;
  271. if not LContinue then begin
  272. TLSNotAvailable;
  273. end;
  274. end;
  275. procedure TIdExplicitTLSClient.Loaded;
  276. begin
  277. inherited Loaded;
  278. if not (IOHandler is TIdSSLIOHandlerSocketBase) then begin
  279. SetUseTLS(utNoTLSSupport);
  280. end;
  281. end;
  282. procedure TIdExplicitTLSClient.ProcessTLSHandShakeFailed;
  283. begin
  284. if FUseTLS = utUseRequireTLS then begin
  285. TLSHandShakeFailed;
  286. end else begin
  287. DoOnTLSHandShakeFailed;
  288. end;
  289. end;
  290. procedure TIdExplicitTLSClient.ProcessTLSNegCmdFailed;
  291. begin
  292. if FUseTLS = utUseRequireTLS then begin
  293. TLSNegCmdFailed;
  294. end else begin
  295. DoOnTLSNegCmdFailed;
  296. end;
  297. end;
  298. procedure TIdExplicitTLSClient.ProcessTLSNotAvail;
  299. begin
  300. if FUseTLS = utUseRequireTLS then begin
  301. TLSNotAvailable;
  302. end else begin
  303. DoOnTLSNotAvailable;
  304. end;
  305. end;
  306. procedure TIdExplicitTLSClient.SetIOHandler(AValue: TIdIOHandler);
  307. begin
  308. inherited SetIOHandler(AValue);
  309. if not (IOHandler is TIdSSLIOHandlerSocketBase) then begin
  310. if FUseTLS <> utNoTLSSupport then begin
  311. SetUseTLS(utNoTLSSupport);
  312. end;
  313. end;
  314. end;
  315. procedure TIdExplicitTLSClient.SetUseTLS(AValue: TIdUseTLS);
  316. begin
  317. if Connected then begin
  318. raise EIdTLSClientCanNotSetWhileConnected.Create(RSTLSSLCanNotSetWhileConnected);
  319. end;
  320. if IsLoading then begin
  321. FUseTLS := AValue;
  322. Exit;
  323. end;
  324. if FUseTLS <> AValue then
  325. begin
  326. if AValue <> utNoTLSSupport then begin
  327. CheckIfCanUseTLS;
  328. end;
  329. case AValue of
  330. utUseImplicitTLS: begin
  331. if (Port = FRegularProtPort) or (Port = FExplicitTLSProtPort) then begin
  332. Port := FImplicitTLSProtPort;
  333. end;
  334. end;
  335. utUseExplicitTLS: begin
  336. if (Port = FRegularProtPort) or (Port = FImplicitTLSProtPort) then begin
  337. Port := iif(FExplicitTLSProtPort <> 0, FExplicitTLSProtPort, FRegularProtPort);
  338. end;
  339. end;
  340. else
  341. if (Port = FImplicitTLSProtPort) or (Port = FExplicitTLSProtPort) then begin
  342. Port := FRegularProtPort;
  343. end;
  344. end;
  345. FUseTLS := AValue;
  346. end;
  347. end;
  348. procedure TIdExplicitTLSClient.TLSHandshake;
  349. begin
  350. try
  351. if (IOHandler is TIdSSLIOHandlerSocketBase) then begin
  352. (IOHandler as TIdSSLIOHandlerSocketBase).PassThrough := False;
  353. end;
  354. except
  355. ProcessTLSHandShakeFailed;
  356. end;
  357. end;
  358. procedure TIdExplicitTLSClient.TLSHandShakeFailed;
  359. begin
  360. if Connected then begin
  361. // RLebeau 9/19/2013: do not send a goodbye command to the peer.
  362. // The socket data may be in a bad state at this point!
  363. Disconnect(False);
  364. end;
  365. // This method should always be called in the context of an active 'except'
  366. // block, so use IndyRaiseOuterException() to capture the inner exception
  367. // (if possible) when raising this outer exception...
  368. IndyRaiseOuterException(EIdTLSClientTLSHandShakeFailed.Create(RSTLSSLSSLHandshakeFailed));
  369. end;
  370. procedure TIdExplicitTLSClient.TLSNegCmdFailed;
  371. begin
  372. if Connected then begin
  373. Disconnect;
  374. end;
  375. // This method should never be called in the context of an active 'except'
  376. // block, so do not use IndyRaiseOuterException() to capture an inner exception
  377. // when raising this exception...
  378. raise EIdTLSClientTLSNegCmdFailed.Create(RSTLSSLSSLCmdFailed);
  379. end;
  380. procedure TIdExplicitTLSClient.TLSNotAvailable;
  381. begin
  382. if Connected then begin
  383. Disconnect;
  384. end;
  385. // This method should never be called in the context of an active 'except'
  386. // block, so do not use IndyRaiseOuterException() to capture an inner exception
  387. // when raising this exception...
  388. raise EIdTLSClientTLSNotAvailable.Create(RSTLSSLSSLNotAvailable);
  389. end;
  390. function TIdExplicitTLSClient.GetSupportsTLS: boolean;
  391. begin
  392. //this is a dummy for descendants to override. NET doesn't support
  393. //abstract methods.
  394. Result := False;
  395. end;
  396. end.