📄 ospcryptowrap.c
字号:
errorcode = OSPC_ERR_CRYPTO_IMPLEMENTATION_SPECIFIC_ERROR; OSPM_DBGERRORLOG(errorcode, errmsg); } } if (errorcode == 0) { errorcode = B_EncryptFinal(rsaEncrypter, encryptedData+updateLength, &finalLength, (OSPC_CRYPTO_ENCRYPT_BUFFER_MAXLENGTH - updateLength), randomAlgorithm, (A_SURRENDER_CTX *) NULL); if (errorcode) { sprintf(errmsg, "B-SAFE error %d occurred in Encrypt", errorcode); errorcode = OSPC_ERR_CRYPTO_IMPLEMENTATION_SPECIFIC_ERROR; OSPM_DBGERRORLOG(errorcode, errmsg); } } if (errorcode == 0) { encryptedDataLength = updateLength+finalLength; if (ospvEncryptedData == NULL) { errorcode = OSPC_ERR_CRYPTO_INVALID_POINTER; OSPM_DBGERRORLOG(errorcode, "Error allocating space for encrypted data"); } if (*ospvEncryptedDataLength < encryptedDataLength) { errorcode = OSPC_ERR_CRYPTO_NOT_ENOUGH_SPACE; OSPM_DBGERRORLOG(errorcode, "Not enough space for encrypted data"); } if (errorcode == 0) { memcpy(ospvEncryptedData, encryptedData, encryptedDataLength); *ospvEncryptedDataLength = encryptedDataLength; OSPTNLOGDUMP(ospvEncryptedData, *ospvEncryptedDataLength, "ENCRYPT: ospvEncryptedData"); } } B_DestroyAlgorithmObject(&rsaEncrypter); B_DestroyAlgorithmObject(&randomAlgorithm); B_DestroyKeyObject(&signerKey); return errorcode;}int OSPPCryptoWrapDecrypt( unsigned char *ospvDecryptedData, unsigned *ospvDecryptedDataLength, unsigned char *ospvBERAlgorithm, unsigned ospvBERAlgorithmLength, unsigned char *ospvEncryptedData, unsigned ospvEncryptedDataLength, unsigned char *ospvBERReaderKey, unsigned ospvBERReaderKeyLength, unsigned char ospvFlags){ int errorcode = 0; unsigned char decryptedData[OSPC_CRYPTO_ENCRYPT_BUFFER_MAXLENGTH]; unsigned int decryptedDataLength = 0; unsigned int updateLength = 0; unsigned int finalLength = 0; char errmsg[100]; ITEM algorithmItem; ITEM keyItem; B_INFO_TYPE keyType = (B_INFO_TYPE)OSPC_OSNULL; B_KEY_OBJ readerKey = (B_KEY_OBJ)OSPC_OSNULL; B_ALGORITHM_OBJ rsaDecrypter = (B_ALGORITHM_OBJ) NULL; B_ALGORITHM_OBJ randomAlgorithm = (B_ALGORITHM_OBJ) NULL; OSPM_ARGUSED(ospvFlags); OSPTNLOGDUMP(ospvEncryptedData, ospvEncryptedDataLength, "DECRYPT: ospvEncryptedData"); OSPTNLOGDUMP(ospvBERReaderKey, ospvBERReaderKeyLength, "DECRYPT: ospvBERReaderKey"); /* Create an algorithm object */ errorcode = B_CreateAlgorithmObject(&rsaDecrypter); if (errorcode) { sprintf(errmsg, "B-SAFE error %d occurred in Decrypt", errorcode); errorcode = OSPC_ERR_CRYPTO_IMPLEMENTATION_SPECIFIC_ERROR; OSPM_DBGERRORLOG(errorcode, errmsg); } if (errorcode == 0) { /* Setup the algorithm object */ algorithmItem.data = ospvBERAlgorithm; algorithmItem.len = ospvBERAlgorithmLength; if (ospvFlags & OSPC_CRYPTO_FLAG_USE_BER_ALGORITHM) { keyType = KI_RSAPublicBER; errorcode = B_SetAlgorithmInfo(rsaDecrypter, AI_PKCS_RSAPublicBER, (POINTER) &algorithmItem); if (errorcode != OSPC_ERR_NO_ERROR) { /* Maybe the algorithm doesn't match BER because BER is for Decryption using Public key. */ /* Maybe this is a public key encryption request */ OSPM_DBGERRORLOG(0, "RSAPublic algorithm not specified for " "Decrypt - trying RSAPrivate algorithm"); keyType = KI_PKCS_RSAPrivateBER; errorcode = B_SetAlgorithmInfo(rsaDecrypter, AI_PKCS_RSAPrivateBER, (POINTER) &algorithmItem); } } else { if (!(ospvFlags & OSPC_CRYPTO_FLAG_ENCRYPTED_WITH_PUBLIC_KEY)) { /* Normally decrypt using public key */ keyType = KI_RSAPublicBER; errorcode = B_SetAlgorithmInfo(rsaDecrypter, AI_PKCS_RSAPublic, (POINTER) NULL); } else { /* This is alternative to decrypt for receiver only */ keyType = KI_PKCS_RSAPrivateBER; errorcode = B_SetAlgorithmInfo(rsaDecrypter, AI_PKCS_RSAPrivate, (POINTER) NULL); } } } if (errorcode == 0) { errorcode = B_CreateKeyObject(&readerKey); if (errorcode) { sprintf(errmsg, "B-SAFE error %d occurred in Decrypt", errorcode); errorcode = OSPC_ERR_CRYPTO_IMPLEMENTATION_SPECIFIC_ERROR; OSPM_DBGERRORLOG(errorcode, errmsg); } if (errorcode == 0) { keyItem.data = ospvBERReaderKey; keyItem.len = ospvBERReaderKeyLength; errorcode = B_SetKeyInfo(readerKey, keyType, (POINTER) &keyItem); if (errorcode) { sprintf(errmsg, "B-SAFE error %d occurred in Decrypt", errorcode); errorcode = OSPC_ERR_CRYPTO_IMPLEMENTATION_SPECIFIC_ERROR; OSPM_DBGERRORLOG(errorcode, errmsg); } } } if (errorcode == 0) { /* Initialize */ errorcode = B_DecryptInit(rsaDecrypter, (B_KEY_OBJ) readerKey, Chooser, (A_SURRENDER_CTX *)NULL); if (errorcode) { sprintf(errmsg, "B-SAFE error %d occurred in Decrypt", errorcode); errorcode = OSPC_ERR_CRYPTO_IMPLEMENTATION_SPECIFIC_ERROR; OSPM_DBGERRORLOG(errorcode, errmsg); } } if (errorcode == 0) { errorcode = B_DecryptUpdate(rsaDecrypter, decryptedData, &updateLength, OSPC_CRYPTO_ENCRYPT_BUFFER_MAXLENGTH, ospvEncryptedData, ospvEncryptedDataLength, randomAlgorithm, (A_SURRENDER_CTX *) NULL); if (errorcode) { sprintf(errmsg, "B-SAFE error %d occurred in Decrypt", errorcode); errorcode = OSPC_ERR_CRYPTO_IMPLEMENTATION_SPECIFIC_ERROR; OSPM_DBGERRORLOG(errorcode, errmsg); } } if (errorcode == 0) { errorcode = B_DecryptFinal(rsaDecrypter, decryptedData+updateLength, &finalLength, (OSPC_CRYPTO_ENCRYPT_BUFFER_MAXLENGTH - updateLength), randomAlgorithm, (A_SURRENDER_CTX *) NULL); if (errorcode) { sprintf(errmsg, "B-SAFE error %d occurred in Decrypt", errorcode); errorcode = OSPC_ERR_CRYPTO_IMPLEMENTATION_SPECIFIC_ERROR; OSPM_DBGERRORLOG(errorcode, errmsg); } } if (errorcode == 0) { decryptedDataLength = (updateLength + finalLength); if (ospvDecryptedData == NULL) { errorcode = OSPC_ERR_CRYPTO_UNABLE_TO_ALLOCATE_SPACE; OSPM_DBGERRORLOG(errorcode, "Error allocating space for decrypted data"); } if (errorcode == 0) { memcpy(ospvDecryptedData, decryptedData, decryptedDataLength); *ospvDecryptedDataLength = decryptedDataLength; OSPTNLOGDUMP(ospvDecryptedData, *ospvDecryptedDataLength, "DECRYPT: ospvDecryptedData"); } } B_DestroyAlgorithmObject(&rsaDecrypter); B_DestroyAlgorithmObject(&randomAlgorithm); B_DestroyKeyObject(&readerKey); return errorcode;}int OSPPCryptoWrapVerify( unsigned char *ospvData, unsigned ospvDataLength, unsigned char *ospvSignature, unsigned ospvSignatureLength, unsigned char *ospvBERReaderKey, unsigned ospvBERReaderKeyLength, unsigned char ospvFlags){ int errorcode = 0; char errmsg[100]; ITEM keyItem; B_KEY_OBJ readerKey = (B_KEY_OBJ)OSPC_OSNULL; B_ALGORITHM_OBJ rsaVerifier = (B_ALGORITHM_OBJ) NULL; OSPM_ARGUSED(ospvFlags); OSPTNLOGDUMP(ospvData, ospvDataLength, "VERIFY: ospvData"); OSPTNLOGDUMP(ospvSignature, ospvSignatureLength, "VERIFY: ospvSignature"); OSPTNLOGDUMP(ospvBERReaderKey, ospvBERReaderKeyLength, "VERIFY: ospvBERReaderKey"); /* Create an algorithm object */ errorcode = B_CreateAlgorithmObject(&rsaVerifier); if (errorcode) { sprintf(errmsg, "B-SAFE error %d occurred in Verify", errorcode); errorcode = OSPC_ERR_CRYPTO_IMPLEMENTATION_SPECIFIC_ERROR; OSPM_DBGERRORLOG(errorcode, errmsg); } if (errorcode == 0) { /* Setup the algorithm object */ errorcode = B_SetAlgorithmInfo(rsaVerifier, AI_MD5WithRSAEncryption, (POINTER) NULL); if (errorcode) { sprintf(errmsg, "B-SAFE error %d occurred in Verify", errorcode); errorcode = OSPC_ERR_CRYPTO_IMPLEMENTATION_SPECIFIC_ERROR; OSPM_DBGERRORLOG(errorcode, errmsg); } } if (errorcode == 0) { errorcode = B_CreateKeyObject(&readerKey); if (errorcode) { sprintf(errmsg, "B-SAFE error %d occurred in Verify", errorcode); errorcode = OSPC_ERR_CRYPTO_IMPLEMENTATION_SPECIFIC_ERROR; OSPM_DBGERRORLOG(errorcode, errmsg); } if (errorcode == 0) { keyItem.data = ospvBERReaderKey; keyItem.len = ospvBERReaderKeyLength; errorcode = B_SetKeyInfo(readerKey, KI_RSAPublicBER, (POINTER) &keyItem); if (errorcode) { sprintf(errmsg, "B-SAFE error %d occurred in Verify", errorcode); errorcode = OSPC_ERR_CRYPTO_IMPLEMENTATION_SPECIFIC_ERROR; OSPM_DBGERRORLOG(errorcode, errmsg); } } } if (errorcode == 0) { /* Initialize */ errorcode = B_VerifyInit(rsaVerifier, (B_KEY_OBJ) readerKey, Chooser, (A_SURRENDER_CTX *)NULL); if (errorcode) { sprintf(errmsg, "B-SAFE error %d occurred in Verify", errorcode); errorcode = OSPC_ERR_CRYPTO_IMPLEMENTATION_SPECIFIC_ERROR; OSPM_DBGERRORLOG(errorcode, errmsg); } } if (errorcode == 0) { errorcode = B_VerifyUpdate(rsaVerifier, ospvData, ospvDataLength, (A_SURRENDER_CTX *) NULL); if (errorcode) { sprintf(errmsg, "B-SAFE error %d occurred in Verify", errorcode); errorcode = OSPC_ERR_CRYPTO_IMPLEMENTATION_SPECIFIC_ERROR; OSPM_DBGERRORLOG(errorcode, errmsg); } } if (errorcode == 0) { errorcode = B_VerifyFinal(rsaVerifier, ospvSignature, ospvSignatureLength, (B_ALGORITHM_OBJ) NULL, (A_SURRENDER_CTX *) NULL); if (errorcode == BE_SIGNATURE) { errorcode = OSPC_ERR_CRYPTO_SIGNATURE_DID_NOT_VERIFY; } else if (errorcode) { sprintf(errmsg, "B-SAFE error %d occurred in Verify", errorcode); errorcode = OSPC_ERR_CRYPTO_IMPLEMENTATION_SPECIFIC_ERROR; OSPM_DBGERRORLOG(errorcode, errmsg); } } B_DestroyAlgorithmObject(&rsaVerifier); B_DestroyKeyObject(&readerKey); return errorcode;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -