搭建CA并签发数字证书
Openssl完成私有CA
yum install openssl openssl-devel -y ---完成私有软件包的安装
修改openssl.cnf配置文件
[root@CA ~]# vim /etc/pki/tls/openssl.cnf
[CA_default]
dir = /etc/pki/CA //CA签署工作目录
certs = $dir/certs //用户证书存放路径
certificate = $dir/my-ca.crt //CA根证书文件
private_key = $dir/private/my-ca.key //CA私钥文件创建相关的文件
cd /etc/pki/CA //前往CA签署工作目录
genrsa 生成一段私钥
格式: openssl genrsa 密钥长度
生成私有密钥以后需要将私有密钥进行保存
方法一:先生成私有密钥 openssl genrsa 密钥长度
再进行重定向保存 openssl genrsa 密钥长度 > cakey.pem
由于是私钥最后修改权限: chown 600 cakey.pem
方法二:使用()进行操作
使用括号括起来的命令只在子shell中执行,执行完子shell就退出了,就和当前的无冲突
(umask 077, opensll genrsa -out cakey.pem 密钥长度)
openssl rsa -in 私钥位置 -puboot
格式: openssl genrsa -out /PATH/TO/KEYFILENAME NUMBITS
Openssl rsa -in /PATH/TO/KEYFILENAME puboot
openssl req -new -x509 -key 私钥位置 -out 输出位置 -days 有效天数
Openssl x509 -text -in 查看文件
CA配置文件
[CA_default]
dir = /etc/pki/CA //CA签署工作目录
certs = $dir/certs //用户证书存放路径
certificate = $dir/my-ca.crt //CA根证书文件
private_key = $dir/private/my-ca.key //CA私钥文件
Country Name (2 letter code) [XX]:
国家名称(2个字母代码)
State or Province Name (full name) []:
省/自治区名称
Locality Name (eg, city) [Default City]:
地点名称
Organization Name (eg, company) [Default Company Ltd]:
组织名称
Organizational Unit Name (eg, section) []:
组织单位名称
Common Name (eg, your name or your server's hostname) []:
或者您的服务器名
Email Address []:
电子邮件地址
根据要求将上面的修改为题目要求的数值
[ req_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = CN
countryName_min = 2
countryName_max = 2
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = ZB
localityName = Locality Name (eg, city)
localityName_default = ZB
0.organizationName = Organization Name (eg, company)
0.organizationName_default = chinaskill
organizationalUnitName = Organizational Unit Name (eg, section)
organizationalUnitName_default =sever
commonName = Common Name (eg, your name or your server\'s hostname)
commonName_max = 64
emailAddress = Email Address
emailAddress_max = 64
生成 RSA 私钥
openssl genrsa [args] [numbits]
-des: CBC模式的DES加密
-des3: CBC模式的3DES加密
-aes128: CBC模式的AES128加密
-aes192: CBC模式的AES192加密
-aes256: CBC模式的AES256加密
-passout arg arg为对称加密(des、3des、aes)的密码,使用该参数可以省去指令行交互提示输入密码环节
-out file 输出证书私钥文件
[numbits] 密钥长度
example 生成一个1024位的RSA私钥,并用3DES加密(密码为1111),保存为server.key文件
openssl genrsa -out server.key -passout pass:1111 -des3 1024
输出文件位置 设置对称加密密码 设置加密模式 设置密钥长度
生成证书签名请求(CSR)
openssl req [options] <infile> outfile
-inform arg 输入文件格式 DER、PEM
-outform arg 输出文件格式 DER、PEM
-in arg 待处理文件
-out arg 待输出文件
-passin 用于签名待生成的请求证书的私钥文件的解密密码
-key file 用于签名待生成的请求证书的私钥文件
-keyform arg DER、NET、PEM
-new 新的请求
-x509 输出一个X509格式的证书
-days X509证书的有效时间
-newkey rsa:bits 生成一个bits长度的RSA私钥文件,用于签发
-[digest] HASH算法 md5、sha1、md2、mdc2、md4
-config file 指定openssl配置文件
-text text显示格式
example1 利用CA的RSA密钥创建一个自签署的CA证书(X.509结构)
openssl req -new -x509 -days 365 -key server.key -out ca.crt
新的请求 证书格式 证书有效时间 请求证书私钥文件 待输出文件
example2 用server.key生成证书签署请求CSR(这个CSR用于之外发送待CA中心等待签发)
Openssl req -new -key server.key -out server.cs
新的请求 请求证书私钥文件 待输出文件
example3 查看CSR的细节:
Openssl req -noout -text -in server.csr
CA 签名
openssl ca [options]:
-selfsign 使用对证书请求进行签名的密钥对来签发证书。即"自签名",这种情况发生在生成证书的CA客户端与签发证书的CA服务器都是同一台机器,此时我们可以使用同一个密钥对来进行"自签名"
-in file 需要进行处理的PEM格式的证书
-out file 处理结束后输出的证书文件
-cert file 用于签发的根CA证书
-days arg 指定签发的证书的有效时间
-keyfile arg CA的私钥证书文件
-keyform arg CA的根私钥证书文件格式: PEM、ENGINE
-key arg CA的根私钥证书文件的解密密码(如果加密了的话)
-config file 配置文件
example 利用 CA 证书 ca.crt 签署请求证书 server.crt:
openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key
进行处理证书格式 输出证书格式 用于签发艮证书 CA的私钥证书文件
x509 证书操作
openssl x509 [args]
-inform arg 待处理X509证书文件格式 DER、NET、PEM
-outform arg 待输出X509证书文件格式 DER、NET、PEM
-in arg 待处理X509证书文件
-out arg 待输出X509证书文件
-req 表明输入文件是一个"请求签发证书文件(CSR)",等待进行签发
-days arg 表明将要签发的证书的有效时间
-CA arg 指定用于签发请求证书的根CA证书
-CAform arg 根CA证书格式(默认是PEM)
-CAkey arg 指定用于签发请求证书的CA私钥证书文件,缺省认为私有密钥在CA证书文件里有
-CAkeyform arg 指定根CA私钥证书文件格式(默认为PEM格式)
-CAserial arg 指定序列号文件(serial number file)
-CAcreateserial 如果序列号文件(serial number file)没有指定,则自动创建它
example1 转换DER证书为PEM格式
openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem
待处理证书文件 待处理证书格式 待输出证书格式 待输出证书文件
example2 使用根CA证书对"请求签发证书"进行签发,生成x509格式证书
openssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt
example3 打印出证书的内容
openssl x509 -in server.crt -noout -text
具体搭建步骤
1、安装yum包
yum install openssl* -y
确认openssl包是否安装成功
[root@localhost ~]# rpm -q openssl
openssl-1.0.2k-19.el7.x86_64
//已经查询到openssl包,表示安装成功
- 创建关键目录和文件
[root@localhost ~]# vi /etc/pki/tls/openssl.cnf
[ CA_default ]
dir = /etc/pki/CA # Where everything is kept
certs = $dir/certs # Where the issued certs are kept
crl_dir = $dir/crl # Where the issued crl are kept
database = $dir/index.txt # database index file.
...
serial = $dir/serial # The current serial number
需要在CA目录下查看是否有如上三个目录和两个文本
[root@localhost ~]# cd /etc/pki/CA
[root@localhost CA]# ll
总用量 0
drwxr-xr-x. 2 root root 6 8月 9 2019 certs
drwxr-xr-x. 2 root root 6 8月 9 2019 crl
drwxr-xr-x. 2 root root 6 8月 9 2019 newcerts
drwx------. 2 root root 6 8月 9 2019 private
//查到只有三个目录
[root@localhost CA]# touch index.txt
[root@localhost CA]# touch serial
[root@localhost CA]# echo 01 > serial //起始号01
[root@localhost CA]# ls
certs crl index.txt newcerts private serial
- 私有CA配置
openssl完成私有CA
- 先生成一段私有密钥
- 生成自签署证书
首先根据配置文件要求需要将密钥放在CA目录下
[root@localhost ~]# cd /etc/pki/CA
[root@localhost CA]# (umask 077; openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
...................................+++
............+++
e is 65537 (0x10001)
//生成2048位私钥cakey.pem
[root@localhost CA]# ll -l private/
总用量 4
-rw-------. 1 root root 1679 11月 22 15:54 cakey.pem
//查询是否创建成功
生成自签证书
[root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.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) [XX]:CN //国家名称(2个字母代码)
State or Province Name (full name) []:ZB //省/自治区名称
Locality Name (eg, city) [Default City]:ZB //地点名称
Organization Name (eg, company) [Default Company Ltd]:chianskill //组织名称
Organizational Unit Name (eg, section) []:server //组织单位名称
Common Name (eg, your name or your server's hostname) []:chinaskill.com
//或者您的服务器名
Email Address []:admin@chinaskill.com 电子邮件地址
4、生成httpd的签署证书
首先创建对应目录
[root@localhost CA]# mkdir /etc/httpd
[root@localhost CA]# cd /etc/httpd
[root@localhost httpd]# mkdir ssl
[root@localhost httpd]# cd ssl
创建1024位私钥
[root@localhost ssl]# (umask 077; openssl genrsa -out httpd.key 1024)
Generating RSA private key, 1024 bit long modulus
.......++++++
....................................++++++
e is 65537 (0x10001)
生成自签证书
[root@localhost ssl]# openssl req -new -key httpd.key -out httpd.csr
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) [XX]:CN
State or Province Name (full name) []:ZB
Locality Name (eg, city) [Default City]:ZB
Organization Name (eg, company) [Default Company Ltd]:chinaskill
Organizational Unit Name (eg, section) []:web
Common Name (eg, your name or your server's hostname) []:www.chinaskill.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@localhost ssl]# ll
总用量 8
-rw-r--r--. 1 root root 651 11月 22 16:04 httpd.csr
-rw-------. 1 root root 887 11月 22 16:02 httpd.key
//查看私钥和证书是否生成成功
签发证书
[root@localhost ssl]# openssl ca -in httpd.csr -out httpd.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Nov 22 08:10:28 2020 GMT
Not After : Nov 22 08:10:28 2021 GMT
Subject:
countryName = CN
stateOrProvinceName = ZB
organizationName = chinaskill
organizationalUnitName = web
commonName = www.chinaskill.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
63:B7:78:A6:CD:49:AA:C9:42:7C:70:75:0A:AB:43:EF:38:71:88:FD
X509v3 Authority Key Identifier:
keyid:42:84:AC:D1:77:09:AF:6C:81:45:56:39:0B:D9:35:AC:BC:3A:8B:10
Certificate is to be certified until Nov 22 08:10:28 2021 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated