Search code examples
c++opensslomnet++veinsecdsa

Veins simulation terminates calling openssl ECDSA_SIG_get0 function


I am using the virtual machine instant-veins-4-7-1-i1 on VirtualBox with Omnet++-5.3 and Sumo-0.32.0.

I have installed the library openssl version 1.1.0. When I try to access to the struct ECDSA_SIG that store the signature with the function ECDSA_SIG_get0 the simulation terminate suddenly with the following error

enter image description here

This is a snippet of the code that generate the error:

ECDSA_SIG * signed_hash;
s->generateSignature(message, messageLength, signed_hash);
const BIGNUM **pr;
const BIGNUM **ps;
ECDSA_SIG_get0(signed_hash, pr, ps);

The generateSignature function code is:

void SignatureOpenSSL::generateSignature(const unsigned char* message, int messageLength, ECDSA_SIG * signed_hash)
{

    unsigned char *md;
    unsigned char *hash;
    hash = SHA256(message, messageLength, md);

    // Computes the ECDSA signature of the given message using the supplied private key and returns the created signature
    signed_hash = ECDSA_do_sign(hash, 32, eckey);

    if (signed_hash == NULL){
        std::cout <<" ko signature " << std::endl;
    }else{
        std::cout <<" ok signature" << std::endl;
    }

}

I have upgraded the openssl library to version 1.1.1 but the error keeps occurring.

What am I doing wrong?

Thanks


Solution

  • Your problem has nothing to do with openssl, but your "C" code and your misuse of pointers.

    Your problem line is:

    signed_hash = ECDSA_do_sign(hash, 32, eckey);
    

    Your code assumes it's changing the pointer in the calling function, it is not. It's only changing the "copy" of the pointer. You want to either return the pointer or pass in a pointer to a pointer and set it that way.

    e.g.

    ECDSA_SIG *SignatureOpenSSL::generateSignature(const unsigned char* message, int messageLength)
    {
        ...
        ECDSA_SIG *  signed_hash = ECDSA_do_sign(hash, 32, eckey);
        ...
        return signed_hash;
    }
    
    
    ECDSA_SIG * signed_hash = s->generateSignature(message, messageLength);
    

    or

    void SignatureOpenSSL::generateSignature(const unsigned char* message, int messageLength, ECDSA_SIG ** signed_hash)
    {
        ...
        *signed_hash = ECDSA_do_sign(hash, 32, eckey);
    
        if (*signed_hash == NULL){
            std::cout <<" ko signature " << std::endl;
        }else{
            std::cout <<" ok signature" << std::endl;
        }
    }
    
    ECDSA_SIG * signed_hash;
    s->generateSignature(message, messageLength, &signed_hash);