IdExplicitTLSClientServerBase.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. public
  86. constructor Create(AOwner: TComponent); override;
  87. end;
  88. TIdExplicitTLSClient = class(TIdTCPClientCustom)
  89. protected
  90. FRegularProtPort : TIdPort;
  91. FImplicitTLSProtPort : TIdPort;
  92. FExplicitTLSProtPort : TIdPort;
  93. FUseTLS : TIdUseTLS;
  94. FOnTLSNotAvailable : TIdOnTLSNegotiationFailure;
  95. FOnTLSNegCmdFailed : TIdOnTLSNegotiationFailure;
  96. FOnTLSHandShakeFailed : TIdOnTLSNegotiationFailure;
  97. //feature negotiation stuff
  98. FCapabilities : TStrings;
  99. function GetSupportsTLS : Boolean; virtual;
  100. procedure CheckIfCanUseTLS; virtual;
  101. procedure Loaded; override;
  102. procedure TLSNotAvailable;
  103. procedure DoOnTLSNotAvailable;
  104. procedure ProcessTLSNotAvail;
  105. procedure TLSNegCmdFailed;
  106. procedure DoOnTLSNegCmdFailed;
  107. procedure ProcessTLSNegCmdFailed;
  108. procedure TLSHandShakeFailed;
  109. procedure DoOnTLSHandShakeFailed;
  110. procedure ProcessTLSHandShakeFailed;
  111. procedure SetIOHandler(AValue: TIdIOHandler); override;
  112. procedure SetUseTLS(AValue : TIdUseTLS); virtual;
  113. //Note TLSHandshake should be the ONLY method to do the actual TLS
  114. //or SSL handshake for explicit TLS clients.
  115. procedure TLSHandshake; virtual;
  116. property UseTLS : TIdUseTLS read FUseTLS write SetUseTLS default DEF_USETLS;
  117. public
  118. constructor Create(AOwner: TComponent); override;
  119. destructor Destroy; override;
  120. procedure Connect; override;
  121. property SupportsTLS: boolean read GetSupportsTLS;
  122. property Capabilities : TStrings read FCapabilities;
  123. property OnTLSHandShakeFailed : TIdOnTLSNegotiationFailure read FOnTLSHandShakeFailed write FOnTLSHandShakeFailed;
  124. property OnTLSNotAvailable : TIdOnTLSNegotiationFailure read FOnTLSNotAvailable write FOnTLSNotAvailable;
  125. property OnTLSNegCmdFailed : TIdOnTLSNegotiationFailure read FOnTLSNegCmdFailed write FOnTLSNegCmdFailed;
  126. end;
  127. EIdTLSClientException = class(EIdException);
  128. EIdTLSClientSSLIOHandlerRequred = class(EIdTLSClientException);
  129. EIdTLSClientCanNotSetWhileConnected = class(EIdTLSClientException);
  130. EIdTLSClientTLSNotAvailable = class(EIdTLSClientException);
  131. EIdTLSClientTLSNegCmdFailed = class(EIdTLSClientException);
  132. EIdTLSClientTLSHandShakeFailed = class(EIdTLSClientException);
  133. EIdTLSServerException = class(EIdException);
  134. EIdTLSServerSSLIOHandlerRequired = class(EIdTLSServerException);
  135. EIdTLSClientCanNotSetWhileActive = class(EIdTLSClientException);
  136. implementation
  137. uses
  138. IdResourceStringsProtocols, IdSSL, IdBaseComponent, SysUtils;
  139. { TIdExplicitTLSServer }
  140. constructor TIdExplicitTLSServer.Create(AOwner: TComponent);
  141. begin
  142. inherited Create(AOwner);
  143. FUseTLS := DEF_USETLS;
  144. end;
  145. procedure TIdExplicitTLSServer.Loaded;
  146. begin
  147. inherited Loaded;
  148. if not (IOHandler is TIdServerIOHandler) then begin
  149. SetUseTLS(utNoTLSSupport);
  150. end;
  151. end;
  152. procedure TIdExplicitTLSServer.SetIOHandler(const AValue: TIdServerIOHandler);
  153. begin
  154. inherited SetIOHandler(AValue);
  155. if not (IOHandler is TIdServerIOHandlerSSLBase) then begin
  156. SetUseTLS(utNoTLSSupport);
  157. end;
  158. end;
  159. procedure TIdExplicitTLSServer.SetUseTLS(AValue: TIdUseTLS);
  160. begin
  161. if Active and (not IsDesignTime) then begin
  162. raise EIdTLSClientCanNotSetWhileActive.Create(RSTLSSLCanNotSetWhileConnected);
  163. end;
  164. if IsLoading then begin
  165. FUseTLS := AValue;
  166. Exit;
  167. end;
  168. if FUseTLS <> AValue then
  169. begin
  170. if AValue <> utNoTLSSupport then
  171. begin
  172. if not (IOHandler is TIdServerIOHandlerSSLBase) then begin
  173. raise EIdTLSServerSSLIOHandlerRequired.Create(RSTLSSSLIOHandlerRequired);
  174. end;
  175. end;
  176. case AValue of
  177. utNoTLSSupport: begin
  178. if (DefaultPort = FImplicitTLSProtPort) or (DefaultPort = FExplicitTLSProtPort) then begin
  179. DefaultPort := FRegularProtPort;
  180. end;
  181. if DefaultImplicitTLSPort = FImplicitTLSProtPort then begin
  182. DefaultImplicitTLSPort := 0;
  183. end;
  184. end;
  185. utUseImplicitTLS: begin
  186. if (DefaultPort = FRegularProtPort) or (DefaultPort = FExplicitTLSProtPort) then begin
  187. DefaultPort := FImplicitTLSProtPort;
  188. end;
  189. if DefaultImplicitTLSPort = 0 then begin
  190. DefaultImplicitTLSPort := FImplicitTLSProtPort;
  191. end;
  192. end;
  193. utUseExplicitTLS: begin
  194. if (DefaultPort = FRegularProtPort) or (DefaultPort = FImplicitTLSProtPort) then begin
  195. DefaultPort := iif(FExplicitTLSProtPort <> 0, FExplicitTLSProtPort, FRegularProtPort);
  196. end;
  197. if DefaultImplicitTLSPort = FImplicitTLSProtPort then begin
  198. DefaultImplicitTLSPort := 0;
  199. end;
  200. end;
  201. utUseRequireTLS: begin
  202. // TODO: what to set the DefaultPort and DefaultImplicitTLSPort to here?
  203. end;
  204. end;
  205. FUseTLS := AValue;
  206. end;
  207. end;
  208. { TIdExplicitTLSClient }
  209. constructor TIdExplicitTLSClient.Create(AOwner: TComponent);
  210. begin
  211. inherited Create(AOwner);
  212. FCapabilities := TStringList.Create;
  213. FUseTLS := DEF_USETLS;
  214. end;
  215. destructor TIdExplicitTLSClient.Destroy;
  216. begin
  217. FCapabilities.Free;
  218. inherited Destroy;
  219. end;
  220. procedure TIdExplicitTLSClient.CheckIfCanUseTLS;
  221. begin
  222. if not (IOHandler is TIdSSLIOHandlerSocketBase) then begin
  223. raise EIdTLSClientSSLIOHandlerRequred.Create(RSTLSSSLIOHandlerRequired);
  224. end;
  225. end;
  226. procedure TIdExplicitTLSClient.Connect;
  227. begin
  228. if UseTLS in ExplicitTLSVals then begin
  229. // TLS only enabled later in this case!
  230. (IOHandler as TIdSSLIOHandlerSocketBase).PassThrough := True;
  231. end;
  232. if (IOHandler is TIdSSLIOHandlerSocketBase) then begin
  233. case FUseTLS of
  234. utNoTLSSupport :
  235. begin
  236. (IOHandler as TIdSSLIOHandlerSocketBase).PassThrough := True;
  237. end;
  238. utUseImplicitTLS :
  239. begin
  240. (IOHandler as TIdSSLIOHandlerSocketBase).PassThrough := False;
  241. end;
  242. else
  243. begin
  244. if FUseTLS <> utUseImplicitTLS then begin
  245. (IOHandler as TIdSSLIOHandlerSocketBase).PassThrough := True;
  246. end;
  247. end;
  248. end;
  249. end;
  250. inherited Connect;
  251. end;
  252. //OnTLSHandShakeFailed
  253. procedure TIdExplicitTLSClient.DoOnTLSHandShakeFailed;
  254. var
  255. LContinue : Boolean;
  256. begin
  257. LContinue := False;
  258. if Assigned(OnTLSHandShakeFailed) then begin
  259. FOnTLSHandShakeFailed(Self, LContinue);
  260. end;
  261. if not LContinue then begin
  262. TLSHandShakeFailed;
  263. end;
  264. end;
  265. procedure TIdExplicitTLSClient.DoOnTLSNegCmdFailed;
  266. var
  267. LContinue : Boolean;
  268. begin
  269. LContinue := False;
  270. if Assigned(OnTLSNegCmdFailed) then begin
  271. FOnTLSNegCmdFailed(Self, LContinue);
  272. end;
  273. if not LContinue then begin
  274. TLSNegCmdFailed;
  275. end;
  276. end;
  277. procedure TIdExplicitTLSClient.DoOnTLSNotAvailable;
  278. var
  279. LContinue : Boolean;
  280. begin
  281. LContinue := True;
  282. if Assigned(FOnTLSNotAvailable) then begin
  283. FOnTLSNotAvailable(Self, LContinue);
  284. end;
  285. if not LContinue then begin
  286. TLSNotAvailable;
  287. end;
  288. end;
  289. procedure TIdExplicitTLSClient.Loaded;
  290. begin
  291. inherited Loaded;
  292. if not (IOHandler is TIdSSLIOHandlerSocketBase) then begin
  293. SetUseTLS(utNoTLSSupport);
  294. end;
  295. end;
  296. procedure TIdExplicitTLSClient.ProcessTLSHandShakeFailed;
  297. begin
  298. if FUseTLS = utUseRequireTLS then begin
  299. TLSHandShakeFailed;
  300. end else begin
  301. DoOnTLSHandShakeFailed;
  302. end;
  303. end;
  304. procedure TIdExplicitTLSClient.ProcessTLSNegCmdFailed;
  305. begin
  306. if FUseTLS = utUseRequireTLS then begin
  307. TLSNegCmdFailed;
  308. end else begin
  309. DoOnTLSNegCmdFailed;
  310. end;
  311. end;
  312. procedure TIdExplicitTLSClient.ProcessTLSNotAvail;
  313. begin
  314. if FUseTLS = utUseRequireTLS then begin
  315. TLSNotAvailable;
  316. end else begin
  317. DoOnTLSNotAvailable;
  318. end;
  319. end;
  320. procedure TIdExplicitTLSClient.SetIOHandler(AValue: TIdIOHandler);
  321. begin
  322. inherited SetIOHandler(AValue);
  323. if not (IOHandler is TIdSSLIOHandlerSocketBase) then begin
  324. if FUseTLS <> utNoTLSSupport then begin
  325. SetUseTLS(utNoTLSSupport);
  326. end;
  327. end;
  328. end;
  329. procedure TIdExplicitTLSClient.SetUseTLS(AValue: TIdUseTLS);
  330. begin
  331. if Connected then begin
  332. raise EIdTLSClientCanNotSetWhileConnected.Create(RSTLSSLCanNotSetWhileConnected);
  333. end;
  334. if IsLoading then begin
  335. FUseTLS := AValue;
  336. Exit;
  337. end;
  338. if FUseTLS <> AValue then
  339. begin
  340. if AValue <> utNoTLSSupport then begin
  341. CheckIfCanUseTLS;
  342. end;
  343. case AValue of
  344. utNoTLSSupport: begin
  345. if (Port = FImplicitTLSProtPort) or (Port = FExplicitTLSProtPort) then begin
  346. Port := FRegularProtPort;
  347. end;
  348. end;
  349. utUseImplicitTLS: begin
  350. if (Port = FRegularProtPort) or (Port = FExplicitTLSProtPort) then begin
  351. Port := FImplicitTLSProtPort;
  352. end;
  353. end;
  354. utUseExplicitTLS: begin
  355. if (Port = FRegularProtPort) or (Port = FImplicitTLSProtPort) then begin
  356. Port := iif(FExplicitTLSProtPort <> 0, FExplicitTLSProtPort, FRegularProtPort);
  357. end;
  358. end;
  359. utUseRequireTLS: begin
  360. // this should not be used on the client side! But sometimes users
  361. // make that mistake. So, should we update the Port here?
  362. end;
  363. end;
  364. FUseTLS := AValue;
  365. end;
  366. end;
  367. procedure TIdExplicitTLSClient.TLSHandshake;
  368. begin
  369. try
  370. if (IOHandler is TIdSSLIOHandlerSocketBase) then begin
  371. (IOHandler as TIdSSLIOHandlerSocketBase).PassThrough := False;
  372. end;
  373. except
  374. ProcessTLSHandShakeFailed;
  375. end;
  376. end;
  377. procedure TIdExplicitTLSClient.TLSHandShakeFailed;
  378. begin
  379. if Connected then begin
  380. // RLebeau 9/19/2013: do not send a goodbye command to the peer.
  381. // The socket data may be in a bad state at this point!
  382. Disconnect(False);
  383. end;
  384. // This method should always be called in the context of an active 'except'
  385. // block, so use IndyRaiseOuterException() to capture the inner exception
  386. // (if possible) when raising this outer exception...
  387. IndyRaiseOuterException(EIdTLSClientTLSHandShakeFailed.Create(RSTLSSLSSLHandshakeFailed));
  388. end;
  389. procedure TIdExplicitTLSClient.TLSNegCmdFailed;
  390. begin
  391. if Connected then begin
  392. Disconnect;
  393. end;
  394. // This method should never be called in the context of an active 'except'
  395. // block, so do not use IndyRaiseOuterException() to capture an inner exception
  396. // when raising this exception...
  397. raise EIdTLSClientTLSNegCmdFailed.Create(RSTLSSLSSLCmdFailed);
  398. end;
  399. procedure TIdExplicitTLSClient.TLSNotAvailable;
  400. begin
  401. if Connected then begin
  402. Disconnect;
  403. end;
  404. // This method should never be called in the context of an active 'except'
  405. // block, so do not use IndyRaiseOuterException() to capture an inner exception
  406. // when raising this exception...
  407. raise EIdTLSClientTLSNotAvailable.Create(RSTLSSLSSLNotAvailable);
  408. end;
  409. function TIdExplicitTLSClient.GetSupportsTLS: boolean;
  410. begin
  411. //this is a dummy for descendants to override. NET doesn't support
  412. //abstract methods.
  413. Result := False;
  414. end;
  415. end.