使用Openssl生成根证书CA以及签发子证书

2023-10-25 05:50

本文主要是介绍使用Openssl生成根证书CA以及签发子证书,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

openssl是当前非常流行的SSL密码库工具,如果服务器或者网站需要使用https协议,就需要使用openssl工具生成证书。

之前在Windows上有用Perl编译过OpenSSL,这次项目上要搭一个https server需要用它来生成自签名证书,其中我的配置文件在openssl/apps/openssl.cnf,编译后openssl.exe在openssl/out32dll文件夹中。

准备工作:

需要在系统系统配置文件中配置openssl.cnf的路径,后面生成证书时会用到。

 

下面将罗列生成CA、server、client三方证书的步骤,但对于只做单向鉴定的情况下client证书是不必要的。

 

A、生成根证书及自签名证书

1.创建根证私钥 

    openssl genrsa -out ca-key.pem 1024

该命含义如下:

genrsa——使用RSA算法产生私钥

-out——输出文件的路径

1024——指定私钥长度

2.创建根证书请求文件 

    openssl req -new -out ca-req.csr -key ca-key.pem

该命令含义如下:

req——执行证书签发命令

-new——新证书签发请求

-key——指定私钥路径

-out——输出的csr文件的路径

3.自签根证书 
    openssl x509 -req   -extfile ../apps/openssl.cnf -extensions v3_req  -in ca-req.csr -out ca-cert.pem -signkey ca-key.pem -CAcreateserial -days 3650

该命令的含义如下:

x509——生成x509格式证书

-req——输入csr文件

-days——证书的有效期(天)

-sha1——证书摘要采用sha1算法

-extensions——按照openssl.cnf文件中配置的v3_ca项添加扩展

-signkey——签发证书的私钥

-in——要输入的csr文件

-out——输出的cer证书文件

    重要说明: -extfile ../apps/openssl.cnf -extensions v3_req  参数是生成 X509 V3 版本的证书的必要条件,如果不带此参数,则生成v1版本证书。 ../apps/openssl.cnf  是系统自带的OpenSSL配置文件,该配置文件默认开启 X509 V3 格式。下同。

4.导出p12格式根证书 
    openssl pkcs12 -export -clcerts -in ca-cert.pem -inkey ca-key.pem -out ca.p12 

参数含义如下:

pkcs12——用来处理pkcs#12格式的证书

-export——执行的是导出操作

-clcerts——导出的是客户端证书,-cacerts则表示导出的是ca证书

-inkey——证书的私钥路径

-in——要导出的证书的路径

-out——输出的密钥库文件的路径

 

B、生成服务端证书,使用根证书签发

1.生成服务端key 

    openssl genrsa -out server-key.pem 1024

2.生成服务端请求文件 

    openssl req -new -out server-req.csr -key server-key.pem

3.生成服务端证书,使用根证书签名 

    openssl x509 -req -extfile ../apps/openssl.cnf -extensions v3_req -in server-req.csr -out server-cert.pem -signkey server-key.pem -CA ca-cert.pem  -CAkey ca-key.pem -CAcreateserial -days 3650

4.生成服务端p12格式根证书 
    openssl pkcs12 -export -clcerts -in server-cert.pem -inkey server-key.pem -out server.p12 

 

C、生成客户证书,使用根证书签发

1.生成客户端key 

    openssl genrsa -out client-key.pem 1024

2.生成客户端请求文件 

    openssl req -new -out client-req.csr -key client-key.pem

3.生成客户端证书,使用根证书签名 

    openssl x509 -req -extfile ../apps/openssl.cnf -extensions v3_req -in client-req.csr -out client-cert.pem -signkey client-key.pem -CA ca-cert.pem  -CAkey ca-key.pem -CAcreateserial -days 3650

4.生成客户端p12格式根证书 
    openssl pkcs12 -export -clcerts -in client-cert.pem -inkey client-key.pem -out client.p12 

 

D、查看证书

openssl x509  -in ca-cert.pem -text -noout 

