前言

网上搜索出来的OpenSSL解析证书很多都是命令行工具的操作,还有一些C++实现的,用的版本的比较老,很多接口都变了。整理了基于OpenSSL1.1.1版本解析X509证书的实现。

接口

参照
QSslCertificate接口,基于标准C++实现类似的接口,不依赖Qt,支持RSA和SM2证书信息解析,解析PFX、P7B证书、解析证书链、验证书等

具体接口如下:

#ifndef X509CERTIFICATE_H
#define X509CERTIFICATE_H#include <string>
#include <vector>
#include <map>
#include <memory>#if defined(_MSC_VER) || defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#  define Q_DECL_EXPORT __declspec(dllexport)
#  define Q_DECL_IMPORT __declspec(dllimport)
#else
#  define Q_DECL_EXPORT     __attribute__((visibility("default")))
#  define Q_DECL_IMPORT     __attribute__((visibility("default")))
#endif#if defined(X509CERTIFICATE_LIBRARY)
#  define X509CERTIFICATE_EXPORT Q_DECL_EXPORT
#else
#  define X509CERTIFICATE_EXPORT Q_DECL_IMPORT
#endifusing namespace std;class X509Certificate_p;
class X509CertificateExtension;
class X509CertificateExtension_p;
class X509CERTIFICATE_EXPORT X509Certificate
{
public:X509Certificate();X509Certificate(const char *cert, int len);X509Certificate(const X509Certificate &other);enum HashType {Hash_Sha1,Hash_Sha256,Hash_SM3};enum SubjectInfo {Organization,CommonName,LocalityName,OrganizationalUnitName,CountryName,StateOrProvinceName,DistinguishedNameQualifier,SerialNumber,EmailAddress};X509Certificate &operator=(const X509Certificate &other);bool operator==(const X509Certificate &other) const;inline bool operator!=(const X509Certificate &other) const { return !operator==(other); }bool isNull() const;void *handle() const;int version() const;string serialNumber() const;string subject() const;string subjectInfo(SubjectInfo subject) const;string subjectDisplyName() const;string issuer() const;string issuerInfo(SubjectInfo issuer) const;string issuerDisplyName() const;string notBefor() const;string notAfter() const;string digest(HashType type) const;string publicKeyValue() const;string publicKeyType() const;string signAlgType() const;string signValue() const;vector<X509CertificateExtension> extensions() const;string toDer() const;string toPem() const;string toText() const;static bool importPkcs12(const char *pfxFile, int len, char *priKey, int &priKeyLen,X509Certificate *x509Cert, vector<X509Certificate> &caCerts,const char *pass = "");static bool importP7b(const char *p7b, int len, vector<X509Certificate> &caCerts);static vector<X509Certificate *> splitCertChain(const string &chains);//only pemstatic int verify(const X509Certificate &userCert, vector<X509Certificate> certificateChain);static vector<X509Certificate> systemCaCertificates();
private:shared_ptr<X509Certificate_p> p;friend class X509Certificate_p;
};class X509CERTIFICATE_EXPORT X509CertificateExtension
{
public:X509CertificateExtension();enum ExtMethod {Ext_String,Ext_Vector,Ext_Map};bool isCritical() const;bool isSupported() const;string name() const;string oid() const;string value() const;ExtMethod methodType() const;string toString() const;vector<string> toVector() const;multimap<string, string> toMap() const;private:X509CertificateExtension_p *p = nullptr;friend class X509Certificate;
};#endif // X509CERTIFICATE_H

具体的代码实放在github上,这里(顺便求个Start),整个项目包含一个测试接口的工程和生成共享库的工程,都是Qt工程,当然实现的源码是不依赖Qt,构建其他工程也是可以的。

OpenSSL依赖版本Windows下已提供,默认使用的是静态链接,因为OpenSSL实在是太庞大了,X509只是其中很小的一部分,静态链接的方式会好些,Windows是因为有现成的库,所以采用静态链接的方式。Linux下的需要安装libssl-dev包,采用动态链接的方式,要用静态链接得自己编译了。测试了Windows和Linux下都是能够正常使用的,理论上只要OpenSSL能够使用,其他平台都能够使用,这就需要另外去测试了。

整个接口构造参照Qt的p指针和q指针的方式,即实现的类和接口类是分开的,希望能满足C++的二进制兼容,具体并没有去测试。

结语

最后,自己水平有限,不论是代码结构还是代码实现,其中或多或少都会有问题,欢迎各位大佬指正

参考资料:

1.OpenSSL Certificate Parsing

2.Qt QSslCertificate源码

