how to convert the Certificate String into X509 structure.?

后端 未结 2 1063
别跟我提以往
别跟我提以往 2020-12-31 11:08

can any one tell me how to convert the string content into X509 structure . i am using openssl to read the X509 Structure.

example : certificate string



        
相关标签:
2条回答
  • 2020-12-31 11:31

    You can use this OpenSSL code snippet to load the certificate when provided as a string input:

    #include <openssl/bio.h>
    #include <openssl/x509.h>
    #include <openssl/pem.h>
    
    const unsigned char *data = 
        "-----BEGIN CERTIFICATE-----\n"
        "MIIExDCCA6ygAwIBAgIJAK0JmDc/YXWsMA0GCSqGSIb3DQEBBQUAMIGcMQswCQYD\n"
        /*...*/
        "gRQT0OIU5vXzsmhjqKoZ+dBlh1FpSOX2\n"
        "-----END CERTIFICATE-----";
    
    BIO *bio;
    X509 *certificate;
    
    bio = BIO_new(BIO_s_mem());
    BIO_puts(bio, data);
    certificate = PEM_read_bio_X509(bio, NULL, NULL, NULL);
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-31 11:31

    Below code is not complete code to run. It gives what the API and how to use them:

    #include <openssl/bio.h>
    #include <openssl/x509.h>
    #include <openssl/pem.h>
    
    const unsigned char data[] = 
         "-----BEGIN CERTIFICATE-----\n"
    "MIIExDCCA6ygAwIBAgIJAK0JmDc/YXWsMA0GCSqGSIb3DQEBBQUAMIGcMQswCQYD\n"
    /*...*/
    "gRQT0OIU5vXzsmhjqKoZ+dBlh1FpSOX2\n"
    "-----END CERTIFICATE-----";
    
    BIO *bio;
    X509 *certificate;
    bio = BIO_new(BIO_s_mem());            
    // create BIO structure which deals with memory
    lTemp= BIO_write(lBio, (const void*)data, sizeof(data));  
    // Note Bio write and BIO puts do the exact thing difference is u need to give 
    // the size of input data to copy in the Bio write. (Safe and best use bio write) 
    // Check lTemp should be equal to the size of data or number of characters
    // in the data u want to copy....
    
    // these values are defined in my code not in the library
    if (iFileType == DERFORMAT)
    {
        certificate = d2i_X509_bio(bio, NULL);            
        // this line will decode the DER format certificate
        // and convert that into X509 formatted certificate.
    }
    else if (iFileType == PEMFORMAT) 
    {
        certificate = PEM_read_bio_X509(bio, NULL, 0, NULL);        
        // this line will decode the PEM certificate
        // and convert that into X509 formatted certificate.
    }        
    
    0 讨论(0)
提交回复
热议问题