SSL基础:20:使用x509子命令为其他证书签名

news/2024/12/2 12:52:03/

在这里插入图片描述
ca子命令使用事前准备的CSR文件,可通过-selfsign选项指定私钥生成自签名证书。使用req子命令也可以生成自签名证书,自签名证书在实际的使用中用处一般是用来创建ca证书的,这篇文章介绍一下如何使用x509子命令结合自签名的ca证书对其他证书签名请求CSR文件进行签名。

事前准备: 准备自签名证书

有多种方式可以生成自签名证书,这里使用最为简单的方式,直接生成可以用作ca的私钥和证书文件。

[root@liumiaocn x509]# openssl req -new -x509 -keyout ca.key  -nodes -out ca.crt -subj "/C=CN/ST=LiaoNing/L=DaLian/O=devops/OU=unicorn/CN=devops.com"
Generating a RSA private key
...........................................................................................................................................+++++
............+++++
writing new private key to 'ca.key'
-----
[root@liumiaocn x509]# ls -l
total 8
-rw-r--r--. 1 root root 1342 Dec 14 21:04 ca.crt
-rw-------. 1 root root 1708 Dec 14 21:04 ca.key
[root@liumiaocn x509]#

结果确认

[root@liumiaocn x509]# openssl x509 -noout -in ca.crt -issuer -subject
issuer=C = CN, ST = LiaoNing, L = DaLian, O = devops, OU = unicorn, CN = devops.com
subject=C = CN, ST = LiaoNing, L = DaLian, O = devops, OU = unicorn, CN = devops.com
[root@liumiaocn x509]# 
[root@liumiaocn x509]# openssl x509 -noout -in ca.crt -dates
notBefore=Dec 15 02:04:19 2019 GMT
notAfter=Jan 14 02:04:19 2020 GMT
[root@liumiaocn x509]#

详细信息如下所示:

[root@liumiaocn x509]# cat ca.key 
-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDVriM5yVAuDDOx
RO8i5h1ttuN405U+FvOqBPKvkSB9bOP5ewfUnQpJZqR45H1dXqQU3nSyZtbGMyvI
vbfu4ZItbcNuDUiPVeZIt938PfXauzTLJFneSkBMh3e08D4IIQC7xfRGY0eNljvN
E9aaQ610W5sn8UsHNS+M8/TQFuP7wHr4OUtVjkdQTgNdFZxIvuNbmZ47YxmsULgh
Op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sf7P7OjVyKaif5
hJWlfJaCr7fcUCggici9eWZq+AjfIGCzN9vAJ+ypQPhNrdOA1mk6IbBN5CiUOnPH
Mj+9XOehzP6vAgMBAAGjUzBRMB0GA1UdDgQWBBS0Ltz0qsIrMLZRZA5C7fsFvB+g
5TAfBgNVHSMEGDAWgBS0Ltz0qsIrMLZRZA5C7fsFvB+g5TAPBgNVHRMBAf8EBTAD
AQH/MA0GCSqGSIb3DQEBCwUAA4IBAQBABzki38lSrAPSYgC3m4+BYEHZnpBqyZII
CZvmN/jttAjaJ5K8DWDH3MOQH4G79OiA7nlNdMFEtV+rp0UI1VLul2KpSqJ2q5Pf
lZPyLok4SRVV7tXIUo4Ilwqnp0RQEZXpEbOQix+xWvfypHvTzkJ5dCQ1lxXODtPV
/wAq1HN+D7G5p+U4HycHRCbT9u1eBxap0EkTI3j/++kHWxvYZanMnEn6jEgCdyv4
FA2Z8KT9Ek0zBTT6wUicdYuHN8dS1CuIHqYLTHq+z10rmvuBS/RqiGDW3VWJwmUp
Kt3PLg8vnrIifhMZiv+tTCL9j/JP5gTX7J1Xsn+uSSwJnaUA0UT2
-----END CERTIFICATE-----
[root@liumiaocn x509]# 

使用x509子命令进行签名

步骤1: 生成证书签名请求CSR文件

签名的动作是需要求前提的,CSR文件就是这个前提,而实际向各个CA机构进行收费的证书申请也是需要提供CSR文件,只是可能会以另外一种格式出现,最终CA机构也是类似的需要生成类似的CSR文件。

执行示例文件:openssl req -new -out request-bj.csr -nodes -subj “/C=CN/ST=BeiJing/L=BeiJing/O=devops/OU=dev/CN=devops.com”