Certificate: Data: Version: 3 (0x2) Serial Number: 13373217044989835800 (0xb997360c4ed17a18) Signature Algorithm: sha1WithRSAEncryption Issuer: C=CN, ST=bj, L=Default City, O=Default Company Ltd Validity Not Before: May 16 02:25:21 2018 GMT Not After : May 13 02:25:21 2028 GMT Subject: C=CN, ST=bj, L=bj, O=bj, OU=bj, CN=bj/emailAddress=bj Subject Public Key Info: Public Key Algorithm: rsaEncryption Public-Key: (1024 bit) Modulus: 00:b1:3d:63:35:52:a6:75:c1:9c:2e:5f:88:df:7e: fc:29:a9:d4:bb:91:e5:27:b8:92:cc:63:7d:d8:7a: b0:3f:7c:43:f8:e7:f9:ed:b7:f6:26:00:d1:ee:68: 20:6a:80:bc:0f:0d:3f:94:3f:b2:4d:ab:49:3f:f6: 88:db:5a:0c:f4:41:5d:d5:d3:34:27:b6:87:c0:65: c6:f6:0c:e3:b1:ea:59:24:ff:14:48:6a:d2:51:2a: 61:a9:c9:24:cc:e5:6a:ba:d7:83:76:1a:54:6d:a6: 01:f6:75:98:4c:45:6d:a1:ad:9c:88:1b:d7:ae:c6: a4:1e:99:ba:44:ea:52:1b:37 Exponent: 65537 (0x10001) X509v3 extensions: X509v3 Basic Constraints:  CA:FALSE X509v3 Key Usage:  Digital Signature, Non Repudiation, Key Encipherment X509v3 Basic Constraints:  CA:FALSE X509v3 Key Usage:  Digital Signature, Non Repudiation, Key Encipherment Signature Algorithm: sha1WithRSAEncryption 3f:e5:fd:ab:08:2e:37:6c:5f:12:aa:0c:b4:28:da:2e:7a:c7: 0a:43:89:81:1a:33:c2:d7:dd:95:c5:d6:a9:4c:12:d2:54:ee: ec:9a:15:93:ab:a6:59:40:2e:a8:ad:02:19:69:d3:49:17:08: f5:61:e1:68:0d:1b:ac:0f:9e:eb:a7:03:fa:9d:64:1f:42:cd: 24:58:ce:ad:6c:14:e2:78:77:42:37:1f:be:a9:a3:e1:bb:43: 20:05:a3:9c:94:98:49:c0:f3:09:ce:11:f6:17:cf:3f:07:da: a0:fc:cd:0c:6f:09:d1:3c:5f:5d:c6:81:c8:d5:62:59:3a:9e: 39:49

 

过程都是相同的,先生成1024位的RSA私钥,然后生成证书请求文件(.csr),csr文件经CA私钥签名后生成公钥(即X.509证书),如果需要的话还可以再把它导出为其他格式比如PKCS#12证书(.p12)。

参考代码如下:

D:\Code\openssl\apps>openssl genrsa -out ies/ca-key.pem 1024
Generating RSA private key, 1024 bit long modulus
.............++++++
.....................++++++
e is 65537 (0x10001)D:\Code\openssl\apps>openssl req -new -out ies/ca-req.csr -key ies/ca-key.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Shan-Dong
Locality Name (eg, city) []:jinan
Organization Name (eg, company) [Internet Widgits Pty Ltd]:iESLab
Organizational Unit Name (eg, section) []:SK
Common Name (e.g. server FQDN or YOUR name) []:www.iessk.com
Email Address []:sorata_kasugano@qq.comPlease enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:.
An optional company name []:.D:\Code\openssl\apps>openssl x509 -req -in ies/ca-req.csr -out ies/ca-cert.pem -signkey ies/ca-key.pem -days 3650
Signature ok
subject=/C=CN/ST=Shan-Dong/L=jinan/O=iESLab/OU=SK/CN=www.iessk.com/emailAddress=sorata_kasugano@qq.com
Getting Private keyD:\Code\openssl\apps>openssl pkcs12 -export -clcerts -in ies/ca-cert.pem -inkey ies/ca-key.pem -out ies/ca.p12
Enter Export Password:
Verifying - Enter Export Password:

 

D:\Code\openssl\apps>openssl genrsa -out ies/server-key.pem 1024
Generating RSA private key, 1024 bit long modulus
......++++++
.++++++
e is 65537 (0x10001)D:\Code\openssl\apps>openssl req -new -out ies/server-req.csr -key ies/server-key.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Shan-Dong
Locality Name (eg, city) []:jinan
Organization Name (eg, company) [Internet Widgits Pty Ltd]:iESLab
Organizational Unit Name (eg, section) []:SK
Common Name (e.g. server FQDN or YOUR name) []:www.iessk.com
Email Address []:sorata_kasugano@qq.comPlease enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:.
An optional company name []:.D:\Code\openssl\apps>openssl x509 -req -in ies/server-req.csr -out ies/server-cert.pem -signkey ies/server-key.pem -CA ies/ca-cert.pem -CAkey ies/ca-key.pem -CAcreateserial -days 3650
Signature ok
subject=/C=CN/ST=Shan-Dong/L=jinan/O=iESLab/OU=SK/CN=www.iessk.com/emailAddress=sorata_kasugano@qq.com
Getting Private key
Getting CA Private KeyD:\Code\openssl\apps>openssl pkcs12 -export -clcerts -in ies/server-cert.pem -inkey ies/server-key.pem -out ies/server.p12
Enter Export Password:
Verifying - Enter Export Password:

 

