src/java.base/share/classes/sun/security/provider/certpath/BasicChecker.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 44046 jdk/src/java.base/share/classes/sun/security/provider/certpath/BasicChecker.java@762e807bfac1
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 25859
diff changeset
    54
 * on a PKIX certificate, namely the signature, validity, and subject/issuer
2
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;
13021
72a4386f8976 7176326: CertPath/CertPathBuilderTest failures after webrev 6854712_6637288_7126011
mullan
parents: 12860
diff changeset
    93
        this.prevPubKey = trustedPubKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Initializes the internal state of the checker from parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * specified in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   100
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public void init(boolean forward) throws CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (!forward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            prevPubKey = trustedPubKey;
13806
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 13555
diff changeset
   104
            if (PKIX.isDSAPublicKeyWithoutParams(prevPubKey)) {
13555
fb41599cf196 7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents: 13021
diff changeset
   105
                // If TrustAnchor is a DSA public key and it has no params, it
fb41599cf196 7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents: 13021
diff changeset
   106
                // cannot be used to verify the signature of the first cert,
fb41599cf196 7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents: 13021
diff changeset
   107
                // so throw exception
fb41599cf196 7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents: 13021
diff changeset
   108
                throw new CertPathValidatorException("Key parameters missing");
fb41599cf196 7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents: 13021
diff changeset
   109
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            prevSubject = caName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                CertPathValidatorException("forward checking not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   117
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public boolean isForwardCheckingSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   122
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public Set<String> getSupportedExtensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 25859
diff changeset
   128
     * Performs the signature, validity, and subject/issuer name chaining
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * checks on the certificate using its internal state. This method does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * not remove any critical extensions from the Collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param cert the Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param unresolvedCritExts a Collection of the unresolved critical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * extensions
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   135
     * @throws CertPathValidatorException if certificate does not verify
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   137
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public void check(Certificate cert, Collection<String> unresolvedCritExts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   141
        X509Certificate currCert = (X509Certificate)cert;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (!sigOnly) {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 25859
diff changeset
   144
            verifyValidity(currCert);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   145
            verifyNameChaining(currCert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   147
        verifySignature(currCert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        updateState(currCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   153
     * Verifies the signature on the certificate using the previous public key.
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   154
     *
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   155
     * @param cert the X509Certificate
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   156
     * @throws CertPathValidatorException if certificate does not verify
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   158
    private void verifySignature(X509Certificate cert)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   159
        throws CertPathValidatorException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        String msg = "signature";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            debug.println("---checking " + msg + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            cert.verify(prevPubKey, sigProvider);
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   167
        } catch (SignatureException e) {
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   168
            throw new CertPathValidatorException
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   169
                (msg + " check failed", e, null, -1,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   170
                 BasicReason.INVALID_SIGNATURE);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   171
        } catch (GeneralSecurityException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            throw new CertPathValidatorException(msg + " check failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            debug.println(msg + " verified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 25859
diff changeset
   180
     * Internal method to verify the validity on a certificate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 25859
diff changeset
   182
    private void verifyValidity(X509Certificate cert)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 25859
diff changeset
   185
        String msg = "validity";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            debug.println("---checking " + msg + ":" + date.toString() + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            cert.checkValidity(date);
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   191
        } catch (CertificateExpiredException e) {
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   192
            throw new CertPathValidatorException
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   193
                (msg + " check failed", e, null, -1, BasicReason.EXPIRED);
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   194
        } catch (CertificateNotYetValidException e) {
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   195
            throw new CertPathValidatorException
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   196
                (msg + " check failed", e, null, -1, BasicReason.NOT_YET_VALID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            debug.println(msg + " verified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Internal method to check that cert has a valid DN to be next in a chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   206
    private void verifyNameChaining(X509Certificate cert)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   207
        throws CertPathValidatorException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (prevSubject != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            String msg = "subject/issuer name chaining";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                debug.println("---checking " + msg + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            X500Principal currIssuer = cert.getIssuerX500Principal();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   216
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            // reject null or empty issuer DNs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (X500Name.asX500Name(currIssuer).isEmpty()) {
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: " +
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   221
                     "empty/null issuer DN in certificate is invalid", null,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   222
                     null, -1, PKIXReason.NAME_CHAINING);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (!(currIssuer.equals(prevSubject))) {
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   226
                throw new CertPathValidatorException
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   227
                    (msg + " check failed", null, null, -1,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   228
                     PKIXReason.NAME_CHAINING);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                debug.println(msg + " verified.");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Internal method to manage state information at each iteration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private void updateState(X509Certificate currCert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        PublicKey cKey = currCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            debug.println("BasicChecker.updateState issuer: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                currCert.getIssuerX500Principal().toString() + "; subject: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                currCert.getSubjectX500Principal() + "; serial#: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                currCert.getSerialNumber().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
13806
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 13555
diff changeset
   249
        if (PKIX.isDSAPublicKeyWithoutParams(cKey)) {
13555
fb41599cf196 7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents: 13021
diff changeset
   250
            // cKey needs to inherit DSA parameters from prev key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            cKey = makeInheritedParamsKey(cKey, prevPubKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (debug != null) debug.println("BasicChecker.updateState Made " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                             "key with inherited params");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        prevPubKey = cKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        prevSubject = currCert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
13555
fb41599cf196 7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents: 13021
diff changeset
   260
     * Internal method to create a new key with inherited key parameters.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param keyValueKey key from which to obtain key value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param keyParamsKey key from which to obtain key parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return new public key having value and parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @throws CertPathValidatorException if keys are not appropriate types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * for this operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    static PublicKey makeInheritedParamsKey(PublicKey keyValueKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        PublicKey keyParamsKey) throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (!(keyValueKey instanceof DSAPublicKey) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            !(keyParamsKey instanceof DSAPublicKey))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            throw new CertPathValidatorException("Input key is not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                                                 "appropriate type for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                                                 "inheriting parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        DSAParams params = ((DSAPublicKey)keyParamsKey).getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (params == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            throw new CertPathValidatorException("Key parameters missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            BigInteger y = ((DSAPublicKey)keyValueKey).getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            KeyFactory kf = KeyFactory.getInstance("DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            DSAPublicKeySpec ks = new DSAPublicKeySpec(y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                                       params.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                                       params.getQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                                       params.getG());
13555
fb41599cf196 7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents: 13021
diff changeset
   286
            return kf.generatePublic(ks);
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   287
        } catch (GeneralSecurityException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            throw new CertPathValidatorException("Unable to generate key with" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                                 " inherited parameters: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                                 e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * return the public key associated with the last certificate processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @return PublicKey the last public key processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    PublicKey getPublicKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return prevPubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
}