将证书转换为字节数组

标签 c ssl-certificate

如何将证书(PEM/DER格式)转换为字节数组?

我的设备上没有文件系统,但想在其上使用客户端证书。所以我想将这个 SSL 证书复制到一个缓冲区(unsigned char)中。我的 Windows 机器上有证书文件。

将证书转换为数组的正确方法是什么?简单的字符复制就可以了吗?

维沙尔 N

最佳答案

检查这段代码,解释添加为注释,

1 Load the file into BIO structure

2 Convert it to x509 using PEM_read_bio_X509_AUX

3 convert the x509 to unsigned char * using i2d_X509

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

    buf = NULL;
    BIO_read_filename(certBio, path);
    x509 = PEM_read_bio_X509_AUX(certBio, NULL, 0, NULL);      /*loading the certificate to x509 structure*/
    len = i2d_X509(x509, &buf);     /*loading the certificate to unsigned char buffer*/

    /* You can use this buf as BYTE array since BYTE is typedef of unsigned char and len will contain the length(size) of the certificate */
    BIO_free_all(certBio);  
    return 0;
} 

检查i2d_X509 , PEM_read_bio_X509_AUX功能了解更多详情。

此缓冲区可用于创建 PCCERT_CONTEXT 结构。

关于将证书转换为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4836467/

相关文章:

对字符数组初始化的困惑

c - 如何使用dll?

c - 将 char 指针强制转换为 int 指针是未定义的行为吗?

我的雇主为 gmail、twitter、facebook 等网站签署的 ssl 证书

security - 调查一个主要站点上的不安全证书警告

iphone - 在 UIWebView 中加载 HTTPS

c - 如何编写一个简单的 Linux 设备驱动程序?

c - 在 C99 中将运算符作为参数传递

linux - 通过电子邮件发送 SSL 证书是否安全?

ssl - 如何为 Go 服务器应用程序设置 Let's Encrypt