“ecf40a659bb87b144d60b8df7ee59c953e7fbd30”上不存在“courses/free-courses-pt_BR.md”
AlgorithmChecker.java 18.4 KB
Newer Older
X
xuelei 已提交
1
/*
2
 * Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
X
xuelei 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
X
xuelei 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
X
xuelei 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
X
xuelei 已提交
24 25 26 27
 */

package sun.security.provider.certpath;

28 29
import java.security.AlgorithmConstraints;
import java.security.CryptoPrimitive;
30 31
import java.security.Timestamp;
import java.security.cert.CertPathValidator;
X
xuelei 已提交
32
import java.util.Collection;
33
import java.util.Collections;
34
import java.util.Date;
35 36 37 38 39 40 41
import java.util.Set;
import java.util.EnumSet;
import java.math.BigInteger;
import java.security.PublicKey;
import java.security.KeyFactory;
import java.security.AlgorithmParameters;
import java.security.GeneralSecurityException;
X
xuelei 已提交
42 43
import java.security.cert.Certificate;
import java.security.cert.X509CRL;
44
import java.security.cert.X509Certificate;
X
xuelei 已提交
45
import java.security.cert.PKIXCertPathChecker;
46 47 48 49
import java.security.cert.TrustAnchor;
import java.security.cert.CRLException;
import java.security.cert.CertificateException;
import java.security.cert.CertPathValidatorException;
50 51
import java.security.cert.CertPathValidatorException.BasicReason;
import java.security.cert.PKIXReason;
52 53 54
import java.security.interfaces.DSAParams;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.DSAPublicKeySpec;
X
xuelei 已提交
55

56
import sun.security.util.AnchorCertificates;
57
import sun.security.util.ConstraintsParameters;
58
import sun.security.util.Debug;
59
import sun.security.util.DisabledAlgorithmConstraints;
60
import sun.security.validator.Validator;
61 62
import sun.security.x509.X509CertImpl;
import sun.security.x509.X509CRLImpl;
X
xuelei 已提交
63 64 65
import sun.security.x509.AlgorithmId;

/**
66
 * A {@code PKIXCertPathChecker} implementation to check whether a
67 68 69 70 71
 * specified certificate contains the required algorithm constraints.
 * <p>
 * Certificate fields such as the subject public key, the signature
 * algorithm, key usage, extended key usage, etc. need to conform to
 * the specified algorithm constraints.
X
xuelei 已提交
72
 *
73 74
 * @see PKIXCertPathChecker
 * @see PKIXParameters
X
xuelei 已提交
75
 */
76
public final class AlgorithmChecker extends PKIXCertPathChecker {
77
    private static final Debug debug = Debug.getInstance("certpath");
X
xuelei 已提交
78

79 80
    private final AlgorithmConstraints constraints;
    private final PublicKey trustedPubKey;
81
    private final Date pkixdate;
82
    private PublicKey prevPubKey;
83 84
    private final Timestamp jarTimestamp;
    private final String variant;
85

86
    private static final Set<CryptoPrimitive> SIGNATURE_PRIMITIVE_SET =
J
juh 已提交
87
        Collections.unmodifiableSet(EnumSet.of(CryptoPrimitive.SIGNATURE));
X
xuelei 已提交
88

89
    private static final Set<CryptoPrimitive> KU_PRIMITIVE_SET =
I
igerasim 已提交
90 91 92 93 94 95
        Collections.unmodifiableSet(EnumSet.of(
            CryptoPrimitive.SIGNATURE,
            CryptoPrimitive.KEY_ENCAPSULATION,
            CryptoPrimitive.PUBLIC_KEY_ENCRYPTION,
            CryptoPrimitive.KEY_AGREEMENT));

96
    private static final DisabledAlgorithmConstraints
97 98
        certPathDefaultConstraints = new DisabledAlgorithmConstraints(
            DisabledAlgorithmConstraints.PROPERTY_CERTPATH_DISABLED_ALGS);
X
xuelei 已提交
99

100 101 102 103 104 105 106 107
    // If there is no "cacerts" keyword, then disable anchor checking
    private static final boolean publicCALimits =
            certPathDefaultConstraints.checkProperty("jdkCA");

