概览&证书的结构&证书验签
创建根证书
创建根密钥 ca.key.pem
$ openssl genrsa -des3 -out ca/ca.key.pem 4096
Generating RSA private key, 4096 bit long modulus (2 primes)
.........................................................................................................................................................................++++
..........................................................++++
e is 65537 (0x010001)
Enter pass phrase for ca.key.pem:
Verifying - Enter pass phrase for ca.key.pem:
密码 123456
我们在这儿使用 RSA 4096, 因为根证书不经常使用,为了更好的安全性,我们使用较大的参数。
备注
生产环境一般都会有中间证书,我们这儿为了方便省略了中间证书的签发流程
-des 使用des cbc模式对私钥文件进行加密。
-des3 使用des3 cbc模式对私钥文件进行加密。 需要输入密码,秘钥使用需要输入密码
-idea 使用idea cbc模式对私钥文件进行加密
-out 指定输出私钥文件名。
创建根证书 ca.crt.pem
可以不用创建ca.csr.pem文件,直接生成ca.crt.pem文件,如果分成两步,见下一小节
现在我们使用刚创建的根密钥来创建我们的根证书:
$ openssl req -key ca/ca.key.pem -new -x509 -days 3650 -sha256 -out ca/ca.crt -subj '/C=CN/ST=ShanDong/CN=demo.toolbox/O=dzb'
Enter pass phrase for ca.key.pem:
req大致有3个功能:生成证书请求文件、验证证书请求文件和创建根CA
-config
-key 指定私钥
-new
-sha256 哈希函数
-out 输出的文件
-days 有效期
-subj
subj子参数详解:
缩写 翻译 英文对照
C 国家名称缩写 Country Name (2 letter code)
ST 州或省名称 State or Province Name (full name)
L 城市或区域称 Locality Name (eg, city)
O 组织名(或公司名) Organization Name (eg, company)
OU 组织单位名称(或部门名) Organizational Unit Name (eg, section)
CN 服务器域名/证书拥有者名称 Common Name (e.g. server FQDN or YOUR name)
emailAddress 邮件地址 Email
ca.crt的后缀名不加pem,是方便证书安装的时候,系统可以识别,后续的crt不需要安装,所以添加pem后缀,以区分格式
* 创建根证书 ca.csr.pem ca.crt.pem
$ openssl req -new -key ca/ca.key.pem -sha256 -out ca/ca.csr.pem -subj '/C=CN/ST=ShanDong/CN=demo.toolbox/O=dzb'
Enter pass phrase for ca.key.pem:
$ openssl x509 -req -days 3650 -in ca/ca.csr.pem -signkey ca/ca.key.pem -out ca/ca.crt
查看根证书
查看我们刚刚创建的根证书:
$ openssl x509 -noout -text -in ca/ca.crt
主要检查上面 -subj 填写的内容和证书的有效期
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
71:34:01:05:eb:4b:f7:8c:09:c6:b0:ad:34:b4:aa:03:93:2b:fc:d8
Signature Algorithm: sha256WithRSAEncryption
Issuer: C = CN, ST = ShanDong, CN = demo.toolbox, O = dzb
Validity
Not Before: Apr 24 02:08:43 2023 GMT
Not After : Apr 21 02:08:43 2033 GMT
Subject: C = CN, ST = ShanDong, CN = demo.toolbox, O = dzb
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public-Key: (4096 bit)
Modulus:
00:a7:cb:bb:f2:58:8b:17:60:6d:e3:c8:3f:ed:72:
... ...
73:ad:e1:de:d0:5b:d2:1d:3e:0c:55:a3:aa:b5:0c:
28:8c:b7
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Subject Key Identifier:
FE:D8:0D:72:BE:7B:21:48:1F:14:27:85:3C:9D:16:9E:25:D1:3B:4A
X509v3 Authority Key Identifier:
keyid:FE:D8:0D:72:BE:7B:21:48:1F:14:27:85:3C:9D:16:9E:25:D1:3B:4A
X509v3 Basic Constraints: critical
CA:TRUE
Signature Algorithm: sha256WithRSAEncryption
24:1e:d8:40:7b:0e:3b:4b:96:51:ff:cb:a0:62:ba:e5:e9:55:
... ...
a2:ba:44:ac:97:da:dc:24:d9:b7:5f:3a:fa:05:16:b8:2f:9e:
7a:45:89:fa:be:cf:e4:4e
创建服务器证书
现在让我们使用上面创建的根证书,创建一个我们可以使用的 https 证书。
创建服务器密钥 server.key.pem
$ openssl genrsa -des3 -out server/server.key.pem 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
.......................................................+++++
................................+++++
e is 65537 (0x010001)
Enter pass phrase for server.key.pem:
Verifying - Enter pass phrase for server.key.pem:
密码 123456
备注
我们使用 2048 位的 RSA 证书是因为服务器证书会经常使用,这样可以降低CPU的压力,而且并不影响安全性。
配置文件server.ini
cat > server/server.ini <<EOF
[ req ]
default_bits = 2048
distinguished_name = req_distinguished_name
req_extensions = req_ext
[ req_distinguished_name ]
[ req_ext ]
subjectAltName = @alt_names
[alt_names]
DNS.1 = localhost
IP.1 = 127.0.0.1
IP.2 = 169.254.4.58
IP.3 = 172.20.10.2
EOF
DNS可以配置多个
如果只对IP进行签名,可以如下配置(如果要修改IP,就从这之后进行就行)
cat > server.ini <<EOF
[ req ]
default_bits = 2048
distinguished_name = req_distinguished_name
req_extensions = req_ext
[ req_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = CN
stateOrProvinceName = ShangHai
stateOrProvinceName_default = ShangHai
localityName = Locality Name (eg, city)
localityName_default = ShangHai
organizationName = Organization Name (eg, company)
organizationName_default = dai
commonName = Common Name (e.g. server FQDN or YOUR name)
commonName_max = 64
commonName_default = server.toolbox
[ req_ext ]
subjectAltName = @alt_names
[alt_names]
IP = 169.254.4.58
EOF
DNS可以配置多个(对应多个域名),IP也可以配置多个
生成服务器端证书==请求文件== server.csr.pem
$ openssl req -sha256 -new -key server/server.key.pem -config server/server.ini -out server/server.csr.pem -subj '/C=CN/ST=ShangHai/CN=server.toolbox/O=dai'
$ openssl req -sha256 -new -key server/server.key.pem -config server/server.ini -out server/server.csr.pem
第一种方式,通过 -subj配置了证书授权对象的信息
第二种方式,通过配置文件确定,在执行过程中,会要求确认,可以在这个过程中进行修改
生成服务器端证书 server.crt.pem
openssl x509 -req -days 3650
-CA ca/ca.crt -CAkey ca/ca.key.pem -CAcreateserial
-in server/server.csr.pem -out server/server.crt.pem
-extensions req_ext -extfile server/server.ini
一定要加-extfile server/server.ini
否则最后签发的证书内不会有IP和域名的信息
查看服务器端请求文件
$ openssl x509 -noout -text -in server/server.crt.pem
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
61:79:bd:82:c8:1e:2a:27:6f:a9:34:c8:92:f8:54:3f:22:de:80:0b
Signature Algorithm: sha256WithRSAEncryption
Issuer: C = CN, ST = ShanDong, CN = demo.toolbox, O = dzb
Validity
Not Before: Apr 24 02:19:50 2023 GMT
Not After : Apr 21 02:19:50 2033 GMT
Subject: C = CN, ST = ShangHai, CN = server.toolbox, O = dai
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public-Key: (2048 bit)
Modulus:
00:b1:de:96:bb:43:4b:fb:46:99:7e:be:61:c1:46:
... ...
4a:18:24:b3:79:8c:ff:35:d1:3a:4e:a3:11:02:2c:
98:ff
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Subject Alternative Name:
DNS:localhost, IP Address:127.0.0.1, IP Address:169.254.4.58, IP Address:172.20.10.2
Signature Algorithm: sha256WithRSAEncryption
07:c8:2a:e0:e7:14:86:23:3b:72:38:7f:f8:cc:90:54:f2:fa:
... ...
c2:82:8a:2f:c0:a3:42:78:91:45:92:73:a4:aa:f2:ca:c1:5a:
58:c0:41:f0:ea:65:43:9e
要检查授权的DNS和IP
以上就是详解如何使用openssl创建自签名证书的详细内容,更多关于openssl创建自签名证书的资料请关注aitechtogether.com其它相关文章!