openssl及keytool生成证书命令行梳理

1 jdk生成工具keytool

keytool -genkey -alias xylink -keystore kserver.keystore
keytool -export -alias xylink -keystore kserver.keystore -file server.crt
keytool -import -alias xylink -file server.crt -keystore tclient.keystore

keytool -genkey -alias fwding -keystore kclient.keystore
keytool -export -alias fwding -keystore kclient.keystore -file client.crt
keytool -import -alias fwding -file client.crt -keystore tserver.keystore

2 查看keystore

keytool -list -v -keystore testkeys

如下所示为命令行执行结果

☁  resources [master] ⚡  keytool -list -v -keystore testkeys
输入密钥库口令:
密钥库类型: jks
密钥库提供方: SUN

您的密钥库包含 1 个条目

别名: localhost
创建日期: 2004-7-23
条目类型: PrivateKeyEntry
证书链长度: 1
证书[1]:
所有者: CN=localhost, OU=Widget Development Group, O="Ficticious Widgets, Inc.", L=Sunnyvale, ST=CA, C=US
发布者: CN=localhost, OU=Widget Development Group, O="Ficticious Widgets, Inc.", L=Sunnyvale, ST=CA, C=US
序列号: 41004446
有效期为 Fri Jul 23 06:48:38 CST 2004 至 Mon May 23 06:48:38 CST 2011
证书指纹:
	 MD5:  51:2C:B6:13:13:C5:AF:CC:C6:2F:AA:D8:65:E8:97:B5
	 SHA1: 59:0C:44:64:22:3F:05:91:EF:B7:93:33:81:C5:37:5F:A5:57:FC:BD
	 SHA256: 37:77:E0:DA:37:66:C9:49:75:24:66:BE:F9:D7:CA:3B:FF:5E:9A:F2:C3:A0:BD:0D:90:C2:DB:B4:9D:DE:68:4F
签名算法名称: MD5withRSA ()
主体公共密钥算法: 1024 位 RSA 密钥
版本: 1


*******************************************
*******************************************



Warning:
<localhost> 使用的 MD5withRSA 签名算法存在安全风险。
JKS 密钥库使用专用格式。建议使用 "keytool -importkeystore -srckeystore testkeys -destkeystore testkeys -deststoretype pkcs12" 迁移到行业标准格式 PKCS12。

3 keytool生成证书命令说明

以下描述来自文档:

【SSL】使用Keytool工具生成证书及签名完整步骤

keytool -genkeypair \
        -alias server-local \
        -keyalg RSA \
        -keysize 4096 \
        -keypass mypassword \
        -sigalg SHA256withRSA \
        -dname "cn=server-local,ou=xxx,o=xxx,l=wuhan,st=wuhan,c=CN" \
        -validity 3650 \
        -keystore server_keystore.jks \
        -storetype JKS \
        -storepass mypassword
  • keytool 是jdk提供的工具,该工具名为”keytool“
  • -alias www.mydomain.com 此处”www.mydomain.com“为别名,可以是任意字符,只要不提示错误即可。因一个证书库中可以存放多个证书,通过别名标识证书。
  • -keyalg RSA 此处”RSA“为密钥的算法。可以选择的密钥算法有:RSA、DSA、EC。
  • –keysize 4096 此处”4096“为密钥长度。keysize与keyalg默认对应关系:
    2048 (when using -genkeypair and -keyalg is “RSA”)
    1024 (when using -genkeypair and -keyalg is “DSA”)
    256 (when using -genkeypair and -keyalg is “EC”)
  • -keypass mypassword 此处”mypassword “为本条目的密码(私钥的密码)。最好与storepass一致。
  • -sigalg SHA256withRSA 此处”SHA256withRSA“为签名算法。keyalg=RSA时,签名算法有:MD5withRSA、SHA1withRSA、SHA256withRSA、SHA384withRSA、SHA512withRSA。keyalg=DSA时,签名算法有:SHA1withDSA、SHA256withDSA。此处需要注意:MD5和SHA1的签名算法已经不安全。
  • -dname “cn=www.mydomain.com,ou=xxx,o=xxx,l=Beijing,st=Beijing,c=CN” 在此填写证书信息。“CN=名字与姓氏/域名,OU=组织单位名称,O=组织名称,L=城市或区域名称,ST=州或省份名称,C=单位的两字母国家代码”
  • -validity 3650 此处”3650“为证书有效期天数。
  • -keystore www.mydomain.com_keystore.jks 此处”www.mydomain.com_keystore.jks“为密钥库的名称。此处也给出绝对路径。默认在当前目录创建证书库。
  • -storetype JKS 此处”JKS “为证书库类型。可用的证书库类型为:JKS、PKCS12等。jdk9以前,默认为JKS。自jdk9开始,默认为PKCS12。
  • -storepass mypassword 此处”mypassword “为证书库密码(私钥的密码)。最好与keypass 一致。

