This commit is contained in:
StellaOps Bot
2025-11-27 21:10:06 +02:00
parent cfa2274d31
commit 8abbf9574d
106 changed files with 7078 additions and 3197 deletions

View File

@@ -0,0 +1,53 @@
#if STELLAOPS_PKCS11
using System;
using System.IO;
using System.Security.Cryptography;
using Microsoft.Extensions.Options;
using StellaOps.Cryptography;
using StellaOps.Cryptography.Plugin.Pkcs11Gost;
using Xunit;
namespace StellaOps.Cryptography.Tests;
public class Pkcs11GostProviderTests
{
[Fact]
public void DescribeKeys_ExposesLibraryPathAndThumbprint()
{
if (!string.Equals(Environment.GetEnvironmentVariable("STELLAOPS_PKCS11_ENABLED"), "1", StringComparison.Ordinal))
{
return; // opt-in only when PKCS#11 libs/slots are available
}
using var ecdsa = ECDsa.Create(ECCurve.NamedCurves.nistP256);
var req = new CertificateRequest("CN=pkcs11.test", ecdsa, HashAlgorithmName.SHA256);
var cert = req.CreateSelfSigned(DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddDays(1));
var certPath = Path.Combine(Path.GetTempPath(), $"pkcs11-{Guid.NewGuid():N}.cer");
File.WriteAllBytes(certPath, cert.Export(X509ContentType.Cert));
var options = new Pkcs11GostProviderOptions();
options.Keys.Add(new Pkcs11GostKeyOptions
{
KeyId = "test-key",
Algorithm = SignatureAlgorithms.GostR3410_2012_256,
LibraryPath = "/tmp/libpkcs11-placeholder.so",
PrivateKeyLabel = "priv",
PublicKeyLabel = "pub",
CertificatePath = certPath,
SignMechanismId = Pkcs11Mechanisms.DefaultGost12_256Signature
});
var provider = new Pkcs11GostCryptoProvider(Options.Create(options));
Assert.True(provider.Supports(CryptoCapability.Signing, SignatureAlgorithms.GostR3410_2012_256));
var descriptor = Assert.Single(provider.DescribeKeys());
Assert.Equal("test-key", descriptor.KeyId);
Assert.Equal("/tmp/libpkcs11-placeholder.so", descriptor.Metadata["library"]);
Assert.Equal(cert.Thumbprint, descriptor.Metadata["thumbprint"], ignoreCase: true);
Assert.Equal("priv", descriptor.Metadata["privateKeyLabel"]);
}
}
#endif

View File

@@ -9,6 +9,12 @@
<PropertyGroup Condition="'$(StellaOpsCryptoSodium)' == 'true'">
<DefineConstants>$(DefineConstants);STELLAOPS_CRYPTO_SODIUM</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(StellaOpsEnableCryptoPro)' == 'true'">
<DefineConstants>$(DefineConstants);STELLAOPS_CRYPTO_PRO</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(StellaOpsEnablePkcs11)' == 'true'">
<DefineConstants>$(DefineConstants);STELLAOPS_PKCS11</DefineConstants>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../../StellaOps.Cryptography/StellaOps.Cryptography.csproj" />
<ProjectReference Include="../../StellaOps.Cryptography.DependencyInjection/StellaOps.Cryptography.DependencyInjection.csproj" />
@@ -18,4 +24,7 @@
<ItemGroup Condition="'$(StellaOpsEnableCryptoPro)' == 'true'">
<ProjectReference Include="../../StellaOps.Cryptography.Plugin.CryptoPro/StellaOps.Cryptography.Plugin.CryptoPro.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(StellaOpsEnablePkcs11)' == 'true'">
<ProjectReference Include="../../StellaOps.Cryptography.Plugin.Pkcs11Gost/StellaOps.Cryptography.Plugin.Pkcs11Gost.csproj" />
</ItemGroup>
</Project>