Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Azure SDK for RSA algorithm #251

Merged
merged 2 commits into from
May 12, 2024
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
1 change: 0 additions & 1 deletion src/AzureSignTool/AzureSignTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="RSAKeyVaultProvider" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
26 changes: 24 additions & 2 deletions src/AzureSignTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Security.KeyVault.Keys.Cryptography;
using AzureSign.Core;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.Extensions.FileSystemGlobbing.Abstractions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using RSAKeyVaultProvider;
using XenoAtom.CommandLine;

using static AzureSignTool.HRESULT;
Expand Down Expand Up @@ -248,6 +249,7 @@ private async ValueTask<int> RunSign()
var configurationDiscoverer = new KeyVaultConfigurationDiscoverer(logger);
var materializedResult = await configurationDiscoverer.Materialize(configuration);
AzureKeyVaultMaterializedConfiguration materialized;

switch (materializedResult)
{
case ErrorOr<AzureKeyVaultMaterializedConfiguration>.Ok ok:
Expand All @@ -257,6 +259,14 @@ private async ValueTask<int> RunSign()
logger.LogError("Failed to get configuration from Azure Key Vault.");
return E_INVALIDARG;
}

const string RsaOid = "1.2.840.113549.1.1.1";
if (materialized.PublicCertificate.GetKeyAlgorithm() is string alg and not RsaOid)
{
logger.LogError("Certificate algorithm is not RSA.");
return E_INVALIDARG;
}

int failed = 0, succeeded = 0;
var cancellationSource = new CancellationTokenSource();
Console.CancelKeyPress += (_, e) =>
Expand All @@ -272,7 +282,19 @@ private async ValueTask<int> RunSign()
}
logger.LogTrace("Creating context");

using (var keyVault = RSAFactory.Create(materialized.TokenCredential, materialized.KeyId, materialized.PublicCertificate))
CryptographyClientOptions clientOptions = new() {
Retry =
{
Delay = TimeSpan.FromSeconds(2),
MaxDelay = TimeSpan.FromSeconds(16),
MaxRetries = 5,
Mode = RetryMode.Exponential
}
};

var client = new CryptographyClient(materialized.KeyId, materialized.TokenCredential, clientOptions);

using (var keyVault = await client.CreateRSAAsync())
using (var signer = new AuthenticodeKeyVaultSigner(keyVault, materialized.PublicCertificate, ParseHashAlgorithm(FileDigestAlgorithm), timeStampConfiguration, certificates))
{
Parallel.ForEach(AllFiles, options, () => (succeeded: 0, failed: 0), (filePath, pls, state) =>
Expand Down