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

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

Пример создания подписи CAdES-BES с помощью низкоуровневых функций КриптоПро ЭЦП 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;

    // Создание подписи CAdES-BES
    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;
    }

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