openssl生成证书的原理及使用场景

1. 基本原理

参考:http://www.cnblogs.com/phpinfo/archive/2013/08/09/3246376.html

== Begin http://www.cnblogs.com/phpinfo/archive/2013/08/09/3246376.html ==

公司一个项目要进行交易数据传输,因为这个项目银行那边也是刚刚开始启动,所有的支持只有一个传输字段的说明文档,好吧,总的有人做事不是嘛,于是接口开发正式展开,第一步的难点就是加密解密,我选择使用OpenSSL.

OpenSSL初接触的人恐怕最难的在于先理解各种概念

公钥/私钥/签名/验证签名/加密/解密/非对称加密

我们一般的加密是用一个密码加密文件,然后解密也用同样的密码.这很好理解,这个是对称加密.而有些加密时,加密用的一个密码,而解密用另外一组密码,这个叫非对称加密,意思就是加密解密的密码不一样.初次接触的人恐怕无论如何都理解不了.其实这是数学上的一个素数积求因子的原理的应用,如果你一定要搞懂,百度有大把大把的资料可以看,其结果就是用这一组密钥中的一个来加密数据,可以用另一个解开.是的没错,公钥和私钥都可以用来加密数据,相反用另一个解开,公钥加密数据,然后私钥解密的情况被称为加密解密,私钥加密数据,公钥解密一般被称为签名和验证签名.

因为公钥加密的数据只有它相对应的私钥可以解开,所以你可以把公钥给人和人,让他加密他想要传送给你的数据,这个数据只有到了有私钥的你这里,才可以解开成有用的数据,其他人就是得到了,也看懂内容.同理,如果你用你的私钥对数据进行签名,那这个数据就只有配对的公钥可以解开,有这个私钥的只有你,所以如果配对的公钥解开了数据,就说明这数据是你发的,相反,则不是.这个被称为签名.

实际应用中,一般都是和对方交换公钥,然后你要发给对方的数据,用他的公钥加密,他得到后用他的私钥解密,他要发给你的数据,用你的公钥加密,你得到后用你的私钥解密,这样最大程度保证了安全性.

RSA/DSA/SHA/MD5

非对称加密的算法有很多,比较著名的有RSA/DSA ,不同的是RSA可以用于加/解密,也可以用于签名验签,DSA则只能用于签名.至于SHA则是一种和md5相同的算法,它不是用于加密解密或者签名的,它被称为摘要算法.就是通过一种算法,依据数据内容生成一种固定长度的摘要,这串摘要值与原数据存在对应关系,就是原数据会生成这个摘要,但是,这个摘要是不能还原成原数据的,嗯....,正常情况下是这样的,这个算法起的作用就是,如果你把原数据修改一点点,那么生成的摘要都会不同,传输过程中把原数据给你再给你一个摘要,你把得到的原数据同样做一次摘要算法,与给你的摘要相比较就可以知道这个数据有没有在传输过程中被修改了.

实际应用过程中,因为需要加密的数据可能会很大,进行加密费时费力,所以一般都会把原数据先进行摘要,然后对这个摘要值进行加密,将原数据的明文和加密后的摘要值一起传给你.这样你解开加密后的摘要值,再和你得到的数据进行的摘要值对应一下就可以知道数据有没有被修改了,而且,因为私钥只有你有,只有你能解密摘要值,所以别人就算把原数据做了修改,然后生成一个假的摘要给你也是不行的,你这边用密钥也根本解不开.

CA/PEM/DER/X509/PKCS

一般的公钥不会用明文传输给别人的,正常情况下都会生成一个文件,这个文件就是公钥文件,然后这个文件可以交给其他人用于加密,但是传输过程中如果有人恶意破坏,将你的公钥换成了他的公钥,然后得到公钥的一方加密数据,不是他就可以用他自己的密钥解密看到数据了吗,为了解决这个问题,需要一个公证方来做这个事,任何人都可以找它来确认公钥是谁发的.这就是CA,CA确认公钥的原理也很简单,它将它自己的公钥发布给所有人,然后一个想要发布自己公钥的人可以将自己的公钥和一些身份信息发给CA,CA用自己的密钥进行加密,这里也可以称为签名.然后这个包含了你的公钥和你的信息的文件就可以称为证书文件了.这样一来所有得到一些公钥文件的人,通过CA的公钥解密了文件,如果正常解密那么机密后里面的信息一定是真的,因为加密方只可能是CA,其他人没它的密钥啊.这样你解开公钥文件,看看里面的信息就知道这个是不是那个你需要用来加密的公钥了.

