C++ 将SSL证书字符串转换为有效的X509\u存储\u CTX

C++ 将SSL证书字符串转换为有效的X509\u存储\u CTX,c++,security,ssl,openssl,C++,Security,Ssl,Openssl,我试图将这两个证书都放入X509_STORE_CTX,但当我读取它们时,它们都为空。有什么想法吗 证书看起来像: // Not the real certs. Just trying to illustrate that the certs are just a new line // delimited string const char *certA = "-----BEGIN CERTIFICATE-----\nMIIGWDCCBUCgAwI......\n.....\n" SSL_li

我试图将这两个证书都放入X509_STORE_CTX,但当我读取它们时,它们都为空。有什么想法吗

证书看起来像:

// Not the real certs. Just trying to illustrate that the certs are just a new line
// delimited string
const char *certA = "-----BEGIN CERTIFICATE-----\nMIIGWDCCBUCgAwI......\n.....\n"

SSL_library_init();
SSL_CTX * sslCtx = SSL_CTX_new(SSLv23_client_method());
X509_STORE *store = SSL_CTX_get_cert_store(sslCtx);
X509_STORE_CTX *store_ctx = X509_STORE_CTX_new();

BIO *bio;
X509 *certificate;

/*First cert*/
bio = BIO_new(BIO_s_mem());
BIO_write(bio,(const void*)certA ,sizeof(certA));
certificate = PEM_read_bio_X509(bio, NULL, NULL, NULL);
X509_STORE_add_cert(store, certificate);

/*second cert*/
bio = BIO_new(BIO_s_mem());
BIO_write(bio,(const void*)certB ,sizeof(certB));
certificate = PEM_read_bio_X509(bio, NULL, NULL, NULL);
X509_STORE_add_cert(store, certificate);

X509_STORE_CTX_init(store_ctx, store, NULL, NULL);
sizeofcertA在这里只提供const char*变量的大小,这是指针的大小,主要是4或8

请尝试将证书内容声明为static const char certA[]

另外,完全使用和避免sizeof可能更容易。

sizeofcertA这里只提供const char*变量的大小,这是指针的大小,主要是4或8

请尝试将证书内容声明为static const char certA[]


另外,完全使用和避免sizeof可能更容易。

我这样做了,但当我得到STACK\u of x509*stk=store\u ctx->untrusted;stk是空的。所以在PEM_read_bio_X509之后,变量证书现在至少不再是空的了?现在我还看到,在不使用任何sizeofs或其他gotchas的情况下使用BIO_put可能会更容易。store_ctx->untrusted会不会为NULL,因为您正在将您的store作为第二个参数传递给X509_store_ctx_init,根据文档,它是用于受信任的证书存储的?另外,字段untrusted的源代码注释表示这只是输入,可能是从第三个参数到init函数的输入;stk是空的。所以在PEM_read_bio_X509之后,变量证书现在至少不再是空的了?现在我还看到,在不使用任何sizeofs或其他gotchas的情况下使用BIO_put可能会更容易。store_ctx->untrusted会不会为NULL,因为您正在将您的store作为第二个参数传递给X509_store_ctx_init,根据文档,它是用于受信任的证书存储的?另外,字段untrusted的源代码注释表示这只是输入,可能是从第3个参数到init函数的输入。您无法将X509证书转换为X509_STORE_CTX。你到底想干什么?此问题向您展示了如何通过X509_STORE_CTX验证证书:。不要让问题的标题欺骗你。你不能将X509证书转换为X509\u STORE\u CTX。你到底想干什么?此问题向您展示了如何通过X509_STORE_CTX验证证书:。不要让问题的标题欺骗你。