证书管理

一般情况下,如果能找到可用的证书,就可以直接使用,只不过会因证书的某些信息不正确或与部署证书的主机不匹配而导致浏览器提示证书无效,但这并不影响使用。
需要手工生成证书的情况有:

  1. 找不到可用的证书
  2. 需要配置双向SSL,但缺少客户端证书
  3. 需要对证书作特别的定制

首先,无论是在Linux下还是在Windows下的Cygwin中,进行下面的操作前都须确认已安装OpenSSL软件包。

 
创建根证书密钥文件(自己做CA)root.key
openssl genrsa -des3 -out root.key

输出内容为:

Generating RSA private key, 512 bit long modulus 
……………..++++++++++++ 
..++++++++++++ 
e is 65537 (0×10001) 
Enter pass phrase for root.key: ← 输入一个新密码 
Verifying – Enter pass phrase for root.key: ← 重新输入一遍密码

创建根证书的申请文件root.csr

openssl req -new -key root.key -out root.csr

输出内容为:

Enter pass phrase for root.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) [AU]:CN ← 国家代号,中国输入CN 
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音 
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音 
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 
Organizational Unit Name (eg, section) []: ← 可以不输入 
Common Name (eg, YOUR name) []: ← 此时不输入 
Email Address []:admin@mycompany.com ← 电子邮箱,可随意填

Please enter the following ‘extra’ attributes 
to be sent with your certificate request 
A challenge password []: ← 可以不输入 
An optional company name []: ← 可以不输入

创建一个自当前日期起为期十年的根证书root.crt:

openssl x509 -req -days 3650 -sha1 -extensions v3_ca -signkey root.key -in root.csr -out root.crt

输出内容为:

Signature ok 
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./emailAddress=admin@mycompany.com 
Getting Private key 
Enter pass phrase for root.key: ← 输入前面创建的密码

创建服务器证书密钥server.key

openssl genrsa -des3 -out server.key 2048

输出内容为:

Generating RSA private key, 2048 bit long modulus 
….+++ 
…………………………………………..+++ 
e is 65537 (0×10001)

运行时会提示输入密码,此密码用于加密key文件(参数des3便是指加密算法,当然也可以选用其他你认为安全的算法.),以后每当需读取此文件(通过openssl提供的命令或API)都需输入口令.如果觉得不方便,也可以去除这个口令,但一定要采取其他的保护措施! 
去除key文件口令的命令: 
openssl rsa -in server.key -out server.key

创建服务器证书的申请文件server.csr

openssl req -new -key server.key -out server.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) [AU]:CN ← 国家名称,中国输入CN 
State or Province Name (full name) [Some-State]:BeiJing ← 省名,拼音 
Locality Name (eg, city) []:BeiJing ← 市名,拼音 
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 
Organizational Unit Name (eg, section) []: ← 可以不输入 
Common Name (eg, YOUR name) []:www.mycompany.com ← 服务器主机名,若填写不正确,浏览器会报告证书无效,但并不影响使用 
Email Address []:admin@mycompany.com ← 电子邮箱,可随便填

Please enter the following ‘extra’ attributes 
to be sent with your certificate request 
A challenge password []: ← 可以不输入 
An optional company name []: ← 可以不输入

创建自当前日期起有效期为期两年的服务器证书server.crt

openssl x509 -req -days 730 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAserial root.srl -CAcreateserial -in server.csr -out server.crt

输出内容为:

Signature ok 
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/emailAddress=admin@mycompany.com 
Getting CA Private Key 
Enter pass phrase for root.key: ← 输入前面创建的密码

创建客户端证书密钥文件client.key

openssl genrsa -des3 -out client.key 2048

输出内容为:

Generating RSA private key, 2048 bit long modulus 
……………………………………………………………………………..+++ 
……………………………………………………………………………………………………….+++ 
e is 65537 (0×10001) 
Enter pass phrase for client.key: ← 输入一个新密码 
Verifying – Enter pass phrase for client.key: ← 重新输入一遍密码

创建客户端证书的申请文件client.csr

openssl req -new -key client.key -out client.csr

输出内容为:

Enter pass phrase for client.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) [AU]:CN ← 国家名称,中国输入CN 
State or Province Name (full name) [Some-State]:BeiJing ← 省名称,拼音 
Locality Name (eg, city) []:BeiJing ← 市名称,拼音 
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 
Organizational Unit Name (eg, section) []: ← 可以不填 
Common Name (eg, YOUR name) []:Lenin ← 自己的英文名,可以随便填 
Email Address []:admin@mycompany.com ← 电子邮箱,可以随便填

Please enter the following ‘extra’ attributes 
to be sent with your certificate request 
A challenge password []: ← 可以不填 
An optional company name []: ← 可以不填

创建一个自当前日期起有效期为两年的客户端证书client.crt

openssl x509 -req -days 730 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAserial root.srl -CAcreateserial -in client.csr -out client.crt

输出内容为:

Signature ok 
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/emailAddress=admin@mycompany.com 
Getting CA Private Key 
Enter pass phrase for root.key: ← 输入上面创建的密码