实际应用中,一般人都不会找CA去签名,因为那是收钱的,所以可以自己做一个自签名的证书文件,就是自己生成一对密钥,然后再用自己生成的另外一对密钥对这对密钥进行签名,这个只用于真正需要签名证书的人,普通的加密解密数据,直接用公钥和私钥来做就可以了.

密钥文件的格式用OpenSSL生成的就只有PEM和DER两种格式,PEM的是将密钥用base64编码表示出来的,直接打开你能看到一串的英文字母,DER格式是二进制的密钥文件,直接打开,你可以看到........你什么也看不懂!.X509是通用的证书文件格式定义.pkcs的一系列标准是指定的存放密钥的文件标准,你只要知道PEM DER X509 PKCS这几种格式是可以互相转化的.

== End http://www.cnblogs.com/phpinfo/archive/2013/08/09/3246376.html ==

为了方便理解,我画了一个图,如下:

image

使用 openssl 生成证书(含openssl详解)

一、openssl 简介

openssl 是目前最流行的 SSL 密码库工具,其提供了一个通用、健壮、功能完备的工具套件,用以支持SSL/TLS 协议的实现。
官网:https://www.openssl.org/source/

构成部分

  1. 密码算法库

  2. 密钥和证书封装管理功能

  3. SSL通信API接口

用途

  1. 建立 RSA、DH、DSA key 参数

  2. 建立 X.509 证书、证书签名请求(CSR)和CRLs(证书回收列表)

  3. 计算消息摘要

  4. 使用各种 Cipher加密/解密

  5. SSL/TLS 客户端以及服务器的测试

  6. 处理S/MIME 或者加密邮件

二、RSA密钥操作

默认情况下,openssl 输出格式为 PKCS#1-PEM

生成RSA私钥(无加密)

openssl genrsa -out rsa_private.key 2048

生成RSA公钥

openssl rsa -in rsa_private.key -pubout -out rsa_public.key

生成RSA私钥(使用aes256加密)

openssl genrsa -aes256 -passout pass:111111 -out rsa_aes_private.key 2048

其中 passout 代替shell 进行密码输入,否则会提示输入密码;
生成加密后的内容如:

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,5584D000DDDD53DD5B12AE935F05A007
Base64 Encoded 
Data-----END RSA PRIVATE KEY-----

此时若生成公钥,需要提供密码

openssl rsa -in rsa_aes_private.key -passin pass:111111 -pubout -out rsa_public.key

其中 passout 代替shell 进行密码输入,否则会提示输入密码;

转换命令

私钥转非加密

openssl rsa -in rsa_aes_private.key -passin pass:111111 -out rsa_private.key

私钥转加密

openssl rsa -in rsa_private.key -aes256 -passout pass:111111 -out rsa_aes_private.key

私钥PEM转DER

openssl rsa -in rsa_private.key -outform der-out rsa_aes_private.der

-inform和-outform 参数制定输入输出格式,由der转pem格式同理

查看私钥明细

openssl rsa -in rsa_private.key -noout -text

使用-pubin参数可查看公钥明细

私钥PKCS#1转PKCS#8

openssl pkcs8 -topk8 -in rsa_private.key -passout pass:111111 -out pkcs8_private.key

其中-passout指定了密码,输出的pkcs8格式密钥为加密形式,pkcs8默认采用des3 加密算法,内容如下:

-----BEGIN ENCRYPTED PRIVATE KEY-----
Base64 Encoded Data
-----END ENCRYPTED PRIVATE KEY-----

使用-nocrypt参数可以输出无加密的pkcs8密钥,如下:

-----BEGIN PRIVATE KEY-----
Base64 Encoded Data
-----END PRIVATE KEY-----

三、生成CA自签名证书和RSA私钥(测试场景步骤)

测试场景步骤1:生成 RSA 私钥和自签名证书:

openssl req -newkey rsa:2048 -nodes -keyout rsa_private.key -x509 -days 36500 -out cert.crt

注释:

req是证书请求的子命令,

-newkey rsa:2048 

-keyout private_key.pem 表示生成私钥(PKCS8格式),

-nodes 表示私钥不加密,若不带参数将提示输入密码;

-x509表示输出证书,

-days36500 为有效期,

此后根据提示输入证书拥有者信息;

