summary refs log tree commit diff
path: root/crypto/src/cms/CMSSignedHelper.cs
blob: 17e52a3dec813a4bb841d951eefb4ec120276cfd (plain) (blame)
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
using System;
using System.Collections.Generic;

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.Eac;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.Rosstandart;
using Org.BouncyCastle.Asn1.TeleTrust;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Operators.Utilities;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Collections;
using Org.BouncyCastle.X509;

namespace Org.BouncyCastle.Cms
{
    internal static class CmsSignedHelper
    {
        private static readonly Dictionary<DerObjectIdentifier, string> m_encryptionAlgs =
            new Dictionary<DerObjectIdentifier, string>();
        private static readonly Dictionary<DerObjectIdentifier, string> m_digestAlgs =
            new Dictionary<DerObjectIdentifier, string>();
        private static readonly Dictionary<string, string[]> m_digestAliases = new Dictionary<string, string[]>();

        private static readonly HashSet<DerObjectIdentifier> m_noParams = new HashSet<DerObjectIdentifier>();
        private static readonly Dictionary<string, DerObjectIdentifier> m_ecAlgorithms =
            new Dictionary<string, DerObjectIdentifier>();

        private static void AddEntries(DerObjectIdentifier oid, string digest, string encryption)
        {
            m_digestAlgs.Add(oid, digest);
            m_encryptionAlgs.Add(oid, encryption);
        }

