天天看点

SSL安全证书生成及概念解释SSL安全证书-概念解析SSL安全证书生成

SSL安全证书-概念解析

一、关于证书

数字证书是一种认证机制。简单点说,它代表了一种由权威机构颁发授权的安全标志。

由来

在以前,传统网站采用HTTP协议进行数据传输,所有的数据几乎都用的明文,很容易发生隐私泄露。为了解决安全问题,大家开始考虑采用加解密的方案,于是乎诞生了公钥加密(非对称加解密)及签名算法。浏览器从服务端得到公钥,经过协商并生成动态密钥,此后所有的请求响应都基于动态密钥加解密。然而对于浏览器而言,是不是所有声称了 HTTPS 的服务器都值得信任呢。答案是否定的,服务器必须提供一个凭证以证明自己值得信任,于是乎这就有了证书,通常的证书里面则包含了公钥。浏览器与服务器进行加密数据传输的前提是服务器证书受到信任,即存在于浏览器的受信任证书列表中。

二、PKI - 公钥基础设施

Public Key Infrastructure,是基于公开密钥技术所构建的,用以解决网络安全问题的通用基础平台。其服务范围包括公钥管理、提供认证、加密、完整性和可追究性服务。

PKI 几乎可以代言整个公钥技术体系标准。从概念上,PKI 涵盖了 PMI (权限管理),然而实质上 PKI 不仅如此,目前只要是基于公钥技术实现网络安全的所有协议、组件、服务等都从属于 PKI,包括上述的证书。

PKI 的关键元素:

1 数字证书 Certificate

2 证书签署机构 CA 及批准机构 RA

3 存储目录

4 证书策略、证书路径及使用者

三、CA - 证书授权中心

Certificate Authority,CA 是负责发放并管理数字证书的第三方权威机构,它负责管理 PKI 体系中的所有组织、个人、以及他们持有的的数字证书,将用户的公钥及用户的其他信息捆绑在一起,在网上验证用户的身份。CA机构的数字签名使得攻击者不能伪造和篡改证书。

SSL安全证书生成及概念解释SSL安全证书-概念解析SSL安全证书生成

CA 的层级结构

CA建立自上而下的信任链,下级CA信任上级CA,下级CA由上级CA颁发证书并认证

如github的证书层级:

CA 的功能:

证书颁发:接收、验证及受理用户(包括下级认证中心和最终用户)的数字证书的申请。

证书更新:认证中心可以定期更新所有用户的证书,或者根据用户的请求来更新用户的证书

证书查询:查询当前用户证书申请处理过程;查询用户证书的颁发信息,这类查询由目录服务器LDAP来完成

证书作废:由于用户私钥泄密等原因,需要向认证中心提出证书作废的请求;证书已经过了有效期,认证中心自动将该证书作废。认证中心通过维护证书作废列表 (Certificate Revocation List,CRL) 来完成上述功能。

证书的归档:证书具有一定的有效期,证书过了有效期之后就将作废,但是我们不能将作废的证书简单地丢弃,因为有时我们可能需要验证以前的某个交易过程中产生的数字签名,这时我们就需要查询作废的证书。

来源:

四、Certificates 数字证书

主要构成

  1. 申请者信息;
  2. 申请者公钥;
  3. 签发机构CA及数字签名
  4. 证书有效期

证书标准

  1. x.509 是PKI 体系中最基础的标准,它最先定义了公钥证书的基本结构:

    SSL公钥证书

    证书废除列表CRL(Certificate revocation lists)

  2. PKCS#12

    windows 平台及 mac平台使用的证书标准,通常使用 pfx/p12 作为文件扩展名,

    该标准在X509的基础之上增加了私钥及存取密码。

编码格式

PEM - Privacy Enhanced Mail, BASE64编码,可读

Apache和Unix/Linux 服务器采用的编码格式.

DER - Distinguished Encoding Rules,二进制格式,不可读.

Windows 服务器采用的编码格式.

文件扩展名

pem/der 数字证书,编码格式与其名称对应;

crt 数字证书,常见于unix/linux系统;

cer 数字证书,常见于windows系统;

key 非证书,一般是公钥或私钥文件;

csr certificate signing request,证书签名请求文件;

pfx/p12 - predecessor of PKCS#12,是PKCS#12 标准的证书文件,

同时包含了公钥和私钥,存取时需提供密码,采用DER 编码

此文章转自:

SSL安全证书生成及概念解释SSL安全证书-概念解析SSL安全证书生成

