2014-04-17 41 views
1

我試圖將這兩個證書都放到X509_STORE_CTX中,但是當我讀出它們時,它們都是NULL。有任何想法嗎?將SSL證書字符串轉換爲有效的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); 
+0

您無法將X509證書轉換爲「X509_STORE_CTX」。你真的想做什麼?此問題向您顯示如何通過'X509_STORE_CTX'驗證證書:[檢查文件是證書還是密鑰](https://stackoverflow.com/questions/22398477/check-that-a-file-is-certificate - 或一個鍵)。不要讓這個問題的標題欺騙你。 – jww

回答

4

sizeof(certA)這裏將提供const char*變量,它是一個指針的大小(主要是4或8)的只是大小。

嘗試聲明證書內容爲static const char certA[]

也使用BIO_puts()和避免sizeof()完全可能會更容易。

+0

我做到了,但是當我得到「STACK_OF(X509)* stk = store_ctx-> untrusted;」 stk爲null。 – kmdent

+0

因此,在'PEM_read_bio_X509()'之後,變量'certificate'現在至少不再是NULL了? 現在我也看到,使用沒有任何sizeof()或其他陷阱的'BIO_puts()'可能會更容易。 – Yirkha

+0

由於您將'store'作爲第二個參數傳遞給'X509_STORE_CTX_init()',因此根據文檔將「store_ctx-> untrusted'設置爲NULL,這對於」可信證書存儲「 此外,字段'untrusted'的源代碼註釋表示它只是輸入,可能從第三個參數到init函數。 – Yirkha

相關問題