Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Sign.SignatureProviders.KeyVault/KeyVaultService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ public async Task<X509Certificate2> GetCertificateAsync(CancellationToken cancel

public async Task<RSA> GetRsaAsync(CancellationToken cancellationToken)
{
return await _cryptographyClient.CreateRSAAsync(cancellationToken);
using X509Certificate2 certificate = await GetCertificateAsync(cancellationToken);
RSAKeyVault rsaKeyVault = await _cryptographyClient.CreateRSAAsync(cancellationToken);
RSA rsaPublicKey = certificate.GetRSAPrivateKey()!;
return new RSAKeyVaultWrapper(rsaKeyVault, rsaPublicKey);
}
}
}
52 changes: 52 additions & 0 deletions src/Sign.SignatureProviders.KeyVault/RSAKeyVaultWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE.txt file in the project root for more information.

using System.Security.Cryptography;
using Azure.Security.KeyVault.Keys.Cryptography;

namespace Sign.SignatureProviders.KeyVault
{
internal sealed class RSAKeyVaultWrapper : RSA
{
private readonly RSAKeyVault _rsaKeyVault;
private readonly RSA _rsaPublicKey;

public RSAKeyVaultWrapper(RSAKeyVault rsaKeyVault, RSA publicKey)
{
_rsaKeyVault = rsaKeyVault;
_rsaPublicKey = publicKey;
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
_rsaKeyVault.Dispose();
_rsaPublicKey.Dispose();
}

base.Dispose(disposing);
}


public override RSAParameters ExportParameters(bool includePrivateParameters)
{
if (includePrivateParameters)
{
throw new NotSupportedException();
}

return _rsaPublicKey.ExportParameters(false);
}

public override void ImportParameters(RSAParameters parameters)
=> throw new NotImplementedException();

public override byte[] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
=> _rsaKeyVault.SignHash(hash, hashAlgorithm, padding);

public override bool VerifyHash(byte[] hash, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
=> _rsaPublicKey.VerifyHash(hash, signature, hashAlgorithm, padding);
}
}