2012-05-24 46 views
2

我發現下面的代碼將證書安裝到本地機器可信發佈者。但代碼是在C#我想要在C中完成相同。如何將其轉換爲C?C代碼等效於C#來安裝證書

private static void InstallCertificate(string cerFileName) 
{ 
    X509Certificate2 certificate = new X509Certificate2(cerFileName); 
    X509Store store = new X509Store(StoreName.TrustedPublisher,StoreLocation.LocalMachine); 
    store.Open(OpenFlags.ReadWrite); 
    store.Add(certificate); 
    store.Close(); 
} 

是否提供任何Windows APIS?

+0

我發現這個鏈接的代碼http://stackoverflow.com/questions/566570/how-can-i-install-a-certificate-into-the -local-machine-store-programmatically-us?answertab = active#tab-top,我打算用C編程做同樣的事情。 – 2vision2

回答

4

嘗試看看libpkix lib

的libpkix庫的目的是提供一個廣泛的有用的C 庫,用於構建和驗證X.509證書鏈, 符合最新的IETF PKIX標準(即RFC 3280)。該項目旨在爲RFC 3280的所有必備功能 以及許多可選功能提供完全支持。

+0

我需要在我的代碼中使用它,它看起來像它由sunmicrosyste開發的...它是一個開源嗎? – 2vision2

+0

http://libpkix.sourceforge.net/license.txt –

2

嘗試這個例子:

#include <openssl/ssl.h> 
static int store_cert(SSL_CTX * ctx, X509 * cert) 
{ 
    X509_STORE * x509_store; 

    x509_store=SSL_CTX_get_cert_store(ctx); 

    if (X509_STORE_add_cert(x509_store, cert)==0) 
    { 
     printf("ERROR: add certificate\n"); 
     return 0; 
    } 
    return 1; 

} 
+0

SSL_CTX_get_cert_store ??這個函數是在哪裏定義的? – 2vision2

+0

SSL_CTX_get_cert_store位於libssl庫中,X509_STORE_add_cert位於libcrypto庫中 –