Skip to content

Commit 0277e22

Browse files
committed
Handle certificate for authenticode counters.
Authenicode counter signatures now correctly include their own certificate. The parent's "additional certificates" actually contain the chain for authenticode timestamps, so the signature itself must flow through.
1 parent 5d93bb3 commit 0277e22

File tree

1 file changed

+85
-47
lines changed

1 file changed

+85
-47
lines changed

AuthenticodeLint/Signature.cs

Lines changed: 85 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -115,66 +115,25 @@ internal X509Certificate2Collection GetCertificatesFromMessage(CryptMsgSafeHandl
115115
return certs;
116116
}
117117

118-
public IReadOnlyList<ISignature> GetNestedSignatures()
119-
{
120-
var list = new List<ISignature>();
121-
foreach (var attribute in UnsignedAttributes)
122-
{
123-
foreach (var value in attribute.Values)
124-
{
125-
ISignature signature;
126-
if (attribute.Oid.Value == KnownOids.AuthenticodeCounterSignature)
127-
{
128-
signature = new AuthenticodeSignature(value);
129-
}
130-
else if (attribute.Oid.Value == KnownOids.Rfc3161CounterSignature)
131-
{
132-
signature = new Signature(value, SignatureKind.Rfc3161Signature);
133-
}
134-
else if (attribute.Oid.Value == KnownOids.NestedSignatureOid)
135-
{
136-
signature = new Signature(value, SignatureKind.NestedSignature);
137-
}
138-
else
139-
{
140-
continue;
141-
}
142-
var childAttributes = new CryptographicAttributeObjectCollection();
143-
foreach (var childAttribute in signature.UnsignedAttributes)
144-
{
145-
childAttributes.Add(childAttribute);
146-
}
147-
list.Add(signature);
148-
}
149-
}
150-
return list.AsReadOnly();
151-
}
118+
public abstract IReadOnlyList<ISignature> GetNestedSignatures();
152119
}
153120

154121
public class AuthenticodeSignature : SignatureBase
155122
{
156-
157123
public override Oid DigestAlgorithm { get; protected set; }
158124
public override Oid HashEncryptionAlgorithm { get; protected set; }
159125
public override CryptographicAttributeObjectCollection UnsignedAttributes { get; protected set; }
160126
public override CryptographicAttributeObjectCollection SignedAttributes { get; protected set; }
161127
public override byte[] SerialNumber { get; protected set; }
162128
public override X509Certificate2 Certificate { get; protected set; }
163129
public override SignatureKind Kind { get; } = SignatureKind.AuthenticodeSignature;
164-
public override X509Certificate2Collection AdditionalCertificates
165-
{
166-
get
167-
{
168-
return new X509Certificate2Collection();
169-
}
170-
171-
protected set
172-
{
173-
}
174-
}
130+
public override X509Certificate2Collection AdditionalCertificates { get; protected set; }
131+
public ISignature OwningSignature { get; }
175132

176-
public unsafe AuthenticodeSignature(AsnEncodedData data)
133+
public unsafe AuthenticodeSignature(AsnEncodedData data, ISignature owningSignature)
177134
{
135+
OwningSignature = owningSignature;
136+
AdditionalCertificates = owningSignature.AdditionalCertificates;
178137
fixed (byte* dataPtr = data.RawData)
179138
{
180139
uint size = 0;
@@ -189,6 +148,15 @@ public unsafe AuthenticodeSignature(AsnEncodedData data)
189148
SerialNumber = ReadBlob(signerInfo.SerialNumber);
190149
UnsignedAttributes = ReadAttributes(signerInfo.UnauthAttrs);
191150
SignedAttributes = ReadAttributes(signerInfo.AuthAttrs);
151+
var subjectId = new UniversalSubjectIdentifier(signerInfo.Issuer, signerInfo.SerialNumber);
152+
if (subjectId.Type == SubjectIdentifierType.SubjectKeyIdentifier)
153+
{
154+
Certificate = FindCertificate((string)subjectId.Value, OwningSignature.AdditionalCertificates);
155+
}
156+
else if (subjectId.Type == SubjectIdentifierType.IssuerAndSerialNumber)
157+
{
158+
Certificate = FindCertificate((X509IssuerSerial)subjectId.Value, OwningSignature.AdditionalCertificates);
159+
}
192160
}
193161
}
194162
else
@@ -197,6 +165,41 @@ public unsafe AuthenticodeSignature(AsnEncodedData data)
197165
}
198166
}
199167
}
168+
169+
public override IReadOnlyList<ISignature> GetNestedSignatures()
170+
{
171+
var list = new List<ISignature>();
172+
foreach (var attribute in UnsignedAttributes)
173+
{
174+
foreach (var value in attribute.Values)
175+
{
176+
ISignature signature;
177+
if (attribute.Oid.Value == KnownOids.AuthenticodeCounterSignature)
178+
{
179+
signature = new AuthenticodeSignature(value, OwningSignature);
180+
}
181+
else if (attribute.Oid.Value == KnownOids.Rfc3161CounterSignature)
182+
{
183+
signature = new Signature(value, SignatureKind.Rfc3161Signature);
184+
}
185+
else if (attribute.Oid.Value == KnownOids.NestedSignatureOid)
186+
{
187+
signature = new Signature(value, SignatureKind.NestedSignature);
188+
}
189+
else
190+
{
191+
continue;
192+
}
193+
var childAttributes = new CryptographicAttributeObjectCollection();
194+
foreach (var childAttribute in signature.UnsignedAttributes)
195+
{
196+
childAttributes.Add(childAttribute);
197+
}
198+
list.Add(signature);
199+
}
200+
}
201+
return list.AsReadOnly();
202+
}
200203
}
201204

202205
public class Signature : SignatureBase
@@ -284,6 +287,41 @@ internal unsafe Signature(AsnEncodedData data, SignatureKind kind)
284287
}
285288
}
286289
}
290+
291+
public override IReadOnlyList<ISignature> GetNestedSignatures()
292+
{
293+
var list = new List<ISignature>();
294+
foreach (var attribute in UnsignedAttributes)
295+
{
296+
foreach (var value in attribute.Values)
297+
{
298+
ISignature signature;
299+
if (attribute.Oid.Value == KnownOids.AuthenticodeCounterSignature)
300+
{
301+
signature = new AuthenticodeSignature(value, this);
302+
}
303+
else if (attribute.Oid.Value == KnownOids.Rfc3161CounterSignature)
304+
{
305+
signature = new Signature(value, SignatureKind.Rfc3161Signature);
306+
}
307+
else if (attribute.Oid.Value == KnownOids.NestedSignatureOid)
308+
{
309+
signature = new Signature(value, SignatureKind.NestedSignature);
310+
}
311+
else
312+
{
313+
continue;
314+
}
315+
var childAttributes = new CryptographicAttributeObjectCollection();
316+
foreach (var childAttribute in signature.UnsignedAttributes)
317+
{
318+
childAttributes.Add(childAttribute);
319+
}
320+
list.Add(signature);
321+
}
322+
}
323+
return list.AsReadOnly();
324+
}
287325
}
288326

289327
internal class UniversalSubjectIdentifier

0 commit comments

Comments
 (0)