-
-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathCertificateExample.pas
More file actions
391 lines (358 loc) · 14.4 KB
/
CertificateExample.pas
File metadata and controls
391 lines (358 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
{ *********************************************************************************** }
{ * CryptoLib Library * }
{ * Author - Ugochukwu Mmaduekwe * }
{ * Github Repository <https://github.com/Xor-el> * }
{ * * }
{ * Distributed under the MIT software license, see the accompanying file LICENSE * }
{ * or visit http://www.opensource.org/licenses/mit-license.php. * }
{ * * }
{ * Acknowledgements: * }
{ * * }
{ * Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring * }
{ * the development of this library * }
{ * ******************************************************************************* * }
(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
unit CertificateExample;
interface
{$IFDEF FPC}
{$MODE DELPHI}
{$HINTS OFF}
{$WARNINGS OFF}
{$ENDIF FPC}
uses
SysUtils,
Rtti,
DateUtils,
ClpBigInteger,
ClpDsaParameters,
ClpDsaGenerators,
ClpECParameters,
ClpIECParameters,
ClpIAsn1Objects,
ClpSecObjectIdentifiers,
ClpX509NameBuilder,
ClpIX509NameBuilder,
ClpX509Generators,
ClpIX509Asn1Objects,
ClpX509Asn1Objects,
ClpIX509Asn1Generators,
ClpX509Asn1Generators,
ClpAsn1SignatureFactory,
ClpX509CrlParser,
ClpIX509CrlParser,
ClpIX509Certificate,
ClpIX509Crl,
ClpIX509Generators,
ClpX509Certificate,
ClpX509ExtensionUtilities,
ClpSubjectPublicKeyInfoFactory,
ClpPkcs10CertificationRequestBuilder,
ClpIPkcs10CertificationRequestBuilder,
ClpIPkcs10CertificationRequest,
ClpIAsymmetricCipherKeyPair,
ClpIAsymmetricKeyParameter,
ClpISignatureFactory,
ClpCryptoLibTypes,
ClpIX509CrlEntry,
ClpIX9ECAsn1Objects,
ClpDateTimeHelper,
ExampleBase;
type
TCertificateArtifactsPem = record
PrivateKeyPem: string;
CertificateRequestPem: string;
CertificatePem: string;
CrlPem: string;
end;
TCertificateExample = class(TExampleBase)
public
procedure Run; override;
private
procedure GetValidityUtc(AValidDays: Integer; out ANotBefore, ANotAfter: TDateTime);
function BuildBasicEndEntityExtensions(const APublicKey: IAsymmetricKeyParameter): IX509Extensions;
procedure LogCrlSummary(const ACrl: IX509Crl);
procedure RunCrlImportVerify(const ACrlEncoded: TBytes;
const AIssuerPublicKey: IAsymmetricKeyParameter);
procedure RunCrlCreateExportVerifyRsa(const ASignatureAlgorithm: string);
procedure RunCrlCreateExportVerifyEc(const ACurveName: string;
const ASignatureAlgorithm: string);
procedure RunCertificateArtifactsRsa(const ASignatureAlgorithm: string);
procedure RunCertificateArtifactsEc(const ACurveName: string;
const ASignatureAlgorithm: string);
function BuildX509NameSubject: IX509Name;
function CreateCrl(const AKeyPair: IAsymmetricCipherKeyPair;
const AIssuer: IX509Name; const ASignatureAlgorithm: String): IX509Crl;
function CreateCertificationRequest(const AKeyPair: IAsymmetricCipherKeyPair;
const ASubject: IX509Name; const ASignatureAlgorithm: String): IPkcs10CertificationRequest;
function CreateSelfSignedCertificate(const AKeyPair: IAsymmetricCipherKeyPair;
const ASubject: IX509Name; const ASignatureAlgorithm: String;
AValidDays: Integer): IX509Certificate;
function CreateCertificateArtifacts(const AKeyPair: IAsymmetricCipherKeyPair;
const ASubject: IX509Name; const ASignatureAlgorithm: String;
AValidDaysCert: Integer): TCertificateArtifactsPem;
end;
implementation
procedure TCertificateExample.GetValidityUtc(AValidDays: Integer; out ANotBefore, ANotAfter: TDateTime);
begin
ANotBefore := Now.ToUniversalTime();
ANotAfter := IncSecond(ANotBefore, AValidDays * 86400);
end;
function TCertificateExample.BuildBasicEndEntityExtensions(const APublicKey: IAsymmetricKeyParameter): IX509Extensions;
var
LExtGen: IX509ExtensionsGenerator;
begin
LExtGen := TX509ExtensionsGenerator.Create();
LExtGen.AddExtension(TX509Extensions.BasicConstraints, True,
TBasicConstraints.Create(False) as IBasicConstraints);
LExtGen.AddExtension(TX509Extensions.KeyUsage, True,
TKeyUsage.Create(TKeyUsage.DigitalSignature or TKeyUsage.KeyEncipherment) as IKeyUsage);
Result := LExtGen.Generate();
end;
function TCertificateExample.CreateCrl(const AKeyPair: IAsymmetricCipherKeyPair;
const AIssuer: IX509Name; const ASignatureAlgorithm: String): IX509Crl;
var
LCrlGen: IX509V2CrlGenerator;
LThisUpdate, LNextUpdate: TDateTime;
begin
GetValidityUtc(1, LThisUpdate, LNextUpdate);
LCrlGen := TX509V2CrlGenerator.Create();
LCrlGen.SetIssuerDN(AIssuer);
LCrlGen.SetThisUpdateUtc(LThisUpdate);
LCrlGen.SetNextUpdateUtc(LNextUpdate);
LCrlGen.AddCrlEntryUtc(TBigInteger.One, LThisUpdate, 0);
LCrlGen.AddExtension(TX509Extensions.AuthorityKeyIdentifier, False,
TX509ExtensionUtilities.CreateAuthorityKeyIdentifier(
TSubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(AKeyPair.Public)));
Result := LCrlGen.Generate(TAsn1SignatureFactory.Create(ASignatureAlgorithm,
AKeyPair.Private, nil) as ISignatureFactory);
end;
procedure TCertificateExample.LogCrlSummary(const ACrl: IX509Crl);
var
LRevoked: TCryptoLibGenericArray<IX509CrlEntry>;
LRevokedCount: Int32;
begin
if ACrl = nil then
Exit;
LRevoked := ACrl.GetRevokedCertificates();
if LRevoked <> nil then
LRevokedCount := System.Length(LRevoked)
else
LRevokedCount := 0;
Logger.LogInformation('CRL issuer CN: {0}, revoked count: {1}',
[ACrl.GetIssuerDN().ToString(TX509Name.CN), IntToStr(LRevokedCount)]);
end;
procedure TCertificateExample.RunCrlImportVerify(const ACrlEncoded: TBytes;
const AIssuerPublicKey: IAsymmetricKeyParameter);
var
LParser: IX509CrlParser;
LCrl: IX509Crl;
begin
Logger.LogInformation('--- Certificate example: CRL import and verify ---', []);
if (ACrlEncoded = nil) or (System.Length(ACrlEncoded) = 0) then
begin
Logger.LogWarning('No CRL bytes to parse.', []);
Exit;
end;
LParser := TX509CrlParser.Create();
LCrl := LParser.ReadCrl(ACrlEncoded);
if LCrl = nil then
begin
Logger.LogError('Failed to parse CRL.', []);
Exit;
end;
try
LCrl.Verify(AIssuerPublicKey);
Logger.LogInformation('CRL signature verification passed.', []);
except
on E: Exception do
begin
Logger.LogError('CRL verify failed: {0}', [E.Message]);
Exit;
end;
end;
LogCrlSummary(LCrl);
Logger.LogInformation('CRL this update: {0}{1}',
[FormatDateTime('yyyy-mm-dd hh:nn:ss', LCrl.ThisUpdate), sLineBreak]);
end;
function TCertificateExample.BuildX509NameSubject: IX509Name;
var
LBuilder: IX509NameBuilder;
begin
LBuilder := TX509NameBuilder.Create();
Result := LBuilder
.AddCommonName('CryptoLib')
.AddCountry('NG')
.AddOrganization('CryptoLib4Pascal')
.AddLocality('Alausa')
.AddState('Lagos')
.AddEmailAddress('feedback-crypto@cryptolib4pascal.org')
.Build();
end;
function TCertificateExample.CreateCertificationRequest(const AKeyPair: IAsymmetricCipherKeyPair;
const ASubject: IX509Name; const ASignatureAlgorithm: String): IPkcs10CertificationRequest;
var
LBuilder: IPkcs10CertificationRequestBuilder;
LDnsName: IGeneralName;
LSanNames: IGeneralNames;
LExtensions: IX509Extensions;
begin
LExtensions := BuildBasicEndEntityExtensions(AKeyPair.Public);
LDnsName := TGeneralName.Create(TGeneralName.DnsName, 'cryptolib4pascal.example.com');
LSanNames := TGeneralNames.Create(LDnsName);
LBuilder := TPkcs10CertificationRequestBuilder.Create();
Result := LBuilder
.SetSubject(ASubject)
.SetKeyPair(AKeyPair)
.SetSignatureAlgorithm(ASignatureAlgorithm)
.AddExtensions(LExtensions)
.AddExtension(TX509Extensions.SubjectAlternativeName, False, LSanNames)
.AddSubjectKeyIdentifier(False)
.Build();
end;
function TCertificateExample.CreateSelfSignedCertificate(const AKeyPair: IAsymmetricCipherKeyPair;
const ASubject: IX509Name; const ASignatureAlgorithm: String;
AValidDays: Integer): IX509Certificate;
var
LCertGen: IX509V3CertificateGenerator;
LNotBefore, LNotAfter: TDateTime;
LExtensions: IX509Extensions;
begin
GetValidityUtc(AValidDays, LNotBefore, LNotAfter);
LCertGen := TX509V3CertificateGenerator.Create();
LCertGen.SetSerialNumber(TBigInteger.One);
LCertGen.SetIssuerDN(ASubject);
LCertGen.SetSubjectDN(ASubject);
LCertGen.SetNotBeforeUtc(LNotBefore);
LCertGen.SetNotAfterUtc(LNotAfter);
LCertGen.SetPublicKey(AKeyPair.Public);
LExtensions := BuildBasicEndEntityExtensions(AKeyPair.Public);
LCertGen.AddExtensions(LExtensions);
LCertGen.AddExtension(TX509Extensions.SubjectKeyIdentifier, False,
TX509ExtensionUtilities.CreateSubjectKeyIdentifier(AKeyPair.Public));
LCertGen.AddExtension(TX509Extensions.AuthorityKeyIdentifier, False,
TX509ExtensionUtilities.CreateAuthorityKeyIdentifier(AKeyPair.Public));
Result := LCertGen.Generate(TAsn1SignatureFactory.Create(ASignatureAlgorithm,
AKeyPair.Private, nil) as ISignatureFactory);
end;
function TCertificateExample.CreateCertificateArtifacts(const AKeyPair: IAsymmetricCipherKeyPair;
const ASubject: IX509Name; const ASignatureAlgorithm: String;
AValidDaysCert: Integer): TCertificateArtifactsPem;
var
LCrl: IX509Crl;
LReq: IPkcs10CertificationRequest;
LCert: IX509Certificate;
begin
Result.PrivateKeyPem := ExportToPem(TValue.From<IAsymmetricKeyParameter>(AKeyPair.Private));
Result.CertificateRequestPem := '';
Result.CertificatePem := '';
Result.CrlPem := '';
LCrl := CreateCrl(AKeyPair, ASubject, ASignatureAlgorithm);
Result.CrlPem := ExportToPem(TValue.From<IX509Crl>(LCrl));
LReq := CreateCertificationRequest(AKeyPair, ASubject, ASignatureAlgorithm);
Result.CertificateRequestPem := ExportToPem(TValue.From<IPkcs10CertificationRequest>(LReq));
LCert := CreateSelfSignedCertificate(AKeyPair, ASubject, ASignatureAlgorithm, AValidDaysCert);
Result.CertificatePem := ExportToPem(TValue.From<IX509Certificate>(LCert));
end;
procedure TCertificateExample.RunCrlCreateExportVerifyRsa(const ASignatureAlgorithm: string);
var
LKp: IAsymmetricCipherKeyPair;
LSubject: IX509Name;
LCrl: IX509Crl;
LCrlPem: string;
begin
Logger.LogInformation('--- CRL: create, export to PEM, import and verify (RSA {0}) ---', [ASignatureAlgorithm]);
LKp := GenerateRsaKeyPair();
LSubject := BuildX509NameSubject();
LCrl := CreateCrl(LKp, LSubject, ASignatureAlgorithm);
LCrlPem := ExportToPem(TValue.From<IX509Crl>(LCrl));
Logger.LogInformation('CRL PEM:{0}{1}', [sLineBreak, LCrlPem]);
RunCrlImportVerify(LCrl.GetEncoded(), LKp.Public);
end;
procedure TCertificateExample.RunCrlCreateExportVerifyEc(const ACurveName: string;
const ASignatureAlgorithm: string);
var
LKp: IAsymmetricCipherKeyPair;
LDomainParams: IECDomainParameters;
LSubject: IX509Name;
LCrl: IX509Crl;
LCrlPem: string;
begin
Logger.LogInformation('--- CRL: create, export to PEM, import and verify (EC {0} {1}) ---', [ACurveName, ASignatureAlgorithm]);
try
LDomainParams := TECDomainParameters.LookupName(ACurveName);
except
on E: EArgumentCryptoLibException do
begin
Logger.LogWarning('Curve "{0}" not found: {1}', [ACurveName, E.Message]);
Exit;
end;
on E: EArgumentNilCryptoLibException do
begin
Logger.LogWarning('Curve name empty.', []);
Exit;
end;
end;
LKp := GenerateEcKeyPair(LDomainParams);
LSubject := BuildX509NameSubject();
LCrl := CreateCrl(LKp, LSubject, ASignatureAlgorithm);
LCrlPem := ExportToPem(TValue.From<IX509Crl>(LCrl));
Logger.LogInformation('CRL PEM:{0}{1}', [sLineBreak, LCrlPem]);
RunCrlImportVerify(LCrl.GetEncoded(), LKp.Public);
end;
procedure TCertificateExample.RunCertificateArtifactsRsa(const ASignatureAlgorithm: string);
var
LKp: IAsymmetricCipherKeyPair;
LSubject: IX509Name;
LArtifacts: TCertificateArtifactsPem;
begin
Logger.LogInformation('--- Certificate artifacts: RSA ({0}) ---', [ASignatureAlgorithm]);
LKp := GenerateRsaKeyPair();
LSubject := BuildX509NameSubject();
LArtifacts := CreateCertificateArtifacts(LKp, LSubject, ASignatureAlgorithm, 365);
Logger.LogInformation('Private key PEM:{0}{1}', [sLineBreak, LArtifacts.PrivateKeyPem]);
Logger.LogInformation('Certificate request PEM:{0}{1}', [sLineBreak, LArtifacts.CertificateRequestPem]);
Logger.LogInformation('Certificate PEM:{0}{1}', [sLineBreak, LArtifacts.CertificatePem]);
Logger.LogInformation('CRL PEM:{0}{1}', [sLineBreak, LArtifacts.CrlPem]);
end;
procedure TCertificateExample.RunCertificateArtifactsEc(const ACurveName: string;
const ASignatureAlgorithm: string);
var
LKp: IAsymmetricCipherKeyPair;
LDomainParams: IECDomainParameters;
LSubject: IX509Name;
LArtifacts: TCertificateArtifactsPem;
begin
Logger.LogInformation('--- Certificate artifacts: EC {0} ({1}) ---', [ACurveName, ASignatureAlgorithm]);
try
LDomainParams := TECDomainParameters.LookupName(ACurveName);
except
on E: EArgumentCryptoLibException do
begin
Logger.LogWarning('Curve "{0}" not found: {1}', [ACurveName, E.Message]);
Exit;
end;
on E: EArgumentNilCryptoLibException do
begin
Logger.LogWarning('Curve name empty.', []);
Exit;
end;
end;
LKp := GenerateEcKeyPair(LDomainParams);
LSubject := BuildX509NameSubject();
LArtifacts := CreateCertificateArtifacts(LKp, LSubject, ASignatureAlgorithm, 365);
Logger.LogInformation('Private key PEM:{0}{1}', [sLineBreak, LArtifacts.PrivateKeyPem]);
Logger.LogInformation('Certificate request PEM:{0}{1}', [sLineBreak, LArtifacts.CertificateRequestPem]);
Logger.LogInformation('Certificate PEM:{0}{1}', [sLineBreak, LArtifacts.CertificatePem]);
Logger.LogInformation('CRL PEM:{0}{1}', [sLineBreak, LArtifacts.CrlPem]);
end;
procedure TCertificateExample.Run;
begin
LogWithLineBreak('--- Certificate example: CRL, CSR, self-signed cert (all to PEM) ---');
RunCertificateArtifactsRsa('SHA256WithRSAEncryption');
RunCertificateArtifactsEc('P-256', 'SHA256withECDSA');
RunCertificateArtifactsEc('secp256k1', 'SHA256withECDSA');
RunCrlCreateExportVerifyRsa('SHA256WithRSAEncryption');
RunCrlCreateExportVerifyEc('P-256', 'SHA256withECDSA');
RunCrlCreateExportVerifyEc('secp256k1', 'SHA256withECDSA');
end;
end.