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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
|
//
// PKCS7.cs: PKCS #7 - Cryptographic Message Syntax Standard
// http://www.rsasecurity.com/rsalabs/pkcs/pkcs-7/index.html
//
// Authors:
// Sebastien Pouliot <sebastien@ximian.com>
// Daniel Granath <dgranath#gmail.com>
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Security.Cryptography;
namespace Emby.Common.Implementations.Security
{
public sealed class PKCS7 {
public class Oid {
// pkcs 1
public const string rsaEncryption = "1.2.840.113549.1.1.1";
// pkcs 7
public const string data = "1.2.840.113549.1.7.1";
public const string signedData = "1.2.840.113549.1.7.2";
public const string envelopedData = "1.2.840.113549.1.7.3";
public const string signedAndEnvelopedData = "1.2.840.113549.1.7.4";
public const string digestedData = "1.2.840.113549.1.7.5";
public const string encryptedData = "1.2.840.113549.1.7.6";
// pkcs 9
public const string contentType = "1.2.840.113549.1.9.3";
public const string messageDigest = "1.2.840.113549.1.9.4";
public const string signingTime = "1.2.840.113549.1.9.5";
public const string countersignature = "1.2.840.113549.1.9.6";
public Oid ()
{
}
}
private PKCS7 ()
{
}
static public ASN1 Attribute (string oid, ASN1 value)
{
ASN1 attr = new ASN1 (0x30);
attr.Add (ASN1Convert.FromOid (oid));
ASN1 aset = attr.Add (new ASN1 (0x31));
aset.Add (value);
return attr;
}
static public ASN1 AlgorithmIdentifier (string oid)
{
ASN1 ai = new ASN1 (0x30);
ai.Add (ASN1Convert.FromOid (oid));
ai.Add (new ASN1 (0x05)); // NULL
return ai;
}
static public ASN1 AlgorithmIdentifier (string oid, ASN1 parameters)
{
ASN1 ai = new ASN1 (0x30);
ai.Add (ASN1Convert.FromOid (oid));
ai.Add (parameters);
return ai;
}
/*
* IssuerAndSerialNumber ::= SEQUENCE {
* issuer Name,
* serialNumber CertificateSerialNumber
* }
*/
static public ASN1 IssuerAndSerialNumber (X509Certificate x509)
{
ASN1 issuer = null;
ASN1 serial = null;
ASN1 cert = new ASN1 (x509.RawData);
int tbs = 0;
bool flag = false;
while (tbs < cert[0].Count) {
ASN1 e = cert[0][tbs++];
if (e.Tag == 0x02)
serial = e;
else if (e.Tag == 0x30) {
if (flag) {
issuer = e;
break;
}
flag = true;
}
}
ASN1 iasn = new ASN1 (0x30);
iasn.Add (issuer);
iasn.Add (serial);
return iasn;
}
/*
* ContentInfo ::= SEQUENCE {
* contentType ContentType,
* content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL
* }
* ContentType ::= OBJECT IDENTIFIER
*/
public class ContentInfo {
private string contentType;
private ASN1 content;
public ContentInfo ()
{
content = new ASN1 (0xA0);
}
public ContentInfo (string oid) : this ()
{
contentType = oid;
}
public ContentInfo (byte[] data)
: this (new ASN1 (data)) {}
public ContentInfo (ASN1 asn1)
{
// SEQUENCE with 1 or 2 elements
if ((asn1.Tag != 0x30) || ((asn1.Count < 1) && (asn1.Count > 2)))
throw new ArgumentException ("Invalid ASN1");
if (asn1[0].Tag != 0x06)
throw new ArgumentException ("Invalid contentType");
contentType = ASN1Convert.ToOid (asn1[0]);
if (asn1.Count > 1) {
if (asn1[1].Tag != 0xA0)
throw new ArgumentException ("Invalid content");
content = asn1[1];
}
}
public ASN1 ASN1 {
get { return GetASN1(); }
}
public ASN1 Content {
get { return content; }
set { content = value; }
}
public string ContentType {
get { return contentType; }
set { contentType = value; }
}
internal ASN1 GetASN1 ()
{
// ContentInfo ::= SEQUENCE {
ASN1 contentInfo = new ASN1 (0x30);
// contentType ContentType, -> ContentType ::= OBJECT IDENTIFIER
contentInfo.Add (ASN1Convert.FromOid (contentType));
// content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL
if ((content != null) && (content.Count > 0))
contentInfo.Add (content);
return contentInfo;
}
public byte[] GetBytes ()
{
return GetASN1 ().GetBytes ();
}
}
/*
* EncryptedData ::= SEQUENCE {
* version INTEGER {edVer0(0)} (edVer0),
* encryptedContentInfo EncryptedContentInfo
* }
*/
public class EncryptedData {
private byte _version;
private ContentInfo _content;
private ContentInfo _encryptionAlgorithm;
private byte[] _encrypted;
public EncryptedData ()
{
_version = 0;
}
public EncryptedData (byte[] data)
: this (new ASN1 (data))
{
}
public EncryptedData (ASN1 asn1) : this ()
{
if ((asn1.Tag != 0x30) || (asn1.Count < 2))
throw new ArgumentException ("Invalid EncryptedData");
if (asn1 [0].Tag != 0x02)
throw new ArgumentException ("Invalid version");
_version = asn1 [0].Value [0];
ASN1 encryptedContentInfo = asn1 [1];
if (encryptedContentInfo.Tag != 0x30)
throw new ArgumentException ("missing EncryptedContentInfo");
ASN1 contentType = encryptedContentInfo [0];
if (contentType.Tag != 0x06)
throw new ArgumentException ("missing EncryptedContentInfo.ContentType");
_content = new ContentInfo (ASN1Convert.ToOid (contentType));
ASN1 contentEncryptionAlgorithm = encryptedContentInfo [1];
if (contentEncryptionAlgorithm.Tag != 0x30)
throw new ArgumentException ("missing EncryptedContentInfo.ContentEncryptionAlgorithmIdentifier");
_encryptionAlgorithm = new ContentInfo (ASN1Convert.ToOid (contentEncryptionAlgorithm [0]));
_encryptionAlgorithm.Content = contentEncryptionAlgorithm [1];
ASN1 encryptedContent = encryptedContentInfo [2];
if (encryptedContent.Tag != 0x80)
throw new ArgumentException ("missing EncryptedContentInfo.EncryptedContent");
_encrypted = encryptedContent.Value;
}
public ASN1 ASN1 {
get { return GetASN1(); }
}
public ContentInfo ContentInfo {
get { return _content; }
}
public ContentInfo EncryptionAlgorithm {
get { return _encryptionAlgorithm; }
}
public byte[] EncryptedContent {
get {
if (_encrypted == null)
return null;
return (byte[]) _encrypted.Clone ();
}
}
public byte Version {
get { return _version; }
set { _version = value; }
}
// methods
internal ASN1 GetASN1 ()
{
return null;
}
public byte[] GetBytes ()
{
return GetASN1 ().GetBytes ();
}
}
/*
* EnvelopedData ::= SEQUENCE {
* version Version,
* recipientInfos RecipientInfos,
* encryptedContentInfo EncryptedContentInfo
* }
*
* RecipientInfos ::= SET OF RecipientInfo
*
* EncryptedContentInfo ::= SEQUENCE {
* contentType ContentType,
* contentEncryptionAlgorithm ContentEncryptionAlgorithmIdentifier,
* encryptedContent [0] IMPLICIT EncryptedContent OPTIONAL
* }
*
* EncryptedContent ::= OCTET STRING
*
*/
public class EnvelopedData {
private byte _version;
private ContentInfo _content;
private ContentInfo _encryptionAlgorithm;
private ArrayList _recipientInfos;
private byte[] _encrypted;
public EnvelopedData ()
{
_version = 0;
_content = new ContentInfo ();
_encryptionAlgorithm = new ContentInfo ();
_recipientInfos = new ArrayList ();
}
public EnvelopedData (byte[] data)
: this (new ASN1 (data))
{
}
public EnvelopedData (ASN1 asn1) : this ()
{
if ((asn1[0].Tag != 0x30) || (asn1[0].Count < 3))
throw new ArgumentException ("Invalid EnvelopedData");
if (asn1[0][0].Tag != 0x02)
throw new ArgumentException ("Invalid version");
_version = asn1[0][0].Value[0];
// recipientInfos
ASN1 recipientInfos = asn1 [0][1];
if (recipientInfos.Tag != 0x31)
throw new ArgumentException ("missing RecipientInfos");
for (int i=0; i < recipientInfos.Count; i++) {
ASN1 recipientInfo = recipientInfos [i];
_recipientInfos.Add (new RecipientInfo (recipientInfo));
}
ASN1 encryptedContentInfo = asn1[0][2];
if (encryptedContentInfo.Tag != 0x30)
throw new ArgumentException ("missing EncryptedContentInfo");
ASN1 contentType = encryptedContentInfo [0];
if (contentType.Tag != 0x06)
throw new ArgumentException ("missing EncryptedContentInfo.ContentType");
_content = new ContentInfo (ASN1Convert.ToOid (contentType));
ASN1 contentEncryptionAlgorithm = encryptedContentInfo [1];
if (contentEncryptionAlgorithm.Tag != 0x30)
throw new ArgumentException ("missing EncryptedContentInfo.ContentEncryptionAlgorithmIdentifier");
_encryptionAlgorithm = new ContentInfo (ASN1Convert.ToOid (contentEncryptionAlgorithm [0]));
_encryptionAlgorithm.Content = contentEncryptionAlgorithm [1];
ASN1 encryptedContent = encryptedContentInfo [2];
if (encryptedContent.Tag != 0x80)
throw new ArgumentException ("missing EncryptedContentInfo.EncryptedContent");
_encrypted = encryptedContent.Value;
}
public ArrayList RecipientInfos {
get { return _recipientInfos; }
}
public ASN1 ASN1 {
get { return GetASN1(); }
}
public ContentInfo ContentInfo {
get { return _content; }
}
public ContentInfo EncryptionAlgorithm {
get { return _encryptionAlgorithm; }
}
public byte[] EncryptedContent {
get {
if (_encrypted == null)
return null;
return (byte[]) _encrypted.Clone ();
}
}
public byte Version {
get { return _version; }
set { _version = value; }
}
internal ASN1 GetASN1 ()
{
// SignedData ::= SEQUENCE {
ASN1 signedData = new ASN1 (0x30);
// version Version -> Version ::= INTEGER
/* byte[] ver = { _version };
signedData.Add (new ASN1 (0x02, ver));
// digestAlgorithms DigestAlgorithmIdentifiers -> DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmIdentifier
ASN1 digestAlgorithms = signedData.Add (new ASN1 (0x31));
if (hashAlgorithm != null) {
string hashOid = CryptoConfig.MapNameToOid (hashAlgorithm);
digestAlgorithms.Add (AlgorithmIdentifier (hashOid));
}
// contentInfo ContentInfo,
ASN1 ci = contentInfo.ASN1;
signedData.Add (ci);
if ((mda == null) && (hashAlgorithm != null)) {
// automatically add the messageDigest authenticated attribute
HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm);
byte[] idcHash = ha.ComputeHash (ci[1][0].Value);
ASN1 md = new ASN1 (0x30);
mda = Attribute (messageDigest, md.Add (new ASN1 (0x04, idcHash)));
signerInfo.AuthenticatedAttributes.Add (mda);
}
// certificates [0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL,
if (certs.Count > 0) {
ASN1 a0 = signedData.Add (new ASN1 (0xA0));
foreach (X509Certificate x in certs)
a0.Add (new ASN1 (x.RawData));
}
// crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
if (crls.Count > 0) {
ASN1 a1 = signedData.Add (new ASN1 (0xA1));
foreach (byte[] crl in crls)
a1.Add (new ASN1 (crl));
}
// signerInfos SignerInfos -> SignerInfos ::= SET OF SignerInfo
ASN1 signerInfos = signedData.Add (new ASN1 (0x31));
if (signerInfo.Key != null)
signerInfos.Add (signerInfo.ASN1);*/
return signedData;
}
public byte[] GetBytes () {
return GetASN1 ().GetBytes ();
}
}
/* RecipientInfo ::= SEQUENCE {
* version Version,
* issuerAndSerialNumber IssuerAndSerialNumber,
* keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,
* encryptedKey EncryptedKey
* }
*
* KeyEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
*
* EncryptedKey ::= OCTET STRING
*/
public class RecipientInfo {
private int _version;
private string _oid;
private byte[] _key;
private byte[] _ski;
private string _issuer;
private byte[] _serial;
public RecipientInfo () {}
public RecipientInfo (ASN1 data)
{
if (data.Tag != 0x30)
throw new ArgumentException ("Invalid RecipientInfo");
ASN1 version = data [0];
if (version.Tag != 0x02)
throw new ArgumentException ("missing Version");
_version = version.Value [0];
// issuerAndSerialNumber IssuerAndSerialNumber
ASN1 subjectIdentifierType = data [1];
if ((subjectIdentifierType.Tag == 0x80) && (_version == 3)) {
_ski = subjectIdentifierType.Value;
}
else {
_issuer = X501.ToString (subjectIdentifierType [0]);
_serial = subjectIdentifierType [1].Value;
}
ASN1 keyEncryptionAlgorithm = data [2];
_oid = ASN1Convert.ToOid (keyEncryptionAlgorithm [0]);
ASN1 encryptedKey = data [3];
_key = encryptedKey.Value;
}
public string Oid {
get { return _oid; }
}
public byte[] Key {
get {
if (_key == null)
return null;
return (byte[]) _key.Clone ();
}
}
public byte[] SubjectKeyIdentifier {
get {
if (_ski == null)
return null;
return (byte[]) _ski.Clone ();
}
}
public string Issuer {
get { return _issuer; }
}
public byte[] Serial {
get {
if (_serial == null)
return null;
return (byte[]) _serial.Clone ();
}
}
public int Version {
get { return _version; }
}
}
/*
* SignedData ::= SEQUENCE {
* version Version,
* digestAlgorithms DigestAlgorithmIdentifiers,
* contentInfo ContentInfo,
* certificates [0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL,
* crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
* signerInfos SignerInfos
* }
*/
public class SignedData {
private byte version;
private string hashAlgorithm;
private ContentInfo contentInfo;
private X509CertificateCollection certs;
private ArrayList crls;
private SignerInfo signerInfo;
private bool mda;
private bool signed;
public SignedData ()
{
version = 1;
contentInfo = new ContentInfo ();
certs = new X509CertificateCollection ();
crls = new ArrayList ();
signerInfo = new SignerInfo ();
mda = true;
signed = false;
}
public SignedData (byte[] data)
: this (new ASN1 (data))
{
}
public SignedData (ASN1 asn1)
{
if ((asn1[0].Tag != 0x30) || (asn1[0].Count < 4))
throw new ArgumentException ("Invalid SignedData");
if (asn1[0][0].Tag != 0x02)
throw new ArgumentException ("Invalid version");
version = asn1[0][0].Value[0];
contentInfo = new ContentInfo (asn1[0][2]);
int n = 3;
certs = new X509CertificateCollection ();
if (asn1[0][n].Tag == 0xA0) {
for (int i=0; i < asn1[0][n].Count; i++)
certs.Add (new X509Certificate (asn1[0][n][i].GetBytes ()));
n++;
}
crls = new ArrayList ();
if (asn1[0][n].Tag == 0xA1) {
for (int i=0; i < asn1[0][n].Count; i++)
crls.Add (asn1[0][n][i].GetBytes ());
n++;
}
if (asn1[0][n].Count > 0)
signerInfo = new SignerInfo (asn1[0][n]);
else
signerInfo = new SignerInfo ();
// Exchange hash algorithm Oid from SignerInfo
if (signerInfo.HashName != null) {
HashName = OidToName(signerInfo.HashName);
}
// Check if SignerInfo has authenticated attributes
mda = (signerInfo.AuthenticatedAttributes.Count > 0);
}
public ASN1 ASN1 {
get { return GetASN1(); }
}
public X509CertificateCollection Certificates {
get { return certs; }
}
public ContentInfo ContentInfo {
get { return contentInfo; }
}
public ArrayList Crls {
get { return crls; }
}
public string HashName {
get { return hashAlgorithm; }
// todo add validation
set {
hashAlgorithm = value;
signerInfo.HashName = value;
}
}
public SignerInfo SignerInfo {
get { return signerInfo; }
}
public byte Version {
get { return version; }
set { version = value; }
}
public bool UseAuthenticatedAttributes {
get { return mda; }
set { mda = value; }
}
public bool VerifySignature (AsymmetricAlgorithm aa)
{
if (aa == null) {
return false;
}
RSAPKCS1SignatureDeformatter r = new RSAPKCS1SignatureDeformatter (aa);
r.SetHashAlgorithm (hashAlgorithm);
HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm);
byte[] signature = signerInfo.Signature;
byte[] hash = null;
if (mda) {
ASN1 asn = new ASN1 (0x31);
foreach (ASN1 attr in signerInfo.AuthenticatedAttributes)
asn.Add (attr);
hash = ha.ComputeHash (asn.GetBytes ());
} else {
hash = ha.ComputeHash (contentInfo.Content[0].Value);
}
if (hash != null && signature != null) {
return r.VerifySignature (hash, signature);
}
return false;
}
internal string OidToName (string oid)
{
switch (oid) {
case "1.3.14.3.2.26" :
return "SHA1";
case "1.2.840.113549.2.2" :
return "MD2";
case "1.2.840.113549.2.5" :
return "MD5";
case "2.16.840.1.101.3.4.1" :
return "SHA256";
case "2.16.840.1.101.3.4.2" :
return "SHA384";
case "2.16.840.1.101.3.4.3" :
return "SHA512";
default :
break;
}
// Unknown Oid
return oid;
}
internal ASN1 GetASN1 ()
{
// SignedData ::= SEQUENCE {
ASN1 signedData = new ASN1 (0x30);
// version Version -> Version ::= INTEGER
byte[] ver = { version };
signedData.Add (new ASN1 (0x02, ver));
// digestAlgorithms DigestAlgorithmIdentifiers -> DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmIdentifier
ASN1 digestAlgorithms = signedData.Add (new ASN1 (0x31));
if (hashAlgorithm != null) {
string hashOid = CryptoConfig.MapNameToOID (hashAlgorithm);
digestAlgorithms.Add (AlgorithmIdentifier (hashOid));
}
// contentInfo ContentInfo,
ASN1 ci = contentInfo.ASN1;
signedData.Add (ci);
if (!signed && (hashAlgorithm != null)) {
if (mda) {
// Use authenticated attributes for signature
// Automatically add the contentType authenticated attribute
ASN1 ctattr = Attribute (Oid.contentType, ci[0]);
signerInfo.AuthenticatedAttributes.Add (ctattr);
// Automatically add the messageDigest authenticated attribute
HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm);
byte[] idcHash = ha.ComputeHash (ci[1][0].Value);
ASN1 md = new ASN1 (0x30);
ASN1 mdattr = Attribute (Oid.messageDigest, md.Add (new ASN1 (0x04, idcHash)));
signerInfo.AuthenticatedAttributes.Add (mdattr);
} else {
// Don't use authenticated attributes for signature -- signature is content
RSAPKCS1SignatureFormatter r = new RSAPKCS1SignatureFormatter (signerInfo.Key);
r.SetHashAlgorithm (hashAlgorithm);
HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm);
byte[] sig = ha.ComputeHash (ci[1][0].Value);
signerInfo.Signature = r.CreateSignature (sig);
}
signed = true;
}
// certificates [0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL,
if (certs.Count > 0) {
ASN1 a0 = signedData.Add (new ASN1 (0xA0));
foreach (X509Certificate x in certs)
a0.Add (new ASN1 (x.RawData));
}
// crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
if (crls.Count > 0) {
ASN1 a1 = signedData.Add (new ASN1 (0xA1));
foreach (byte[] crl in crls)
a1.Add (new ASN1 (crl));
}
// signerInfos SignerInfos -> SignerInfos ::= SET OF SignerInfo
ASN1 signerInfos = signedData.Add (new ASN1 (0x31));
if (signerInfo.Key != null)
signerInfos.Add (signerInfo.ASN1);
return signedData;
}
public byte[] GetBytes ()
{
return GetASN1 ().GetBytes ();
}
}
/*
* SignerInfo ::= SEQUENCE {
* version Version,
* issuerAndSerialNumber IssuerAndSerialNumber,
* digestAlgorithm DigestAlgorithmIdentifier,
* authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL,
* digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier,
* encryptedDigest EncryptedDigest,
* unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
* }
*
* For version == 3 issuerAndSerialNumber may be replaced by ...
*/
public class SignerInfo {
private byte version;
private X509Certificate x509;
private string hashAlgorithm;
private AsymmetricAlgorithm key;
private ArrayList authenticatedAttributes;
private ArrayList unauthenticatedAttributes;
private byte[] signature;
private string issuer;
private byte[] serial;
private byte[] ski;
public SignerInfo ()
{
version = 1;
authenticatedAttributes = new ArrayList ();
unauthenticatedAttributes = new ArrayList ();
}
public SignerInfo (byte[] data)
: this (new ASN1 (data)) {}
// TODO: INCOMPLETE
public SignerInfo (ASN1 asn1) : this ()
{
if ((asn1[0].Tag != 0x30) || (asn1[0].Count < 5))
throw new ArgumentException ("Invalid SignedData");
// version Version
if (asn1[0][0].Tag != 0x02)
throw new ArgumentException ("Invalid version");
version = asn1[0][0].Value[0];
// issuerAndSerialNumber IssuerAndSerialNumber
ASN1 subjectIdentifierType = asn1 [0][1];
if ((subjectIdentifierType.Tag == 0x80) && (version == 3)) {
ski = subjectIdentifierType.Value;
}
else {
issuer = X501.ToString (subjectIdentifierType [0]);
serial = subjectIdentifierType [1].Value;
}
// digestAlgorithm DigestAlgorithmIdentifier
ASN1 digestAlgorithm = asn1 [0][2];
hashAlgorithm = ASN1Convert.ToOid (digestAlgorithm [0]);
// authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL
int n = 3;
ASN1 authAttributes = asn1 [0][n];
if (authAttributes.Tag == 0xA0) {
n++;
for (int i=0; i < authAttributes.Count; i++)
authenticatedAttributes.Add (authAttributes [i]);
}
// digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier
n++;
// ASN1 digestEncryptionAlgorithm = asn1 [0][n++];
// string digestEncryptionAlgorithmOid = ASN1Convert.ToOid (digestEncryptionAlgorithm [0]);
// encryptedDigest EncryptedDigest
ASN1 encryptedDigest = asn1 [0][n++];
if (encryptedDigest.Tag == 0x04)
signature = encryptedDigest.Value;
// unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
ASN1 unauthAttributes = asn1 [0][n];
if ((unauthAttributes != null) && (unauthAttributes.Tag == 0xA1)) {
for (int i=0; i < unauthAttributes.Count; i++)
unauthenticatedAttributes.Add (unauthAttributes [i]);
}
}
public string IssuerName {
get { return issuer; }
}
public byte[] SerialNumber {
get {
if (serial == null)
return null;
return (byte[]) serial.Clone ();
}
}
public byte[] SubjectKeyIdentifier {
get {
if (ski == null)
return null;
return (byte[]) ski.Clone ();
}
}
public ASN1 ASN1 {
get { return GetASN1(); }
}
public ArrayList AuthenticatedAttributes {
get { return authenticatedAttributes; }
}
public X509Certificate Certificate {
get { return x509; }
set { x509 = value; }
}
public string HashName {
get { return hashAlgorithm; }
set { hashAlgorithm = value; }
}
public AsymmetricAlgorithm Key {
get { return key; }
set { key = value; }
}
public byte[] Signature {
get {
if (signature == null)
return null;
return (byte[]) signature.Clone ();
}
set {
if (value != null) {
signature = (byte[]) value.Clone ();
}
}
}
public ArrayList UnauthenticatedAttributes {
get { return unauthenticatedAttributes; }
}
public byte Version {
get { return version; }
set { version = value; }
}
internal ASN1 GetASN1 ()
{
if ((key == null) || (hashAlgorithm == null))
return null;
byte[] ver = { version };
ASN1 signerInfo = new ASN1 (0x30);
// version Version -> Version ::= INTEGER
signerInfo.Add (new ASN1 (0x02, ver));
// issuerAndSerialNumber IssuerAndSerialNumber,
signerInfo.Add (PKCS7.IssuerAndSerialNumber (x509));
// digestAlgorithm DigestAlgorithmIdentifier,
string hashOid = CryptoConfig.MapNameToOID (hashAlgorithm);
signerInfo.Add (AlgorithmIdentifier (hashOid));
// authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL,
ASN1 aa = null;
if (authenticatedAttributes.Count > 0) {
aa = signerInfo.Add (new ASN1 (0xA0));
authenticatedAttributes.Sort(new SortedSet ());
foreach (ASN1 attr in authenticatedAttributes)
aa.Add (attr);
}
// digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier,
if (key is RSA) {
signerInfo.Add (AlgorithmIdentifier (PKCS7.Oid.rsaEncryption));
if (aa != null) {
// Calculate the signature here; otherwise it must be set from SignedData
RSAPKCS1SignatureFormatter r = new RSAPKCS1SignatureFormatter (key);
r.SetHashAlgorithm (hashAlgorithm);
byte[] tbs = aa.GetBytes ();
tbs [0] = 0x31; // not 0xA0 for signature
HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm);
byte[] tbsHash = ha.ComputeHash (tbs);
signature = r.CreateSignature (tbsHash);
}
}
else if (key is DSA) {
throw new NotImplementedException ("not yet");
}
else
throw new CryptographicException ("Unknown assymetric algorithm");
// encryptedDigest EncryptedDigest,
signerInfo.Add (new ASN1 (0x04, signature));
// unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
if (unauthenticatedAttributes.Count > 0) {
ASN1 ua = signerInfo.Add (new ASN1 (0xA1));
unauthenticatedAttributes.Sort(new SortedSet ());
foreach (ASN1 attr in unauthenticatedAttributes)
ua.Add (attr);
}
return signerInfo;
}
public byte[] GetBytes ()
{
return GetASN1 ().GetBytes ();
}
}
internal class SortedSet : IComparer {
public int Compare (object x, object y)
{
if (x == null)
return (y == null) ? 0 : -1;
else if (y == null)
return 1;
ASN1 xx = x as ASN1;
ASN1 yy = y as ASN1;
if ((xx == null) || (yy == null)) {
throw new ArgumentException (("Invalid objects."));
}
byte[] xb = xx.GetBytes ();
byte[] yb = yy.GetBytes ();
for (int i = 0; i < xb.Length; i++) {
if (i == yb.Length)
break;
if (xb[i] == yb[i])
continue;
return (xb[i] < yb[i]) ? -1 : 1;
}
// The arrays are equal up to the shortest of them.
if (xb.Length > yb.Length)
return 1;
else if (xb.Length < yb.Length)
return -1;
return 0;
}
}
}
}
|