home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / security / cert / X509CRL.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  18.2 KB  |  514 lines

  1. /*
  2.  * @(#)X509CRL.java    1.6 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security.cert;
  16.  
  17. import java.io.InputStream;
  18. import java.lang.Class;
  19. import java.lang.reflect.Constructor;
  20. import java.lang.reflect.InvocationTargetException;
  21. import java.security.Security;
  22. import java.security.NoSuchAlgorithmException;
  23. import java.security.NoSuchProviderException;
  24. import java.security.InvalidKeyException;
  25. import java.security.SignatureException;
  26.  
  27. import java.math.BigInteger;
  28. import java.security.Principal;
  29. import java.security.PublicKey;
  30. import java.util.BitSet;
  31. import java.util.Date;
  32. import java.util.Set;
  33.  
  34. /**
  35.  * <p>
  36.  * Abstract class for an X.509 Certificate Revocation List (CRL).
  37.  * A CRL is a time-stamped list identifying revoked certificates.
  38.  * It is signed by a Certificate Authority (CA) and made freely
  39.  * available in a public repository.  
  40.  * 
  41.  * <p>Each revoked certificate is
  42.  * identified in a CRL by its certificate serial number. When a
  43.  * certificate-using system uses a certificate (e.g., for verifying a
  44.  * remote user's digital signature), that system not only checks the
  45.  * certificate signature and validity but also acquires a suitably-
  46.  * recent CRL and checks that the certificate serial number is not on
  47.  * that CRL.  The meaning of "suitably-recent" may vary with local
  48.  * policy, but it usually means the most recently-issued CRL.  A CA
  49.  * issues a new CRL on a regular periodic basis (e.g., hourly, daily, or
  50.  * weekly).  Entries are added to CRLs as revocations occur, and an
  51.  * entry may be removed when the certificate expiration date is reached.
  52.  * <p>
  53.  * The X.509 v2 CRL format is described below in ASN.1:
  54.  * <pre>
  55.  * CertificateList  ::=  SEQUENCE  {
  56.  *     tbsCertList          TBSCertList,
  57.  *     signatureAlgorithm   AlgorithmIdentifier,
  58.  *     signature            BIT STRING  }
  59.  * </pre>
  60.  * <p>
  61.  * A good decription and profiling is provided in the IETF PKIX WG
  62.  * draft, Part I:  X.509 Certificate and CRL Profile,
  63.  * <draft-ietf-pkix-ipki-part1-06.txt>.
  64.  * <p>
  65.  * The ASN.1 definition of <code>tbsCertList</code> is:
  66.  * <pre>
  67.  * TBSCertList  ::=  SEQUENCE  {
  68.  *     version                 Version OPTIONAL,
  69.  *                             -- if present, must be v2
  70.  *     signature               AlgorithmIdentifier,
  71.  *     issuer                  Name,
  72.  *     thisUpdate              ChoiceOfTime,
  73.  *     nextUpdate              ChoiceOfTime OPTIONAL,
  74.  *     revokedCertificates     SEQUENCE OF SEQUENCE  {
  75.  *         userCertificate         CertificateSerialNumber,
  76.  *         revocationDate          ChoiceOfTime,
  77.  *         crlEntryExtensions      Extensions OPTIONAL
  78.  *                                 -- if present, must be v2
  79.  *         }  OPTIONAL,
  80.  *     crlExtensions           [0]  EXPLICIT Extensions OPTIONAL
  81.  *                                  -- if present, must be v2
  82.  *     }
  83.  * </pre>
  84.  * <p>
  85.  * Here is sample code to instantiate an X509CRL:
  86.  * <pre><code> 
  87.  * InputStream inStream = new FileInputStream("fileName-of-crl");
  88.  * X509CRL crl = X509CRL.getInstance(inStream);
  89.  * inStream.close();
  90.  * </code></pre>
  91.  * OR
  92.  * <pre><code>
  93.  * byte[] crlData = <crl read from a file, say>
  94.  * X509CRL crl = X509CRL.getInstance(crlData);
  95.  * </code></pre>
  96.  * <p>
  97.  * In either case, the code that instantiates an X509CRL
  98.  * consults the Java security properties file to locate the actual
  99.  * implementation. The Java security properties file is located in the file 
  100.  * named <JAVA_HOME>/lib/security/java.security, where <JAVA_HOME>
  101.  * refers to the directory where the JDK was installed.
  102.  * In the Security properties file, the default implementation
  103.  * for X.509 v2 CRL is given as:
  104.  * <pre>
  105.  * crl.provider.x509=sun.security.x509.X509CRLImpl
  106.  * </pre>
  107.  * <p>
  108.  * The value of this <code>crl.provider.x509</code> property has to be 
  109.  * changed to instantiate another implementation.
  110.  *
  111.  * @author Hemma Prafullchandra
  112.  * @version 1.6
  113.  * @see X509Certificate
  114.  */
  115. public abstract class X509CRL implements X509Extension {
  116.  
  117.     /*
  118.      * Constant to lookup in the Security properties file.
  119.      * In the Security properties file the default implementation
  120.      * for X.509 v2 CRL is given as:
  121.      * <pre>
  122.      * crl.provider.x509=sun.security.x509.X509CRL
  123.      * </pre>
  124.      */  
  125.     private static final String X509_PROVIDER = "crl.provider.x509";
  126.     
  127.     /**
  128.      * Instantiates an X509CRL object, and initializes it with
  129.      * the data read from the input stream <code>inStream</code>.
  130.      * The implementation (X509CRL is an abstract class) is
  131.      * provided by the class specified as the value of the 
  132.      * <code>crl.provider.x509</code>
  133.      * property in the security properties file.
  134.      *
  135.      * <p>Note: Only one CRL is expected to be in the input stream.
  136.      *   
  137.      * @param inStream an input stream with the data to be read to
  138.      *        initialize the CRL.
  139.      * @return an X509CRL object initialized with the data
  140.      * from the input stream.
  141.      * 
  142.      * @exception CRLException if a class initialization
  143.      *            or CRL parsing error occurs.
  144.      * @exception X509ExtensionException on extension parsing errors.
  145.      */
  146.     public static final X509CRL getInstance(InputStream inStream)
  147.     throws CRLException, X509ExtensionException {
  148.         return getInst((Object)inStream);
  149.     }
  150.  
  151.     /**
  152.      * Instantiates an X509CRL object, and initializes it with
  153.      * the specified byte array.
  154.      * The implementation (X509CRL is an abstract class) is
  155.      * provided by the class specified as the value of the 
  156.      * <code>crl.provider.x509</code>
  157.      * property in the security properties file.
  158.      *   
  159.      * @param crlData a byte array containing the DER-encoded CRL.
  160.      * @return an X509CRL object initialized with the data
  161.      * from <code>crlData</code>.
  162.      * @exception CRLException if a class initialization
  163.      *            or CRL parsing error occurs.
  164.      * @exception X509ExtensionException on extension parsing errors.
  165.      */
  166.     public static final X509CRL getInstance(byte[] crlData)
  167.     throws CRLException, X509ExtensionException {
  168.         return getInst((Object)crlData);
  169.     }
  170.  
  171.     private static final X509CRL getInst(Object value)
  172.     throws CRLException, X509ExtensionException {
  173.         String className = null;
  174.  
  175.     try {
  176.         java.security.AccessController.beginPrivileged();
  177.         className = Security.getProperty(X509_PROVIDER);
  178.     } finally {
  179.         java.security.AccessController.endPrivileged();
  180.     }
  181.  
  182.         if (className == null) {
  183.             // shouldn't happen, but assume corrupted properties file
  184.             // provide access to sun implementation
  185.             className = "sun.security.x509.X509CRLImpl";
  186.         }
  187.         try {
  188.             Class[] params = null;
  189.             if (value instanceof InputStream) {
  190.                 params = new Class[] { InputStream.class };
  191.             } else if (value instanceof byte[]) {
  192.                 params = new Class[] { value.getClass() };
  193.             } else
  194.                 throw new CRLException("Unsupported arg type");
  195.             Class certClass = Class.forName(className);
  196.  
  197.             // get the appropriate constructor and instantiate it
  198.             Constructor cons = certClass.getConstructor(params);
  199.  
  200.             // get a new instance
  201.             Object obj = cons.newInstance(new Object[] {value});
  202.             return (X509CRL)obj;
  203.  
  204.         } catch (ClassNotFoundException e) {
  205.           throw new CRLException("Could not find class: " + e);
  206.         } catch (IllegalAccessException e) {
  207.           throw new CRLException("Could not access class: " + e);
  208.         } catch (InstantiationException e) {
  209.           throw new CRLException("Problems instantiating: " + e);
  210.         } catch (InvocationTargetException e) {
  211.           throw new CRLException("InvocationTargetException: "
  212.                                          + e.getTargetException());
  213.         } catch (NoSuchMethodException e) {
  214.           throw new CRLException("Could not find class method: "
  215.                                           + e.getMessage());
  216.         }
  217.     }
  218.  
  219.     /**
  220.      * Compares this CRL for equality with the specified 
  221.      * object. If the <code>other</code> object is an 
  222.      * <code>instanceof</code> <code>X509CRL</code>, then
  223.      * its encoded form is retrieved and compared with the
  224.      * encoded form of this CRL.
  225.      * 
  226.      * @param other the object to test for equality with this CRL.
  227.      * 
  228.      * @return true iff the encoded forms of the two CRLs
  229.      * match, false otherwise.
  230.      */  
  231.     public boolean equals(Object other) {
  232.         if (this == other)
  233.             return true;
  234.         if (!(other instanceof X509CRL))
  235.             return false;
  236.         try {
  237.             byte[] thisCRL = this.getEncoded();
  238.             byte[] otherCRL = ((X509CRL)other).getEncoded();
  239.  
  240.             if (thisCRL.length != otherCRL.length)
  241.                 return false;
  242.             for (int i = 0; i < thisCRL.length; i++)
  243.                  if (thisCRL[i] != otherCRL[i])
  244.                      return false;
  245.             return true;
  246.         } catch (CRLException e) {
  247.         return false;
  248.         }
  249.     }
  250.  
  251.     /**
  252.      * Returns a hashcode value for this CRL from its
  253.      * encoded form.
  254.      *
  255.      * @return the hashcode value.
  256.      */  
  257.     public int hashCode() {
  258.         int     retval = 0;
  259.         try {
  260.             byte[] crlData = this.getEncoded();
  261.             for (int i = 1; i < crlData.length; i++) {
  262.                  retval += crlData[i] * i;
  263.             }
  264.             return(retval);
  265.         } catch (CRLException e) {
  266.             return(retval);
  267.         }
  268.     }
  269.  
  270.     /**
  271.      * Returns the ASN.1 DER-encoded form of this CRL.
  272.      *
  273.      * @exception CRLException if an encoding error occurs.
  274.      */
  275.     public abstract byte[] getEncoded()
  276.         throws CRLException;
  277.  
  278.     /**
  279.      * Verifies that this CRL was signed using the 
  280.      * private key that corresponds to the specified public key.
  281.      *
  282.      * @param key the PublicKey used to carry out the verification.
  283.      *
  284.      * @exception NoSuchAlgorithmException on unsupported signature
  285.      * algorithms.
  286.      * @exception InvalidKeyException on incorrect key.
  287.      * @exception NoSuchProviderException if there's no default provider.
  288.      * @exception SignatureException on signature errors.
  289.      * @exception CRLException on encoding errors.
  290.      */  
  291.     public abstract void verify(PublicKey key)
  292.         throws CRLException,  NoSuchAlgorithmException,
  293.         InvalidKeyException, NoSuchProviderException,
  294.         SignatureException;
  295.  
  296.     /**
  297.      * Verifies that this CRL was signed using the 
  298.      * private key that corresponds to the specified public key.
  299.      * This method uses the signature verification engine
  300.      * supplied by the specified provider.
  301.      *
  302.      * @param key the PublicKey used to carry out the verification.
  303.      * @param sigProvider the name of the signature provider.
  304.      * 
  305.      * @exception NoSuchAlgorithmException on unsupported signature
  306.      * algorithms.
  307.      * @exception InvalidKeyException on incorrect key.
  308.      * @exception NoSuchProviderException on incorrect provider.
  309.      * @exception SignatureException on signature errors.
  310.      * @exception CRLException on encoding errors.
  311.      */  
  312.     public abstract void verify(PublicKey key, String sigProvider)
  313.         throws CRLException, NoSuchAlgorithmException,
  314.         InvalidKeyException, NoSuchProviderException,
  315.         SignatureException;
  316.  
  317.     /**
  318.      * Returns a string representation of this CRL.
  319.      *
  320.      * @return a string representation of this CRL.
  321.      */
  322.     public abstract String toString();
  323.  
  324.     /**
  325.      * Checks whether the given serial number is on this CRL.
  326.      * Here is sample code to use this:
  327.      * <pre><code>
  328.      * X509Certificate cert;
  329.      * X509CRL         crl;
  330.      * ...
  331.      * if (crl.isRevoked(cert.getSerialNumber()))
  332.      *     throw new SensibleException;
  333.      * else {
  334.      *     doUsefulWork();
  335.      * }
  336.      * </code></pre>
  337.      *
  338.      * @param serialNumber the number to check for.
  339.      * @return true if the given serial number is on this CRL,
  340.      * false otherwise.
  341.      */
  342.     public abstract boolean isRevoked(BigInteger serialNumber);
  343.  
  344.     /**
  345.      * Gets the <code>version</code> (version number) value from the CRL.
  346.      * The ASN.1 definition for this is:
  347.      * <pre>
  348.      * version    Version OPTIONAL,
  349.      *             -- if present, must be v2<p>
  350.      * Version  ::=  INTEGER  {  v1(0), v2(1), v3(2)  }
  351.      *             -- v3 does not apply to CRLs but appears for consistency
  352.      *             -- with definition of Version for certs
  353.      * </pre>
  354.      *
  355.      * @return the version number.
  356.      */
  357.     public abstract int getVersion();
  358.  
  359.     /**
  360.      * Gets the <code>issuer</code> (issuer distinguished name) value from 
  361.      * the CRL. The issuer name identifies the entity that signed (and
  362.      * issued) the CRL. 
  363.      * 
  364.      * <p>The issuer name field contains an
  365.      * X.500 distinguished name (DN).
  366.      * The ASN.1 definition for this is:
  367.      * <pre>
  368.      * issuer    Name
  369.      *
  370.      * Name ::= CHOICE { RDNSequence }
  371.      * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
  372.      * RelativeDistinguishedName ::=
  373.      *     SET OF AttributeValueAssertion
  374.      *
  375.      * AttributeValueAssertion ::= SEQUENCE {
  376.      *                               AttributeType,
  377.      *                               AttributeValue }
  378.      * AttributeType ::= OBJECT IDENTIFIER
  379.      * AttributeValue ::= ANY
  380.      * </pre>
  381.      * The <code>Name</code> describes a hierarchical name composed of attributes,
  382.      * such as country name, and corresponding values, such as US.
  383.      * The type of the <code>AttributeValue</code> component is determined by the
  384.      * <code>AttributeType</code>; in general it will be a 
  385.      * <code>directoryString</code>. A <code>directoryString</code> is usually 
  386.      * one of <code>PrintableString</code>,
  387.      * <code>TeletexString</code> or <code>UniversalString</code>.
  388.      * 
  389.      * @return a Principal whose name is the issuer distinguished name.
  390.      */
  391.     public abstract Principal getIssuerDN();
  392.  
  393.     /**
  394.      * Gets the <code>thisUpdate</code> date from the CRL.
  395.      * The ASN.1 definition for this is:
  396.      * <pre>
  397.      * thisUpdate   ChoiceOfTime
  398.      * ChoiceOfTime ::= CHOICE {
  399.      *     utcTime        UTCTime,
  400.      *     generalTime    GeneralizedTime }
  401.      * </pre>
  402.      *
  403.      * @return the <code>thisUpdate</code> date from the CRL.
  404.      */
  405.     public abstract Date getThisUpdate();
  406.  
  407.     /**
  408.      * Gets the <code>nextUpdate</code> date from the CRL.
  409.      *
  410.      * @return the <code>nextUpdate</code> date from the CRL, or null if
  411.      * not present.
  412.      */
  413.     public abstract Date getNextUpdate();
  414.  
  415.     /**
  416.      * Gets the revoked certificate with serial number 
  417.      * <code>serialNumber</code> from the CRL.
  418.      *
  419.      * @return the revoked certificate, or null if there is
  420.      * no entry in the CRL marked with the provided serial number.
  421.      * @see RevokedCertificate
  422.      */
  423.     public abstract RevokedCertificate
  424.         getRevokedCertificate(BigInteger serialNumber);
  425.   
  426.     /**
  427.      * Gets all the revoked certificates from the CRL.
  428.      * This returns a Set of RevokedCertificate objects.
  429.      *
  430.      * @return all the revoked certificates or null if there
  431.      * are none present.
  432.      * @see RevokedCertificate
  433.      */
  434.     public abstract Set getRevokedCertificates();
  435.   
  436.     /**
  437.      * Gets the DER-encoded CRL information, the
  438.      * <code>tbsCertList</code> from this CRL.
  439.      * This can be used to verify the signature independently.
  440.      *
  441.      * @return the DER-encoded CRL information.
  442.      * @exception CRLException if a parsing error occurs.
  443.      * @exception X509ExtensionException on extension parsing errors.
  444.      */
  445.     public abstract byte[] getTBSCertList()
  446.         throws CRLException, X509ExtensionException;
  447.  
  448.     /**
  449.      * Gets the <code>signature</code> value (the raw signature bits) from 
  450.      * the CRL.
  451.      * The ASN.1 definition for this is:
  452.      * <pre>
  453.      * signature     BIT STRING  
  454.      * </pre>
  455.      *
  456.      * @return the signature.
  457.      */
  458.     public abstract byte[] getSignature();
  459.  
  460.     /**
  461.      * Gets the signature algorithm name for the CRL
  462.      * signature algorithm. An example is the string "SHA-1/DSA".
  463.      * The ASN.1 definition for this is:
  464.      * <pre>
  465.      * signatureAlgorithm   AlgorithmIdentifier<p>
  466.      * AlgorithmIdentifier  ::=  SEQUENCE  {
  467.      *     algorithm               OBJECT IDENTIFIER,
  468.      *     parameters              ANY DEFINED BY algorithm OPTIONAL  }
  469.      *                             -- contains a value of the type
  470.      *                             -- registered for use with the
  471.      *                             -- algorithm object identifier value
  472.      * </pre>
  473.      * 
  474.      * <p>The algorithm name is determined from the <code>algorithm</code>
  475.      * OID string.
  476.      *
  477.      * @return the signature algorithm name.
  478.      */
  479.     public abstract String getSigAlgName();
  480.  
  481.     /**
  482.      * Gets the signature algorithm OID string from the CRL.
  483.      * An OID is represented by a set of positive whole numbers separated
  484.      * by periods.
  485.      * For example, the string "1.2.840.10040.4.3" identifies the SHA-1
  486.      * with DSA signature algorithm, as per the PKIX part I.
  487.      * 
  488.      * <p>See <a href = "#getSigAlgName">getSigAlgName</a> for 
  489.      * relevant ASN.1 definitions.
  490.      *
  491.      * @return the signature algorithm OID string.
  492.      */
  493.     public abstract String getSigAlgOID();
  494.  
  495.     /**
  496.      * Gets the DER-encoded signature algorithm parameters from this
  497.      * CRL's signature algorithm. In most cases, the signature
  498.      * algorithm parameters are null; the parameters are usually
  499.      * supplied with the public key.
  500.      * If access to individual parameter values is needed then use
  501.      * <a href="java.security.AlgorithmParameters.html">
  502.      * AlgorithmParameters</a>
  503.      * and instantiate with the name returned by
  504.      * <a href = "#getSigAlgName">getSigAlgName</a>.
  505.      * 
  506.      * <p>See <a href = "#getSigAlgName">getSigAlgName</a> for 
  507.      * relevant ASN.1 definitions.
  508.      *
  509.      * @return the DER-encoded signature algorithm parameters, or
  510.      *         null if no parameters are present.
  511.      */
  512.     public abstract byte[] getSigAlgParams();
  513. }
  514.