        static CmsSignedHelper()
        {
            AddEntries(NistObjectIdentifiers.DsaWithSha224, "SHA224", "DSA");
            AddEntries(NistObjectIdentifiers.DsaWithSha256, "SHA256", "DSA");
            AddEntries(NistObjectIdentifiers.DsaWithSha384, "SHA384", "DSA");
            AddEntries(NistObjectIdentifiers.DsaWithSha512, "SHA512", "DSA");
            AddEntries(OiwObjectIdentifiers.DsaWithSha1, "SHA1", "DSA");
            AddEntries(OiwObjectIdentifiers.MD4WithRsa, "MD4", "RSA");
            AddEntries(OiwObjectIdentifiers.MD4WithRsaEncryption, "MD4", "RSA");
            AddEntries(OiwObjectIdentifiers.MD5WithRsa, "MD5", "RSA");
            AddEntries(OiwObjectIdentifiers.Sha1WithRsa, "SHA1", "RSA");
            AddEntries(PkcsObjectIdentifiers.MD2WithRsaEncryption, "MD2", "RSA");
            AddEntries(PkcsObjectIdentifiers.MD4WithRsaEncryption, "MD4", "RSA");
            AddEntries(PkcsObjectIdentifiers.MD5WithRsaEncryption, "MD5", "RSA");
            AddEntries(PkcsObjectIdentifiers.Sha1WithRsaEncryption, "SHA1", "RSA");
            AddEntries(PkcsObjectIdentifiers.Sha224WithRsaEncryption, "SHA224", "RSA");
            AddEntries(PkcsObjectIdentifiers.Sha256WithRsaEncryption, "SHA256", "RSA");
            AddEntries(PkcsObjectIdentifiers.Sha384WithRsaEncryption, "SHA384", "RSA");
            AddEntries(PkcsObjectIdentifiers.Sha512WithRsaEncryption, "SHA512", "RSA");
            AddEntries(PkcsObjectIdentifiers.Sha512_224WithRSAEncryption, "SHA512(224)", "RSA");
            AddEntries(PkcsObjectIdentifiers.Sha512_256WithRSAEncryption, "SHA512(256)", "RSA");
            AddEntries(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_224, "SHA3-224", "RSA");
            AddEntries(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_256, "SHA3-256", "RSA");
            AddEntries(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_384, "SHA3-384", "RSA");
            AddEntries(NistObjectIdentifiers.IdRsassaPkcs1V15WithSha3_512, "SHA3-512", "RSA");
            AddEntries(X9ObjectIdentifiers.ECDsaWithSha1, "SHA1", "ECDSA");
            AddEntries(X9ObjectIdentifiers.ECDsaWithSha224, "SHA224", "ECDSA");
            AddEntries(X9ObjectIdentifiers.ECDsaWithSha256, "SHA256", "ECDSA");
            AddEntries(X9ObjectIdentifiers.ECDsaWithSha384, "SHA384", "ECDSA");
            AddEntries(X9ObjectIdentifiers.ECDsaWithSha512, "SHA512", "ECDSA");
            AddEntries(X9ObjectIdentifiers.IdDsaWithSha1, "SHA1", "DSA");
            AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_1, "SHA1", "ECDSA");
            AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_224, "SHA224", "ECDSA");
            AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_256, "SHA256", "ECDSA");
            AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_384, "SHA384", "ECDSA");
            AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_512, "SHA512", "ECDSA");
            AddEntries(EacObjectIdentifiers.id_TA_RSA_v1_5_SHA_1, "SHA1", "RSA");
            AddEntries(EacObjectIdentifiers.id_TA_RSA_v1_5_SHA_256, "SHA256", "RSA");
            AddEntries(EacObjectIdentifiers.id_TA_RSA_PSS_SHA_1, "SHA1", "RSAandMGF1");
            AddEntries(EacObjectIdentifiers.id_TA_RSA_PSS_SHA_256, "SHA256", "RSAandMGF1");
            AddEntries(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94, "GOST3411", "GOST3410");
            AddEntries(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001, "GOST3411", "ECGOST3410");
            AddEntries(RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_256, "GOST3411-2012-256", "ECGOST3410");
            AddEntries(RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_512, "GOST3411-2012-512", "ECGOST3410");

            m_encryptionAlgs.Add(X9ObjectIdentifiers.IdDsa, "DSA");
            m_encryptionAlgs.Add(PkcsObjectIdentifiers.RsaEncryption, "RSA");
            m_encryptionAlgs.Add(TeleTrusTObjectIdentifiers.TeleTrusTRsaSignatureAlgorithm, "RSA");
            m_encryptionAlgs.Add(X509ObjectIdentifiers.IdEARsa, "RSA");
            m_encryptionAlgs.Add(PkcsObjectIdentifiers.IdRsassaPss, "RSAandMGF1");
            m_encryptionAlgs.Add(CryptoProObjectIdentifiers.GostR3410x94, "GOST3410");
            m_encryptionAlgs.Add(CryptoProObjectIdentifiers.GostR3410x2001, "ECGOST3410");
            m_encryptionAlgs.Add(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256, "ECGOST3410");
            m_encryptionAlgs.Add(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512, "ECGOST3410");
            m_encryptionAlgs.Add(new DerObjectIdentifier("1.3.6.1.4.1.5849.1.6.2"), "ECGOST3410");
            m_encryptionAlgs.Add(new DerObjectIdentifier("1.3.6.1.4.1.5849.1.1.5"), "GOST3410");
            m_encryptionAlgs.Add(X9ObjectIdentifiers.IdECPublicKey, "ECDSA");

            m_digestAlgs.Add(PkcsObjectIdentifiers.MD2, "MD2");
            m_digestAlgs.Add(PkcsObjectIdentifiers.MD4, "MD4");
            m_digestAlgs.Add(PkcsObjectIdentifiers.MD5, "MD5");
            m_digestAlgs.Add(OiwObjectIdentifiers.IdSha1, "SHA1");
            m_digestAlgs.Add(NistObjectIdentifiers.IdSha224, "SHA224");
            m_digestAlgs.Add(NistObjectIdentifiers.IdSha256, "SHA256");
            m_digestAlgs.Add(NistObjectIdentifiers.IdSha384, "SHA384");
            m_digestAlgs.Add(NistObjectIdentifiers.IdSha512, "SHA512");
            m_digestAlgs.Add(NistObjectIdentifiers.IdSha512_224, "SHA512(224)");
            m_digestAlgs.Add(NistObjectIdentifiers.IdSha512_256, "SHA512(256)");
            m_digestAlgs.Add(NistObjectIdentifiers.IdSha3_224, "SHA3-224");
            m_digestAlgs.Add(NistObjectIdentifiers.IdSha3_256, "SHA3-256");
            m_digestAlgs.Add(NistObjectIdentifiers.IdSha3_384, "SHA3-384");
            m_digestAlgs.Add(NistObjectIdentifiers.IdSha3_512, "SHA3-512");
            m_digestAlgs.Add(TeleTrusTObjectIdentifiers.RipeMD128, "RIPEMD128");
            m_digestAlgs.Add(TeleTrusTObjectIdentifiers.RipeMD160, "RIPEMD160");
            m_digestAlgs.Add(TeleTrusTObjectIdentifiers.RipeMD256, "RIPEMD256");
            m_digestAlgs.Add(CryptoProObjectIdentifiers.GostR3411, "GOST3411");
            m_digestAlgs.Add(new DerObjectIdentifier("1.3.6.1.4.1.5849.1.2.1"), "GOST3411");
            m_digestAlgs.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256, "GOST3411-2012-256");
            m_digestAlgs.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, "GOST3411-2012-512");

            m_digestAliases.Add("SHA1", new string[]{ "SHA-1" });
            m_digestAliases.Add("SHA224", new string[]{ "SHA-224" });
            m_digestAliases.Add("SHA256", new string[]{ "SHA-256" });
            m_digestAliases.Add("SHA384", new string[]{ "SHA-384" });
            m_digestAliases.Add("SHA512", new string[]{ "SHA-512" });

            m_noParams.Add(X9ObjectIdentifiers.IdDsaWithSha1);
            m_noParams.Add(X9ObjectIdentifiers.ECDsaWithSha1);
            m_noParams.Add(X9ObjectIdentifiers.ECDsaWithSha224);
            m_noParams.Add(X9ObjectIdentifiers.ECDsaWithSha256);
            m_noParams.Add(X9ObjectIdentifiers.ECDsaWithSha384);
            m_noParams.Add(X9ObjectIdentifiers.ECDsaWithSha512);

            m_ecAlgorithms.Add(CmsSignedGenerator.DigestSha1, X9ObjectIdentifiers.ECDsaWithSha1);
            m_ecAlgorithms.Add(CmsSignedGenerator.DigestSha224, X9ObjectIdentifiers.ECDsaWithSha224);
            m_ecAlgorithms.Add(CmsSignedGenerator.DigestSha256, X9ObjectIdentifiers.ECDsaWithSha256);
            m_ecAlgorithms.Add(CmsSignedGenerator.DigestSha384, X9ObjectIdentifiers.ECDsaWithSha384);
            m_ecAlgorithms.Add(CmsSignedGenerator.DigestSha512, X9ObjectIdentifiers.ECDsaWithSha512);
        }

        /**
        * Return the digest algorithm using one of the standard JCA string
        * representations rather than the algorithm identifier (if possible).
        */
        internal static string GetDigestAlgName(DerObjectIdentifier digestOid)
        {
            if (m_digestAlgs.TryGetValue(digestOid, out var name))
                return name;

            return digestOid.Id;
        }

        internal static AlgorithmIdentifier GetEncAlgorithmIdentifier(DerObjectIdentifier encOid,
            Asn1Encodable sigX509Parameters)
        {
            if (m_noParams.Contains(encOid))
                return new AlgorithmIdentifier(encOid);

            return new AlgorithmIdentifier(encOid, sigX509Parameters);
        }

        internal static string[] GetDigestAliases(string algName)
        {
            return m_digestAliases.TryGetValue(algName, out var aliases) ? (string[])aliases.Clone() : new string[0];
        }

        /**
        * Return the digest encryption algorithm using one of the standard
        * JCA string representations rather than the algorithm identifier (if
        * possible).
        */
        internal static string GetEncryptionAlgName(DerObjectIdentifier encryptionOid)
        {
            if (m_encryptionAlgs.TryGetValue(encryptionOid, out var name))
                return name;

            return encryptionOid.Id;
        }

        internal static IDigest GetDigestInstance(string algorithm)
        {
            try
            {
                return DigestUtilities.GetDigest(algorithm);
            }
            catch (SecurityUtilityException)
            {
                // This is probably superfluous on C#, since no provider infrastructure,
                // assuming DigestUtilities already knows all the aliases
                foreach (string alias in GetDigestAliases(algorithm))
                {
                    try { return DigestUtilities.GetDigest(alias); }
                    catch (SecurityUtilityException) {}
                }
                throw;
            }
        }

        internal static ISigner GetSignatureInstance(string algorithm)
        {
            return SignerUtilities.GetSigner(algorithm);
        }

        internal static AlgorithmIdentifier FixDigestAlgID(AlgorithmIdentifier algID,
            IDigestAlgorithmFinder digestAlgorithmFinder)
        {
            var parameters = algID.Parameters;
            if (parameters == null || DerNull.Instance.Equals(parameters))
                return digestAlgorithmFinder.Find(algID.Algorithm);

            return algID;
        }

        internal static DerObjectIdentifier GetEncOid(AsymmetricKeyParameter key, string digestOID)
        {
            DerObjectIdentifier encOid = null;

            if (key is RsaKeyParameters rsaKeyParameters)
            {
                if (!rsaKeyParameters.IsPrivate)
                    throw new ArgumentException("Expected RSA private key");

                encOid = PkcsObjectIdentifiers.RsaEncryption;
            }
            else if (key is DsaPrivateKeyParameters)
            {
                if (digestOID.Equals(CmsSignedGenerator.DigestSha1))
                {
                    encOid = X9ObjectIdentifiers.IdDsaWithSha1;
                }
                else if (digestOID.Equals(CmsSignedGenerator.DigestSha224))
                {
                    encOid = NistObjectIdentifiers.DsaWithSha224;
                }
                else if (digestOID.Equals(CmsSignedGenerator.DigestSha256))
                {
                    encOid = NistObjectIdentifiers.DsaWithSha256;
                }
                else if (digestOID.Equals(CmsSignedGenerator.DigestSha384))
                {
                    encOid = NistObjectIdentifiers.DsaWithSha384;
                }
                else if (digestOID.Equals(CmsSignedGenerator.DigestSha512))
                {
                    encOid = NistObjectIdentifiers.DsaWithSha512;
                }
                else
                {
                    throw new ArgumentException("can't mix DSA with anything but SHA1/SHA2");
                }
            }
            else if (key is ECPrivateKeyParameters ecPrivKey)
            {
                string algName = ecPrivKey.AlgorithmName;

                if (algName == "ECGOST3410")
                {
                    encOid = CryptoProObjectIdentifiers.GostR3410x2001;
                }
                else if (ecPrivKey.Parameters is ECGost3410Parameters ecGost3410Parameters)
                {
                    var digestParamSet = ecGost3410Parameters.DigestParamSet;
                    if (digestParamSet.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256))
                    {
                        encOid = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256;
                    }
                    else if (digestParamSet.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512))
                    {
                        encOid = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512;
                    }
                    else
                    {
                        throw new ArgumentException("can't determine GOST3410 algorithm");
                    }
                }
                else
                {
                    // TODO Should we insist on algName being one of "EC" or "ECDSA", as Java does?
                    if (!m_ecAlgorithms.TryGetValue(digestOID, out encOid))
                        throw new ArgumentException("can't mix ECDSA with anything but SHA family digests");
                }
            }
            else if (key is Gost3410PrivateKeyParameters)
            {
                encOid = CryptoProObjectIdentifiers.GostR3410x94;
            }
            else
            {
                throw new ArgumentException("Unknown algorithm in CmsSignedGenerator.GetEncOid");
            }

            return encOid;
        }

        internal static IStore<X509V2AttributeCertificate> GetAttributeCertificates(Asn1Set attrCertSet)
        {
            var contents = new List<X509V2AttributeCertificate>();
            if (attrCertSet != null)
            {
                foreach (Asn1Encodable ae in attrCertSet)
                {
                    if (ae.ToAsn1Object() is Asn1TaggedObject taggedObject && taggedObject.HasContextTag(2))
                    {
                        var attributeCertificate = AttributeCertificate.GetInstance(taggedObject, false);

                        contents.Add(new X509V2AttributeCertificate(attributeCertificate));
                    }
                }
            }
            return CollectionUtilities.CreateStore(contents);
        }

        internal static IStore<X509Certificate> GetCertificates(Asn1Set certSet)
        {
            var contents = new List<X509Certificate>();
            if (certSet != null)
            {
                foreach (Asn1Encodable ae in certSet)
                {
                    if (ae is X509CertificateStructure c)
                    {
                        contents.Add(new X509Certificate(c));
                    }
                    else if (ae.ToAsn1Object() is Asn1Sequence s)
                    {
                        contents.Add(new X509Certificate(X509CertificateStructure.GetInstance(s)));
                    }
                }
            }
            return CollectionUtilities.CreateStore(contents);
        }

        internal static IStore<X509Crl> GetCrls(Asn1Set crlSet)
        {
            var contents = new List<X509Crl>();
            if (crlSet != null)
            {
                foreach (Asn1Encodable ae in crlSet)
                {
                    if (ae is CertificateList c)
                    {
                        contents.Add(new X509Crl(c));
                    }
                    else if (ae.ToAsn1Object() is Asn1Sequence s)
                    {
                        contents.Add(new X509Crl(CertificateList.GetInstance(s)));
                    }
                }
            }
            return CollectionUtilities.CreateStore(contents);
        }

        internal static IStore<Asn1Encodable> GetOtherRevInfos(Asn1Set crlSet, DerObjectIdentifier infoFormat)
        {
            var contents = new List<Asn1Encodable>();
            if (crlSet != null && infoFormat != null)
            {
                foreach (Asn1Encodable ae in crlSet)
                {
                    if (ae.ToAsn1Object() is Asn1TaggedObject taggedObject && taggedObject.HasContextTag(1))
                    {
                        var otherRevocationInfoFormat = OtherRevocationInfoFormat.GetInstance(taggedObject, false);

                        if (infoFormat.Equals(otherRevocationInfoFormat.InfoFormat))
                        {
                            contents.Add(otherRevocationInfoFormat.Info);
                        }
                    }
                }
            }
            return CollectionUtilities.CreateStore(contents);
        }
    }
}