jdk/src/share/classes/sun/security/provider/certpath/BasicChecker.java
author mullan
Wed, 30 May 2012 17:19:46 -0400
changeset 12860 9ffbd4e43413
parent 5506 202f599c92aa
child 13021 72a4386f8976
permissions -rw-r--r--
6854712: Revocation checking enhancements (JEP-124) 6637288: Add OCSP support to PKIX CertPathBuilder implementation 7126011: ReverseBuilder.getMatchingCACerts may throws NPE Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2012, 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: 1451
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: 1451
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: 1451
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1451
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1451
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.provider.certpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Set;
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
    32
import java.security.GeneralSecurityException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.KeyFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PublicKey;
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
    35
import java.security.SignatureException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.Certificate;
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
    37
import java.security.cert.CertificateExpiredException;
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
    38
import java.security.cert.CertificateNotYetValidException;
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
    39
import java.security.cert.CertPathValidatorException;
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
    40
import java.security.cert.CertPathValidatorException.BasicReason;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.cert.PKIXCertPathChecker;
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
    43
import java.security.cert.PKIXReason;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.cert.TrustAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.interfaces.DSAParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.interfaces.DSAPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.spec.DSAPublicKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.security.x509.X500Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * BasicChecker is a PKIXCertPathChecker that checks the basic information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * on a PKIX certificate, namely the signature, timestamp, and subject/issuer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * name chaining.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
class BasicChecker extends PKIXCertPathChecker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final PublicKey trustedPubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final X500Principal caName;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    65
    private final Date date;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final String sigProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final boolean sigOnly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private X500Principal prevSubject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private PublicKey prevPubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Constructor that initializes the input parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param anchor the anchor selected to validate the target certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param testDate the time for which the validity of the certificate
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    76
     *        should be determined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param sigProvider the name of the signature provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param sigOnly true if only signature checking is to be done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *        if false, all checks are done
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    81
    BasicChecker(TrustAnchor anchor, Date date, String sigProvider,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    82
                 boolean sigOnly) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (anchor.getTrustedCert() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            this.trustedPubKey = anchor.getTrustedCert().getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            this.caName = anchor.getTrustedCert().getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            this.trustedPubKey = anchor.getCAPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            this.caName = anchor.getCA();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    90
        this.date = date;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        this.sigProvider = sigProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        this.sigOnly = sigOnly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Initializes the internal state of the checker from parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * specified in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    99
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public void init(boolean forward) throws CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (!forward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            prevPubKey = trustedPubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            prevSubject = caName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                CertPathValidatorException("forward checking not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   110
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public boolean isForwardCheckingSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   115
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public Set<String> getSupportedExtensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Performs the signature, timestamp, and subject/issuer name chaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * checks on the certificate using its internal state. This method does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * not remove any critical extensions from the Collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param cert the Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param unresolvedCritExts a Collection of the unresolved critical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * extensions
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   128
     * @throws CertPathValidatorException if certificate does not verify
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   130
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public void check(Certificate cert, Collection<String> unresolvedCritExts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   134
        X509Certificate currCert = (X509Certificate)cert;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (!sigOnly) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   137
            verifyTimestamp(currCert);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   138
            verifyNameChaining(currCert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   140
        verifySignature(currCert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        updateState(currCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   146
     * Verifies the signature on the certificate using the previous public key.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   147
     *
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   148
     * @param cert the X509Certificate
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   149
     * @throws CertPathValidatorException if certificate does not verify
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   151
    private void verifySignature(X509Certificate cert)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   152
        throws CertPathValidatorException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        String msg = "signature";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            debug.println("---checking " + msg + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            cert.verify(prevPubKey, sigProvider);
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   160
        } catch (SignatureException e) {
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   161
            throw new CertPathValidatorException
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   162
                (msg + " check failed", e, null, -1,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   163
                 BasicReason.INVALID_SIGNATURE);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   164
        } catch (GeneralSecurityException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new CertPathValidatorException(msg + " check failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            debug.println(msg + " verified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Internal method to verify the timestamp on a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   175
    private void verifyTimestamp(X509Certificate cert)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        String msg = "timestamp";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            debug.println("---checking " + msg + ":" + date.toString() + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            cert.checkValidity(date);
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   184
        } catch (CertificateExpiredException e) {
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   185
            throw new CertPathValidatorException
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   186
                (msg + " check failed", e, null, -1, BasicReason.EXPIRED);
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   187
        } catch (CertificateNotYetValidException e) {
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   188
            throw new CertPathValidatorException
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   189
                (msg + " check failed", e, null, -1, BasicReason.NOT_YET_VALID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            debug.println(msg + " verified.");
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
     * Internal method to check that cert has a valid DN to be next in a chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   199
    private void verifyNameChaining(X509Certificate cert)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   200
        throws CertPathValidatorException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (prevSubject != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            String msg = "subject/issuer name chaining";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                debug.println("---checking " + msg + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            X500Principal currIssuer = cert.getIssuerX500Principal();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   209
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            // reject null or empty issuer DNs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (X500Name.asX500Name(currIssuer).isEmpty()) {
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   212
                throw new CertPathValidatorException
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   213
                    (msg + " check failed: " +
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   214
                     "empty/null issuer DN in certificate is invalid", null,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   215
                     null, -1, PKIXReason.NAME_CHAINING);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (!(currIssuer.equals(prevSubject))) {
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   219
                throw new CertPathValidatorException
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   220
                    (msg + " check failed", null, null, -1,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   221
                     PKIXReason.NAME_CHAINING);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                debug.println(msg + " verified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Internal method to manage state information at each iteration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private void updateState(X509Certificate currCert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        PublicKey cKey = currCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            debug.println("BasicChecker.updateState issuer: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                currCert.getIssuerX500Principal().toString() + "; subject: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                currCert.getSubjectX500Principal() + "; serial#: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                currCert.getSerialNumber().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (cKey instanceof DSAPublicKey &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            ((DSAPublicKey)cKey).getParams() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            //cKey needs to inherit DSA parameters from prev key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            cKey = makeInheritedParamsKey(cKey, prevPubKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (debug != null) debug.println("BasicChecker.updateState Made " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                             "key with inherited params");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        prevPubKey = cKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        prevSubject = currCert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Internal method to create a new key with inherited key parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param keyValueKey key from which to obtain key value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param keyParamsKey key from which to obtain key parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @return new public key having value and parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @throws CertPathValidatorException if keys are not appropriate types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * for this operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    static PublicKey makeInheritedParamsKey(PublicKey keyValueKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        PublicKey keyParamsKey) throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        PublicKey usableKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (!(keyValueKey instanceof DSAPublicKey) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            !(keyParamsKey instanceof DSAPublicKey))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            throw new CertPathValidatorException("Input key is not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                                 "appropriate type for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                                 "inheriting parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        DSAParams params = ((DSAPublicKey)keyParamsKey).getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (params == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            throw new CertPathValidatorException("Key parameters missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            BigInteger y = ((DSAPublicKey)keyValueKey).getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            KeyFactory kf = KeyFactory.getInstance("DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            DSAPublicKeySpec ks = new DSAPublicKeySpec(y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                                       params.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                                       params.getQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                                       params.getG());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            usableKey = kf.generatePublic(ks);
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   282
        } catch (GeneralSecurityException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            throw new CertPathValidatorException("Unable to generate key with" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                                 " inherited parameters: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                                 e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return usableKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * return the public key associated with the last certificate processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @return PublicKey the last public key processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    PublicKey getPublicKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return prevPubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
}