繁体   English   中英

unique_ptr和OpenSSL的STACK_OF(X509)*

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

另一个问题中所述 ,我使用一些using语句和unique_ptr与OpenSSL一起使用。 没有它,代码将变得非常丑陋,我不是goto语句的忠实拥护者。

到目前为止,我已尽可能地更改了代码。 这是我使用的示例:

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

现在我需要一个STACK_OF(X509) ,我不知道,是否也可以使用unique_ptr 我正在寻找与下面类似的东西,但这不起作用。

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

我也尝试过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()));

编译器接受这一点,然后应用程序运行。 只是一个问题:在上面显示的其他unique_ptrs中,我总是指定了第二个参数,所以我敢打赌我错过了一些东西:

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

如何使用C ++ unique_ptr和OpenSSL的STACK_OF(X509)*

我定义了一个常规函数:

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

然后在代码中使用它:

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