Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -1,13 +1,20 @@
|
||||
using Net.Pkcs11Interop.Common;
|
||||
using Net.Pkcs11Interop.HighLevelAPI;
|
||||
using Net.Pkcs11Interop.HighLevelAPI.MechanismParams;
|
||||
using Pkcs11 = Net.Pkcs11Interop.HighLevelAPI.Pkcs11;
|
||||
using Slot = Net.Pkcs11Interop.HighLevelAPI.Slot;
|
||||
using ISession = Net.Pkcs11Interop.HighLevelAPI.Session;
|
||||
using ObjectHandle = Net.Pkcs11Interop.HighLevelAPI.ObjectHandle;
|
||||
using ObjectAttribute = Net.Pkcs11Interop.HighLevelAPI.ObjectAttribute;
|
||||
using Mechanism = Net.Pkcs11Interop.HighLevelAPI.Mechanism;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Formats.Asn1;
|
||||
using System.Security.Cryptography;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace StellaOps.Cryptography.Kms;
|
||||
|
||||
internal interface IPkcs11Facade : IDisposable
|
||||
public interface IPkcs11Facade : IDisposable
|
||||
{
|
||||
Task<Pkcs11KeyDescriptor> GetKeyAsync(CancellationToken cancellationToken);
|
||||
|
||||
@@ -16,12 +23,12 @@ internal interface IPkcs11Facade : IDisposable
|
||||
Task<byte[]> SignDigestAsync(ReadOnlyMemory<byte> digest, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
internal sealed record Pkcs11KeyDescriptor(
|
||||
public sealed record Pkcs11KeyDescriptor(
|
||||
string KeyId,
|
||||
string? Label,
|
||||
DateTimeOffset CreatedAt);
|
||||
|
||||
internal sealed record Pkcs11PublicKeyMaterial(
|
||||
public sealed record Pkcs11PublicKeyMaterial(
|
||||
string KeyId,
|
||||
string Curve,
|
||||
byte[] Qx,
|
||||
@@ -57,11 +64,11 @@ internal sealed class Pkcs11InteropFacade : IPkcs11Facade
|
||||
throw new InvalidOperationException("PKCS#11 private key not found.");
|
||||
}
|
||||
|
||||
var labelAttr = GetAttribute(session, privateHandle.Value, CKA.CKA_LABEL);
|
||||
var labelAttr = GetAttribute(session, privateHandle, CKA.CKA_LABEL);
|
||||
var label = labelAttr?.GetValueAsString();
|
||||
|
||||
return new Pkcs11KeyDescriptor(
|
||||
KeyId: label ?? privateHandle.Value.ObjectId.ToString(),
|
||||
KeyId: label ?? privateHandle.ObjectId.ToString(),
|
||||
Label: label,
|
||||
CreatedAt: DateTimeOffset.UtcNow);
|
||||
}
|
||||
@@ -76,9 +83,9 @@ internal sealed class Pkcs11InteropFacade : IPkcs11Facade
|
||||
throw new InvalidOperationException("PKCS#11 public key not found.");
|
||||
}
|
||||
|
||||
var pointAttr = GetAttribute(session, publicHandle.Value, CKA.CKA_EC_POINT)
|
||||
var pointAttr = GetAttribute(session, publicHandle, CKA.CKA_EC_POINT)
|
||||
?? throw new InvalidOperationException("Public key missing EC point.");
|
||||
var paramsAttr = GetAttribute(session, publicHandle.Value, CKA.CKA_EC_PARAMS)
|
||||
var paramsAttr = GetAttribute(session, publicHandle, CKA.CKA_EC_PARAMS)
|
||||
?? throw new InvalidOperationException("Public key missing EC parameters.");
|
||||
|
||||
var ecPoint = ExtractEcPoint(pointAttr.GetValueAsByteArray());
|
||||
@@ -92,8 +99,8 @@ internal sealed class Pkcs11InteropFacade : IPkcs11Facade
|
||||
var qx = ecPoint.AsSpan(1, coordinateSize).ToArray();
|
||||
var qy = ecPoint.AsSpan(1 + coordinateSize, coordinateSize).ToArray();
|
||||
|
||||
var keyId = GetAttribute(session, publicHandle.Value, CKA.CKA_LABEL)?.GetValueAsString()
|
||||
?? publicHandle.Value.ObjectId.ToString();
|
||||
var keyId = GetAttribute(session, publicHandle, CKA.CKA_LABEL)?.GetValueAsString()
|
||||
?? publicHandle.ObjectId.ToString();
|
||||
|
||||
return new Pkcs11PublicKeyMaterial(
|
||||
keyId,
|
||||
@@ -110,7 +117,7 @@ internal sealed class Pkcs11InteropFacade : IPkcs11Facade
|
||||
?? throw new InvalidOperationException("PKCS#11 private key not found.");
|
||||
|
||||
var mechanism = new Mechanism(_options.MechanismId);
|
||||
return session.Sign(mechanism, privateHandle.Value, digest.ToArray());
|
||||
return session.Sign(mechanism, privateHandle, digest.ToArray());
|
||||
}
|
||||
|
||||
private async Task<SessionContext> OpenSessionAsync(CancellationToken cancellationToken)
|
||||
|
||||
Reference in New Issue
Block a user