-
Notifications
You must be signed in to change notification settings - Fork 1
/
PGPMail.pas
2506 lines (2214 loc) · 69.8 KB
/
PGPMail.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 PGPMail;
interface
{$IFDEF VER120}
{$DEFINE D_3_UP}
{$DEFINE D_4_UP}
{$DEFINE VCL40}
{$ENDIF}
{$IFDEF VER125}
{$DEFINE B_3_UP}
{$DEFINE B_4_UP}
{$DEFINE B_4}
{$DEFINE VCL40}
{$DEFINE BUILDER_USED}
{$ENDIF}
{$IFDEF VER130}
{$IFDEF BCB}
{$DEFINE B_3_UP}
{$DEFINE B_4_UP}
{$DEFINE B_5_UP}
{$DEFINE B_5}
{$DEFINE VCL40}
{$DEFINE VCL50}
{$DEFINE BUILDER_USED}
{$ELSE}
{$DEFINE D_3_UP}
{$DEFINE D_4_UP}
{$DEFINE D_5_UP}
{$DEFINE VCL40}
{$DEFINE VCL50}
{.DEFINE USEADO}
{$ENDIF}
{$ENDIF}
{$IFDEF VER140}
{$IFDEF BCB}
{$DEFINE B_3_UP}
{$DEFINE B_4_UP}
{$DEFINE B_5_UP}
{$DEFINE B_6_UP}
{$DEFINE B_6}
{$DEFINE VCL40}
{$DEFINE VCL50}
{$DEFINE VCL60}
{$DEFINE BUILDER_USED}
{$ELSE}
{$DEFINE D_3_UP}
{$DEFINE D_4_UP}
{$DEFINE D_5_UP}
{$DEFINE D_6_UP}
{$DEFINE D_6}
{$DEFINE VCL40}
{$DEFINE VCL50}
{$DEFINE VCL60}
{.DEFINE USEADO}
{$ENDIF}
{$ENDIF}
{$IFDEF VER150}
{$IFNDEF BCB}
{$DEFINE D_3_UP}
{$DEFINE D_4_UP}
{$DEFINE D_5_UP}
{$DEFINE D_6_UP}
{$DEFINE D_7_UP}
{$DEFINE D_7}
{$DEFINE VCL40}
{$DEFINE VCL50}
{$DEFINE VCL60}
{$DEFINE VCL70}
{.DEFINE USEADO}
{$ENDIF}
{$ENDIF}
{$IFDEF VER160}
{$DEFINE D_3_UP}
{$DEFINE D_4_UP}
{$DEFINE D_5_UP}
{$DEFINE D_6_UP}
{$DEFINE D_7_UP}
{$DEFINE D_8_UP}
{$DEFINE D_8}
{$DEFINE VCL40}
{$DEFINE VCL50}
{$DEFINE VCL60}
{$DEFINE VCL70}
{$DEFINE VCL80}
{.$DEFINE USE_NAME_SPACE} // Optional !!!
{$ENDIF}
{$IFDEF VER170}
{$DEFINE D_3_UP}
{$DEFINE D_4_UP}
{$DEFINE D_5_UP}
{$DEFINE D_6_UP}
{$DEFINE D_7_UP}
{$DEFINE D_8_UP}
{$DEFINE D_9_UP}
{$DEFINE D_9}
{$DEFINE VCL40}
{$DEFINE VCL50}
{$DEFINE VCL60}
{$DEFINE VCL70}
{$DEFINE VCL80}
{$DEFINE VCL90}
{.$DEFINE USE_NAME_SPACE} // Optional !!!
{$ENDIF}
{$ifdef CLR}
{$DEFINE DELPHI_NET}
{$endif}
uses
{$IFDEF DELPHI_NET}
System.IO,
System.Text,
System.Drawing,
Borland.VCL.Windows, Borland.VCL.Messages, Borland.VCL.SysUtils,
Borland.VCL.Classes,
Borland.VCL.Forms, Borland.VCL.Controls,
Borland.VCL.Dialogs, Borland.VCL.StdCtrls, Borland.VCL.Buttons,
Borland.VCL.ComCtrls, Borland.VCL.ExtCtrls,
Borland.VCL.Graphics,
{$ELSE}
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, {$IFDEF D_6_UP}Variants,{$ENDIF}
Dialogs, StdCtrls, Buttons, ComCtrls, ExtCtrls,
{$ENDIF}
SBRDN, SBUtils, SBCustomCertStorage, SBX509, SBX509Ext, SBConstants,
SBMessages, SBMIME, SBSMIMECore,
SBPGPKeys, SBPGPConstants, SBPGPUtils, SBPGPMIME;
type
TmailPGP1 = class(TForm)
Panel3: TPanel;
Bevel1: TBevel;
Panel2: TPanel;
btnBack: TButton;
btnNext: TButton;
btnCancel: TButton;
Panel1: TPanel;
imgLogo: TImage;
PageControl: TPageControl;
tsSelectCertificates: TTabSheet;
lbSelectCertificates: TLabel;
lbxCertificates: TListBox;
btnAddCertificate: TButton;
btnRemoveCertificate: TButton;
tsAlgorithm: TTabSheet;
lbChooseAlgorithm: TLabel;
rbTripleDES: TRadioButton;
rbRC4_128: TRadioButton;
rbRC4_40: TRadioButton;
rbRC2: TRadioButton;
rbAES_128: TRadioButton;
rbAES_256: TRadioButton;
tsSelectFiles: TTabSheet;
lbSelectFiles: TLabel;
lbInputFile: TLabel;
sbInputFile: TSpeedButton;
sbOutputFile: TSpeedButton;
lbOutputFile: TLabel;
edInputFile: TEdit;
edOutputFile: TEdit;
tsCheckData: TTabSheet;
lbInfo: TLabel;
mmInfo: TMemo;
btnDoIt: TButton;
tsSignAlgorithm: TTabSheet;
lbChooseSignAlgorithm: TLabel;
rbMD5: TRadioButton;
rbSHA1: TRadioButton;
OpenDlg: TOpenDialog;
SaveDlg: TSaveDialog;
tsSelectAction: TTabSheet;
lbActionToPerform: TLabel;
rbSMimeVerify: TRadioButton;
rbSMimeDecrypt: TRadioButton;
rbSMimeSign: TRadioButton;
rbSMimeEncrypt: TRadioButton;
tsResult: TTabSheet;
lbSMime: TLabel;
lbPGPMime: TLabel;
rbPGPMimeEncrypt: TRadioButton;
rbPGPMimeSign: TRadioButton;
rbPGPMimeDecrypt: TRadioButton;
rbPGPMimeVerify: TRadioButton;
rbDES: TRadioButton;
rbAES_192: TRadioButton;
mmResult: TMemo;
lbResult: TLabel;
tsSelectKeys: TTabSheet;
lbSelectKeys: TLabel;
btnAddKey: TButton;
btnRemoveKey: TButton;
tsSelectKey: TTabSheet;
lbSelectKey: TLabel;
lbKeyring: TLabel;
edKeyring: TEdit;
sbKeyring: TSpeedButton;
tvKeys: TTreeView;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnNextClick(Sender: TObject);
procedure sbInputFileClick(Sender: TObject);
procedure sbOutputFileClick(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
procedure btnBackClick(Sender: TObject);
procedure btnAddCertificateClick(Sender: TObject);
procedure btnRemoveCertificateClick(Sender: TObject);
procedure btnRemoveKeyClick(Sender: TObject);
procedure sbKeyringClick(Sender: TObject);
procedure btnAddKeyClick(Sender: TObject);
private
FAction: Integer;
FCurrentPage: Integer;
FMemoryCertStorage: TElMemoryCertStorage;
FKeyring: TElPGPKeyring;
FSecretRing: TElPGPKeyring;
FPublicRing: TElPGPKeyring;
procedure ClearData;
procedure SMimeEncryptNext;
procedure SMimeDecryptNext;
procedure SMimeSignNext;
procedure SMimeVerifyNext;
function SMimeEncrypt(const InputFileName, OutputFileName: string;
const CryptAlgorithm, CryptAlgorithmBitsInKey: Integer): string;
function SMimeDecrypt(const InputFileName, OutputFileName: string): string;
function SMimeSign(const InputFileName, OutputFileName: string;
const SignAlgorithm: string): string;
function SMimeVerify(const InputFileName: string): string;
procedure PGPEncryptNext;
procedure PGPDecryptNext;
procedure PGPSignNext;
procedure PGPVerifyNext;
function PGPEncrypt(const InputFileName, OutputFileName: string): string;
function PGPDecrypt(const InputFileName, OutputFileName: string): string;
function PGPSign(const InputFileName, OutputFileName: string): string;
function PGPVerify(const InputFileName: string): string;
procedure PGPMIMEKeyPassphrase(Sender: TObject; Key : TElPGPCustomSecretKey;
var Passphrase: string; var Cancel: boolean);
function GetAlgorithm: Integer;
function GetAlgorithmBitsInKey: Integer;
function GetAlgorithmName: string;
function GetSignAlgorithm: string;
procedure SetResults(const Res: string);
procedure UpdateCertificatesList;
function WriteCertificateInfo(Storage: TElCustomCertStorage): string;
procedure UpdateKeysList;
function WriteKeyringInfo(Keyring: TElPGPKeyring): string;
public
procedure SetPage(Page: Integer);
procedure Back;
procedure Next;
property Action: Integer read FAction write FAction;
property CurrentPage: Integer read FCurrentPage write FCurrentPage;
end;
var
mailPGP1: TmailPGP1;
const
ACTION_UNKNOWN = 0;
ACTION_SMIME_ENCRYPT = 1;
ACTION_SMIME_SIGN = 2;
ACTION_SMIME_DECRYPT = 3;
ACTION_SMIME_VERIFY = 4;
ACTION_PGPMIME_ENCRYPT = 5;
ACTION_PGPMIME_SIGN = 6;
ACTION_PGPMIME_DECRYPT = 7;
ACTION_PGPMIME_VERIFY = 8;
PAGE_DEFAULT = 0;
PAGE_SELECT_ACTION = 1;
PAGE_SELECT_FILES = 2;
PAGE_SELECT_CERTIFICATES = 3;
PAGE_SELECT_ALGORITHM = 4;
PAGE_SELECT_KEYS = 5;
PAGE_CHECK_DATA = 6;
PAGE_PROCESS = 7;
const
cDemoVersion = '2005.04.18';
cXMailerDemoFieldValue = 'EldoS ElMime Demos, version: ' + cDemoVersion +
' ( '+cXMailerDefaultFieldValue + ' )';
resourcestring
sSelectFilesForEncryption = 'Please select message file to encrypt and file where to write encrypted data';
sSelectFilesForDecryption = 'Please select input (encrypted) file and file where to write decrypted data';
sSelectFilesForSigning = 'Please select file to sign and file where to write signed data';
sSelectFilesForVerifying = 'Please select file with a signed message'{ and file where to put original message'};
sSelectCertificatesForEncryption = 'Please choose certificates which should be used to encrypt message';
sSelectCertificatesForDecryption = 'Please select certificates which may be used to decrypt message. Each certificate should be loaded with corresponding private key';
sSelectCertificatesForSigning = 'Please choose certificates which should be used to sign the file. At least one certificate must be loaded with corresponding private key';
sSelectCertificatesForVerifying = 'Please select certificates which may be used to verify digital signature. Note, that in most cases signer''s certificates are included in signed message, so you may leave certificate list empty';
sSelectKeysForEncryption = 'Please choose PGP public key which should be used to encrypt message';
sSelectKeysForDecryption = 'Please select PGP secret keys which may be used to decrypt message';
sSelectKeysForSigning = 'Please choose PGP secret key which should be used to sign the file';
sSelectKeysForVerifying = 'Please select PGP public keys which may be used to verify digital signature.';
sInfoEncryption = 'Ready to start encryption. Please check all the parameters to be valid';
sInfoSigning = 'Ready to start signing. Please check that all signing options are correct.';
sInfoDecryption = 'Ready to start decryption. Please check that all decryption options are correct.';
sInfoVerifying = 'Ready to start verifying. Please check that all options are correct.';
sSelectInputFiles = 'You must select input file';
sSelectInputOutputFiles = 'You must select both input and output files';
implementation
{$R *.dfm}
function GetStringByOID(const S : BufferType) : string;
begin
if CompareContent(S, SB_CERT_OID_COMMON_NAME) then
Result := 'CommonName'
else
if CompareContent(S, SB_CERT_OID_COUNTRY) then
Result := 'Country'
else
if CompareContent(S, SB_CERT_OID_LOCALITY) then
Result := 'Locality'
else
if CompareContent(S, SB_CERT_OID_STATE_OR_PROVINCE) then
Result := 'StateOrProvince'
else
if CompareContent(S, SB_CERT_OID_ORGANIZATION) then
Result := 'Organization'
else
if CompareContent(S, SB_CERT_OID_ORGANIZATION_UNIT) then
Result := 'OrganizationUnit'
else
if CompareContent(S, SB_CERT_OID_EMAIL) then
Result := 'Email'
else
Result := 'UnknownField';
end;
function GetOIDValue(NTS: TElRelativeDistinguishedName; const S: BufferType; const Delimeter: AnsiString = ' / '): AnsiString;
var
i: Integer;
t: AnsiString;
begin
Result := '';
for i := 0 to NTS.Count - 1 do
if CompareContent(S, NTS.OIDs[i]) then
begin
t := AnsiString(NTS.Values[i]);
if t = '' then
Continue;
if Result = '' then
begin
Result := t;
if Delimeter = '' then
Exit;
end
else
Result := Result + Delimeter + t;
end;
end;
function GetPublicKeyNames(Key: TElPGPPublicKey): string;
var
i: Integer;
begin
Result := '';
if not Assigned(Key) then
Exit;
for i := 0 to Key.UserIDCount - 1 do
if Key.UserIDs[i].Name <> '' then
begin
if Result <> '' then
Result := Result + ', ';
Result := Result + Key.UserIDs[i].Name;
end;
end;
procedure TmailPGP1.Back;
var
NewPage: Integer;
begin
if not Assigned(PageControl.ActivePage) then
begin
SetPage(PAGE_DEFAULT);
Exit;
end;
NewPage := PAGE_DEFAULT;
case CurrentPage of
PAGE_SELECT_FILES: NewPage := PAGE_SELECT_ACTION;
PAGE_SELECT_CERTIFICATES: NewPage := PAGE_SELECT_FILES;
PAGE_SELECT_ALGORITHM: NewPage := PAGE_SELECT_CERTIFICATES;
PAGE_SELECT_KEYS: NewPage := PAGE_SELECT_FILES;
PAGE_CHECK_DATA:
begin
case Action of
ACTION_SMIME_ENCRYPT, ACTION_SMIME_SIGN:
NewPage := PAGE_SELECT_ALGORITHM;
ACTION_SMIME_DECRYPT, ACTION_SMIME_VERIFY:
NewPage := PAGE_SELECT_CERTIFICATES;
else
NewPage := PAGE_SELECT_KEYS;
end;
end;
PAGE_PROCESS: NewPage := PAGE_CHECK_DATA;
end;
SetPage(NewPage);
end;
procedure TmailPGP1.btnAddCertificateClick(Sender: TObject);
var
F: TFileStream;
Buf: array of Byte;
Cert: TElX509Certificate;
sFrom: string;
KeyLoaded: Boolean;
Res: Integer;
{$IFDEF DELPHI_NET}
Sz: Integer;
{$ELSE}
Sz: Word;
{$ENDIF}
Index : integer;
begin
KeyLoaded := False;
OpenDlg.FileName := '';
OpenDlg.Title := 'Select certificate file';
OpenDlg.Filter := 'PEM-encoded certificate (*.pem)|*.pem|DER-encoded certificate (*.cer)|*.cer|PFX-encoded certificate (*.pfx)|*.pfx';
if not OpenDlg.Execute then
Exit;
F := TFileStream.Create(OpenDlg.Filename, fmOpenRead or fmShareExclusive);
SetLength(Buf, F.Size);
F.Read({$IFDEF DELPHI_NET}Buf, 0{$ELSE}Buf[0]{$ENDIF}, F.Size);
F.Free;
Res := 0;
Cert := TElX509Certificate.Create(nil);
if OpenDlg.FilterIndex = 3 then
Res := Cert.LoadFromBufferPFX({$IFDEF DELPHI_NET}Buf{$ELSE}@Buf[0], Length(Buf){$ENDIF}, InputBox('Please enter passphrase:', '',''))
else
if OpenDlg.FilterIndex = 1 then
Res := Cert.LoadFromBufferPEM({$IFDEF DELPHI_NET}Buf{$ELSE}@Buf[0], Length(Buf){$ENDIF}, '')
else
if OpenDlg.FilterIndex = 2 then
Cert.LoadFromBuffer({$IFDEF DELPHI_NET}Buf{$ELSE}@Buf[0], Length(Buf){$ENDIF})
else
Res := -1;
if (Res <> 0) or (Cert.CertificateSize = 0) then
begin
Cert.Free;
ShowMessage('Error loading the certificate');
Exit;
end;
if (Action = ACTION_SMIME_DECRYPT) or (Action = ACTION_SMIME_SIGN) then
begin
Sz := 0;
{$IFDEF DELPHI_NET}
SetLength(Buf, 0);
Cert.SaveKeyToBuffer(Buf, Sz);
{$ELSE}
Cert.SaveKeyToBuffer(nil, Sz);
{$ENDIF}
if (Sz = 0) then
begin
OpenDlg.Title := 'Select the corresponding private key file';
OpenDlg.Filter := 'PEM-encoded key (*.pem)|*.PEM|DER-encoded key (*.key)|*.key';
if OpenDlg.Execute then
begin
F := TFileStream.Create(OpenDlg.Filename, fmOpenRead or fmShareExclusive);
SetLength(Buf, F.Size);
F.Read({$IFDEF DELPHI_NET}Buf, 0{$ELSE}Buf[0]{$ENDIF}, F.Size);
F.Free;
if OpenDlg.FilterIndex = 1 then
Cert.LoadKeyFromBufferPEM({$IFDEF DELPHI_NET}Buf{$ELSE}@Buf[0], Length(Buf){$ENDIF}, InputBox('Please enter passphrase:', '',''))
else
Cert.LoadKeyFromBuffer({$IFDEF DELPHI_NET}Buf{$ELSE}@Buf[0], Length(Buf){$ENDIF});
KeyLoaded := True;
end;
end
else
KeyLoaded := True;
end;
// certificate e-mail in UTF8
sFrom := GetOIDValue(Cert.SubjectRDN, SB_CERT_OID_EMAIL);
if sFrom = '' then
begin
Index := Cert.Extensions.SubjectAlternativename.Content.FindNameByType(gnRFC822Name);
if Index >= 0 then
sFrom := Cert.Extensions.SubjectAlternativeName.Content.Names[Index].RFC822Name
else
MessageDlg('Warning: Certificate does not contain e-mail address.', mtWarning, [mbOk], 0);
end;
if (Action = ACTION_SMIME_DECRYPT) and (not KeyLoaded) then
MessageDlg('Private key was not loaded, certificate ignored', mtError, [mbOk], 0)
else
begin
FMemoryCertStorage.Add(Cert);
UpdateCertificatesList;
end;
Cert.Free;
end;
procedure TmailPGP1.btnAddKeyClick(Sender: TObject);
var
TempKeyring : TElPGPKeyring;
begin
OpenDlg.Title := 'Select input file';
OpenDlg.Filter := 'PGP Keyring files (*.asc, *.pkr, *.skr, *.gpg, *.pgp)|*.asc;*.pkr;*.skr;*.gpg;*.pgp';
OpenDlg.FileName := '';
if OpenDlg.Execute then
begin
TempKeyring := TElPGPKeyring.Create(nil);
try
TempKeyring.Load(OpenDlg.Filename, '', True);
if (Action = ACTION_PGPMIME_VERIFY) and (TempKeyring.PublicCount > 0) then
TempKeyring.ExportTo(FKeyring);
if (Action = ACTION_PGPMIME_DECRYPT) and (TempKeyring.SecretCount > 0) then
TempKeyring.ExportTo(FKeyring);
finally
TempKeyring.Free;
end;
UpdateKeysList;
end;
end;
procedure TmailPGP1.btnBackClick(Sender: TObject);
begin
Back;
end;
procedure TmailPGP1.btnCancelClick(Sender: TObject);
begin
Close;
end;
procedure TmailPGP1.btnNextClick(Sender: TObject);
begin
Next;
end;
procedure TmailPGP1.btnRemoveCertificateClick(Sender: TObject);
begin
if lbxCertificates.ItemIndex >= 0 then
begin
FMemoryCertStorage.Remove(lbxCertificates.ItemIndex);
UpdateCertificatesList;
end;
end;
procedure TmailPGP1.btnRemoveKeyClick(Sender: TObject);
begin
if Assigned(tvKeys.Selected) and Assigned(tvKeys.Selected.Data) then
begin
if TObject(tvKeys.Selected.Data) is TElPGPPublicKey then
begin
if Assigned(TElPGPPublicKey(tvKeys.Selected.Data).SecretKey) then
FKeyring.RemoveSecretkey(TElPGPPublicKey(tvKeys.Selected.Data).SecretKey)
else
FKeyring.RemovePublickey(TElPGPPublicKey(tvKeys.Selected.Data));
end
else
if TObject(tvKeys.Selected.Data) is TElPGPSecretKey then
FKeyring.RemoveSecretkey(TElPGPSecretKey(tvKeys.Selected.Data));
UpdateKeysList;
end;
end;
procedure TmailPGP1.ClearData;
begin
edInputFile.Text := '';
edOutputFile.Text := '';
FMemoryCertStorage.Clear;
lbxCertificates.Clear;
rbTripleDES.Checked := True;
rbSHA1.Checked := True;
FPublicRing.Clear;
FSecretRing.Clear;
FKeyring.Clear;
edKeyring.Text := '';
tvKeys.Items.Clear;
mmInfo.Clear;
mmResult.Clear;
end;
procedure TmailPGP1.FormCreate(Sender: TObject);
begin
FMemoryCertStorage := TElMemoryCertStorage.Create(nil);
FKeyring := TElPGPKeyring.Create(nil);
FSecretRing := TElPGPKeyring.Create(nil);
FPublicRing := TElPGPKeyring.Create(nil);
SetPage(PAGE_DEFAULT);
end;
procedure TmailPGP1.FormDestroy(Sender: TObject);
begin
FreeAndNil(FPublicRing);
FreeAndNil(FSecretRing);
FreeAndNil(FKeyring);
FreeAndNil(FMemoryCertStorage);
end;
function TmailPGP1.GetAlgorithm: Integer;
begin
if rbDES.Checked then
Result := SB_ALGORITHM_CNT_DES
else if rbTripleDES.Checked then
Result := SB_ALGORITHM_CNT_3DES
else if rbRC2.Checked then
Result := SB_ALGORITHM_CNT_RC2
else if rbRC4_40.Checked or rbRC4_128.Checked then
Result := SB_ALGORITHM_CNT_RC4
else if rbAES_128.Checked then
Result := SB_ALGORITHM_CNT_AES128
else if rbAES_192.Checked then
Result := SB_ALGORITHM_CNT_AES192
else if rbAES_256.Checked then
Result := SB_ALGORITHM_CNT_AES256
else
Result := SB_ALGORITHM_CNT_3DES;
end;
function TmailPGP1.GetAlgorithmBitsInKey: Integer;
begin
// this is only for SB_ALGORITHM_CNT_RC2 or SB_ALGORITHM_CNT_RC4
if rbRC4_40.Checked then
Result := 40
else
Result := 128;
end;
function TmailPGP1.GetAlgorithmName: string;
begin
if rbDES.Checked then
Result := rbDES.Caption
else if rbTripleDES.Checked then
Result := rbTripleDES.Caption
else if rbRC2.Checked then
Result := rbRC2.Caption
else if rbRC4_40.Checked then
Result := rbRC4_40.Caption
else if rbRC4_128.Checked then
Result := rbRC4_128.Caption
else if rbAES_128.Checked then
Result := rbAES_128.Caption
else if rbAES_192.Checked then
Result := rbAES_192.Caption
else if rbAES_256.Checked then
Result := rbAES_256.Caption
else
Result := rbTripleDES.Caption;
end;
function TmailPGP1.GetSignAlgorithm: string;
begin
if rbMD5.Checked then
Result := 'MD5'
else
Result := 'SHA1';
end;
procedure TmailPGP1.Next;
begin
if not Assigned(PageControl.ActivePage) then
begin
SetPage(PAGE_DEFAULT);
Exit;
end;
if (CurrentPage = PAGE_SELECT_ACTION) and
((rbSMimeEncrypt.Checked and (Action <> ACTION_SMIME_ENCRYPT)) or
(rbSMimeDecrypt.Checked and (Action <> ACTION_SMIME_DECRYPT)) or
(rbSMimeSign.Checked and (Action <> ACTION_SMIME_SIGN)) or
(rbSMimeVerify.Checked and (Action <> ACTION_SMIME_VERIFY)) or
(rbPGPMimeEncrypt.Checked and (Action <> ACTION_PGPMIME_ENCRYPT)) or
(rbPGPMimeDecrypt.Checked and (Action <> ACTION_PGPMIME_DECRYPT)) or
(rbPGPMimeSign.Checked and (Action <> ACTION_PGPMIME_SIGN)) or
(rbPGPMimeVerify.Checked and (Action <> ACTION_PGPMIME_VERIFY)) ) then
begin
if rbSMimeEncrypt.Checked then
Action := ACTION_SMIME_ENCRYPT
else if rbSMimeDecrypt.Checked then
Action := ACTION_SMIME_DECRYPT
else if rbSMimeSign.Checked then
Action := ACTION_SMIME_SIGN
else if rbSMimeVerify.Checked then
Action := ACTION_SMIME_VERIFY
else if rbPGPMimeEncrypt.Checked then
Action := ACTION_PGPMIME_ENCRYPT
else if rbPGPMimeDecrypt.Checked then
Action := ACTION_PGPMIME_DECRYPT
else if rbPGPMimeSign.Checked then
Action := ACTION_PGPMIME_SIGN
else if rbPGPMimeVerify.Checked then
Action := ACTION_PGPMIME_VERIFY
else
Action := ACTION_UNKNOWN;
ClearData;
end;
case Action of
ACTION_SMIME_ENCRYPT: SMimeEncryptNext;
ACTION_SMIME_DECRYPT: SMimeDecryptNext;
ACTION_SMIME_SIGN: SMimeSignNext;
ACTION_SMIME_VERIFY: SMimeVerifyNext;
ACTION_PGPMIME_ENCRYPT: PGPEncryptNext;
ACTION_PGPMIME_DECRYPT: PGPDecryptNext;
ACTION_PGPMIME_SIGN: PGPSignNext;
ACTION_PGPMIME_VERIFY: PGPVerifyNext;
else
SetPage(PAGE_DEFAULT);
end;
end;
function TmailPGP1.PGPDecrypt(const InputFileName, OutputFileName: string): string;
var
Msg: TElMessage;
MainPart: TElMessagePart;
Stream: TFileStream;
Res: Integer;
begin
Result := '';
Msg := TElMessage.Create(cXMailerDemoFieldValue);
Stream := TFileStream.Create(InputFileName, fmOpenRead or fmShareExclusive);
try
Res := Msg.ParseMessage(Stream, '', '',
{$IFDEF DELPHI_NET}
mpoStoreStream + mpoLoadData + mpoCalcDataSize,
{$ELSE}
[mpoStoreStream, mpoLoadData, mpoCalcDataSize],
{$ENDIF}
False, False, False);
except
on E: Exception do
begin
Result := E.Message;
Res := EL_ERROR;
end;
end;
if (Res = EL_OK) or (Res = EL_WARNING) then
begin
if not Assigned(Msg.MainPart) or
not Assigned(Msg.MainPart.MessagePartHandler) or
Msg.MainPart.IsActivatedMessagePartHandler then
begin
Result := 'Mesage not encoded. No action done.';
Stream.Free;
Msg.Free;
Exit;
end;
if Msg.MainPart.MessagePartHandler.IsError then
begin
Result := Msg.MainPart.MessagePartHandler.ErrorText;
Res := EL_ERROR;
end
else
begin
if Msg.MainPart.MessagePartHandler is TElMessagePartHandlerPGPMime then
begin
with TElMessagePartHandlerPGPMime(Msg.MainPart.MessagePartHandler) do
begin
DecryptingKeys := FKeyring;
OnKeyPassphrase := PGPMIMEKeyPassphrase;
end;
try
Res := Msg.MainPart.MessagePartHandler.Decode(True);
except
on E: Exception do
begin
Result := E.Message;
Res := EL_ERROR;
end;
end;
end
else
begin
Result := 'Unknown message handler.';
Res := EL_ERROR;
end;
end;
end;
Stream.Free;
if (Res <> EL_OK) and (Res <> EL_WARNING) then
begin
if Result <> '' then
Result := 'Message: "' + Result + '"'
else
if (Res = EL_HANDLERR_ERROR) and Assigned(Msg.MainPart.MessagePartHandler) then
Result := 'Message: "' + Msg.MainPart.MessagePartHandler.ErrorText + '"';
Result := Format('Error parsing mime message "%s".'#13#10'ElMime error code: %d'#13#10'%s',
[InputFileName, Res, Result]);
Msg.Free;
Exit;
end;
MainPart := Msg.MainPart.MessagePartHandler.DecodedPart;
Msg.MainPart.MessagePartHandler.DecodedPart := nil;
Msg.SetMainPart(MainPart, False);
Stream := TFileStream.Create(OutputFileName, fmCreate or fmShareExclusive);
Stream.Size := 0;
try
Res := Msg.AssembleMessage(Stream,
// Charset of message:
'utf-8',
// HeaderEncoding
heBase64, // variants: he8bit | heQuotedPrintable | heBase64
// BodyEncoding
'base64', // variants: '8bit' | 'quoted-printable' | 'base64'
// AttachEncoding
'base64' // variants: '8bit' | 'quoted-printable' | 'base64'
);
except
on E: Exception do
begin
Result := E.Message;
Res := EL_ERROR;
end;
end;
if (Res = EL_OK) or (Res = EL_WARNING) then
Result := 'Message decrypted and assembled OK'
else
begin
if Result <> '' then
Result := 'Message: "' + Result + '"'
else
if (Res = EL_HANDLERR_ERROR) and Assigned(Msg.MainPart.MessagePartHandler) then
Result := 'Message: "' + Msg.MainPart.MessagePartHandler.ErrorText + '"';
Result := Format('Failed to assemble a message.'#13#10'ElMime error code: %d'#13#10'%s', [Res, Result]);
end;
Stream.Free;
Msg.Free;
end;
procedure TmailPGP1.PGPDecryptNext;
var
NextPage: Integer;
begin
NextPage := -1;
case CurrentPage of
PAGE_SELECT_ACTION:
begin
NextPage := PAGE_SELECT_FILES;
end;
PAGE_SELECT_FILES:
begin
if (edInputFile.Text = '') or (edOutputFile.Text = '') then
MessageDlg(sSelectInputOutputFiles, mtError, [mbOk], 0)
else
NextPage := PAGE_SELECT_KEYS;
end;
PAGE_SELECT_KEYS:
begin
if FKeyring.SecretCount = 0 then
MessageDlg('No recipient secret keys selected. Please select one.', mtError, [mbOk], 0)
else
NextPage := PAGE_CHECK_DATA;
end;
PAGE_CHECK_DATA:
begin
NextPage := PAGE_PROCESS;
end;
else
NextPage := PAGE_DEFAULT;
end;
if NextPage >= 0 then
SetPage(NextPage);
if NextPage = PAGE_PROCESS then
begin
Application.ProcessMessages;
SetResults( PGPDecrypt(edInputFile.Text, edOutputFile.Text) );
end;
end;
function TmailPGP1.PGPEncrypt(const InputFileName, OutputFileName: string): string;
var
Msg: TElMessage;
Stream: TFileStream;
PGPMime: TElMessagePartHandlerPGPMime;
Res: Integer;
begin
Result := '';
Msg := TElMessage.Create(cXMailerDemoFieldValue);
Stream := TFileStream.Create(InputFileName, fmOpenRead or fmShareExclusive);
try
Res := Msg.ParseMessage(Stream, '', '',
{$IFDEF DELPHI_NET}
mpoStoreStream + mpoLoadData + mpoCalcDataSize,
{$ELSE}
[mpoStoreStream, mpoLoadData, mpoCalcDataSize],
{$ENDIF}
False, False, True);
except
on E: Exception do
begin
Result := E.Message;
Res := EL_ERROR;
end;
end;
Stream.Free;
if (Res <> EL_OK) and (Res <> EL_WARNING) then
begin
if Result <> '' then
Result := 'Message: "' + Result + '"'
else
if (Res = EL_HANDLERR_ERROR) and Assigned(Msg.MainPart.MessagePartHandler) then
Result := 'Message: "' + Msg.MainPart.MessagePartHandler.ErrorText + '"';
Result := Format('Error parsing mime message "%s".'#13#10'ElMime error code: %d'#13#10'%s',
[InputFileName, Res, Result]);
Msg.Free;
Exit;
end;
PGPMime := TElMessagePartHandlerPGPMime.Create(nil);
Msg.MainPart.MessagePartHandler := PGPMime;
PGPMime.EncryptingKeys := FPublicRing;
// PGPMime.OnKeyPassphrase := PGPMIMEKeyPassphrase;
PGPMime.Encrypt := True;
Stream := TFileStream.Create(OutputFileName, fmCreate or fmShareExclusive);
Stream.Size := 0;
try
Res := Msg.AssembleMessage(Stream,
// Charset of message:
'utf-8',
// HeaderEncoding
heBase64, // variants: he8bit | heQuotedPrintable | heBase64
// BodyEncoding
'base64', // variants: '8bit' | 'quoted-printable' | 'base64'
// AttachEncoding
'base64' // variants: '8bit' | 'quoted-printable' | 'base64'
);
except
on E: Exception do
begin
Result := E.Message;
Res := EL_ERROR;
end;