使用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

相关文章

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客