比如这里生成如下的CSR文件:

[root@liumiaocn x509]# ls
ca.crt  ca.key
[root@liumiaocn x509]# openssl req -new -out request-bj.csr -nodes -subj "/C=CN/ST=BeiJing/L=BeiJing/O=devops/OU=dev/CN=devops.com"
Generating a RSA private key
................................+++++
...................+++++
writing new private key to 'privkey.pem'
-----
[root@liumiaocn x509]# ls
ca.crt  ca.key  privkey.pem  request-bj.csr
[root@liumiaocn x509]# cat request-bj.csr 
-----BEGIN CERTIFICATE REQUEST-----
MIICqjCCAZICAQAwZTELMAkGA1UEBhMCQ04xEDAOBgNVBAgMB0JlaUppbmcxEDAO
BgNVBAcMB0JlaUppbmcxDzANBgNVBAoMBmRldm9wczEMMAoGA1UECwwDZGV2MRMw
EQYDVQQDDApkZXZvcHMuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC
AQEAxyozo4nkONn1A7QG8okv7eKrSiWYQ2fgLSzfaYdUcUBV21GDQksOLHDoxle+
kPTV0yLZFJPnDvA41Pi+Iv7dWsKoLgHwWmbQ9tpwVPpVN7Az01vwQAAObiYpcvlr
50DNjWjtInN9gzkj3wfkHHShVErpwmFE7RQIwEjUHIJzTvXaJuhxyvC6usWQsQzO
aIwxUKYaeoxcoS2TN61Mzdx2Rtxij4sebjSjyECKVs5yCxiFh7RFvSo+pliHl0VP
6DrFA/AouC6WrR8coysln0qtBnpUdcBvV8QGuLAaO1kfpRQAFFhGMF4SQagUbW6m
TwQbyHYwGz+FAkk51amg6QiGdQIDAQABoAAwDQYJKoZIhvcNAQELBQADggEBAGOA
wt8OvY4T+d2gEF9Gyrsl6/PxiWLDgTPiPHE8Qr4bmJLb88aG2rYdZtDVf/gEc4Ip
a3Qht6eF4YVzpbAZBgo3xSPCEqC6ut8sCPdjOMjna3n2sCuRo0Pkktx9oJXD5ofd
d6nh9k8JlBbGtUINBW2JEhlZdLOjcd9OEG9VHA84PSBJu7yvjAF1/LxI0skV79Xb
z5j3bVgJJYN7Lydu0LKl+ebkqohBkplx1eUV48sPPyu89lnnJoK7SzhIlCoCRVi5
6/c/enV6VdMaEoHIM0HzML6tnxbGBJlpUC4XgrVi6ft7gLUnSBc9i+cF8JPg9RYm
pWtjkbDPOc8lx7xTw0s=
-----END CERTIFICATE REQUEST-----
[root@liumiaocn x509]# cat privkey.pem 
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAg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QDaRb6HtI02nHnGUA3qvj0UJ1xl13AD2JXYSuLvKhrfiWWHIBnQ
UYLd6ltu+b7EfP9s5Jwq+gd74xUpYlVFBGasH8dofy/uwtM3EoFa08n7I1W84JZp
zIBepNDVM6tZYunoFLbvmp3TvNTwtWMaN7G9CzFn3GCMEw92M+k2PtV15QKBgB2y
Iwf2nMCIsP+OvtJurkwi2Nh2mP6k3cAK0UT7haT1WtkAKNkFkI54FiXFIjZNQfSA
kD7MpCcpR8rrAEbKTStXY+1bQJc8pb0JLyuUTXhtpa6cLLLeeeN2K2Mnhv2iSBeD
zGD0ZQut0B8FqFRquiykD0tCOC57d5Bs/+pVhQV5AoGBAMP38T6xGhtSmvu5af9p
XSgGhR3EHIEZ1GoIuwY7FBoLdhGZPZAKx72Q4vq/PXCqRA8p/mUdORJPwfQ8iKKU
G66hLJQK/BEjvgo+ldRabwa8ZNB6VTT0XVspWCILlw16GQFGhB7B2Wekos9gI54c
8nXn93kEOuuI4LxBMZhq+2/N
-----END PRIVATE KEY-----
[root@liumiaocn x509]# 

步骤2: 使用x509子命令和ca证书进行签名

使用CA和CAkey指定CA的私钥和证书文件,然后对CSR文件进行签名,得到签名之后的证书文件cert-test-bj.crt

