Skip to content

Commit

Permalink
Use Azure SDK for RSA algorithm (vcsjones#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
vcsjones authored May 12, 2024
2 parents 64b8ec3 + 72ea486 commit e6e4046
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
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

0 comments on commit e6e4046

Please sign in to comment.