用openssl 和 keytool 生成 SSL證書

SSL(Secure Sockets Layer (SSL) and Transport Layer Security (TLS))被設(shè)計(jì)為加強(qiáng)Web安全傳輸(HTTP/HTTPS/)的協(xié)議(事實(shí)上還有SMTP/NNTP等) ,默認(rèn)使用443端口

1. openssl vs keytool

openssl 適用范圍廣。

keytool 單獨(dú)針對(duì) java application

  • 需要安裝 jre 之后才會(huì)有 keytool
  • java只能用 Java Keystore,而它需要keytool 工具生成。
  • keystore 可以把私鑰和證書放一起,只用一個(gè)文件。

2. 什么是x509證書鏈

數(shù)字證書是現(xiàn)代互聯(lián)網(wǎng)中個(gè)體間相互信任的基石。

如果沒有了數(shù)字證書,那么也就沒有了各式各樣的電商平臺(tái)以及方便的電子支付服務(wù)。目前我們所提到的數(shù)字證書都是基于 ITU 制定的 X.509 標(biāo)準(zhǔn)。

簡單來說,數(shù)字證書就是一張附帶了數(shù)字簽名的信息表。

image

x509證書一般會(huì)用到三類文件,key,csr,crt。

Key是私用密鑰,openssl格式,通常是rsa算法。

csr是證書請(qǐng)求文件(certificate signing request),用于申請(qǐng)證書。在制作csr文件的時(shí)候,必須使用自己的私鑰來簽署申請(qǐng),還可以設(shè)定一個(gè)密鑰。

crt是CA認(rèn)證后的證書文件(certificate),簽署人用自己的key給你簽署的憑證。

3. openssl 方式

CA根證書的生成步驟

生成CA私鑰(.key)-->生成CA證書請(qǐng)求(.csr)-->自簽名得到根證書(.crt)(CA給自已頒發(fā)的證書)。

本質(zhì)上就是用私鑰去獲取證書,然后把這兩個(gè)文件一起放到server,以此來證明我就是我。

3.1 生成CA私鑰(.key)

openssl genrsa -out ca.key 2048

$ openssl genrsa -out ca.key 2048
Generating RSA private key, 2048 bit long modulus
...+++
...........................................................................................+++
e is 65537 (0x10001)

3.2 生成CA證書請(qǐng)求(.csr)

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

$ openssl req -new -key ca.key -out ca.csr
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
State or Province Name (full name) [Some-State]:Guangdong
Locality Name (eg, city) []:guangzhou
Organization Name (eg, company) [Internet Widgits Pty Ltd]:roytest
Organizational Unit Name (eg, section) []:unit name
Common Name (e.g. server FQDN or YOUR name) []:small-nginx
Email Address []:royzeng@mail.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

上面是交互式輸入,非交互的方式如下

$ openssl req -new -key ca.key -out ca.csr -subj "/C=CN/ST=Guangdong/L=Guangzhou/O=roytest/CN=small-nginx"

3.3 自簽名得到根證書(.crt)

openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt

$ openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt
Signature ok
subject=/C=CN/ST=Guangdong/L=guangzhou/O=roytest/OU=unit name/CN=small-nginx/emailAddress=royzeng@mail.com
Getting Private key

自簽名是免費(fèi)/測試的證書,瀏覽器默認(rèn)不認(rèn)可。

通常的方法是:提交CSR到證書公司(比如VeriSign,Inc),等對(duì)方發(fā)來證書。(當(dāng)然這是要花錢的)

3.4 把證書放到web 服務(wù)器,并配置生效

