-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathCertificateExample.pas
More file actions
183 lines (164 loc) · 6.74 KB
/
Copy pathCertificateExample.pas
File metadata and controls
183 lines (164 loc) · 6.74 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
{ *********************************************************************************** }
{ * 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,
ClpIX509Asn1Objects,
ClpX509Asn1Objects,
ClpIX509Crl,
ClpIX509CrlEntry,
ClpIX509Certificate,
ClpIAsymmetricCipherKeyPair,
ClpIAsymmetricKeyParameter,
ClpX509Certificate,
ClpCryptoLibTypes,
ClpStringUtilities,
ExampleBase,
AsymmetricExampleUtilities,
KeyEncodingExampleUtilities,
CertificateExampleUtilities;
type
TCertificateExample = class(TExampleBase)
private
function BuildDemoSubject: IX509Name;
function IsRsaKeySpec(const AKeySpec: string): Boolean;
procedure LogCrlSummary(const ACrl: IX509Crl);
procedure LogCertificateArtifactsPem(const AArtifacts: TCertificateArtifactsPem);
procedure RunCrlImportVerify(const ACrlEncoded: TBytes;
const AIssuerPublicKey: IAsymmetricKeyParameter);
procedure RunCrlCreateExportVerify(const AKeySpec, ASignatureAlgorithm: string);
procedure RunCertificateArtifacts(const AKeySpec, ASignatureAlgorithm: string;
AValidDaysCert: Integer);
procedure RunCertificateDemos;
public
procedure Run; override;
end;
implementation
function TCertificateExample.BuildDemoSubject: IX509Name;
begin
Result := TCertificateExampleUtilities.BuildX509Name(
'CryptoLib', 'NG', 'CryptoLib4Pascal', 'Alausa', 'Lagos',
'feedback-crypto@cryptolib4pascal.org');
end;
function TCertificateExample.IsRsaKeySpec(const AKeySpec: string): Boolean;
begin
Result := SameText(AKeySpec, 'RSA') or TStringUtilities.StartsWith(AKeySpec, 'RSA-', True);
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.LogCertificateArtifactsPem(const AArtifacts: TCertificateArtifactsPem);
begin
Logger.LogInformation('Private key PEM:{0}{1}', [sLineBreak, AArtifacts.PrivateKeyPem]);
Logger.LogInformation('Certificate request PEM:{0}{1}', [sLineBreak, AArtifacts.CertificateRequestPem]);
Logger.LogInformation('Certificate PEM:{0}{1}', [sLineBreak, AArtifacts.CertificatePem]);
Logger.LogInformation('CRL PEM:{0}{1}', [sLineBreak, AArtifacts.CrlPem]);
end;
procedure TCertificateExample.RunCrlImportVerify(const ACrlEncoded: TBytes;
const AIssuerPublicKey: IAsymmetricKeyParameter);
var
LCrl: IX509Crl;
begin
Logger.LogInformation('--- Certificate example: CRL import and verify ---', []);
LCrl := TCertificateExampleUtilities.ParseCrl(ACrlEncoded);
if LCrl = nil then
begin
Logger.LogError('Failed to parse CRL.', []);
Exit;
end;
if TCertificateExampleUtilities.VerifyCrl(LCrl, AIssuerPublicKey) then
Logger.LogInformation('CRL signature verification passed.', [])
else
begin
Logger.LogError('CRL verify failed.', []);
Exit;
end;
LogCrlSummary(LCrl);
Logger.LogInformation('CRL this update: {0}{1}',
[FormatDateTime('yyyy-mm-dd hh:nn:ss', LCrl.ThisUpdate), sLineBreak]);
end;
procedure TCertificateExample.RunCrlCreateExportVerify(const AKeySpec, ASignatureAlgorithm: string);
var
LKp: IAsymmetricCipherKeyPair;
LSubject: IX509Name;
LCrl: IX509Crl;
LCrlPem: string;
begin
if IsRsaKeySpec(AKeySpec) then
Logger.LogInformation('--- CRL: create, export to PEM, import and verify (RSA {0}) ---', [ASignatureAlgorithm])
else
Logger.LogInformation('--- CRL: create, export to PEM, import and verify (EC {0} {1}) ---', [AKeySpec, ASignatureAlgorithm]);
if not TAsymmetricExampleUtilities.TryGenerateKeyPair(AKeySpec, LKp) then
Exit;
LSubject := BuildDemoSubject();
LCrl := TCertificateExampleUtilities.CreateCrl(LKp, LSubject, ASignatureAlgorithm);
LCrlPem := TKeyEncodingExampleUtilities.ExportToPem(TValue.From<IX509Crl>(LCrl));
Logger.LogInformation('CRL PEM:{0}{1}', [sLineBreak, LCrlPem]);
RunCrlImportVerify(LCrl.GetEncoded(), LKp.Public);
end;
procedure TCertificateExample.RunCertificateArtifacts(const AKeySpec, ASignatureAlgorithm: string;
AValidDaysCert: Integer);
var
LKp: IAsymmetricCipherKeyPair;
LSubject: IX509Name;
LArtifacts: TCertificateArtifactsPem;
const
DemoDnsSan = 'cryptolib4pascal.example.com';
begin
if IsRsaKeySpec(AKeySpec) then
Logger.LogInformation('--- Certificate artifacts: RSA ({0}) ---', [ASignatureAlgorithm])
else
Logger.LogInformation('--- Certificate artifacts: EC {0} ({1}) ---', [AKeySpec, ASignatureAlgorithm]);
if not TAsymmetricExampleUtilities.TryGenerateKeyPair(AKeySpec, LKp) then
Exit;
LSubject := BuildDemoSubject();
LArtifacts := TCertificateExampleUtilities.CreateCertificateArtifacts(LKp, LSubject,
ASignatureAlgorithm, DemoDnsSan, AValidDaysCert);
LogCertificateArtifactsPem(LArtifacts);
end;
procedure TCertificateExample.RunCertificateDemos;
begin
LogWithLineBreak('--- Certificate example: CRL, CSR, self-signed cert (all to PEM) ---');
RunCertificateArtifacts('RSA', 'SHA256WithRSAEncryption', 365);
RunCertificateArtifacts('P-256', 'SHA256withECDSA', 365);
RunCertificateArtifacts('secp256k1', 'SHA256withECDSA', 365);
RunCrlCreateExportVerify('RSA', 'SHA256WithRSAEncryption');
RunCrlCreateExportVerify('P-256', 'SHA256withECDSA');
RunCrlCreateExportVerify('secp256k1', 'SHA256withECDSA');
end;
procedure TCertificateExample.Run;
begin
RunCertificateDemos;
end;
end.