为什么我的 OpenSSL/libcrypto 签名不正确?

Why is my OpenSSL/libcrypto signature incorrect?

我需要签署一个字符串,将 public 密钥作为字符串发布,然后在其他地方使用 public 密钥来验证已签名的消息。这是消息签名的部分:

        // RSA keypair generation
        EVP_PKEY *keypair = NULL;
        EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
        if (1 != EVP_PKEY_keygen_init(ctx)) {
            initFail = true;
        }
        if (1 != EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048)) {
            initFail = true;
        }
        if (1 != EVP_PKEY_keygen(ctx, &keypair)) {
            initFail = true;
        }
        EVP_PKEY_CTX_free(ctx);


        // Create public key string.
        BIO* bo = BIO_new(BIO_s_mem());
        PEM_write_bio_PUBKEY(bo, keypair);
        char buff[1000];
        BIO_read(bo, &buff[0], 1000);
        BIO_free(bo);
        pubkey = buff;


        // Create signature
        size_t *slen = new size_t;
        unsigned char *sig = NULL;
        std::string msg;
        msg = stuffThatCreatesMessage();
        EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
        if (1 != EVP_DigestSignInit(mdctx, NULL, EVP_sha256(), 
                    NULL, keypair)) {
            initFail = true;
        }
        if (1 != EVP_DigestSignUpdate(mdctx, msg.c_str(), msg.length())) {
            initFail = true;
        }
        if (1 != EVP_DigestSignFinal(mdctx, NULL, slen)) {
            initFail = true;
        }
        sig = (unsigned char *) OPENSSL_malloc(
                sizeof(unsigned char) * (*slen));
        if (1 != EVP_DigestSignFinal(mdctx, sig, slen)) {
            initFail = true;
        }
        signature = *sig;
        OPENSSL_free(sig);

        bool isSuccess = verifySignature(signature, pubkey, msg);

这是获取字符串消息和密钥并实际验证签名的代码:

bool verifySignature(std::string sig, std::string key_str, std::string msg) {
    BIO* bo = BIO_new(BIO_s_mem());
    BIO_write(bo, key_str.c_str(), key_str.length());
    EVP_PKEY *key = EVP_PKEY_new();
    PEM_read_bio_PUBKEY(bo, &key, 0, 0);
    BIO_free(bo);

    unsigned char *unsigned_sig = new unsigned char[sig.length()+1];
    strcpy((char *) unsigned_sig, sig.c_str());
    EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
    if (1 != EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, key))    {  
        return false;
    }
    if (1 != EVP_DigestVerifyUpdate(mdctx, msg.c_str(), msg.length())) {
        return false;
    }
    bool retval = (1 == EVP_DigestVerifyFinal(mdctx, unsigned_sig, 
                sig.length()));
    delete unsigned_sig;
    return retval;
}

每次我这样做,后一个函数告诉我签名无效,isSuccess变量等于0。这是因为EVP_DigestSignFinal返回0,表明签名是不正确。我哪里错了?

@jww 指出的问题是行 signature = *sig。我最终只有第一个字节作为我的签名字符串。正确的做法是:

std::string temp_sig = reinterpret_cast<char*>(sig);
signature = temp_sig.substr(0, *slen);