openssl常用使用总结

介绍

 SSL证书通过在客户端浏览器和Web服务器之间建立一条SSL安全通道(Secure socketlayer(SSL),SSL安全协议主要用来提供对用户和服务器的认证;对传送的数据进行加密和隐藏;确保数据在传送中不被改变,即数据的完整性,现已成为该领域中全球化的标准。

证书文件后缀介绍

.key:私用密钥,openssl格式,通常是rsa算法。

.csr:证书请求文件,用于申请证书。在制作csr文件的时候,必须使用自己的私钥来签署申请,还可以设定一个密钥,certificate signing request的缩写。

.crt:CA认证后的证书文件(windows下面的csr,其实是crt),签署人用自己的key给你签署的凭证,certificate的缩写。

.pem:用于导出,导入证书时候的证书的格式,有证书开头,结尾的格式

.crl格式:证书吊销列表,Certificate Revocation List的缩写

证书生成流程

CA根证书的生成

生成CA私钥(.key)---->生成CA证书请求(.csr)----->自签名得到根证书(.crt)(CA给自已颁发的证书)。

用户证书的生成

生成私钥(.key)---->生成证书请求(.csr)---->用CA根证书签名得到证书(.crt)   (如果没有CA证书,用x509替代,x509相当于小型的内置的CA)

使用 x509 进行生成证书(不用自己生成CA)

x509证书一般会用到三类文件,key,csr,crt。

1.生成一个RSA密钥,这个是自己生成的私钥

[root@localhost ssl]# openssl genrsa -des3 -out nginx.key 1024 #实际使用中看服务器性能,如果足够好也可以使用4096位秘钥
Generating RSA private key, 1024 bit long modulus
…….++++++
…++++++
e is 65537 (0x10001)
Enter pass phrase for nginx.key: #输入密码,自定义,不少于4个字符
Verifying - Enter pass phrase for nginx.key: #确认密码


#也可以使用参数 -passout pass:123456 就不用再填密码了

2.生成一个证书请求

[root@localhost ssl]# openssl req -new -key nginx.key -out nginx.csr
Enter pass phrase for nginx.key: #输入刚刚创建的秘密码
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) []:xxxx #省
Locality Name (eg, city) [Default City]:xxxxx #市
Organization Name (eg, company) [Default Company Ltd]:xxxxx #公司
Organizational Unit Name (eg, section) []:xxxxxx #部门
Common Name (eg, your name or your server's hostname) []:.mydomain.com #注意,此处应当填写你要部署的域名,如果是单个则直接添加即可,如果不确定,使用,表示可以对所有mydomain.com的子域名做认证
Email Address []:admin@mydomain.com #以域名结尾即可
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: #是否设置密码,可以不写直接回车

3.创建不需要输入密码的RSA证书,否则每次reload、restart nginx都需要输入密码(一个场景而已)

[root@localhost ssl]# openssl rsa -in nginx.key -out nginx_nopass.key 
Enter pass phrase for nginx.key: #之前RSA秘钥创建时的密码 writing RSA key

4.签发证书

[root@localhost ssl]# openssl x509 -req -days 36500 -in nginx.csr -signkey nginx.key -out nginx.crt 
Signature ok subject=/C=CN/ST=xxxx/L=xxxxx/O=xxxxx/OU=xxxxx/CN=*.mydomain.com/emailAddress=admin@mydomain.com Getting Private key Enter pass phrase for nginx.key: #RSA创建时的密码 days代表证书有效天数,先弄个十年的。

之后把nginx.crt证书和nginx_nopass.key 文件配置进入nginx即可。

自己生成CA证书,并给自己用户签证

# Generate CA private key 
openssl genrsa -out ca.key 2048 
# Generate CSR 
openssl req -new -key ca.key -out ca.csr
# Generate Self Signed certificate(CA 根证书)
openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt


你看到的没错,即使是CA根证书也要x509进行创建
然后用自己创建的CA给服务端签证
# private key
$openssl genrsa -des3 -out server.key 1024 
# generate csr
$openssl req -new -key server.key -out server.csr
# generate certificate
$openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key

生成pem格式证书: 
有时需要用到pem格式的证书,可以用以下方式合并证书文件(crt)和私钥文件(key)来生成 

$cat server.crt server.key > server.pem

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值