mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 602
Expand file tree
/
Copy pathCMSSignedGenerator.cs
More file actions
244 lines (202 loc) · 11.1 KB
/
Copy pathCMSSignedGenerator.cs
File metadata and controls
244 lines (202 loc) · 11.1 KB
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
using System;
using System.Collections.Generic;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.GM;
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.Operators.Utilities;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Collections;
using Org.BouncyCastle.X509;
namespace Org.BouncyCastle.Cms
{
[Obsolete("Use 'Org.BouncyCastle.Operators.Utilities.DefaultSignatureAlgorithmFinder' instead")]
public class DefaultSignatureAlgorithmIdentifierFinder
: Operators.Utilities.ISignatureAlgorithmFinder
{
// TODO[api] Make virtual
public AlgorithmIdentifier Find(string sigAlgName) =>
Operators.Utilities.DefaultSignatureAlgorithmFinder.Instance.Find(sigAlgName);
}
[Obsolete("Use 'Org.BouncyCastle.Operators.Utilities.DefaultDigestAlgorithmFinder' instead")]
public class DefaultDigestAlgorithmIdentifierFinder
: Operators.Utilities.IDigestAlgorithmFinder
{
// TODO[api] Make virtual
public AlgorithmIdentifier Find(AlgorithmIdentifier sigAlgId) =>
Operators.Utilities.DefaultDigestAlgorithmFinder.Instance.Find(sigAlgId);
public virtual AlgorithmIdentifier Find(DerObjectIdentifier digAlgOid) =>
Operators.Utilities.DefaultDigestAlgorithmFinder.Instance.Find(digAlgOid);
// TODO[api] Make virtual
public AlgorithmIdentifier Find(string digAlgName) =>
Operators.Utilities.DefaultDigestAlgorithmFinder.Instance.Find(digAlgName);
}
public abstract class CmsSignedGenerator
{
/**
* Default type for the signed data.
*/
public static readonly string Data = CmsObjectIdentifiers.Data.Id;
public static readonly string DigestSha1 = OiwObjectIdentifiers.IdSha1.Id;
public static readonly string DigestSha224 = NistObjectIdentifiers.IdSha224.Id;
public static readonly string DigestSha256 = NistObjectIdentifiers.IdSha256.Id;
public static readonly string DigestSha384 = NistObjectIdentifiers.IdSha384.Id;
public static readonly string DigestSha512 = NistObjectIdentifiers.IdSha512.Id;
public static readonly string DigestSha512_224 = NistObjectIdentifiers.IdSha512_224.Id;
public static readonly string DigestSha512_256 = NistObjectIdentifiers.IdSha512_256.Id;
public static readonly string DigestSha3_224 = NistObjectIdentifiers.IdSha3_224.Id;
public static readonly string DigestSha3_256 = NistObjectIdentifiers.IdSha3_256.Id;
public static readonly string DigestSha3_384 = NistObjectIdentifiers.IdSha3_384.Id;
public static readonly string DigestSha3_512 = NistObjectIdentifiers.IdSha3_512.Id;
public static readonly string DigestShake128 = NistObjectIdentifiers.IdShake128.Id;
public static readonly string DigestShake256 = NistObjectIdentifiers.IdShake256.Id;
public static readonly string DigestMD5 = PkcsObjectIdentifiers.MD5.Id;
public static readonly string DigestGost3411 = CryptoProObjectIdentifiers.GostR3411.Id;
public static readonly string DigestRipeMD128 = TeleTrusTObjectIdentifiers.RipeMD128.Id;
public static readonly string DigestRipeMD160 = TeleTrusTObjectIdentifiers.RipeMD160.Id;
public static readonly string DigestRipeMD256 = TeleTrusTObjectIdentifiers.RipeMD256.Id;
public static readonly string DigestSM3 = GMObjectIdentifiers.sm3.Id;
public static readonly string EncryptionRsa = PkcsObjectIdentifiers.RsaEncryption.Id;
public static readonly string EncryptionDsa = X9ObjectIdentifiers.IdDsaWithSha1.Id;
public static readonly string EncryptionECDsa = X9ObjectIdentifiers.ECDsaWithSha1.Id;
public static readonly string EncryptionRsaPss = PkcsObjectIdentifiers.IdRsassaPss.Id;
public static readonly string EncryptionGost3410 = CryptoProObjectIdentifiers.GostR3410x94.Id;
public static readonly string EncryptionECGost3410 = CryptoProObjectIdentifiers.GostR3410x2001.Id;
public static readonly string EncryptionECGost3410_2012_256 = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256.Id;
public static readonly string EncryptionECGost3410_2012_512 = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512.Id;
internal List<Asn1Encodable> _certs = new List<Asn1Encodable>();
internal List<Asn1Encodable> _crls = new List<Asn1Encodable>();
internal List<SignerInformation> _signers = new List<SignerInformation>();
internal Dictionary<DerObjectIdentifier, byte[]> m_digests = new Dictionary<DerObjectIdentifier, byte[]>();
internal IDigestAlgorithmFinder m_digestAlgorithmFinder = DefaultDigestAlgorithmFinder.Instance;
internal bool _useDerForCerts = false;
internal bool _useDerForCrls = false;
// TODO[api] Restrict to internal
protected readonly SecureRandom m_random;
// TODO[api] Restrict to internal
protected CmsSignedGenerator()
: this(CryptoServicesRegistrar.GetSecureRandom())
{
}
/// <summary>Constructor allowing specific source of randomness</summary>
/// <param name="random">Instance of <c>SecureRandom</c> to use.</param>
// TODO[api] Restrict to internal
protected CmsSignedGenerator(SecureRandom random)
{
m_random = random ?? throw new ArgumentNullException(nameof(random));
}
// TODO[api] After removing this, SignatureAlgorithmIdentifier can be assumed present in
// DefaultSignedAttributeTableGenerator.AddMissingStandardAttributes
[Obsolete("Will be removed")]
protected internal virtual IDictionary<CmsAttributeTableParameter, object> GetBaseParameters(
DerObjectIdentifier contentType, AlgorithmIdentifier digAlgId, byte[] hash)
{
var param = new Dictionary<CmsAttributeTableParameter, object>();
if (contentType != null)
{
param[CmsAttributeTableParameter.ContentType] = contentType;
}
param[CmsAttributeTableParameter.DigestAlgorithmIdentifier] = digAlgId;
param[CmsAttributeTableParameter.Digest] = hash.Clone();
return param;
}
internal virtual IDictionary<CmsAttributeTableParameter, object> GetBaseParameters(
DerObjectIdentifier contentType, AlgorithmIdentifier digAlgID, AlgorithmIdentifier sigAlgID, byte[] hash)
{
var param = new Dictionary<CmsAttributeTableParameter, object>();
if (contentType != null)
{
param[CmsAttributeTableParameter.ContentType] = contentType;
}
param[CmsAttributeTableParameter.DigestAlgorithmIdentifier] = digAlgID;
param[CmsAttributeTableParameter.SignatureAlgorithmIdentifier] = sigAlgID;
param[CmsAttributeTableParameter.Digest] = hash.Clone();
return param;
}
// TODO[api] Restrict to internal
protected internal virtual Asn1Set GetAttributeSet(Asn1.Cms.AttributeTable attr)
{
return attr == null ? null : DerSet.FromCollection(attr);
}
public void AddAttributeCertificate(X509V2AttributeCertificate attrCert) =>
CmsUtilities.CollectAttributeCertificate(_certs, attrCert);
public void AddAttributeCertificates(IStore<X509V2AttributeCertificate> attrCertStore) =>
CmsUtilities.CollectAttributeCertificates(_certs, attrCertStore);
public void AddCertificate(X509Certificate cert) => CmsUtilities.CollectCertificate(_certs, cert);
public void AddCertificates(IStore<X509Certificate> certStore) =>
CmsUtilities.CollectCertificates(_certs, certStore);
public void AddCrl(X509Crl crl) => CmsUtilities.CollectCrl(_crls, crl);
public void AddCrls(IStore<X509Crl> crlStore) => CmsUtilities.CollectCrls(_crls, crlStore);
public void AddOtherRevocationInfo(OtherRevocationInfoFormat otherRevocationInfo) =>
CmsUtilities.CollectOtherRevocationInfo(_crls, otherRevocationInfo);
public void AddOtherRevocationInfo(DerObjectIdentifier otherRevInfoFormat, Asn1Encodable otherRevInfo) =>
CmsUtilities.CollectOtherRevocationInfo(_crls, otherRevInfoFormat, otherRevInfo);
public void AddOtherRevocationInfos(IStore<OtherRevocationInfoFormat> otherRevocationInfoStore) =>
CmsUtilities.CollectOtherRevocationInfos(_crls, otherRevocationInfoStore);
public void AddOtherRevocationInfos(DerObjectIdentifier otherRevInfoFormat,
IStore<Asn1Encodable> otherRevInfoStore)
{
CmsUtilities.CollectOtherRevocationInfos(_crls, otherRevInfoFormat, otherRevInfoStore);
}
/**
* Add a store of precalculated signers to the generator.
*
* @param signerStore store of signers
*/
public void AddSigners(SignerInformationStore signerStore)
{
foreach (SignerInformation o in signerStore.SignersInternal)
{
_signers.Add(o);
AddSignerCallback(o);
}
}
public IDigestAlgorithmFinder DigestAlgorithmFinder
{
get { return m_digestAlgorithmFinder; }
set { m_digestAlgorithmFinder = value ?? throw new ArgumentNullException(nameof(value)); }
}
/**
* Return a map of oids and byte arrays representing the digests calculated on the content during
* the last generate.
*
* @return a map of oids (as string objects) and byte[] representing digests.
*/
// TODO[api] Prefer to return just a copy of m_digests (i.e. keyed by DerObjectIdentifier)?
public IDictionary<string, byte[]> GetGeneratedDigests()
{
var result = new Dictionary<string, byte[]>(StringComparer.OrdinalIgnoreCase);
foreach (var entry in m_digests)
{
result.Add(entry.Key.GetID(), entry.Value);
}
return result;
}
[Obsolete("Will be removed. Replaced in CmsSignedDataGenerator by the similar 'UseDefiniteLength' property")]
public bool UseDerForCerts
{
get { return _useDerForCerts; }
set { this._useDerForCerts = value; }
}
[Obsolete("Will be removed. Replaced in CmsSignedDataGenerator by the similar 'UseDefiniteLength' property")]
public bool UseDerForCrls
{
get { return _useDerForCrls; }
set { this._useDerForCrls = value; }
}
internal virtual void AddSignerCallback(SignerInformation signerInformation)
{
}
internal static SignerIdentifier GetSignerIdentifier(X509Certificate c) => CmsUtilities.GetSignerIdentifier(c);
internal static SignerIdentifier GetSignerIdentifier(byte[] subjectKeyIdentifier) =>
CmsUtilities.GetSignerIdentifier(subjectKeyIdentifier);
}
}