File size: 14,341 Bytes
8c763fb | 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 424 425 426 427 428 429 430 431 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.ComponentModel;
using System.Management.Automation.Internal;
using System.Security.Cryptography.X509Certificates;
using DWORD = System.UInt32;
namespace System.Management.Automation
{
internal static class Win32Errors
{
internal const DWORD NO_ERROR = 0;
internal const DWORD E_FAIL = 0x80004005;
internal const DWORD TRUST_E_NOSIGNATURE = 0x800b0100;
internal const DWORD TRUST_E_BAD_DIGEST = 0x80096010;
internal const DWORD TRUST_E_PROVIDER_UNKNOWN = 0x800b0001;
internal const DWORD TRUST_E_SUBJECT_FORM_UNKNOWN = 0x800B0003;
internal const DWORD CERT_E_UNTRUSTEDROOT = 0x800b0109;
internal const DWORD TRUST_E_EXPLICIT_DISTRUST = 0x800B0111;
internal const DWORD CRYPT_E_BAD_MSG = 0x8009200d;
internal const DWORD NTE_BAD_ALGID = 0x80090008;
}
/// <summary>
/// Defines the valid status flags that a signature
/// on a file may have.
/// </summary>
public enum SignatureStatus
{
/// <summary>
/// The file has a valid signature. This means only that
/// the signature is syntactically valid. It does not
/// imply trust in any way.
/// </summary>
Valid,
/// <summary>
/// The file has an invalid signature.
/// </summary>
UnknownError,
/// <summary>
/// The file has no signature.
/// </summary>
NotSigned,
/// <summary>
/// The hash of the file does not match the hash stored
/// along with the signature.
/// </summary>
HashMismatch,
/// <summary>
/// The certificate was signed by a publisher not trusted
/// on the system.
/// </summary>
NotTrusted,
/// <summary>
/// The specified file format is not supported by the system
/// for signing operations. This usually means that the
/// system does not know how to sign or verify the file
/// type requested.
/// </summary>
NotSupportedFileFormat,
/// <summary>
/// The signature cannot be verified because it is incompatible
/// with the current system.
/// </summary>
Incompatible
}
/// <summary>
/// Defines the valid types of signatures.
/// </summary>
public enum SignatureType
{
/// <summary>
/// The file is not signed.
/// </summary>
None = 0,
/// <summary>
/// The signature is an Authenticode signature embedded into the file itself.
/// </summary>
Authenticode = 1,
/// <summary>
/// The signature is a catalog signature.
/// </summary>
Catalog = 2
}
/// <summary>
/// Represents a digital signature on a signed
/// file.
/// </summary>
public sealed class Signature
{
private string _path;
private SignatureStatus _status = SignatureStatus.UnknownError;
private DWORD _win32Error;
private X509Certificate2 _signerCert;
private string _statusMessage = string.Empty;
private X509Certificate2 _timeStamperCert;
// private DateTime signedOn = new DateTime(0);
// Three states:
// - True: we can rely on the catalog API to check catalog signature.
// - False: we cannot rely on the catalog API, either because it doesn't exist in the OS (win7, nano),
// or it's not working properly (OneCore SKUs or dev environment where powershell might
// be updated/refreshed).
// - Null: it's not determined yet whether catalog API can be relied on or not.
internal static bool? CatalogApiAvailable = null;
/// <summary>
/// Gets the X509 certificate of the publisher that
/// signed the file.
/// </summary>
public X509Certificate2 SignerCertificate
{
get
{
return _signerCert;
}
}
/// <summary>
/// Gets the X509 certificate of the authority that
/// time-stamped the file.
/// </summary>
public X509Certificate2 TimeStamperCertificate
{
get
{
return _timeStamperCert;
}
}
/// <summary>
/// Gets the status of the signature on the file.
/// </summary>
public SignatureStatus Status
{
get
{
return _status;
}
}
/// <summary>
/// Gets the message corresponding to the status of the
/// signature on the file.
/// </summary>
public string StatusMessage
{
get
{
return _statusMessage;
}
}
/// <summary>
/// Gets the path of the file to which this signature
/// applies.
/// </summary>
public string Path
{
get
{
return _path;
}
}
/// <summary>
/// Returns the signature type of the signature.
/// </summary>
public SignatureType SignatureType { get; internal set; }
/// <summary>
/// True if the item is signed as part of an operating system release.
/// </summary>
public bool IsOSBinary { get; internal set; }
/// <summary>
/// Gets the Subject Alternative Name from the signer certificate.
/// </summary>
public string[] SubjectAlternativeName { get; private set; }
/// <summary>
/// Constructor for class Signature
///
/// Call this to create a validated time-stamped signature object.
/// </summary>
/// <param name="filePath">This signature is found in this file.</param>
/// <param name="error">Win32 error code.</param>
/// <param name="signer">Cert of the signer.</param>
/// <param name="timestamper">Cert of the time stamper.</param>
/// <returns>Constructed object.</returns>
internal Signature(string filePath,
DWORD error,
X509Certificate2 signer,
X509Certificate2 timestamper)
{
Utils.CheckArgForNullOrEmpty(filePath, "filePath");
Utils.CheckArgForNull(signer, "signer");
Utils.CheckArgForNull(timestamper, "timestamper");
Init(filePath, signer, error, timestamper);
}
/// <summary>
/// Constructor for class Signature
///
/// Call this to create a validated signature object.
/// </summary>
/// <param name="filePath">This signature is found in this file.</param>
/// <param name="signer">Cert of the signer.</param>
/// <returns>Constructed object.</returns>
internal Signature(string filePath,
X509Certificate2 signer)
{
Utils.CheckArgForNullOrEmpty(filePath, "filePath");
Utils.CheckArgForNull(signer, "signer");
Init(filePath, signer, 0, null);
}
/// <summary>
/// Constructor for class Signature
///
/// Call this ctor when creating an invalid signature object.
/// </summary>
/// <param name="filePath">This signature is found in this file.</param>
/// <param name="error">Win32 error code.</param>
/// <param name="signer">Cert of the signer.</param>
/// <returns>Constructed object.</returns>
internal Signature(string filePath,
DWORD error,
X509Certificate2 signer)
{
Utils.CheckArgForNullOrEmpty(filePath, "filePath");
Utils.CheckArgForNull(signer, "signer");
Init(filePath, signer, error, null);
}
/// <summary>
/// Constructor for class Signature
///
/// Call this ctor when creating an invalid signature object.
/// </summary>
/// <param name="filePath">This signature is found in this file.</param>
/// <param name="error">Win32 error code.</param>
/// <returns>Constructed object.</returns>
internal Signature(string filePath, DWORD error)
{
Utils.CheckArgForNullOrEmpty(filePath, "filePath");
Init(filePath, null, error, null);
}
private void Init(string filePath,
X509Certificate2 signer,
DWORD error,
X509Certificate2 timestamper)
{
_path = filePath;
_win32Error = error;
_signerCert = signer;
_timeStamperCert = timestamper;
SignatureType = SignatureType.None;
SignatureStatus isc =
GetSignatureStatusFromWin32Error(error);
_status = isc;
_statusMessage = GetSignatureStatusMessage(isc,
error,
filePath);
// Extract Subject Alternative Name from the signer certificate
SubjectAlternativeName = GetSubjectAlternativeName(signer);
}
private static SignatureStatus GetSignatureStatusFromWin32Error(DWORD error)
{
SignatureStatus isc = SignatureStatus.UnknownError;
switch (error)
{
case Win32Errors.NO_ERROR:
isc = SignatureStatus.Valid;
break;
case Win32Errors.NTE_BAD_ALGID:
isc = SignatureStatus.Incompatible;
break;
case Win32Errors.TRUST_E_NOSIGNATURE:
isc = SignatureStatus.NotSigned;
break;
case Win32Errors.TRUST_E_BAD_DIGEST:
case Win32Errors.CRYPT_E_BAD_MSG:
isc = SignatureStatus.HashMismatch;
break;
case Win32Errors.TRUST_E_PROVIDER_UNKNOWN:
isc = SignatureStatus.NotSupportedFileFormat;
break;
case Win32Errors.TRUST_E_EXPLICIT_DISTRUST:
isc = SignatureStatus.NotTrusted;
break;
}
return isc;
}
private static string GetSignatureStatusMessage(SignatureStatus status,
DWORD error,
string filePath)
{
string message = null;
string resourceString = null;
string arg = null;
switch (status)
{
case SignatureStatus.Valid:
resourceString = MshSignature.MshSignature_Valid;
break;
case SignatureStatus.UnknownError:
int intError = SecuritySupport.GetIntFromDWORD(error);
Win32Exception e = new Win32Exception(intError);
message = e.Message;
break;
case SignatureStatus.Incompatible:
if (error == Win32Errors.NTE_BAD_ALGID)
{
resourceString = MshSignature.MshSignature_Incompatible_HashAlgorithm;
}
else
{
resourceString = MshSignature.MshSignature_Incompatible;
}
arg = filePath;
break;
case SignatureStatus.NotSigned:
resourceString = MshSignature.MshSignature_NotSigned;
arg = filePath;
break;
case SignatureStatus.HashMismatch:
resourceString = MshSignature.MshSignature_HashMismatch;
arg = filePath;
break;
case SignatureStatus.NotTrusted:
resourceString = MshSignature.MshSignature_NotTrusted;
arg = filePath;
break;
case SignatureStatus.NotSupportedFileFormat:
resourceString = MshSignature.MshSignature_NotSupportedFileFormat;
arg = System.IO.Path.GetExtension(filePath);
if (string.IsNullOrEmpty(arg))
{
resourceString = MshSignature.MshSignature_NotSupportedFileFormat_NoExtension;
arg = null;
}
break;
}
if (message == null)
{
if (arg == null)
{
message = resourceString;
}
else
{
message = StringUtil.Format(resourceString, arg);
}
}
return message;
}
/// <summary>
/// Extracts the Subject Alternative Name from the certificate.
/// </summary>
/// <param name="certificate">The certificate to extract SAN from.</param>
/// <returns>Array of SAN entries or null if not found.</returns>
private static string[] GetSubjectAlternativeName(X509Certificate2 certificate)
{
if (certificate == null)
{
return null;
}
foreach (X509Extension extension in certificate.Extensions)
{
if (extension.Oid != null && extension.Oid.Value == CertificateFilterInfo.SubjectAlternativeNameOid)
{
string formatted = extension.Format(multiLine: true);
if (string.IsNullOrEmpty(formatted))
{
return null;
}
return formatted.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
}
}
return null;
}
}
}
|