OpenSSL解析X509证书相关推荐

  1. 通过OpenSSL解析X509证书基本项

    在之前的文章"通过OpenSSL解码X509证书文件"里,讲述了如何使用OpenSSL将证书文件解码,得到证书上下文结构体X509的方法.下面我们接着讲述如何通过证书上下文结构体X ...

  2. 【openssl】利用openssl完成X509证书和PFX证书之间的互转

    利用openssl完成X509证书和PFX证书之间的互转 # OpenSSL的下载与安装: 1.下载地址: 官方网址-- https://www.openssl.org/source/ OpenSSL ...

  3. 利用openssl生成X509证书

    利用openssl生成X509证书 1.生成密钥 openssl genrsa -out test.key 2048 2.生成cert证书(sha512/sha256/sha1要根据实际算法) ope ...

  4. java解码p7b证书文件,通过OpenSSL解码X509证书文件

    在Windows平台下,如果要解析一个X509证书文件,最直接的办法是使用微软的CryptoAPI.但是在非Windows平台下,就只能使用强大的开源跨平台库OpenSSL了.一个X509证书通过Op ...

  5. 使用Python Openssl库解析X509证书信息

    文章目录 X.509 证书结构描述 证书数据结构 源码 编译运行输出结果 参考文献 X.509 证书结构描述 常见的X.509证书格式包括: 对于常见的https证书 一般是用crt或者pem来保存, ...

  6. php x.509,php – 解析X509证书

    我想在php中解析X.509证书. 证书采用DER编码的X.509格式. 我尝试在php中使用openssl_x509_parse方法,但它不起作用. 证书数据是在mdm中为CertificateLi ...

  7. Python OpenSSL 解析证书

    openssl 介绍这里不做过多赘述,可以搜到很多相关资料.本文提供python 使用OpenSSL解析证书的方法. OpenSSL 解析certificate 证书 import OpenSSL i ...

  8. 数字证书 X509详解 python解析SSL证书

    数字证书 ​ 数字证书就是互联网通讯中标志通讯各方身份信息的一系列数据,提供了一种在Internet上验证您身份的方式,其作用类似于司机的驾驶执照或日常生活中的身份证.它是由一个由权威机构-----C ...

  9. 【Go】解析X509

    解析DER证书 //读der证书derTmp, err := ioutil.ReadFile("123.cer")if err != nil {fmt.Println(" ...

  10. openssl解析国密X509证书

    openssl解析国密X509证书,把公钥拿出来重写一下就行了         x = strToX509(pbCert, pulCertLen); dwRet = getCertPubKey(x,  ...

最新文章

  1. micropython stm32f103_micropython
  2. IOS 6.0+ Autolayout — UITableViewCell 高度调整
  3. Oracle修改监听IP地址
  4. 用户界面框架jQuery EasyUI示例大全之DataGrid(1/4)
  5. 面向过程与面向对象编程的区别和优缺点
  6. linux下remove函数
  7. SQL中的CASE WHEN用法
  8. Java 把一个InputStream转换为一个BufferedReader
  9. unity vs没有智能提示_Unity博主营地你不可不知的Unity C#代码小技巧
  10. oracle认证都需要考哪几个方面,Oracle OCP认证要通过哪些考试
  11. 23、jQuery九类选择器/jQuery常用Method-API/jQuery常用Event-API
  12. kubernetes 应用快速入门
  13. YOLO系列专题——YOLOv2理论篇
  14. Jquery设置select控件指定text的值为选中项
  15. ITOO 第一个任务,新建界面
  16. java基础回顾(一)—— sleep和wait的区别
  17. Excel连接MySQL数据库进行数据的可视化
  18. python xlsxwriter生成图片保存_Python xlsxwriter库 图表Demo
  19. xlsx表格怎么做汇总统计_excel考勤统计表汇总怎么做
  20. win7安装PS2019CC启动时报d3dcompiler_47.dll的问题解决

热门文章

  1. apache2.4.37无法解析php,编译安装apache2.4.37(Server version: Apache/2.4.37 )
  2. start request repeated too quickly for docker.service
  3. 如何用Matlab修正异方差性,matlab 异方差 white
  4. wpa_supplicant使用方法(WiFi工具)
  5. windows服务器系统和专业版差别,Win10专业版和企业版哪个好?教你区分win10企业版和专业版...
  6. SSD网络及代码理解
  7. Centos7.X安装mariadb及卸载mariadb安装mysql方法
  8. TwinCAT 3 轴程序
  9. iconfont 图标不生效
  10. 教育培训教师说课通用PPT模板