/* * Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved. * * This file is part of System Informer. * * Authors: * * dmex * */ namespace CustomBuildTool { /// /// ECDsa implementation that uses Azure Key Vault /// public sealed class ECDsaKeyVault : ECDsa { private readonly KeyVaultContext VaultContext; private ECDsa PublicKey; private bool Disposed; /// /// Creates a new ECDsaKeyVault instance /// /// Context with parameters public ECDsaKeyVault(KeyVaultContext Context) { if (!Context.IsValid) throw new ArgumentException("Must not be the default", nameof(Context)); this.VaultContext = Context; if (Context.Certificate != null) { this.PublicKey = Context.Certificate.GetECDsaPublicKey(); } else { throw new ArgumentException("Public key must be provided in KeyVaultContext", nameof(Context)); } this.KeySizeValue = this.PublicKey!.KeySize; this.LegalKeySizesValue = [new KeySizes(this.PublicKey.KeySize, this.PublicKey.KeySize, 0)]; } /// public override string KeyExchangeAlgorithm { get { CheckDisposed(); return null; } } /// public override int KeySize { get { CheckDisposed(); return base.KeySize; } set => throw new NotSupportedException("Key generation is not supported by this provider"); } /// public override KeySizes[] LegalKeySizes { get { CheckDisposed(); return (KeySizes[])base.LegalKeySizes.Clone(); } } /// public override string SignatureAlgorithm { get { CheckDisposed(); return "ECDsa"; } } /// /// Generates a digital signature for the specified hash using the configured elliptic curve key and algorithm. /// /// The method selects the appropriate hash algorithm based on the key size. Supported /// key sizes are 256, 384, and 521 bits, corresponding to SHA-256, SHA-384, and SHA-512 respectively. /// The hash value to be signed. The length of the hash must correspond to the key size and supported hash /// algorithm. /// A byte array containing the digital signature of the provided hash. /// Thrown if the length of the hash does not match the expected digest length for the key size. public override byte[] SignHash(byte[] Hash) { CheckDisposed(); ValidateKeyDigestCombination(KeySize, Hash.Length); if (KeySize == 256) return this.VaultContext.SignDigest(Hash, HashAlgorithmName.SHA256, KeyVaultSignatureAlgorithm.ECDsa); if (KeySize == 384) return this.VaultContext.SignDigest(Hash, HashAlgorithmName.SHA384, KeyVaultSignatureAlgorithm.ECDsa); if (KeySize == 521) return this.VaultContext.SignDigest(Hash, HashAlgorithmName.SHA512, KeyVaultSignatureAlgorithm.ECDsa); throw new ArgumentException("Digest length is not valid for the key size.", nameof(Hash)); } /// public override bool TrySignHash(ReadOnlySpan Hash, Span Destination, out int BytesWritten) { CheckDisposed(); byte[] signature = SignHash(Hash.ToArray()); if (signature.Length > Destination.Length) { BytesWritten = 0; return false; } signature.CopyTo(Destination); BytesWritten = signature.Length; return true; } /// protected override byte[] SignHashCore(ReadOnlySpan Hash, DSASignatureFormat SignatureFormat) { CheckDisposed(); return ConvertSignatureToFormat(SignHash(Hash.ToArray()), SignatureFormat); } /// protected override bool TrySignHashCore(ReadOnlySpan Hash, Span Destination, DSASignatureFormat SignatureFormat, out int BytesWritten) { CheckDisposed(); byte[] signature = SignHashCore(Hash, SignatureFormat); if (signature.Length > Destination.Length) { BytesWritten = 0; return false; } signature.CopyTo(Destination); BytesWritten = signature.Length; return true; } /// public override byte[] SignData(byte[] Data, HashAlgorithmName HashAlgorithm) { ArgumentNullException.ThrowIfNull(Data); return SignData(Data, 0, Data.Length, HashAlgorithm); } /// public override byte[] SignData(byte[] Data, int Offset, int Count, HashAlgorithmName HashAlgorithm) { CheckDisposed(); return SignHash(HashData(Data, Offset, Count, HashAlgorithm)); } /// public override byte[] SignData(Stream Data, HashAlgorithmName HashAlgorithm) { CheckDisposed(); return SignHash(HashData(Data, HashAlgorithm)); } /// public override bool TrySignData(ReadOnlySpan Data, Span Destination, HashAlgorithmName HashAlgorithm, out int BytesWritten) { CheckDisposed(); byte[] hash = HashDataIncremental(Data, HashAlgorithm); return TrySignHash(hash, Destination, out BytesWritten); } /// protected override byte[] SignDataCore(ReadOnlySpan Data, HashAlgorithmName HashAlgorithm, DSASignatureFormat SignatureFormat) { CheckDisposed(); return SignHashCore(HashDataIncremental(Data, HashAlgorithm), SignatureFormat); } /// protected override byte[] SignDataCore(Stream Data, HashAlgorithmName HashAlgorithm, DSASignatureFormat SignatureFormat) { CheckDisposed(); return SignHashCore(HashData(Data, HashAlgorithm), SignatureFormat); } /// protected override bool TrySignDataCore(ReadOnlySpan Data, Span Destination, HashAlgorithmName HashAlgorithm, DSASignatureFormat SignatureFormat, out int BytesWritten) { CheckDisposed(); byte[] hash = HashDataIncremental(Data, HashAlgorithm); return TrySignHashCore(hash, Destination, SignatureFormat, out BytesWritten); } /// /// Computes the hash value for a specified region of the input data using the provided hash algorithm. /// /// The method supports hashing only the specified segment of the input array, allowing /// partial data hashing. The hash algorithm must be compatible with the key size used by the /// implementation. /// The byte array containing the data to be hashed. /// The zero-based index in the data array at which to begin hashing. /// The number of bytes to hash, starting from the specified offset. /// The hash algorithm to use for computing the hash value. /// A byte array containing the computed hash value for the specified data segment. protected override byte[] HashData(byte[] Data, int Offset, int Count, HashAlgorithmName HashAlgorithm) { CheckDisposed(); ValidateKeyDigestCombination(KeySize, HashAlgorithm); return HashDataIncremental(Data, Offset, Count, HashAlgorithm); } /// protected override byte[] HashData(Stream Data, HashAlgorithmName HashAlgorithm) { CheckDisposed(); ValidateKeyDigestCombination(KeySize, HashAlgorithm); using (IncrementalHash incrementalHash = IncrementalHash.CreateHash(HashAlgorithm)) { byte[] buffer = new byte[0x4000]; int bytesRead; while ((bytesRead = Data.Read(buffer, 0, buffer.Length)) != 0) { incrementalHash.AppendData(buffer, 0, bytesRead); } return incrementalHash.GetHashAndReset(); } } /// protected override bool TryHashData(ReadOnlySpan Data, Span Destination, HashAlgorithmName HashAlgorithm, out int BytesWritten) { CheckDisposed(); ValidateKeyDigestCombination(KeySize, HashAlgorithm); byte[] hash = HashDataIncremental(Data, HashAlgorithm); if (hash.Length > Destination.Length) { BytesWritten = 0; return false; } hash.CopyTo(Destination); BytesWritten = hash.Length; return true; } /// /// Computes the hash value for a specified segment of a byte array using the given hash algorithm. /// /// This method processes only the specified segment of the input array, defined by the /// offset and count parameters. The hash algorithm is applied incrementally, which can be useful for hashing /// large data in segments. /// The byte array containing the data to hash. /// The zero-based index in the array at which to begin hashing. /// The number of bytes to hash, starting from the specified offset. /// The hash algorithm to use for computing the hash value. /// A byte array containing the computed hash value for the specified data segment. private static byte[] HashDataIncremental(byte[] Data, int Offset, int Count, HashAlgorithmName HashAlgorithm) { ReadOnlySpan span = Data.AsSpan(Offset, Count); using (IncrementalHash incrementalHash = IncrementalHash.CreateHash(HashAlgorithm)) { incrementalHash.AppendData(span); return incrementalHash.GetHashAndReset(); } } /// /// Computes the hash value for a specified segment of a byte array using the given hash algorithm. /// /// This method processes only the specified segment of the input array, defined by the /// offset and count parameters. The caller is responsible for ensuring that the offset and count are within the /// bounds of the array. The method does not allocate an IncrementalHash instance for each call, which may /// improve performance in scenarios where hashing is performed infrequently. /// The byte array containing the data to hash. /// The zero-based index in the array at which to begin hashing. /// The number of bytes to hash, starting from the specified offset. /// The hash algorithm to use for computing the hash value. /// A byte array containing the computed hash value for the specified data segment. private static byte[] HashDataStatic(byte[] Data, int Offset, int Count, HashAlgorithmName HashAlgorithm) { ReadOnlySpan span = Data.AsSpan(Offset, Count); return HashAlgorithm.Name switch { // Fast path: use allocation-free static hashing for supported algorithms nameof(HashAlgorithmName.SHA256) => SHA256.HashData(span), nameof(HashAlgorithmName.SHA384) => SHA384.HashData(span), nameof(HashAlgorithmName.SHA512) => SHA512.HashData(span), // Fallback path: required for algorithms without static HashData APIs _ => HashDataIncremental(span, HashAlgorithm) }; } /// /// Computes the hash of a specified segment of a byte array using the given hash algorithm. /// /// This method avoids allocation of incremental hash objects by using static hash /// algorithm instances. Only SHA256, SHA384, and SHA512 are supported. /// The byte array containing the data to hash. /// The zero-based index in the array at which to begin hashing. /// The number of bytes to hash, starting from the specified offset. /// The hash algorithm to use for computing the hash. Supported values are SHA256, SHA384, and SHA512. /// A byte array containing the computed hash value for the specified segment of the input data. /// Thrown if the specified hash algorithm is not supported. private static byte[] HashDataFixed(byte[] Data, int Offset, int Count, HashAlgorithmName HashAlgorithm) { ReadOnlySpan span = Data.AsSpan(Offset, Count); return HashAlgorithm.Name switch { // Fast path: use allocation-free static hashing for supported algorithms nameof(HashAlgorithmName.SHA256) => SHA256.HashData(span), nameof(HashAlgorithmName.SHA384) => SHA384.HashData(span), nameof(HashAlgorithmName.SHA512) => SHA512.HashData(span), // Fallback path: required for algorithms without static HashData APIs _ => HashDataIncremental(span, HashAlgorithm) }; } /// /// Computes the cryptographic hash of the specified data using the provided hash algorithm. /// /// This method processes the entire input data in a single operation using an /// incremental hashing approach. The caller is responsible for selecting a supported hash algorithm. /// The input data to compute the hash for. /// The hash algorithm to use for computing the hash. /// A byte array containing the computed hash value. private static byte[] HashDataIncremental(ReadOnlySpan Data, HashAlgorithmName HashAlgorithm) { using (IncrementalHash incrementalHash = IncrementalHash.CreateHash(HashAlgorithm)) { incrementalHash.AppendData(Data); return incrementalHash.GetHashAndReset(); } } /// /// Verifies that a digital signature is valid for the specified hash value using the current public key. /// /// This method requires that the object has not been disposed and that the key and hash /// sizes are compatible. If the public key is not available or the object is disposed, an exception is /// thrown. /// The hash value to verify. This must match the hash algorithm and key size used to generate the signature. /// The digital signature to verify against the specified hash. /// true if the signature is valid for the specified hash and public key; otherwise, false. public override bool VerifyHash(byte[] Hash, byte[] Signature) { CheckDisposed(); ValidateKeyDigestCombination(KeySize, Hash.Length); ECDsa publicKey = this.PublicKey; ObjectDisposedException.ThrowIf(publicKey is null, this); return publicKey.VerifyHash(Hash, Signature); } /// public override bool VerifyHash(ReadOnlySpan Hash, ReadOnlySpan Signature) { CheckDisposed(); ValidateKeyDigestCombination(KeySize, Hash.Length); return this.PublicKey.VerifyHash(Hash, Signature); } /// protected override bool VerifyHashCore(ReadOnlySpan Hash, ReadOnlySpan Signature, DSASignatureFormat SignatureFormat) { CheckDisposed(); ValidateKeyDigestCombination(KeySize, Hash.Length); return this.PublicKey.VerifyHash(Hash, ConvertSignatureFromFormat(Signature, SignatureFormat)); } /// public override bool VerifyData(byte[] Data, int Offset, int Count, byte[] Signature, HashAlgorithmName HashAlgorithm) { CheckDisposed(); return this.PublicKey.VerifyHash(HashData(Data, Offset, Count, HashAlgorithm), Signature); } /// public override bool VerifyData(ReadOnlySpan Data, ReadOnlySpan Signature, HashAlgorithmName HashAlgorithm) { CheckDisposed(); return this.PublicKey.VerifyHash(HashDataIncremental(Data, HashAlgorithm), Signature); } /// protected override bool VerifyDataCore(ReadOnlySpan Data, ReadOnlySpan Signature, HashAlgorithmName HashAlgorithm, DSASignatureFormat SignatureFormat) { CheckDisposed(); return this.PublicKey.VerifyHash(HashDataIncremental(Data, HashAlgorithm), ConvertSignatureFromFormat(Signature, SignatureFormat)); } /// protected override bool VerifyDataCore(Stream Data, ReadOnlySpan Signature, HashAlgorithmName HashAlgorithm, DSASignatureFormat SignatureFormat) { CheckDisposed(); return this.PublicKey.VerifyHash(HashData(Data, HashAlgorithm), ConvertSignatureFromFormat(Signature, SignatureFormat)); } /// /// Exports the public key parameters for the current elliptic curve cryptography (ECC) key. /// /// This method only supports exporting public key parameters. Attempting to export /// private key parameters will result in an exception. /// true to include private parameters; otherwise, false. Private parameters are not supported by this provider. /// An ECParameters structure that contains the public key parameters. /// Thrown if IncludePrivateParameters is set to true, as exporting private key parameters is not supported by /// this provider. public override ECParameters ExportParameters(bool IncludePrivateParameters) { CheckDisposed(); if (IncludePrivateParameters) { throw new NotSupportedException("Private keys cannot be exported by this provider"); } return this.PublicKey.ExportParameters(false); } /// /// Exports the explicit parameters for the elliptic curve public key associated with this instance. /// /// true to include private parameters; otherwise, false. Private parameters are not supported by this provider. /// An ECParameters structure that contains the explicit parameters for the public key. /// Thrown if IncludePrivateParameters is set to true, as exporting private key parameters is not supported by /// this provider. public override ECParameters ExportExplicitParameters(bool IncludePrivateParameters) { CheckDisposed(); if (IncludePrivateParameters) { throw new NotSupportedException("Private keys cannot be exported by this provider"); } return this.PublicKey.ExportExplicitParameters(false); } /// /// Importing parameters is not supported. /// public override void ImportParameters(ECParameters Parameters) => throw new NotSupportedException(); /// public override byte[] ExportSubjectPublicKeyInfo() { CheckDisposed(); return this.PublicKey.ExportSubjectPublicKeyInfo(); } /// public override byte[] ExportPkcs8PrivateKey() { throw new NotSupportedException("Private keys cannot be exported by this provider"); } /// public override byte[] ExportEncryptedPkcs8PrivateKey(ReadOnlySpan PasswordBytes, PbeParameters PbeParameters) { throw new NotSupportedException("Private keys cannot be exported by this provider"); } /// public override byte[] ExportEncryptedPkcs8PrivateKey(ReadOnlySpan Password, PbeParameters PbeParameters) { throw new NotSupportedException("Private keys cannot be exported by this provider"); } /// /// Key generation is not supported. /// public override void GenerateKey(ECCurve Curve) => throw new NotSupportedException(); /// public override string ToXmlString(bool IncludePrivateParameters) { CheckDisposed(); if (IncludePrivateParameters) { throw new NotSupportedException("Private keys cannot be exported by this provider"); } return this.PublicKey.ToXmlString(false); } /// /// Importing parameters from XML is not supported. public override void FromXmlString(string XmlString) => throw new NotSupportedException(); private byte[] ConvertSignatureToFormat(byte[] IeeeP1363Signature, DSASignatureFormat SignatureFormat) { return SignatureFormat switch { DSASignatureFormat.IeeeP1363FixedFieldConcatenation => IeeeP1363Signature, DSASignatureFormat.Rfc3279DerSequence => ConvertIeeeP1363ToDer(IeeeP1363Signature), _ => throw new ArgumentOutOfRangeException(nameof(SignatureFormat)) }; } private byte[] ConvertSignatureFromFormat(ReadOnlySpan Signature, DSASignatureFormat SignatureFormat) { return SignatureFormat switch { DSASignatureFormat.IeeeP1363FixedFieldConcatenation => Signature.ToArray(), DSASignatureFormat.Rfc3279DerSequence => ConvertDerToIeeeP1363(Signature), _ => throw new ArgumentOutOfRangeException(nameof(SignatureFormat)) }; } private byte[] ConvertDerToIeeeP1363(ReadOnlySpan Signature) { int offset = 0; if (ReadByte(Signature, ref offset) != 0x30) throw new System.Security.Cryptography.CryptographicException("Invalid ECDSA signature."); int sequenceLength = ReadDerLength(Signature, ref offset); int sequenceEnd = offset + sequenceLength; if (sequenceEnd != Signature.Length) throw new System.Security.Cryptography.CryptographicException("Invalid ECDSA signature."); ReadOnlySpan r = ReadDerInteger(Signature, ref offset); ReadOnlySpan s = ReadDerInteger(Signature, ref offset); if (offset != sequenceEnd) throw new System.Security.Cryptography.CryptographicException("Invalid ECDSA signature."); int fieldSize = GetFieldSizeBytes(); byte[] signature = new byte[fieldSize * 2]; CopyDerIntegerToFixedField(r, signature.AsSpan(0, fieldSize)); CopyDerIntegerToFixedField(s, signature.AsSpan(fieldSize, fieldSize)); return signature; } private byte[] ConvertIeeeP1363ToDer(ReadOnlySpan Signature) { int fieldSize = GetFieldSizeBytes(); if (Signature.Length != fieldSize * 2) throw new System.Security.Cryptography.CryptographicException("Invalid ECDSA signature."); ReadOnlySpan r = Signature.Slice(0, fieldSize); ReadOnlySpan s = Signature.Slice(fieldSize, fieldSize); int rLength = GetDerIntegerLength(r); int sLength = GetDerIntegerLength(s); int sequenceContentLength = 2 + rLength + 2 + sLength; int sequenceLengthLength = GetDerLengthLength(sequenceContentLength); byte[] signature = new byte[1 + sequenceLengthLength + sequenceContentLength]; int offset = 0; signature[offset++] = 0x30; WriteDerLength(signature, ref offset, sequenceContentLength); WriteDerInteger(signature, ref offset, r, rLength); WriteDerInteger(signature, ref offset, s, sLength); return signature; } private int GetFieldSizeBytes() { return (this.KeySize + 7) / 8; } private static byte ReadByte(ReadOnlySpan Data, ref int Offset) { if (Offset >= Data.Length) throw new System.Security.Cryptography.CryptographicException("Invalid ECDSA signature."); return Data[Offset++]; } private static int ReadDerLength(ReadOnlySpan Data, ref int Offset) { byte firstLengthByte = ReadByte(Data, ref Offset); if ((firstLengthByte & 0x80) == 0) return firstLengthByte; int lengthByteCount = firstLengthByte & 0x7f; if (lengthByteCount == 0 || lengthByteCount > sizeof(int)) throw new System.Security.Cryptography.CryptographicException("Invalid ECDSA signature."); int length = 0; for (int i = 0; i < lengthByteCount; i++) { length = (length << 8) | ReadByte(Data, ref Offset); } return length; } private static ReadOnlySpan ReadDerInteger(ReadOnlySpan Data, ref int Offset) { if (ReadByte(Data, ref Offset) != 0x02) throw new System.Security.Cryptography.CryptographicException("Invalid ECDSA signature."); int length = ReadDerLength(Data, ref Offset); if (length <= 0 || Offset + length > Data.Length) throw new System.Security.Cryptography.CryptographicException("Invalid ECDSA signature."); ReadOnlySpan value = Data.Slice(Offset, length); Offset += length; return value; } private static void CopyDerIntegerToFixedField(ReadOnlySpan Source, Span Destination) { while (Source.Length > 1 && Source[0] == 0) { Source = Source.Slice(1); } if (Source.Length > Destination.Length) throw new System.Security.Cryptography.CryptographicException("Invalid ECDSA signature."); Source.CopyTo(Destination.Slice(Destination.Length - Source.Length)); } private static int GetDerIntegerLength(ReadOnlySpan Value) { Value = TrimLeadingZeroes(Value); if (Value.Length == 0) return 1; if ((Value[0] & 0x80) != 0) return Value.Length + 1; return Value.Length; } private static int GetDerLengthLength(int Length) { if (Length < 0x80) return 1; if (Length <= 0xff) return 2; if (Length <= 0xffff) return 3; return 4; } private static void WriteDerLength(Span Destination, ref int Offset, int Length) { if (Length < 0x80) { Destination[Offset++] = (byte)Length; } else if (Length <= 0xff) { Destination[Offset++] = 0x81; Destination[Offset++] = (byte)Length; } else if (Length <= 0xffff) { Destination[Offset++] = 0x82; Destination[Offset++] = (byte)(Length >> 8); Destination[Offset++] = (byte)Length; } else { Destination[Offset++] = 0x83; Destination[Offset++] = (byte)(Length >> 16); Destination[Offset++] = (byte)(Length >> 8); Destination[Offset++] = (byte)Length; } } private static void WriteDerInteger(Span Destination, ref int Offset, ReadOnlySpan Value, int Length) { Value = TrimLeadingZeroes(Value); Destination[Offset++] = 0x02; WriteDerLength(Destination, ref Offset, Length); if (Value.Length == 0) { Destination[Offset++] = 0; return; } if ((Value[0] & 0x80) != 0) { Destination[Offset++] = 0; } Value.CopyTo(Destination.Slice(Offset)); Offset += Value.Length; } private static ReadOnlySpan TrimLeadingZeroes(ReadOnlySpan Value) { while (Value.Length != 0 && Value[0] == 0) { Value = Value.Slice(1); } return Value; } /// /// Validates that the specified key size and digest size combination is supported. /// /// Supported combinations are: 256 bits with 32 bytes, 384 bits with 48 bytes, and 521 /// bits with 64 bytes. /// The size of the cryptographic key, in bits. Supported values are 256, 384, or 521. /// The size of the digest, in bytes. Supported values are 32, 48, or 64, corresponding to the key size. /// Thrown if the combination of key size and digest size is not supported. private static void ValidateKeyDigestCombination(int KeySizeBits, int DigestSizeBytes) { if ((KeySizeBits == 256 && DigestSizeBytes == 32) || (KeySizeBits == 384 && DigestSizeBytes == 48) || (KeySizeBits == 521 && DigestSizeBytes == 64)) { return; } throw new NotSupportedException($"The key size '{KeySizeBits}' is not valid for digest of size '{DigestSizeBytes}' bytes."); } /// /// Validates that the specified key size and hash algorithm combination is supported for cryptographic /// operations. /// /// This method ensures that only valid key size and hash algorithm pairs are used, /// preventing unsupported or insecure configurations. /// The size of the cryptographic key, in bits. Supported values are 256, 384, or 521. /// The hash algorithm to validate for use with the specified key size. Supported values are SHA256 for 256 /// bits, SHA384 for 384 bits, and SHA512 for 521 bits. /// Thrown when the specified key size and hash algorithm combination is not supported. private static void ValidateKeyDigestCombination(int KeySizeBits, HashAlgorithmName HashAlgorithmName) { switch (KeySizeBits) { case 256 when HashAlgorithmName == HashAlgorithmName.SHA256: case 384 when HashAlgorithmName == HashAlgorithmName.SHA384: case 521 when HashAlgorithmName == HashAlgorithmName.SHA512: return; } throw new NotSupportedException($"The key size '{KeySizeBits}' is not valid for digest algorithm '{HashAlgorithmName}'."); } /// /// Throws an exception if the current object has been disposed. /// /// Call this method before performing operations that require the object to be in a /// valid, non-disposed state. This helps ensure that methods are not called on an object that has already been /// disposed, which could lead to undefined behavior. private void CheckDisposed() { ObjectDisposedException.ThrowIf(this.Disposed, this); } /// /// Releases the unmanaged resources used by the object and optionally releases the managed resources. /// /// This method is called by the public Dispose() method and the finalizer. When /// Disposing is true, this method disposes of managed resources in addition to unmanaged resources. Override /// this method to release resources specific to the derived class. /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected override void Dispose(bool Disposing) { if (this.Disposed) return; if (Disposing) { this.PublicKey?.Dispose(); this.PublicKey = null; } this.Disposed = true; base.Dispose(Disposing); } } }