Основа на примере
https://github.com/AktivCo/rutoken-demo … er/Token.m
Инициализация
CK_RV rv = rt_eng_init();
if (rv != 1) {
[ACPKCS11Exception raiseWithPKCS11ErrorCode:0 fromFunction:@"rt_eng_init"];
}
ENGINE* rtEngine = rt_eng_get0_engine();
self.rtEngine = rtEngine;
if (rtEngine == nil) {
[ACPKCS11Exception raiseWithPKCS11ErrorCode:0 fromFunction:@"rt_eng_get0_engine"];
}
rv = ENGINE_set_default(rtEngine, ENGINE_METHOD_ALL - ENGINE_METHOD_RAND);
if (rv != 1) {
[ACPKCS11Exception raiseWithPKCS11ErrorCode:0 fromFunction:@"ENGINE_set_default"];
}
rv = C_GetFunctionList(&_functions);
if (CKR_OK != rv) {
[ACPKCS11Exception raiseWithPKCS11ErrorCode:rv fromFunction:@"C_GetFunctionList"];
}
CK_C_INITIALIZE_ARGS args = {};
args.CreateMutex = NULL_PTR;
args.DestroyMutex = NULL_PTR;
args.LockMutex = NULL_PTR;
args.UnlockMutex = NULL_PTR;
args.pReserved = NULL_PTR;
args.flags = CKF_OS_LOCKING_OK;
rv = self.functions->C_Initialize(&args);
if (rv != CKR_OK) {
[ACPKCS11Exception raiseWithPKCS11ErrorCode:rv fromFunction:@"C_Initialize"];
}
получение handle по id
- (void)findCertificate:(CK_SESSION_HANDLE)session certId:(NSData *)certId certificate:(CK_OBJECT_HANDLE_PTR)certificate key:(CK_OBJECT_HANDLE_PTR)key {
CK_RV rv = CKR_OK;
// класс сертификата
CK_ULONG certClass = CKO_CERTIFICATE;
CK_CHAR_PTR attrCertId = 0;
NSUInteger attrCertIdSize = 0;
if (certId != NULL) {
attrCertId = (unsigned char *)certId.bytes;
attrCertIdSize = certId.length;
}
// атрибуты для поиска сертификатов
CK_ATTRIBUTE certAttribs[] = {
{CKA_CLASS, &certClass, sizeof(certClass)},
{CKA_ID, attrCertId, attrCertIdSize}
};
*certificate = [self findObj:session attributes:certAttribs attributesCount:sizeof(certAttribs)/sizeof(CK_ATTRIBUTE)];
if (*certificate == CK_INVALID_HANDLE) {
return;
}
if (key == nil) {
return;
}
certClass = CKO_PRIVATE_KEY;
*key = [self findObj:session attributes:certAttribs attributesCount:sizeof(certAttribs)/sizeof(CK_ATTRIBUTE)];
}
- (CK_OBJECT_HANDLE)findObj:(CK_SESSION_HANDLE)session attributes:(CK_ATTRIBUTE*)attributes attributesCount:(CK_ULONG)attributesCount {
CK_OBJECT_HANDLE result = CK_INVALID_HANDLE;
CK_RV rv = CKR_OK;
rv = self.functions->C_FindObjectsInit(session, attributes, attributesCount);
if (rv != CKR_OK) {
[ACPKCS11Exception raiseWithPKCS11ErrorCode:rv fromFunction:@"C_FindObjectsInit"];
} else {
// массив хендлов сертификатов
CK_OBJECT_HANDLE certificates[1];
// количество найденых сертификатов
CK_ULONG certsCount = 0;
// поиск сертификатов
rv = self.functions->C_FindObjects(session, certificates, 1, &certsCount);
if (rv != CKR_OK) {
[ACPKCS11Exception raiseWithPKCS11ErrorCode:rv fromFunction:@"C_FindObjects"];
}
if (certsCount != 0) {
CK_OBJECT_HANDLE certHandle = certificates[0];
result = certHandle;
}
// завершение операции поиска
rv = self.functions->C_FindObjectsFinal(session);
if (rv != CKR_OK) {
[ACPKCS11Exception raiseWithPKCS11ErrorCode:rv fromFunction:@"C_FindObjectsFinal"];
}
}
return result;
}
подключение поиск хендлов
rv = self.functions->C_OpenSession(slotID, (CKF_SERIAL_SESSION | CKF_RW_SESSION), nil, nil, &session);
if (rv != CKR_OK) {
[ACPKCS11Exception raiseWithPKCS11ErrorCode:rv fromFunction:@"C_OpenSession"];
}
NSString *pin = [signingParams.pinCode copy];
if (pin == nil)
pin = @"";
// подключение к токену с ПИН-кодом
rv = self.functions->C_Login(session, CKU_USER, (CK_CHAR_PTR)[pin UTF8String], [pin length]);
if (rv != CKR_OK) {
if (rv == CKR_PIN_LOCKED) {
[NSException raise:@"E_PIN_LOCKED" format:@"Пин заблокирован"];
}
if (rv == CKR_PIN_INVALID || rv == CKR_PIN_INCORRECT || rv == CKR_ARGUMENTS_BAD || rv == CKR_PIN_LEN_RANGE) {
[NSException raise:@"E_PIN_ERROR" format:@"Не верный пин код, установите пин код в настройках приложения"];
}
[ACPKCS11Exception raiseWithPKCS11ErrorCode:rv fromFunction:@"C_Login"];
}
// Дескриптор сертификата и ключа
CK_OBJECT_HANDLE certificateHandle = CK_INVALID_HANDLE;
CK_OBJECT_HANDLE keyHandle = CK_INVALID_HANDLE;
[self findCertificate:session certId:signingParams.signerInfo.signerCertificate.certificateId certificate:&certificateHandle key:&keyHandle];
NSLog(@"certificateHandle: %ld", certificateHandle);
NSLog(@"keyHandle: %ld", keyHandle);
wrappedSession = rt_eng_p11_session_new(self.functions, session, 0, NULL);
if (!wrappedSession.self) {
[ACPKCS11Exception raiseWithPKCS11ErrorCode:rv fromFunction:@"rt_eng_p11_session_new"];
}
evpPKey = rt_eng_new_p11_ossl_evp_pkey(wrappedSession, keyHandle, certificateHandle);
RT_ENG_CALL(wrappedSession, free);
evpPKey = NULL
сертификаты просроченные, может это является проблемой получения EVP_PKEY?