操作步骤如下:提示填写过程中如果想删除填写的内容,用ctrl+Backspace删除前面的字符

[root@szxelab01-web-100 cert]# openssl req -newkey rsa:2048 -nodes -keyout rsa_private.key -x509 -days 36500 -out cert.crt
Generating a 2048 bit RSA private key
.............+++
........................+++
writing new private key to 'rsa_private.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) []:GuangDong
Locality Name (eg, city) [Default City]:ShenZhen
Organization Name (eg, company) [Default Company Ltd]:SunFoBank
Organizational Unit Name (eg, section) []:IT Dept
Common Name (eg, your name or your server's hostname) []:sunfobank.com
Email Address []:admin@sunfobank.com
此时就生成了:未加密码的私钥rsa_private.key和CA自签名证书cert.crt
[root@szxjdwins01-web-27 cert]# ll
total 8
-rw-r--r--. 1 root root 1452 Jun 22 14:29 cert.crt
-rw-r--r--. 1 root root 1708 Jun 22 14:29 rsa_private.key
如果想执行自动输入证书拥有者信息,可使用-subj选项:
openssl req -newkey rsa:2048 -nodes -keyout rsa_private.key -x509 -days 36500 -out cert.crt -subj "/C=CN/ST=GuangDong/L=ShenZhen/O=SunFoBank/OU=IT Dept/CN=sunfobank.com/emailAddress=admin@sunfobank.com"
如果想使用已有RSA 私钥生成CA自签名证书,请操作如下:
openssl req -new -x509 -days 36500 -key rsa_private.key -out cert.crt
-new 指生成证书请求,加上-x509 表示直接输出证书,-key 指定私钥文件,其余选项与上述命令相同

四、生成服务器签名请求文件及CA 签名颁发服务器证书()

** server.key建议不要加密码,如果加密码,重启nginx的时候每次都需要密码才可以启动nginx影响效率。**

nginx配置只需要server.key和server.crt两个文件。

测试场景步骤2:使用 RSA私钥生成 CSR 签名请求:server.key为aes256位加密
openssl genrsa -aes256 -passout pass:111111 -out server.key 2048
openssl req -new -key server.key -out server.csr
实战如下:对比下加-passout pass:111111的区别
[root@szxjdwins01-web-27 cert]# openssl genrsa -aes256 -passout pass:111111 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
............................+++
.......+++
e is 65537 (0x10001)

[root@szxjdwins01-web-27 cert]# openssl genrsa -aes256 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
.............................................+++
........................................................+++
e is 65537 (0x10001)
Enter pass phrase for server.key: 111111手动输入密码
Verifying - Enter pass phrase for server.key: 111111手动输入密码
[root@szxelab01-web-27 cert]# openssl req -new -key server.key -out server.csr
Enter pass phrase for server.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) []:GuangDong
Locality Name (eg, city) [Default City]:ShenZhen
Organization Name (eg, company) [Default Company Ltd]:SunFoBank
Organizational Unit Name (eg, section) []:IT Dept
Common Name (eg, your name or your server's hostname) []:sunfobank.com
Email Address []:admin@sunfobank.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:   不输入密码
An optional company name []: 不输入密码

此后输入密码、server证书信息完成,也可以命令行指定各类参数

openssl req -new -key server.key -passin pass:111111 -out server.csr -subj "/C=CN/ST=GuangDong/L=ShenZhen/O=SunFoBank/OU=IT Dept/CN=sunfobank.com/emailAddress=admin@sunfobank.com"

*** 此时生成的 csr签名请求文件可提交至 CA进行签发 ***

查看CSR 的细节
cat server.csr
-----BEGIN CERTIFICATE REQUEST-----
Base64EncodedData
-----END CERTIFICATE REQUEST-----

openssl req -noout -text -in server.csr
使用 CA 证书及CA密钥 对服务器请求签发证书进行签发,生成 x509证书:
openssl x509 -req -days 365000 -in server.csr -CA cert.crt -CAkey rsa_private.key -passin pass:111111 -CAcreateserial -out server.crt
[root@szxelab01-web-27 cert]# openssl x509 -req -days 365000 -in server.csr -CA cert.crt -CAkey rsa_private.key -passin pass:111111 -CAcreateserial -out server.crt
Signature ok
subject=/C=CN/ST=GuangDong/L=ShenZhen/O=SunFoBank/OU=IT Dept/CN=sunfobank.com/emailAddress=admin@sunfobank.com
Getting CA Private Key

其中 CAxxx 选项用于指定CA 参数输入

[root@szxelab01-web-27 cert]# ll
total 24
-rw-r--r--. 1 root root 1452 Jun 22 14:29 cert.crt
-rw-r--r--. 1 root root   17 Jun 22 15:07 cert.srl
-rw-r--r--. 1 root root 1708 Jun 22 14:29 rsa_private.key
-rw-r--r--. 1 root root 1334 Jun 22 15:07 server.crt
-rw-r--r--. 1 root root 1070 Jun 22 15:04 server.csr
-rw-r--r--. 1 root root 1766 Jun 22 14:54 server.key

此时对nginx任何操作,都需要提示输入server.key的密码才可以执行。

[root@szxelab01-web-27 nginx]# /application/nginx/sbin/nginx -t
Enter PEM pass phrase:  输入密码111111
nginx: the configuration file /application/nginx-1.12.2//conf/nginx.conf syntax is ok

为例不输入密码,需要把加密server.key转换成不加密的server.key

[root@szxelab01-web-27 cert]# openssl rsa -in server.key -passin pass:111111 -out server.key                      
writing RSA key

此时nginx操作就不提示输入密码了:

[root@szxelab01-web-27 cert]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.12.2//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.12.2//conf/nginx.conf test is successful

证书位置:

[root@szxelab01-web-27 cert]# pwd
/application/nginx/cert
[root@szxelab01-web-27 cert]# ll
total 24
-rw-r--r--. 1 root root 1452 Jun 22 14:29 cert.crt
-rw-r--r--. 1 root root   17 Jun 22 15:07 cert.srl
-rw-r--r--. 1 root root 1708 Jun 22 14:29 rsa_private.key
-rw-r--r--. 1 root root 1334 Jun 22 15:07 server.crt
-rw-r--r--. 1 root root 1070 Jun 22 15:04 server.csr
-rw-r--r--. 1 root root 1679 Jun 22 15:19 server.key

至此测试场景私有证书配置完成

五、证书查看及转换

查看证书细节

<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">openssl x509 -in cert.crt -noout -text</pre>

转换证书编码格式

<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem</pre>

合成 pkcs#12 证书(含私钥)

** 将 pem 证书和私钥转 pkcs#12 证书 **

<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">openssl pkcs12 -export -in server.crt -inkey server.key -passin pass:111111 -password pass:111111 -out server.p12</pre>

其中-export指导出pkcs#12 证书,-inkey 指定了私钥文件,-passin 为私钥(文件)密码(nodes为无加密),-password 指定 p12文件的密码(导入导出)

** 将 pem 证书和私钥/CA 证书 合成pkcs#12 证书**

<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">openssl pkcs12 -export -in server.crt -inkey server.key -passin pass:111111 \ -chain -CAfile ca.crt -password pass:111111 -out server-all.p12</pre>

其中-chain指示同时添加证书链,-CAfile 指定了CA证书,导出的p12文件将包含多个证书。(其他选项:-name可用于指定server证书别名;-caname用于指定ca证书别名)

** pcks#12 提取PEM文件(含私钥) **

<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">openssl pkcs12 -in server.p12 -password pass:111111 -passout pass:111111 -out out/server.pem</pre>

其中-password 指定 p12文件的密码(导入导出),-passout指输出私钥的加密密码(nodes为无加密)
导出的文件为pem格式,同时包含证书和私钥(pkcs#8):

<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">Bag Attributes
localKeyID: 97 DD 46 3D 1E 91 EF 01 3B 2E 4A 75 81 4F 11 A6 E7 1F 79 40 subject=/C=CN/ST=GD/L=SZ/O=vihoo/OU=dev/CN=vihoo.com/emailAddress=yy@vihoo.com
issuer=/C=CN/ST=GD/L=SZ/O=viroot/OU=dev/CN=viroot.com/emailAddress=yy@viroot.com-----BEGIN CERTIFICATE-----MIIDazCCAlMCCQCIOlA9/dcfEjANBgkqhkiG9w0BAQUFADB5MQswCQYDVQQGEwJD
1LpQCA+2B6dn4scZwaCD-----END CERTIFICATE-----Bag Attributes
localKeyID: 97 DD 46 3D 1E 91 EF 01 3B 2E 4A 75 81 4F 11 A6 E7 1F 79 40 Key Attributes: <No Attributes>
-----BEGIN ENCRYPTED PRIVATE KEY-----MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDC/6rAc1YaPRNf
K9ZLHbyBTKVaxehjxzJHHw==
-----END ENCRYPTED PRIVATE KEY-----</pre>

仅提取私钥

<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;"> openssl pkcs12 -in server.p12 -password pass:111111 -passout pass:111111 -nocerts -out out/key.pem</pre>

仅提取证书(所有证书)

<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;"> openssl pkcs12 -in server.p12 -password pass:111111 -nokeys -out out/key.pem</pre>

仅提取ca证书

<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">openssl pkcs12 -in server-all.p12 -password pass:111111 -nokeys -cacerts -out out/cacert.pem</pre>

仅提取server证书

<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">openssl pkcs12 -in server-all.p12 -password pass:111111 -nokeys -clcerts -out out/cert.pem</pre>

六、openssl 命令参考

<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">1. openssl list-standard-commands(标准命令)
1) asn1parse: asn1parse用于解释用ANS.1语法书写的语句(ASN一般用于定义语法的构成)
2) ca: ca用于CA的管理
openssl ca [options]:
2.1) -selfsign
使用对证书请求进行签名的密钥对来签发证书。即"自签名",这种情况发生在生成证书的客户端、签发证书的CA都是同一台机器(也是我们大多数实验中的情况),我们可以使用同一个
密钥对来进行"自签名"
2.2) -in file
需要进行处理的PEM格式的证书
2.3) -out file
处理结束后输出的证书文件
2.4) -cert file
用于签发的根CA证书
2.5) -days arg
指定签发的证书的有效时间
2.6) -keyfile arg
CA的私钥证书文件
2.7) -keyform arg
CA的根私钥证书文件格式:
2.7.1) PEM
2.7.2) ENGINE
2.8) -key arg
CA的根私钥证书文件的解密密码(如果加密了的话)
2.9) -config file
配置文件
example1: 利用CA证书签署请求证书
openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key

3) req: X.509证书签发请求(CSR)管理
openssl req [options] <infile >outfile        
    3.1) -inform arg
    输入文件格式            
        3.1.1) DER            
        3.1.2) PEM        
    3.2) -outform arg   
    输出文件格式            
        3.2.1) DER            
        3.2.2) PEM        
    3.3) -in arg
    待处理文件        
    3.4) -out arg
    待输出文件        
    3.5) -passin        
    用于签名待生成的请求证书的私钥文件的解密密码        
    3.6) -key file
    用于签名待生成的请求证书的私钥文件        
    3.7) -keyform arg  
        3.7.1) DER            
        3.7.2) NET            
        3.7.3) PEM        
    3.8) -new
    新的请求        
    3.9) -x509          
    输出一个X509格式的证书 
    3.10) -days
    X509证书的有效时间  
    3.11) -newkey rsa:bits 
    生成一个bits长度的RSA私钥文件,用于签发  
    3.12) -[digest]
    HASH算法            
        3.12.1) md5            
        3.12.2) sha1            
        3.12.3) md2            
        3.12.4) mdc2            
        3.12.5) md4        
    3.13) -config file   
    指定openssl配置文件        
    3.14) -text: text显示格式
example1: 利用CA的RSA密钥创建一个自签署的CA证书(X.509结构) 
openssl req -new -x509 -days 3650 -key server.key -out ca.crt 
example2: 用server.key生成证书签署请求CSR(这个CSR用于之外发送待CA中心等待签发)
openssl req -new -key server.key -out server.csr
example3: 查看CSR的细节
openssl req -noout -text -in server.csr    
4) genrsa: 生成RSA参数
openssl genrsa [args] [numbits]
    [args]        
4.1) 对生成的私钥文件是否要使用加密算法进行对称加密:           
     4.1.1) -des: CBC模式的DES加密            
     4.1.2) -des3: CBC模式的DES加密            
     4.1.3) -aes128: CBC模式的AES128加密            
     4.1.4) -aes192: CBC模式的AES192加密            
     4.1.5) -aes256: CBC模式的AES256加密        
4.2) -passout arg: arg为对称加密(des、des、aes)的密码(使用这个参数就省去了console交互提示输入密码的环节)        
4.3) -out file: 输出证书私钥文件
    [numbits]: 密钥长度
example: 生成一个1024位的RSA私钥,并用DES加密(密码为1111),保存为server.key文件
openssl genrsa -out server.key -passout pass:1111 -des3 1024 

5) rsa: RSA数据管理
openssl rsa [options] <infile >outfile        
    5.1) -inform arg
    输入密钥文件格式:            
        5.1.1) DER(ASN1)            
        5.1.2) NET            
        5.1.3) PEM(base64编码格式)         
    5.2) -outform arg
     输出密钥文件格式            
         5.2.1) DER            
         5.2.2) NET            
         5.2.3) PEM        
    5.3) -in arg
    待处理密钥文件 
    5.4) -passin arg
    输入这个加密密钥文件的解密密钥(如果在生成这个密钥文件的时候,选择了加密算法了的话)        
    5.5) -out arg
    待输出密钥文件        
    5.6) -passout arg  
    如果希望输出的密钥文件继续使用加密算法的话则指定密码 
    5.7) -des: CBC模式的DES加密        
    5.8) -des3: CBC模式的DES加密        
    5.9) -aes128: CBC模式的AES128加密        
    5.10) -aes192: CBC模式的AES192加密        
    5.11) -aes256: CBC模式的AES256加密        
    5.12) -text: 以text形式打印密钥key数据 
    5.13) -noout: 不打印密钥key数据 
    5.14) -pubin: 检查待处理文件是否为公钥文件        
    5.15) -pubout: 输出公钥文件
