Using keytool to create certificate chain

  sonic0002        2015-12-17 07:09:33       50,218        1          English  简体中文  Tiếng Việt 

JDK 提供了一个命令行工具 -- keytool 用于处理密钥和证书的生成。此工具具有一组选项,可用于生成密钥、创建证书、导入密钥、安装证书和导出证书等。在本教程中,我们将演示如何使用 keytool 创建证书链。如果您想了解如何以编程方式创建证书链,请参考 Generate certificate in Java -- Certificate chain

首先,我们生成一对密钥,将用作 CA,其私钥将用于签署它发出的证书。

keytool -genkeypair -alias ca -keystore test.jks -dname "CN=CA" -storepass password -keypass password -ext bc=ca:true

这里注意使用 BasicaContraint 生成的扩展名来表明它是一个 CA。

然后,生成一对密钥,其证书将由上面的 CA 签署。

keytool -genkeypair -alias leaf -keystore test.jks -dname "CN=Leaf" -storepass password -keypass password

接下来,需要为“CN=Leaf”证书创建一个证书请求。

keytool -certreq -keystore test.jks -storepass password -alias leaf -file leaf.csr

现在使用上面生成的证书请求创建证书。

keytool -gencert -keystore test.jks -storepass password -alias ca -infile leaf.csr -outfile leaf.cer

将创建一个输出证书文件 leaf.cer。现在让我们看看它的内容。

keytool -printcert -file leaf.cer

证书将如下所示:

Owner: CN=Leaf
Issuer: CN=CA
Serial number: 49fdb896
Valid from: Thu Dec 17 20:44:51 CST 2015 until: Wed Mar 16 20:44:51 CST 2016
Certificate fingerprints:
	 MD5:  EE:C8:78:72:69:C0:45:2C:DA:C6:E5:A9:6C:F9:A6:33
	 SHA1: DC:21:3A:7A:6B:BE:55:2A:24:0D:A0:F3:7B:FA:AD:B7:B3:E9:6B:88
	 SHA256: 78:11:E2:42:37:66:16:1D:02:02:03:1B:36:91:FC:C9:98:10:28:43:B6:0E:A2:63:34:93:9F:77:EA:D9:15:AB
	 Signature algorithm name: SHA1withDSA
	 Version: 3

Extensions: 

#1: ObjectId: 2.5.29.35 Criticality=false
AuthorityKeyIdentifier [
KeyIdentifier [
0000: 24 67 26 EE 9F 42 BE BF   CE 9E 8B 4D 8E 1E BD AF  $g&..B.....M....
0010: 97 82 3D E4                                        ..=.
]
]

#2: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 23 19 D0 58 48 6B 41 4B   0E 24 42 3A FF 60 38 69  #..XHkAK.$B:.`8i
0010: F1 63 11 12                                        .c..
]
]

从证书中,我们可以看到它的颁发者是“CN=CA”。

此证书可以导入到其他密钥库或信任库中,以便将来用于验证服务器身份。

keytool -importcert -keystore test.jks -storepass password -file leaf.cer

或者可以将其安装到原始密钥库中,成为别名 leaf 的证书链的叶子证书。

keytool -importcert -keystore test.jks -storepass password -file leaf.cer -alias leaf

安装证书后,您会发现带有别名 leaf 的条目将包含两个证书的链。

Alias name: leaf
Creation date: Dec 17, 2015
Entry type: PrivateKeyEntry
Certificate chain length: 2
Certificate[1]:
Owner: CN=Leaf
Issuer: CN=CA
Serial number: 49fdb896
Valid from: Thu Dec 17 20:44:51 CST 2015 until: Wed Mar 16 20:44:51 CST 2016
Certificate fingerprints:
	 MD5:  EE:C8:78:72:69:C0:45:2C:DA:C6:E5:A9:6C:F9:A6:33
	 SHA1: DC:21:3A:7A:6B:BE:55:2A:24:0D:A0:F3:7B:FA:AD:B7:B3:E9:6B:88
	 SHA256: 78:11:E2:42:37:66:16:1D:02:02:03:1B:36:91:FC:C9:98:10:28:43:B6:0E:A2:63:34:93:9F:77:EA:D9:15:AB
	 Signature algorithm name: SHA1withDSA
	 Version: 3

Extensions: 

#1: ObjectId: 2.5.29.35 Criticality=false
AuthorityKeyIdentifier [
KeyIdentifier [
0000: 24 67 26 EE 9F 42 BE BF   CE 9E 8B 4D 8E 1E BD AF  $g&..B.....M....
0010: 97 82 3D E4                                        ..=.
]
]

#2: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 23 19 D0 58 48 6B 41 4B   0E 24 42 3A FF 60 38 69  #..XHkAK.$B:.`8i
0010: F1 63 11 12                                        .c..
]
]

Certificate[2]:
Owner: CN=CA
Issuer: CN=CA
Serial number: 768422fa
Valid from: Thu Dec 17 20:35:40 CST 2015 until: Wed Mar 16 20:35:40 CST 2016
Certificate fingerprints:
	 MD5:  19:FC:D0:AA:59:EA:52:DC:35:35:65:46:0A:AF:91:4F
	 SHA1: BA:17:1B:33:95:4E:1C:7B:68:B0:4C:EE:1C:D8:F4:6A:EE:25:8E:B0
	 SHA256: 99:5B:90:82:99:8D:79:FA:AA:E7:72:B1:C6:76:FE:2A:65:5F:B7:EF:C9:3C:A4:55:C6:27:E2:62:D3:01:99:79
	 Signature algorithm name: SHA1withDSA
	 Version: 3

Extensions: 

#1: ObjectId: 2.5.29.19 Criticality=false
BasicConstraints:[
  CA:true
  PathLen:2147483647
]

#2: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 24 67 26 EE 9F 42 BE BF   CE 9E 8B 4D 8E 1E BD AF  $g&..B.....M....
0010: 97 82 3D E4                                        ..=.
]
]

通过本教程,您应该能够创建任何类型的证书或证书链。显然,如果您想创建可在生产环境中使用的证书,则需要找到受信任的 CA,例如 VeriSign。

这只是一个介绍如何创建证书链的教程。如果您想了解密钥库,可以参考 Different types of keystore in Java -- Overview

JAVA  CERTIFICATE  CERTIFICATE CHAIN  KEYTOOL 

       

  RELATED


  1 COMMENT


nkosev [Reply]@ 2017-05-16 12:53:25

For a whole day i was using open_ssl and then found this article - made what i needed in 1/10 of time and it is actually working for me.

Thanks!



  RANDOM FUN

Is this car fully tested?

Is this car fully tested? Or is it doing the Ice Bucket Challenge for ALS?