一、安装Openssl
[root@harbor ~]# yum -y install openssl
二、生成服务端证书文件
生成CA私钥(.key)→ 生成CA证书请求(.csr)→ 自签名得到根证书(.crt)(CA给自已颁发的证书)
1、生成服务端私钥(key文件)
[root@localhost ~]# mkdir /etc/cert
[root@localhost ~]# cd /etc/cert/
[root@localhost cert]# openssl genrsa -des3 -out server.key 1024

注意:运行时会提示输入密码,此密码用于加密key文件(参数des3是加密算法,也可以选用其他安全的算法),以后每当需读取此文件(通过openssl提供的命令或API)都需输入口令.如果不要口令,则可用以下命令去除口令:
[root@localhost cert]# openssl rsa -in server.key -out server.key
Enter pass phrase for server.key:
writing RSA key
2、生成证书签名文件(csr文件)
[root@localhost cert]# openssl req -new -key server.key -out server.csr
注意:www.baidu.com

[root@localhost cert]# ls
server.csr server.key
三、生成CA证书文件
server.csr文件必须有CA的签名才可形成证书.可以将server.csr交给权威的CA签名机构认证,生成ssl证书,但需要缴server.scr进行签名生成ssl证书。
1、首先生成CA的key文件
[root@localhost ~]# mkdir /etc/cert/ca
[root@localhost ~]# cd /etc/cert/ca/
[root@localhost ca]# openssl genrsa -des3 -out ca.key 1024

#去除ca.key的口令:
[root@localhost ca]# openssl rsa -in ca.key -out ca.key
Enter pass phrase for ca.key: #输入CA私钥文件密码
writing RSA key
2、生成CA自签名证书(根证书)
[root@localhost ca]# openssl req -new -x509 -key ca.key -out ca.crt

-days 365 证书过期时间选项
四、利用CA证书进行签名
- 用生成的CA证书为
server.csr文件签名
[root@localhost ~]# cd /etc/cer
[root@localhost cert]# openssl x509 -req -days 3650 -in server.csr -CA /etc/cert/ca/ca.crt -CAkey /etc/cert/ca/ca.key -CAcreateserial -out server.crt
Signature ok
subject=/C=CN/ST=SH/L=SH/O=SCM/OU=SCM/CN=dqzboy.com
Getting CA Private Key
输入key的密钥后,完成证书生成。-days指定有效期,-in 指定证书签名请求文件,-out指明生成的ssl证书,-CA选项指明自定义的CA机构,-CAkey选项指明用于签名的密钥,-CAserial指明序列号文件,而-CAcreateserial指明文件不存在时自动生成。
最后/etc/cert/目录下的server.crt与server.key就是我们需要用到的证书文件和私钥文件
五、生成pem格式证书
有时需要用到pem格式的证书,可以用以下方式合并证书文件(crt)和私钥文件(key)来生成
[root@localhost ~]# cd /etc/cert/
[root@localhost cert]# ls
ca server.crt server.csr server.key
[root@localhost cert]# cat server.key server.crt > server.pem
[root@localhost cert]# ls
ca server.crt server.csr server.key server.pem

必须 注册 为本站用户, 登录 后才可以发表评论!