Arno Duvenhage
Arno Duvenhage

Reputation: 1960

using unique_ptr<SSL> with OpenSSL v1.1.0

I have some code that manages SSL and SSL_CTX pointers with unique_ptr.

The following code compiles with OpenSSL 1.0.2, but not with OpenSSL 1.1.1:

std::unique_ptr<SSL>    m_pSession

I include 'openssl/ssl.h', but with OpenSSL 1.1.1 I get the following compile error (using Visual Studio):

error C2027: use of undefined type 'ssl_st'

I have googled a little and it seems that the later version of OpenSSL does not provide the real declaration of ssl_st anywhere? What would be the solution to this?

Upvotes: 3

Views: 1354

Answers (1)

Arno Duvenhage
Arno Duvenhage

Reputation: 1960

With the help of the OpenSSL forum I got to the following solution.

Defined a custom deleter for unique_ptr:

struct SslDeleter {
    void operator()(SSL *_p)
    {
        SSL_shutdown(_p);
        SSL_free(_p);
    }

    void operator()(SSL_CTX *_p)
    {
        SSL_CTX_free(_p);
    }
};

Use a typedef to easily work with the smart pointers:

using UniqueSslPtr = std::unique_ptr<SSL, SslDeleter>;

and

using UniqueCtxPtr = std::unique_ptr<SSL_CTX, SslDeleter>;

The custom deleter works for SSL and SSL_CTX and should also work for shared_ptr.

Upvotes: 3

Related Questions