-
Notifications
You must be signed in to change notification settings - Fork 1
/
SBPGPMIME.pas
1575 lines (1442 loc) · 55 KB
/
SBPGPMIME.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
(******************************************************)
(* *)
(* EldoS SecureBlackbox Library *)
(* *)
(* Copyright (c) 2002-2007 EldoS Corporation *)
(* http://www.secureblackbox.com *)
(* *)
(******************************************************)
unit SBPGPMIME;
{$I SecBbox.inc}
interface
uses
SysUtils,
Classes,
SBUtils,
SBMIME,
SBMIMETypes,
SBMIMEStream,
SBMIMEUtils,
SBSimpleMIME,
SBPGPConstants,
SBPGPKeys,
SBPGP,
SBPGPUtils,
SBPGPStreams;
type
TSBPGPMIMEType = (pmtUnknown, pmtSigned, pmtEncrypted, pmtSignedEncrypted);
TSBPGPMIMEError = (pmeUnknown, pmePGPPartNotFound, pmeInvalidSignature,
pmeUnverifiableSignature, pmeRecipientKeyNotFound, pmeSenderKeyNotFound,
pmeNoRecipients, pmeNoSigners, pmeActionNotSelected);
TSBPGPMIMEErrors = set of TSBPGPMIMEError;
TSBPGPKeysError = (pkeUnknown, pkeKeysPartNotFound, pkeNoPublicKeys);
TSBPGPKeysErrors = set of TSBPGPKeysError;
TSBPGPKeyNotFoundEvent = procedure(Sender: TObject; Address: TElMailAddress;
var ExcludeFromList: boolean) of object;
TElMessagePartHandlerPGPMime = class;
ElMessagePartHandlerPGPMime = TElMessagePartHandlerPGPMime;
TElMessagePartHandlerPGPMime = class(TElMessagePartHandler)
private
FDecryptingKeys : TElPGPKeyring;
FVerifyingKeys : TElPGPKeyring;
FEncryptingKeys : TElPGPKeyring;
FSigningKeys : TElPGPKeyring;
FEncrypt : boolean;
FSign : boolean;
FCompress: boolean;
FSigned : boolean;
FEncrypted : boolean;
FErrors : TSBPGPMIMEErrors;
FType : TSBPGPMIMEType;
FBoundary : AnsiString;
FDecryptedPart: TElMessagePart;
FReaderOutputStream: TMemoryStream;
FOnEncrypted : TSBPGPEncryptedEvent;
FOnSigned : TSBPGPSignedEvent;
FOnKeyPassphrase: TSBPGPKeyPassphraseEvent;
FOnPassphrase : TSBPGPPassphraseEvent;
FOnSignatures : TSBPGPSignaturesEvent;
FOnEncryptingKeyNotFound : TSBPGPKeyNotFoundEvent;
FOnSigningKeyNotFound : TSBPGPKeyNotFoundEvent;
FAssemblingOuterHeaders: boolean;
FInternalKeyrings : array[0..1] of TElPGPKeyring;
FIgnoreHeaderRecipients : boolean;
FIgnoreHeaderSigners : boolean;
FCompressionAlgorithm: integer;
FCompressionLevel: integer;
FHashAlgorithm: integer;
FSymmetricKeyAlgorithm: TSBPGPSymmetricKeyAlgorithm;
FUseNewFeatures: boolean;
FUseOldPackets: boolean;
FProtection : TSBPGPProtectionType;
FEncryptionType : TSBPGPEncryptionType;
FPassphrases : TElStringList;
procedure SetPassphrases(Value : TStringList);
procedure SetDecryptingKeys(Value: TElPGPKeyring);
procedure SetVerifyingKeys(Value: TElPGPKeyring);
procedure SetEncryptingKeys(Value: TElPGPKeyring);
procedure SetSigningKeys(Value: TElPGPKeyring);
procedure HandleReaderEncrypted(Sender: TObject; const KeyIDs : TSBKeyIDs;
IntegrityProtected: boolean; PassphraseUsed: boolean);
procedure HandleReaderCreateOutputStream(Sender: TObject; const Filename : string;
TimeStamp: TDateTime; var Stream: TStream; var FreeOnExit: boolean);
procedure HandleReaderKeyPassphrase(Sender: TObject;
Key : TElPGPCustomSecretKey; var Passphrase: string; var Cancel: boolean);
procedure HandleReaderPassphrase(Sender: TObject;
var Passphrase: string; var Cancel : boolean);
procedure HandleReaderSigned(Sender: TObject;
const KeyIDs : TSBKeyIDs; SignatureType : TSBPGPSignatureType);
{$ifndef BUILDER_USED}
procedure HandleReaderSignatures(Sender: TObject;
Signatures: array of TElPGPSignature; Validities : array of TSBPGPSignatureValidity);
{$else}
procedure HandleReaderSignatures(Sender: TObject;
Signatures: TList; Validities : array of TSBPGPSignatureValidity);
{$endif}
procedure HandleWriterKeyPassphrase(Sender: TObject;
Key : TElPGPCustomSecretKey; var Passphrase: string; var Cancel: boolean);
protected
class function IsText(const wsContentType: TWideString;
ContentTypeField: TElMessageHeaderField; Header: TElMessageHeader): Boolean; override;
class function IsTextPlain(const wsContentType: TWideString;
ContentTypeField: TElMessageHeaderField; Header: TElMessageHeader): Boolean; override;
class function IsMultipart(const wsContentType: TWideString;
ContentTypeField: TElMessageHeaderField; Header: TElMessageHeader): Boolean; override;
class function IsSupportedThisContentType(const wsContentType: TWideString;
ContentTypeField: TElMessageHeaderField; Header: TElMessageHeader): Boolean; override;
procedure Clear; override;
function Clone: TElMessagePartHandler; override;
function OnAssemble(Destination: TAnsiStringStream;
const Charset: AnsiString; HeaderEncoding: TElHeaderEncoding;
const BodyEncoding, AttachEncoding: AnsiString;
State: TElOnAssembleState; Item: Integer; const sBoundary: AnsiString;
var bIsHandled, bStopAssemble: Boolean
): ELMIMERESULT; override;
function OnParse(Source: TElNativeStream;
const Boundary, HeaderCharset, BodyCharset: AnsiString;
AOptions: TElMessageParsingOptions; IgnoreHeaderNativeCharset,
IgnoreBodyNativeCharset, bActivatePartHandlers: Boolean;
State: TElOnParseState; Item: Integer;
var bIsHandled, bStopParse: Boolean
): ELMIMERESULT; override;
procedure SetError; override;
function DecodePGPMime( const HeaderCharset,
BodyCharset: AnsiString; AOptions: TElMessageParsingOptions;
IgnoreHeaderNativeCharset, IgnoreBodyNativeCharset: Boolean;
bActivatePartHandlers: Boolean {$ifdef HAS_DEF_PARAMS}= False{$endif};
RootHandler: TElMessagePartHandlerPGPMime {$ifdef HAS_DEF_PARAMS}= nil {$endif}): Boolean;
public
class function GetDescription: TWideString; override;
constructor Create(aParams: TObject); override;
destructor Destroy; override;
function IsSigned : boolean;
function IsEncrypted : boolean;
property Encrypt : boolean read FEncrypt write FEncrypt default true;
property Sign : boolean read FSign write FSign default false;
property Compress: boolean read FCompress write FCompress default false;
property IgnoreHeaderRecipients: boolean read FIgnoreHeaderRecipients
write FIgnoreHeaderRecipients default false;
property IgnoreHeaderSigners: boolean read FIgnoreHeaderSigners
write FIgnoreHeaderSigners default false;
property DecryptingKeys : TElPGPKeyring read FDecryptingKeys write SetDecryptingKeys;
property VerifyingKeys : TElPGPKeyring read FVerifyingKeys write SetVerifyingKeys;
property EncryptingKeys : TElPGPKeyring read FEncryptingKeys write SetEncryptingKeys;
property SigningKeys : TElPGPKeyring read FSigningKeys write SetSigningKeys;
property EncryptionType : TSBPGPEncryptionType read FEncryptionType write FEncryptionType;
property Passphrases : TStringList read FPassphrases write SetPassphrases;
property Protection : TSBPGPProtectionType read FProtection write FProtection default ptNormal;
property CompressionAlgorithm: integer read FCompressionAlgorithm write
FCompressionAlgorithm;
property CompressionLevel : integer read FCompressionLevel write FCompressionLevel;
property HashAlgorithm: integer read FHashAlgorithm write FHashAlgorithm;
property SymmetricKeyAlgorithm: TSBPGPSymmetricKeyAlgorithm read
FSymmetricKeyAlgorithm write FSymmetricKeyAlgorithm;
property UseNewFeatures: boolean read FUseNewFeatures write FUseNewFeatures;
property UseOldPackets: boolean read FUseOldPackets write FUseOldPackets;
property OnEncrypted : TSBPGPEncryptedEvent read FOnEncrypted write FOnEncrypted;
property OnSigned : TSBPGPSignedEvent read FOnSigned write FOnSigned;
property OnKeyPassphrase : TSBPGPKeyPassphraseEvent read FOnKeyPassphrase
write FOnKeyPassphrase;
property OnPassphrase: TSBPGPPassphraseEvent read FOnPassphrase
write FOnPassphrase;
property OnSignatures : TSBPGPSignaturesEvent read FOnSignatures
write FOnSignatures;
property OnEncryptingKeyNotFound : TSBPGPKeyNotFoundEvent read
FOnEncryptingKeyNotFound write FOnEncryptingKeyNotFound;
property OnSigningKeyNotFound : TSBPGPKeyNotFoundEvent read
FOnSigningKeyNotFound write FOnSigningKeyNotFound;
end;
TElMessagePartHandlerPGPKeys = class;
ElMessagePartHandlerPGPKeys = TElMessagePartHandlerPGPKeys;
TElMessagePartHandlerPGPKeys = class(TElMessagePartHandler)
private
FKeys : TElPGPKeyring;
FErrors : TSBPGPKeysErrors;
protected
class function IsText(const wsContentType: TWideString;
ContentTypeField: TElMessageHeaderField; Header: TElMessageHeader): Boolean; override;
class function IsTextPlain(const wsContentType: TWideString;
ContentTypeField: TElMessageHeaderField; Header: TElMessageHeader): Boolean; override;
class function IsMultipart(const wsContentType: TWideString;
ContentTypeField: TElMessageHeaderField; Header: TElMessageHeader): Boolean; override;
class function IsSupportedThisContentType(const wsContentType: TWideString;
ContentTypeField: TElMessageHeaderField; Header: TElMessageHeader): Boolean; override;
procedure Clear; override;
function Clone: TElMessagePartHandler; override;
function OnAssemble(Destination: TAnsiStringStream;
const Charset: AnsiString; HeaderEncoding: TElHeaderEncoding;
const BodyEncoding, AttachEncoding: AnsiString;
State: TElOnAssembleState; Item: Integer; const sBoundary: AnsiString;
var bIsHandled, bStopAssemble: Boolean
): ELMIMERESULT; override;
function OnParse(Source: TElNativeStream;
const Boundary, HeaderCharset, BodyCharset: AnsiString;
AOptions: TElMessageParsingOptions; IgnoreHeaderNativeCharset,
IgnoreBodyNativeCharset, bActivatePartHandlers: Boolean;
State: TElOnParseState; Item: Integer;
var bIsHandled, bStopParse: Boolean
): ELMIMERESULT; override;
function DecodeKeys : boolean;
procedure SetError; override;
public
class function GetDescription: TWideString; override;
constructor Create(aParams: TObject); override;
destructor Destroy; override;
property Keys : TElPGPKeyring read FKeys;
end;
TElSimplePGPMIMEOptions = class;
ElSimplePGPMIMEOptions = TElSimplePGPMIMEOptions;
TElSimplePGPMIMEOptions = class(TPersistent)
protected
FCompressMessage : boolean;
FEncryptMessage : boolean;
FIgnoreHeaderRecipients: boolean;
FIgnoreHeaderSigners: boolean;
FSignMessage : boolean;
FHashAlgorithm : integer;
FCompressionAlgorithm : integer;
FCompressionLevel : integer;
FSymmetricKeyAlgorithm: TSBPGPSymmetricKeyAlgorithm;
FUseOldPackets : boolean;
FUseNewFeatures : boolean;
FProtection : TSBPGPProtectionType;
FEncryptionType : TSBPGPEncryptionType;
FPassphrases : TStringList;
procedure SetPassphrases(Value : TStringList);
public
procedure Assign(Source : TPersistent); override;
constructor Create;
destructor Destroy; override;
published
property EncryptionType : TSBPGPEncryptionType read FEncryptionType write FEncryptionType;
property Passphrases : TStringList read FPassphrases write SetPassphrases;
property Protection : TSBPGPProtectionType read FProtection write FProtection default ptNormal;
property HashAlgorithm : integer read FHashAlgorithm write FHashAlgorithm;
property CompressionAlgorithm : integer read FCompressionAlgorithm write FCompressionAlgorithm;
property CompressionLevel : integer read FCompressionLevel write FCompressionLevel;
property SymmetricKeyAlgorithm: TSBPGPSymmetricKeyAlgorithm read FSymmetricKeyAlgorithm
write FSymmetricKeyAlgorithm default SB_PGP_ALGORITHM_SK_CAST5;
property UseOldPackets : boolean read FUseOldPackets write FUseOldPackets default false;
property UseNewFeatures : boolean read FUseNewFeatures write FUseNewFeatures default true;
property CompressMessage: boolean read FCompressMessage write FCompressMessage;
property EncryptMessage : boolean read FEncryptMessage write FEncryptMessage;
property IgnoreHeaderRecipients: boolean read FIgnoreHeaderRecipients write
FIgnoreHeaderRecipients;
property IgnoreHeaderSigners: boolean read FIgnoreHeaderSigners write
FIgnoreHeaderSigners;
property SignMessage : boolean read FSignMessage write FSignMessage;
end;
TElSimplePGPMIMEMessage = class;
ElSimplePGPMIMEMessage = TElSimplePGPMIMEMessage;
TElSimplePGPMIMEMessage = class(TElSimpleMIMEMessage)
protected
FEncryptingKeys: TElPGPKeyring;
FSigningKeys: TElPGPKeyring;
FOnEncryptingKeyNotFound: TSBPGPKeyNotFoundEvent;
FOnKeyPassphrase: TSBPGPKeyPassphraseEvent;
FOnSigningKeyNotFound: TSBPGPKeyNotFoundEvent;
FPGPMIMEOptions: TElSimplePGPMIMEOptions;
procedure SetEncryptingKeys(Value: TElPGPKeyring);
procedure SetSigningKeys(Value: TElPGPKeyring);
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
function DoAssemble(Stream: TStream): Integer; override;
procedure SetPGPMIMEOptions(const Value: TElSimplePGPMIMEOptions);
public
constructor Create(AOwner: TSBComponentBase); override;
destructor Destroy; override;
published
property EncryptingKeys: TElPGPKeyring read FEncryptingKeys write
SetEncryptingKeys;
property PGPMIMEOptions: TElSimplePGPMIMEOptions read FPGPMIMEOptions write
SetPGPMIMEOptions;
property SigningKeys: TElPGPKeyring read FSigningKeys write SetSigningKeys;
property OnEncryptingKeyNotFound: TSBPGPKeyNotFoundEvent read
FOnEncryptingKeyNotFound write FOnEncryptingKeyNotFound;
property OnKeyPassphrase: TSBPGPKeyPassphraseEvent read FOnKeyPassphrase write
FOnKeyPassphrase;
property OnSigningKeyNotFound: TSBPGPKeyNotFoundEvent read
FOnSigningKeyNotFound write FOnSigningKeyNotFound;
end;
EElPGPMIMEException = class(ElMimePartHandlerException);
procedure Initialize;
implementation
////////////////////////////////////////////////////////////////////////////////
// TElMessagePartHandlerPGPMime class
constructor TElMessagePartHandlerPGPMime.Create(aParams: TObject);
begin
inherited Create(aParams);
FReaderOutputStream := TMemoryStream.Create;
FInternalKeyrings[0] := TElPGPKeyring.Create(nil);
FInternalKeyrings[1] := TElPGPKeyring.Create(nil);
FPassphrases := TElStringList.Create;
FEncryptionType := etPublicKey;
FProtection := ptNormal;
FSymmetricKeyAlgorithm := SB_PGP_ALGORITHM_SK_CAST5;
Clear;
end;
destructor TElMessagePartHandlerPGPMime.Destroy;
begin
FReaderOutputStream.Free;
FInternalKeyrings[0].Free;
FInternalKeyrings[1].Free;
FPassphrases.Free;
inherited;
end;
procedure TElMessagePartHandlerPGPMime.Clear;
begin
inherited;
FEncrypt := true;
FSign := false;
FType := pmtUnknown;
FErrors := [];
FSigned := false;
FEncrypted := false;
FSign := false;
FEncrypt := false;
FCompress := false;
FReaderOutputStream.Clear;
FInternalKeyrings[0].Clear;
FInternalKeyrings[1].Clear;
FIgnoreHeaderRecipients := false;
FIgnoreHeaderSigners := false;
end;
function TElMessagePartHandlerPGPMime.Clone: TElMessagePartHandler;
var
ph : TElMessagePartHandlerPGPMIME;
begin
ph := TElMessagePartHandlerPGPMime.Create(nil);
Result := ph;
ph.FDecryptingKeys := FDecryptingKeys;
ph.FVerifyingKeys := FVerifyingKeys;
ph.FEncryptingKeys := FEncryptingKeys;
ph.FSigningKeys := FSigningKeys;
ph.FEncrypt := FEncrypt;
ph.FSign := FSign;
ph.FCompress := FCompress;
ph.FSigned := FSigned;
ph.FEncrypted := FEncrypted;
ph.FType := FType;
ph.FBoundary := FBoundary;
ph.FErrors := FErrors;
ph.FIgnoreHeaderRecipients := FIgnoreHeaderRecipients;
ph.FIgnoreHeaderSigners := FIgnoreHeaderSigners;
if Assigned(FDecryptedPart) then
ph.FDecryptedPart := FDecryptedPart.Clone;
end;
function TElMessagePartHandlerPGPMime.OnAssemble(Destination: TAnsiStringStream;
const Charset: AnsiString; HeaderEncoding: TElHeaderEncoding;
const BodyEncoding, AttachEncoding: AnsiString;
State: TElOnAssembleState; Item: Integer; const sBoundary: AnsiString;
var bIsHandled, bStopAssemble: Boolean
): ELMIMERESULT;
var
h: TElMessageHeaderField;
function FindEncryptingKeyByAddress(const Address: string): TElPGPPublicKey;
var
Index: integer;
begin
Result := nil;
Index := -1;
repeat
Index := FEncryptingKeys.FindPublicKeyByEmailAddress(Address, Index + 1);
if (Index >= 0) then
begin
if FEncryptingKeys.PublicKeys[Index].IsEncryptingKey(true) then
begin
Result := FEncryptingKeys.PublicKeys[Index];
Break;
end;
end;
until Index = -1;
end;
function FindSigningKeyByAddress(const Address: string): TElPGPSecretKey;
var
Index: integer;
begin
Result := nil;
Index := -1;
repeat
Index := FSigningKeys.FindSecretKeyByEmailAddress(Address, Index + 1);
if Index >= 0 then
begin
if FSigningKeys.SecretKeys[Index].IsSigningKey(true) then
begin
Result := FSigningKeys.SecretKeys[Index];
Break;
end;
end;
until Index = -1;
end;
function CheckRecipientKeysForAvailability(List : TElMailAddressList; Storage:
TElPGPKeyring) : boolean;
var
PublicKey : TElPGPPublicKey;
I : integer;
Exclude : boolean;
begin
Result := true;
for I := 0 to List.Count - 1 do
begin
PublicKey := FindEncryptingKeyByAddress(List.GetAddress(I).Address);
if PublicKey = nil then
begin
Exclude := false;
if Assigned(FOnEncryptingKeyNotFound) then
FOnEncryptingKeyNotFound(Self, List.GetAddress(I),
Exclude);
if not Exclude then
begin
Result := false;
Break;
end;
end
else
Storage.AddPublicKey(PublicKey);
end;
end;
function CheckSenderKeysForAvailability(List : TElMailAddressList; Storage:
TElPGPKeyring) : boolean;
var
SecretKey : TElPGPSecretKey;
I : integer;
Exclude : boolean;
begin
Result := true;
for I := 0 to List.Count - 1 do
begin
SecretKey := FindSigningKeyByAddress(List.GetAddress(I).Address);
if SecretKey = nil then
begin
Exclude := false;
if Assigned(FOnSigningKeyNotFound) then
FOnSigningKeyNotFound(Self, List.GetAddress(I),
Exclude);
if not Exclude then
begin
Result := false;
Break;
end;
end
else
Storage.AddSecretKey(SecretKey);
end;
end;
procedure Encrypt;
var
Writer : TElPGPWriter;
Dest : TAnsiStringStream;
Hdr : AnsiString;
EncKeyring : TElPGPKeyring;
const
Boundary = 'PGP MESSAGE';
BodyStub = 'This is an OpenPGP/MIME encrypted message (RFC 2440 and 3156)'#13#10;
VersionPart = 'Content-Type: application/pgp-encrypted'#13#10'Content-Description: PGP/MIME version identification'#13#10#13#10'Version: 1'#13#10#13#10;
EncPartHeaders = 'Content-Type: application/octet-stream; name="encrypted.asc"'#13#10'Content-Description: OpenPGP encrypted message'#13#10'Content-Disposition: inline; filename="encrypted.asc"'#13#10#13#10;
begin
if not FIgnoreHeaderRecipients then
begin
if FElMessagePart.ParentMessage = nil then
begin
FErrors := FErrors + [pmeNoRecipients];
Exit;
end;
FInternalKeyrings[0].Clear;
if (not CheckRecipientKeysForAvailability(FElMessagePart.ParentMessage.To_,
FInternalKeyrings[0])) or
(not CheckRecipientKeysForAvailability(FElMessagePart.ParentMessage.CC,
FInternalKeyrings[0])) or
(not CheckRecipientKeysForAvailability(FElMessagePart.ParentMessage.BCC,
FInternalKeyrings[0])) then
begin
FErrors := FErrors + [pmeRecipientKeyNotFound];
Exit;
end;
EncKeyring := FInternalKeyrings[0];
end
else
EncKeyring := FEncryptingKeys;
Writer := TElPGPWriter.Create(nil);
try
Writer.EncryptingKeys := EncKeyring;
Writer.Timestamp := Now;
Writer.Armor := true;
Writer.EncryptionType := EncryptionType;
Writer.Protection := Protection;
Writer.Passphrases.Assign(Passphrases);
Writer.ArmorBoundary := Boundary;
Writer.ArmorHeaders.Add('Version: StalkerSTS');
Writer.Filename := '';
Writer.Compress := FCompress;
Writer.OnKeyPassphrase := HandleWriterKeyPassphrase;
Destination.Position := 0;
Dest := TAnsiStringStream.Create;
try
Writer.Encrypt(Destination, Dest {$ifndef HAS_DEF_PARAMS}, 0{$endif});
FAssemblingOuterHeaders := true;
try
FElMessagePart.Header.Assemble(Hdr, Charset, HeaderEncoding, Self);
finally
FAssemblingOuterHeaders := false;
end;
Hdr := Hdr + #13#10;
Destination.Clear {$ifndef HAS_DEF_PARAMS}(0){$endif};
WriteStringToStream(Hdr, Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStringToStream((BodyStub), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStringToStream(('--' + fBoundary + #13#10), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStringToStream((VersionPart), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStringToStream(('--' + fBoundary + #13#10), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStringToStream((EncPartHeaders), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
Dest.Position := 0;
WriteStreamToStream(Dest, Destination {$ifndef HAS_DEF_PARAMS}, -1, nil{$endif});
WriteStringToStream((#13#10#13#10'--' + fBoundary + '--'#13#10), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
finally
Dest.Free;
end;
finally
Writer.Free;
end;
end;
procedure Sign;
var
Writer : TElPGPWriter;
Dest, Temp : TAnsiStringStream;
Hdr : AnsiString;
SgnKeyring : TElPGPKeyring;
const
Boundary = 'PGP SIGNATURE';
BodyStub = 'This is an OpenPGP/MIME signed message (RFC 2440 and 3156)'#13#10;
SigPartHeaders = 'Content-Type: application/pgp-signature; name="signature.asc"'#13#10'Content-Description: OpenPGP digital signature'#13#10'Content-Disposition: attachment; filename="signature.asc"'#13#10#13#10;
begin
if not FIgnoreHeaderSigners then
begin
if FElMessagePart.ParentMessage = nil then
begin
FErrors := FErrors + [pmeNoSigners];
Exit;
end;
FInternalKeyrings[0].Clear;
if (not CheckSenderKeysForAvailability(FElMessagePart.ParentMessage.From,
FInternalKeyrings[0])) then
begin
FErrors := FErrors + [pmeSenderKeyNotFound];
Exit;
end;
SgnKeyring := FInternalKeyrings[0];
end
else
SgnKeyring := FSigningKeys;
Writer := TElPGPWriter.Create(nil);
try
Writer.SigningKeys := SgnKeyring;
Writer.Timestamp := Now;
Writer.Armor := true;
Writer.ArmorBoundary := Boundary;
Writer.ArmorHeaders.Add('Version: StalkerSTS');
Writer.Filename := '';
Writer.InputIsText := true;
Writer.OnKeyPassphrase := HandleWriterKeyPassphrase;
Destination.Position := 0;
Dest := TAnsiStringStream.Create;
try
Writer.Sign(Destination, Dest, true {$ifndef HAS_DEF_PARAMS}, 0{$endif});
FAssemblingOuterHeaders := true;
try
FElMessagePart.Header.Assemble(Hdr, Charset, HeaderEncoding, Self);
finally
FAssemblingOuterHeaders := false;
end;
Hdr := Hdr + #13#10;
Temp := TAnsiStringStream.Create;
try
Destination.Position := 0;
WriteStreamToStream(Destination, Temp {$ifndef HAS_DEF_PARAMS}, -1, nil{$endif});
Temp.Position := 0;
Destination.Clear {$ifndef HAS_DEF_PARAMS}(0){$endif};
WriteStringToStream(Hdr, Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStringToStream((BodyStub), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStringToStream(('--' + fBoundary + #13#10), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStreamToStream(Temp, Destination {$ifndef HAS_DEF_PARAMS}, -1, nil{$endif});
WriteStringToStream((#13#10'--' + fBoundary + #13#10), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStringToStream((SigPartHeaders), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
Dest.Position := 0;
WriteStreamToStream(Dest, Destination {$ifndef HAS_DEF_PARAMS}, -1, nil{$endif});
WriteStringToStream((#13#10#13#10'--' + fBoundary + '--'#13#10), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
finally
Temp.Free;
end;
finally
Dest.Free;
end;
finally
Writer.Free;
end;
end;
procedure EncryptAndSign;
var
Writer : TElPGPWriter;
Dest : TAnsiStringStream;
Hdr : AnsiString;
EncKeyring, SgnKeyring : TElPGPKeyring;
const
Boundary = 'PGP MESSAGE';
BodyStub = 'This is an OpenPGP/MIME encrypted message (RFC 2440 and 3156)'#13#10;
VersionPart = 'Content-Type: application/pgp-encrypted'#13#10'Content-Description: PGP/MIME version identification'#13#10#13#10'Version: 1'#13#10#13#10;
EncPartHeaders = 'Content-Type: application/octet-stream; name="encrypted.asc"'#13#10'Content-Description: OpenPGP encrypted message'#13#10'Content-Disposition: inline; filename="encrypted.asc"'#13#10#13#10;
begin
if not FIgnoreHeaderRecipients then
begin
if FElMessagePart.ParentMessage = nil then
begin
FErrors := FErrors + [pmeNoRecipients];
Exit;
end;
FInternalKeyrings[0].Clear;
if (not CheckRecipientKeysForAvailability(FElMessagePart.ParentMessage.To_,
FInternalKeyrings[0])) or
(not CheckRecipientKeysForAvailability(FElMessagePart.ParentMessage.CC,
FInternalKeyrings[0])) or
(not CheckRecipientKeysForAvailability(FElMessagePart.ParentMessage.BCC,
FInternalKeyrings[0])) then
begin
FErrors := FErrors + [pmeRecipientKeyNotFound];
Exit;
end;
EncKeyring := FInternalKeyrings[0];
end
else
EncKeyring := FEncryptingKeys;
if not FIgnoreHeaderSigners then
begin
if FElMessagePart.ParentMessage = nil then
begin
FErrors := FErrors + [pmeNoSigners];
Exit;
end;
FInternalKeyrings[1].Clear;
if (not CheckSenderKeysForAvailability(FElMessagePart.ParentMessage.From,
FInternalKeyrings[1])) then
begin
FErrors := FErrors + [pmeSenderKeyNotFound];
Exit;
end;
SgnKeyring := FInternalKeyrings[1];
end
else
SgnKeyring := FSigningKeys;
Writer := TElPGPWriter.Create(nil);
try
Writer.EncryptingKeys := EncKeyring;
Writer.SigningKeys := SgnKeyring;
Writer.Timestamp := Now;
Writer.Armor := true;
Writer.Compress := FCompress;
Writer.EncryptionType := etPublicKey;
Writer.ArmorBoundary := Boundary;
Writer.ArmorHeaders.Add('Version: StalkerSTS');
Writer.Filename := '';
Writer.OnKeyPassphrase := HandleWriterKeyPassphrase;
Writer.CompressionAlgorithm := CompressionAlgorithm;
Writer.HashAlgorithm := HashAlgorithm;
Writer.SymmetricKeyAlgorithm := SymmetricKeyAlgorithm;
Writer.UseNewFeatures := UseNewFeatures;
Writer.UseOldPackets := UseOldPackets;
Destination.Position := 0;
Dest := TAnsiStringStream.Create;
try
Writer.EncryptAndSign(Destination, Dest {$ifndef HAS_DEF_PARAMS}, 0{$endif});
FAssemblingOuterHeaders := true;
try
FElMessagePart.Header.Assemble(Hdr, Charset, HeaderEncoding, Self);
finally
FAssemblingOuterHeaders := false;
end;
Hdr := Hdr + #13#10;
Destination.Clear {$ifndef HAS_DEF_PARAMS}(0){$endif};
WriteStringToStream(Hdr, Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStringToStream((BodyStub), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStringToStream(('--' + fBoundary + #13#10), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStringToStream((VersionPart), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStringToStream(('--' + fBoundary + #13#10), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStringToStream((EncPartHeaders), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
Dest.Position := 0;
WriteStreamToStream(Dest, Destination {$ifndef HAS_DEF_PARAMS}, -1, nil{$endif});
WriteStringToStream((#13#10#13#10'--' + fBoundary + '--'#13#10), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
finally
Dest.Free;
end;
finally
Writer.Free;
end;
end;
const
EncryptedContentType = 'Content-Type: multipart/encrypted;'#13#10#9'protocol="application/pgp-encrypted";'#13#10#9;
SignedContentType = 'Content-Type: multipart/signed; micalg=pgp-sha1;'#13#10#9'protocol="application/pgp-signature";'#13#10#9;
begin
Result := EL_OK;
bIsHandled := false;
if bStopAssemble then
begin
if State = astAfter then
begin
Exit;
end;
end;
if (State <> astBefore) and
(FType = pmtUnknown) then
Exit;
case State of
astBefore:
begin
FAssemblingOuterHeaders := false;
if (not (FSign or FEncrypt)) or (FElMessagePart.ParentMessage = nil) then
Exit;
if FSign and FEncrypt then
FType := pmtSignedEncrypted
else if FSign then
FType := pmtSigned
else if FEncrypt then
FType := pmtEncrypted
else
begin
FErrors := FErrors + [pmeActionNotSelected];
Result := EL_HANDLERR_ERROR;
Exit;
end;
FBoundary := TElMultiPartList.GenerateBoundary;
end;
astAfter:
begin
if not (bIsHandled or bStopAssemble) then
begin
if FType = pmtEncrypted then
Encrypt
else if FType = pmtSigned then
Sign
else if FType = pmtSignedEncrypted then
EncryptAndSign
else
FErrors := FErrors + [pmeActionNotSelected];
if FErrors <> [] then
begin
Result := EL_HANDLERR_ERROR;
Exit;
end;
end;
end;
astAddressFieldBefore:
begin
{ skipping all address fields, as they should not appear in encrypted message part }
if not FAssemblingOuterHeaders then
bIsHandled := true;
end;
astHeaderFieldItem:
begin
if FAssemblingOuterHeaders then
begin
h := fElMessagePart.Header.GetField(Item);
if Assigned(h) then
begin
if WideSameText(h.Name, 'Content-Type') then
begin
bIsHandled := true;
case FType of
pmtEncrypted,
pmtSignedEncrypted :
begin
WriteStringToStream((EncryptedContentType), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
if HeaderEncoding = he8bit then
WriteStringToStream(('charset="' + (Charset) + '";'#13#10#09), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStringToStream(('boundary="' + fBoundary + '"'#13#10), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
end;
pmtSigned:
begin
WriteStringToStream((SignedContentType), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
if HeaderEncoding = he8bit then
WriteStringToStream(('charset="' + (Charset) + '";'#13#10#09), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
WriteStringToStream(('boundary="' + fBoundary + '"'#13#10), Destination {$ifndef HAS_DEF_PARAMS}, 1, -1{$endif});
end;
end;
end
else if WideSameText(h.Name, 'Content-Transfer-Encoding') or
WideSameText(h.Name, 'Content-Disposition') then
bIsHandled := true;
end;
end
else
begin
h := fElMessagePart.Header.GetField(Item);
if Assigned(h) then
bIsHandled := not (WideSameText(h.Name, 'Content-Type')
or WideSameText(h.Name, 'Content-Transfer-Encoding')
or WideSameText(h.Name, 'Content-Disposition')
or WideSameText(h.Name, 'Content-Description')
or WideSameText(h.Name, 'Content-ID')
or WideSameText(h.Name, 'Content-Location'));
end;
end;
end;
end;
function TElMessagePartHandlerPGPMime.OnParse(Source: TElNativeStream;
const Boundary, HeaderCharset, BodyCharset: AnsiString;
AOptions: TElMessageParsingOptions; IgnoreHeaderNativeCharset,
IgnoreBodyNativeCharset, bActivatePartHandlers: Boolean;
State: TElOnParseState; Item: Integer;
var bIsHandled, bStopParse: Boolean
): ELMIMERESULT;
begin
Result := El_OK;
if (State = pstDecode) and
not (bIsHandled and bStopParse) then
begin
if DecodePGPMime(HeaderCharset, BodyCharset, AOptions,
IgnoreHeaderNativeCharset, IgnoreBodyNativeCharset, bActivatePartHandlers {$ifndef HAS_DEF_PARAMS}, nil{$endif})
then
begin
if fErrors = [pmeUnverifiableSignature] then
Result := EL_WARNING
else
if pmeInvalidSignature in fErrors then
Result := EL_HANDLERR_ERROR
else
Result := EL_OK;
end
else
begin
Result := EL_HANDLERR_ERROR;
end
end;
end;
procedure TElMessagePartHandlerPGPMime.SetError;
var
i : TSBPGPMIMEError;
begin
fErrorText := '';
for i := High(TSBPGPMIMEError) downto Low(TSBPGPMIMEError) do
begin
if i in fErrors then
case i of
pmeUnknown :
FErrorText := FErrorText + ' Unknown error.';
pmePGPPartNotFound :
FErrorText := FErrorText + ' PGP part not found.';
pmeInvalidSignature :
FErrorText := FErrorText + ' Invalid signature.';
pmeRecipientKeyNotFound :
FErrorText := FErrorText + ' Recipient''s key not found.';
pmeSenderKeyNotFound :
FErrorText := FErrorText + ' Sender''s key not found.';
pmeNoRecipients :
FErrorText := FErrorText + ' No recipients.';
pmeNoSigners :
FErrorText := FErrorText + ' No signers.';
pmeActionNotSelected :
FErrorText := FErrorText + ' Action (encryption/signing) not selected.';
pmeUnverifiableSignature:
FErrorText := FErrorText + ' Some signatures can not be verified. ';
end;
end;
end;
procedure TElMessagePartHandlerPGPMime.SetPassphrases(Value : TStringList);
begin
FPassphrases.Assign(Value);
end;
procedure TElMessagePartHandlerPGPMime.SetDecryptingKeys(Value: TElPGPKeyring);
begin
FDecryptingKeys := Value;
end;
procedure TElMessagePartHandlerPGPMime.SetVerifyingKeys(Value: TElPGPKeyring);
begin
FVerifyingKeys := Value;
end;
procedure TElMessagePartHandlerPGPMime.SetEncryptingKeys(Value: TElPGPKeyring);
begin
FEncryptingKeys := Value;
end;
procedure TElMessagePartHandlerPGPMime.SetSigningKeys(Value: TElPGPKeyring);
begin
FSigningKeys := Value;
end;
function TElMessagePartHandlerPGPMime.DecodePGPMime( const HeaderCharset,
BodyCharset: AnsiString; AOptions: TElMessageParsingOptions;
IgnoreHeaderNativeCharset, IgnoreBodyNativeCharset: Boolean;
bActivatePartHandlers: Boolean {$ifdef HAS_DEF_PARAMS}= False{$endif};
RootHandler: TElMessagePartHandlerPGPMime {$ifdef HAS_DEF_PARAMS}= nil {$endif}): Boolean;
function IsSigned(mp: TElMessagePart) : boolean;
var
h : TElMessageHeaderField;
wsParam: TWideString;
begin
Result := false;
if mp = nil then
Exit;
if (mp.IsMultipart) and (mp.PartsCount = 2) then
begin
h := mp.Header.GetField('Content-Type' {$ifndef HAS_DEF_PARAMS}, 0{$endif});
if h = nil then
Exit;
if not WideSameText(h.Value, 'multipart/signed') then
Exit;
wsParam := h.GetParamValue('micalg');
if Length(wsParam) = 0 then
Exit;
wsParam := h.GetParamValue('protocol');
if not WideSameText(wsParam, 'application/pgp-signature') then
Exit;
h := mp.GetPart(1).Header.GetField('Content-Type' {$ifndef HAS_DEF_PARAMS}, 0{$endif});
if h = nil then
Exit;
if not WideSameText(h.Value, 'application/pgp-signature') then
Exit;
Result := True;
end;
end;
function IsEncrypted(mp: TElMessagePart) : boolean;
var
h : TElMessageHeaderField;
wsParam: TWideString;
begin
Result := false;
if mp = nil then
Exit;
if (mp.IsMultipart) and (mp.PartsCount = 2) then
begin
h := mp.Header.GetField('Content-Type' {$ifndef HAS_DEF_PARAMS}, 0{$endif});
if h = nil then
Exit;
if not WideSameText(h.Value, 'multipart/encrypted') then
Exit;
wsParam := h.GetParamValue('protocol');
if WideSameText(wsParam, 'application/pgp-encrypted') then
Result := True;
h := mp.GetPart(0).Header.GetField('Content-Type' {$ifndef HAS_DEF_PARAMS}, 0{$endif});