android安全学习之4—结合java中keytool工具理解数字证书


At an administrative level, keys are managed by keytool, a utility supplied with the JRE. This tool allows you to create new keys, import digital certificates, export existing keys, and generally interact with the key management system.

keytool是和JRE一块的一个软件。可以创建新key,导入数字证书,导出已有的key,和key management 系统进行交互。
本文后续将首先介绍keytool的常用命令,然后一步一步利用keytool生成private key和数字证书,然后再提供给CA进行签名,直到形成可用的CA签署颁发的数字证书。

全局的选项

下面是keytool的常用选项:

  • −alias alias

Specify the alias the operation should apply to (e.g., −alias sdo). The default for this value is “mykey.”

  • −dname distinguishedName

Specify the distinguished name. There is no default for this value, and if you do not specify it on the command line, you will be prompted to enter it when it is needed. Letting keytool prompt you is generally easier since the tool will prompt for the name one field at a time. Otherwise, you must enter the entire name in one quoted string, like this:
−dname \
“CN=Scott Oaks, OU=JSD, O=Sun Microsystems, L=NY, S=NY, C=US”

  • −keypass password

Specify the password used to protect the entire keystore. Access to any element in the keystore requires this global password. If this password is not provided on the command line, you will be prompted for it. This is more secure than typing it on a command line or in a script where others might see it. Passwords must be at least six characters long.
For certain commands, the password may be omitted.
类似于给这个keystore设置一个密码,当要访问这个keystore中任何东西的时候,都要输入这个密码。

  • −keystore filename

Specify the name of the file that holds the keystore. The default value is$HOME/.keystore,as described before.

  • −storepass password

Specify the password used to protect a particular entry’s private key. This is usually not (and should not be) the same as the global password. There should be a different password for each private key that is specific to that entry. This allows the keystore to be shared among many users. If the password is not provided on the command line, you will be prompted for it, which is the more secure way to enter this password.
这个密码的作用主要是为了很多用户同时共享一个keystore,它应该不和上面的全局访问密码相同,主要是为了保护你自己相关的entry的信息,有了它别人没法访问你的东西。

  • −storetype storetype

Specify the typeof keystore that the keytool should operate on. This defaults to the keystore type in thejava.security file, which defaults to JKS, the keystore type provided by the Sun security provider.
keystore的类型,主要有三种JKS JCEKS PKCS#12.

创建一个private key和证书

按照如下的命令,创建一个private key和证书(其实就是建立一个private key,同时产生一个包含public key的自签名证书)。

  • −genkey

Generate a key pair and add thatentry to the keystore. This command supports these global options:
−alias alias
−dname DN
−keypass keypass
−keystore keystore
−storepass storepass
−storetype storetype
同时支持:
−keyalg AlgorithmName
Use the given algorithm to generate the key pair. The default for this option is DSA; you must use an algorithm name that is supported by a security provider that you have installed.(创建key pair的算法)
−keysize keysize
Use the givenkeysize to initialize the key pair generator. The default value for this option is 1024;
you must use a key size that is supported by the key algorithm you want to use.
−sigalg signatureAlgorithm
Specify the signature algorithm that will be used to create the self−signed certificate; this defaults to SHA1withDSA, which is supported by the Sun security provider. If you’ve specified a different key algorithm (e.g., RSA), you’ll have a different default signature algorithm (e.g., SHA1withRSA).
(创建自签名certificate时的签名算法)
−validity nDays
Specify the number of days for which theself−signed certificate should be valid. The default value for this option is 90 days.
下面这段话挺重要的。

The key entry that is created in this manner has the generated private key. In addition, the public key is placed into a self−signed certificate; that is, a certificate that identifies the holder of the public key (using the distinguished name argument) and is signed by the holder of the key itself. This is a valid certificate in all senses, although other sites will probably not accept the certificate since it was not issued by a known CA.
However, the self−signed certificate can be used to obtain a certificate from a known CA, as we'll see in just a bit.

这种方式下建立的key entry包含private key,而public key包含在自签名的certificate中。详细点:这个certificate利用distinguished name来区别public key的持有者,同时这个certificate是用持有者的private key进行的自签名。其实这个certificate在很多方面已经是一个完整的certificate了,但是由于不是CA发布的,所以其他人还不能使用。
但是这个自签名的证书可以用来提供给CA,然后让他给你签发他签名的证书。
上述步骤举例如下:

keytool −genkey −alias sdo −keyalg RSA
Enter keystore password:  ******
What is your first and last name?
[Unknown]:  Scott Oaks
What is the name of your organizational unit?
[Unknown]:  JSD
What is the name of your organization?
[Unknown]:  Sun Microsystems
What is the name of your City or Locality?
[Unknown]:  NY
What is the name of your State or Province?
[Unknown]:  NY
What is the two−letter country code for this unit?
[Unknown]:  US
Is <CN=Scott Oaks, OU=JSD, O=Sun Microsystems, L=NY, S=NY, C=US> correct?
[no]:  yes
Enter key password for <sdo>
(RETURN if same as keystore password):  ******

At this point, we now have an entry for sdo in the keystore. That entry has a self−signed certificate; note that we had the tool prompt us for all the entries that comprise the DN rather than attempting to type it all in on the command line. We also chose to generate an RSA key pair since in later chapters, we’ll want to use this key with SSL algorithms.
利用上面的命令和步骤,我们在keystore中已经有了sdo的一个entry,这个entry有个自签名的证书。(注意我们很多参数都是没有在命令行直接写,都是按照提示一步一步添加的)。

