-
Notifications
You must be signed in to change notification settings - Fork 1
/
uValidate.pas
198 lines (181 loc) · 5.84 KB
/
uValidate.pas
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
{
This form used to validate certificates
}
unit uValidate;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
SBConstants, SBX509, SBX509Ext, SBPKCS12, SBCustomCertStorage,
SBWinCertStorage, StdCtrls, ExtCtrls, ComCtrls, ImgList;
type
TfrmValidate = class(TForm)
tvPath: TTreeView;
pnlBottom: TPanel;
pnlValidity: TPanel;
lblCertificateState: TLabel;
memNotes: TMemo;
ilTree: TImageList;
btnOk: TButton;
private
{ Private declarations }
FTrustedStorages : array of TElCustomCertStorage;
FTrustedStorageNames : array of string;
FStorages : array of TElCustomCertStorage;
FStorageNames : array of string;
FCert : TElx509Certificate;
procedure DoValidateCertificate;
public
{ Public declarations }
class procedure ValidateCertificate(TrustedStorages,Storages : TStringList;
Cert : TElX509Certificate);
end;
implementation
uses frmMain, ExtensionEncoder, GenerateCert, SBUtils;
var
frmValidate: TfrmValidate;
{$R *.DFM}
{ TfrmValidate }
function GetCertDisplayName(Cert : TelX509Certificate) : string;
begin
try
Result := GetOIDValue(Cert.SubjectRDN, SB_CERT_OID_COMMON_NAME);
if Result = '' then
Result := GetOIDValue(Cert.SubjectRDN, SB_CERT_OID_ORGANIZATION);
except Result:=''; end;
end;
procedure TfrmValidate.DoValidateCertificate;
{
Returns : 0 - Trust unknown
1 - Not trusted
2 - Not valid
3 - Valid
}
function ValidateCert(Cert : TELx509Certificate) : boolean;
begin
Result := True;
if Cert.ValidTo < Date then Result := False;
end;
function TestCert(PrevCert : TElX509Certificate; var PrevNode : TTreeNode) : integer;
var i : integer;
CACert : TElX509Certificate;
CAIdx : integer;
begin
if not ValidateCert(PrevCert) then
begin
PrevNode := tvPath.Items.AddChild(PrevNode,GetCertDisplayName(PrevCert));
PrevNode.ImageIndex := 1;
PrevNode.SelectedIndex := 1;
PrevNode.Data := PrevCert;
memNotes.Text := PrevNode.Text + ' - not valid (expired)';
Result := 2;
exit;
end;
if PrevCert.SelfSigned then
begin
PrevNode := tvPath.Items.AddChild(PrevNode,GetCertDisplayName(PrevCert));
PrevNode.ImageIndex := 0;
PrevNode.SelectedIndex := 0;
PrevNode.Data := PrevCert;
memNotes.Text := PrevNode.Text + ' - self signed';
Result := 3;
exit;
end;
// not self signed
// Looking in trusted storages
for i:=0 to High(FTrustedStorages) - 1 do
begin
CAIdx:=FTrustedStorages[i].GetIssuerCertificate(PrevCert);
if CAIdx > -1 then
begin
CACert := FTrustedStorages[i].Certificates[CAIdx];
PrevNode := tvPath.Items.AddChild(PrevNode,GetCertDisplayName(CACert));
PrevNode.Data := CACert;
if ValidateCert(CACert) then
begin
PrevNode.ImageIndex := 0;
PrevNode.SelectedIndex := 0;
memNotes.Text := PrevNode.Text + ' - belongs to trusted storage ' +
FTrustedStorageNames[i];
PrevNode := tvPath.Items.AddChild(PrevNode,GetCertDisplayName(PrevCert));
PrevNode.ImageIndex := 0;
PrevNode.SelectedIndex := 0;
memNotes.Lines.Add(GetCertDisplayName(PrevCert));
Result := 3;
end else
begin
PrevNode.ImageIndex := 1;
PrevNode.SelectedIndex := 1;
memNotes.Text := PrevNode.Text + ' - belongs to trusted storage but not valid (expired)' +
FTrustedStorageNames[i];
PrevNode := tvPath.Items.AddChild(PrevNode,GetCertDisplayName(PrevCert));
PrevNode.ImageIndex := 1;
PrevNode.SelectedIndex := 1;
memNotes.Lines.Add(GetCertDisplayName(PrevCert));
Result := 2;
end;
exit;
end;
end;
// Looking in untrusted storages
for i:=0 to High(FStorages) - 1 do
begin
CAIdx:=FStorages[i].GetIssuerCertificate(PrevCert);
if CAIdx > -1 then
begin
CACert := FTrustedStorages[i].Certificates[CAIdx];
Result := TestCert(CACert, PrevNode);
PrevNode := tvPath.Items.AddChild(PrevNode,GetCertDisplayName(PrevCert));
PrevNode.ImageIndex := 0;
PrevNode.SelectedIndex := 0;
PrevNode.Data := CACert;
memNotes.Text := PrevNode.Text + ' - belongs to untrusted storage';
exit;
end;
end;
// No path:(
PrevNode := tvPath.Items.AddChild(PrevNode,GetCertDisplayName(PrevCert));
PrevNode.ImageIndex := 1;
PrevNode.SelectedIndex := 1;
PrevNode.Data := PrevCert;
memNotes.Text := PrevNode.Text + ' - trust unknown';
Result:=0;
end;
var TN : TTreeNode;
begin
TN := nil;
case TestCert(FCert, TN) of
0 : lblCertificateState.Caption := 'Trust unknown';
1 : lblCertificateState.Caption := 'Not trusted';
2 : lblCertificateState.Caption := 'Not valid';
3 : lblCertificateState.Caption := 'Valid';
end;
if TN <> nil then TN.Selected := True;
ShowModal;
end;
class procedure TfrmValidate.ValidateCertificate(TrustedStorages,
Storages: TStringList; Cert: TElX509Certificate);
var i : integer;
begin
frmValidate := TfrmValidate.Create(Application);
with frmValidate do
begin
FCert := Cert;
SetLength(FTrustedStorages, TrustedStorages.Count);
SetLength(FTrustedStorageNames, TrustedStorages.Count);
for i:=0 to TrustedStorages.Count - 1 do
begin
FTrustedStorages[i] := TElCustomCertStorage(TrustedStorages.Objects[i]);
FTrustedStorageNames[i] := TrustedStorages[i];
end;
SetLength(FStorages, Storages.Count);
SetLength(FStorageNames, Storages.Count);
for i:=0 to Storages.Count - 1 do
begin
FStorages[i] := TElCustomCertStorage(Storages.Objects[i]);
FStorageNames[i] := Storages[i];
end;
DoValidateCertificate;
Free;
end;
end;
end.