jeudi 17 mars 2022

Is it necessary to use OPENSSL_malloc for OpenSSL function parameters?

Most OpenSSL objects are allocated by special functions like SSL_new, BN_new, etc. You cannot use 'new' or the other modern C++ functions to allocated those objects.

Some functions need pre-allocated byte buffers to read/write binary data. For example, the second parameter of BN_hex2bn is just a binary buffer. It is an input parameter. is it safe to pass a std::vector::data() to there?

Some functions need a writable buffer which has size enough for multiple OpenSSL object. I usually find the following examples on the Internet:

OPENSSL_OBJECT* buffer = (OPENSSL_OBJECT*)OPENSSL_malloc(sizeof(OPENSSL_OBJECT) * number);
A_FUNCTION(buffer, number); // buffer is an output parameter

Although descriptions of the functions do not say anything about OPENSSL_malloc. They do not say how the buffer must be allocated.

The OpenSSL_malloc man does not say if it is necessary.

OPENSSL_malloc(), OPENSSL_realloc(), and OPENSSL_free() are like the C malloc(), realloc(), and free() functions. OPENSSL_zalloc() calls memset() to zero the memory before returning.

Can you use C++ objects like std::vector, std::unique_ptr for OpenSSL function buffers? Is there any theoretical issues?

Aucun commentaire:

Enregistrer un commentaire