Search code examples
c++opensslcryptographylibcrypto

Why is my OpenSSL/libcrypto signature incorrect?


I need to sign a string, publish the public key as a string, and then somewhere else use the public key to verify the signed message. This is the part where the message is signed:

        // 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);

Here's the code that takes the string message and key and actually verifies the signature:

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

Every time I do this, the latter function tells me the signature is invalid, with the isSuccess variable equal to 0. It's because the EVP_DigestSignFinal is returning 0, indicating that the signature is incorrect. Where am I going wrong?


Solution

  • The problem, pointed out by @jww, was the line signature = *sig. I was ending up with only the first byte as my signature string. The correct way to do it is:

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