CA签署数字证书

将上面产生的自签名证书提交给CA,让他们去签名。签完名后,这个证书就可以提供给他人使用了。

In order to obtain a certificate from a CA, you must first generate a certificate signing request (CSR). The CSR contains the distinguished name and public key for a particular alias and is signed using the private key of the alias; the CA can then verify that signature and issue a certificate verifying the public key. CSRs are generated with this option:

要想获得CA签发的证书,你首先必须生成一个CSR。CSR包含distinguished name和public key,并用私钥进行签名。然后CA验证签名,然后签发证书。

  • −certreq

Generate acertificate signing request. This command supports the following global options:
−alias alias
−keypass keypass
−keystore keystore
−storepass storepass
−storetype storetype
−v
同时支持
−sigalg signatureAlgorithm
Use the given algorithm to sign the CSR. The CSR must be signed by an algorithm the CA expects, and the algorithm must be consistent with the key being verified. The default algorithm will be based on the type of key held by the alias.
用来签名CSR的算法。
−file outputFile
Store the CSR in the given file. The format of the CSR is defined in PKCS#10. The default is to write the CSR to System.out.
keytool −certreq −alias sdo −file sdoCSR.cer
Enter keystore password: **
Enter key password for : **

CA颁发数字证书后,将证书导入

  • −import

Import a certificate into the database. This command either creates a new certificate entry or imports a certificate for an existing key entry. This command supports the following global options:
−alias alias
−keypass keypass
−keystore keystore
−storepass storepass
−storetype storetype
−v
同时支持
−file inputFile
The certificate file sent by a CA will contain a certificate chain. The first certificate in the chain will
be for the alias itself and will be issued by the certificate authority; the next certificate in the chain
will be for the certificate authority and will be self−signed (a root certificate) or issued by another
certificate authority, and so on until a self−signed certificate is present. While the encoding of the
chain is defined by RFC 1421, the format of the chain itself is often referred to as a Netscape
certificate chain or a PKCS #7 certificate chain. Keytoolcan read either format; if your CA gives
you a choice of formats, pick either one.
RFC 1421就是PEM,PEM现在的作用仅仅是剩下一种证书的格式,即数据的描述形式,规格。PKCS7定义一种通用的消息语法,包括数字签名和加密等用于增强的加密机制,PKCS#7与PEM兼容,所以不需其他密码操作,就可以将加密的消息转换成PEM消息。
−trustcacerts
Use the cacerts file to obtain trusted certificates fromcertificate authorities that have signed the
certificate that is being imported. Without this option, no CAs are considered trusted, and the user will always be asked whether or not to accept the certificate (unless, of course, the no prompt option is in effect).
如果没这个选项,每次都会问是不是接受这个证书。

keytool −import −file sdo.cer −alias sdo −trustcacerts
Enter keystore password:  ******
Certificate reply was installed in keystore
As a result of this command, the state of the sdo entry has significantly changed:
1.When we created the key entry, the sdo entry had a single certificate; that certificate was issued by sdo.
2.After the import command, the sdo entry has two or more certificates in its certificate chain: the first certificate is issued by the certificate authority and has a principal of sdo; the last certificate is the CA's self−signed certificate. There may be intermediate certificates in this chain.

在上面的命令执行完成后,keystore中sdo entry的状态发生了很大的变化:
1. 当我们创建了key entry时,sdo entry仅有一个单一证书,是由sdo自签名的。
2. 在import命令后,sdo有两个或者更多的证书在它的证书链中:第一个就是由CA签发的证书,最后一个是CA自己的自签名证书,中间可能还有其他的证书。
注意,上述都是涉及的是key entry,自己建立private key和数字证书,然后将CA签发后的证书导入,导入后的证书都在sdo 这个key entry中,目前还没涉及certificate entry。

导入别人的数字证书

Certificate entries in a keystoreare always created by importing an existing certificate. The certificate may be the root certificate of a known CA (or the internal CA for your enterprise), or it may be a certificate that verifies the identity of someone with whom you will exchange information. For example, if I'm going to send you a digitally signed message, you must have my certificate (issued by a CA) within your keystore.

certificate entry一般都是由import一个已有的certificate产生的。这个证书可能是一个CA,或者是别人发送给我的一个数字证书(想和我进行秘密交互)。我想和别人进行交互,我的keystore中必须有别人相关的certificate的entry。

Let's say that I send you my certificate, and you've saved it to the file fromsdo.cer. You'd import it into your keystore like this:

我给你发了一个证书,你保存为fromsdo.cer,这就将这个certificate保存到keystore中了??

keytool −import −alias sdo −file fromsdo.cer
Enter keystore password:  ******
Owner: EmailAddress=scott.oaks@sun.com, CN=Thawte Freemail Member 
Issuer: CN=Personal Freemail RSA 2000.8.30, OU=Certificate Services, O=Thawte, 
L=Cape Town, ST=Western Cape, C=ZA
Serial number: 3df48
Valid from: Thu Dec 28 22:18:29 EST 2000 until: Fri Dec 28 22:18:29 EST 2001 
Certificate fingerprints: 
MD5: BE:E1:5C:54:E8:60:D4:09:7D:D8:C5:16:56:CA:72:5A 
SHA1: 4F:22:2D:E9:1C:7D:A6:D6:E4:1B:92:A5:CC:BE:DC:E8:DD:65:F6:45
Trust this certificate? [no]:  yes
Certificate was added to keystore
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值