2011-06-28 19 views
10

का उपयोग करके प्रोग्रामेटिक रूप से पेम प्रमाणपत्र जानकारी निकालें openssl कमांड लाइन का उपयोग करना, मानव पठनीय मोड में, .pem प्रमाणपत्र में मौजूद सभी जानकारी निकालने के लिए संभव है; यह है:openssl

openssl x509 -noout -in <MyCertificate>.pem -text 

openssl API का उपयोग करके इस जानकारी को निकालने के लिए उपयुक्त कदम क्या हैं?

सादर,

+2

openssl कमांड को शामिल करने के लिए धन्यवाद, मैं इसके लिए गुगल रहा था और यहां आया था। – Andrey

उत्तर

11

कार्यों का X509_print_ex परिवार अपने जवाब है।

#include <openssl/x509.h> 
#include <openssl/pem.h> 
#include <openssl/bio.h> 

int main(int argc, char **argv) 
{ 
    X509 *x509; 
    BIO *i = BIO_new(BIO_s_file()); 
    BIO *o = BIO_new_fp(stdout,BIO_NOCLOSE); 

    if((argc < 2) || 
     (BIO_read_filename(i, argv[1]) <= 0) || 
     ((x509 = PEM_read_bio_X509_AUX(i, NULL, NULL, NULL)) == NULL)) { 
     return -1; 
    } 

    X509_print_ex(o, x509, XN_FLAG_COMPAT, X509_FLAG_COMPAT); 
} 
4

इस प्रश्न से संबंधित अतिरिक्त जानकारी के रूप में, पीईएम के बजाय डीईआर प्रारूप के साथ प्रमाण पत्र होने के मामले में; निम्नलिखित कोड का उपयोग करके मानव पठनीय मोड में जानकारी निकालना भी संभव है:

//Assuming that the DER certificate binary information is stored in 
//a byte array (unsigned char) called "pData" whose size is "lenData" 
X509* x509; 
BIO* input = BIO_new_mem_buf((void*)pData, lenData); 
//d2i_X509_bio: Decodes the binary DER certificate 
//and parses it to a X509 structure 
x509 = d2i_X509_bio(input, NULL); 
if (x509 == NULL) 
{ 
    //Error in d2i_X509_bio 
} 
else 
{ 
    //"certificateFile" is the full path file 
    //where to store the certificate information 
    //in a human readable mode (instead of stdout) 
    FILE* fd = fopen(certificateFile, "w+"); 
    BIO* output = BIO_new_fp(fd, BIO_NOCLOSE); 
    X509_print_ex(output, x509, XN_FLAG_COMPAT, X509_FLAG_COMPAT); 
    fclose(fd); 
    BIO_free_all(output); 
} 
BIO_free_all(input); 
संबंधित मुद्दे