Renamed namespaces and projects from DocumentOperator to DocumentService, updating all references accordingly. Refactored PdfSignatureAppearanceDto and related enums to use properties with init accessors for improved clarity. Removed and re-added IPkcs11SelfTestStatusProvider with updated namespace. Simplified dependency injection by removing DocumentOperatorApplication registration. Updated appsettings.json with a specific AssetsRootPath. Incremented version numbers in project files for a new release. Introduced PdfSignatureClient and IPdfSignatureClient for PDF digital-signing operations. Updated project files to use newer Microsoft.Extensions package versions. Removed all DocumentOperator references, completing the transition to DocumentService. Added new DTOs and enums for PDF signature options and appearance.
65 lines
2.5 KiB
C#
65 lines
2.5 KiB
C#
using DocumentService.Application.Common.Configuration;
|
|
using Net.Pkcs11Interop.Common;
|
|
using Net.Pkcs11Interop.HighLevelAPI;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace DocumentService.Infrastructure.Crypt.Services;
|
|
|
|
internal sealed class Pkcs11PdfSigningRsa(Pkcs11Options options) : RSA
|
|
{
|
|
public override int KeySize
|
|
{
|
|
get => KeySizeValue;
|
|
set => KeySizeValue = value;
|
|
}
|
|
|
|
public override KeySizes[] LegalKeySizes => [new(1024, 8192, 8)];
|
|
public override string? KeyExchangeAlgorithm => "RSA";
|
|
public override string SignatureAlgorithm => "RSA";
|
|
|
|
public override byte[] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
|
|
{
|
|
if (padding != RSASignaturePadding.Pkcs1)
|
|
throw new NotSupportedException("Only RSA PKCS#1 v1.5 padding is supported.");
|
|
|
|
byte[] digestInfo = Pkcs11DigestInfoBuilder.Build(hash, hashAlgorithm);
|
|
try
|
|
{
|
|
return Pkcs11SessionRunner.Run(options, session =>
|
|
{
|
|
var attrs = new List<IObjectAttribute>
|
|
{
|
|
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY),
|
|
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_LABEL, options.PrivateKeyLabel)
|
|
};
|
|
|
|
var handles = session.FindAllObjects(attrs);
|
|
if (handles.Count == 0)
|
|
throw new InvalidOperationException($"Private key '{options.PrivateKeyLabel}' not found in token.");
|
|
|
|
var mechanism = session.Factories.MechanismFactory.Create(CKM.CKM_RSA_PKCS);
|
|
return session.Sign(mechanism, handles[0], digestInfo);
|
|
});
|
|
}
|
|
catch when (Pkcs11ToolBridge.IsAvailable(options))
|
|
{
|
|
return Pkcs11ToolBridge.SignDigestInfo(options, digestInfo);
|
|
}
|
|
}
|
|
|
|
public override bool VerifyHash(byte[] hash, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
|
|
=> throw new NotSupportedException();
|
|
|
|
public override byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
|
|
=> throw new NotSupportedException();
|
|
|
|
public override byte[] Encrypt(byte[] data, RSAEncryptionPadding padding)
|
|
=> throw new NotSupportedException();
|
|
|
|
public override RSAParameters ExportParameters(bool includePrivateParameters)
|
|
=> throw new NotSupportedException();
|
|
|
|
public override void ImportParameters(RSAParameters parameters)
|
|
=> throw new NotSupportedException();
|
|
}
|