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

Создание подписи (низкоуровневые функции)

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

C++
    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;

    CMSG_SIGNED_ENCODE_INFO info = { sizeof(CMSG_SIGNED_ENCODE_INFO) };
    info.cSigners = 1;
    info.rgSigners = &signer;

    CADES_ENCODE_INFO cadesInfo = { sizeof(cadesInfo) };
    cadesInfo.pSignedEncodeInfo = &info;

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

    std::vector<BYTE> data(10,25);

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

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

    std::vector<BYTE> message(size);
    if(!CryptMsgGetParam(hMsg,CMSG_CONTENT_PARAM,0,&message[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 << "CryptMsgGetParam() failed" << std::endl;
        return empty;
    }

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

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

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

    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;
    }

    message.resize(size);
    if(!CryptMsgGetParam(hMsg,CMSG_ENCODED_MESSAGE,0,&message[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);
    return message;