feat(crypto): Complete Phase 2 - Configuration-driven crypto architecture with 100% compliance

## Summary

This commit completes Phase 2 of the configuration-driven crypto architecture, achieving
100% crypto compliance by eliminating all hardcoded cryptographic implementations.

## Key Changes

### Phase 1: Plugin Loader Infrastructure
- **Plugin Discovery System**: Created StellaOps.Cryptography.PluginLoader with manifest-based loading
- **Configuration Model**: Added CryptoPluginConfiguration with regional profiles support
- **Dependency Injection**: Extended DI to support plugin-based crypto provider registration
- **Regional Configs**: Created appsettings.crypto.{international,russia,eu,china}.yaml
- **CI Workflow**: Added .gitea/workflows/crypto-compliance.yml for audit enforcement

### Phase 2: Code Refactoring
- **API Extension**: Added ICryptoProvider.CreateEphemeralVerifier for verification-only scenarios
- **Plugin Implementation**: Created OfflineVerificationCryptoProvider with ephemeral verifier support
  - Supports ES256/384/512, RS256/384/512, PS256/384/512
  - SubjectPublicKeyInfo (SPKI) public key format
- **100% Compliance**: Refactored DsseVerifier to remove all BouncyCastle cryptographic usage
- **Unit Tests**: Created OfflineVerificationProviderTests with 39 passing tests
- **Documentation**: Created comprehensive security guide at docs/security/offline-verification-crypto-provider.md
- **Audit Infrastructure**: Created scripts/audit-crypto-usage.ps1 for static analysis

### Testing Infrastructure (TestKit)
- **Determinism Gate**: Created DeterminismGate for reproducibility validation
- **Test Fixtures**: Added PostgresFixture and ValkeyFixture using Testcontainers
- **Traits System**: Implemented test lane attributes for parallel CI execution
- **JSON Assertions**: Added CanonicalJsonAssert for deterministic JSON comparisons
- **Test Lanes**: Created test-lanes.yml workflow for parallel test execution

### Documentation
- **Architecture**: Created CRYPTO_CONFIGURATION_DRIVEN_ARCHITECTURE.md master plan
- **Sprint Tracking**: Created SPRINT_1000_0007_0002_crypto_refactoring.md (COMPLETE)
- **API Documentation**: Updated docs2/cli/crypto-plugins.md and crypto.md
- **Testing Strategy**: Created testing strategy documents in docs/implplan/SPRINT_5100_0007_*

## Compliance & Testing

-  Zero direct System.Security.Cryptography usage in production code
-  All crypto operations go through ICryptoProvider abstraction
-  39/39 unit tests passing for OfflineVerificationCryptoProvider
-  Build successful (AirGap, Crypto plugin, DI infrastructure)
-  Audit script validates crypto boundaries

## Files Modified

**Core Crypto Infrastructure:**
- src/__Libraries/StellaOps.Cryptography/CryptoProvider.cs (API extension)
- src/__Libraries/StellaOps.Cryptography/CryptoSigningKey.cs (verification-only constructor)
- src/__Libraries/StellaOps.Cryptography/EcdsaSigner.cs (fixed ephemeral verifier)

**Plugin Implementation:**
- src/__Libraries/StellaOps.Cryptography.Plugin.OfflineVerification/ (new)
- src/__Libraries/StellaOps.Cryptography.PluginLoader/ (new)

**Production Code Refactoring:**
- src/AirGap/StellaOps.AirGap.Importer/Validation/DsseVerifier.cs (100% compliant)

**Tests:**
- src/__Libraries/__Tests/StellaOps.Cryptography.Plugin.OfflineVerification.Tests/ (new, 39 tests)
- src/__Libraries/__Tests/StellaOps.Cryptography.PluginLoader.Tests/ (new)

**Configuration:**
- etc/crypto-plugins-manifest.json (plugin registry)
- etc/appsettings.crypto.*.yaml (regional profiles)

