如何将证书(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/