-
Notifications
You must be signed in to change notification settings - Fork 1
/
frmMain.pas
1123 lines (1053 loc) · 38.9 KB
/
frmMain.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit frmMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, SBConstants, SBX509, SBX509Ext, SBPKCS12, SBCustomCertStorage, SBWinCertStorage,
Grids, Menus, GenerateCert, ToolWin, ExtCtrls, Buttons, SelectStorage, ComCtrls,
ActnList, ImgList, SBRDN, SBMessages;
type
TSertX509 = class(TForm)
OpenDlgCert: TOpenDialog;
saveDlgCert: TSaveDialog;
sbStatus: TStatusBar;
OpenDlgStorage: TOpenDialog;
SaveDlgStorage: TSaveDialog;
mmMain: TMainMenu;
mmiStorage: TMenuItem;
mmiNewMemStorage: TMenuItem;
mmiMountStorage: TMenuItem;
mmiSaveStorage: TMenuItem;
mmiSaveStorageAs: TMenuItem;
mmiImportFromWinStorage: TMenuItem;
mmiCertificate: TMenuItem;
mmiRemoveCertificate: TMenuItem;
mmiNewCertificate: TMenuItem;
mmiSaveCertificate: TMenuItem;
mmiLoadCertificate: TMenuItem;
mmiValidate: TMenuItem;
mmiLoadPrivateKey: TMenuItem;
OpenDlgPvtKey: TOpenDialog;
mmiUnmountStorage: TMenuItem;
pmiMain: TPopupMenu;
pmiNewMemoryStorage: TMenuItem;
pmiMountStorage: TMenuItem;
pmiSaveStorage: TMenuItem;
pmiSaveStorageAs: TMenuItem;
pmiImportFromWinStorage: TMenuItem;
pmiUnmountStorage: TMenuItem;
pmiNewCertificate: TMenuItem;
pmiRemoveCertificate: TMenuItem;
pmioLoadCertificate: TMenuItem;
pmiSaveCertificate: TMenuItem;
pmiValidate: TMenuItem;
pmiLoadPrivateKey: TMenuItem;
mmiMoveToStorage: TMenuItem;
mmiCopyToStorage: TMenuItem;
pmiMoveToStorage: TMenuItem;
pmiCopyToStorage: TMenuItem;
mmiExportToMemoryStorage: TMenuItem;
pmiExportToMemoryStorage: TMenuItem;
mmiNewFileStorage: TMenuItem;
pmiNewFileStorage: TMenuItem;
mmiSeparator1: TMenuItem;
mmiSeparator2: TMenuItem;
mmiSeparator5: TMenuItem;
mmiSeparator6: TMenuItem;
pmiSeparator1: TMenuItem;
pmiSeparator2: TMenuItem;
pmiSeparator3: TMenuItem;
pmiSeparator5: TMenuItem;
pmiSeparator6: TMenuItem;
mmiSeparator3: TMenuItem;
mmiExit: TMenuItem;
mmiSeparator4: TMenuItem;
mmiCreateCSR: TMenuItem;
pmiSeparator4: TMenuItem;
pmiCreateCSR: TMenuItem;
alMain: TActionList;
acNewMemStorage: TAction;
acNewFileStorage: TAction;
acSaveStorage: TAction;
acSaveStorageAs: TAction;
acMountStorage: TAction;
acUnmountStorage: TAction;
acImportFromWinStorage: TAction;
acExportToMemoryStorage: TAction;
acNewCertificate: TAction;
acLoadCertificate: TAction;
acSaveCertificate: TAction;
acRemoveCertificate: TAction;
acCreateCSR: TAction;
acValidate: TAction;
acLoadPrivateKey: TAction;
acMoveToStorage: TAction;
acCopyToStorage: TAction;
treeCert: TTreeView;
ilTree: TImageList;
pcInfo: TPageControl;
tsNoInfo: TTabSheet;
tsCertificate: TTabSheet;
imgCertificate: TImage;
lblIssuedByLabel: TLabel;
lnlIssuedToLabel: TLabel;
lblIssuedBy: TLabel;
lblIssuedTo: TLabel;
memAdditional: TMemo;
lblValidFromLabel: TLabel;
lblValidFrom: TLabel;
lblValidToLabel: TLabel;
lblValidTo: TLabel;
lvCertProperties: TListView;
bvlCertInfoSeparator: TBevel;
splTree: TSplitter;
acTrusted: TAction;
pmiTrusted: TMenuItem;
mmiTrusted: TMenuItem;
procedure FormDestroy(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure mmiExitClick(Sender: TObject);
procedure treeCertChange(Sender: TObject; Node: TTreeNode);
procedure treeCertDeletion(Sender: TObject; Node: TTreeNode);
procedure acNewMemStorageExecute(Sender: TObject);
procedure acNewFileStorageExecute(Sender: TObject);
procedure acSaveStorageExecute(Sender: TObject);
procedure acSaveStorageAsExecute(Sender: TObject);
procedure acMountStorageExecute(Sender: TObject);
procedure acUnmountStorageExecute(Sender: TObject);
procedure acImportFromWinStorageExecute(Sender: TObject);
procedure acExportToMemoryStorageExecute(Sender: TObject);
procedure acNewCertificateExecute(Sender: TObject);
procedure acLoadCertificateExecute(Sender: TObject);
procedure acSaveCertificateExecute(Sender: TObject);
procedure acRemoveCertificateExecute(Sender: TObject);
procedure acCreateCSRExecute(Sender: TObject);
procedure acValidateExecute(Sender: TObject);
procedure acLoadPrivateKeyExecute(Sender: TObject);
procedure acMoveToStorageExecute(Sender: TObject);
procedure acCopyToStorageExecute(Sender: TObject);
procedure mmiAboutClick(Sender: TObject);
procedure lvCertPropertiesChange(Sender: TObject; Item: TListItem;
Change: TItemChange);
procedure acTrustedExecute(Sender: TObject);
private
protected
function DoCopyToStorage: Boolean;
procedure LoadStorage(ParentNode: TTreeNode; StorageName: string;
Storage: TElCustomCertStorage);
public
procedure DisplayCertificateInfo(Cert : TElX509Certificate);
end;
var
SertX509 : TSertX509;
Storage : TElCustomCertStorage;
WinStorage1, WinStorage2, WinStorage3, WinStorage4 : TElWinCertStorage;
GlobalCert : TElX509Certificate;
GlobalPrivateKey : array[0..4095] of byte;
PrivateKeySize : Word;
StorageList : TList;
CurrStorageName : TStringList;
SelectedIndex : integer;
{$J+}
const StorageNumber : integer = 0;
implementation
uses
SBPEM, SBUtils, AboutForm, ExtensionEncoder, uValidate;
{$R *.DFM}
//--------------- Helper function ------------------
// Get algorithm string
function GetEncryptionAlghorithm(Algorithm : integer) : string;
begin
case (Algorithm) of
SB_CERT_ALGORITHM_ID_RSA_ENCRYPTION : Result := 'RSA';
SB_CERT_ALGORITHM_MD2_RSA_ENCRYPTION : Result := 'MD2 with RSA';
SB_CERT_ALGORITHM_MD5_RSA_ENCRYPTION : Result := 'MD5 with RSA';
SB_CERT_ALGORITHM_SHA1_RSA_ENCRYPTION : Result := 'SHA1 with RSA';
SB_CERT_ALGORITHM_ID_DSA : Result := 'DSA';
SB_CERT_ALGORITHM_ID_DSA_SHA1 : Result := 'DSA with SHA1';
SB_CERT_ALGORITHM_DH_PUBLIC : Result := 'DH';
SB_CERT_ALGORITHM_SHA224_RSA_ENCRYPTION : Result := 'SHA224 with RSA';
SB_CERT_ALGORITHM_SHA256_RSA_ENCRYPTION : Result := 'SHA256 with RSA';
SB_CERT_ALGORITHM_SHA384_RSA_ENCRYPTION : Result := 'SHA384 with RSA';
SB_CERT_ALGORITHM_SHA512_RSA_ENCRYPTION : Result := 'SHA512 with RSA';
SB_CERT_ALGORITHM_ID_RSAPSS : Result := 'RSA-PSS';
SB_CERT_ALGORITHM_ID_RSAOAEP : Result := 'RSA-OAEP';
SB_CERT_ALGORITHM_UNKNOWN : Result := 'Unknown';
end;
end;
//Get display name for certificate
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 TSertX509.LoadStorage(ParentNode : TTreeNode;StorageName : string;
Storage : TElCustomCertStorage);
var C : integer;
S : String;
Cert : TElX509Certificate;
StorageNode,TI : TTreeNode;
begin
treeCert.Items.BeginUpdate;
treeCert.Enabled := False;
try
// Add Storage node
StorageNode:=treeCert.Items.AddChildObject(ParentNode, StorageName, Storage);
StorageNode.ImageIndex:=2;
StorageNode.SelectedIndex:=2;
// Windows storages assumed as trusted
if Storage is TElWinCertStorage then
begin
StorageNode.ImageIndex:=6;
StorageNode.SelectedIndex:=6;
end;
StorageNode.Selected:=True;
StorageList.Add(Storage);
CurrStorageName.Add(StorageName);
// Add certificates
C := 0;
while C < Storage.Count do
begin
try
Cert := TElX509Certificate.Create(nil);
Cert.Assign(Storage.Certificates[C]);
S := GetCertDisplayName(Cert);
TI:=treeCert.Items.AddChildObject(StorageNode, S, Cert);
TI.ImageIndex:=3; TI.SelectedIndex:=3;
Inc(C);
sbStatus.Panels[0].Text := 'Loading Certificates ' + StorageName + '...'
+ IntToStr(C * 100 div Storage.Count) + '%';
Application.ProcessMessages;
except
on E : Exception do Application.ShowException(E);
end;
end;
finally
treeCert.Enabled := True;
treeCert.Items.EndUpdate;
end;
end;
procedure TSertX509.FormDestroy(Sender: TObject);
begin
treeCert.Items.Clear;
treeCert.Free;
WinStorage1.Free;
WinStorage2.Free;
WinStorage3.Free;
WinStorage4.Free;
StorageList.Free;
CurrStorageName.Free;
end;
procedure TSertX509.DisplayCertificateInfo(Cert : TElX509Certificate);
// Add new line to detail listview
procedure AddListValue(FieldName,Value : string;
Additional : string = '';ImageIndex : integer= 4);
var LI : TListItem;
begin
If (Trim(Value) = '') and (Trim(Additional)='') then exit;
LI:=lvCertProperties.Items.Add;
LI.Caption:=FieldName;
LI.ImageIndex:=ImageIndex;
LI.SubItems.Add(StringReplace(Value,#13#10,' ',[rfReplaceAll]));
If Additional<> '' then LI.SubItems.Add(Additional)
else LI.SubItems.Add(Value);
end;
function ConcateString(SrcSt,AddSt,SeparatorSt : string; AddToStart : boolean) : string;
begin
Result:='';
if SrcSt<>'' then begin
if AddToStart then Result:=AddSt + SeparatorSt + SrcSt
else Result:=SrcSt + SeparatorSt + AddSt;
end else Result:=AddSt;
end;
// Add RDN to list
procedure AddRDN(FieldName : string;Data : TElRelativeDistinguishedName);
var i : integer;
Value,Add,OIDString : string;
AddToStart,CommonAdded : boolean;
begin
Value:=''; Add:=''; CommonAdded:=False;
for i:=0 to Data.Count - 1 do
begin
OIDString := GetStringByOID(Data.OIDs[i]);
// Put common name as first. If no this - add Organization
AddToStart := AnsiUpperCase(OIDString) = 'COMMONNAME';
if AddToStart then CommonAdded:=True;
if not CommonAdded then
AddToStart := AnsiUpperCase(OIDString) = 'ORGANIZATION';
Add:=ConcateString(Add,OIDString + ' = ' + Data.Values[i],#13#10,AddToStart);
Value:=ConcateString(Value,Data.Values[i],',',AddToStart);
end;
AddListValue(FieldName,Value,Add);
end;
procedure DisplayExtensions(Cert : TelX509Certificate);
var i : integer;
// Add extension in a case that Value <>''
procedure AddExtension(Name : string; Ext : TElCustomExtension;
CheckValue : TSBCertificateExtension; ValueSt : string='');
begin
If not (CheckValue in Cert.Extensions.Included) then exit;
if ValueSt='' then
AddListValue(Name,BuildHexString(Ext.Value),'',5)
else AddListValue(Name,ValueSt,ValueSt,5);
end;
begin
// Extensions
AddExtension('AuthorityInformationAccess',Cert.Extensions.AuthorityInformationAccess,
ceAuthorityInformationAccess,GetAuthorityInformationAccess(Cert.Extensions.AuthorityInformationAccess));
AddExtension('Authority Key Identifier',Cert.Extensions.AuthorityKeyIdentifier,
ceAuthorityKeyIdentifier,GetAuthorityKeyIdentifierValue(Cert.Extensions.AuthorityKeyIdentifier));
AddExtension('Basic constraints',Cert.Extensions.BasicConstraints,
ceBasicConstraints,GetBasicConstraintValue(Cert.Extensions.BasicConstraints));
AddExtension('CertificatePolicies',Cert.Extensions.CertificatePolicies,
ceCertificatePolicies,GetCertificatePoliciesValue(Cert.Extensions.CertificatePolicies));
AddExtension('CommonName',Cert.Extensions.CommonName,
ceCommonName,Cert.Extensions.CommonName.Content);
AddExtension('CRLDistributionPoints',Cert.Extensions.CRLDistributionPoints,
ceCRLDistributionPoints,GetDistributionPointValue(Cert.Extensions.CRLDistributionPoints));
// Extended key usage
AddExtension('Extended key usage',Cert.Extensions.ExtendedKeyUsage,
ceExtendedKeyUsage,GetExtendedKeyUsageValue(Cert.Extensions.ExtendedKeyUsage));
AddExtension('IssuerAlternativeName',Cert.Extensions.IssuerAlternativeName,
ceIssuerAlternativeName,GetIssuerAlternativeNameValue(Cert.Extensions.IssuerAlternativeName));
// Key usage
AddExtension('Key usage',Cert.Extensions.KeyUsage,
ceKeyUsage, GetKeyUsageValue(Cert.Extensions.KeyUsage));
AddExtension('NameConstraints', Cert.Extensions.NameConstraints,
ceNameConstraints,GetNameConstraints(Cert.Extensions.NameConstraints));
AddExtension('NetscapeBaseURL', Cert.Extensions.NetscapeBaseURL,
ceNetscapeBaseURL,Cert.Extensions.NetscapeBaseURL.Content);
AddExtension('NetscapeCAPolicy', Cert.Extensions.NetscapeCAPolicy,
ceNetscapeCAPolicyURL, Cert.Extensions.NetscapeCAPolicy.Content);
AddExtension('NetscapeCARevokeURL',Cert.Extensions.NetscapeCARevokeURL,
ceNetscapeCARevokeURL,Cert.Extensions.NetscapeCARevokeURL.Content);
AddExtension('NetscapeCertType',Cert.Extensions.NetscapeCertType,
ceNetscapeCertType, GetNetscapeCertType(Cert.Extensions.NetscapeCertType));
AddExtension('NetscapeComment',Cert.Extensions.NetscapeComment,
ceNetscapeComment,Cert.Extensions.NetscapeComment.Content);
AddExtension('NetscapeRenewalURL',Cert.Extensions.NetscapeRenewalURL,
ceNetscapeRenewalURL,Cert.Extensions.NetscapeRenewalURL.Content);
AddExtension('NetscapeRevokeURL',Cert.Extensions.NetscapeRevokeURL,
ceNetscapeRevokeURL,Cert.Extensions.NetscapeRevokeURL.Content);
AddExtension('NetscapeServerName',Cert.Extensions.NetscapeServerName,
ceNetscapeServerName,Cert.Extensions.NetscapeServerName.Content);
AddExtension('PolicyConstraints',Cert.Extensions.PolicyConstraints,
cePolicyConstraints,GetPolicyConstraintsValue(Cert.Extensions.PolicyConstraints));
AddExtension('PolicyMappings',Cert.Extensions.PolicyMappings,
cePolicyMappings,GetPoliciesMappingValue(Cert.Extensions.PolicyMappings));
AddExtension('PrivateKeyUsagePeriod',Cert.Extensions.PrivateKeyUsagePeriod,
cePrivateKeyUsagePeriod,GetUsagePeriodValue(Cert.Extensions.PrivateKeyUsagePeriod));
AddExtension('SubjectAlternativeName',Cert.Extensions.SubjectAlternativeName,
ceSubjectAlternativeName,GetSubjectAltNameValue(Cert.Extensions.SubjectAlternativeName));
AddExtension('SubjectKeyIdentifier',Cert.Extensions.SubjectKeyIdentifier,
ceSubjectKeyIdentifier,BuildHexString(Cert.Extensions.SubjectKeyIdentifier.KeyIdentifier));
// Custom extensions
For i:=0 to Cert.Extensions.OtherCount -1 do
AddListValue(OIDToStr(Cert.Extensions.OtherExtensions[i].OID),
BuildHexString(Cert.Extensions.OtherExtensions[i].Value),'',5);
end;
var Par1, Par2, Par3, Par4 : string;
Sz1, Sz2, Sz3, Sz4 : integer;
begin
lvCertProperties.Items.Clear;
lvCertProperties.Items.BeginUpdate;
// printing ALL certificate properties
// Version
AddListValue('Version',IntToStr(Cert.Version));
// SerialNumber
AddListValue('Serial number',BuildHexString(Cert.SerialNumber));
// SignatureAlgorithm
AddListValue('Signature algorithm',GetEncryptionAlghorithm(Cert.SignatureAlgorithm));
// Issuer
AddRDN('Issuer',Cert.IssuerRDN);
// Use CommonName or Organization
lblIssuedBy.Caption:=Trim(Cert.IssuerName.CommonName);
if (lblIssuedBy.Caption = '') then lblIssuedBy.Caption:=Cert.IssuerName.Organization;
// IssuerUniqueID
AddListValue('IssuerUniqueID',BuildHexString(Cert.IssuerUniqueID));
// ValidFrom
lblValidFrom.Caption := DateToStr(Cert.ValidFrom);
AddListValue('Valid from',lblValidFrom.Caption,DateTimeToStr(Cert.ValidFrom));
// ValidTo
lblValidTo.Caption := DateToStr(Cert.ValidTo);
AddListValue('Valid to',lblValidTo.Caption,DateTimeToStr(Cert.ValidTo));
// Subject
AddRDN('Subject',Cert.SubjectRDN);
// Use CommonName or Organization
lblIssuedTo.Caption:=Trim(Cert.SubjectName.CommonName);
if (lblIssuedTo.Caption = '') then lblIssuedTo.Caption:=Cert.SubjectName.Organization;
// SubjectUniqueID
AddListValue('SubjectUniqueID',BuildHexString(Cert.SubjectUniqueID));
// CertificateSize
AddListValue('Size',IntToStr(Cert.CertificateSize));
// Signature
AddListValue('Signature',BuildHexString(Cert.Signature));
// SelfSigned
if (Cert.SelfSigned) then AddListValue('Self-signed','True')
else AddListValue('Self-signed','False');
if Cert.PublicKeyAlgorithm = SB_CERT_ALGORITHM_ID_RSA_ENCRYPTION then
begin
Sz1 := 0; Sz2:=0;
Cert.GetRSAParams(nil, Sz1, nil, Sz2);
SetLength(Par1, Sz1); SetLength(Par2, Sz2);
Cert.GetRSAParams(@Par1[1], Sz1, @Par2[1], Sz2);
Sz3 := Cert.GetPublicKeySize;
AddListValue('Public key','RSA (' + IntToStr(Sz3) + ')',
'RSAModulus = ' + BuildHexString(Par1) + #13#10 +
'RSAPublicKey = ' + BuildHexString(Par2));
end
else if Cert.PublicKeyAlgorithm = SB_CERT_ALGORITHM_ID_DSA then
begin
Sz1 := 0; Sz2:=0; Sz3:=0; Sz4:=0;
Cert.GetDSSParams(nil, Sz1, nil, Sz2, nil, Sz3, nil, Sz4);
SetLength(Par1, Sz1); SetLength(Par2, Sz2); SetLength(Par3, Sz3); SetLength(Par4, Sz4);
Cert.GetDSSParams(@Par1[1], Sz1, @Par2[1], Sz2, @Par3[1], Sz3, @Par4[1], Sz4);
AddListValue('Public key','DSA (' + IntToStr(Sz4*8) + ')',
'DSSP = ' + BuildHexString(Par1) + #13#10 + 'DSSQ = ' + BuildHexString(Par2) + #13#10 +
'DSSG = ' + BuildHexString(Par3) + #13#10 + 'DSSY = ' + BuildHexString(Par4));
end
else if Cert.PublicKeyAlgorithm = SB_CERT_ALGORITHM_DH_PUBLIC then
begin
Sz1 := 0; Sz2:=0; Sz3:=0;
Cert.GetDHParams(nil, Sz1, nil, Sz2, nil, Sz3);
SetLength(Par1, Sz1); SetLength(Par2, Sz2); SetLength(Par3, Sz3);
Cert.GetDHParams(@Par1[1], Sz1, @Par2[1], Sz2, @Par3[1], Sz3);
AddListValue('Public key','DH (' + IntToStr(Sz3*8) + ')',
'DHP = ' + BuildHexString(Par1) + #13#10 + 'DHG = ' + BuildHexString(Par2) + #13#10 +
'DHY = ' + BuildHexString(Par3));
end
else
begin
Sz1 := 0;
Cert.GetPublicKeyBlob(nil, Sz1);
SetLength(Par1, Sz1);
Cert.GetPublicKeyBlob(@Par1[1], Sz1);
AddListValue('Public key', 'Unknown', 'Key blob = ' + BuildHexString(Par1));
end;
// Private key
if not Cert.PrivateKeyExists then AddListValue('Private Key','Not Available') else
begin
if Cert.PrivateKeyExtractable then AddListValue('Private Key','Available, Exportable')
else AddListValue('Private Key','Available, Not Exportable');
end;
// Extensions
DisplayExtensions(Cert);
try
if Cert.Validate then
sbStatus.SimpleText := 'Certificate is self signed'
else
sbStatus.SimpleText := 'Certificate is not self signed';
except end;
if Cert.PrivateKeyExists then
sbStatus.SimpleText := sbStatus.SimpleText + ', Private Key exists'
else
sbStatus.SimpleText := sbStatus.SimpleText + ', Private Key does not exist';
// The code below can be used to show private keys
(*
PrivateKeySize:=0;
Cert.SaveKeyToBuffer(nil, PrivateKeySize);
Cert.SaveKeyToBuffer(@GlobalPrivateKey[0], PrivateKeySize);
if PrivateKeySize <> 0 then
begin
sbStatus.SimpleText := StatusBar1.SimpleText + ', Private Key exists';
S := 'PrivateKey = ' + BuildHexString(GlobalPrivateKey);
end
else begin
sbStatus.SimpleText := StatusBar1.SimpleText + ', Private Key does not exist';
S := '';
end;
memContent.Lines.Add(S);
*)
lvCertProperties.Items.EndUpdate;
end;
procedure TSertX509.FormActivate(Sender: TObject);
var i : integer;
begin
// Hide tabs
For i:=0 to pcInfo.PageCount - 1 do pcInfo.Pages[i].TabVisible:=False;
GlobalCert := TElX509Certificate.Create(nil);
WinStorage1 := TElWinCertStorage.Create(nil);
WinStorage2 := TElWinCertStorage.Create(nil);
WinStorage3 := TElWinCertStorage.Create(nil);
WinStorage4 := TElWinCertStorage.Create(nil);
StorageList := TList.Create;
CurrStorageName := TStringList.Create;
if treeCert.Items[0].GetLastChild.AbsoluteIndex < 4 then
begin
sbStatus.Panels[0].Text := 'Loading Windows Certificates...';
Screen.Cursor := crHourGlass;
WinStorage1.SystemStores.Clear;
// ROOT
WinStorage1.SystemStores.Add('ROOT');
LoadStorage(treeCert.Items[1],'ROOT',WinStorage1);
// CA
WinStorage2.SystemStores.Clear;
WinStorage2.SystemStores.Add('CA');
LoadStorage(treeCert.Items[1],'CA',WinStorage2);
// MY
WinStorage3.SystemStores.Clear;
WinStorage3.SystemStores.Add('MY');
LoadStorage(treeCert.Items[1],'MY',WinStorage3);
// SPC
WinStorage4.SystemStores.Clear;
WinStorage4.SystemStores.Add('SPC');
//LoadStorage(treeCert.Items[1],'SPC',WinStorage4);
Screen.Cursor := crDefault;
sbStatus.Panels[0].Text := '';
TreeCertChange(nil, TreeCert.Selected);
end;
end;
function TSertX509.DoCopyToStorage: Boolean;
var J : integer;
DestStorage : TElCustomCertStorage;
SourceCert : TElX509Certificate;
begin
result := false;
if (treeCert.Selected.Data <> nil) and
(TObject(treeCert.Selected.Data) is TElX509Certificate) then
begin
SourceCert := TElX509Certificate(treeCert.Selected.Data);
StorageSelectForm := TStorageSelectForm.Create(nil);
try
if StorageSelectForm.ShowModal = mrCancel then exit;
DestStorage := TElCustomCertStorage(StorageSelectForm.treeStorage.Selected.Data);
finally
FreeAndNil(StorageSelectForm);
end;
J := 0;
while J < treeCert.Items.Count do
begin
if (treeCert.Items[J].Data = DestStorage) then
break;
inc(j);
end;
if (j < treeCert.Items.Count) and (treeCert.Items[J].Data = DestStorage) then
begin
DestStorage.Add(SourceCert);
treeCert.Items.AddChildObject(treeCert.Items[J], treeCert.Selected.Text, SourceCert);
result := true;
end;
end;
end;
procedure TSertX509.mmiExitClick(Sender: TObject);
begin
Close;
end;
procedure TSertX509.treeCertChange(Sender: TObject; Node: TTreeNode);
begin
// Disable all storages
acNewMemStorage.Enabled := False;
acNewFileStorage.Enabled := False;
acMountStorage.Enabled := False;
acUnmountStorage.Enabled := False;
acImportFromWinStorage.Enabled := False;
acSaveStorage.Enabled := False;
acSaveStorageAs.Enabled := False;
acExportToMemoryStorage.Enabled := False;
acTrusted.Checked := False;
acTrusted.Enabled := False;
// Disable all certificates
acNewCertificate.Enabled := False;
acLoadCertificate.Enabled := False;
acSaveCertificate.Enabled := False;
acRemoveCertificate.Enabled := False;
acValidate.Enabled := False;
acLoadPrivateKey.Enabled := False;
acMoveToStorage.Enabled := False;
acCopyToStorage.Enabled := False;
// Conditions check
if (Node = nil) or (Node.Data = nil) then
begin
acNewMemStorage.Enabled := True;
acNewFileStorage.Enabled := True;
acMountStorage.Enabled := True;
acImportFromWinStorage.Enabled := True;
Storage := nil;
GlobalCert := nil;
end
else
begin
if (TObject(Node.Data) is TElX509Certificate) then
begin
DisplayCertificateInfo(TElX509Certificate(Node.Data));
acSaveStorageAs.Enabled := True;
acExportToMemoryStorage.Enabled := True;
acNewCertificate.Enabled := True;
acLoadCertificate.Enabled := True;
acSaveCertificate.Enabled := True;
acRemoveCertificate.Enabled := True;
acValidate.Enabled := True;
acLoadPrivateKey.Enabled := True;
acMoveToStorage.Enabled := True;
acCopyToStorage.Enabled := True;
Storage := treeCert.Selected.Parent.Data;
GlobalCert := treeCert.Selected.Data;
if not (TObject(Node.Parent.Data) is TElWinCertStorage) then
acMoveToStorage.Enabled := True;
end;
if (TObject(Node.Data) is TElWinCertStorage) or (TObject(Node.Data) is TElFileCertStorage)
or (TObject(Node.Data) is TElMemoryCertStorage)then
begin
acSaveStorage.Enabled := True;
acSaveStorageAs.Enabled := True;
acExportToMemoryStorage.Enabled := True;
acNewCertificate.Enabled := True;
acLoadCertificate.Enabled := True;
acTrusted.Enabled := True;
acTrusted.Checked := Node.ImageIndex = 6; // Check for trusted storage
Storage := treeCert.Selected.Data;
GlobalCert := nil;
end;
if (TObject(Node.Data) is TElFileCertStorage)
or (TObject(Node.Data) is TElMemoryCertStorage) then acUnmountStorage.Enabled := True;
end;
if ((Node <> nil) and (Node.Data <> nil) and
(TObject(Node.Data) is TElX509Certificate)) then pcInfo.ActivePage:=tsCertificate
else pcInfo.ActivePage:=tsNoInfo;
end;
procedure TSertX509.treeCertDeletion(Sender: TObject; Node: TTreeNode);
begin
if (Node <> nil) and
(TObject(Node.data) is TElX509Certificate) then
begin
if GlobalCert = Node.data then
GlobalCert := nil;
TObject(Node.Data).Free;
Node.Data := nil;
end;
end;
procedure TSertX509.acNewMemStorageExecute(Sender: TObject);
var MemoryStorage : TElMemoryCertStorage;
S : String;
begin
MemoryStorage := TElMemoryCertStorage.Create(self);
Storage := MemoryStorage;
S := 'Storage' + IntToStr(StorageNumber);
Inc(StorageNumber);
LoadStorage(treeCert.Items[0].Item[2], S, Storage);
end;
procedure TSertX509.acNewFileStorageExecute(Sender: TObject);
var FileStorage : TElFileCertStorage;
begin
if (treeCert.Selected.Data = nil) and (SaveDlgStorage.Execute) then
begin
if Pos('.p7b',SaveDlgStorage.FileName) = 0 then
SaveDlgStorage.FileName:=SaveDlgStorage.FileName + '.p7b';
FileStorage := TElFileCertStorage.Create(nil);
FileStorage.FileName := SaveDlgStorage.FileName;
LoadStorage(treeCert.Items[0].Item[1],FileStorage.FileName, FileStorage);
end;
end;
procedure TSertX509.acSaveStorageExecute(Sender: TObject);
begin
if not Assigned(Storage) then exit;
if Storage.Count = 0 then
begin
ShowMessage('This Storage is empty. Cannot save.');
Exit;
end;
if Storage is TElFileCertStorage then
TElFileCertStorage(Storage).SaveToFile(TElFileCertStorage(Storage).FileName)
else
acSaveStorageAsExecute(Self);
end;
procedure TSertX509.acSaveStorageAsExecute(Sender: TObject);
var TmpFileSt : TElFileCertStorage;
Pwd : string;
FileName : string;
Stream : TFileStream;
begin
if Storage.Count = 0 then
begin
ShowMessage('This Storage is empty. Cannot save.');
Exit;
end;
if (treeCert.Selected.Data = nil) or (not Assigned(Storage)) then exit;
if not SaveDlgStorage.Execute then exit;
FileName := SaveDlgStorage.FileName;
if SaveDlgStorage.FilterIndex = 1 then
begin
if lowercase(ExtractFileExt(FileName)) <> '.p7b' then
FileName := FileName + '.p7b';
if Storage is TElFileCertStorage then
TElFileCertStorage(Storage).SaveToFile(FileName)
else begin
TmpFileSt := TElFileCertStorage.Create(Self);
Storage.ExportTo(TmpFileSt);
TElFileCertStorage(TmpFileSt).SaveToFile(FileName);
TmpFileSt.Free;
end;
end
else begin
if lowercase(ExtractFileExt(FileName)) <> '.pfx' then
FileName := FileName + '.pfx';
Pwd:='';
if not (Storage is TElWinCertStorage) then
if MessageDlg('Do you want to save private keys?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
if not InputQuery('Enter password', 'Enter password for private keys', Pwd) then exit;
Stream := TFileStream.Create(FileName, fmCreate or fmShareDenyWrite);
try
Storage.SaveToStreamPFX(Stream, Pwd, SB_ALGORITHM_PBE_SHA1_3DES, SB_ALGORITHM_PBE_SHA1_RC2_40)
finally
Stream.Free;
end;
end;
end;
procedure TSertX509.acMountStorageExecute(Sender: TObject);
var FileStorage : TElFileCertStorage;
begin
if (treeCert.Selected.Data = nil) and (OpenDlgStorage.Execute) then
begin
FileStorage := TElFileCertStorage.Create(nil);
FileStorage.FileName := OpenDlgStorage.FileName;
LoadStorage(treeCert.Items[0].Item[1], OpenDlgStorage.FileName, FileStorage);
end;
end;
procedure TSertX509.acUnmountStorageExecute(Sender: TObject);
var I : integer;
begin
if Assigned(Storage) then
if (Storage is TElFileCertStorage) or
(Storage is TElMemoryCertStorage) then
begin
treeCert.Selected.Delete;
I := 0;
while I < StorageList.Count do
begin
if StorageList.Items[I] = Storage then
begin
StorageList.Delete(I);
Break;
end;
Inc(I);
end;
end;
end;
procedure TSertX509.acImportFromWinStorageExecute(Sender: TObject);
var S : string;
TmpMemStorage : TElMemoryCertStorage;
ClickedOk : Boolean;
begin
S := 'ROOT';
ClickedOk := InputQuery('Store name','Enter Windows store name', S);
if not ClickedOk then exit;
TmpMemStorage := TElMemoryCertStorage.Create(self);
WinStorage1.SystemStores.Clear;
sbStatus.Panels[0].Text := 'Loading Windows Certificates...';
WinStorage1.SystemStores.Add(S);
WinStorage1.ExportTo(TmpMemStorage);
Storage := TmpMemStorage;
S := 'Storage' + IntToStr(StorageNumber);
Inc(StorageNumber);
LoadStorage(treeCert.Items[0].Item[2], S, Storage);
end;
procedure TSertX509.acExportToMemoryStorageExecute(Sender: TObject);
var TmpMemStorage : TElMemoryCertStorage;
I : integer;
S : String;
begin
if (treeCert.Selected.Data <> nil) then
begin
TmpMemStorage := TElMemoryCertStorage.Create(self);
for I := 0 to StorageList.Count - 1 do
if Storage = StorageList.Items[I] then
begin
if Storage is TElWinCertStorage then
begin
TElWinCertStorage(Storage).SystemStores.Clear;
TElWinCertStorage(Storage).SystemStores.Add(CurrStorageName[I]);
end;
Storage.ExportTo(TmpMemStorage);
Break;
end;
Storage := TmpMemStorage;
S := 'Storage' + IntToStr(StorageNumber);
Inc(StorageNumber);
LoadStorage(treeCert.Items[0].Item[2], S, Storage);
end;
end;
procedure TSertX509.acNewCertificateExecute(Sender: TObject);
begin
TfrmGenerateCert.DoCreateCertificate;
end;
procedure TSertX509.acLoadCertificateExecute(Sender: TObject);
var S : string;
Cert : TElX509Certificate;
Pwd : string;
Stream : TFileStream;
FileName,
KeyFileName : string;
ti,Parent : TTreeNode;
R : integer;
begin
if (treeCert.Selected = nil) or (treeCert.Selected.Data = nil) then exit;
if not (OpenDlgCert.Execute) then exit;
FileName := OpenDlgCert.FileName;
Pwd:='';
Cert := TElX509Certificate.Create(nil);
case OpenDlgCert.FilterIndex of
1: begin
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
Cert.LoadFromStream(Stream);
finally
Stream.Free;
end;
KeyFileName := ChangeFileExt(FileName, '.der');
if not FileExists(KeyFileName) then
KeyFileName := ChangeFileExt(FileName, '.key');
if FileExists(KeyFileName) then
begin
if (MessageDlg('Do you want to load private key?', mtConfirmation, [mbYes, mbNo], 0) = mrYes) then
begin
// Pwd := '';
// if not InputQuery('Enter password', 'Enter password for private key', Pwd) then exit;
Stream := TFileStream.Create(KeyFileName, fmOpenRead or fmShareDenyWrite);
try
Cert.LoadKeyFromStream(Stream);
finally
Stream.Free;
end;
end;
end;
end;
2: begin
if not InputQuery('Enter password', 'Enter password for private key', Pwd) then exit;
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
R := Cert.LoadFromStreamPEM(Stream, Pwd);
finally
Stream.Free;
end;
if R <> 0 then
begin
MessageDlg('Failed to load PEM certificate: error ' + IntToHex(R, 4),
mtError, [mbOk], 0);
Exit;
end;
end;
3: begin
if not InputQuery('Enter password', 'Enter password for private key', Pwd) then exit;
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
R := Cert.LoadFromStreamPFX(Stream, Pwd);
finally
Stream.Free;
end;
if R <> 0 then
begin
MessageDlg('Failed to load PFX certificate: error ' + IntToHex(R, 4),
mtError, [mbOk], 0);
Exit;
end;
end;
end; { case }
Storage.Add(Cert);
S := GetOIDValue(Cert.SubjectRDN, SB_CERT_OID_COMMON_NAME);
if s = '' then
s := GetOIDValue(Cert.SubjectRDN, SB_CERT_OID_ORGANIZATION);
if TObject(treeCert.Selected.Data) is TElCustomCertStorage then
Parent := treeCert.Selected
else
Parent := treeCert.Selected.Parent;
ti:=treeCert.Items.AddChildObject(Parent, S, Cert);
ti.ImageIndex:=3;
ti.SelectedIndex:=3;
GlobalCert := Cert;
end;
procedure TSertX509.acSaveCertificateExecute(Sender: TObject);
var S : string;
FileName,
KeyFileName : string;
SavePvtKey : boolean;
Pwd : string;
Stream : TFileStream;
begin
if (TObject(treeCert.Selected.Data) is TElX509Certificate) and
(SaveDlgCert.Execute) then
begin
FileName := SaveDlgCert.FileName;
S := ''; Pwd := '';
if GlobalCert.PrivateKeyExists then
begin
SavePvtKey := true;
if SaveDlgCert.FilterIndex = 1 then
SavePvtKey := MessageDlg('Do you want to save private key?',
mtConfirmation, [mbYes, mbNo], 0) = mrYes;
end else SavePvtKey := False;
if SavePvtKey then
if not InputQuery('Enter password', 'Enter password for private key', Pwd) then exit;
if SaveDlgCert.FilterIndex = 1 then
begin
if lowercase(ExtractFileExt(FileName)) <> '.cer' then
FileName := FileName + '.cer';
Stream := TFileStream.Create(FileName, fmCreate or fmShareDenyWrite);
try
GlobalCert.SaveToStream(Stream);
finally
Stream.Free;
end;
if SavePvtKey and GlobalCert.PrivateKeyExists then
begin
KeyFileName := ChangeFileExt(FileName, '.key');
Stream := TFileStream.Create(KeyFileName, fmCreate or fmShareDenyWrite);
try
GlobalCert.SaveKeyToStream(Stream);
finally
Stream.Free;
end;
end;
end;
// PEM format
if SaveDlgCert.FilterIndex = 2 then
begin
if lowercase(ExtractFileExt(FileName)) <> '.pem' then
FileName := FileName + '.pem';
Stream := TFileStream.Create(FileName, fmCreate or fmShareDenyWrite);
try
GlobalCert.SaveToStreamPEM(Stream, '');
if SavePvtKey then
GlobalCert.SaveKeyToStreamPEM(Stream, Pwd);
finally
Stream.Free;
end;
end;
// PFX format
if SaveDlgCert.FilterIndex = 3 then
begin
if lowercase(ExtractFileExt(FileName)) <> '.pfx' then
FileName := FileName + '.pfx';
Stream := TFileStream.Create(FileName, fmCreate or fmShareDenyWrite);
try
GlobalCert.SaveToStreamPFX(Stream, Pwd, SB_ALGORITHM_PBE_SHA1_3DES, SB_ALGORITHM_PBE_SHA1_RC2_40);
finally
Stream.Free;
end;
end;
end;
end;
procedure TSertX509.acRemoveCertificateExecute(Sender: TObject);
var I : integer;
begin
if not Assigned(Storage) then exit;
if (treeCert.Selected.Data <> nil) and
(treeCert.Selected.Data = GlobalCert)
and (treeCert.Selected.Parent.Data = Storage) then
begin
I := Storage.FindByHash(TElX509Certificate(treeCert.Selected.Data).GetHashSHA1);
if i <> - 1 then
begin
treeCert.Selected.Delete;