作者: zale

出处: http://www.cnblogs.com/littleatp/, 如果喜欢我的文章,请关注我的公众号

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 原文链接  如有问题, 可留言咨询.

SSL安全证书生成

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

需要手工生成证书的情况有:

找不到可用的证书

需要配置双向SSL,但缺少客户端证书

需要对证书作特别的定制

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

1. 创建根证书密钥文件(自己做CA)root.key:

openssl genrsa -des3 -out root.key

输出内容为:

[[email protected] ~]$ 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: ← 重新输入一遍密码

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

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

输出内容为:

[[email protected] ~]$ 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]:SiChuan ← 省的全名,拼音 

Locality Name (eg, city) []:SiChuan ← 市的全名,拼音 

Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 

Organizational Unit Name (eg, section) []: ← 可以不输入 

Common Name (eg, YOUR name) []: ← 此时不输入 

Email Address []:[email protected] ← 电子邮箱,可随意填

Please enter the following ‘extra’ attributes 

to be sent with your certificate request 

A challenge password []: ← 可以不输入 

An optional company name []: ← 可以不输入

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

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

输出内容为:

[[email protected] ~]$ openssl x509 -req -days 3650 -sha1 -extensions v3_ca -signkey root.key -in root.csr -out root.crt 

Signature ok 

subject=/C=CN/ST=SiChuan/L=SiChuan/O=MyCompany Corp./[email protected] 

Getting Private key 

Enter pass phrase for root.key: ← 输入前面创建的密码

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

openssl genrsa –des3 -out server.key 2048

输出内容为:

[[email protected] ~]$ openssl genrsa -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

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

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

输出内容为:

[[email protected] ~]$ openssl req -new -key server.key -out server.req 

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]:SiChuan ← 省名,拼音 

Locality Name (eg, city) []:SiChuan ← 市名,拼音 

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 []:[email protected] ← 电子邮箱,可随便填

Please enter the following ‘extra’ attributes 

to be sent with your certificate request 

A challenge password []: ← 可以不输入 

An optional company name []: ← 可以不输入

6. 创建自当前日期起有效期为期两年的服务器证书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

输出内容为:

[[email protected] ~]$ openssl x509 -req -days 730 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAcreateserial -in server.csr -out server.crt 

Signature ok 

subject=/C=CN/ST=SiChuan/L=SiChuan/O=MyCompany Corp./CN=www.mycompany.com/[email protected] 

Getting CA Private Key 

Enter pass phrase for root.key: ← 输入前面创建的密码

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

openssl genrsa -des3 -out client.key 2048

输出内容为:

[[email protected] ~]$ 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: ← 重新输入一遍密码

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

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

输出内容为:

[[email protected] ~]$ 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]:SiChuan ← 省名称,拼音 

Locality Name (eg, city) []:SiChuan ← 市名称,拼音 

Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 

Organizational Unit Name (eg, section) []: ← 可以不填 

Common Name (eg, YOUR name) []:Lenin ← 自己的英文名,可以随便填 

Email Address []:[email protected] ← 电子邮箱,可以随便填

Please enter the following ‘extra’ attributes 

to be sent with your certificate request 

A challenge password []: ← 可以不填 

An optional company name []: ← 可以不填

9. 创建一个自当前日期起有效期为两年的客户端证书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

输出内容为:

[[email protected] ~]$ openssl x509 -req -days 730 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAcreateserial -in client.csr -out client.crt 

Signature ok 

subject=/C=CN/ST=SiChuan/L=SiChuan/O=MyCompany Corp./CN=www.mycompany.com/[email protected] 

Getting CA Private Key 

Enter pass phrase for root.key: ← 输入上面创建的密码

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

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

输出内容为:

[[email protected] ~]$ openssl pkcs12 -export -in client.crt -inkey client.key -out client.pfx 

Enter pass phrase for client.key: ← 输入上面创建的密码 

Enter Export Password: ← 输入一个新的密码,用作客户端证书的保护密码,在客户端安装证书时需要输入此密码 

Verifying – Enter Export Password: ← 确认密码

11. 保存生成的文件备用,其中server.crt和server.key是配置单向SSL时需要使用的证书文件,client.crt是配置双向SSL时需要使用的证书文件,client.pfx是配置双向SSL时需要客户端安装的证书文件

.crt文件和.key可以合到一个文件里面,把2个文件合成了一个.pem文件(直接拷贝过去就行了)

转自:https://blog.csdn.net/imilli/article/details/72843971