Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (DWORD) verifyEnhancedDetachedSignature:(const char *)signatureFilename ofInputFile:(const char *)inFilename {
- printf("\nRunning function: verifyEnhancedDetachedSignature\n");
- memset(&verficationPara, 0, sizeof(CADES_VERIFICATION_PARA));
- if (!inFilename || !signatureFilename) {
- return [self handleError:"No input file was specified" errorCode:ERROR_NO_INPUT_FILE];
- }
- tbs = fopen (inFilename, "rb");
- if (!tbs) {
- return [self handleError:"Cannot open message file" errorCode:ERROR_CANT_OPEN_MESSAGE_FILE];
- }
- signature = fopen (signatureFilename, "rb");
- if (!signature) {
- return [self handleError:"Cannot open signature file" errorCode:ERROR_CANT_OPEN_SIGNATURE_FILE];
- }
- tbs_len = 0;
- while (!feof(tbs)) {
- int r = 0;
- BYTE tmp[1024];
- r = fread (tmp, 1, 1024, tbs);
- tbs_mem = (BYTE *)realloc(tbs_mem, tbs_len+r);
- memcpy (&tbs_mem[tbs_len], tmp, r);
- tbs_len += r;
- }
- fclose (tbs);
- tbs = NULL;
- if (signature) {
- signature_len = 0;
- while (!feof(signature)) {
- int r = 0;
- BYTE tmp[1024];
- r = fread (tmp, 1, 1024, signature);
- signature_mem = (BYTE *)realloc (signature_mem, signature_len+r);
- memcpy (&signature_mem [signature_len], tmp, r);
- signature_len += r;
- }
- fclose (signature);
- signature = NULL;
- }
- DWORD dwSigners = CryptGetMessageSignerCount(TYPE_DER, signature_mem, signature_len);
- printf("Count of signers: %d\n",dwSigners);
- //--------------------------------------------------------------------
- // Open a message for decoding.
- if (hMsg) CryptMsgClose(hMsg);
- if (!(hMsg = CryptMsgOpenToDecode(
- TYPE_DER, // Encoding type.
- CMSG_DETACHED_FLAG, // Flags.
- 0, // Use the default message type.
- hCryptProv, // Cryptographic provider.
- NULL, // Recipient information.
- NULL))){ // Stream information.
- return [self handleError:"CryptMsgOpenToDecode() failed" errorCode:0];
- }
- printf("The message to decode is open. \n");
- //--------------------------------------------------------------------
- // Update the message with an encoded blob.
- // Both pbEncodedBlob, the encoded data,
- // and cbEnclodeBlob, the length of the encoded data,
- // must be available.
- if (!CryptMsgUpdate(
- hMsg, // Handle to the message
- signature_mem, // Pointer to the encoded blob
- signature_len, // Size of the encoded blob
- TRUE)){ // Last call)
- return [self handleError:"CryptMsgUpdate() signature failed" errorCode:0];
- }
- if (CryptMsgUpdate(
- hMsg, // Handle to the message
- tbs_mem, // Pointer to the encoded blob
- tbs_len, // Size of the encoded blob
- TRUE)){ // Last call)
- }
- printf("The encoded blob has been added to the message. \n");
- //--------------------------------------------------------------------
- // Get the size of the content.
- if (!(ret = CryptMsgGetParam(
- hMsg, // Handle to the message
- CMSG_CONTENT_PARAM, // Parameter type
- 0, // Index
- NULL, // Address for returned info
- &cbDecoded))){ // Size of the returned info
- return [self handleError:"CryptMsgGetParam() CMSG_CONTENT_PARAM failed" errorCode:0];
- }
- printf("The message parameter (CMSG_CONTENT_PARAM) has been acquired. Message size: %d\n", cbDecoded);
- //--------------------------------------------------------------------
- // Allocate memory.
- pbDecoded = (BYTE *) malloc(cbDecoded);
- if (!pbDecoded){
- return [self handleError:"Decode memory allocation failed" errorCode:ERROR_MEMORY_ALLOCATION_FAILED];
- }
- //--------------------------------------------------------------------
- // Get a pointer to the content.
- if (!(ret = CryptMsgGetParam(
- hMsg, // Handle to the message
- CMSG_CONTENT_PARAM, // Parameter type
- 0, // Index
- pbDecoded, // Address for returned
- &cbDecoded))){ // Size of the returned
- return [self handleError:"Decode CMSG_CONTENT_PARAM #2 failed" errorCode:0];
- }
- printf("The message param (CMSG_CONTENT_PARAM) updated. Length is %lu.\n",(unsigned long)cbDecoded);
- //--------------------------------------------------------------------
- // Verify the signature.
- // First, get the signer CERT_INFO from the message.
- //--------------------------------------------------------------------
- // Get the size of memory required.
- if (!pUserCert) {
- if (!(ret = CryptMsgGetParam(
- hMsg, // Handle to the message
- CMSG_SIGNER_CERT_INFO_PARAM, // Parameter type
- 0, // Index
- NULL, // Address for returned
- &cbSignerCertInfo))){ // Size of the returned
- return [self handleError:"No user certificate found in message." errorCode:0];
- }
- printf("Try to get user cert. OK. Length %d.\n",cbSignerCertInfo);
- }
- if (pUserCert) {
- hCertStore = CertOpenStore(CERT_STORE_PROV_MEMORY, TYPE_DER, 0, CERT_STORE_CREATE_NEW_FLAG,NULL);
- if (!hCertStore){
- return [self handleError:"Cannot create temporary store in memory." errorCode:0];
- }
- if (pUserCert) {
- ret = CertAddCertificateContextToStore(hCertStore, pUserCert, CERT_STORE_ADD_ALWAYS, NULL);
- pSignerCertInfo = pUserCert->pCertInfo;
- }
- else
- ret = 0;
- if (!ret){
- return [self handleError:"Cannot add user certificate to store." errorCode:0];
- }
- }
- //--------------------------------------------------------------------
- // Allocate memory.
- if (!pUserCert) {
- pSignerCertInfo = (PCERT_INFO) malloc(cbSignerCertInfo);
- if (!pSignerCertInfo){
- return [self handleError:"Verify memory allocation failed" errorCode:ERROR_MEMORY_ALLOCATION_FAILED];
- }
- }
- //--------------------------------------------------------------------
- // Get the message certificate information (CERT_INFO
- // structure).
- if (! pUserCert) {
- if (!(ret = CryptMsgGetParam(
- hMsg, // Handle to the message
- CMSG_SIGNER_CERT_INFO_PARAM, // Parameter type
- 0, // Index
- pSignerCertInfo, // Address for returned
- &cbSignerCertInfo))){ // Size of the returned
- return [self handleError:"Verify SIGNER_CERT_INFO #2 failed" errorCode:0];
- }
- printf("The signer info has been returned. \n");
- }
- //--------------------------------------------------------------------
- // Open a certificate store in memory using CERT_STORE_PROV_MSG,
- // which initializes it with the certificates from the message.
- if (! hCertStore) {
- hCertStore = CertOpenStore(
- CERT_STORE_PROV_MSG, // Store provider type
- TYPE_DER, // Encoding type
- hCryptProv, // Cryptographic provider
- 0, // Flags
- hMsg); // Handle to the message
- if (hCertStore)
- printf("The message certificate store be used for verifying\n");
- }
- if (! hCertStore) {
- return [self handleError:"Cannot open certificate store form message\n" errorCode:0];
- }
- //--------------------------------------------------------------------
- // Find the signer's certificate in the store.
- if((pSignerCertContext = CertGetSubjectCertificateFromStore(
- hCertStore, // Handle to store
- TYPE_DER, // Encoding type
- pSignerCertInfo))){ // Pointer to retrieved CERT_CONTEXT{
- DWORD errCode = 0;
- DWORD err;
- printf("A signer certificate has been retrieved. \n");
- err=VerifyCertificate(pSignerCertContext,&errCode);
- if (err){
- printf("Subject cert verification failed: err=%x\n",err);
- return [self handleError:"Subject cert verification failed" errorCode:err];
- }
- if (errCode){
- printf("Subject cert BAD: errCode=%x\n",errCode);
- return [self handleError:"Subject cert BAD" errorCode:errCode];
- }
- } else {
- return [self handleError:"Verify GetSubjectCert failed" errorCode:0];
- }
- //--------------------------------------------------------------------
- // Use the CERT_INFO from the signer certificate to verify
- // the signature.
- for( DWORD i = 0; i < dwSigners; i++) {
- verficationPara.dwCadesType = CADES_X_LONG_TYPE_1;
- pInfo = NULL;
- if (!CadesMsgVerifySignature(hMsg, i, &verficationPara, &pInfo)) {
- [self handleError:"CadesMsgVerifySignature() (enhanced) failed" errorCode:0];
- }
- if (pInfo->dwStatus == CADES_VERIFY_SUCCESS) {
- printf("Enhanced Signature №%d was VERIFIED.\n",i);
- ret = 0;
- } else {
- printf("The enhansed signature was NOT VEIFIED.\n");
- printf("Status: %x.\n", pInfo->dwStatus);
- CSP_BOOL bResult = false;
- if (!CadesMsgIsType(hMsg, i, CADES_BES, &bResult)) {
- return [self handleError:"CadesMsgIsType() failed" errorCode:0];
- }
- if (bResult == false) {
- return [self handleError:"Message is not Cades-BES message" errorCode:0];
- }
- verficationPara.dwCadesType = CADES_BES;
- pInfo = NULL;
- if (!CadesMsgVerifySignature(hMsg, i, &verficationPara, &pInfo)){
- [self handleError:"CadesMsgVerifySignature() failed" errorCode:0];
- }
- if (pInfo->dwStatus == CADES_VERIFY_SUCCESS || pInfo->dwStatus == CADES_VERIFY_NO_VALID_SIGNATURE_TIMESTAMP || pInfo->dwStatus == CADES_VERIFY_NO_VALID_CADES_C_TIMESTAMP) {
- printf("Signature №%d was VERIFIED.\n",i);
- printf("Status: %x.\n", pInfo->dwStatus);
- ret = 0x80070490L;
- } else {
- printf("Signature №%d was NOT VEIFIED.\n",i);
- printf("Status: %x.\n", pInfo->dwStatus);
- return [self handleError:"The signature was NOT VEIFIED.\n" errorCode:0];
- }
- }
- }
- printf("All signatures was VEIFIED.\n");
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement