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