简体   繁体   English

unique_ptr和OpenSSL的STACK_OF(X509)*

[英]unique_ptr and OpenSSL's STACK_OF(X509)*

I use some using statements and unique_ptr to work with OpenSSL, as suggested in another question . 另一个问题中所述 ,我使用一些using语句和unique_ptr与OpenSSL一起使用。 Without, code becomes really ugly and I am not so much a fan of goto statements. 没有它,代码将变得非常丑陋,我不是goto语句的忠实拥护者。

So far I have changed my code as far as possible. 到目前为止,我已尽可能地更改了代码。 Here are examples, what I use: 这是我使用的示例:

using BIO_ptr = std::unique_ptr<BIO, decltype(&::BIO_free)>;
using X509_ptr = std::unique_ptr<X509, decltype(&::X509_free)>;
using EVP_PKEY_ptr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>;
using PKCS7_ptr = std::unique_ptr<PKCS7, decltype(&::PKCS7_free)>;
...

BIO_ptr tbio(BIO_new_file(some_filename, "r"), ::BIO_free);

Now I have the need of a STACK_OF(X509) and I do not know, if this is also possible with unique_ptr . 现在我需要一个STACK_OF(X509) ,我不知道,是否也可以使用unique_ptr I am looking for something similar to below, but this is not working. 我正在寻找与下面类似的东西,但这不起作用。

using STACK_OF_X509_ptr = std::unique_ptr<STACK_OF(X509), decltype(&::sk_X509_free)>;

I also tried the Functor: 我也尝试过Functor:

struct StackX509Deleter {
    void operator()(STACK_OF(X509) *ptr) {
        sk_X509_free(ptr);
    }
};

using STACK_OF_X509_ptr = std::unique_ptr<STACK_OF(X509), StackX509Deleter>;

STACK_OF_X509_ptr chain(loadIntermediate(cert.string()));

The compiler accepts this and the application runs. 编译器接受这一点,然后应用程序运行。 Just one question: In other unique_ptrs as shown above, I always had specified a second argument, so I bet I am missing something: 只是一个问题:在上面显示的其他unique_ptrs中,我总是指定了第二个参数,所以我敢打赌我错过了一些东西:

STACK_OF_X509_ptr chain(loadIntermediate(cert.string()),  ??????);

How do I use C++ unique_ptr and OpenSSL's STACK_OF(X509)* ? 如何使用C ++ unique_ptr和OpenSSL的STACK_OF(X509)*

I defined a regular function: 我定义了一个常规函数:

void stackOfX509Deleter(STACK_OF(X509) *ptr) {
    sk_X509_free(ptr);
}

Then I use it in my code: 然后在代码中使用它:

using STACK_OF_X509_ptr = std::unique_ptr<STACK_OF(X509),
    decltype(&stackOfX509Deleter)>;

STACK_OF_X509_ptr chain(loadIntermediate(cert.string()),
                    stackOfX509Deleter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM