static int pub_encode_gost94(X509_PUBKEY *pub, const EVP_PKEY *pk)
{
    ASN1_OBJECT *algobj = NULL;
    ASN1_OCTET_STRING *octet = NULL;
    void *pval = NULL;
    unsigned char *buf = NULL, *databuf, *sptr;
    int i, j, data_len, ret = 0;

    int ptype = V_ASN1_UNDEF;
    DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
    algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
    if (pk->save_parameters) {
        ASN1_STRING *params = encode_gost_algor_params(pk);
        pval = params;
        ptype = V_ASN1_SEQUENCE;
    }
    data_len = BN_num_bytes(dsa->pub_key);
    databuf = OPENSSL_malloc(data_len);
    if (databuf == NULL) {
        GOSTerr(GOST_F_PUB_ENCODE_GOST94, ERR_R_MALLOC_FAILURE);
        return 0;
    }
    BN_bn2bin(dsa->pub_key, databuf);
    octet = ASN1_OCTET_STRING_new();
    if (octet == NULL) {
        GOSTerr(GOST_F_PUB_ENCODE_GOST94, ERR_R_MALLOC_FAILURE);
        OPENSSL_free(databuf);
        return 0;
    }
    ASN1_STRING_set(octet, NULL, data_len);
    sptr = ASN1_STRING_data(octet);
    for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
        sptr[i] = databuf[j];
    }
    OPENSSL_free(databuf);
    ret = i2d_ASN1_OCTET_STRING(octet, &buf);
    ASN1_BIT_STRING_free(octet);
    if (ret < 0)
        return 0;
    return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
}