Import private key and search associated public key
CK_BYTE p[] = { 0xd8, 0x62, 0x45, 0x54, 0x66, 0x02, 0xea, 0x8f, 0xde, 0x9f, 0xff, 0xe1, 0xc4, 0x34, 0xf7, 0x83, 0x1e, 0xd7, 0x9d, 0x2e, 0xf7, 0x66, 0xc6, 0xfd, 0x6e, 0x81, 0xf7, 0xa5, 0x05, 0xed, 0x4d, 0xc6, 0x06, 0xaa, 0x2e, 0xf3, 0x37, 0x78, 0x07, 0x03, 0x46, 0x5a, 0x51, 0xa2, 0x4d, 0x9f, 0xd2, 0x3a, 0x11, 0xff, 0x89, 0x4f, 0x21, 0xff, 0x3d, 0xa8, 0xa7, 0x0a, 0xe8, 0x3a, 0x2e, 0xcd, 0xfa, 0x75, 0xdd, 0x11, 0x3a, 0xf6, 0x29, 0x49, 0x54, 0xd8, 0x06, 0xc9, 0xa0, 0xb1, 0x47, 0x23, 0x5e, 0xc3, 0x52, 0x19, 0xae, 0x72, 0x54, 0x66, 0x71, 0xd9, 0xff, 0x9a, 0x4a, 0x13, 0x6e, 0x44, 0xb7, 0x42, 0x9f, 0x1a, 0xe4, 0xf2, 0xce, 0xe1, 0x94, 0x57, 0x27, 0x3d, 0x9d, 0xd5, 0x19, 0x73, 0xfe, 0x9b, 0x29, 0x50, 0x21, 0xc1, 0xea, 0x8e, 0x71, 0xbe, 0x5f, 0xff, 0xe5, 0x3a, 0xd5, 0xc4, 0x8e, 0x3b }; CK_BYTE q[] = { 0xdd, 0x81, 0x04, 0xb3, 0xd9, 0x47, 0x28, 0xa4, 0xeb, 0x95, 0x70, 0x14, 0x18, 0xa4, 0x11, 0x07, 0xe9, 0xec, 0x10, 0x44, 0xd5, 0x63, 0xa9, 0x52, 0xc9, 0x39, 0x49, 0x43, 0xd8, 0x05, 0xaf, 0xa2, 0x60, 0xcc, 0xe3, 0x49, 0xb4, 0x52, 0x38, 0xd4, 0x71, 0xc1, 0x5b, 0x75, 0x60, 0x46, 0xe1, 0xff, 0x46, 0xce, 0x5c, 0xdf, 0x97, 0xe1, 0x00, 0x89, 0x05, 0x16, 0x80, 0x3d, 0x15, 0x7e, 0x03, 0xbf, 0x09, 0xb6, 0x9e, 0x7e, 0x38, 0x55, 0xed, 0x47, 0x58, 0x9d, 0xc9, 0x72, 0x39, 0xe6, 0x50, 0xc4, 0x93, 0xe5, 0x36, 0x41, 0x81, 0x3e, 0xef, 0xdd, 0x29, 0xab, 0xc5, 0xdd, 0x25, 0x77, 0x85, 0x3c, 0x25, 0xb4, 0x94, 0xfe, 0x4c, 0x0d, 0x43, 0xef, 0x29, 0x61, 0x99, 0xbe, 0xa5, 0x71, 0x65, 0xdd, 0x1f, 0xde, 0xa9, 0x87, 0x6c, 0x85, 0x26, 0x42, 0x7b, 0x49, 0x68, 0xdc, 0x16, 0x5b, 0xa6, 0xeb }; CK_BYTE publicExponent[] = { 0x01, 0x00, 0x01 }; CK_OBJECT_HANDLE publicKeyObjectHandle = 0; CK_OBJECT_HANDLE privateKeyObjectHandle = 0; CK_ATTRIBUTE aClass, aKeyType, aID, aToken, aPrivate; CK_OBJECT_CLASS classType = CKO_PRIVATE_KEY; CK_KEY_TYPE keyType = CKK_RSA; CK_BYTE ID[16]; fillRandomID( ID, 16 ); CK_BBOOL trueValue = CK_TRUE; aClass.type = CKA_CLASS; aKeyType.type = CKA_KEY_TYPE; aID.type = CKA_ID; aToken.type = CKA_TOKEN; aPrivate.type = CKA_PRIVATE; aClass.pValue = &classType; aKeyType.pValue = &keyType; aID.pValue = ID; aToken.pValue = &trueValue; aPrivate.pValue = &trueValue; aClass.ulValueLen = sizeof( classType ); aKeyType.ulValueLen = sizeof( keyType ); aID.ulValueLen = sizeof( ID ); aToken.ulValueLen = sizeof( trueValue ); aPrivate.ulValueLen = sizeof( trueValue ); CK_ATTRIBUTE aPrimeP, aPrimeQ, aPublicExponent; aPrimeP.type = CKA_PRIME_1; aPrimeQ.type = CKA_PRIME_2; aPublicExponent.type = CKA_PUBLIC_EXPONENT; aPrimeP.pValue = p.data(); aPrimeQ.pValue = q.data(); aPublicExponent.pValue = publicExponent.data(); aPrimeP.ulValueLen = p.size(); aPrimeQ.ulValueLen = q.size(); aPublicExponent.ulValueLen = publicExponent.size(); CK_ATTRIBUTE attributes[] = {aClass, aKeyType, aID, aToken, aPrivate, aPrimeP, aPrimeQ, aPublicExponent}; pList->C_CreateObject( session, attributes, sizeof( attributes ) / sizeof( CK_ATTRIBUTE ), &privateKeyObjectHandle ); //find corresponding public key CK_OBJECT_CLASS anotherClassType = CKO_PUBLIC_KEY; aClass.pValue = &anotherClassType; CK_ATTRIBUTE publicKeyAttributes[] = {aClass, aID}; CK_ULONG count = 1; pList->C_FindObjectsInit( session, publicKeyAttributes, sizeof( publicKeyAttributes ) / sizeof( CK_ATTRIBUTE ) ); pList->C_FindObjects( session, &publicKeyObjectHandle, 1, &count ); pList->C_FindObjectsFinal( session );
Create RSA public key session object to verify some signature
CK_OBJECT_HANDLE publicKeyHandle; CK_BYTE data[] = {'h', 'a', 'l', 'l', 'o'}; CK_BYTE signature[] = {0x8E, 0x99, 0xE3, 0x0D, 0xB4, 0xA6, 0x44, 0x0D, 0x91, 0x9D, 0x64, 0x07, 0xB4, 0xED, 0x07, 0x4F, 0x83, 0x80, 0xA8, 0x41, 0x9E, 0xB0, 0xB4, 0xE3, 0x74, 0x77, 0x48, 0xD7, 0x2F, 0xE4, 0xAC, 0x74, 0xF4, 0xD5, 0xE2, 0x3A, 0xAB, 0xB7, 0xD4, 0xC7, 0xE8, 0xD9, 0x54, 0xD6, 0x61, 0xB9, 0xC9, 0x01, 0xBF, 0xA6, 0x02, 0x98, 0x33, 0xDC, 0xA0, 0x9B, 0x4E, 0xBF, 0xFF, 0xB8, 0x07, 0x02, 0x35, 0x72}; CK_BYTE modulus[] = {0x99, 0x69, 0x9D, 0x45, 0x39, 0xDB, 0x53, 0x68, 0xC4, 0x6D, 0xEB, 0x6E, 0x4C, 0x25, 0xEC, 0x5B, 0x84, 0xBE, 0x17, 0xC8, 0x85, 0xF5, 0x63, 0x03, 0x2E, 0x3A, 0xFE, 0x59, 0x92, 0x9A, 0x29, 0x3C, 0xD0, 0x4F, 0x7B, 0x57, 0xB7, 0x11, 0x9D, 0x5E, 0xE6, 0x86, 0x5B, 0x03, 0xA0, 0xD9, 0xA9, 0xEA, 0x2E, 0xB9, 0x15, 0xA7, 0xBF, 0x80, 0xA6, 0x69, 0x6E, 0x23, 0x5A, 0x01, 0xDE, 0xCE, 0xED, 0x5D}; CK_BYTE public_exponent[] = {0x01, 0x00, 0x01}; CK_ATTRIBUTE aClass, aKeyType, aID, aToken, aPrivate; CK_OBJECT_CLASS classType = CKO_PUBLIC_KEY; CK_KEY_TYPE keyType = CKK_RSA; CK_BYTE ID[] = {'t', 'e', 's', 't', '_', 'R', 'S', 'A', '_', '5', '1', '2'}; CK_BBOOL trueValue = CK_TRUE; CK_BBOOL falseValue = CK_FALSE; aClass.type = CKA_CLASS; aKeyType.type = CKA_KEY_TYPE; aID.type = CKA_ID; aToken.type = CKA_TOKEN; aPrivate.type = CKA_PRIVATE; aClass.pValue = &classType; aKeyType.pValue = &keyType; aID.pValue = ID; aToken.pValue = &falseValue; aPrivate.pValue = &falseValue; aClass.ulValueLen = sizeof( classType ); aKeyType.ulValueLen = sizeof( keyType ); aID.ulValueLen = sizeof( ID ); aToken.ulValueLen = sizeof( falseValue ); aPrivate.ulValueLen = sizeof( falseValue ); CK_ATTRIBUTE aModulus, aPublicExponent; aModulus.type = CKA_MODULUS; aPublicExponent.type = CKA_PUBLIC_EXPONENT; aModulus.pValue = modulus; aPublicExponent.pValue = public_exponent; aModulus.ulValueLen = sizeof( modulus ); aPublicExponent.ulValueLen = sizeof( public_exponent ); CK_ATTRIBUTE attributes[] = {aClass, aKeyType, aID, aToken, aPrivate, aModulus, aPublicExponent}; pList->C_CreateObject( sessionID, attributes, sizeof( attributes ) / sizeof( CK_ATTRIBUTE ), &publicKeyHandle ); CK_MECHANISM mechanism; mechanism.mechanism = CKM_SHA1_RSA_PKCS; pList->C_VerifyInit( sessionID, &mechanism, publicKeyHandle); pList->C_Verify( sessionID, data, sizeof( data ), signature, sizeof( signature ));
Generate EC keypair
CK_OBJECT_HANDLE publicKeyHandle, privateKeyHandle; CK_MECHANISM mech; mech.mechanism = CKM_ECDSA_KEY_PAIR_GEN; CK_ATTRIBUTE aClass, aToken, aPrivate, aKeyType, aECParams; CK_OBJECT_CLASS classType = CKO_PRIVATE_KEY; CK_BBOOL falseValue = CK_FALSE; CK_BBOOL trueValue = CK_TRUE; CK_KEY_TYPE keyType = CKK_EC; aClass.type = CKA_CLASS; aClass.pValue = &classType; aClass.ulValueLen = sizeof( classType ); aToken.type = CKA_TOKEN; aToken.pValue = &trueValue; aToken.ulValueLen = sizeof( trueValue ); aPrivate.type = CKA_PRIVATE; aPrivate.pValue = &trueValue; aPrivate.ulValueLen = sizeof( trueValue ); aKeyType.type = CKA_KEY_TYPE; aKeyType.pValue = &keyType; aKeyType.ulValueLen = sizeof( keyType ); aECParams.type = CKA_EC_PARAMS; aECParams.pValue = oid; aECParams.ulValueLen = oidSize; CK_ATTRIBUTE attributes[] = {aClass, aToken, aPrivate, aKeyType, aECParams}; pList->C_GenerateKeyPair( session, &mech, attributes, sizeof( attributes ) / sizeof( CK_ATTRIBUTE ), attributes, sizeof( attributes ) / sizeof( CK_ATTRIBUTE ), &publicKeyHandle, &privateKeyHandle );
Change attribute
//... privateKey & publicKey have been created / generated previously -> we change their IDs and Label CK_ATTRIBUTE aID, aLabel; char test[] = "Test"; char ID[4]; char LABEL[4]; aID.type = CKA_ID; aID.pValue = test; aID.ulValueLen = 4; aLabel.type = CKA_LABEL; aLabel.pValue = test; aLabel.ulValueLen = 4; CK_ATTRIBUTE attributes[] = {aID, aLabel}; pFunctionList->C_SetAttributeValue( session, privateKey, attributes, sizeof( attributes ) / sizeof( CK_ATTRIBUTE ) ); pFunctionList->C_SetAttributeValue( session, publicKey, attributes, sizeof( attributes ) / sizeof( CK_ATTRIBUTE ) );