/* * Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved. * * This file is part of System Informer. * * Authors: * * dmex * */ namespace CustomBuildTool { /// /// RSA implementation that uses Azure Key Vault /// public sealed class RSAKeyVault : RSA { private readonly KeyVaultContext VaultContext; private RSA PublicKey; private bool Disposed; /// /// Creates a new RSAKeyVault instance /// /// Context with parameters public RSAKeyVault(KeyVaultContext Context) { if (!Context.IsValid) throw new ArgumentException("Must not be the default", nameof(Context)); this.VaultContext = Context; // // Build public key from certificate if available // if (Context.Certificate != null) { this.PublicKey = Context.Certificate.GetRSAPublicKey(); ArgumentNullException.ThrowIfNull(this.PublicKey); } 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 "RSA"; } } /// 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 "RSA"; } } /// public override byte[] SignHash(byte[] Hash, HashAlgorithmName HashAlgorithm, RSASignaturePadding Padding) { CheckDisposed(); // // Key Vault only supports PKCSv1 padding // if (Padding.Mode != RSASignaturePaddingMode.Pkcs1) { throw new NotSupportedException("Unsupported padding mode"); } return this.VaultContext.SignDigest(Hash, HashAlgorithm, KeyVaultSignatureAlgorithm.RSAPkcs15); } /// public override bool TrySignHash(ReadOnlySpan Hash, Span Destination, HashAlgorithmName HashAlgorithm, RSASignaturePadding Padding, out int BytesWritten) { CheckDisposed(); byte[] signature = SignHash(Hash.ToArray(), HashAlgorithm, Padding); if (signature.Length > Destination.Length) { BytesWritten = 0; return false; } signature.CopyTo(Destination); BytesWritten = signature.Length; return true; } /// public override byte[] SignData(byte[] Data, int Offset, int Count, HashAlgorithmName HashAlgorithm, RSASignaturePadding Padding) { CheckDisposed(); return SignHash(HashData(Data, Offset, Count, HashAlgorithm), HashAlgorithm, Padding); } /// public override byte[] SignData(Stream Data, HashAlgorithmName HashAlgorithm, RSASignaturePadding Padding) { CheckDisposed(); return SignHash(HashData(Data, HashAlgorithm), HashAlgorithm, Padding); } /// public override bool TrySignData(ReadOnlySpan Data, Span Destination, HashAlgorithmName HashAlgorithm, RSASignaturePadding Padding, out int BytesWritten) { CheckDisposed(); byte[] hash = HashData(Data, HashAlgorithm); return TrySignHash(hash, Destination, HashAlgorithm, Padding, out BytesWritten); } /// public override bool VerifyHash(byte[] Hash, byte[] Signature, HashAlgorithmName HashAlgorithm, RSASignaturePadding Padding) { CheckDisposed(); return this.PublicKey.VerifyHash(Hash, Signature, HashAlgorithm, Padding); } /// public override bool VerifyHash(ReadOnlySpan Hash, ReadOnlySpan Signature, HashAlgorithmName HashAlgorithm, RSASignaturePadding Padding) { CheckDisposed(); return this.PublicKey.VerifyHash(Hash, Signature, HashAlgorithm, Padding); } /// public override bool VerifyData(byte[] Data, int Offset, int Count, byte[] Signature, HashAlgorithmName HashAlgorithm, RSASignaturePadding Padding) { CheckDisposed(); return this.PublicKey.VerifyHash(HashData(Data, Offset, Count, HashAlgorithm), Signature, HashAlgorithm, Padding); } /// public override bool VerifyData(ReadOnlySpan Data, ReadOnlySpan Signature, HashAlgorithmName HashAlgorithm, RSASignaturePadding Padding) { CheckDisposed(); byte[] hash = HashData(Data, HashAlgorithm); return this.PublicKey.VerifyHash(hash, Signature, HashAlgorithm, Padding); } /// protected override byte[] HashData(byte[] Data, int Offset, int Count, HashAlgorithmName HashAlgorithm) { CheckDisposed(); using var digestAlgorithm = Create(HashAlgorithm); return digestAlgorithm.ComputeHash(Data, Offset, Count); } /// protected override byte[] HashData(Stream Data, HashAlgorithmName HashAlgorithm) { CheckDisposed(); using (var digestAlgorithm = Create(HashAlgorithm)) { return digestAlgorithm.ComputeHash(Data); } } /// protected override bool TryHashData(ReadOnlySpan Data, Span Destination, HashAlgorithmName HashAlgorithm, out int BytesWritten) { CheckDisposed(); byte[] hash = HashData(Data, HashAlgorithm); if (hash.Length > Destination.Length) { BytesWritten = 0; return false; } hash.CopyTo(Destination); BytesWritten = hash.Length; return true; } /// public override byte[] Decrypt(byte[] Data, RSAEncryptionPadding Padding) { CheckDisposed(); return this.VaultContext.DecryptData(Data, Padding); } /// public override bool TryDecrypt(ReadOnlySpan Data, Span Destination, RSAEncryptionPadding Padding, out int BytesWritten) { CheckDisposed(); byte[] decrypted = Decrypt(Data.ToArray(), Padding); if (decrypted.Length > Destination.Length) { BytesWritten = 0; return false; } decrypted.CopyTo(Destination); BytesWritten = decrypted.Length; return true; } /// public override byte[] Encrypt(byte[] Data, RSAEncryptionPadding Padding) { CheckDisposed(); return this.PublicKey.Encrypt(Data, Padding); } /// public override bool TryEncrypt(ReadOnlySpan Data, Span Destination, RSAEncryptionPadding Padding, out int BytesWritten) { CheckDisposed(); return this.PublicKey.TryEncrypt(Data, Destination, Padding, out BytesWritten); } /// [Obsolete("This method is obsolete. Use Decrypt instead.")] public override byte[] DecryptValue(byte[] Rgb) { throw new NotSupportedException(); } /// [Obsolete("This method is obsolete. Use Encrypt instead.")] public override byte[] EncryptValue(byte[] Rgb) { throw new NotSupportedException(); } /// public override RSAParameters ExportParameters(bool IncludePrivateParameters) { CheckDisposed(); if (IncludePrivateParameters) { throw new NotSupportedException("Private keys cannot be exported by this provider"); } return this.PublicKey.ExportParameters(false); } /// public override void ImportParameters(RSAParameters Parameters) { throw new NotSupportedException(); } /// public override byte[] ExportRSAPublicKey() { CheckDisposed(); return this.PublicKey.ExportRSAPublicKey(); } /// public override bool TryExportRSAPublicKey(Span Destination, out int BytesWritten) { CheckDisposed(); return this.PublicKey.TryExportRSAPublicKey(Destination, out BytesWritten); } /// public override byte[] ExportSubjectPublicKeyInfo() { CheckDisposed(); return this.PublicKey.ExportSubjectPublicKeyInfo(); } /// public override bool TryExportSubjectPublicKeyInfo(Span Destination, out int BytesWritten) { CheckDisposed(); return this.PublicKey.TryExportSubjectPublicKeyInfo(Destination, out BytesWritten); } /// public override byte[] ExportRSAPrivateKey() { throw new NotSupportedException("Private keys cannot be exported by this provider"); } /// public override bool TryExportRSAPrivateKey(Span Destination, out int BytesWritten) { BytesWritten = 0; throw new NotSupportedException("Private keys cannot be exported by this provider"); } /// public override byte[] ExportPkcs8PrivateKey() { throw new NotSupportedException("Private keys cannot be exported by this provider"); } /// public override bool TryExportPkcs8PrivateKey(Span Destination, out int BytesWritten) { BytesWritten = 0; 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"); } /// public override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan PasswordBytes, PbeParameters PbeParameters, Span Destination, out int BytesWritten) { BytesWritten = 0; throw new NotSupportedException("Private keys cannot be exported by this provider"); } /// public override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan Password, PbeParameters PbeParameters, Span Destination, out int BytesWritten) { BytesWritten = 0; throw new NotSupportedException("Private keys cannot be exported by this provider"); } /// public override void ImportRSAPrivateKey(ReadOnlySpan Source, out int BytesRead) { BytesRead = 0; throw new NotSupportedException(); } /// public override void ImportPkcs8PrivateKey(ReadOnlySpan Source, out int BytesRead) { BytesRead = 0; throw new NotSupportedException(); } /// public override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan PasswordBytes, ReadOnlySpan Source, out int BytesRead) { BytesRead = 0; throw new NotSupportedException(); } /// public override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan Password, ReadOnlySpan Source, out int BytesRead) { BytesRead = 0; throw new NotSupportedException(); } /// public override void ImportRSAPublicKey(ReadOnlySpan Source, out int BytesRead) { BytesRead = 0; throw new NotSupportedException(); } /// public override void ImportSubjectPublicKeyInfo(ReadOnlySpan Source, out int BytesRead) { BytesRead = 0; throw new NotSupportedException(); } /// public override void ImportFromPem(ReadOnlySpan Input) { throw new NotSupportedException(); } /// public override void ImportFromEncryptedPem(ReadOnlySpan Input, ReadOnlySpan PasswordBytes) { throw new NotSupportedException(); } /// public override void ImportFromEncryptedPem(ReadOnlySpan Input, ReadOnlySpan Password) { 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); } /// public override void FromXmlString(string XmlString) { throw new NotSupportedException(); } private void CheckDisposed() { ObjectDisposedException.ThrowIf(this.Disposed, this); } ~RSAKeyVault() { Dispose(false); } public new void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// protected override void Dispose(bool Disposing) { if (this.Disposed) return; if (Disposing) { this.PublicKey?.Dispose(); this.PublicKey = null; } this.Disposed = true; base.Dispose(Disposing); } private static HashAlgorithm Create(HashAlgorithmName Algorithm) { return Algorithm.Name switch { nameof(HashAlgorithmName.SHA1) => SHA1.Create(), nameof(HashAlgorithmName.SHA256) => SHA256.Create(), nameof(HashAlgorithmName.SHA384) => SHA384.Create(), nameof(HashAlgorithmName.SHA512) => SHA512.Create(), _ => throw new NotSupportedException("The specified algorithm is not supported.") }; } private static byte[] HashData(ReadOnlySpan Data, HashAlgorithmName Algorithm) { using (var digestAlgorithm = IncrementalHash.CreateHash(Algorithm)) { digestAlgorithm.AppendData(Data); return digestAlgorithm.GetHashAndReset(); } } } }