天天看點

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