example1: 对私钥文件进行解密
openssl rsa -in server.key -passin pass:111 -out server_nopass.key
example:2: 利用私钥文件生成对应的公钥文件
openssl rsa -in server.key -passin pass:111 -pubout -out server_public.key    6) x509:
本指令是一个功能很丰富的证书处理工具。可以用来显示证书的内容,转换其格式,给CSR签名等X.509证书的管理工作
openssl x509 [args]    
    6.1) -inform arg
    待处理X509证书文件格式            
    6.1.1) DER            
    6.1.2) NET            
    6.1.3) PEM        
    6.2) -outform arg   
    待输出X509证书文件格式            
    6.2.1) DER            
    6.2.2) NET            
    6.2.3) PEM        
    6.3) -in arg 
    待处理X509证书文件        
    6.4) -out arg       
    待输出X509证书文件        
    6.5) -req            
    表明输入文件是一个"请求签发证书文件(CSR)",等待进行签发 
    6.6) -days arg       
    表明将要签发的证书的有效时间 
    6.7) -CA arg 
    指定用于签发请求证书的根CA证书 
    6.8) -CAform arg     
    根CA证书格式(默认是PEM) 
    6.9) -CAkey arg      
    指定用于签发请求证书的CA私钥证书文件,如果这个option没有参数输入,那么缺省认为私有密钥在CA证书文件里有        
    6.10) -CAkeyform arg  
    指定根CA私钥证书文件格式(默认为PEM格式)        
    6.11) -CAserial arg   
    指定序列号文件(serial number file)        
    6.12) -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 

7) crl: crl是用于管理CRL列表 
openssl crl [args]        
    7.1) -inform arg
    输入文件的格式            
         7.1.1) DER(DER编码的CRL对象)            
         7.1.2) PEM(默认的格式)(base64编码的CRL对象)        
    7.2) -outform arg
    指定文件的输出格式 
        7.2.1) DER(DER编码的CRL对象)            
        7.2.2) PEM(默认的格式)(base64编码的CRL对象)        
    7.3) -text: 
    以文本格式来打印CRL信息值。        
    7.4) -in filename
    指定的输入文件名。默认为标准输入。        
    7.5) -out filename
    指定的输出文件名。默认为标准输出。        
    7.6) -hash
    输出颁发者信息值的哈希值。这一项可用于在文件中根据颁发者信息值的哈希值来查询CRL对象。        
    7.7) -fingerprint
    打印CRL对象的标识。        
    7.8) -issuer
    输出颁发者的信息值。        
    7.9) -lastupdate
    输出上一次更新的时间。        
    7.10) -nextupdate
    打印出下一次更新的时间。 
    7.11) -CAfile file
    指定CA文件,用来验证该CRL对象是否合法。 
    7.12) -verify
    是否验证证书。        
example1: 输出CRL文件,包括(颁发者信息HASH值、上一次更新的时间、下一次更新的时间)
openssl crl -in crl.crl -text -issuer -hash -lastupdate –nextupdate 
example2: 将PEM格式的CRL文件转换为DER格式
openssl crl -in crl.pem -outform DER -out crl.der  