D:\Code\openssl\apps>openssl genrsa -out ies/client-key.pem 1024
Generating RSA private key, 1024 bit long modulus
......................++++++
...............++++++
e is 65537 (0x10001)D:\Code\openssl\apps>openssl req -new -out ies/client-req.csr -key ies/client-key.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Shan-Dong
Locality Name (eg, city) []:jinan
Organization Name (eg, company) [Internet Widgits Pty Ltd]:iESLab
Organizational Unit Name (eg, section) []:SK
Common Name (e.g. server FQDN or YOUR name) []:www.iessk.com
Email Address []:sorata_kasugano@qq.comPlease enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:.
An optional company name []:.D:\Code\openssl\apps>openssl x509 -req -in ies/client-req.csr -out ies/client-cert.pem -signkey ies/client-key.pem -CA ies/ca-cert.pem -CAkey ies/ca-key.pem -CAcreateserial -days 3650
Signature ok
subject=/C=CN/ST=Shan-Dong/L=jinan/O=iESLab/OU=SK/CN=www.iessk.com/emailAddress=sorata_kasugano@qq.com
Getting Private key
Getting CA Private KeyD:\Code\openssl\apps>openssl pkcs12 -export -clcerts -in ies/client-cert.pem -inkey ies/client-key.pem -out ies/client.p12
Enter Export Password:
Verifying - Enter Export Password:

 

 

 

 

 

 

这篇关于使用Openssl生成根证书CA以及签发子证书的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/280434

相关文章

Spring @RequestMapping 注解及使用技巧详解

《Spring@RequestMapping注解及使用技巧详解》@RequestMapping是SpringMVC中定义请求映射规则的核心注解,用于将HTTP请求映射到Controller处理方法... 目录一、核心作用二、关键参数说明三、快捷组合注解四、动态路径参数(@PathVariable)五、匹配请

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

Java 枚举的基本使用方法及实际使用场景

《Java枚举的基本使用方法及实际使用场景》枚举是Java中一种特殊的类,用于定义一组固定的常量,枚举类型提供了更好的类型安全性和可读性,适用于需要定义一组有限且固定的值的场景,本文给大家介绍Jav... 目录一、什么是枚举?二、枚举的基本使用方法定义枚举三、实际使用场景代替常量状态机四、更多用法1.实现接

springboot项目中使用JOSN解析库的方法

《springboot项目中使用JOSN解析库的方法》JSON,全程是JavaScriptObjectNotation,是一种轻量级的数据交换格式,本文给大家介绍springboot项目中使用JOSN... 目录一、jsON解析简介二、Spring Boot项目中使用JSON解析1、pom.XML文件引入依

Java中的record使用详解

《Java中的record使用详解》record是Java14引入的一种新语法(在Java16中成为正式功能),用于定义不可变的数据类,这篇文章给大家介绍Java中的record相关知识,感兴趣的朋友... 目录1. 什么是 record?2. 基本语法3. record 的核心特性4. 使用场景5. 自定

Python使用Tkinter打造一个完整的桌面应用

《Python使用Tkinter打造一个完整的桌面应用》在Python生态中,Tkinter就像一把瑞士军刀,它没有花哨的特效,却能快速搭建出实用的图形界面,作为Python自带的标准库,无需安装即可... 目录一、界面搭建:像搭积木一样组合控件二、菜单系统:给应用装上“控制中枢”三、事件驱动:让界面“活”

python如何生成指定文件大小

《python如何生成指定文件大小》:本文主要介绍python如何生成指定文件大小的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python生成指定文件大小方法一(速度最快)方法二(中等速度)方法三(生成可读文本文件–较慢)方法四(使用内存映射高效生成

C/C++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示

Python验证码识别方式(使用pytesseract库)

《Python验证码识别方式(使用pytesseract库)》:本文主要介绍Python验证码识别方式(使用pytesseract库),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全... 目录1、安装Tesseract-OCR2、在python中使用3、本地图片识别4、结合playwrigh

Python使用Code2flow将代码转化为流程图的操作教程

《Python使用Code2flow将代码转化为流程图的操作教程》Code2flow是一款开源工具,能够将代码自动转换为流程图,该工具对于代码审查、调试和理解大型代码库非常有用,在这篇博客中,我们将深... 目录引言1nVflRA、为什么选择 Code2flow?2、安装 Code2flow3、基本功能演示