4 三方公正流程

在这里插入图片描述

4.1 生成秘钥证书库-keystore

01-server_keystore.jks

keytool -genkeypair \
        -alias server-local \
        -keyalg RSA \
        -keysize 4096 \
        -keypass server \
        -sigalg SHA256withRSA \
        -dname "cn=server-local,ou=xxx,o=xxx,l=wuhan,st=wuhan,c=CN" \
        -validity 3650 \
        -keystore 01-server_keystore.jks \
        -storetype JKS \
        -storepass server

4.2 生成证书签名-CSR

01-server.csr – 签名证书

GlobalSign_cert.cer – 签名机构证书

keytool -certreq -keyalg RSA \
        -alias server-local \
        -keystore 01-server_keystore.jks \
        -storetype JKS \
        -storepass server \
        -file 01-server.csr

4.3 向认证机构提交请求获取证书-CER

01-server.cer

4.4 将已签名证书导入客户端证书库

导入签名机构的根证书:

keytool -import -trustcacerts \
        -keystore 02-client_trust_keystore.jks \
        -storepass client \
        -alias root_GlobalSign \
        -file GlobalSign_cert.cer

导入服务器证书

keytool -import -trustcacerts \
        -keystore 02-client_trust_keystore.jks \
        -storepass client \
        -alias server_csr \
        -file 01-server.cer

5 jdk keytool自签名流程

5.1 生成秘钥证书库-keystore

keytool -genkeypair \
        -alias server-local \
        -keyalg RSA \
        -keysize 4096 \
        -keypass server \
        -sigalg SHA256withRSA \
        -dname "cn=server-local,ou=xxx,o=xxx,l=wuhan,st=wuhan,c=CN" \
        -validity 3650 \
        -keystore 01-server_keystore.jks \
        -storetype JKS \
        -storepass server

5.2 验证keystore

keytool -list -v -keystore 01-server_keystore.jks 

5.3 导出公钥证书-CER

注意这里与4.2不同,对应为4.3生成的cer证书

keytool -export -alias server-local -keystore 01-server_keystore.jks -rfc -file 01-server.cer

5.4 导入客户端信任keystore

Keytool -import -alias server-local -file 01-server.cer -keystore 02-client_trust.jks

6 openssl生成秘钥

pem与store相互转换

pem->pk12->keystore

#### 1- 生成私钥
openssl genrsa -out server.pem 1024
#### 2-创建证书
openssl req -new -out server.csr -key server.pem
#### 3-自签署证书
openssl x509 -req -in server.csr -out server.cer -signkey server.pem -days 3650
#### 4-将证书导出成浏览器支持的.p12格式
openssl pkcs12 -export -clcerts -in server.cer -inkey server.pem -out server.p12 -name localhost

7 openssl与keytool证书转换

pem->pk12->keystore

#### keytool -importkeystore -deststorepass 目标存储库口令 -destkeypass 目标密钥库口令 -destkeystore out.keystore(目标密钥库) -srckeystore out.pk12(源密钥库) -srcstoretype PKCS12(源存储类型) -srcstorepass 源存储库口令 -alias 别名

keytool -importkeystore -deststorepass server -destkeypass server  -destkeystore out.keystore -srckeystore server.p12 -srcstoretype PKCS12 -srcstorepass server -alias localhost

p12–>pem

openssl pkcs12 -nocerts -nodes -in testkeys -out test.pem
openssl rsa -in test.pem > clearkey.pem
发布了88 篇原创文章 · 获赞 16 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/xinquanv1/article/details/104118552
今日推荐