node
若是您不熟悉證書籤名請求(CSRs), 先讀第一節git
除了第一節, 這份文檔以備忘紙條(cheat sheet)的格式編寫, 包含對應的命令行算法
直接查看和問題相關的章節瀏覽器
大多數命令行爲了淺顯易讀, 寫成了多行的格式安全
若是須要從certificate authority(CA)獲取一份SSL證書, 你須要先生成一份certificate signing request(CSR). 一個CSR包含的主要信息是key pair中的公鑰, 以及一些附加的信息. 在被簽名時這些信息都會被塞進證書.bash
不管何時, 建立一個CSR, 都會提示你輸入一些證書相關的信息, 這些信息被稱爲Distinguished Name(DN). 在DN中最重要的字段就是Common Name (CN), 其內容應當與你將要使用此證書的主機的Fully Qualified Domain Name (FQDN)徹底一致. 在建立CSR時能夠在命令行中傳遞信息來跳過交互填寫的步驟.服務器
在DN中提供的其餘字段是關於你的組織或業務的信息, 若是你從一個certificate authority購買了一份SSL證書, 這些字段例如"Organization"須要和你的機構一致.dom
下面是一個CSR的信息填寫交互的例子工具
Country Name (2 letter code) [AU]:US State or Province Name (full name) [Some-State]:New York Locality Name (eg, city) []:Brooklyn Organization Name (eg, company) [Internet Widgits Pty Ltd]:Example Brooklyn Company Organizational Unit Name (eg, section) []:Technology Division Common Name (e.g. server FQDN or YOUR name) []:examplebrooklyn.com Email Address []:
若是你打算避免交互式的填寫, 你能夠使用 -subj
參數來輸入這些信息, 下面是一個例子, 輸入的信息和前面交互式的是同樣的ui
-subj "/C=US/ST=New York/L=Brooklyn/O=Example Brooklyn Company/CN=examplebrooklyn.com"
如今你應該瞭解CSR的含義了, 能夠根據須要查看下面的其餘章節
這一節介紹如何使用生成CSR相關的OpenSSL命令(以及生成私鑰, 若是尚未私鑰的話). CSR能夠用來從certificate authority請求SSL證書.
記住你能夠用-subj
參數來避免交互式填寫CSR信息.
若是你打算用HTTPS來加強你的Apache HTTP或Nginx服務器, 而且用一個Certificate Authority(CA)來簽發SSL證書, 你須要使用這個. 生成一份CSR而後發送給CA要求其簽發一份CA-signed SSL證書. 若是你的CA支持SHA-2, 在參數中添加-sha256
來生成SHA-2簽名的CSR.
下面的命令用於生成一份2048-bit的私鑰(domain.key), 和一份CSR(domain.csr)
openssl req \ -newkey rsa:2048 -nodes -keyout domain.key \ -out domain.csr
在彈出交互中填寫相關信息
-newkey rsa:2048
參數用於指定私鑰長度爲2048bit, 而且使用RSA算法. -nodes
參數指定私鑰不須要密碼加密. -new
參數, 這裏沒使用可是實際上包含了, 用於指定生成CSR.
若是手裏已經有一份私鑰了, 要用這個私鑰來從CA那裏請求證書, 能夠用這個方法.
下面的命令用於根據存在的私鑰(domain.key)來生成新的CSR(domain.csr)
openssl req \ -key domain.key \ -new -out domain.csr
在彈出交互中填寫相關信息
-key
參數用於指定現有的用於生成CSR的私鑰文件, -new
參數用於指定生成CSR.
若是你手裏有私鑰, 想續簽一個證書, 可是你的CA卻找不到原先的CSR了, 這個方法能夠省掉你從新輸入CSR信息的時間, 會從當前的證書中提取相應的信息.
下面的命令用於根據存在的私鑰(domain.key)和證書(domain.crt)生成一個新的CSR(domain.csr)
openssl x509 \ -in domain.crt \ -signkey domain.key \ -x509toreq -out domain.csr
-x509toreq
參數用於說明使用的是X509證書來生成CSR
若是你須要用SSL證書來增強服務安全性, 可是不想經過CA來簽發證書, 一個可行而且免費的方案就是本身簽發證書.
能夠本身簽發的證書就稱爲自簽名證書. 自簽名證書表示這個證書是使用本身的私鑰來簽名的. 自簽名證書也能夠像CA簽發的證書同樣, 用來加密數據, 可是你的用戶將會看到一個警告, 提示他們這個證書並非他們的操做系統或瀏覽器信任的. 所以自簽發證書僅適用於你不須要跟用戶證實本身身份真實性的場合.
這一節介紹自簽名證書相關的OpenSSL命令.
若是你須要用HTTPS來增強Apache HTTP或Nginx的服務安全性, 可是不想經過CA來簽發證書, 能夠使用這個方法.
下面的命令建立了一個2048bit的私鑰(domain.key)和一個自簽名證書(domain.crt)
openssl req \ -newkey rsa:2048 -nodes -keyout domain.key \ -x509 -days 365 -out domain.crt
在彈出交互中填寫相關信息
-x509
參數用於告訴req要建立一個自簽名證書. -days 365
參數指定有效期爲365天. 一個臨時的CSR會同時被建立, 用於收集關聯的證書相關的信息.
若是你手裏有私鑰, 想用這個私鑰來簽發一個自簽名證書.
下面的命令用現有的私鑰(domain.key)生成一個自簽名證書(domain.crt)
openssl req \ -key domain.key \ -new \ -x509 -days 365 -out domain.crt
在彈出交互中填寫相關CSR信息
-x509
參數告訴req要建立一個自簽名證書, -days 365
參數指定有效期爲365天. -new
參數用於開啓CSR信息填寫
若是你手裏有私鑰和CSR, 想經過它們生成一個自簽名證書.
下面的命令用於根據現有的私鑰(domain.key)和CSR(domain.csr)生成一個新的自簽名證書(domain.crt)
openssl x509 \ -signkey domain.key \ -in domain.csr \ -req -days 365 -out domain.crt
-days 365
參數用於指定有效期爲365天.
證書和CSR編碼是PEM格式, 不是文本, 要經過命令進行查看. 這一節介紹如何使用OpenSSL命令查看PEM編碼格式的文件內容.
下面的命令用於明文展現CSR(domain.csr)的內容
openssl req -text -noout -verify -in domain.csr
下面的命令用於明文展現證書(domain.crt)的內容
openssl x509 -text -noout -in domain.crt
下面的命令用於校驗使用CA證書(ca.crt)簽發的證書(domain.crt)
openssl verify -verbose -CAFile ca.crt domain.crt
這一節介紹建立和校驗私鑰的OpenSSL命令
使用下面的命令建立一個有密碼保護的2048bit私鑰(domain.key)
openssl genrsa -des3 -out domain.key 2048
過程當中須要輸入密碼
使用下面的命令檢查一個私鑰(domain.key)是否有效
openssl rsa -check -in domain.key
若是私鑰是被加密的, 會彈出提示讓輸入密碼, 若是密碼正確, 界面會展現解密後的私鑰.
使用下面的命令檢查私鑰(domain.key)是否與證書(domain.crt)和CSR(domain.csr)匹配
openssl rsa -noout -modulus -in domain.key | openssl md5 openssl x509 -noout -modulus -in domain.crt | openssl md5 openssl req -noout -modulus -in domain.csr | openssl md5
若是上面命令的輸出是相同的, 說明這三個文件相關的可能性很是高
下面的命令使用一個未加密的私鑰(unencrypted.key)生成一個加密的私鑰(encrypted.key)
openssl rsa -des3 \ -in unencrypted.key \ -out encrypted.key
過程當中須要輸入密碼
下面的命令使用加密的私鑰(encrypted.key)輸出解密的私鑰(decrypted.key)
openssl rsa \ -in encrypted.key \ -out decrypted.key
過程當中須要輸入密碼
以上生成的證書都是ASCII PEM編碼的X.509證書. 實際使用中存在多種編碼格式和容器類型的證書, 各類應用有各自使用的證書類型, 而不少證書會在同一個文件裏包含多種類型的信息例如私鑰, 證書, CA證書.
OpenSSL能夠用於在多種證書格式之間進行轉換. 這一節介紹一些經常使用的轉換.
下面的命令用於將PEM編碼的證書(domain.crt)轉換爲DER編碼的二進制格式證書(domain.der)
openssl x509 \ -in domain.crt \ -outform der -out domain.der
DER格式主要用於Java
下面的命令用於將DER編碼的證書(domain.der)轉換爲PEM編碼證書(domain.crt)
openssl x509 \ -inform der -in domain.der \ -out domain.crt
下面的命令用於將PEM編碼的證書(domain.crt and ca-chain.crt) 添加到PKCS7文件(domain.p7b):
openssl crl2pkcs7 -nocrl \ -certfile domain.crt \ -certfile ca-chain.crt \ -out domain.p7b
注意你能夠使用一個或多個-certfile
參數指定須要添加到PKS7文件的證書.
PKCS7文件, 也稱P7B, 常使用於Java Keystores和Microsoft IIS (Windows). 是一種ASCII格式的文件, 能夠包含證書和CA證書.
Use this command if you want to convert a PKCS7 file (domain.p7b) to a PEM file:
openssl pkcs7 \ -in domain.p7b \ -print_certs -out domain.crt
Note that if your PKCS7 file has multiple items in it (e.g. a certificate and a CA intermediate certificate), the PEM file that is created will contain all of the items in it.
Use this command if you want to take a private key (domain.key) and a certificate (domain.crt), and combine them into a PKCS12 file (domain.pfx):
openssl pkcs12 \ -inkey domain.key \ -in domain.crt \ -export -out domain.pfx
You will be prompted for export passwords, which you may leave blank. Note that you may add a chain of certificates to the PKCS12 file by concatenating the certificates together in a single PEM file (domain.crt) in this case.
PKCS12 files, also known as PFX files, are typically used for importing and exporting certificate chains in Micrsoft IIS (Windows).
Use this command if you want to convert a PKCS12 file (domain.pfx) and convert it to PEM format (domain.combined.crt):
openssl pkcs12 \ -in domain.pfx \ -nodes -out domain.combined.crt
Note that if your PKCS12 file has multiple items in it (e.g. a certificate and private key), the PEM file that is created will contain all of the items in it.
openssl version
命令能夠用於檢查當前使用的OpenSSL版本, 以及編譯時使用的參數. 這些會影響到實際能使用的功能.
下面的命令用於展現版本和編譯參數信息
openssl version -a
上面的文檔使用的OpenSSL參數爲:
OpenSSL 1.0.1f 6 Jan 2014 built on: Mon Apr 7 21:22:23 UTC 2014 platform: debian-amd64 options: bn(64,64) rc4(16x,int) des(idx,cisc,16,int) blowfish(idx) compiler: cc -fPIC -DOPENSSL_PIC -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -m64 -DL_ENDIAN -DTERMIO -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -Wl,-Bsymbolic-functions -Wl,-z,relro -Wa,--noexecstack -Wall -DMD32_REG_T=int -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM OPENSSLDIR: "/usr/lib/ssl"
以上的內容基本覆蓋了大部分使用OpenSSL處理SSL證書的功能, 除此之外, OpenSSL還有不少其餘的功能未介紹.