jdk/src/share/classes/sun/security/provider/certpath/BasicChecker.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1238 6d1f4b722acd
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.KeyFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.PKIXCertPathChecker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.CertPathValidatorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.TrustAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.interfaces.DSAParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.interfaces.DSAPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.spec.DSAPublicKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.x509.X500Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * BasicChecker is a PKIXCertPathChecker that checks the basic information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * on a PKIX certificate, namely the signature, timestamp, and subject/issuer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * name chaining.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
class BasicChecker extends PKIXCertPathChecker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final PublicKey trustedPubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final X500Principal caName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private final Date testDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private final String sigProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final boolean sigOnly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private X500Principal prevSubject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private PublicKey prevPubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Constructor that initializes the input parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param anchor the anchor selected to validate the target certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param testDate the time for which the validity of the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * should be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param sigProvider the name of the signature provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param sigOnly true if only signature checking is to be done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *        if false, all checks are done
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    BasicChecker(TrustAnchor anchor, Date testDate, String sigProvider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        boolean sigOnly) throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (anchor.getTrustedCert() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            this.trustedPubKey = anchor.getTrustedCert().getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            this.caName = anchor.getTrustedCert().getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            this.trustedPubKey = anchor.getCAPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            this.caName = anchor.getCA();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this.testDate = testDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this.sigProvider = sigProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.sigOnly = sigOnly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        init(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Initializes the internal state of the checker from parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * specified in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public void init(boolean forward) throws CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (!forward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            prevPubKey = trustedPubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            prevSubject = caName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                CertPathValidatorException("forward checking not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public boolean isForwardCheckingSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public Set<String> getSupportedExtensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Performs the signature, timestamp, and subject/issuer name chaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * checks on the certificate using its internal state. This method does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * not remove any critical extensions from the Collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param cert the Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param unresolvedCritExts a Collection of the unresolved critical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @exception CertPathValidatorException Exception thrown if certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * does not verify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public void check(Certificate cert, Collection<String> unresolvedCritExts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        X509Certificate currCert = (X509Certificate) cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (!sigOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            verifyTimestamp(currCert, testDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            verifyNameChaining(currCert, prevSubject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        verifySignature(currCert, prevPubKey, sigProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        updateState(currCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Verifies the signature on the certificate using the previous public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param cert the Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param prevPubKey the previous PublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param sigProvider a String containing the signature provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @exception CertPathValidatorException Exception thrown if certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * does not verify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private void verifySignature(X509Certificate cert, PublicKey prevPubKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        String sigProvider) throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        String msg = "signature";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            debug.println("---checking " + msg + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            cert.verify(prevPubKey, sigProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                debug.println(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new CertPathValidatorException(msg + " check failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            debug.println(msg + " verified.");
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
     * Internal method to verify the timestamp on a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private void verifyTimestamp(X509Certificate cert, Date date)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        String msg = "timestamp";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            debug.println("---checking " + msg + ":" + date.toString() + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            cert.checkValidity(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                debug.println(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw new CertPathValidatorException(msg + " check failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            debug.println(msg + " verified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Internal method to check that cert has a valid DN to be next in a chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private void verifyNameChaining(X509Certificate cert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        X500Principal prevSubject) throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (prevSubject != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            String msg = "subject/issuer name chaining";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                debug.println("---checking " + msg + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            X500Principal currIssuer = cert.getIssuerX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            // reject null or empty issuer DNs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (X500Name.asX500Name(currIssuer).isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                throw new CertPathValidatorException(msg + " check failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    "empty/null issuer DN in certificate is invalid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (!(currIssuer.equals(prevSubject))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                throw new CertPathValidatorException(msg + " check failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                debug.println(msg + " verified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Internal method to manage state information at each iteration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    private void updateState(X509Certificate currCert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        PublicKey cKey = currCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            debug.println("BasicChecker.updateState issuer: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                currCert.getIssuerX500Principal().toString() + "; subject: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                currCert.getSubjectX500Principal() + "; serial#: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                currCert.getSerialNumber().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (cKey instanceof DSAPublicKey &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            ((DSAPublicKey)cKey).getParams() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            //cKey needs to inherit DSA parameters from prev key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            cKey = makeInheritedParamsKey(cKey, prevPubKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (debug != null) debug.println("BasicChecker.updateState Made " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                             "key with inherited params");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        prevPubKey = cKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        prevSubject = currCert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Internal method to create a new key with inherited key parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param keyValueKey key from which to obtain key value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param keyParamsKey key from which to obtain key parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @return new public key having value and parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @throws CertPathValidatorException if keys are not appropriate types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * for this operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    static PublicKey makeInheritedParamsKey(PublicKey keyValueKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        PublicKey keyParamsKey) throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        PublicKey usableKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (!(keyValueKey instanceof DSAPublicKey) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            !(keyParamsKey instanceof DSAPublicKey))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            throw new CertPathValidatorException("Input key is not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                                 "appropriate type for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                                 "inheriting parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        DSAParams params = ((DSAPublicKey)keyParamsKey).getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (params == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throw new CertPathValidatorException("Key parameters missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            BigInteger y = ((DSAPublicKey)keyValueKey).getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            KeyFactory kf = KeyFactory.getInstance("DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            DSAPublicKeySpec ks = new DSAPublicKeySpec(y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                                       params.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                                       params.getQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                                       params.getG());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            usableKey = kf.generatePublic(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            throw new CertPathValidatorException("Unable to generate key with" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                                                 " inherited parameters: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                                                 e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return usableKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * return the public key associated with the last certificate processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @return PublicKey the last public key processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    PublicKey getPublicKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return prevPubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
}