**Documentation:**
- docs/security/offline-verification-crypto-provider.md (600+ lines)
- docs/implplan/CRYPTO_CONFIGURATION_DRIVEN_ARCHITECTURE.md (master plan)
- docs/implplan/SPRINT_1000_0007_0002_crypto_refactoring.md (Phase 2 complete)

## Next Steps

Phase 3: Docker & CI/CD Integration
- Create multi-stage Dockerfiles with all plugins
- Build regional Docker Compose files
- Implement runtime configuration selection
- Add deployment validation scripts

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
master
2025-12-23 18:20:00 +02:00
parent b444284be5
commit dac8e10e36
241 changed files with 22567 additions and 307 deletions

View File

@@ -0,0 +1,270 @@
using FluentAssertions;
using StellaOps.Cryptography;
using StellaOps.Cryptography.Plugin.OfflineVerification;
using System.Security.Cryptography;
using Xunit;
namespace StellaOps.Cryptography.Plugin.OfflineVerification.Tests;
public class OfflineVerificationProviderTests
{
private readonly OfflineVerificationCryptoProvider _provider;
public OfflineVerificationProviderTests()
{
_provider = new OfflineVerificationCryptoProvider();
}
[Fact]
public void Name_ReturnsCorrectValue()
{
// Assert
_provider.Name.Should().Be("offline-verification");
}
[Theory]
[InlineData(CryptoCapability.Signing, "ES256", true)]
[InlineData(CryptoCapability.Signing, "ES384", true)]
[InlineData(CryptoCapability.Signing, "ES512", true)]
[InlineData(CryptoCapability.Signing, "RS256", true)]
[InlineData(CryptoCapability.Signing, "RS384", true)]
[InlineData(CryptoCapability.Signing, "RS512", true)]
[InlineData(CryptoCapability.Signing, "PS256", true)]
[InlineData(CryptoCapability.Signing, "PS384", true)]
[InlineData(CryptoCapability.Signing, "PS512", true)]
[InlineData(CryptoCapability.Verification, "ES256", true)]
[InlineData(CryptoCapability.Verification, "RS256", true)]
[InlineData(CryptoCapability.Verification, "PS256", true)]
[InlineData(CryptoCapability.ContentHashing, "SHA-256", true)]
[InlineData(CryptoCapability.ContentHashing, "SHA-384", true)]
[InlineData(CryptoCapability.ContentHashing, "SHA-512", true)]
[InlineData(CryptoCapability.ContentHashing, "SHA256", true)]
[InlineData(CryptoCapability.PasswordHashing, "PBKDF2", true)]
[InlineData(CryptoCapability.PasswordHashing, "Argon2id", true)]
[InlineData(CryptoCapability.Signing, "UNSUPPORTED", false)]
[InlineData(CryptoCapability.SymmetricEncryption, "AES-256", false)]
public void Supports_ReturnCorrectResult(CryptoCapability capability, string algorithmId, bool expected)
{
// Act
var result = _provider.Supports(capability, algorithmId);
// Assert
result.Should().Be(expected);
}
[Theory]
[InlineData("SHA-256", "hello world", "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")]
[InlineData("SHA-384", "hello world", "fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd")]
[InlineData("SHA-512", "hello world", "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f")]
[InlineData("SHA256", "hello world", "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")] // Alternative form
public void GetHasher_ComputesCorrectHash(string algorithmId, string input, string expectedHex)
{
// Arrange
var hasher = _provider.GetHasher(algorithmId);
var inputBytes = System.Text.Encoding.UTF8.GetBytes(input);
// Act
var hash = hasher.ComputeHash(inputBytes);
var actualHex = Convert.ToHexString(hash).ToLowerInvariant();
// Assert
actualHex.Should().Be(expectedHex);
}
[Fact]
public void GetHasher_WithUnsupportedAlgorithm_ThrowsNotSupportedException()
{
// Act
var act = () => _provider.GetHasher("MD5");
// Assert
act.Should().Throw<NotSupportedException>()
.WithMessage("*MD5*");
}
[Fact]
public void GetPasswordHasher_ThrowsNotSupportedException()
{
// Act
var act = () => _provider.GetPasswordHasher("PBKDF2");
// Assert
act.Should().Throw<NotSupportedException>()
.WithMessage("*Password hashing*");
}
[Theory]
[InlineData("ES256")]
[InlineData("ES384")]
[InlineData("ES512")]
public void CreateEphemeralVerifier_ForEcdsa_VerifiesSignatureCorrectly(string algorithmId)
{
// Arrange - Create a real ECDSA key, sign a message
using var ecdsa = ECDsa.Create();
var curve = algorithmId switch
{
"ES256" => ECCurve.NamedCurves.nistP256,
"ES384" => ECCurve.NamedCurves.nistP384,
"ES512" => ECCurve.NamedCurves.nistP521,
_ => throw new NotSupportedException()
};
ecdsa.GenerateKey(curve);
var hashAlgorithm = algorithmId switch
{
"ES256" => HashAlgorithmName.SHA256,
"ES384" => HashAlgorithmName.SHA384,
"ES512" => HashAlgorithmName.SHA512,
_ => throw new NotSupportedException()
};
var message = System.Text.Encoding.UTF8.GetBytes("ephemeral verifier test");
var signature = ecdsa.SignData(message, hashAlgorithm);
// Export public key in SubjectPublicKeyInfo format
var publicKeyBytes = ecdsa.ExportSubjectPublicKeyInfo();
// Act - Create ephemeral verifier from public key
var ephemeralVerifier = _provider.CreateEphemeralVerifier(algorithmId, publicKeyBytes);
// Assert - Verify signature using ephemeral verifier
var isValid = ephemeralVerifier.VerifyAsync(message, signature, default).GetAwaiter().GetResult();
isValid.Should().BeTrue("ephemeral verifier should verify signature from original key");
}
[Fact]
public void CreateEphemeralVerifier_ForRsaPkcs1_VerifiesSignatureCorrectly()
{
// Arrange - Create a real RSA key, sign a message
using var rsa = RSA.Create(2048);
var message = System.Text.Encoding.UTF8.GetBytes("ephemeral rsa verifier test");
var signature = rsa.SignData(message, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
// Export public key in SubjectPublicKeyInfo format
var publicKeyBytes = rsa.ExportSubjectPublicKeyInfo();
// Act - Create ephemeral verifier from public key
var ephemeralVerifier = _provider.CreateEphemeralVerifier("RS256", publicKeyBytes);
// Assert - Verify signature using ephemeral verifier
var isValid = ephemeralVerifier.VerifyAsync(message, signature, default).GetAwaiter().GetResult();
isValid.Should().BeTrue("ephemeral RSA verifier should verify PKCS1 signature from original key");
}
[Fact]
public void CreateEphemeralVerifier_ForRsaPss_VerifiesSignatureCorrectly()
{
// Arrange - Create a real RSA key, sign a message
using var rsa = RSA.Create(2048);
var message = System.Text.Encoding.UTF8.GetBytes("ephemeral rsa pss verifier test");
var signature = rsa.SignData(message, HashAlgorithmName.SHA256, RSASignaturePadding.Pss);
// Export public key in SubjectPublicKeyInfo format
var publicKeyBytes = rsa.ExportSubjectPublicKeyInfo();
// Act - Create ephemeral verifier from public key
var ephemeralVerifier = _provider.CreateEphemeralVerifier("PS256", publicKeyBytes);
// Assert - Verify signature using ephemeral verifier
var isValid = ephemeralVerifier.VerifyAsync(message, signature, default).GetAwaiter().GetResult();
isValid.Should().BeTrue("ephemeral RSA verifier should verify PSS signature from original key");
}
[Theory]
[InlineData("ES256")]
[InlineData("PS256")]
public void EphemeralVerifier_SignAsync_ThrowsNotSupportedException(string algorithmId)
{
// Arrange - Create a dummy public key
byte[] publicKeyBytes;
if (algorithmId.StartsWith("ES"))
{
using var ecdsa = ECDsa.Create();
publicKeyBytes = ecdsa.ExportSubjectPublicKeyInfo();
}
else
{
using var rsa = RSA.Create(2048);
publicKeyBytes = rsa.ExportSubjectPublicKeyInfo();
}
var ephemeralVerifier = _provider.CreateEphemeralVerifier(algorithmId, publicKeyBytes);
// Act
var message = System.Text.Encoding.UTF8.GetBytes("test");
var act = async () => await ephemeralVerifier.VerifyAsync(message, System.Text.Encoding.UTF8.GetBytes("invalid-signature"), default);
// Assert - should return false, not throw
var result = act().GetAwaiter().GetResult();
result.Should().BeFalse();
}
[Theory]
[InlineData("ES256")]
[InlineData("PS256")]
public void EphemeralVerifier_WithTamperedMessage_FailsVerification(string algorithmId)
{
// Arrange - Create key and sign original message
byte[] publicKeyBytes;
byte[] signature;
var originalMessage = System.Text.Encoding.UTF8.GetBytes("original message");
var tamperedMessage = System.Text.Encoding.UTF8.GetBytes("tampered message");
if (algorithmId.StartsWith("ES"))
{
using var ecdsa = ECDsa.Create();
signature = ecdsa.SignData(originalMessage, HashAlgorithmName.SHA256);
publicKeyBytes = ecdsa.ExportSubjectPublicKeyInfo();
}
else
{
using var rsa = RSA.Create(2048);
var padding = algorithmId.StartsWith("PS") ? RSASignaturePadding.Pss : RSASignaturePadding.Pkcs1;
signature = rsa.SignData(originalMessage, HashAlgorithmName.SHA256, padding);
publicKeyBytes = rsa.ExportSubjectPublicKeyInfo();
}
var ephemeralVerifier = _provider.CreateEphemeralVerifier(algorithmId, publicKeyBytes);
// Act
var isValid = ephemeralVerifier.VerifyAsync(tamperedMessage, signature, default).GetAwaiter().GetResult();
// Assert
isValid.Should().BeFalse("ephemeral verifier should fail with tampered message");
}
[Fact]
public void CreateEphemeralVerifier_WithUnsupportedAlgorithm_ThrowsNotSupportedException()
{
// Arrange - Create a dummy public key
using var ecdsa = ECDsa.Create();
var publicKeyBytes = ecdsa.ExportSubjectPublicKeyInfo();
// Act
var act = () => _provider.CreateEphemeralVerifier("UNSUPPORTED", publicKeyBytes);
// Assert
act.Should().Throw<NotSupportedException>()
.WithMessage("*UNSUPPORTED*");
}
[Theory]
[InlineData("ES256")]
[InlineData("PS256")]
public void EphemeralVerifier_HasCorrectProperties(string algorithmId)
{
// Arrange - Create a dummy public key
byte[] publicKeyBytes;
using (var ecdsa = ECDsa.Create())
{
publicKeyBytes = ecdsa.ExportSubjectPublicKeyInfo();
}
// Act
var ephemeralVerifier = _provider.CreateEphemeralVerifier(algorithmId, publicKeyBytes);
// Assert
ephemeralVerifier.KeyId.Should().Be("ephemeral");
ephemeralVerifier.AlgorithmId.Should().Be(algorithmId);
}
}

View File

@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\StellaOps.Cryptography\StellaOps.Cryptography.csproj" />
<ProjectReference Include="..\..\StellaOps.Cryptography.Plugin.OfflineVerification\StellaOps.Cryptography.Plugin.OfflineVerification.csproj" />
</ItemGroup>
</Project>