jdk/src/share/classes/sun/security/x509/X509CRLImpl.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5462 cb614e59f7f9
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.x509;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.Signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.NoSuchProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.cert.X509CRL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.cert.X509CRLEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.cert.CRLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.security.provider.X509Factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * An implmentation for X509 CRL (Certificate Revocation List).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * The X.509 v2 CRL format is described below in ASN.1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * CertificateList  ::=  SEQUENCE  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *     tbsCertList          TBSCertList,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *     signatureAlgorithm   AlgorithmIdentifier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *     signature            BIT STRING  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * More information can be found in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <a href="http://www.ietf.org/rfc/rfc3280.txt">RFC 3280: Internet X.509
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Public Key Infrastructure Certificate and CRL Profile</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * The ASN.1 definition of <code>tbsCertList</code> is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * TBSCertList  ::=  SEQUENCE  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *     version                 Version OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *                             -- if present, must be v2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *     signature               AlgorithmIdentifier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *     issuer                  Name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *     thisUpdate              ChoiceOfTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *     nextUpdate              ChoiceOfTime OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *     revokedCertificates     SEQUENCE OF SEQUENCE  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *         userCertificate         CertificateSerialNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *         revocationDate          ChoiceOfTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *         crlEntryExtensions      Extensions OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *                                 -- if present, must be v2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *         }  OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *     crlExtensions           [0]  EXPLICIT Extensions OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *                                  -- if present, must be v2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * @see X509CRL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
public class X509CRLImpl extends X509CRL {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // CRL data, and its envelope
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private byte[]      signedCRL = null; // DER encoded crl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private byte[]      signature = null; // raw signature bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private byte[]      tbsCertList = null; // DER encoded "to-be-signed" CRL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private AlgorithmId sigAlgId = null; // sig alg in CRL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // crl information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private int              version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private AlgorithmId      infoSigAlgId; // sig alg in "to-be-signed" crl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private X500Name         issuer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private X500Principal    issuerPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private Date             thisUpdate = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private Date             nextUpdate = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private Map<X509IssuerSerial,X509CRLEntry> revokedCerts = new LinkedHashMap<X509IssuerSerial,X509CRLEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private CRLExtensions    extensions = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private final static boolean isExplicit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static final long YR_2050 = 2524636800000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private boolean readOnly = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * PublicKey that has previously been used to successfully verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * the signature of this CRL. Null if the CRL has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * yet been verified (successfully).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private PublicKey verifiedPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * If verifiedPublicKey is not null, name of the provider used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * successfully verify the signature of this CRL, or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * empty String if no provider was explicitly specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private String verifiedProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Not to be used. As it would lead to cases of uninitialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * CRL objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private X509CRLImpl() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Unmarshals an X.509 CRL from its encoded form, parsing the encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * bytes.  This form of constructor is used by agents which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * need to examine and use CRL contents. Note that the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * must include only one CRL, and no "garbage" may be left at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param crlData the encoded bytes, with no trailing padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @exception CRLException on parsing errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public X509CRLImpl(byte[] crlData) throws CRLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            parse(new DerValue(crlData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            signedCRL = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new CRLException("Parsing error: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Unmarshals an X.509 CRL from an DER value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param val a DER value holding at least one CRL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @exception CRLException on parsing errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public X509CRLImpl(DerValue val) throws CRLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            parse(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            signedCRL = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new CRLException("Parsing error: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Unmarshals an X.509 CRL from an input stream. Only one CRL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * is expected at the end of the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param inStrm an input stream holding at least one CRL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @exception CRLException on parsing errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public X509CRLImpl(InputStream inStrm) throws CRLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            parse(new DerValue(inStrm));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            signedCRL = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            throw new CRLException("Parsing error: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Initial CRL constructor, no revoked certs, and no extensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param issuer the name of the CA issuing this CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param thisUpdate the Date of this issue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param nextUpdate the Date of the next CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public X509CRLImpl(X500Name issuer, Date thisDate, Date nextDate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        this.issuer = issuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        this.thisUpdate = thisDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        this.nextUpdate = nextDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * CRL constructor, revoked certs, no extensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param issuer the name of the CA issuing this CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param thisUpdate the Date of this issue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param nextUpdate the Date of the next CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param badCerts the array of CRL entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @exception CRLException on parsing/construction errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public X509CRLImpl(X500Name issuer, Date thisDate, Date nextDate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                       X509CRLEntry[] badCerts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        throws CRLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        this.issuer = issuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        this.thisUpdate = thisDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        this.nextUpdate = nextDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (badCerts != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            X500Principal crlIssuer = getIssuerX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            X500Principal badCertIssuer = crlIssuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            for (int i = 0; i < badCerts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                X509CRLEntryImpl badCert = (X509CRLEntryImpl)badCerts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    badCertIssuer = getCertIssuer(badCert, badCertIssuer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    throw new CRLException(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                badCert.setCertificateIssuer(crlIssuer, badCertIssuer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                X509IssuerSerial issuerSerial = new X509IssuerSerial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    (badCertIssuer, badCert.getSerialNumber());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                this.revokedCerts.put(issuerSerial, badCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                if (badCert.hasExtensions()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    this.version = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * CRL constructor, revoked certs and extensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param issuer the name of the CA issuing this CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param thisUpdate the Date of this issue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param nextUpdate the Date of the next CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param badCerts the array of CRL entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param crlExts the CRL extensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @exception CRLException on parsing/construction errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public X509CRLImpl(X500Name issuer, Date thisDate, Date nextDate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
               X509CRLEntry[] badCerts, CRLExtensions crlExts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        throws CRLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        this(issuer, thisDate, nextDate, badCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (crlExts != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            this.extensions = crlExts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            this.version = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Returned the encoding as an uncloned byte array. Callers must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * guarantee that they neither modify it nor expose it to untrusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public byte[] getEncodedInternal() throws CRLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (signedCRL == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            throw new CRLException("Null CRL to encode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return signedCRL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Returns the ASN.1 DER encoded form of this CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @exception CRLException if an encoding error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public byte[] getEncoded() throws CRLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return getEncodedInternal().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Encodes the "to-be-signed" CRL to the OutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param out the OutputStream to write to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @exception CRLException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public void encodeInfo(OutputStream out) throws CRLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            DerOutputStream rCerts = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            DerOutputStream seq = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (version != 0) // v2 crl encode version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                tmp.putInteger(version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            infoSigAlgId.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if ((version == 0) && (issuer.toString() == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                throw new CRLException("Null Issuer DN not allowed in v1 CRL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            issuer.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (thisUpdate.getTime() < YR_2050)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                tmp.putUTCTime(thisUpdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                tmp.putGeneralizedTime(thisUpdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (nextUpdate != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                if (nextUpdate.getTime() < YR_2050)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    tmp.putUTCTime(nextUpdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    tmp.putGeneralizedTime(nextUpdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if (!revokedCerts.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                for (X509CRLEntry entry : revokedCerts.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    ((X509CRLEntryImpl)entry).encode(rCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                tmp.write(DerValue.tag_Sequence, rCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (extensions != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                extensions.encode(tmp, isExplicit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            seq.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            tbsCertList = seq.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            out.write(tbsCertList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
             throw new CRLException("Encoding error: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Verifies that this CRL was signed using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * private key that corresponds to the given public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param key the PublicKey used to carry out the verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @exception NoSuchAlgorithmException on unsupported signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @exception InvalidKeyException on incorrect key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @exception NoSuchProviderException if there's no default provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @exception SignatureException on signature errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @exception CRLException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public void verify(PublicKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    throws CRLException, NoSuchAlgorithmException, InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
           NoSuchProviderException, SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        verify(key, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Verifies that this CRL was signed using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * private key that corresponds to the given public key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * and that the signature verification was computed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * the given provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @param key the PublicKey used to carry out the verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param sigProvider the name of the signature provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @exception NoSuchAlgorithmException on unsupported signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @exception InvalidKeyException on incorrect key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @exception NoSuchProviderException on incorrect provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @exception SignatureException on signature errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @exception CRLException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public synchronized void verify(PublicKey key, String sigProvider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            throws CRLException, NoSuchAlgorithmException, InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            NoSuchProviderException, SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (sigProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            sigProvider = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if ((verifiedPublicKey != null) && verifiedPublicKey.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            // this CRL has already been successfully verified using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            // this public key. Make sure providers match, too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (sigProvider.equals(verifiedProvider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (signedCRL == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            throw new CRLException("Uninitialized CRL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        Signature   sigVerf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (sigProvider.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            sigVerf = Signature.getInstance(sigAlgId.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            sigVerf = Signature.getInstance(sigAlgId.getName(), sigProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        sigVerf.initVerify(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (tbsCertList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            throw new CRLException("Uninitialized CRL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        sigVerf.update(tbsCertList, 0, tbsCertList.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (!sigVerf.verify(signature)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            throw new SignatureException("Signature does not match.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        verifiedPublicKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        verifiedProvider = sigProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Encodes an X.509 CRL, and signs it using the given key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @param key the private key used for signing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @param algorithm the name of the signature algorithm used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @exception NoSuchAlgorithmException on unsupported signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @exception InvalidKeyException on incorrect key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @exception NoSuchProviderException on incorrect provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @exception SignatureException on signature errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @exception CRLException if any mandatory data was omitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public void sign(PrivateKey key, String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    throws CRLException, NoSuchAlgorithmException, InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        NoSuchProviderException, SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        sign(key, algorithm, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Encodes an X.509 CRL, and signs it using the given key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param key the private key used for signing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param algorithm the name of the signature algorithm used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @exception NoSuchAlgorithmException on unsupported signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @exception InvalidKeyException on incorrect key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @exception NoSuchProviderException on incorrect provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @exception SignatureException on signature errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @exception CRLException if any mandatory data was omitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public void sign(PrivateKey key, String algorithm, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    throws CRLException, NoSuchAlgorithmException, InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        NoSuchProviderException, SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if (readOnly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                throw new CRLException("cannot over-write existing CRL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            Signature sigEngine = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            if ((provider == null) || (provider.length() == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                sigEngine = Signature.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                sigEngine = Signature.getInstance(algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            sigEngine.initSign(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                                // in case the name is reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            sigAlgId = AlgorithmId.get(sigEngine.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            infoSigAlgId = sigAlgId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            // encode crl info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            encodeInfo(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            // encode algorithm identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            sigAlgId.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            // Create and encode the signature itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            sigEngine.update(tbsCertList, 0, tbsCertList.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            signature = sigEngine.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            tmp.putBitString(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            // Wrap the signed data in a SEQUENCE { data, algorithm, sig }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            out.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            signedCRL = out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            readOnly = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            throw new CRLException("Error while encoding data: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                   e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Returns a printable string of this CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @return value of this CRL in a printable form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        sb.append("X.509 CRL v" + (version+1) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (sigAlgId != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            sb.append("Signature Algorithm: " + sigAlgId.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                  ", OID=" + (sigAlgId.getOID()).toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (issuer != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            sb.append("Issuer: " + issuer.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if (thisUpdate != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            sb.append("\nThis Update: " + thisUpdate.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if (nextUpdate != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            sb.append("Next Update: " + nextUpdate.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if (revokedCerts.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            sb.append("\nNO certificates have been revoked\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            sb.append("\nRevoked Certificates: " + revokedCerts.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            int i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            for (Iterator<X509CRLEntry> iter = revokedCerts.values().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                                             iter.hasNext(); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                sb.append("\n[" + i + "] " + iter.next().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (extensions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            Collection<Extension> allExts = extensions.getAllExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            Object[] objs = allExts.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            sb.append("\nCRL Extensions: " + objs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            for (int i = 0; i < objs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                sb.append("\n[" + (i+1) + "]: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                Extension ext = (Extension)objs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                   if (OIDMap.getClass(ext.getExtensionId()) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                       sb.append(ext.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                       byte[] extValue = ext.getExtensionValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                       if (extValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                           DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                           out.putOctetString(extValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                           extValue = out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                           HexDumpEncoder enc = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                           sb.append("Extension unknown: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                     + "DER encoded OCTET string =\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                                     + enc.encodeBuffer(extValue) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                   } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                       sb.append(ext.toString()); // sub-class exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    sb.append(", Error parsing this extension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        if (signature != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            HexDumpEncoder encoder = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            sb.append("\nSignature:\n" + encoder.encodeBuffer(signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                      + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            sb.append("NOT signed yet\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Checks whether the given certificate is on this CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @param cert the certificate to check for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @return true if the given certificate is on this CRL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    public boolean isRevoked(Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (revokedCerts.isEmpty() || (!(cert instanceof X509Certificate))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        X509Certificate xcert = (X509Certificate) cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        X509IssuerSerial issuerSerial = new X509IssuerSerial(xcert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        return revokedCerts.containsKey(issuerSerial);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Gets the version number from this CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * The ASN.1 definition for this is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * Version  ::=  INTEGER  {  v1(0), v2(1), v3(2)  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *             -- v3 does not apply to CRLs but appears for consistency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *             -- with definition of Version for certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @return the version number, i.e. 1 or 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    public int getVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        return version+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Gets the issuer distinguished name from this CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * The issuer name identifies the entity who has signed (and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * issued the CRL). The issuer name field contains an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * X.500 distinguished name (DN).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * The ASN.1 definition for this is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * issuer    Name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Name ::= CHOICE { RDNSequence }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * RelativeDistinguishedName ::=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *     SET OF AttributeValueAssertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * AttributeValueAssertion ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *                               AttributeType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *                               AttributeValue }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * AttributeType ::= OBJECT IDENTIFIER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * AttributeValue ::= ANY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * The Name describes a hierarchical name composed of attributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * such as country name, and corresponding values, such as US.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * The type of the component AttributeValue is determined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * AttributeType; in general it will be a directoryString.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * A directoryString is usually one of PrintableString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * TeletexString or UniversalString.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @return the issuer name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public Principal getIssuerDN() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        return (Principal)issuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * Return the issuer as X500Principal. Overrides method in X509CRL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * to provide a slightly more efficient version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    public X500Principal getIssuerX500Principal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (issuerPrincipal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            issuerPrincipal = issuer.asX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return issuerPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * Gets the thisUpdate date from the CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * The ASN.1 definition for this is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @return the thisUpdate date from the CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    public Date getThisUpdate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        return (new Date(thisUpdate.getTime()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * Gets the nextUpdate date from the CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @return the nextUpdate date from the CRL, or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * not present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    public Date getNextUpdate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (nextUpdate == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        return (new Date(nextUpdate.getTime()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * Gets the CRL entry with the given serial number from this CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @return the entry with the given serial number, or <code>null</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * no such entry exists in the CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @see X509CRLEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    public X509CRLEntry getRevokedCertificate(BigInteger serialNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        if (revokedCerts.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        // assume this is a direct CRL entry (cert and CRL issuer are the same)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        X509IssuerSerial issuerSerial = new X509IssuerSerial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            (getIssuerX500Principal(), serialNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        return revokedCerts.get(issuerSerial);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * Gets the CRL entry for the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    public X509CRLEntry getRevokedCertificate(X509Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        if (revokedCerts.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        X509IssuerSerial issuerSerial = new X509IssuerSerial(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        return revokedCerts.get(issuerSerial);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * Gets all the revoked certificates from the CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * A Set of X509CRLEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @return all the revoked certificates or <code>null</code> if there are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @see X509CRLEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    public Set<X509CRLEntry> getRevokedCertificates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        if (revokedCerts.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            return new HashSet<X509CRLEntry>(revokedCerts.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * Gets the DER encoded CRL information, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * <code>tbsCertList</code> from this CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * This can be used to verify the signature independently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @return the DER encoded CRL information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @exception CRLException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    public byte[] getTBSCertList() throws CRLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        if (tbsCertList == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            throw new CRLException("Uninitialized CRL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        byte[] dup = new byte[tbsCertList.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        System.arraycopy(tbsCertList, 0, dup, 0, dup.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        return dup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * Gets the raw Signature bits from the CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @return the signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    public byte[] getSignature() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        if (signature == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        byte[] dup = new byte[signature.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        System.arraycopy(signature, 0, dup, 0, dup.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        return dup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * Gets the signature algorithm name for the CRL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * signature algorithm. For example, the string "SHA1withDSA".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * The ASN.1 definition for this is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * AlgorithmIdentifier  ::=  SEQUENCE  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *     algorithm               OBJECT IDENTIFIER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *     parameters              ANY DEFINED BY algorithm OPTIONAL  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *                             -- contains a value of the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     *                             -- registered for use with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *                             -- algorithm object identifier value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @return the signature algorithm name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    public String getSigAlgName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        if (sigAlgId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        return sigAlgId.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * Gets the signature algorithm OID string from the CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * An OID is represented by a set of positive whole number separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * by ".", that means,<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * &lt;positive whole number&gt;.&lt;positive whole number&gt;.&lt;...&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * For example, the string "1.2.840.10040.4.3" identifies the SHA-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * with DSA signature algorithm defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * <a href="http://www.ietf.org/rfc/rfc3279.txt">RFC 3279: Algorithms and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Identifiers for the Internet X.509 Public Key Infrastructure Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * and CRL Profile</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @return the signature algorithm oid string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    public String getSigAlgOID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        if (sigAlgId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        ObjectIdentifier oid = sigAlgId.getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        return oid.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * Gets the DER encoded signature algorithm parameters from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * CRL's signature algorithm. In most cases, the signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * algorithm parameters are null, the parameters are usually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * supplied with the Public Key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @return the DER encoded signature algorithm parameters, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *         null if no parameters are present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    public byte[] getSigAlgParams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        if (sigAlgId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            return sigAlgId.getEncodedParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * return the AuthorityKeyIdentifier, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @returns AuthorityKeyIdentifier or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *          (if no AuthorityKeyIdentifierExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    public KeyIdentifier getAuthKeyId() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        AuthorityKeyIdentifierExtension aki = getAuthKeyIdExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        if (aki != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            KeyIdentifier keyId = (KeyIdentifier)aki.get(aki.KEY_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            return keyId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * return the AuthorityKeyIdentifierExtension, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @returns AuthorityKeyIdentifierExtension or null (if no such extension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    public AuthorityKeyIdentifierExtension getAuthKeyIdExtension()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        Object obj = getExtension(PKIXExtensions.AuthorityKey_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        return (AuthorityKeyIdentifierExtension)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * return the CRLNumberExtension, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * @returns CRLNumberExtension or null (if no such extension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    public CRLNumberExtension getCRLNumberExtension() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        Object obj = getExtension(PKIXExtensions.CRLNumber_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        return (CRLNumberExtension)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * return the CRL number from the CRLNumberExtension, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @returns number or null (if no such extension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    public BigInteger getCRLNumber() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        CRLNumberExtension numExt = getCRLNumberExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        if (numExt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            BigInteger num = (BigInteger)numExt.get(numExt.NUMBER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            return num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * return the DeltaCRLIndicatorExtension, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * @returns DeltaCRLIndicatorExtension or null (if no such extension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    public DeltaCRLIndicatorExtension getDeltaCRLIndicatorExtension()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        Object obj = getExtension(PKIXExtensions.DeltaCRLIndicator_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        return (DeltaCRLIndicatorExtension)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * return the base CRL number from the DeltaCRLIndicatorExtension, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @returns number or null (if no such extension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    public BigInteger getBaseCRLNumber() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        DeltaCRLIndicatorExtension dciExt = getDeltaCRLIndicatorExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        if (dciExt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            BigInteger num = (BigInteger)dciExt.get(dciExt.NUMBER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            return num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * return the IssuerAlternativeNameExtension, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * @returns IssuerAlternativeNameExtension or null (if no such extension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    public IssuerAlternativeNameExtension getIssuerAltNameExtension()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        Object obj = getExtension(PKIXExtensions.IssuerAlternativeName_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        return (IssuerAlternativeNameExtension)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * return the IssuingDistributionPointExtension, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * @returns IssuingDistributionPointExtension or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     *          (if no such extension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    public IssuingDistributionPointExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        getIssuingDistributionPointExtension() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        Object obj = getExtension(PKIXExtensions.IssuingDistributionPoint_Id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        return (IssuingDistributionPointExtension) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * Return true if a critical extension is found that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * not supported, otherwise return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    public boolean hasUnsupportedCriticalExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        if (extensions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        return extensions.hasUnsupportedCriticalExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * Gets a Set of the extension(s) marked CRITICAL in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * CRL. In the returned set, each extension is represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * its OID string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @return a set of the extension oid strings in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * CRL that are marked critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    public Set<String> getCriticalExtensionOIDs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        if (extensions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        Set<String> extSet = new HashSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        for (Extension ex : extensions.getAllExtensions()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            if (ex.isCritical()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                extSet.add(ex.getExtensionId().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        return extSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * Gets a Set of the extension(s) marked NON-CRITICAL in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * CRL. In the returned set, each extension is represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * its OID string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @return a set of the extension oid strings in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * CRL that are NOT marked critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    public Set<String> getNonCriticalExtensionOIDs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        if (extensions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        Set<String> extSet = new HashSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        for (Extension ex : extensions.getAllExtensions()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            if (!ex.isCritical()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                extSet.add(ex.getExtensionId().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        return extSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * Gets the DER encoded OCTET string for the extension value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * (<code>extnValue</code>) identified by the passed in oid String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * The <code>oid</code> string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * represented by a set of positive whole number separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * by ".", that means,<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * &lt;positive whole number&gt;.&lt;positive whole number&gt;.&lt;...&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * @param oid the Object Identifier value for the extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * @return the der encoded octet string of the extension value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    public byte[] getExtensionValue(String oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        if (extensions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            String extAlias = OIDMap.getName(new ObjectIdentifier(oid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            Extension crlExt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            if (extAlias == null) { // may be unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                ObjectIdentifier findOID = new ObjectIdentifier(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                Extension ex = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                ObjectIdentifier inCertOID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                for (Enumeration<Extension> e = extensions.getElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                                                 e.hasMoreElements();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                    ex = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                    inCertOID = ex.getExtensionId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    if (inCertOID.equals(findOID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                        crlExt = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                crlExt = extensions.get(extAlias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            if (crlExt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            byte[] extData = crlExt.getExtensionValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            if (extData == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            out.putOctetString(extData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * get an extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * @param oid ObjectIdentifier of extension desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * @returns Object of type <extension> or null, if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    public Object getExtension(ObjectIdentifier oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        if (extensions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        // XXX Consider cloning this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        return extensions.get(OIDMap.getName(oid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * Parses an X.509 CRL, should be used only by constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    private void parse(DerValue val) throws CRLException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        // check if can over write the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        if (readOnly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            throw new CRLException("cannot over-write existing CRL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        if ( val.getData() == null || val.tag != DerValue.tag_Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            throw new CRLException("Invalid DER-encoded CRL data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        signedCRL = val.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        DerValue seq[] = new DerValue[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        seq[0] = val.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        seq[1] = val.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        seq[2] = val.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        if (val.data.available() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            throw new CRLException("signed overrun, bytes = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                                     + val.data.available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        if (seq[0].tag != DerValue.tag_Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            throw new CRLException("signed CRL fields invalid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        sigAlgId = AlgorithmId.parse(seq[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        signature = seq[2].getBitString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        if (seq[1].data.available() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            throw new CRLException("AlgorithmId field overrun");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        if (seq[2].data.available() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            throw new CRLException("Signature field overrun");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        // the tbsCertsList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        tbsCertList = seq[0].toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        // parse the information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        DerInputStream derStrm = seq[0].data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        DerValue       tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        byte           nextByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        // version (optional if v1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        version = 0;   // by default, version = v1 == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        nextByte = (byte)derStrm.peekByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        if (nextByte == DerValue.tag_Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            version = derStrm.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            if (version != 1)  // i.e. v2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                throw new CRLException("Invalid version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        tmp = derStrm.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        // signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        AlgorithmId tmpId = AlgorithmId.parse(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        // the "inner" and "outer" signature algorithms must match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        if (! tmpId.equals(sigAlgId))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            throw new CRLException("Signature algorithm mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        infoSigAlgId = tmpId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        // issuer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        issuer = new X500Name(derStrm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        if (issuer.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            throw new CRLException("Empty issuer DN not allowed in X509CRLs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        // thisUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        // check if UTCTime encoded or GeneralizedTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        nextByte = (byte)derStrm.peekByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        if (nextByte == DerValue.tag_UtcTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            thisUpdate = derStrm.getUTCTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        } else if (nextByte == DerValue.tag_GeneralizedTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            thisUpdate = derStrm.getGeneralizedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            throw new CRLException("Invalid encoding for thisUpdate"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                                   + " (tag=" + nextByte + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        if (derStrm.available() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
           return;     // done parsing no more optional fields present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        // nextUpdate (optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        nextByte = (byte)derStrm.peekByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        if (nextByte == DerValue.tag_UtcTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            nextUpdate = derStrm.getUTCTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        } else if (nextByte == DerValue.tag_GeneralizedTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            nextUpdate = derStrm.getGeneralizedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        } // else it is not present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        if (derStrm.available() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            return;     // done parsing no more optional fields present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        // revokedCertificates (optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        nextByte = (byte)derStrm.peekByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        if ((nextByte == DerValue.tag_SequenceOf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            && (! ((nextByte & 0x0c0) == 0x080))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            DerValue[] badCerts = derStrm.getSequence(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            X500Principal crlIssuer = getIssuerX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            X500Principal badCertIssuer = crlIssuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            for (int i = 0; i < badCerts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                X509CRLEntryImpl entry = new X509CRLEntryImpl(badCerts[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                badCertIssuer = getCertIssuer(entry, badCertIssuer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                entry.setCertificateIssuer(crlIssuer, badCertIssuer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                X509IssuerSerial issuerSerial = new X509IssuerSerial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                    (badCertIssuer, entry.getSerialNumber());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                revokedCerts.put(issuerSerial, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        if (derStrm.available() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            return;     // done parsing no extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        // crlExtensions (optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        tmp = derStrm.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        if (tmp.isConstructed() && tmp.isContextSpecific((byte)0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            extensions = new CRLExtensions(tmp.data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        readOnly = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * Extract the issuer X500Principal from an X509CRL. Parses the encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * form of the CRL to preserve the principal's ASN.1 encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * Called by java.security.cert.X509CRL.getIssuerX500Principal().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    public static X500Principal getIssuerX500Principal(X509CRL crl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            byte[] encoded = crl.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            DerInputStream derIn = new DerInputStream(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            DerValue tbsCert = derIn.getSequence(3)[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            DerInputStream tbsIn = tbsCert.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            DerValue tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            // skip version number if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            byte nextByte = (byte)tbsIn.peekByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            if (nextByte == DerValue.tag_Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                tmp = tbsIn.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            tmp = tbsIn.getDerValue();  // skip signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            tmp = tbsIn.getDerValue();  // issuer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            byte[] principalBytes = tmp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            return new X500Principal(principalBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            throw new RuntimeException("Could not parse issuer", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * Returned the encoding of the given certificate for internal use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * Callers must guarantee that they neither modify it nor expose it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * to untrusted code. Uses getEncodedInternal() if the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * is instance of X509CertImpl, getEncoded() otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    public static byte[] getEncodedInternal(X509CRL crl) throws CRLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        if (crl instanceof X509CRLImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            return ((X509CRLImpl)crl).getEncodedInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            return crl.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * Utility method to convert an arbitrary instance of X509CRL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * to a X509CRLImpl. Does a cast if possible, otherwise reparses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * the encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    public static X509CRLImpl toImpl(X509CRL crl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            throws CRLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        if (crl instanceof X509CRLImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            return (X509CRLImpl)crl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            return X509Factory.intern(crl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * Returns the X500 certificate issuer DN of a CRL entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * @param entry the entry to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * @param prevCertIssuer the previous entry's certificate issuer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * @return the X500Principal in a CertificateIssuerExtension, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     *   prevCertIssuer if it does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    private X500Principal getCertIssuer(X509CRLEntryImpl entry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        X500Principal prevCertIssuer) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        CertificateIssuerExtension ciExt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            entry.getCertificateIssuerExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        if (ciExt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            GeneralNames names = (GeneralNames)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                ciExt.get(CertificateIssuerExtension.ISSUER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            X500Name issuerDN = (X500Name) names.get(0).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            return issuerDN.asX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            return prevCertIssuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * Immutable X.509 Certificate Issuer DN and serial number pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    private final static class X509IssuerSerial {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        final X500Principal issuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        final BigInteger serial;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        volatile int hashcode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
         * Create an X509IssuerSerial.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
         * @param issuer the issuer DN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
         * @param serial the serial number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        X509IssuerSerial(X500Principal issuer, BigInteger serial) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            this.issuer = issuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            this.serial = serial;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
         * Construct an X509IssuerSerial from an X509Certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        X509IssuerSerial(X509Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            this(cert.getIssuerX500Principal(), cert.getSerialNumber());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
         * Returns the issuer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
         * @return the issuer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        X500Principal getIssuer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            return issuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
         * Returns the serial number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
         * @return the serial number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        BigInteger getSerial() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            return serial;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
         * Compares this X509Serial with another and returns true if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
         * are equivalent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
         * @param o the other object to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
         * @return true if equal, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            if (o == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            if (!(o instanceof X509IssuerSerial)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            X509IssuerSerial other = (X509IssuerSerial) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            if (serial.equals(other.getSerial()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                issuer.equals(other.getIssuer())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
         * Returns a hash code value for this X509IssuerSerial.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
         * @return the hash code value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            if (hashcode == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                int result = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                result = 37*result + issuer.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                result = 37*result + serial.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                hashcode = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            return hashcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
}