Поставщик загружается нормально, CPAcquireContext и CPGetUserKey вызываются успешно.
Я также пробовал проверить через командную строку.
Код: Выделить всё
PS C:\WINDOWS\system32> certutil -csp "EKENG Remote Signing CSP" -key
EKENG Remote Signing CSP:
[Default Container]
RSA
AT_KEYEXCHANGE, AT_SIGNATURE
Код: Выделить всё
using (RSA privateKey = cert.GetRSAPrivateKey())
{
// ...
}
Код: Выделить всё
=== EKENG CSP: Using MS_ENH_RSA_AES_PROV (full SHA-2 support) ===
=== EKENG CSP: CPAcquireContext called ===
Raw container name: 'Container length: 12
First 10 chars in hex: 4B45 4E45 5F47 4552 4F4D 4554 335F 3437 3333 3633
=== Container: '=== Processing named container ===
=== Using default phone number for testing ===
=== Container name validated successfully ===
=== CPAcquireContext SUCCESS ===
=== EKENG CSP: CPGetUserKey called ===
=== hProv: 1, KeySpec: 2 ===
=== Created new key handle: 2 with KeySpec: 2
=== EKENG CSP: CPGetUserKey SUCCESS ===
=== CPGetKeyParam called ===
CPGetKeyParam - hKey: 1, dwParam: 2, pbData: NOT NULL, pdwDataLen: NULL
Found context in g_KeyContexts (hKey is actually hProv)
UNKNOWN PARAMETER: 2 - Returning safe default
Код: Выделить всё
private void InitTestCertificate()
{
X509Certificate2 x509Certificate2 = new X509Certificate2("D:\\Projects\\remote_signing_ksp\\test.cer");
string phone = "374xxxxxxxx";
StoreCertificateWithKsp(x509Certificate2, phone);
}
private void StoreCertificateWithKsp(X509Certificate2 cert, string phone)
{
try
{
using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
{
store.Open(OpenFlags.ReadWrite);
// Link certificate to CSP
bool linked = LinkCertificateToCSP(cert, phone);
if (linked)
{
// Set friendly name
cert.FriendlyName = $"EKENG_{phone.Trim()}";
// Add to store
store.Add(cert);
Console.WriteLine("✓ Certificate stored and linked to CSP successfully!");
}
else
{
Console.WriteLine("✗ Failed to link certificate to CSP");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error storing certificate: {ex.Message}");
}
}
static bool LinkCertificateToCSP(X509Certificate2 cert, string phone)
{
var provInfo = new CRYPT_KEY_PROV_INFO
{
pwszContainerName = $"EKENG_REMOTE_{phone.Trim()}",
pwszProvName = "EKENG Remote Signing CSP",
dwProvType = PROV_RSA_FULL,
dwFlags = 0,
cProvParam = 0,
rgProvParam = IntPtr.Zero,
dwKeySpec = AT_SIGNATURE
};
bool ok = CertSetCertificateContextProperty(
cert.Handle, CERT_KEY_PROV_INFO_PROP_ID, 0, ref provInfo);
if (!ok)
{
Console.WriteLine($"Failed to set CRYPT_KEY_PROV_INFO. Win32: 0x{Marshal.GetLastWin32Error():X8}");
}
return ok;
}
https://github.com/grishapipoyan-gp/custom-csp
Подробнее здесь: https://stackoverflow.com/questions/798 ... rt-getrsap
Мобильная версия