Try   HackMD

openSSL 自發憑證

簡介

以下範例中,A為傳送資料的人,B為接收資料的人,C為試圖攔截資料的人

  • 對稱式加密(Symmetric Encryption):傳送方與接收方使用同一把金鑰,傳送時使用這把密鑰加密,接收時使用這把密鑰解密。

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    優點:計算量小,因此加密速度快
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    缺點:當金鑰被知道後,其他人便可以透過金鑰輕鬆破譯加密訊息
    <流程>

    1. 傳送者A(server)接收者B(browser)先進行金鑰的握手(Handshake),讓雙方知道金鑰
    2. A將要傳資料的明文用金鑰加密
    3. B將收到的密文用金鑰解密
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
  • 非對稱式加密(Asymmetric Encryption):每個使用者都擁有一份私鑰(Private key)、公鑰(Public key)。私鑰本人自行留存,公鑰公開發布發給其他人,私鑰、公鑰皆可以加密或解密。當A傳送檔案給B時,A先獲得B發布的公鑰將檔案加密,傳送給B後B再用私鑰解密,即使中間被C竊取道檔案,沒有B的私鑰也無法閱讀。(當A將檔案加密後,若原檔案遺失,沒有B的私鑰,A也無法透過密文得知原文)
    <流程>

    1. A先使用B的公鑰將要傳送的明文加密
    2. B獲得密文後使用自己的私鑰解密
    3. 同時為確保傳送者身分不是假冒的,A要將數位簽章用自己的私鑰加密發給B
    4. B收到加密的數位簽章後用A的公鑰解密
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
  • ssl安全協議(Secure Socket Layer):
    讓web application能夠加密傳輸。
    測試時可使用自簽憑證,若是要等第三方簽發有效憑證,很耗時。
    主要功能:

    • 加密解密在網路中傳輸的資料包,同時保護這些資料不被修改和偽造。
    • 驗證網路對話中雙方的身份。
  • 數位憑證認證機構CA(Certificate Authority):權威單位發給A(個人或組織)一對公鑰和私鑰,並對外宣稱此公鑰確實代表A本人。CA除了負責發行數位憑證外,也需隨時提供使用者查詢數位憑證真偽的責任。

  • 數位簽章(Digital Signature):當A透過自己發布的公鑰傳送檔案給B,B要如何判斷檔案真的是A傳過來,而不是C假冒A傳送病毒檔?此時A使用他的私鑰加密數位簽章的明文並傳給B,B透過A發布的公鑰即可解密數位簽章,由於私鑰只由該使用者自己持有,故可以肯定該檔案必定出自於該使用者,主要用於陌生環境與陌生人溝通。

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • X509:國際標準證書結構

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

數位簽章與驗簽流程(CA 與 需要簽章的server之間)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

簽名

  1. CA機構對要簽屬的資料(傳送者提供的Data)進行雜湊運算生成摘要
  2. CA用CA的私鑰將結果加密形成簽章
  3. 將憑證(憑證有效期限、發行者、公鑰等資訊)與簽章形成數位簽章

驗簽

  1. 接收者將數位簽證中資料進行雜湊運算得雜湊值H
  2. 簽章用CA的公鑰解密後得雜湊值H'
  3. 若H=H',表示此數位簽證有效

ssl常用語法

  • genrsa:生成RSA私鑰
usage: genrsa [args] [numbits]       #第一個是選項參數
                                      第二個是生成金鑰長度,預設2048 bits
-out <檔案名稱>                       #輸出檔案
-des3                                #encrypt the generated key with DES in ede cbc mode (168 bit key)
                                      加密私鑰
  • rsa:對密鑰進行管理,如從私鑰提取公鑰、查看密鑰信息
usage: rsa [options]
-check                               #檢測密鑰合法性
-text                                #列印出私有金鑰的各個組成部分
-noout                               #不列印出key的編碼版本資訊
-in <檔案名稱>                        #輸入的檔案
-out <檔案名稱>                       #輸出檔案
例 $ openssl rsa -noout -text -in <key檔案名稱>        #可以查看key檔案的內容
  • req:CSR 文件的生成、查看、驗證、自簽名等功能
usage: req [options] <infile> outfile
-new                            #生成證書請求
-x509                           #生成自簽證書
-key                            #指定已有密鑰的文件
-newkey                         #在生成證書請求或者自簽名證書時自動生成密鑰
                                後方指定rsa:bits 產生rsa密鑰
                                (與key選項互斥)
-in <檔案名稱>                   #輸入的.csr憑證請求檔案
-out <檔案名稱>                  #輸出的.csr憑證請求檔案
-text                           #印出請求檔中的所有信息(不包含公鑰)
-noout                          #不要印出CSR檔案的編碼版本資訊
–verify                         #檢驗請求檔案裡的簽名資訊
例
$ openssl req -509 -in request.csr -out client.csr -key private.key
#使用private.key 私鑰對request.csr的證書請求文件進行簽名並輸出
  • x509:證書處理功能,顯示憑證的內容,轉換其格式,給CSR簽名等等
-in <檔案名稱>                   #要查詢檔案名稱
-pubkey                         #查看證書中的公鑰
-subject                        #列印出證書擁有者的名字

openssl設定實例(網頁伺服器獲得憑證)

  • rootCA 所有網站憑證的源頭,rootCA自身的憑證就叫根憑證(root certificate)。本次範例為CA伺服器,正式環境為第三方機構,測試時由本機擔任
  • hostCA 本次範例為提供自發簽憑證的CA
  • CSR (Certificate Signing Request) 憑證請求檔,檔名使用域名較好判別。CA要完成簽屬但發送者的私鑰不能交給其他人時,就要透過CSR,內容包括發送者公鑰、關於公司/個人資料的文件。
  • CRT 憑證格式
  • PEM or CRT 中繼憑證(intermediate certificate)格式

1.產生rootCA私鑰

$ openssl genrsa -des3 -out ca.key 4096

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

2.用設定檔req.conf及ca.key產rootCA 憑證ca.crt

$  openssl req -x509 -new -nodes -key ca.key -sha256 -days 3000 -out ca.crt -extensions req_ext -config req.conf

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

3.檢查剛產生的憑證ca.crt

 $ openssl x509 -noout -text -in ca.crt

4.host產私鑰host.key

$ openssl genrsa -out host.key 4096 

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

5.host產生憑證申請檔host.csr,要交給CA伺服器產生對應的憑證檔用

$ openssl req -new -key host.key -subj "/C=TW/ST=Taiwan/L=Taipei City/O=BOT/OU=IT/CN=tocp4.bot.com.tw" -sha256 -out host.csr -config req.conf

6.host產生自簽憑證檔host.crt (#這一步給第三方或組織的rootCA執行就不是自簽憑證,而是實際對外網頁服務)

openssl x509 -req -in host.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out host.crt -days 3000 -sha256 -extensions req_ext -extfile req.conf

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

7.檢查剛產生的憑證host.crt

$  openssl x509 -noout -text -in host.crt

參考連結

基本概念
非對稱式加密wiki
https連線圖解
數位簽證詳解
數位簽證詳解(英文)
密碼學原理
常用ssl指令
ssl命令詳解
rsa參數詳解
申請ssl過程
根憑證解釋