比如Nginx服務(wù)器,把 ca.key 和 ca.crt 放到 /etc/nginx/certs 目錄。修改/etc/nginx/nginx.conf

    server {
        listen       443 ssl http2 default_server;
        .....

        ssl_certificate "/etc/nginx/certs/ca.crt";
        ssl_certificate_key "/etc/nginx/certs/ca.key";

重啟服務(wù)生效

3.5 進(jìn)階:制作多域名的CSR文件

引入一個(gè)概念:SAN

SAN stands for “Subject Alternative Names” and this helps you to have a single certificate for multiple CN (Common Name).

Reduce SSL cost and maintenance by using a single certificate for multiple websites using SAN certificate

簡而言之,用SAN是為了省錢,一個(gè)證書給多個(gè)網(wǎng)址使用。如果用之前的交互方式來申請(qǐng)證書,根本沒有地方來輸入SAN,要解決這問題,需用到配置文件。

配置cnf文件

[ req ]
default_bits       = 2048 
distinguished_name = req_distinguished_name 
req_extensions     = req_ext 
prompt = no 
[ req_distinguished_name ]
countryName                = 所屬國家 
stateOrProvinceName        = 省份 
localityName               = 城市
organizationName           = 公司名稱 
organizationalUnitName     = 部門 
commonName                 = 域名 
[ req_ext ] 
subjectAltName = @alt_names 
[alt_names] 
DNS.1   = 域名 (注意,commonName 要在這里再寫一次)
DNS.2   = 子域名1
DNS.3   = 子域名2
...

The entries in SAN certificate:

  • CAN be a Fully Qualified Domain Name (FQDN).
  • CAN be a wildcard domain name (i.e. *.domain.com or *.store.domain.com) but NOT a multiple-level wildcard (like *.*.domain.com).

生成CSR

Create new Private Key and Certificate Signing Request

生成 private key 和 生成 CSR 合并成一步

openssl req -out ca.csr -newkey rsa:2048 -nodes -keyout private.key -config san.cnf

例子

$ openssl req -out ca.csr -newkey rsa:2048 -nodes -keyout ca.key -config san.cnf
Generating a 2048 bit RSA private key
.....................+++
......+++
writing new private key to 'ca.key'
-----

于是 ca.csr ca.key 都生成了,csr 用于申請(qǐng)證書。

4. keytool 方式

Keytool 是一個(gè)Java數(shù)據(jù)證書的管理工具 , Keytool將密鑰(key)和證書(certificates)存在一個(gè)稱為keystore的文件中。

這是java專用方式,過程跟openssl 類似。

  • 創(chuàng)建一個(gè)新的證書JKS(Java Key Store)文件(里面包含了一個(gè)新生成的服務(wù)器密鑰)
  • 生成證書請(qǐng)求 CSR(Certificate Signung Request)
  • 獲取簽名(或者自簽名)
  • 導(dǎo)入一個(gè)簽名后的證書文件到j(luò)ks文件中 (Add Data to the Keystore)

服務(wù)器配置可以使用私鑰+證書合并在一起的文件,如jks或者pkcs12文件,這類文件一般叫key.keystore。(openssl使用兩個(gè)文件)

4.1 生成新的證書文件(Java Key Store)

keytool -genkeypair -keystore certificate.jks -alias roykey -storetype pkcs12 -keyalg RSA -keysize 2048

$ keytool -genkeypair -alias cdserver -keystore jenkins-cd.jks -keyalg RSA -keysize
2048 -storetype PKCS12
Enter keystore password:
Re-enter new password:
What is your first and last name?
  [Unknown]:  www.myexample.com
What is the name of your organizational unit?
  [Unknown]:  IT service
What is the name of your organization?
  [Unknown]:  commany name
What is the name of your City or Locality?
  [Unknown]:  Guangzhou
What is the name of your State or Province?
  [Unknown]:  Guangdong
What is the two-letter country code for this unit?
  [Unknown]:  CN
Is CN=www.myexample.com, OU=IT service, O=commany name, L=Guangzhou, ST=Guangdong, C=CN correct?
  [no]:  yes

4.2 生成一個(gè)CSR(Certificate Signung Request)證書申請(qǐng)文件

keytool -certreq -keystore certificate.jks -alias roykey -file server.csr

$ keytool -certreq -alias cdserver -keystore jenkins-cd.jks -file jenkins-cd.csr

Enter keystore password:

4.3 用CA對(duì)請(qǐng)求文件進(jìn)行簽名

(openssl 自簽名參考上面)

測試階段,也可以用keytool 來實(shí)現(xiàn)自簽名(根據(jù)證書請(qǐng)求生成證書)。

keytool -gencert -keystore certificate.jks -alias roykey -infile server.csr -outfile ca.crt

$ keytool -gencert -keystore jenkins-cd.jks -alias cdserver -infile jenkins-cd.csr -outfile jenkins-cd.crt
Enter keystore password:

4.4 導(dǎo)入簽名后的證書文件到j(luò)ks文件中

keytool -importcert -alias roykey -file server.crt -keystore certificate.jks

$ keytool -importcert -alias cdserver -file jenkins-cd.crt -keystore jenkins-cd.jks
Enter keystore password:
Certificate reply was installed in keystore

4.5 配置java程序使用jks

用 jenkins 來舉例

--httpsKeyStore=${JENKINS_CONF_DIR}/jenkins_certificate.jks --httpsKeyStorePassword=the_password_you_chose_for_the_keystore_earlier

4.6 其它keytool 命令

查看單個(gè)證書

keytool -printcert -v -file mydomain.crt

列出keystore存在的所有證書

keytool -list -v -keystore keystore.jks

使用別名查看keystore特定條目

keytool -list -v -keystore keystore.jks -alias mydomain

刪除keystore里面指定證書

keytool -delete -alias mydomain -keystore keystore.jks

更改keysore密碼

keytool -storepasswd -new new_storepass -keystore keystore.jks

導(dǎo)出keystore里面的指定證書

keytool -export -alias mydomain -file mydomain.crt -keystore keystore.jks

參考文檔

https://docs.oracle.com/javase/8/docs/technotes/tools/unix/keytool.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 用openssl生成nginx.key 1.key的生成openssl genrsa -des3 -out ser...
    灝瑋閱讀 15,372評(píng)論 0 1
  • CA和證書安全協(xié)議(SSL/TLS)OpenSSH 一、CA和證書 (一) PKI(Public Key Infr...
    哈嘍別樣閱讀 1,518評(píng)論 0 0
  • SSL(Secure Socket Layer,安全套接字層)是為網(wǎng)絡(luò)通信提供安全保障和數(shù)據(jù)完整性的一種安全協(xié)議,...
    CHUANHAI閱讀 2,190評(píng)論 0 7
  • 我橫沖直撞 馬路上搖搖晃晃 雜樂、車鳴 哪里是一個(gè)方向 回頭是人群 向前是絕望 沒有一顆勇敢的心 你上哪去流浪 閉...
    荒蕪年月閱讀 73評(píng)論 0 0
  • 生命陪伴心語系統(tǒng): (當(dāng)下)此刻就是我享受愛,體驗(yàn)愛和表達(dá)愛的最大機(jī)會(huì) (過程)深呼吸一,二,三,我看見了我的情緒...
    洪蕓閱讀 201評(píng)論 0 8

友情鏈接更多精彩內(nèi)容