    // If anchor checking enabled, this will be true if the trust anchor
    // has a match in the cacerts file
    private boolean trustedMatch = false;

X
xuelei 已提交
108
    /**
109 110
     * Create a new {@code AlgorithmChecker} with the given algorithm
     * given {@code TrustAnchor} and {@code String} variant.
111 112 113
     *
     * @param anchor the trust anchor selected to validate the target
     *     certificate
114 115
     * @param variant is the Validator variants of the operation. A null value
     *                passed will set it to Validator.GENERIC.
X
xuelei 已提交
116
     */
117 118
    public AlgorithmChecker(TrustAnchor anchor, String variant) {
        this(anchor, certPathDefaultConstraints, null, null, variant);
X
xuelei 已提交
119 120 121
    }

    /**
122 123 124 125 126 127
     * Create a new {@code AlgorithmChecker} with the given
     * {@code AlgorithmConstraints}, {@code Timestamp}, and {@code String}
     * variant.
     *
     * Note that this constructor can initialize a variation of situations where
     * the AlgorithmConstraints, Timestamp, or Variant maybe known.
128 129
     *
     * @param constraints the algorithm constraints (or null)
130 131 132 133
     * @param jarTimestamp Timestamp passed for JAR timestamp constraint
     *                     checking. Set to null if not applicable.
     * @param variant is the Validator variants of the operation. A null value
     *                passed will set it to Validator.GENERIC.
X
xuelei 已提交
134
     */
135 136 137
    public AlgorithmChecker(AlgorithmConstraints constraints,
            Timestamp jarTimestamp, String variant) {
        this(null, constraints, null, jarTimestamp, variant);
X
xuelei 已提交
138 139 140
    }

    /**
141 142 143
     * Create a new {@code AlgorithmChecker} with the
     * given {@code TrustAnchor}, {@code AlgorithmConstraints},
     * {@code Timestamp}, and {@code String} variant.
144 145 146 147
     *
     * @param anchor the trust anchor selected to validate the target
     *     certificate
     * @param constraints the algorithm constraints (or null)
148 149 150 151 152 153 154
     * @param pkixdate The date specified by the PKIXParameters date.  If the
     *                 PKIXParameters is null, the current date is used.  This
     *                 should be null when jar files are being checked.
     * @param jarTimestamp Timestamp passed for JAR timestamp constraint
     *                     checking. Set to null if not applicable.
     * @param variant is the Validator variants of the operation. A null value
     *                passed will set it to Validator.GENERIC.
X
xuelei 已提交
155
     */
156
    public AlgorithmChecker(TrustAnchor anchor,
157 158
            AlgorithmConstraints constraints, Date pkixdate,
            Timestamp jarTimestamp, String variant) {
159

160 161 162 163 164 165 166 167 168 169
        if (anchor != null) {
            if (anchor.getTrustedCert() != null) {
                this.trustedPubKey = anchor.getTrustedCert().getPublicKey();
                // Check for anchor certificate restrictions
                trustedMatch = checkFingerprint(anchor.getTrustedCert());
                if (trustedMatch && debug != null) {
                    debug.println("trustedMatch = true");
                }
            } else {
                this.trustedPubKey = anchor.getCAPublicKey();
170
            }
171
        } else {
172 173 174 175
            this.trustedPubKey = null;
            if (debug != null) {
                debug.println("TrustAnchor is null, trustedMatch is false.");
            }
176 177
        }

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        this.prevPubKey = this.trustedPubKey;
        this.constraints = (constraints == null ? certPathDefaultConstraints :
                constraints);
        // If we are checking jar files, set pkixdate the same as the timestamp
        // for certificate checking
        this.pkixdate = (jarTimestamp != null ? jarTimestamp.getTimestamp() :
                pkixdate);
        this.jarTimestamp = jarTimestamp;
        this.variant = (variant == null ? Validator.VAR_GENERIC : variant);
    }

