将证书转换为字节数组 [英] Convert certificate to byte array

查看:306
本文介绍了将证书转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to convert a certificate(PEM/DER format) into byte array?

我没有我的设备上的文件系统和要使用的客户端证书就可以了。所以我想这个SSL证书复制到缓冲区(无符号字符)。我有我的Windows机器上的证书文件。

I don't have file system on my device and want to use the client certificate on it. So I want to copy this SSL certificate into a buffer(unsigned char). I have certificate file on my windows machine.

什么是证书转换成数组的正确方法?简单的字符复制是否行得通呢?

What is the right way to convert the certificate into array? Simple character copy will work?

维沙尔ñ

推荐答案

当你使用gcc + GNU-的binutils + OpenSSL的,你可以使用的 LD 包括文字到程序的文件。然后使用 d2i_X509 字面解析为X509的结构。

When you use gcc+gnu-binutils+openssl, you can use ld to include a file literal into the program. Then you use d2i_X509 to parse the literal into a X509 structure.

首先运行 LD -r -b二进制-o cert.crt.o cert.crt (cert.crt必须是DER形式,我不知道.CRT是DER正确的扩展名)。

First run ld -r -b binary -o cert.crt.o cert.crt (cert.crt MUST be in DER form, I don't know if .crt is the correct extension for DER).

example.c

example.c

#include <openssl/x509.h>
#include <stdio.h>

extern unsigned char _binary_cert_crt_start[];
extern unsigned char _binary_cert_crt_end[];

int main()
{
   X509 *cert;
   const unsigned char *buf = _binary_cert_crt_start;
   unsigned const char** inp = &buf;
   cert = d2i_X509(NULL, inp, _binary_cert_crt_end-_binary_cert_crt_start);
   printf("%p\n", cert);
   return !cert;
}

然后编译这个程序与的gcc -o克拉example.c cert.crt.o -lcrypto

这篇关于将证书转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