SSL/TLS工具

OPENSSL

以下都是我自己常用的指令,詳細資訊可以將關鍵字餵Google大神

測試連線:
	openssl s_client -showcerts -connect localhost:9092
產生證書及私鑰
	openssl req -x509 -new -nodes -sha256 -utf8 -days 356 -newkey rsa:2048 -keyout server.key -out server.crt -config ssl.cnf
	openssl req -x509 -new -nodes -sha256 -utf8 -days 3650 -newkey rsa:2048 -keyout server.key -out server.crt -config ssl.conf

產生PKCS12金鑰儲存庫
	openssl pkcs12 -export -out server.p12 -inkey server.key -in server.crt
// ssl.cnf
[req]
prompt = no
default_md = sha256
default_bits = 2048
distinguished_name = dn
x509_extensions = v3_req

[dn]
C = TW
ST = Taiwan
L = Taipei
O = Caster Inc.
OU = IT Department
emailAddress = admin@example.com
CN = localhost

[v3_req]
subjectAltName = @alt_names

[alt_names]
DNS.1 = *.localhost
DNS.2 = localhost
IP.1 = 127.0.0.1f

Keytool

以下都是我自己常用的指令,詳細資訊可以將關鍵字餵Google大神

Create Self signed certificate 流程

init keystore

  1. 建立一個keystore

2. 清空keystore

3. 確認keystore 內容

Create certificate

  1. init key config (ssl.cnf) 利用上述說的 ssl.cnf 去設置

  2. 生成一對密鑰及證書

3. 產生PKCS12金鑰儲存庫

4. 導入PKCS12金鑰儲存庫 to JKS

5. 導入證書進入keystore 信任庫

6. 將自簽憑證加入本機信任庫(mac OS版本) - 此動作會跳出電腦密碼驗證

Bug 參考網址

JKS無法透過keytool放入私鑰

Believe or not, keytool does not provide such basic functionality like importing private key to keystore. You can try this workaround with merging PKSC12 file with private key to a keystore:

Or just use more user-friendly KeyMan from IBM for keystore handling instead of keytool.

參考網址

轉換 PKCS12 to PEM

2023/12/19 - 追加轉換 PKCS12 to PEM 範例

  1. 生成測試用的 SSL key

    • 使用 OpenSSL 工具生成一對包含私鑰和公鑰的測試 SSL 金鑰 (server.key) 和憑證 (server.crt)。

    • -x509 表示生成自簽名的憑證,-newkey rsa:2048 表示生成一個包含 RSA 2048 位金鑰的新憑證。

  2. SSL key to PKCS12 檔案

    • 使用 OpenSSL 工具將產生的 SSL 金鑰和憑證轉換成 PKCS12 格式的檔案 (server.p12)。

    • -export 表示執行匯出操作,-inkey-in 分別指定私鑰和憑證的輸入檔案。

  3. 轉換 PKCS12 to PEM

    • 使用 OpenSSL 工具將 PKCS12 格式的檔案轉換為 PEM 格式的檔案 (server.pem)。

    • -nocerts 表示不包含憑證部分,-nodes 表示不加密私鑰,-out 指定輸出檔案。

Last updated