/
CmsSigner.cs
423 lines (366 loc) · 17 KB
/
CmsSigner.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Formats.Asn1;
using System.Security.Cryptography.Asn1;
using System.Security.Cryptography.Pkcs.Asn1;
using System.Security.Cryptography.X509Certificates;
using Internal.Cryptography;
namespace System.Security.Cryptography.Pkcs
{
public sealed class CmsSigner
{
private static readonly Oid s_defaultAlgorithm = Oids.Sha256Oid;
private SubjectIdentifierType _signerIdentifierType;
private RSASignaturePadding? _signaturePadding;
public X509Certificate2? Certificate { get; set; }
public AsymmetricAlgorithm? PrivateKey { get; set; }
public X509Certificate2Collection Certificates { get; private set; } = new X509Certificate2Collection();
public Oid DigestAlgorithm { get; set; }
public X509IncludeOption IncludeOption { get; set; }
public CryptographicAttributeObjectCollection SignedAttributes { get; private set; } = new CryptographicAttributeObjectCollection();
public CryptographicAttributeObjectCollection UnsignedAttributes { get; private set; } = new CryptographicAttributeObjectCollection();
/// <summary>
/// Gets or sets the RSA signature padding to use.
/// </summary>
/// <value>The RSA signature padding to use.</value>
public RSASignaturePadding? SignaturePadding
{
get => _signaturePadding;
set
{
if (value is not null &&
value != RSASignaturePadding.Pkcs1 && value != RSASignaturePadding.Pss)
{
throw new ArgumentException(SR.Argument_InvalidRsaSignaturePadding, nameof(value));
}
_signaturePadding = value;
}
}
public SubjectIdentifierType SignerIdentifierType
{
get { return _signerIdentifierType; }
set
{
if (value < SubjectIdentifierType.IssuerAndSerialNumber || value > SubjectIdentifierType.NoSignature)
throw new ArgumentException(SR.Format(SR.Cryptography_Cms_Invalid_Subject_Identifier_Type, value));
_signerIdentifierType = value;
}
}
public CmsSigner()
: this(SubjectIdentifierType.IssuerAndSerialNumber, null)
{
}
public CmsSigner(SubjectIdentifierType signerIdentifierType)
: this(signerIdentifierType, null)
{
}
public CmsSigner(X509Certificate2? certificate)
: this(SubjectIdentifierType.IssuerAndSerialNumber, certificate)
{
}
#if NETCOREAPP
[Obsolete(Obsoletions.CmsSignerCspParamsCtorMessage, DiagnosticId = Obsoletions.CmsSignerCspParamsCtorDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
#endif
[EditorBrowsable(EditorBrowsableState.Never)]
public CmsSigner(CspParameters parameters) => throw new PlatformNotSupportedException();
public CmsSigner(SubjectIdentifierType signerIdentifierType, X509Certificate2? certificate) : this(signerIdentifierType, certificate, null)
{
}
public CmsSigner(SubjectIdentifierType signerIdentifierType, X509Certificate2? certificate, AsymmetricAlgorithm? privateKey)
: this(signerIdentifierType, certificate, privateKey, signaturePadding: null)
{
}
/// <summary>
/// Initializes a new instance of the CmsSigner class with a specified signer
/// certificate, subject identifier type, private key object, and RSA signature padding.
/// </summary>
/// <param name="signerIdentifierType">
/// One of the enumeration values that specifies the scheme to use for identifying
/// which signing certificate was used.
/// </param>
/// <param name="certificate">
/// The certificate whose private key will be used to sign a message.
/// </param>
/// <param name="privateKey">
/// The private key object to use when signing the message.
/// </param>
/// <param name="signaturePadding">
/// The RSA signature padding to use.
/// </param>
public CmsSigner(
SubjectIdentifierType signerIdentifierType,
X509Certificate2? certificate,
RSA? privateKey,
RSASignaturePadding? signaturePadding)
: this(signerIdentifierType, certificate, (AsymmetricAlgorithm?)privateKey, signaturePadding)
{
}
private CmsSigner(
SubjectIdentifierType signerIdentifierType,
X509Certificate2? certificate,
AsymmetricAlgorithm? privateKey,
RSASignaturePadding? signaturePadding)
{
if (signaturePadding is not null &&
signaturePadding != RSASignaturePadding.Pkcs1 && signaturePadding != RSASignaturePadding.Pss)
{
throw new ArgumentException(SR.Argument_InvalidRsaSignaturePadding, nameof(signaturePadding));
}
switch (signerIdentifierType)
{
case SubjectIdentifierType.Unknown:
_signerIdentifierType = SubjectIdentifierType.IssuerAndSerialNumber;
IncludeOption = X509IncludeOption.ExcludeRoot;
break;
case SubjectIdentifierType.IssuerAndSerialNumber:
_signerIdentifierType = signerIdentifierType;
IncludeOption = X509IncludeOption.ExcludeRoot;
break;
case SubjectIdentifierType.SubjectKeyIdentifier:
_signerIdentifierType = signerIdentifierType;
IncludeOption = X509IncludeOption.ExcludeRoot;
break;
case SubjectIdentifierType.NoSignature:
_signerIdentifierType = signerIdentifierType;
IncludeOption = X509IncludeOption.None;
break;
default:
_signerIdentifierType = SubjectIdentifierType.IssuerAndSerialNumber;
IncludeOption = X509IncludeOption.ExcludeRoot;
break;
}
Certificate = certificate;
DigestAlgorithm = s_defaultAlgorithm.CopyOid();
PrivateKey = privateKey;
_signaturePadding = signaturePadding;
}
internal void CheckCertificateValue()
{
if (SignerIdentifierType == SubjectIdentifierType.NoSignature)
{
return;
}
if (Certificate == null)
{
throw new PlatformNotSupportedException(SR.Cryptography_Cms_NoSignerCert);
}
if (PrivateKey == null && !Certificate.HasPrivateKey)
{
throw new CryptographicException(SR.Cryptography_Cms_Signing_RequiresPrivateKey);
}
}
internal SignerInfoAsn Sign(
ReadOnlyMemory<byte> data,
string? contentTypeOid,
bool silent,
out X509Certificate2Collection chainCerts)
{
HashAlgorithmName hashAlgorithmName = PkcsHelpers.GetDigestAlgorithm(DigestAlgorithm);
SignerInfoAsn newSignerInfo = default;
newSignerInfo.DigestAlgorithm.Algorithm = DigestAlgorithm.Value!;
byte[] dataHash;
IncrementalHash hasher;
try
{
hasher = IncrementalHash.CreateHash(hashAlgorithmName);
}
catch (PlatformNotSupportedException ex)
{
throw new CryptographicException(SR.Format(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithmName), ex);
}
using (hasher)
{
hasher.AppendData(data.Span);
dataHash = hasher.GetHashAndReset();
// If the user specified attributes (not null, count > 0) we need attributes.
// If the content type is null we're counter-signing, and need the message digest attr.
// If the content type is otherwise not-data we need to record it as the content-type attr.
if (SignedAttributes?.Count > 0 || contentTypeOid != Oids.Pkcs7Data)
{
List<AttributeAsn> signedAttrs = BuildAttributes(SignedAttributes);
AsnWriter writer = new AsnWriter(AsnEncodingRules.DER);
writer.WriteOctetString(dataHash);
signedAttrs.Add(
new AttributeAsn
{
AttrType = Oids.MessageDigest,
AttrValues = new[] { new ReadOnlyMemory<byte>(writer.Encode()) },
});
if (contentTypeOid != null)
{
writer.Reset();
writer.WriteObjectIdentifierForCrypto(contentTypeOid);
signedAttrs.Add(
new AttributeAsn
{
AttrType = Oids.ContentType,
AttrValues = new[] { new ReadOnlyMemory<byte>(writer.Encode()) },
});
}
// Use the serializer/deserializer to DER-normalize the attribute order.
SignedAttributesSet signedAttrsSet = default;
signedAttrsSet.SignedAttributes = PkcsHelpers.NormalizeAttributeSet(
signedAttrs.ToArray(),
hasher.AppendData);
// Since this contains user data in a context where BER is permitted, use BER.
// There shouldn't be any observable difference here between BER and DER, though,
// since the top level fields were written by NormalizeSet.
AsnWriter attrsWriter = new AsnWriter(AsnEncodingRules.BER);
signedAttrsSet.Encode(attrsWriter);
newSignerInfo.SignedAttributes = attrsWriter.Encode();
dataHash = hasher.GetHashAndReset();
}
}
switch (SignerIdentifierType)
{
case SubjectIdentifierType.IssuerAndSerialNumber:
byte[] serial = Certificate!.GetSerialNumber();
Array.Reverse(serial);
newSignerInfo.Sid.IssuerAndSerialNumber = new IssuerAndSerialNumberAsn
{
Issuer = Certificate.IssuerName.RawData,
SerialNumber = serial,
};
newSignerInfo.Version = 1;
break;
case SubjectIdentifierType.SubjectKeyIdentifier:
newSignerInfo.Sid.SubjectKeyIdentifier = PkcsPal.Instance.GetSubjectKeyIdentifier(Certificate!);
newSignerInfo.Version = 3;
break;
case SubjectIdentifierType.NoSignature:
newSignerInfo.Sid.IssuerAndSerialNumber = new IssuerAndSerialNumberAsn
{
Issuer = SubjectIdentifier.DummySignerEncodedValue,
SerialNumber = new byte[1],
};
newSignerInfo.Version = 1;
break;
default:
Debug.Fail($"Unresolved SignerIdentifierType value: {SignerIdentifierType}");
throw new CryptographicException();
}
if (UnsignedAttributes != null && UnsignedAttributes.Count > 0)
{
List<AttributeAsn> attrs = BuildAttributes(UnsignedAttributes);
newSignerInfo.UnsignedAttributes = PkcsHelpers.NormalizeAttributeSet(attrs.ToArray());
}
bool signed;
string? signatureAlgorithm;
ReadOnlyMemory<byte> signatureValue;
ReadOnlyMemory<byte> signatureParameters = default;
if (SignerIdentifierType == SubjectIdentifierType.NoSignature)
{
signatureAlgorithm = Oids.NoSignature;
signatureValue = dataHash;
signed = true;
}
else
{
signed = CmsSignature.Sign(
dataHash,
hashAlgorithmName,
Certificate!,
PrivateKey,
silent,
SignaturePadding,
out signatureAlgorithm,
out signatureValue,
out signatureParameters);
}
if (!signed)
{
throw new CryptographicException(SR.Cryptography_Cms_CannotDetermineSignatureAlgorithm);
}
newSignerInfo.SignatureValue = signatureValue;
newSignerInfo.SignatureAlgorithm.Algorithm = signatureAlgorithm!;
if (!signatureParameters.IsEmpty)
{
newSignerInfo.SignatureAlgorithm.Parameters = signatureParameters;
}
X509Certificate2Collection certs = new X509Certificate2Collection();
certs.AddRange(Certificates);
if (SignerIdentifierType != SubjectIdentifierType.NoSignature)
{
if (IncludeOption == X509IncludeOption.EndCertOnly)
{
certs.Add(Certificate!);
}
else if (IncludeOption != X509IncludeOption.None)
{
X509Chain chain = new X509Chain();
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
chain.ChainPolicy.VerificationTime = Certificate!.NotBefore;
if (!chain.Build(Certificate!))
{
foreach (X509ChainStatus status in chain.ChainStatus)
{
if (status.Status == X509ChainStatusFlags.PartialChain)
{
if (chain.ChainElements.Count == 0)
{
// On Android, we will fail with PartialChain to build a cert chain
// even if the failure is an untrusted root cert since the underlying platform
// does not provide a way to distinguish the failure.
// In that case, just use the provided cert.
certs.Add(Certificate!);
}
else
{
throw new CryptographicException(SR.Cryptography_Cms_IncompleteCertChain);
}
}
}
}
X509ChainElementCollection elements = chain.ChainElements;
int count = elements.Count;
int last = count - 1;
if (last == 0)
{
// If there's always one cert treat it as EE, not root.
last = -1;
}
for (int i = 0; i < count; i++)
{
X509Certificate2 cert = elements[i].Certificate;
if (i == last &&
IncludeOption == X509IncludeOption.ExcludeRoot &&
cert.SubjectName.RawData.AsSpan().SequenceEqual(cert.IssuerName.RawData))
{
break;
}
certs.Add(cert);
}
}
}
chainCerts = certs;
return newSignerInfo;
}
internal static List<AttributeAsn> BuildAttributes(CryptographicAttributeObjectCollection? attributes)
{
List<AttributeAsn> signedAttrs = new List<AttributeAsn>();
if (attributes == null || attributes.Count == 0)
{
return signedAttrs;
}
foreach (CryptographicAttributeObject attributeObject in attributes)
{
AttributeAsn newAttr = new AttributeAsn
{
AttrType = attributeObject.Oid!.Value!,
AttrValues = new ReadOnlyMemory<byte>[attributeObject.Values.Count],
};
for (int i = 0; i < attributeObject.Values.Count; i++)
{
newAttr.AttrValues[i] = attributeObject.Values[i].RawData;
}
signedAttrs.Add(newAttr);
}
return signedAttrs;
}
}
}