-
Notifications
You must be signed in to change notification settings - Fork 695
/
SignatureTrustAndValidityVerificationProvider.cs
308 lines (259 loc) · 14.1 KB
/
SignatureTrustAndValidityVerificationProvider.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
namespace NuGet.Packaging.Signing
{
public sealed class SignatureTrustAndValidityVerificationProvider : ISignatureVerificationProvider
{
private readonly HashAlgorithmName _fingerprintAlgorithm;
private readonly IEnumerable<KeyValuePair<string, HashAlgorithmName>> _allowUntrustedRootList;
public SignatureTrustAndValidityVerificationProvider(IEnumerable<KeyValuePair<string, HashAlgorithmName>> allowUntrustedRootList = null)
{
_fingerprintAlgorithm = HashAlgorithmName.SHA256;
_allowUntrustedRootList = allowUntrustedRootList;
}
public Task<PackageVerificationResult> GetTrustResultAsync(
ISignedPackageReader package,
PrimarySignature signature,
SignedPackageVerifierSettings settings,
CancellationToken token)
{
token.ThrowIfCancellationRequested();
if (package == null)
{
throw new ArgumentNullException(nameof(package));
}
if (signature == null)
{
throw new ArgumentNullException(nameof(signature));
}
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}
var result = Verify(signature, settings);
return Task.FromResult(result);
}
#if IS_SIGNING_SUPPORTED
private PackageVerificationResult Verify(
PrimarySignature signature,
SignedPackageVerifierSettings settings)
{
var certificateExtraStore = signature.SignedCms.Certificates;
var repositoryCountersignatureExists = SignatureUtility.HasRepositoryCountersignature(signature);
var isRepositoryCountersignatureVerificationRequested = settings.VerificationTarget.HasFlag(VerificationTarget.Repository) &&
settings.SignaturePlacement.HasFlag(SignaturePlacement.Countersignature);
var allowDeferralToRepositoryCountersignature = isRepositoryCountersignatureVerificationRequested &&
repositoryCountersignatureExists;
var status = SignatureVerificationStatus.Unknown;
var issues = Enumerable.Empty<SignatureLog>();
var isUntrustedRootAllowed = IsUntrustedRootAllowed(signature);
var verifySettings = new SignatureVerifySettings(
allowIllegal: settings.AllowIllegal,
allowUntrusted: settings.AllowUntrusted || isUntrustedRootAllowed,
allowUnknownRevocation: settings.AllowUnknownRevocation,
reportUnknownRevocation: settings.ReportUnknownRevocation,
reportUntrustedRoot: !isUntrustedRootAllowed,
revocationMode: settings.RevocationMode);
SignatureVerificationSummary primarySummary = null;
if (settings.SignaturePlacement.HasFlag(SignaturePlacement.PrimarySignature) &&
VerificationUtility.IsVerificationTarget(signature.Type, settings.VerificationTarget))
{
primarySummary = VerifyValidityAndTrust(signature, settings, verifySettings, certificateExtraStore);
issues = issues.Concat(primarySummary.Issues);
status = primarySummary.Status;
}
Debug.Assert(isRepositoryCountersignatureVerificationRequested != (settings.RepositoryCountersignatureVerificationBehavior == SignatureVerificationBehavior.Never));
bool shouldVerifyRepositoryCountersignature;
switch (settings.RepositoryCountersignatureVerificationBehavior)
{
case SignatureVerificationBehavior.IfExists:
shouldVerifyRepositoryCountersignature = isRepositoryCountersignatureVerificationRequested &&
repositoryCountersignatureExists;
break;
case SignatureVerificationBehavior.IfExistsAndIsNecessary:
// The repository countersignature should be evaluated if settings allow it, if a repository countersignature exists
// and if either settings only allow a repository countersignature to be evaluated or the primary signature has some
// validation/trust issues that may benefit from a repository countersignature fallback.
shouldVerifyRepositoryCountersignature = isRepositoryCountersignatureVerificationRequested &&
repositoryCountersignatureExists &&
(primarySummary == null ||
(primarySummary != null &&
(HasUntrustedRoot(primarySummary) || IsSignatureExpired(primarySummary))));
break;
case SignatureVerificationBehavior.Always:
shouldVerifyRepositoryCountersignature = isRepositoryCountersignatureVerificationRequested;
break;
case SignatureVerificationBehavior.Never:
shouldVerifyRepositoryCountersignature = false;
break;
default:
throw new NotImplementedException();
}
if (shouldVerifyRepositoryCountersignature)
{
var countersignature = RepositoryCountersignature.GetRepositoryCountersignature(signature);
if (countersignature == null)
{
if (settings.RepositoryCountersignatureVerificationBehavior == SignatureVerificationBehavior.Always)
{
issues = issues.Concat(new[] { SignatureLog.Error(NuGetLogCode.NU3038, Strings.NoRepositoryCountersignature) });
status = SignatureVerificationStatus.Disallowed;
}
}
else
{
isUntrustedRootAllowed = IsUntrustedRootAllowed(countersignature);
verifySettings = new SignatureVerifySettings(
allowIllegal: settings.AllowIllegal,
allowUntrusted: settings.AllowUntrusted || isUntrustedRootAllowed,
allowUnknownRevocation: settings.AllowUnknownRevocation,
reportUnknownRevocation: settings.ReportUnknownRevocation,
reportUntrustedRoot: !isUntrustedRootAllowed,
revocationMode: settings.RevocationMode);
var countersignatureSummary = VerifyValidityAndTrust(countersignature, settings, verifySettings, certificateExtraStore);
if (primarySummary == null)
{
status = countersignatureSummary.Status;
}
else
{
if (countersignatureSummary.Status == SignatureVerificationStatus.Valid)
{
if (IsSignatureExpired(primarySummary) && HasUntrustedRoot(primarySummary))
{
// Exclude the issue of the primary signature being untrusted since the repository countersignature fulfills the role of a trust anchor.
issues = issues.Where(log => log.Code != NuGetLogCode.NU3018);
if (countersignatureSummary.Timestamp != null &&
Rfc3161TimestampVerificationUtility.ValidateSignerCertificateAgainstTimestamp(signature.SignerInfo.Certificate, countersignatureSummary.Timestamp))
{
// Exclude the issue of the primary signature being expired since the repository countersignature fulfills the role of a trusted timestamp.
issues = issues.Where(log => log.Code != NuGetLogCode.NU3037);
status = SignatureVerificationStatus.Valid;
}
}
else if (IsSignatureExpired(primarySummary) &&
countersignatureSummary.Timestamp != null &&
Rfc3161TimestampVerificationUtility.ValidateSignerCertificateAgainstTimestamp(signature.SignerInfo.Certificate, countersignatureSummary.Timestamp))
{
// Exclude the issue of the primary signature being expired since the repository countersignature fulfills the role of a trusted timestamp.
issues = issues.Where(log => log.Code != NuGetLogCode.NU3037);
status = SignatureVerificationStatus.Valid;
}
else if (HasUntrustedRoot(primarySummary))
{
// Exclude the issue of the primary signature being untrusted since the repository countersignature fulfills the role of a trust anchor.
issues = issues.Where(log => log.Code != NuGetLogCode.NU3018);
status = SignatureVerificationStatus.Valid;
}
}
// Both the primary signature and the repository countersignature were evaluated.
// The overall status should be the more severe status of the two.
status = (SignatureVerificationStatus)Math.Min((int)status, (int)countersignatureSummary.Status);
}
issues = issues.Concat(countersignatureSummary.Issues);
}
}
return new SignedPackageVerificationResult(status, signature, issues);
}
private SignatureVerificationSummary GetTimestamp(
Signature signature,
SignedPackageVerifierSettings verifierSettings,
out Timestamp timestamp)
{
var issues = new List<SignatureLog>();
SignatureVerificationStatus status;
SignatureVerificationStatusFlags statusFlags;
var succeeded = signature.TryGetValidTimestamp(verifierSettings, _fingerprintAlgorithm, issues, out statusFlags, out timestamp);
status = VerificationUtility.GetSignatureVerificationStatus(statusFlags);
if (!succeeded)
{
if (statusFlags == SignatureVerificationStatusFlags.NoValidTimestamp ||
statusFlags == SignatureVerificationStatusFlags.MultipleTimestamps)
{
status = SignatureVerificationStatus.Disallowed;
}
}
return new SignatureVerificationSummary(signature.Type, status, statusFlags, issues);
}
private SignatureVerificationSummary VerifyValidityAndTrust(
Signature signature,
SignedPackageVerifierSettings verifierSettings,
SignatureVerifySettings settings,
X509Certificate2Collection certificateExtraStore)
{
Timestamp timestamp;
var timestampSummary = GetTimestamp(signature, verifierSettings, out timestamp);
var status = signature.Verify(
timestamp,
settings,
_fingerprintAlgorithm,
certificateExtraStore);
if (timestampSummary.Status != SignatureVerificationStatus.Valid && !verifierSettings.AllowIgnoreTimestamp)
{
return new SignatureVerificationSummary(
signature.Type,
SignatureVerificationStatus.Disallowed,
SignatureVerificationStatusFlags.NoValidTimestamp,
status.Issues.Concat(timestampSummary.Issues));
}
return new SignatureVerificationSummary(
status.SignatureType,
status.Status,
status.Flags,
status.Timestamp,
status.ExpirationTime,
status.Issues.Concat(timestampSummary.Issues));
}
private bool IsUntrustedRootAllowed(Signature signature)
{
if (_allowUntrustedRootList != null)
{
foreach (var allowUntrustedRoot in _allowUntrustedRootList)
{
try
{
var signingCertificateFingerprint = signature.GetSigningCertificateFingerprint(allowUntrustedRoot.Value);
if (string.Equals(allowUntrustedRoot.Key, signingCertificateFingerprint, StringComparison.Ordinal))
{
return true;
}
}
// if the exception is InvalidOperationException it means the signature is invalid
catch (InvalidOperationException)
{
return false;
}
// any other exception means the entry is invalid, therefore we just skip it
catch { }
}
}
return false;
}
private static bool HasUntrustedRoot(SignatureVerificationSummary summary)
{
return summary.SignatureType != SignatureType.Repository &&
summary.Status != SignatureVerificationStatus.Valid &&
summary.Flags.HasFlag(SignatureVerificationStatusFlags.UntrustedRoot);
}
private static bool IsSignatureExpired(SignatureVerificationSummary summary)
{
return summary.SignatureType != SignatureType.Repository && summary.ExpirationTime.HasValue;
}
#else
private PackageVerificationResult Verify(
PrimarySignature signature,
SignedPackageVerifierSettings settings)
{
throw new NotSupportedException();
}
#endif
}
}