jdk/src/share/classes/java/security/cert/CertificateFactory.java
changeset 18551 882a3948c6e6
parent 18156 edb590d448c5
child 21334 c60dfce46a77
equal deleted inserted replaced
18550:6d0f51c99930 18551:882a3948c6e6
     1 /*
     1 /*
     2  * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    39 import sun.security.jca.*;
    39 import sun.security.jca.*;
    40 import sun.security.jca.GetInstance.Instance;
    40 import sun.security.jca.GetInstance.Instance;
    41 
    41 
    42 /**
    42 /**
    43  * This class defines the functionality of a certificate factory, which is
    43  * This class defines the functionality of a certificate factory, which is
    44  * used to generate certificate, certification path (<code>CertPath</code>)
    44  * used to generate certificate, certification path ({@code CertPath})
    45  * and certificate revocation list (CRL) objects from their encodings.
    45  * and certificate revocation list (CRL) objects from their encodings.
    46  *
    46  *
    47  * <p>For encodings consisting of multiple certificates, use
    47  * <p>For encodings consisting of multiple certificates, use
    48  * <code>generateCertificates</code> when you want to
    48  * {@code generateCertificates} when you want to
    49  * parse a collection of possibly unrelated certificates. Otherwise,
    49  * parse a collection of possibly unrelated certificates. Otherwise,
    50  * use <code>generateCertPath</code> when you want to generate
    50  * use {@code generateCertPath} when you want to generate
    51  * a <code>CertPath</code> (a certificate chain) and subsequently
    51  * a {@code CertPath} (a certificate chain) and subsequently
    52  * validate it with a <code>CertPathValidator</code>.
    52  * validate it with a {@code CertPathValidator}.
    53  *
    53  *
    54  * <p>A certificate factory for X.509 must return certificates that are an
    54  * <p>A certificate factory for X.509 must return certificates that are an
    55  * instance of <code>java.security.cert.X509Certificate</code>, and CRLs
    55  * instance of {@code java.security.cert.X509Certificate}, and CRLs
    56  * that are an instance of <code>java.security.cert.X509CRL</code>.
    56  * that are an instance of {@code java.security.cert.X509CRL}.
    57  *
    57  *
    58  * <p>The following example reads a file with Base64 encoded certificates,
    58  * <p>The following example reads a file with Base64 encoded certificates,
    59  * which are each bounded at the beginning by -----BEGIN CERTIFICATE-----, and
    59  * which are each bounded at the beginning by -----BEGIN CERTIFICATE-----, and
    60  * bounded at the end by -----END CERTIFICATE-----. We convert the
    60  * bounded at the end by -----END CERTIFICATE-----. We convert the
    61  * <code>FileInputStream</code> (which does not support <code>mark</code>
    61  * {@code FileInputStream} (which does not support {@code mark}
    62  * and <code>reset</code>) to a <code>BufferedInputStream</code> (which
    62  * and {@code reset}) to a {@code BufferedInputStream} (which
    63  * supports those methods), so that each call to
    63  * supports those methods), so that each call to
    64  * <code>generateCertificate</code> consumes only one certificate, and the
    64  * {@code generateCertificate} consumes only one certificate, and the
    65  * read position of the input stream is positioned to the next certificate in
    65  * read position of the input stream is positioned to the next certificate in
    66  * the file:<p>
    66  * the file:<p>
    67  *
    67  *
    68  * <pre>{@code
    68  * <pre>{@code
    69  * FileInputStream fis = new FileInputStream(filename);
    69  * FileInputStream fis = new FileInputStream(filename);
    90  *    System.out.println(cert);
    90  *    System.out.println(cert);
    91  * }
    91  * }
    92  * </pre>
    92  * </pre>
    93  *
    93  *
    94  * <p> Every implementation of the Java platform is required to support the
    94  * <p> Every implementation of the Java platform is required to support the
    95  * following standard <code>CertificateFactory</code> type:
    95  * following standard {@code CertificateFactory} type:
    96  * <ul>
    96  * <ul>
    97  * <li><tt>X.509</tt></li>
    97  * <li>{@code X.509}</li>
    98  * </ul>
    98  * </ul>
    99  * and the following standard <code>CertPath</code> encodings:
    99  * and the following standard {@code CertPath} encodings:
   100  * <ul>
   100  * <ul>
   101  * <li><tt>PKCS7</tt></li>
   101  * <li>{@code PKCS7}</li>
   102  * <li><tt>PkiPath</tt></li>
   102  * <li>{@code PkiPath}</li>
   103  * </ul>
   103  * </ul>
   104  * The type and encodings are described in the <a href=
   104  * The type and encodings are described in the <a href=
   105  * "{@docRoot}/../technotes/guides/security/StandardNames.html#CertificateFactory">
   105  * "{@docRoot}/../technotes/guides/security/StandardNames.html#CertificateFactory">
   106  * CertificateFactory section</a> and the <a href=
   106  * CertificateFactory section</a> and the <a href=
   107  * "{@docRoot}/../technotes/guides/security/StandardNames.html#CertPathEncodings">
   107  * "{@docRoot}/../technotes/guides/security/StandardNames.html#CertPathEncodings">
   256      *
   256      *
   257      * @exception CertificateException if a CertificateFactorySpi
   257      * @exception CertificateException if a CertificateFactorySpi
   258      *          implementation for the specified algorithm is not available
   258      *          implementation for the specified algorithm is not available
   259      *          from the specified Provider object.
   259      *          from the specified Provider object.
   260      *
   260      *
   261      * @exception IllegalArgumentException if the <code>provider</code> is
   261      * @exception IllegalArgumentException if the {@code provider} is
   262      *          null.
   262      *          null.
   263      *
   263      *
   264      * @see java.security.Provider
   264      * @see java.security.Provider
   265      *
   265      *
   266      * @since 1.4
   266      * @since 1.4
   297         return this.type;
   297         return this.type;
   298     }
   298     }
   299 
   299 
   300     /**
   300     /**
   301      * Generates a certificate object and initializes it with
   301      * Generates a certificate object and initializes it with
   302      * the data read from the input stream <code>inStream</code>.
   302      * the data read from the input stream {@code inStream}.
   303      *
   303      *
   304      * <p>In order to take advantage of the specialized certificate format
   304      * <p>In order to take advantage of the specialized certificate format
   305      * supported by this certificate factory,
   305      * supported by this certificate factory,
   306      * the returned certificate object can be typecast to the corresponding
   306      * the returned certificate object can be typecast to the corresponding
   307      * certificate class. For example, if this certificate
   307      * certificate class. For example, if this certificate
   308      * factory implements X.509 certificates, the returned certificate object
   308      * factory implements X.509 certificates, the returned certificate object
   309      * can be typecast to the <code>X509Certificate</code> class.
   309      * can be typecast to the {@code X509Certificate} class.
   310      *
   310      *
   311      * <p>In the case of a certificate factory for X.509 certificates, the
   311      * <p>In the case of a certificate factory for X.509 certificates, the
   312      * certificate provided in <code>inStream</code> must be DER-encoded and
   312      * certificate provided in {@code inStream} must be DER-encoded and
   313      * may be supplied in binary or printable (Base64) encoding. If the
   313      * may be supplied in binary or printable (Base64) encoding. If the
   314      * certificate is provided in Base64 encoding, it must be bounded at
   314      * certificate is provided in Base64 encoding, it must be bounded at
   315      * the beginning by -----BEGIN CERTIFICATE-----, and must be bounded at
   315      * the beginning by -----BEGIN CERTIFICATE-----, and must be bounded at
   316      * the end by -----END CERTIFICATE-----.
   316      * the end by -----END CERTIFICATE-----.
   317      *
   317      *
   322      * method consumes one certificate and the read position of the
   322      * method consumes one certificate and the read position of the
   323      * input stream is positioned to the next available byte after
   323      * input stream is positioned to the next available byte after
   324      * the inherent end-of-certificate marker. If the data in the input stream
   324      * the inherent end-of-certificate marker. If the data in the input stream
   325      * does not contain an inherent end-of-certificate marker (other
   325      * does not contain an inherent end-of-certificate marker (other
   326      * than EOF) and there is trailing data after the certificate is parsed, a
   326      * than EOF) and there is trailing data after the certificate is parsed, a
   327      * <code>CertificateException</code> is thrown.
   327      * {@code CertificateException} is thrown.
   328      *
   328      *
   329      * @param inStream an input stream with the certificate data.
   329      * @param inStream an input stream with the certificate data.
   330      *
   330      *
   331      * @return a certificate object initialized with the data
   331      * @return a certificate object initialized with the data
   332      * from the input stream.
   332      * from the input stream.
   338     {
   338     {
   339         return certFacSpi.engineGenerateCertificate(inStream);
   339         return certFacSpi.engineGenerateCertificate(inStream);
   340     }
   340     }
   341 
   341 
   342     /**
   342     /**
   343      * Returns an iteration of the <code>CertPath</code> encodings supported
   343      * Returns an iteration of the {@code CertPath} encodings supported
   344      * by this certificate factory, with the default encoding first. See
   344      * by this certificate factory, with the default encoding first. See
   345      * the CertPath Encodings section in the <a href=
   345      * the CertPath Encodings section in the <a href=
   346      * "{@docRoot}/../technotes/guides/security/StandardNames.html#CertPathEncodings">
   346      * "{@docRoot}/../technotes/guides/security/StandardNames.html#CertPathEncodings">
   347      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
   347      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
   348      * for information about standard encoding names and their formats.
   348      * for information about standard encoding names and their formats.
   349      * <p>
   349      * <p>
   350      * Attempts to modify the returned <code>Iterator</code> via its
   350      * Attempts to modify the returned {@code Iterator} via its
   351      * <code>remove</code> method result in an
   351      * {@code remove} method result in an
   352      * <code>UnsupportedOperationException</code>.
   352      * {@code UnsupportedOperationException}.
   353      *
   353      *
   354      * @return an <code>Iterator</code> over the names of the supported
   354      * @return an {@code Iterator} over the names of the supported
   355      *         <code>CertPath</code> encodings (as <code>String</code>s)
   355      *         {@code CertPath} encodings (as {@code String}s)
   356      * @since 1.4
   356      * @since 1.4
   357      */
   357      */
   358     public final Iterator<String> getCertPathEncodings() {
   358     public final Iterator<String> getCertPathEncodings() {
   359         return(certFacSpi.engineGetCertPathEncodings());
   359         return(certFacSpi.engineGetCertPathEncodings());
   360     }
   360     }
   361 
   361 
   362     /**
   362     /**
   363      * Generates a <code>CertPath</code> object and initializes it with
   363      * Generates a {@code CertPath} object and initializes it with
   364      * the data read from the <code>InputStream</code> inStream. The data
   364      * the data read from the {@code InputStream} inStream. The data
   365      * is assumed to be in the default encoding. The name of the default
   365      * is assumed to be in the default encoding. The name of the default
   366      * encoding is the first element of the <code>Iterator</code> returned by
   366      * encoding is the first element of the {@code Iterator} returned by
   367      * the {@link #getCertPathEncodings getCertPathEncodings} method.
   367      * the {@link #getCertPathEncodings getCertPathEncodings} method.
   368      *
   368      *
   369      * @param inStream an <code>InputStream</code> containing the data
   369      * @param inStream an {@code InputStream} containing the data
   370      * @return a <code>CertPath</code> initialized with the data from the
   370      * @return a {@code CertPath} initialized with the data from the
   371      *   <code>InputStream</code>
   371      *   {@code InputStream}
   372      * @exception CertificateException if an exception occurs while decoding
   372      * @exception CertificateException if an exception occurs while decoding
   373      * @since 1.4
   373      * @since 1.4
   374      */
   374      */
   375     public final CertPath generateCertPath(InputStream inStream)
   375     public final CertPath generateCertPath(InputStream inStream)
   376         throws CertificateException
   376         throws CertificateException
   377     {
   377     {
   378         return(certFacSpi.engineGenerateCertPath(inStream));
   378         return(certFacSpi.engineGenerateCertPath(inStream));
   379     }
   379     }
   380 
   380 
   381     /**
   381     /**
   382      * Generates a <code>CertPath</code> object and initializes it with
   382      * Generates a {@code CertPath} object and initializes it with
   383      * the data read from the <code>InputStream</code> inStream. The data
   383      * the data read from the {@code InputStream} inStream. The data
   384      * is assumed to be in the specified encoding. See
   384      * is assumed to be in the specified encoding. See
   385      * the CertPath Encodings section in the <a href=
   385      * the CertPath Encodings section in the <a href=
   386      * "{@docRoot}/../technotes/guides/security/StandardNames.html#CertPathEncodings">
   386      * "{@docRoot}/../technotes/guides/security/StandardNames.html#CertPathEncodings">
   387      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
   387      * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
   388      * for information about standard encoding names and their formats.
   388      * for information about standard encoding names and their formats.
   389      *
   389      *
   390      * @param inStream an <code>InputStream</code> containing the data
   390      * @param inStream an {@code InputStream} containing the data
   391      * @param encoding the encoding used for the data
   391      * @param encoding the encoding used for the data
   392      * @return a <code>CertPath</code> initialized with the data from the
   392      * @return a {@code CertPath} initialized with the data from the
   393      *   <code>InputStream</code>
   393      *   {@code InputStream}
   394      * @exception CertificateException if an exception occurs while decoding or
   394      * @exception CertificateException if an exception occurs while decoding or
   395      *   the encoding requested is not supported
   395      *   the encoding requested is not supported
   396      * @since 1.4
   396      * @since 1.4
   397      */
   397      */
   398     public final CertPath generateCertPath(InputStream inStream,
   398     public final CertPath generateCertPath(InputStream inStream,
   400     {
   400     {
   401         return(certFacSpi.engineGenerateCertPath(inStream, encoding));
   401         return(certFacSpi.engineGenerateCertPath(inStream, encoding));
   402     }
   402     }
   403 
   403 
   404     /**
   404     /**
   405      * Generates a <code>CertPath</code> object and initializes it with
   405      * Generates a {@code CertPath} object and initializes it with
   406      * a <code>List</code> of <code>Certificate</code>s.
   406      * a {@code List} of {@code Certificate}s.
   407      * <p>
   407      * <p>
   408      * The certificates supplied must be of a type supported by the
   408      * The certificates supplied must be of a type supported by the
   409      * <code>CertificateFactory</code>. They will be copied out of the supplied
   409      * {@code CertificateFactory}. They will be copied out of the supplied
   410      * <code>List</code> object.
   410      * {@code List} object.
   411      *
   411      *
   412      * @param certificates a <code>List</code> of <code>Certificate</code>s
   412      * @param certificates a {@code List} of {@code Certificate}s
   413      * @return a <code>CertPath</code> initialized with the supplied list of
   413      * @return a {@code CertPath} initialized with the supplied list of
   414      *   certificates
   414      *   certificates
   415      * @exception CertificateException if an exception occurs
   415      * @exception CertificateException if an exception occurs
   416      * @since 1.4
   416      * @since 1.4
   417      */
   417      */
   418     public final CertPath
   418     public final CertPath
   422         return(certFacSpi.engineGenerateCertPath(certificates));
   422         return(certFacSpi.engineGenerateCertPath(certificates));
   423     }
   423     }
   424 
   424 
   425     /**
   425     /**
   426      * Returns a (possibly empty) collection view of the certificates read
   426      * Returns a (possibly empty) collection view of the certificates read
   427      * from the given input stream <code>inStream</code>.
   427      * from the given input stream {@code inStream}.
   428      *
   428      *
   429      * <p>In order to take advantage of the specialized certificate format
   429      * <p>In order to take advantage of the specialized certificate format
   430      * supported by this certificate factory, each element in
   430      * supported by this certificate factory, each element in
   431      * the returned collection view can be typecast to the corresponding
   431      * the returned collection view can be typecast to the corresponding
   432      * certificate class. For example, if this certificate
   432      * certificate class. For example, if this certificate
   433      * factory implements X.509 certificates, the elements in the returned
   433      * factory implements X.509 certificates, the elements in the returned
   434      * collection can be typecast to the <code>X509Certificate</code> class.
   434      * collection can be typecast to the {@code X509Certificate} class.
   435      *
   435      *
   436      * <p>In the case of a certificate factory for X.509 certificates,
   436      * <p>In the case of a certificate factory for X.509 certificates,
   437      * <code>inStream</code> may contain a sequence of DER-encoded certificates
   437      * {@code inStream} may contain a sequence of DER-encoded certificates
   438      * in the formats described for
   438      * in the formats described for
   439      * {@link #generateCertificate(java.io.InputStream) generateCertificate}.
   439      * {@link #generateCertificate(java.io.InputStream) generateCertificate}.
   440      * In addition, <code>inStream</code> may contain a PKCS#7 certificate
   440      * In addition, {@code inStream} may contain a PKCS#7 certificate
   441      * chain. This is a PKCS#7 <i>SignedData</i> object, with the only
   441      * chain. This is a PKCS#7 <i>SignedData</i> object, with the only
   442      * significant field being <i>certificates</i>. In particular, the
   442      * significant field being <i>certificates</i>. In particular, the
   443      * signature and the contents are ignored. This format allows multiple
   443      * signature and the contents are ignored. This format allows multiple
   444      * certificates to be downloaded at once. If no certificates are present,
   444      * certificates to be downloaded at once. If no certificates are present,
   445      * an empty collection is returned.
   445      * an empty collection is returned.
   462         return certFacSpi.engineGenerateCertificates(inStream);
   462         return certFacSpi.engineGenerateCertificates(inStream);
   463     }
   463     }
   464 
   464 
   465     /**
   465     /**
   466      * Generates a certificate revocation list (CRL) object and initializes it
   466      * Generates a certificate revocation list (CRL) object and initializes it
   467      * with the data read from the input stream <code>inStream</code>.
   467      * with the data read from the input stream {@code inStream}.
   468      *
   468      *
   469      * <p>In order to take advantage of the specialized CRL format
   469      * <p>In order to take advantage of the specialized CRL format
   470      * supported by this certificate factory,
   470      * supported by this certificate factory,
   471      * the returned CRL object can be typecast to the corresponding
   471      * the returned CRL object can be typecast to the corresponding
   472      * CRL class. For example, if this certificate
   472      * CRL class. For example, if this certificate
   473      * factory implements X.509 CRLs, the returned CRL object
   473      * factory implements X.509 CRLs, the returned CRL object
   474      * can be typecast to the <code>X509CRL</code> class.
   474      * can be typecast to the {@code X509CRL} class.
   475      *
   475      *
   476      * <p>Note that if the given input stream does not support
   476      * <p>Note that if the given input stream does not support
   477      * {@link java.io.InputStream#mark(int) mark} and
   477      * {@link java.io.InputStream#mark(int) mark} and
   478      * {@link java.io.InputStream#reset() reset}, this method will
   478      * {@link java.io.InputStream#reset() reset}, this method will
   479      * consume the entire input stream. Otherwise, each call to this
   479      * consume the entire input stream. Otherwise, each call to this
   480      * method consumes one CRL and the read position of the input stream
   480      * method consumes one CRL and the read position of the input stream
   481      * is positioned to the next available byte after the inherent
   481      * is positioned to the next available byte after the inherent
   482      * end-of-CRL marker. If the data in the
   482      * end-of-CRL marker. If the data in the
   483      * input stream does not contain an inherent end-of-CRL marker (other
   483      * input stream does not contain an inherent end-of-CRL marker (other
   484      * than EOF) and there is trailing data after the CRL is parsed, a
   484      * than EOF) and there is trailing data after the CRL is parsed, a
   485      * <code>CRLException</code> is thrown.
   485      * {@code CRLException} is thrown.
   486      *
   486      *
   487      * @param inStream an input stream with the CRL data.
   487      * @param inStream an input stream with the CRL data.
   488      *
   488      *
   489      * @return a CRL object initialized with the data
   489      * @return a CRL object initialized with the data
   490      * from the input stream.
   490      * from the input stream.
   497         return certFacSpi.engineGenerateCRL(inStream);
   497         return certFacSpi.engineGenerateCRL(inStream);
   498     }
   498     }
   499 
   499 
   500     /**
   500     /**
   501      * Returns a (possibly empty) collection view of the CRLs read
   501      * Returns a (possibly empty) collection view of the CRLs read
   502      * from the given input stream <code>inStream</code>.
   502      * from the given input stream {@code inStream}.
   503      *
   503      *
   504      * <p>In order to take advantage of the specialized CRL format
   504      * <p>In order to take advantage of the specialized CRL format
   505      * supported by this certificate factory, each element in
   505      * supported by this certificate factory, each element in
   506      * the returned collection view can be typecast to the corresponding
   506      * the returned collection view can be typecast to the corresponding
   507      * CRL class. For example, if this certificate
   507      * CRL class. For example, if this certificate
   508      * factory implements X.509 CRLs, the elements in the returned
   508      * factory implements X.509 CRLs, the elements in the returned
   509      * collection can be typecast to the <code>X509CRL</code> class.
   509      * collection can be typecast to the {@code X509CRL} class.
   510      *
   510      *
   511      * <p>In the case of a certificate factory for X.509 CRLs,
   511      * <p>In the case of a certificate factory for X.509 CRLs,
   512      * <code>inStream</code> may contain a sequence of DER-encoded CRLs.
   512      * {@code inStream} may contain a sequence of DER-encoded CRLs.
   513      * In addition, <code>inStream</code> may contain a PKCS#7 CRL
   513      * In addition, {@code inStream} may contain a PKCS#7 CRL
   514      * set. This is a PKCS#7 <i>SignedData</i> object, with the only
   514      * set. This is a PKCS#7 <i>SignedData</i> object, with the only
   515      * significant field being <i>crls</i>. In particular, the
   515      * significant field being <i>crls</i>. In particular, the
   516      * signature and the contents are ignored. This format allows multiple
   516      * signature and the contents are ignored. This format allows multiple
   517      * CRLs to be downloaded at once. If no CRLs are present,
   517      * CRLs to be downloaded at once. If no CRLs are present,
   518      * an empty collection is returned.
   518      * an empty collection is returned.