    /**
     * Create a new {@code AlgorithmChecker} with the given {@code TrustAnchor},
     * {@code PKIXParameter} date, and {@code varient}
     *
     * @param anchor the trust anchor selected to validate the target
     *     certificate
     * @param pkixdate Date the constraints are checked against. The value is
     *             either the PKIXParameters date or null for the current date.
     * @param variant is the Validator variants of the operation. A null value
     *                passed will set it to Validator.GENERIC.
     */
    public AlgorithmChecker(TrustAnchor anchor, Date pkixdate, String variant) {
        this(anchor, certPathDefaultConstraints, pkixdate, null, variant);
202 203
    }

204 205 206 207 208 209 210 211 212 213 214 215 216
    // Check this 'cert' for restrictions in the AnchorCertificates
    // trusted certificates list
    private static boolean checkFingerprint(X509Certificate cert) {
        if (!publicCALimits) {
            return false;
        }

        if (debug != null) {
            debug.println("AlgorithmChecker.contains: " + cert.getSigAlgName());
        }
        return AnchorCertificates.contains(cert);
    }

217
    @Override
X
xuelei 已提交
218
    public void init(boolean forward) throws CertPathValidatorException {
219 220 221 222 223 224 225 226 227 228 229
        //  Note that this class does not support forward mode.
        if (!forward) {
            if (trustedPubKey != null) {
                prevPubKey = trustedPubKey;
            } else {
                prevPubKey = null;
            }
        } else {
            throw new
                CertPathValidatorException("forward checking not supported");
        }
X
xuelei 已提交
230 231
    }

232
    @Override
X
xuelei 已提交
233
    public boolean isForwardCheckingSupported() {
234 235
        //  Note that as this class does not support forward mode, the method
        //  will always returns false.
X
xuelei 已提交
236 237 238
        return false;
    }

239
    @Override
X
xuelei 已提交
240 241 242 243
    public Set<String> getSupportedExtensions() {
        return null;
    }

244 245 246
    @Override
    public void check(Certificate cert,
            Collection<String> unresolvedCritExts)
X
xuelei 已提交
247 248
            throws CertPathValidatorException {

249 250 251 252 253 254
        if (!(cert instanceof X509Certificate) || constraints == null) {
            // ignore the check for non-x.509 certificate or null constraints
            return;
        }

        // check the key usage and key size
255
        boolean[] keyUsage = ((X509Certificate) cert).getKeyUsage();
256 257
        if (keyUsage != null && keyUsage.length < 9) {
            throw new CertPathValidatorException(
258 259
                "incorrect KeyUsage extension",
                null, null, -1, PKIXReason.INVALID_KEY_USAGE);
260 261
        }

262 263 264 265 266 267 268 269 270 271 272
        X509CertImpl x509Cert;
        AlgorithmId algorithmId;
        try {
            x509Cert = X509CertImpl.toImpl((X509Certificate)cert);
            algorithmId = (AlgorithmId)x509Cert.get(X509CertImpl.SIG_ALG);
        } catch (CertificateException ce) {
            throw new CertPathValidatorException(ce);
        }

        AlgorithmParameters currSigAlgParams = algorithmId.getParameters();
        PublicKey currPubKey = cert.getPublicKey();
273
        String currSigAlg = x509Cert.getSigAlgName();
274 275 276 277 278 279 280 281 282 283

        // Check the signature algorithm and parameters against constraints.
        if (!constraints.permits(SIGNATURE_PRIMITIVE_SET, currSigAlg,
                currSigAlgParams)) {
            throw new CertPathValidatorException(
                    "Algorithm constraints check failed on signature " +
                            "algorithm: " + currSigAlg, null, null, -1,
                    BasicReason.ALGORITHM_CONSTRAINED);
        }

I
igerasim 已提交
284 285 286
        // Assume all key usage bits are set if key usage is not present
        Set<CryptoPrimitive> primitives = KU_PRIMITIVE_SET;

287
        if (keyUsage != null) {
I
igerasim 已提交
288
                primitives = EnumSet.noneOf(CryptoPrimitive.class);
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

            if (keyUsage[0] || keyUsage[1] || keyUsage[5] || keyUsage[6]) {
                // keyUsage[0]: KeyUsage.digitalSignature
                // keyUsage[1]: KeyUsage.nonRepudiation
                // keyUsage[5]: KeyUsage.keyCertSign
                // keyUsage[6]: KeyUsage.cRLSign
                primitives.add(CryptoPrimitive.SIGNATURE);
            }

            if (keyUsage[2]) {      // KeyUsage.keyEncipherment
                primitives.add(CryptoPrimitive.KEY_ENCAPSULATION);
            }

            if (keyUsage[3]) {      // KeyUsage.dataEncipherment
                primitives.add(CryptoPrimitive.PUBLIC_KEY_ENCRYPTION);
            }

            if (keyUsage[4]) {      // KeyUsage.keyAgreement
                primitives.add(CryptoPrimitive.KEY_AGREEMENT);
            }

            // KeyUsage.encipherOnly and KeyUsage.decipherOnly are
            // undefined in the absence of the keyAgreement bit.

I
igerasim 已提交
313 314
            if (primitives.isEmpty()) {
                throw new CertPathValidatorException(
315
                    "incorrect KeyUsage extension bits",
I
igerasim 已提交
316
                    null, null, -1, PKIXReason.INVALID_KEY_USAGE);
317 318 319
            }
        }

320 321 322
        ConstraintsParameters cp =
                new ConstraintsParameters((X509Certificate)cert,
                        trustedMatch, pkixdate, jarTimestamp, variant);
323

324
        // Check against local constraints if it is DisabledAlgorithmConstraints
325
        if (constraints instanceof DisabledAlgorithmConstraints) {
326 327 328
            ((DisabledAlgorithmConstraints)constraints).permits(currSigAlg, cp);
            // DisabledAlgorithmsConstraints does not check primitives, so key
            // additional key check.
329

330 331 332 333 334
        } else {
            // Perform the default constraints checking anyway.
            certPathDefaultConstraints.permits(currSigAlg, cp);
            // Call locally set constraints to check key with primitives.
            if (!constraints.permits(primitives, currPubKey)) {
335
                throw new CertPathValidatorException(
336 337 338 339 340
                        "Algorithm constraints check failed on key " +
                                currPubKey.getAlgorithm() + " with size of " +
                                sun.security.util.KeyUtil.getKeySize(currPubKey) +
                                "bits",
                        null, null, -1, BasicReason.ALGORITHM_CONSTRAINED);
341
            }
I
igerasim 已提交
342
        }
343 344 345 346 347

        // If there is no previous key, set one and exit
        if (prevPubKey == null) {
            prevPubKey = currPubKey;
            return;
348
        }
I
igerasim 已提交
349

350
        // Check with previous cert for signature algorithm and public key
351 352 353 354
        if (!constraints.permits(
                SIGNATURE_PRIMITIVE_SET,
                currSigAlg, prevPubKey, currSigAlgParams)) {
            throw new CertPathValidatorException(
355 356
                    "Algorithm constraints check failed on " +
                            "signature algorithm: " + currSigAlg,
357 358
                    null, null, -1, BasicReason.ALGORITHM_CONSTRAINED);
        }
359

360 361 362 363 364
        // Inherit key parameters from previous key
        if (PKIX.isDSAPublicKeyWithoutParams(currPubKey)) {
            // Inherit DSA parameters from previous key
            if (!(prevPubKey instanceof DSAPublicKey)) {
                throw new CertPathValidatorException("Input key is not " +
365
                        "of a appropriate type for inheriting parameters");
366
            }
367

368 369 370
            DSAParams params = ((DSAPublicKey)prevPubKey).getParams();
            if (params == null) {
                throw new CertPathValidatorException(
371
                        "Key parameters missing from public key.");
372
            }
373

374 375 376 377 378 379 380 381
            try {
                BigInteger y = ((DSAPublicKey)currPubKey).getY();
                KeyFactory kf = KeyFactory.getInstance("DSA");
                DSAPublicKeySpec ks = new DSAPublicKeySpec(y, params.getP(),
                        params.getQ(), params.getG());
                currPubKey = kf.generatePublic(ks);
            } catch (GeneralSecurityException e) {
                throw new CertPathValidatorException("Unable to generate " +
382 383 384 385 386 387
                        "key with inherited parameters: " + e.getMessage(), e);
            }
        }

        // reset the previous public key
        prevPubKey = currPubKey;
X
xuelei 已提交
388 389
    }

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
    /**
     * Try to set the trust anchor of the checker.
     * <p>
     * If there is no trust anchor specified and the checker has not started,
     * set the trust anchor.
     *
     * @param anchor the trust anchor selected to validate the target
     *     certificate
     */
    void trySetTrustAnchor(TrustAnchor anchor) {
        // Don't bother if the check has started or trust anchor has already
        // specified.
        if (prevPubKey == null) {
            if (anchor == null) {
                throw new IllegalArgumentException(
                        "The trust anchor cannot be null");
            }

            // Don't bother to change the trustedPubKey.
            if (anchor.getTrustedCert() != null) {
                prevPubKey = anchor.getTrustedCert().getPublicKey();
411 412 413 414 415
                // Check for anchor certificate restrictions
                trustedMatch = checkFingerprint(anchor.getTrustedCert());
                if (trustedMatch && debug != null) {
                    debug.println("trustedMatch = true");
                }
416 417 418 419
            } else {
                prevPubKey = anchor.getCAPublicKey();
            }
        }
X
xuelei 已提交
420 421
    }

422 423 424 425 426
    /**
     * Check the signature algorithm with the specified public key.
     *
     * @param key the public key to verify the CRL signature
     * @param crl the target CRL
427 428
     * @param variant is the Validator variants of the operation. A null value
     *                passed will set it to Validator.GENERIC.
429
     */
430
    static void check(PublicKey key, X509CRL crl, String variant)
431
                        throws CertPathValidatorException {
X
xuelei 已提交
432

433 434 435 436 437 438
        X509CRLImpl x509CRLImpl = null;
        try {
            x509CRLImpl = X509CRLImpl.toImpl(crl);
        } catch (CRLException ce) {
            throw new CertPathValidatorException(ce);
        }
X
xuelei 已提交
439

440
        AlgorithmId algorithmId = x509CRLImpl.getSigAlgId();
441
        check(key, algorithmId, variant);
442 443 444 445 446 447
    }

    /**
     * Check the signature algorithm with the specified public key.
     *
     * @param key the public key to verify the CRL signature
448 449 450
     * @param algorithmId signature algorithm Algorithm ID
     * @param variant is the Validator variants of the operation. A null value
     *                passed will set it to Validator.GENERIC.
451
     */
452
    static void check(PublicKey key, AlgorithmId algorithmId, String variant)
453 454 455 456
                        throws CertPathValidatorException {
        String sigAlgName = algorithmId.getName();
        AlgorithmParameters sigAlgParams = algorithmId.getParameters();

457 458
        certPathDefaultConstraints.permits(new ConstraintsParameters(
                sigAlgName, sigAlgParams, key, variant));
X
xuelei 已提交
459 460
    }
}
461