Развернуть все
Свернуть все

Добавление усовершенствованной подписи

Пример добавления подписи в существующее сообщение с помощью низкоуровневых функций КриптоПро ЭЦП SDK

C++
    HCRYPTMSG hMsg = CryptMsgOpenToDecode(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,0,0,0,0,0);
    if(!hMsg)
    {
        CertFreeCertificateContext(pCertContext);
        CryptReleaseContext(hProv,0);
        std::cout << "CryptMsgOpenToDecode() failed" << std::endl;
        return empty;
    }

    if(!CryptMsgUpdate(hMsg,&message[0],(DWORD)message.size(),TRUE))
    {
        CertFreeCertificateContext(pCertContext);
        CryptReleaseContext(hProv,0);
        CryptMsgClose(hMsg);
        std::cout << "CryptMsgUpdate() failed" << std::endl;
        return empty;
    }

    CMSG_SIGNER_ENCODE_INFO signer = { sizeof(CMSG_SIGNER_ENCODE_INFO) };
    signer.pCertInfo = pCertContext->pCertInfo;
    signer.hCryptProv = hProv;
    signer.dwKeySpec = dwKeySpec;
    signer.HashAlgorithm.pszObjId = szOID_OIWSEC_sha1;

    CADES_SIGN_PARA signPara = { sizeof(signPara) };
    signPara.dwCadesType = CADES_X_LONG_TYPE_1;

    CADES_COSIGN_PARA cosignPara = { sizeof(cosignPara) };
    cosignPara.pSigner = &signer;
    cosignPara.pCadesSignPara = &signPara;

    if(!CadesMsgAddEnhancedSignature(hMsg,&cosignPara))
    {
        CertFreeCertificateContext(pCertContext);
        CryptReleaseContext(hProv,0);
        CryptMsgClose(hMsg);
        std::cout << "CadesMsgAddEnhanceSignature() failed" << std::endl;
        return empty;
    }

    DWORD size = 0;
    if(!CryptMsgGetParam(hMsg,CMSG_ENCODED_MESSAGE,0,0,&size))
    {
        CryptReleaseContext(hProv,0);
        CertFreeCertificateContext(pCertContext);
        CryptMsgClose(hMsg);
        std::cout << "CryptMsgGetParam() failed" << std::endl;
        return empty;
    }

    std::vector<BYTE> retMessage(size);
    if(!CryptMsgGetParam(hMsg,CMSG_ENCODED_MESSAGE,0,&retMessage[0],&size))
    {
        CryptReleaseContext(hProv,0);
        CertFreeCertificateContext(pCertContext);
        CryptMsgClose(hMsg);
        std::cout << "CryptMsgGetParam() failed" << std::endl;
        return empty;
    }

    if(!CryptMsgClose(hMsg))
    {
        CryptReleaseContext(hProv,0);
        CertFreeCertificateContext(pCertContext);
        std::cout << "CryptMsgClose() failed" << std::endl;
        return empty;
    }

    CryptReleaseContext(hProv,0);
    CertFreeCertificateContext(pCertContext);

    std::cout << "Enhanced signature added." << std::endl;

    return retMessage;