Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save claudhu/2470a914fbd66bd5a87968fecb251b8a to your computer and use it in GitHub Desktop.
Save claudhu/2470a914fbd66bd5a87968fecb251b8a to your computer and use it in GitHub Desktop.
Self Signed Certificate with Custom Root CA

建立根CA

建立根私鑰

注意: 此私鑰用於簽署憑證請求,任何人擁有此私鑰都可以用你的名義簽署證書,所以請保管於安全的位置

openssl genrsa -des3 -out rootCA.key 4096

如果你不想使用密碼保護私鑰,可以移除-des3的選項

建立並且自簽署根憑證

openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.crt

使用根私鑰建立根憑證,並將此憑證分配於所有需與我們建立信任關係之電腦上

建立證書 Create a certificate (Done for each server)

每一台server或者應用都需要一份來自CA的信任憑證

建立證書私鑰 Create the certificate key

openssl genrsa -out mydomain.com.key 2048

建立憑證簽發請求文件 Create the signing (csr

你需要撰寫一些憑證的資料細節,憑證簽發請求文件將還需要透過RootKey(就是你早先建立的RootKey)來產生證書

重要: 請記得當產生簽發請求文件時需要指定Common Name,Common Name可以服務器的IP位置或者Domain Name,否則憑證將會被拒絕

I will describe here two ways to gener

方法 A (互動式)

假如你透過此方式產生憑證,openssl將會詢問你一些產生證書的細節,包含Organization細節以及Common Name(CN),該CN就是為你的網路地址建立證書,例如mydomain.com

openssl req -new -key mydomain.com.key -out mydomain.com.csr

方法 B (預定義|一行完成)

這個方法產生的結果跟方法A一樣,但此方法比較適合自動化產出:)

openssl req -new -sha256 -key mydomain.com.key -subj "/C=US/ST=CA/O=MyOrg, Inc./CN=mydomain.com" -out mydomain.com.csr

如果你想要添加一些額外的設定你可以添加 -config 參數, 舉例來說我想要添加一些替代名稱到我我的證書之中

openssl req -new -sha256 \
    -key mydomain.com.key \
    -subj "/C=US/ST=CA/O=MyOrg, Inc./CN=mydomain.com" \
    -reqexts SAN \
    -config <(cat /etc/ssl/openssl.cnf \
        <(printf "\n[SAN]\nsubjectAltName=DNS:mydomain.com,DNS:www.mydomain.com")) \
    -out mydomain.com.csr

驗證CSR的內容

openssl req -in mydomain.com.csr -noout -text

透過 mydomain csr以及CA Root Key來產生證書

openssl x509 -req -in mydomain.com.csr -CA rootCA.crt -CAkey rootCA.key -CAcreateserial -out mydomain.com.crt -days 500 -sha256

驗證

openssl x509 -in mydomain.com.crt -text -noout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment