public class CertificateFactory
extends Object
| java.lang.Object | |
| java.security.cert.CertificateFactory | |
该类定义了证书工厂的功能,该工厂用于从其编码生成证书,证书路径( CertPath )和证书撤销列表(CRL)对象。
对于由多个证书组成的编码,当您想解析可能不相关的证书的集合时,请使用generateCertificates 。 否则,使用generateCertPath当你想生成CertPath (证书链)并随后与验证它CertPathValidator 。
X.509的证书工厂必须返回 java.security.cert.X509Certificate实例的 java.security.cert.X509Certificate和 java.security.cert.X509CRL实例的 java.security.cert.X509CRL 。
下面的例子读取一个带有Base64编码证书的文件,每个证书在开始时都以----- BEGIN CERTIFICATE -----为界,最后以----- END CERTIFICATE -----结尾。 。 我们将FileInputStream (不支持mark和reset )转换为BufferedInputStream (支持这些方法),以便每次调用generateCertificate只消耗一个证书,并将输入流的读取位置定位到下一个证书文件:
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
while (bis.available() > 0) {
Certificate cert = cf.generateCertificate(bis);
System.out.println(cert.toString());
}
以下示例解析存储在文件中的PKCS#7格式的证书答复,并从中提取所有证书:
FileInputStream fis = new FileInputStream(filename);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Collection c = cf.generateCertificates(fis);
Iterator i = c.iterator();
while (i.hasNext()) {
Certificate cert = (Certificate)i.next();
System.out.println(cert);
}
Android提供以下 CertificateFactory类型:
| Name | Supported (API Levels) |
|---|---|
| X.509 | 1+ |
CertPath encodings:
| Name | Supported (API Levels) |
|---|---|
| PKCS7 | 1+ |
| PkiPath | 1+ |
也可以看看:
Protected constructors |
|
|---|---|
CertificateFactory(CertificateFactorySpi certFacSpi, Provider provider, String type) 创建给定类型的CertificateFactory对象,并将给定的提供者实现(SPI对象)封装在其中。 |
|
公共方法(Public methods) |
|
|---|---|
final CRL |
generateCRL(InputStream inStream) 生成证书撤销列表(CRL)对象,并使用从输入流 |
final Collection<? extends CRL> |
generateCRLs(InputStream inStream) 返回从给定输入流 |
final CertPath |
generateCertPath(List<? extends Certificate> certificates) 生成一个 |
final CertPath |
generateCertPath(InputStream inStream) 生成一个 |
final CertPath |
generateCertPath(InputStream inStream, String encoding) 生成一个 |
final Certificate |
generateCertificate(InputStream inStream) 生成一个证书对象并使用从输入流 |
final Collection<? extends Certificate> |
generateCertificates(InputStream inStream) 返回从给定输入流 |
final Iterator<String> |
getCertPathEncodings() 返回此证书工厂支持的 |
static final CertificateFactory |
getInstance(String type) 返回实现指定证书类型的证书工厂对象。 |
static final CertificateFactory |
getInstance(String type, String provider) 返回指定证书类型的证书工厂对象。 |
static final CertificateFactory |
getInstance(String type, Provider provider) 返回指定证书类型的证书工厂对象。 |
final Provider |
getProvider() 返回此证书工厂的提供者。 |
final String |
getType() 返回与此证书工厂关联的证书类型的名称。 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
CertificateFactory (CertificateFactorySpi certFacSpi, Provider provider, String type)
创建给定类型的CertificateFactory对象,并将给定的提供者实现(SPI对象)封装在其中。
| 参数(Parameters) | |
|---|---|
certFacSpi |
CertificateFactorySpi: the provider implementation. |
provider |
Provider: the provider. |
type |
String: the certificate type. |
CRL generateCRL (InputStream inStream)
生成证书撤销列表(CRL)对象,并使用从输入流 inStream读取的数据对其进行初始化。
为了利用此证书工厂支持的专用CRL格式,可将返回的CRL对象类型化为相应的CRL类。 例如,如果此证书工厂实现X.509 CRL,则返回的CRL对象可以类型X509CRL类。
请注意,如果给定的输入流不支持mark和reset ,则此方法将消耗整个输入流。 否则,对此方法的每次调用都会消耗一个CRL,并且输入流的读取位置将定位到固有的CRL结束标记之后的下一个可用字节。 如果输入流中的数据不包含固有的CRL结束标记(EOF除外),并且在解析CRL之后存在尾随数据, CRLException引发CRLException 。
| 参数(Parameters) | |
|---|---|
inStream |
InputStream: an input stream with the CRL data. |
| 返回(Returns) | |
|---|---|
CRL |
a CRL object initialized with the data from the input stream. |
| 抛出异常(Throws) | |
|---|---|
CRLException |
on parsing errors. |
Collection<? extends CRL> generateCRLs (InputStream inStream)
返回从给定输入流 inStream读取的CRL的(可能为空)集合视图。
为了利用此证书工厂支持的专用CRL格式,可以将返回的集合视图中的每个元素类型化为相应的CRL类。 例如,如果此证书工厂实现了X.509 CRL,则返回集合中的元素可以类型X509CRL类。
对于X.509 CRL的证书工厂, inStream可能包含一系列DER编码的CRL。 另外, inStream可能包含PKCS#7 CRL集。 这是一个PKCS#7 签名数据对象,唯一重要的字段是crls 。 特别是,签名和内容被忽略。 这种格式允许一次下载多个CRL。 如果不存在CRL,则返回空集合。
请注意,如果给定的输入流不支持 mark和 reset ,则此方法将消耗整个输入流。
| 参数(Parameters) | |
|---|---|
inStream |
InputStream: the input stream with the CRLs. |
| 返回(Returns) | |
|---|---|
Collection<? extends CRL> |
a (possibly empty) collection view of java.security.cert.CRL objects initialized with the data from the input stream. |
| 抛出异常(Throws) | |
|---|---|
CRLException |
on parsing errors. |
CertPath generateCertPath (List<? extends Certificate> certificates)
生成一个 CertPath对象并用 List的 Certificate s对其进行初始化。
提供的证书必须是CertificateFactory支持的类型。 它们将被复制出提供的List对象。
| 参数(Parameters) | |
|---|---|
certificates |
List: a List of Certificates |
| 返回(Returns) | |
|---|---|
CertPath |
a CertPath initialized with the supplied list of certificates |
| 抛出异常(Throws) | |
|---|---|
CertificateException |
if an exception occurs |
CertPath generateCertPath (InputStream inStream)
生成一个CertPath对象,并使用从InputStream inStream中读取的数据对它进行初始化。 数据被假定为默认编码。 默认编码的名称是的第一个元素Iterator由返回getCertPathEncodings方法。
| 参数(Parameters) | |
|---|---|
inStream |
InputStream: an InputStream containing the data |
| 返回(Returns) | |
|---|---|
CertPath |
a CertPath initialized with the data from the InputStream |
| 抛出异常(Throws) | |
|---|---|
CertificateException |
if an exception occurs while decoding |
CertPath generateCertPath (InputStream inStream, String encoding)
生成一个CertPath对象,并使用从InputStream inStream中读取的数据对它进行初始化。 假定数据是在指定的编码中。 有关标准编码名称及其格式的信息,请参阅Java Cryptography Architecture Standard Algorithm Name Documentation中的CertPath编码部分。
| 参数(Parameters) | |
|---|---|
inStream |
InputStream: an InputStream containing the data |
encoding |
String: the encoding used for the data |
| 返回(Returns) | |
|---|---|
CertPath |
a CertPath initialized with the data from the InputStream |
| 抛出异常(Throws) | |
|---|---|
CertificateException |
if an exception occurs while decoding or the encoding requested is not supported |
Certificate generateCertificate (InputStream inStream)
生成一个证书对象并使用从输入流 inStream读取的数据初始化它。
为了利用此证书工厂支持的专用证书格式,可以将返回的证书对象转换为相应的证书类别。 例如,如果此证书工厂实现了X.509证书,则返回的证书对象可以类型X509Certificate类。
对于X.509证书的证书工厂, inStream提供的证书必须是DER编码的,并且可以采用二进制或可打印(Base64)编码。 如果证书是以Base64编码提供的,则它必须以----- BEGIN CERTIFICATE -----开始,并且必须以----- END CERTIFICATE -----结尾。 。
请注意,如果给定的输入流不支持mark和reset ,则此方法将消耗整个输入流。 否则,每次调用此方法都会消耗一个证书,并且输入流的读取位置将位于固有的证书结束标记之后的下一个可用字节。 如果输入流中的数据不包含固有的证书结束标记(EOF除外),并且在解析证书后存在尾随数据, CertificateException引发CertificateException 。
| 参数(Parameters) | |
|---|---|
inStream |
InputStream: an input stream with the certificate data. |
| 返回(Returns) | |
|---|---|
Certificate |
a certificate object initialized with the data from the input stream. |
| 抛出异常(Throws) | |
|---|---|
CertificateException |
on parsing errors. |
Collection<? extends Certificate> generateCertificates (InputStream inStream)
返回从给定输入流 inStream读取的证书(可能为空)的集合视图。
为了利用此证书工厂支持的专用证书格式,可以将返回的集合视图中的每个元素转换为相应的证书类别。 例如,如果此证书工厂实现了X.509证书,则返回集合中的元素可以类型X509Certificate类。
在证书工厂X.509证书的情况下, inStream可以包含在所描述的格式的DER编码的证书的序列generateCertificate 。 另外, inStream可能包含PKCS#7证书链。 这是一个PKCS#7 签名数据对象,唯一重要的字段是证书 。 特别是,签名和内容被忽略。 这种格式允许一次下载多个证书。 如果没有证书,则返回空集合。
请注意,如果给定的输入流不支持 mark和 reset ,则此方法将消耗整个输入流。
| 参数(Parameters) | |
|---|---|
inStream |
InputStream: the input stream with the certificates. |
| 返回(Returns) | |
|---|---|
Collection<? extends Certificate> |
a (possibly empty) collection view of java.security.cert.Certificate objects initialized with the data from the input stream. |
| 抛出异常(Throws) | |
|---|---|
CertificateException |
on parsing errors. |
Iterator<String> getCertPathEncodings ()
返回此证书工厂支持的CertPath编码的迭代,首先使用默认编码。 有关标准编码名称及其格式的信息,请参阅Java Cryptography Architecture Standard Algorithm Name Documentation中的CertPath编码部分。
试图通过其 remove方法修改返回的 Iterator导致 UnsupportedOperationException 。
| 返回(Returns) | |
|---|---|
Iterator<String> |
an Iterator over the names of the supported CertPath encodings (as Strings) |
CertificateFactory getInstance (String type)
返回实现指定证书类型的证书工厂对象。
该方法遍历注册安全提供程序的列表,从最优先的提供程序开始。 返回一个新的CertificateFactory对象,它封装了支持指定类型的第一个Provider的CertificateFactorySpi实现。
请注意,可以通过 Security.getProviders()方法检索注册供应商列表。
| 参数(Parameters) | |
|---|---|
type |
String: the name of the requested certificate type. See the CertificateFactory section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard certificate types. |
| 返回(Returns) | |
|---|---|
CertificateFactory |
a certificate factory object for the specified type. |
| 抛出异常(Throws) | |
|---|---|
CertificateException |
if no Provider supports a CertificateFactorySpi implementation for the specified type. |
也可以看看:
CertificateFactory getInstance (String type, String provider)
返回指定证书类型的证书工厂对象。
返回封装指定提供者的CertificateFactorySpi实现的新CertificateFactory对象。 指定的提供者必须在安全提供者列表中注册。
请注意,注册供应商列表可能通过 Security.getProviders()方法检索。
| 参数(Parameters) | |
|---|---|
type |
String: the certificate type. See the CertificateFactory section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard certificate types. |
provider |
String: the name of the provider. |
| 返回(Returns) | |
|---|---|
CertificateFactory |
a certificate factory object for the specified type. |
| 抛出异常(Throws) | |
|---|---|
CertificateException |
if a CertificateFactorySpi implementation for the specified algorithm is not available from the specified provider. |
NoSuchProviderException |
if the specified provider is not registered in the security provider list. |
IllegalArgumentException |
if the provider name is null or empty. |
也可以看看:
CertificateFactory getInstance (String type, Provider provider)
返回指定证书类型的证书工厂对象。
返回封装指定Provider对象的CertificateFactorySpi实现的新CertificateFactory对象。 请注意,指定的Provider对象不必在提供程序列表中注册。
| 参数(Parameters) | |
|---|---|
type |
String: the certificate type. See the CertificateFactory section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard certificate types. |
provider |
Provider: the provider. |
| 返回(Returns) | |
|---|---|
CertificateFactory |
a certificate factory object for the specified type. |
| 抛出异常(Throws) | |
|---|---|
CertificateException |
if a CertificateFactorySpi implementation for the specified algorithm is not available from the specified Provider object. |
IllegalArgumentException |
if the provider is null. |
也可以看看:
Provider getProvider ()
返回此证书工厂的提供者。
| 返回(Returns) | |
|---|---|
Provider |
the provider of this certificate factory. |
String getType ()
返回与此证书工厂关联的证书类型的名称。
| 返回(Returns) | |
|---|---|
String |
the name of the certificate type associated with this certificate factory. |