I want to programmatically import certificate to safenet dongle. I want to do it in a swift application.
below is my code.
void Dongle::importCertificate(char *password, char* enrollmentId, unsigned char * subject, unsigned char * issuer, unsigned char * serialNumber, unsigned char * value ) {
CK_RV rv = CKR_OK;
CK_BBOOL bFalse = CK_FALSE;
CK_BBOOL bTrue = CK_TRUE;
CK_KEY_TYPE keyType = CKK_RSA;
CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
LoadCryptoki(pkcs11_path);
rv = GetFirstSlotId(&slotId);
rv = C_OpenSession(slotId, (CKF_SERIAL_SESSION | CKF_RW_SESSION), NULL_PTR, NULL_PTR, &hSession);
// We can also use Dongle::generateKeyPairUtil
CK_OBJECT_HANDLE hPrivateKey = Dongle::getPrivateKeyHandle(hSession, password, enrollmentId);
CK_OBJECT_HANDLE hObject;
std::string labelPriv = std::string("private") + enrollmentId;
CK_UTF8CHAR * labelPrivate = convertToCK_UTF8CHAR(labelPriv); //Label of private key.
std::string idPriv = std::string(enrollmentId);
CK_UTF8CHAR * idPrivate = convertToCK_UTF8CHAR(idPriv);
CK_OBJECT_CLASS classCertificate = CKO_CERTIFICATE;
CK_OBJECT_CLASS typeCertificate = CKC_X_509;
CK_ATTRIBUTE certificateTemplate[] =
{
{ CKA_CLASS, &classCertificate, sizeof(classCertificate) },
{ CKA_TOKEN, &bTrue, sizeof(bTrue) },
{ CKA_PRIVATE, &bFalse, sizeof(bFalse) },
{ CKA_MODIFIABLE, &bTrue, sizeof(bTrue) },
{ CKA_LABEL, &labelPrivate, strlen((char *)labelPrivate) },
{ CKA_CERTIFICATE_TYPE, &typeCertificate, sizeof(typeCertificate) },
{ CKA_TRUSTED, &bFalse, sizeof(bFalse) },
{ CKA_SUBJECT, &subject, strlen((char*) subject) },
{ CKA_ID, &idPrivate, strlen((char*)idPrivate) },
{ CKA_ISSUER, &issuer, strlen((char*) issuer) },
{ CKA_SERIAL_NUMBER, &serialNumber, strlen((char*) serialNumber) },
{ CKA_VALUE, &value, strlen((char*) value) }
};
//CK_OBJECT_HANDLE hObject;
rv = C_CreateObject( hSession,
certificateTemplate,
DIM(certificateTemplate),
&hObject );
std::cout << "object handle code: \n";
std::cout << rv;
if (rv == CKR_OK) {
std::cout<< "Created object handle"<< hObject<< std::endl;
}
if (hSession) {
C_CloseSession(hSession);
UnloadCryptoki();
}
}
I have extracted subjectDN, issuerName, serialNumber and certificate as DER and then sending it here. But I am getting error code D1(209) which says that CKR_TEMPLATE_INCONSISTENT.