НАКОНЕЦ код, который делает вещь:
var x509Certificate = new X509Certificate2(File.ReadAllBytes(path), "",
X509KeyStorageFlags.Exportable);
var privateKey = x509Certificate.PrivateKey as RSACryptoServiceProvider;
if (privateKey == null) throw new Exception($"Private key is null for {x509Certificate.SerialNumber}");
var privateKeyParams = privateKey.ExportParameters(true);
session.Login(CKU.CKU_USER, pin);
// Create temporary DES3 key for wrapping/unwrapping
var tempKeyAttributes = new List<ObjectAttribute>
{
new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY),
new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_DES3),
new ObjectAttribute(CKA.CKA_ENCRYPT, true),
new ObjectAttribute(CKA.CKA_DECRYPT, true),
new ObjectAttribute(CKA.CKA_UNWRAP, true),
new ObjectAttribute(CKA.CKA_WRAP, true)
};
// Preparing unencrypted private key
var unencryptedPrivateKey = PrivateKeyInfoFactory.CreatePrivateKeyInfo(
new RsaPrivateCrtKeyParameters(
new BigInteger(1, privateKeyParams.Modulus),
new BigInteger(1, privateKeyParams.Exponent),
new BigInteger(1, privateKeyParams.D),
new BigInteger(1, privateKeyParams.P),
new BigInteger(1, privateKeyParams.Q),
new BigInteger(1, privateKeyParams.DP),
new BigInteger(1, privateKeyParams.DQ),
new BigInteger(1, privateKeyParams.InverseQ))).GetEncoded();
var tempKey = session.GenerateKey(new Mechanism(CKM.CKM_DES3_KEY_GEN), tempKeyAttributes);
var result = new MemoryStream();
var stream = new MemoryStream(unencryptedPrivateKey);
//Encrypting
session.Encrypt(new Mechanism(CKM.CKM_DES3_ECB), tempKey, stream,
result);
var encrypted = result.ToArray();
string label = x509Certificate.SerialNumber;
// Define how the new RSA private key should look like on the HSM
var privateKeyAttributes = new List<ObjectAttribute>
{
new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY),
new ObjectAttribute(CKA.CKA_TOKEN, true),
new ObjectAttribute(CKA.CKA_PRIVATE, true),
new ObjectAttribute(CKA.CKA_MODIFIABLE, true),
new ObjectAttribute(CKA.CKA_SENSITIVE, false),
new ObjectAttribute(CKA.CKA_LABEL, label),
new ObjectAttribute(CKA.CKA_ID, Encoding.ASCII.GetBytes(label)),
new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_RSA)
};
var privateKeyHandle = session.UnwrapKey(new Mechanism(CKM.CKM_DES3_ECB), tempKey,
encrypted, privateKeyAttributes);
return privateKeyHandle;