本文整理汇总了C++中CK_FUNCTION_LIST_PTR::C_Sign方法的典型用法代码示例。如果您正苦于以下问题:C++ CK_FUNCTION_LIST_PTR::C_Sign方法的具体用法?C++ CK_FUNCTION_LIST_PTR::C_Sign怎么用?C++ CK_FUNCTION_LIST_PTR::C_Sign使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CK_FUNCTION_LIST_PTR
的用法示例。
在下文中一共展示了CK_FUNCTION_LIST_PTR::C_Sign方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: EstEID_RealSign
int EstEID_RealSign(CK_SESSION_HANDLE session, char **signature, unsigned int *signatureLength, const char *hash, unsigned int hashLength, char* name) {
CK_OBJECT_HANDLE privateKeyHandle;
CK_ULONG objectCount;
unsigned int hashWithPaddingLength = 0;
char *hashWithPadding;
CK_MECHANISM mechanism = {CKM_RSA_PKCS, 0, 0};
CK_OBJECT_CLASS objectClass = CKO_PRIVATE_KEY;
CK_ATTRIBUTE searchAttribute = {CKA_CLASS, &objectClass, sizeof(objectClass)};
if (EstEID_CK_failure("C_FindObjectsInit", fl->C_FindObjectsInit(session, &searchAttribute, 1))) CLOSE_SESSION_AND_RETURN(FAILURE);
if (EstEID_CK_failure("C_FindObjects", fl->C_FindObjects(session, &privateKeyHandle, 1, &objectCount))) CLOSE_SESSION_AND_RETURN(FAILURE);
if (EstEID_CK_failure("C_FindObjectsFinal", fl->C_FindObjectsFinal(session))) CLOSE_SESSION_AND_RETURN(FAILURE);
if (objectCount == 0) CLOSE_SESSION_AND_RETURN(FAILURE); // todo ?? set error message
if (EstEID_CK_failure("C_SignInit", fl->C_SignInit(session, &mechanism, privateKeyHandle))) CLOSE_SESSION_AND_RETURN(FAILURE);
hashWithPadding = EstEID_addPadding(hash, hashLength, &hashWithPaddingLength);
if (hashWithPadding) { // This is additional safeguard, as digest length is checked already before calling EstEID_addPadding()
CK_ULONG len;
if (EstEID_CK_failure("C_Sign", fl->C_Sign(session, (CK_BYTE_PTR)hashWithPadding, hashWithPaddingLength, NULL, &len))) {
free(hashWithPadding);
CLOSE_SESSION_AND_RETURN(FAILURE);
}
*signature = (char *)malloc(len);
if (EstEID_CK_failure("C_Sign", fl->C_Sign(session, (CK_BYTE_PTR)hashWithPadding, hashWithPaddingLength, (CK_BYTE_PTR) * signature, &len))) {
free(hashWithPadding);
CLOSE_SESSION_AND_RETURN(FAILURE);
}
*signatureLength = len;
free(hashWithPadding);
}
if (session) {
if (EstEID_CK_failure("C_CloseSession", fl->C_CloseSession(session))) {
return FAILURE;
}
}
if(name) {
free(name);
}
if (!hashWithPaddingLength) { // This is additional safeguard, as digest length is checked already before calling EstEID_addPadding()
EstEID_log("will not sign due to incorrect incoming message digest length");
return FAILURE;
}
EstEID_log("successfully signed");
return SUCCESS;
}
示例2:
int testStability_sign
(
CK_SESSION_HANDLE hSession,
CK_OBJECT_HANDLE hPrivateKey,
CK_BYTE_PTR pData,
CK_ULONG ulDataLen,
CK_BYTE_PTR *ppSignature,
CK_ULONG_PTR pulSignatureLen
)
{
CK_RV rv;
CK_MECHANISM mechanism = {
CKM_RSA_PKCS, NULL_PTR, 0
};
rv = p11->C_SignInit(hSession, &mechanism, hPrivateKey);
if (rv != CKR_OK)
{
printf("ERROR: Failed to initialize signing. rv=%s\n", rv2string(rv));
return 1;
}
*pulSignatureLen = 0;
rv = p11->C_Sign(hSession, pData, ulDataLen, NULL_PTR, pulSignatureLen);
if (rv != CKR_OK)
{
printf("ERROR: Failed to check the size of the signature. rv=%s\n", rv2string(rv));
return 1;
}
*ppSignature = (CK_BYTE_PTR)malloc(*pulSignatureLen);
rv = p11->C_Sign(hSession, pData, ulDataLen, *ppSignature, pulSignatureLen);
if (rv != CKR_OK)
{
printf("ERROR: Failed to sign the data. rv=%s\n", rv2string(rv));
free(*ppSignature);
*ppSignature = NULL;
*pulSignatureLen = 0;
return 1;
}
return 0;
}
示例3: DataMarshalling
//.........这里部分代码省略.........
PCCERT_CONTEXT pCertContext;
pCertContext = CertCreateCertificateContext(X509_ASN_ENCODING,((BYTE *)attr[i].pValue),attr[i].ulValueLen);
printf("data len: %d\n", attr[i].ulValueLen);
printf("issuer len: %d\n", pCertContext->pCertInfo->Issuer.cbData);
std::wcout << byte2str(pCertContext->pCertInfo->Issuer.pbData, pCertContext->pCertInfo->Issuer.cbData);
CertFreeCertificateContext(pCertContext);
}
#endif
}
}
d2->sendData();
delete d2;
}
} else if (!strcmp(d->getMsgType(), "C_FindObjectsFinal")) {
CK_SESSION_HANDLE sessionId = 0;
printf("Processing: C_FindObjectsFinal\n");
sessionId = d->unpackInt();
{
CK_RV ret = 0;
DataMarshalling *d2 = new DataMarshalling(client);
/*
* Opening session
*/
ret = pFunctionList->C_FindObjectsFinal(sessionId);
d2->setMsgType(d->getMsgType());
d2->packInt((char *)&ret);
d2->sendData();
delete d2;
}
} else if (!strcmp(d->getMsgType(), "C_SignInit")) {
CK_SESSION_HANDLE sessionId = 0;
CK_MECHANISM mechanism;
CK_OBJECT_HANDLE hKey;
printf("Processing: C_SignInit\n");
sessionId = d->unpackInt();
hKey = d->unpackInt();
mechanism.mechanism = d->unpackInt();
mechanism.ulParameterLen = d->unpackInt();
mechanism.pParameter = NULL;
{
CK_RV ret = 0;
DataMarshalling *d2 = new DataMarshalling(client);
/*
* Opening session
*/
ret = pFunctionList->C_SignInit(sessionId, &mechanism, hKey);
d2->setMsgType(d->getMsgType());
d2->packInt((char *)&ret);
d2->sendData();
delete d2;
}
} else if (!strcmp(d->getMsgType(), "C_Sign")) {
CK_SESSION_HANDLE sessionId = 0;
char *data = NULL, *signature = NULL;
CK_ULONG dataLen = 0, signatureLen = 0;
printf("Processing: C_Sign\n");
sessionId = d->unpackInt();
dataLen = d->unpackInt();
示例4: EstEID_signHash
//.........这里部分代码省略.........
}
else {
// PIN pad
#ifdef _WIN32
EstEID_log("creating pinpad dialog UI thread");
pinpad_thread_result = -1;
FAIL_IF_THREAD_ERROR("CreateMutex", (pinpad_thread_mutex = CreateMutex(NULL, FALSE, NULL)));
#else
EstEID_log("creating pinpad worker thread");
pinpad_thread_result = -1;
FAIL_IF_PTHREAD_ERROR("pthread_mutex_init", pthread_mutex_init(&pinpad_thread_mutex, NULL));
FAIL_IF_PTHREAD_ERROR("pthread_cond_init", pthread_cond_init(&pinpad_thread_condition, NULL));
pthread_t pinpad_thread;
EstEID_PINPadThreadData threadData;
threadData.session = session;
threadData.result = CKR_OK;
#endif
EstEID_log("thread launched");
#ifdef _WIN32
/*
NB! Due to Firefox for Windows specific behaviour C_Login() is launched from main thread
and UI code is running in separate thread if running on Windows.
*/
EstEID_PINPromptDataEx pinPromptDataEx;
pinPromptDataEx.pinPromptData = pinPromptData;
pinPromptDataEx.message = message;
pinPromptDataEx.name = name;
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&EstEID_pinPadLogin, (LPVOID)&pinPromptDataEx, 0, NULL);
loginResult = fl->C_Login(session, CKU_USER, NULL, 0);
closePinPadModalSheet();
#else
FAIL_IF_PTHREAD_ERROR("pthread_create", pthread_create(&pinpad_thread, NULL, EstEID_pinPadLogin, (void*)&threadData));
pinPromptData.promptFunction(pinPromptData.nativeWindowHandle, name, message, 0, isPinPad);
loginResult = threadData.result;
#endif
EstEID_log("pinpad sheet/dialog closed");
if (loginResult == CKR_FUNCTION_CANCELED) {
setUserCancelErrorCodeAndMessage();
CLOSE_SESSION_AND_FAIL;
}
}
EstEID_log("loginResult = %s", pkcs11_error_message(loginResult));
switch (loginResult) {
case CKR_PIN_LOCKED:
blocked = TRUE;
case CKR_PIN_INCORRECT:
case CKR_PIN_INVALID:
case CKR_PIN_LEN_RANGE:
EstEID_log("this was attempt %i, loginResult causes to run next round", attempt);
continue;
default:
if (EstEID_CK_failure("C_Login", loginResult)) CLOSE_SESSION_AND_FAIL;
}
break; // Login successful - correct PIN supplied
}
if (name){
free(name);
name = NULL;
}
CK_OBJECT_CLASS objectClass = CKO_PRIVATE_KEY;
CK_ATTRIBUTE searchAttribute = {CKA_CLASS, &objectClass, sizeof(objectClass)};
if (EstEID_CK_failure("C_FindObjectsInit", fl->C_FindObjectsInit(session, &searchAttribute, 1))) CLOSE_SESSION_AND_FAIL;
CK_OBJECT_HANDLE privateKeyHandle;
CK_ULONG objectCount;
if (EstEID_CK_failure("C_FindObjects", fl->C_FindObjects(session, &privateKeyHandle, 1, &objectCount))) CLOSE_SESSION_AND_FAIL;
if (EstEID_CK_failure("C_FindObjectsFinal", fl->C_FindObjectsFinal(session))) CLOSE_SESSION_AND_FAIL;
if (objectCount == 0) CLOSE_SESSION_AND_FAIL; // todo ?? set error message
CK_MECHANISM mechanism = {CKM_RSA_PKCS, 0, 0};
if (EstEID_CK_failure("C_SignInit", fl->C_SignInit(session, &mechanism, privateKeyHandle))) CLOSE_SESSION_AND_FAIL;
unsigned int hashWithPaddingLength;
char *hashWithPadding = EstEID_addPadding(hash, hashLength, &hashWithPaddingLength);
CK_ULONG len;
if (EstEID_CK_failure("C_Sign", fl->C_Sign(session, (CK_BYTE_PTR)hashWithPadding, hashWithPaddingLength, NULL, &len))) {
free(hashWithPadding);
CLOSE_SESSION_AND_FAIL;
}
*signature = (char *)malloc(len);
if (EstEID_CK_failure("C_Sign", fl->C_Sign(session, (CK_BYTE_PTR)hashWithPadding, hashWithPaddingLength, (CK_BYTE_PTR) * signature, &len))) {
free(hashWithPadding);
CLOSE_SESSION_AND_FAIL;
}
*signatureLength = len;
free(hashWithPadding);
if (session) {
if (EstEID_CK_failure("C_CloseSession", fl->C_CloseSession(session))) {
return FAILURE;
}
}
EstEID_log("successfully signed");
return SUCCESS;
}
示例5: sizeof
//.........这里部分代码省略.........
exit(EXIT_FAILURE);
tm = ASN1_TIME_new();
if (tm == NULL)
exit(EXIT_FAILURE);
ASN1_TIME_set_string(tm, "000001010000Z");
X509_set_notBefore(cert, tm);
X509_set_notAfter(cert, tm);
cert->sig_alg->algorithm = OBJ_nid2obj(8);
cert->cert_info->signature->algorithm = OBJ_nid2obj(8);
ASN1_BIT_STRING_set_bit(cert->signature, 8, 1);
ASN1_BIT_STRING_set(cert->signature, "\x00", 1);
px = value_c;
if ((cert_len = (CK_ULONG) i2d_X509(cert, &px)) == 0 || cert_len > sizeof(value_c))
exit(EXIT_FAILURE);
publicKeyTemplate[2].ulValueLen = cert_len;
asrt(funcs->C_Initialize(NULL), CKR_OK, "INITIALIZE");
asrt(funcs->C_OpenSession(0, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL, NULL, &session), CKR_OK, "OpenSession1");
asrt(funcs->C_Login(session, CKU_SO, "010203040506070801020304050607080102030405060708", 48), CKR_OK, "Login SO");
for (i = 0; i < 24; i++) {
id = i;
asrt(funcs->C_CreateObject(session, publicKeyTemplate, 3, obj + i), CKR_OK, "IMPORT CERT");
asrt(funcs->C_CreateObject(session, privateKeyTemplate, 9, obj + i), CKR_OK, "IMPORT KEY");
}
asrt(funcs->C_Logout(session), CKR_OK, "Logout SO");
for (i = 0; i < 24; i++) {
for (j = 0; j < 10; j++) {
if(RAND_pseudo_bytes(some_data, sizeof(some_data)) == -1)
exit(EXIT_FAILURE);
asrt(funcs->C_Login(session, CKU_USER, "123456", 6), CKR_OK, "Login USER");
asrt(funcs->C_SignInit(session, &mech, obj[i]), CKR_OK, "SignInit");
recv_len = sizeof(sig);
asrt(funcs->C_Sign(session, some_data, sizeof(some_data), sig, &recv_len), CKR_OK, "Sign");
/* r_len = 32; */
/* s_len = 32; */
/* der_ptr = der_encoded; */
/* *der_ptr++ = 0x30; */
/* *der_ptr++ = 0xff; // placeholder, fix below */
/* r_ptr = sig; */
/* *der_ptr++ = 0x02; */
/* *der_ptr++ = r_len; */
/* if (*r_ptr >= 0x80) { */
/* *(der_ptr - 1) = *(der_ptr - 1) + 1; */
/* *der_ptr++ = 0x00; */
/* } */
/* else if (*r_ptr == 0x00 && *(r_ptr + 1) < 0x80) { */
/* r_len--; */
/* *(der_ptr - 1) = *(der_ptr - 1) - 1; */
/* r_ptr++; */
/* } */
/* memcpy(der_ptr, r_ptr, r_len); */
/* der_ptr+= r_len; */
/* s_ptr = sig + 32; */
/* *der_ptr++ = 0x02; */
/* *der_ptr++ = s_len; */
/* if (*s_ptr >= 0x80) { */
/* *(der_ptr - 1) = *(der_ptr - 1) + 1; */
/* *der_ptr++ = 0x00; */
/* } */
/* else if (*s_ptr == 0x00 && *(s_ptr + 1) < 0x80) { */
/* s_len--; */
/* *(der_ptr - 1) = *(der_ptr - 1) - 1; */
/* s_ptr++; */
/* } */
/* memcpy(der_ptr, s_ptr, s_len); */
/* der_ptr+= s_len; */
/* der_encoded[1] = der_ptr - der_encoded - 2; */
/* dump_hex(der_encoded, der_encoded[1] + 2, stderr, 1); */
/* asrt(ECDSA_verify(0, some_data, sizeof(some_data), der_encoded, der_encoded[1] + 2, eck), 1, "ECDSA VERIFICATION"); */
}
}
asrt(funcs->C_Logout(session), CKR_OK, "Logout USER");
asrt(funcs->C_CloseSession(session), CKR_OK, "CloseSession");
asrt(funcs->C_Finalize(NULL), CKR_OK, "FINALIZE");
}
示例6: if
//.........这里部分代码省略.........
exit(EXIT_FAILURE);
tm = ASN1_TIME_new();
if (tm == NULL)
exit(EXIT_FAILURE);
ASN1_TIME_set_string(tm, "000001010000Z");
X509_set_notBefore(cert, tm);
X509_set_notAfter(cert, tm);
cert->sig_alg->algorithm = OBJ_nid2obj(8);
cert->cert_info->signature->algorithm = OBJ_nid2obj(8);
ASN1_BIT_STRING_set_bit(cert->signature, 8, 1);
ASN1_BIT_STRING_set(cert->signature, "\x00", 1);
p = value_c;
if ((cert_len = (CK_ULONG) i2d_X509(cert, &p)) == 0 || cert_len > sizeof(value_c))
exit(EXIT_FAILURE);
publicKeyTemplate[2].ulValueLen = cert_len;
asrt(funcs->C_Initialize(NULL), CKR_OK, "INITIALIZE");
asrt(funcs->C_OpenSession(0, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL, NULL, &session), CKR_OK, "OpenSession1");
asrt(funcs->C_Login(session, CKU_SO, "010203040506070801020304050607080102030405060708", 48), CKR_OK, "Login SO");
for (i = 0; i < 24; i++) {
id = i;
asrt(funcs->C_CreateObject(session, publicKeyTemplate, 3, obj + i), CKR_OK, "IMPORT CERT");
asrt(funcs->C_CreateObject(session, privateKeyTemplate, 5, obj + i), CKR_OK, "IMPORT KEY");
}
asrt(funcs->C_Logout(session), CKR_OK, "Logout SO");
for (i = 0; i < 24; i++) {
for (j = 0; j < 10; j++) {
if(RAND_pseudo_bytes(some_data, sizeof(some_data)) == -1)
exit(EXIT_FAILURE);
asrt(funcs->C_Login(session, CKU_USER, "123456", 6), CKR_OK, "Login USER");
asrt(funcs->C_SignInit(session, &mech, obj[i]), CKR_OK, "SignInit");
recv_len = sizeof(sig);
asrt(funcs->C_Sign(session, some_data, sizeof(some_data), sig, &recv_len), CKR_OK, "Sign");
r_len = 32;
s_len = 32;
der_ptr = der_encoded;
*der_ptr++ = 0x30;
*der_ptr++ = 0xff; // placeholder, fix below
r_ptr = sig;
*der_ptr++ = 0x02;
*der_ptr++ = r_len;
if (*r_ptr >= 0x80) {
*(der_ptr - 1) = *(der_ptr - 1) + 1;
*der_ptr++ = 0x00;
}
else if (*r_ptr == 0x00 && *(r_ptr + 1) < 0x80) {
r_len--;
*(der_ptr - 1) = *(der_ptr - 1) - 1;
r_ptr++;
}
memcpy(der_ptr, r_ptr, r_len);
der_ptr+= r_len;
s_ptr = sig + 32;
*der_ptr++ = 0x02;
*der_ptr++ = s_len;
if (*s_ptr >= 0x80) {
*(der_ptr - 1) = *(der_ptr - 1) + 1;
*der_ptr++ = 0x00;
}
else if (*s_ptr == 0x00 && *(s_ptr + 1) < 0x80) {
s_len--;
*(der_ptr - 1) = *(der_ptr - 1) - 1;
s_ptr++;
}
memcpy(der_ptr, s_ptr, s_len);
der_ptr+= s_len;
der_encoded[1] = der_ptr - der_encoded - 2;
dump_hex(der_encoded, der_encoded[1] + 2, stderr, 1);
asrt(ECDSA_verify(0, some_data, sizeof(some_data), der_encoded, der_encoded[1] + 2, eck), 1, "ECDSA VERIFICATION");
}
}
asrt(funcs->C_Logout(session), CKR_OK, "Logout USER");
asrt(funcs->C_CloseSession(session), CKR_OK, "CloseSession");
asrt(funcs->C_Finalize(NULL), CKR_OK, "FINALIZE");
}