将客户端证书文件client.crt和客户端证书密钥文件client.key合并成客户端证书安装包client.pfx

openssl pkcs12 -export -in client.crt -inkey client.key -out client.pfx

常见的数字证书格式

cer后缀的证书文件有两种编码-->DER二进制编码或者BASE64编码(也就是.pem) p7b一般是证书链,里面包括1到多个证书pfx是指以pkcs#12格式存储的证书和相应私钥。在Security编程中,有几种典型的密码交换信息文件格式: 

DER-encoded certificate: .cer, .crt
PEM-encoded message: .pem
PKCS#12 Personal Information Exchange: .pfx, .p12
PKCS#10 Certification Request: .p10
PKCS#7 cert request response: .p7r   
PKCS#7 binary message: .p7b
.cer/.crt是用于存放证书,它是2进制形式存放的,不含私钥。
.pem跟crt/cer的区别是它以Ascii来表示。
pfx/p12用于存放个人证书/私钥,他通常包含保护密码,2进制方式
p10是证书请求
p7r是CA对证书请求的回复,只用于导入
p7b以树状展示证书链(certificate chain),同时也支持单个证书,不含私钥。

Java KeyStore的类型

JKS和JCEKS是Java密钥库(KeyStore)的两种比较常见类型(我所知道的共有5种,JKS, JCEKS, PKCS12, BKS,UBER)。

JKS的Provider是SUN,在每个版本的JDK中都有,JCEKS的Provider是SUNJCE,1.4后我们都能够直接使用它。

JCEKS在安全级别上要比JKS强,使用的Provider是JCEKS(推荐),尤其在保护KeyStore中的私钥上(使用TripleDes)。

PKCS#12是公钥加密标准,它规定了可包含所有私钥、公钥和证书。其以二进制格式存储,也称为 PFX 文件,在windows中可以直接导入到密钥区,注意,PKCS#12的密钥库保护密码同时也用于保护Key。

证书导入

要从某个文件中导入某个证书,使用keytool工具的-import命令: 

keytool -import -file mycert.der -keystore mykeystore.jks 

可使用-list命令来查看密钥仓库里的内容:

keytool -list -rfc -keystore mykeystore.jks  

P12格式证书导入:

keytool无法直接导入PKCS12文件。

第一种方法是使用IE将pfx证书导入,再导出为cert格式文件。使用上面介绍的方法将其导入到密钥仓库中。这样的话仓库里面只包含了证书信息,没有私钥内容。     第二种方法是将pfx文件导入到IE浏览器中,再导出为pfx文件。

新生成的pfx不能被导入到keystore中,报错:keytool错误: java.lang.Exception: 所输入的不是一个

X.509 认证。新生成的pfx文件可以被当作keystore使用。但会报个错误as unknown attr1.3.6.1.4.1.311.17.1

查了下资料,说IE导出的就会这样,使用Netscape就不会有这个错误.

第三种方法是将pfx文件当作一个keystore使用。但是通过微软的证书管理控制台生成的pfx文件不能直接使用。

keytool不认此格式,报keytool错误: java.io.IOException: failed to decrypt safe contents entry。

通过OpenSSL转换一下:

openssl pkcs12 -in mycerts.pfx -out mycerts.pem
openssl pkcs12 -export -in mycerts.pem -out mykeystore.p12 

通过keytool的-list命令可检查下密钥仓库中的内容:

keytool -rfc -list -keystore mykeystore.p12 -storetype pkcs12

这里需要指明仓库类型为pkcs12,因为缺省的类型为jks。这样此密钥仓库就即包含证书信息也包含私钥信息。

P7B格式证书导入:

keytool无法直接导入p7b文件。

需要将证书链RootServer.p7b(包含根证书)导出为根rootca.cer和子rootcaserver.cer 。

将这两个证书导入到可信任的密钥仓库中。

keytool -import -alias rootca -trustcacerts -file rootca.cer -keystore testkeytrust.jks

遇到是否信任该证书提示时,输入y

keytool -import -alias rootcaserver -trustcacerts -file rootcaserver.cer -keystore testkeytrust.jks 

Tomcat 使用 p12

修改$catalina_home/conf/server.xml

 <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
  maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
  clientAuth="false" sslProtocol="TLS" keystoreFile="/home/server.p12" keystoreType="PKCS12" keystorePass="820123" />

Keytool 命令参考

http://blog.sina.com.cn/s/blog_69ede63a0100z63h.html

http://blog.chinaunix.net/uid-17102734-id-2830223.html

证书流程图

http://blog.liuts.com/post/141/

Openssl 生成证书

http://blog.csdn.net/atco/article/details/7590069

数字证书格式

http://wenku.baidu.com/link?url=nDChCPapZZW4_YcCmeMvAJiCVDsorc6vXKXIfnPvLlGG-LPtH0ci3XivgWgn96i9VIp2NZXMCVrhxmVn4t1QuFXta6LQ3uFAaFYQDMaMJ8u

生成
NGINX证书

http://blog.csdn.net/babydavic/article/details/8442817

原文地址:https://www.cnblogs.com/saintaxl/p/4138959.html