3

我有一个 509* 格式的证书,我想将其序列化为一个字符缓冲区,然后再将其反序列化以再次恢复证书 509*。

我这样做是为了序列化:

int size_cert = 0;
unsigned char* data;
BIO* bio = BIO_new(BIO_s_mem());
PEM_write_bio_X509(bio,certificate);
size_cert = BIO_get_mem_data(bio, &data);
BIO_free(bio);

哪里 data 应该有证书数据!

要从数据缓冲区重建 X509* 证书,我正在这样做:

    BIO* bio;
    X509* cert;

    bio = BIO_new(BIO_s_mem());
    BIO_puts(bio, data);
    cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);    

cert 应该在哪里获得证书。这不能正常工作,有人可以给我一个很好的例子吗?

4

1 回答 1

1

我已经用下面的代码完成了,

1 使用BIO_read_filename将证书加载到 BIO

2 使用PEM_read_bio_X509_AUX将其转换为 x509

3 使用i2d_X509将其转换为 unsigned char*

4 使用d2i_X509从 unsigned char* 重建 x509

int main()
{   
    X509 *x509,*x509ser;    
    BIO *certBio = BIO_new(BIO_s_file());   
    char * path = "E:\\share\TempCert.pem"; // certificate path
    int len;
    unsigned char *buf;
    buf = NULL;

    BIO_read_filename(certBio, path); // reading certificate to bio
    x509 = PEM_read_bio_X509_AUX(certBio, NULL, 0, NULL);  //converting to x509  
    len = i2d_X509(x509, &buf);  // converting to unsigned char*
    x509ser = d2i_X509(NULL, &buf, len); // converting back to x509 from unsigned char*

    BIO_free_all(certBio); 
    return 0;
} 
于 2017-03-02T06:46:14.813 回答