[root@liumiaocn x509]# ls
ca.crt  ca.key  privkey.pem  request-bj.csr
[root@liumiaocn x509]# openssl x509 -req -in request-bj.csr -CA ca.crt -CAkey ca.key -out cert-test-bj.crt -CAcreateserial
Signature ok
subject=C = CN, ST = BeiJing, L = BeiJing, O = devops, OU = dev, CN = devops.com
Getting CA Private Key
[root@liumiaocn x509]#

结果确认如下所示

[root@liumiaocn x509]# ls
ca.crt  ca.key  ca.srl  cert-test-bj.crt  privkey.pem  request-bj.csr
[root@liumiaocn x509]# 
[root@liumiaocn x509]# cat ca.srl 
7501AB7B9A89D78837CB3C6B2F38B224D0E0BF13
[root@liumiaocn x509]# cat cert-test-bj.crt 
-----BEGIN CERTIFICATE-----
MIIDVTCCAj0CFHUBq3uaideIN8s8ay84siTQ4L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gNQ6E9Or863nFZ1KZIVOMF9WTQi8IuMDAO4vwU3WYJTUViQdQZUOkDs=
-----END CERTIFICATE-----
[root@liumiaocn x509]# openssl x509 -noout -issuer -subject -dates -in cert-test-bj.crt 
issuer=C = CN, ST = LiaoNing, L = DaLian, O = devops, OU = unicorn, CN = devops.com
subject=C = CN, ST = BeiJing, L = BeiJing, O = devops, OU = dev, CN = devops.com
notBefore=Dec 15 02:13:11 2019 GMT
notAfter=Jan 14 02:13:11 2020 GMT
[root@liumiaocn x509]# 

可以看到证书的发行者和拥有者不再跟之前自签名的示例那样都是相同的,这里的issuer是作为CA的证书ca.crt所提供,而subject显示的内容正是证书签名请求中所设定的内容。


http://www.ppmy.cn/news/208605.html

相关文章

OA项目之我的审批(查询会议签字)

目录 一、我的审核查询①实现思路②后端编写③前端搭建 二、会议签字①前言②实现思路③后端实现④前端实现 一、我的审核查询 ①实现思路 实现思路 域想要实现我的审核查询,那么我们查询表时就要加入条件id登录者,而且审核状态也需要是待审核 ②后端编…

OA之我的会议(会议排座送审)

目录 一、会议排座插件介绍 需求背景: 1、实现思路: 2.明确了开发会议排座的意义 1.查询出本场会议中的所有参与人员2.需要完成在页面上元素的拖动功能,把对应的参会人员放在指定位置,如:重要的人就放在主位3.将已经…

会议OA项目(我的会议中的会议排座送审功能)

文章目录 一、会议排座插件介绍 1)会议项目为什么要有会议排座的功能 2)完成在页面上元素的拖动功能 2.1分析现有素材的不足 2.2修改现有素材的不足⬇⬇⬇ 2.3 content需要传递到后台,并且生成图片,只有这样&#xf…

会议OA项目之我的会议排座批审功能

目录 背景: 一、会议排座 后台代码 前台代码 二、会议送审 后台代码 前台代码 背景: 会议排座犹如我们日常生活中的餐桌礼仪:一般来说,面朝大门的座位是留给最年长或者最尊贵的客人的,所谓面朝大门即为尊说的就是这…

OA项目之会议排座和送审

目录 一,会议排座 二,会议送审 一,会议排座 分析: 1. 查找资料 做选择,哪一个素材更适合完成需求 2. 素材改造 素材的缺陷: ①:样式:座位小方块重叠/太小 ②…

会议OA系统03

目录 一,会议排座 二,会议送审 一,会议排座 分析: 1. 查找资料 做选择,哪一个素材更适合完成需求 2. 素材改造 素材的缺陷: ①:样式:座位小方块重叠/太小 …

layui值会议OA系统3.0

目录 一、会议排座1.前台编码调试后的jsp代码 2后台编码1.图片工具类2.js代码3.SQL语句编写4.子控制器5.dao方法 二、图片处理图片处理类前台代码后台接收 三、会议送审1.前台编码2.后台编写 一、会议排座 首先我们的先查出会议里有哪些参会人员,在会议排座中&…

会议OA(会议排座送审)

目录 一,会议排座 二,会议送审 一,会议排座 分析: 1. 查找资料 做选择,哪一个素材更适合完成需求 2. 素材改造 素材的缺陷: ①:样式:座位小方块重叠/太小 …