8) crl2pkcs7: 用于CRL和PKCS#7之间的转换 
openssl crl2pkcs7 [options] <infile >outfile
转换pem到spc
openssl crl2pkcs7 -nocrl -certfile venus.pem -outform DER -out venus.spc
https://www.openssl.org/docs/apps/crl2pkcs7.html

9) pkcs12: PKCS#12数据的管理
pkcs12文件工具,能生成和分析pkcs12文件。PKCS#12文件可以被用于多个项目,例如包含Netscape、 MSIE 和 MS Outlook
openssl pkcs12 [options] 
http://blog.csdn.net/as3luyuan123/article/details/16105475
https://www.openssl.org/docs/apps/pkcs12.html

10) pkcs7: PCKS#7数据的管理 
用于处理DER或者PEM格式的pkcs#7文件
openssl pkcs7 [options] <infile >outfile
http://blog.csdn.net/as3luyuan123/article/details/16105407
https://www.openssl.org/docs/apps/pkcs7.html
  1. openssl list-message-digest-commands(消息摘要命令)
    1. dgst: dgst用于计算消息摘要
      openssl dgst [args]
      1.1) -hex
      以16进制形式输出摘要
      1.2) -binary
      以二进制形式输出摘要
      1.3) -sign file
      以私钥文件对生成的摘要进行签名
      1.4) -verify file
      使用公钥文件对私钥签名过的摘要文件进行验证
      1.5) -prverify file
      以私钥文件对公钥签名过的摘要文件进行验证
      verify a signature using private key in file
      1.6) 加密处理
      1.6.1) -md5: MD5
      1.6.2) -md4: MD4
      1.6.3) -sha1: SHA1
      1.6.4) -ripemd160
      example1: 用SHA1算法计算文件file.txt的哈西值,输出到stdout
      openssl dgst -sha1 file.txt
      example2: 用dss1算法验证file.txt的数字签名dsasign.bin,验证的private key为DSA算法产生的文件dsakey.pem
      openssl dgst -dss1 -prverify dsakey.pem -signature dsasign.bin file.txt 2) sha1: 用于进行RSA处理
      openssl sha1 [args]
      2.1) -sign file
      用于RSA算法的私钥文件
      2.2) -out file
      输出文件爱你
      2.3) -hex
      以16进制形式输出
      2.4) -binary
      以二进制形式输出
      example1: 用SHA1算法计算文件file.txt的HASH值,输出到文件digest.txt
      openssl sha1 -out digest.txt file.txt
      example2: 用sha1算法为文件file.txt签名,输出到文件rsasign.bin,签名的private key为RSA算法产生的文件rsaprivate.pem
      openssl sha1 -sign rsaprivate.pem -out rsasign.bin file.txt
  2. openssl list-cipher-commands (Cipher命令的列表)
    1. aes-128-cbc
    2. aes-128-ecb
    3. aes-192-cbc
    4. aes-192-ecb
    5. aes-256-cbc
    6. aes-256-ecb
    7. base64
    8. bf
    9. bf-cbc
    10. bf-cfb
    11. bf-ecb
    12. bf-ofb
    13. cast
    14. cast-cbc
    15. cast5-cbc
    16. cast5-cfb
    17. cast5-ecb
    18. cast5-ofb
    19. des
    20. des-cbc
    21. des-cfb
    22. des-ecb
    23. des-ede
    24. des-ede-cbc
    25. des-ede-cfb
    26. des-ede-ofb
    27. des-ede3
    28. des-ede3-cbc
    29. des-ede3-cfb
    30. des-ede3-ofb
    31. des-ofb
    32. des3
    33. desx
    34. rc2
    35. rc2-40-cbc
    36. rc2-64-cbc
    37. rc2-cbc
    38. rc2-cfb
    39. rc2-ecb
    40. rc2-ofb
    41. rc4
    42. rc4-40</pre>

参考:

https://blog.csdn.net/oldmtn/article/details/52208747

https://blog.csdn.net/gengxiaoming7/article/details/78505107

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,408评论 4 371
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,690评论 2 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,036评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,726评论 0 221
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,123评论 3 296
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,037评论 1 225
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,178评论 2 318
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,964评论 0 213
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,703评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,863评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,333评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,658评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,374评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,195评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,988评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,167评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,970评论 2 279

推荐阅读更多精彩内容