将SSL证书字符串转换为有效的X509_STORE_CTX

时间:2014-04-17 00:06:47

标签: c++ security ssl openssl

我正在尝试将这两个证书都放到X509_STORE_CTX中,但是当我去读取它们时,它们都是NULL。有什么想法吗?

证书看起来像:

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

1 个答案:

答案 0 :(得分:4)

sizeof(certA)这里只提供const char*变量的大小,这是指针的大小(大多数是4或8)。

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

同样使用BIO_puts()并完全避开sizeof()可能会更容易。