|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Linq; |
| 6 | +using System.Security.Cryptography.X509Certificates; |
| 7 | +using NuGet.Services.Validation; |
| 8 | + |
| 9 | +namespace Validation.PackageSigning.ValidateCertificate |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Deciders are created from a given <see cref="CertificateVerificationResult"/> to decide |
| 13 | + /// how each of the certificate's dependent <see cref="PackageSignature"/>s should be affected. |
| 14 | + /// </summary> |
| 15 | + /// <param name="signature">A signature that depends on the <see cref="EndCertificate"/> that was verified.</param> |
| 16 | + /// <returns>How the signature should be handled.</returns> |
| 17 | + public delegate SignatureDecision SignatureDecider(PackageSignature signature); |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Creates functions that decide how <see cref="PackageSignature"/>s should be affected by |
| 21 | + /// <see cref="EndCertificate.Status"/> changes. |
| 22 | + /// </summary> |
| 23 | + public class SignatureDeciderFactory |
| 24 | + { |
| 25 | + /// <summary> |
| 26 | + /// Create a function to decide how signatures should be affected by a revoked certificate. |
| 27 | + /// </summary> |
| 28 | + /// <param name="certificate">The certificate that was revoked.</param> |
| 29 | + /// <param name="result">The verification result that describes when the certificate was revoked.</param> |
| 30 | + /// <returns>The function that describes how dependent signatures should be affected by the certificate status change.</returns> |
| 31 | + public SignatureDecider MakeDeciderForRevokedCertificate(EndCertificate certificate, CertificateVerificationResult result) |
| 32 | + { |
| 33 | + switch (certificate.Use) |
| 34 | + { |
| 35 | + case EndCertificateUse.Timestamping: |
| 36 | + return RejectSignaturesAtIngestionOtherwiseWarnDecider; |
| 37 | + |
| 38 | + case EndCertificateUse.CodeSigning: |
| 39 | + return MakeDeciderForRevokedCodeSigningCertificate(result.RevocationTime); |
| 40 | + |
| 41 | + default: |
| 42 | + throw new InvalidOperationException($"Revoked certificate has unknown use: {certificate.Use}"); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Create a function to decide how signatures should be affected by an invalidated certificate. |
| 48 | + /// </summary> |
| 49 | + /// <param name="certificate">The certificate that was invalidated.</param> |
| 50 | + /// <param name="result">The verification result that describes why the certificate was invalidated.</param> |
| 51 | + /// <returns>The function that describes how dependent signatures should be affected by the certificate status change.</returns> |
| 52 | + public SignatureDecider MakeDeciderForInvalidatedCertificate(EndCertificate certificate, CertificateVerificationResult result) |
| 53 | + { |
| 54 | + if (result.Status != EndCertificateStatus.Invalid) |
| 55 | + { |
| 56 | + throw new ArgumentException($"Result must have a status of {nameof(EndCertificateStatus.Invalid)}", nameof(result)); |
| 57 | + } |
| 58 | + |
| 59 | + if (result.StatusFlags == X509ChainStatusFlags.NoError || |
| 60 | + (result.StatusFlags & (X509ChainStatusFlags.OfflineRevocation | X509ChainStatusFlags.RevocationStatusUnknown)) != 0) |
| 61 | + { |
| 62 | + throw new ArgumentException($"Invalid flags on invalid verification result: {result.StatusFlags}!", nameof(result)); |
| 63 | + } |
| 64 | + |
| 65 | + // If a certificate used for the primary signature is revoked, all dependent signatures should be invalidated. |
| 66 | + // Note that in this case the revoked certificate is a parent of the end certificate - NOT the end certificate. |
| 67 | + if (certificate.Use == EndCertificateUse.CodeSigning && (result.StatusFlags & X509ChainStatusFlags.Revoked) != 0) |
| 68 | + { |
| 69 | + return RejectAllSignaturesDecider; |
| 70 | + } |
| 71 | + |
| 72 | + // NotTimeValid and HasWeakSignature fail packages only at ingestion. |
| 73 | + else if (ResultHasOnlyFlags(result, X509ChainStatusFlags.NotTimeValid | X509ChainStatusFlags.HasWeakSignature)) |
| 74 | + { |
| 75 | + return RejectSignaturesAtIngestionDecider; |
| 76 | + } |
| 77 | + |
| 78 | + // NotTimeNested does not affect signatures and should be ignored. |
| 79 | + else if (ResultHasOnlyFlags(result, X509ChainStatusFlags.NotTimeNested)) |
| 80 | + { |
| 81 | + return NoActionDecider; |
| 82 | + } |
| 83 | + |
| 84 | + // In all other cases, reject signatures at ingestion and warn on all other signatures. |
| 85 | + else |
| 86 | + { |
| 87 | + return RejectSignaturesAtIngestionOtherwiseWarnDecider; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private SignatureDecider MakeDeciderForRevokedCodeSigningCertificate(DateTime? revocationTime) |
| 92 | + { |
| 93 | + // If the time the certificate was revoked is unknown, assume the worst and reject all dependent signatures. |
| 94 | + if (!revocationTime.HasValue) |
| 95 | + { |
| 96 | + return RejectAllSignaturesDecider; |
| 97 | + } |
| 98 | + |
| 99 | + return (PackageSignature signature) => |
| 100 | + { |
| 101 | + // The revoked code signing certificate invalidates signatures with no valid timestamps. |
| 102 | + // TODO: This should skip trusted timestamps with invalid/revoked certs. This requires: |
| 103 | + // * Getting trusted timestamps' certificates and their statuses. |
| 104 | + if (!signature.TrustedTimestamps.Any()) |
| 105 | + { |
| 106 | + return SignatureDecision.Reject; |
| 107 | + } |
| 108 | + |
| 109 | + // The revoked code signing certificate invalidates signatures with at least one trusted |
| 110 | + // timestamp before the revocation date. |
| 111 | + if (signature.TrustedTimestamps.Any(t => revocationTime.Value <= t.Value)) |
| 112 | + { |
| 113 | + return SignatureDecision.Reject; |
| 114 | + } |
| 115 | + |
| 116 | + // This signature lives to see another day. |
| 117 | + return SignatureDecision.Ignore; |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + private SignatureDecision NoActionDecider(PackageSignature signature) => SignatureDecision.Ignore; |
| 122 | + |
| 123 | + private SignatureDecision RejectAllSignaturesDecider(PackageSignature signature) => SignatureDecision.Reject; |
| 124 | + |
| 125 | + private SignatureDecision RejectSignaturesAtIngestionDecider(PackageSignature signature) |
| 126 | + { |
| 127 | + return (signature.Status == PackageSignatureStatus.Unknown) |
| 128 | + ? SignatureDecision.Reject |
| 129 | + : SignatureDecision.Ignore; |
| 130 | + } |
| 131 | + |
| 132 | + private SignatureDecision RejectSignaturesAtIngestionOtherwiseWarnDecider(PackageSignature signature) |
| 133 | + { |
| 134 | + return (signature.Status == PackageSignatureStatus.Unknown) |
| 135 | + ? SignatureDecision.Reject |
| 136 | + : SignatureDecision.Warn; |
| 137 | + } |
| 138 | + |
| 139 | + private bool ResultHasOnlyFlags(CertificateVerificationResult result, X509ChainStatusFlags flags) |
| 140 | + { |
| 141 | + if (result.StatusFlags == X509ChainStatusFlags.NoError) |
| 142 | + { |
| 143 | + return flags == X509ChainStatusFlags.NoError; |
| 144 | + } |
| 145 | + |
| 146 | + return (result.StatusFlags & flags) == result.StatusFlags; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments