sslbase.pp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. unit sslbase;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils;
  6. Type
  7. TSSLType = (stAny,stSSLv2,stSSLv3,stTLSv1,stTLSv1_1,stTLSv1_2);
  8. { TSSLData }
  9. TSSLData = Class(TPersistent)
  10. private
  11. FFileName: String;
  12. FValue: TBytes;
  13. Public
  14. Function Empty : Boolean;
  15. Procedure Assign(Source : TPersistent);override;
  16. Property FileName : String Read FFileName Write FFileName;
  17. Property Value: TBytes Read FValue Write FValue;
  18. end;
  19. Const
  20. SSLDataCount = 4; // 0 based.
  21. StrDataCount = 2; // 0 based.
  22. Type
  23. { TSSLSocketHandler }
  24. { TCertificateData }
  25. TCertificateData = Class(TPersistent)
  26. Private
  27. FStrData : Array[0..StrDataCount] of string;
  28. FCertData : Array[0..SSLDataCount] of TSSLData;
  29. function GetSSLData(AIndex: Integer): TSSLData;
  30. procedure SetSSLData(AIndex: Integer; AValue: TSSLData);
  31. function GetString(AIndex: Integer): String;
  32. procedure SetString(AIndex: Integer; AValue: String);
  33. Public
  34. constructor Create;
  35. Destructor Destroy; override;
  36. Procedure Assign(Source : TPersistent); override;
  37. Function NeedCertificateData : Boolean;
  38. Published
  39. property KeyPassword: string Index 0 read GetString write SetString;
  40. property CipherList: string Index 1 read GetString write SetString;
  41. Property HostName : String Index 2 read GetString write SetString;
  42. property Certificate : TSSLData Index 0 Read GetSSLData Write SetSSLData;
  43. property TrustedCertificate : TSSLData Index 1 Read GetSSLData Write SetSSLData;
  44. property PrivateKey : TSSLData Index 2 Read GetSSLData Write SetSSLData;
  45. property PFX: TSSLData Index 3 Read GetSSLData Write SetSSLData;
  46. property CertCA: TSSLData Index 4 Read GetSSLData Write SetSSLData;
  47. end;
  48. { TX509Certificate }
  49. TCertAndKey = Record
  50. Certificate : TBytes;
  51. PrivateKey : TBytes;
  52. end;
  53. TX509Certificate = Class (TObject)
  54. private
  55. FCommonName: string;
  56. FCountry: String;
  57. FHostName: string;
  58. FKeySize: Integer;
  59. FOrganization: String;
  60. FSerial: Integer;
  61. FValidFrom: TDateTime;
  62. FValidTo: TDateTime;
  63. FVersion: Integer;
  64. function GetKeySize: Integer;
  65. function GetValidFrom: TDateTime;
  66. function GetValidTo: TDateTime;
  67. function GetVersion: Integer;
  68. Protected
  69. Function GetRealSerial : Integer;
  70. Public
  71. Function CreateCertificateAndKey : TCertAndKey; virtual; abstract;
  72. Procedure CreateCertificateAndKey(Var aCertificate,aKey : TBytes);
  73. Property Country : String Read FCountry Write FCountry;
  74. Property HostName : string Read FHostName Write FHostName;
  75. Property CommonName : string Read FCommonName Write FCommonName;
  76. Property Organization : String Read FOrganization Write FOrganization;
  77. Property KeySize : Integer Read GetKeySize Write FKeySize;
  78. // Valid from. Default today -1;
  79. Property ValidFrom : TDateTime Read GetValidFrom Write FValidFrom;
  80. // Valid To. Default today + 31;
  81. Property ValidTo : TDateTime Read GetValidTo Write FValidTo;
  82. // Version Default 1.
  83. Property Version : Integer Read GetVersion Write FVersion;
  84. // Serial. If zero, then a serial is generated.
  85. Property Serial : Integer Read FSerial Write FSerial;
  86. end;
  87. implementation
  88. { TSSLData }
  89. Function TSSLData.Empty: Boolean;
  90. begin
  91. Result:=(Length(Value)=0) and (FileName='');
  92. end;
  93. Procedure TSSLData.Assign(Source: TPersistent);
  94. begin
  95. if Source is TSSLData then
  96. With TSSLData(Source) do
  97. begin
  98. Self.FValue:=FValue;
  99. Self.FFileName:=FFileName;
  100. end
  101. else
  102. inherited Assign(Source);
  103. end;
  104. { TCertificateData }
  105. function TCertificateData.GetSSLData(AIndex: Integer): TSSLData;
  106. begin
  107. Result:=FCertData[AIndex];
  108. end;
  109. procedure TCertificateData.SetSSLData(AIndex: Integer; AValue: TSSLData);
  110. begin
  111. FCertData[AIndex].Assign(AValue);
  112. end;
  113. function TCertificateData.GetString(AIndex: Integer): String;
  114. begin
  115. Result:=FStrData[AIndex];
  116. if (AIndex=2) and (result='') then
  117. Result:='localhost';
  118. end;
  119. procedure TCertificateData.SetString(AIndex: Integer; AValue: String);
  120. begin
  121. FStrData[AIndex]:=aValue;
  122. end;
  123. constructor TCertificateData.Create;
  124. Var
  125. I : Integer;
  126. begin
  127. CipherList:='DEFAULT';
  128. HostName:='localhost';
  129. For I:=0 to SSLDataCount do
  130. FCertData[i]:=TSSLData.Create;
  131. end;
  132. destructor TCertificateData.Destroy;
  133. Var
  134. I : Integer;
  135. begin
  136. For I:=0 to SSLDataCount do
  137. FreeAndNil(FCertData[i]);
  138. inherited Destroy;
  139. end;
  140. procedure TCertificateData.Assign(Source: TPersistent);
  141. Var
  142. CD : TCertificateData;
  143. I : Integer;
  144. begin
  145. if Source is TCertificateData then
  146. begin
  147. CD:=Source as TCertificateData;
  148. For I:=0 to StrDataCount do
  149. FStrData[i]:=CD.FStrData[i];
  150. For I:=0 to SSLDataCount do
  151. FCertData[i].Assign(CD.FCertData[i])
  152. end
  153. else
  154. inherited Assign(Source);
  155. end;
  156. function TCertificateData.NeedCertificateData: Boolean;
  157. begin
  158. Result:=Certificate.Empty and PFX.Empty;
  159. end;
  160. function TX509Certificate.GetKeySize: Integer;
  161. begin
  162. Result:=FKeySize;
  163. if Result=0 then
  164. Result:=1024;
  165. end;
  166. function TX509Certificate.GetValidFrom: TDateTime;
  167. begin
  168. Result:=FValidFrom;
  169. If Result=0 then
  170. Result:=Date-1;
  171. end;
  172. function TX509Certificate.GetValidTo: TDateTime;
  173. begin
  174. Result:=FValidTo;
  175. If Result=0 then
  176. Result:=Date+31;
  177. end;
  178. function TX509Certificate.GetVersion: Integer;
  179. begin
  180. Result:=FVersion;
  181. if FVersion=0 then
  182. FVersion:=1;
  183. end;
  184. function TX509Certificate.GetRealSerial: Integer;
  185. begin
  186. Result:=FSerial;
  187. if Result=0 then
  188. Result:=10; // MinutesBetween(Now,EncodeDate(2019,1,1));
  189. end;
  190. procedure TX509Certificate.CreateCertificateAndKey(var aCertificate, aKey: TBytes);
  191. Var
  192. CK : TCertAndKey;
  193. begin
  194. CK:=CreateCertificateAndKey;
  195. aCertificate:=CK.Certificate;
  196. aKey:=CK.PrivateKey;
  197. end;
  198. end.