jdk/src/java.base/share/classes/sun/security/provider/certpath/AlgorithmChecker.java
author ascarpino
Mon, 02 May 2016 16:45:38 -0700
changeset 37726 bbecfff95ec3
parent 33297 5970d160cbc0
child 38576 ccaac80108c5
permissions -rw-r--r--
8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions Reviewed-by: mullan, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
     1
/*
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
     2
 * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
     4
 *
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
227655c2ff8c 6861062: Disable MD2 support
xuelei
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: 4190
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4190
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    10
 *
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    15
 * accompanied this code).
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    16
 *
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4190
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4190
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4190
diff changeset
    23
 * questions.
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    24
 */
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    25
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    26
package sun.security.provider.certpath;
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    27
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    28
import java.security.AlgorithmConstraints;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    29
import java.security.CryptoPrimitive;
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    30
import java.util.Collection;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    31
import java.util.Collections;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    32
import java.util.Set;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    33
import java.util.EnumSet;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    34
import java.math.BigInteger;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    35
import java.security.PublicKey;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    36
import java.security.KeyFactory;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    37
import java.security.AlgorithmParameters;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    38
import java.security.GeneralSecurityException;
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    39
import java.security.cert.Certificate;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    40
import java.security.cert.X509CRL;
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    41
import java.security.cert.X509Certificate;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    42
import java.security.cert.PKIXCertPathChecker;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    43
import java.security.cert.TrustAnchor;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    44
import java.security.cert.CRLException;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    45
import java.security.cert.CertificateException;
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    46
import java.security.cert.CertPathValidatorException;
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
    47
import java.security.cert.CertPathValidatorException.BasicReason;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
    48
import java.security.cert.PKIXReason;
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
    49
import java.security.interfaces.DSAParams;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
    50
import java.security.interfaces.DSAPublicKey;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
    51
import java.security.spec.DSAPublicKeySpec;
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    52
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
    53
import sun.security.util.AnchorCertificates;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
    54
import sun.security.util.CertConstraintParameters;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
    55
import sun.security.util.Debug;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    56
import sun.security.util.DisabledAlgorithmConstraints;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    57
import sun.security.x509.X509CertImpl;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    58
import sun.security.x509.X509CRLImpl;
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    59
import sun.security.x509.AlgorithmId;
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    60
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    61
/**
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    62
 * A <code>PKIXCertPathChecker</code> implementation to check whether a
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    63
 * specified certificate contains the required algorithm constraints.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    64
 * <p>
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    65
 * Certificate fields such as the subject public key, the signature
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    66
 * algorithm, key usage, extended key usage, etc. need to conform to
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    67
 * the specified algorithm constraints.
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    68
 *
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    69
 * @see PKIXCertPathChecker
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    70
 * @see PKIXParameters
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    71
 */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
    72
public final class AlgorithmChecker extends PKIXCertPathChecker {
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
    73
    private static final Debug debug = Debug.getInstance("certpath");
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    74
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    75
    private final AlgorithmConstraints constraints;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    76
    private final PublicKey trustedPubKey;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    77
    private PublicKey prevPubKey;
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    78
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
    79
    private static final Set<CryptoPrimitive> SIGNATURE_PRIMITIVE_SET =
23897
911b1eb93667 8029745: Enhance algorithm checking
juh
parents: 13806
diff changeset
    80
        Collections.unmodifiableSet(EnumSet.of(CryptoPrimitive.SIGNATURE));
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    81
33297
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
    82
    private static final Set<CryptoPrimitive> KU_PRIMITIVE_SET =
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
    83
        Collections.unmodifiableSet(EnumSet.of(
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
    84
            CryptoPrimitive.SIGNATURE,
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
    85
            CryptoPrimitive.KEY_ENCAPSULATION,
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
    86
            CryptoPrimitive.PUBLIC_KEY_ENCRYPTION,
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
    87
            CryptoPrimitive.KEY_AGREEMENT));
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
    88
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
    89
    private static final DisabledAlgorithmConstraints
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    90
        certPathDefaultConstraints = new DisabledAlgorithmConstraints(
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    91
            DisabledAlgorithmConstraints.PROPERTY_CERTPATH_DISABLED_ALGS);
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
    92
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
    93
    // If there is no "cacerts" keyword, then disable anchor checking
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
    94
    private static final boolean publicCALimits =
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
    95
            certPathDefaultConstraints.checkProperty("jdkCA");
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
    96
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
    97
    // If anchor checking enabled, this will be true if the trust anchor
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
    98
    // has a match in the cacerts file
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
    99
    private boolean trustedMatch = false;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   100
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   101
    /**
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   102
     * Create a new <code>AlgorithmChecker</code> with the algorithm
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   103
     * constraints specified in security property
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   104
     * "jdk.certpath.disabledAlgorithms".
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   105
     *
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   106
     * @param anchor the trust anchor selected to validate the target
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   107
     *     certificate
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   108
     */
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   109
    public AlgorithmChecker(TrustAnchor anchor) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   110
        this(anchor, certPathDefaultConstraints);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   111
    }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   112
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   113
    /**
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   114
     * Create a new <code>AlgorithmChecker</code> with the
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   115
     * given {@code AlgorithmConstraints}.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   116
     * <p>
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   117
     * Note that this constructor will be used to check a certification
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   118
     * path where the trust anchor is unknown, or a certificate list which may
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   119
     * contain the trust anchor. This constructor is used by SunJSSE.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   120
     *
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   121
     * @param constraints the algorithm constraints (or null)
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   122
     */
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   123
    public AlgorithmChecker(AlgorithmConstraints constraints) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   124
        this.prevPubKey = null;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   125
        this.trustedPubKey = null;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   126
        this.constraints = constraints;
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   127
    }
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   128
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   129
    /**
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   130
     * Create a new <code>AlgorithmChecker</code> with the
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   131
     * given <code>TrustAnchor</code> and <code>AlgorithmConstraints</code>.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   132
     *
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   133
     * @param anchor the trust anchor selected to validate the target
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   134
     *     certificate
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   135
     * @param constraints the algorithm constraints (or null)
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   136
     *
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   137
     * @throws IllegalArgumentException if the <code>anchor</code> is null
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   138
     */
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   139
    public AlgorithmChecker(TrustAnchor anchor,
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   140
            AlgorithmConstraints constraints) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   141
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   142
        if (anchor == null) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   143
            throw new IllegalArgumentException(
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   144
                        "The trust anchor cannot be null");
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   145
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   146
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   147
        if (anchor.getTrustedCert() != null) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   148
            this.trustedPubKey = anchor.getTrustedCert().getPublicKey();
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   149
            // Check for anchor certificate restrictions
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   150
            trustedMatch = checkFingerprint(anchor.getTrustedCert());
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   151
            if (trustedMatch && debug != null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   152
                debug.println("trustedMatch = true");
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   153
            }
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   154
        } else {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   155
            this.trustedPubKey = anchor.getCAPublicKey();
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   156
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   157
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   158
        this.prevPubKey = trustedPubKey;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   159
        this.constraints = constraints;
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   160
    }
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   161
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   162
    // Check this 'cert' for restrictions in the AnchorCertificates
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   163
    // trusted certificates list
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   164
    private static boolean checkFingerprint(X509Certificate cert) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   165
        if (!publicCALimits) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   166
            return false;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   167
        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   168
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   169
        if (debug != null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   170
            debug.println("AlgorithmChecker.contains: " + cert.getSigAlgName());
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   171
        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   172
        return AnchorCertificates.contains(cert);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   173
    }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   174
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   175
    @Override
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   176
    public void init(boolean forward) throws CertPathValidatorException {
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   177
        //  Note that this class does not support forward mode.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   178
        if (!forward) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   179
            if (trustedPubKey != null) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   180
                prevPubKey = trustedPubKey;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   181
            } else {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   182
                prevPubKey = null;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   183
            }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   184
        } else {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   185
            throw new
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   186
                CertPathValidatorException("forward checking not supported");
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   187
        }
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   188
    }
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   189
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   190
    @Override
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   191
    public boolean isForwardCheckingSupported() {
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   192
        //  Note that as this class does not support forward mode, the method
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   193
        //  will always returns false.
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   194
        return false;
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   195
    }
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   196
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   197
    @Override
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   198
    public Set<String> getSupportedExtensions() {
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   199
        return null;
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   200
    }
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   201
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   202
    @Override
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   203
    public void check(Certificate cert,
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   204
            Collection<String> unresolvedCritExts)
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   205
            throws CertPathValidatorException {
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   206
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   207
        if (!(cert instanceof X509Certificate) || constraints == null) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   208
            // ignore the check for non-x.509 certificate or null constraints
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   209
            return;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   210
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   211
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   212
        // check the key usage and key size
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   213
        boolean[] keyUsage = ((X509Certificate) cert).getKeyUsage();
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   214
        if (keyUsage != null && keyUsage.length < 9) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   215
            throw new CertPathValidatorException(
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   216
                "incorrect KeyUsage extension",
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   217
                null, null, -1, PKIXReason.INVALID_KEY_USAGE);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   218
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   219
33297
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
   220
        // Assume all key usage bits are set if key usage is not present
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
   221
        Set<CryptoPrimitive> primitives = KU_PRIMITIVE_SET;
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
   222
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   223
        if (keyUsage != null) {
33297
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
   224
                primitives = EnumSet.noneOf(CryptoPrimitive.class);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   225
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   226
            if (keyUsage[0] || keyUsage[1] || keyUsage[5] || keyUsage[6]) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   227
                // keyUsage[0]: KeyUsage.digitalSignature
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   228
                // keyUsage[1]: KeyUsage.nonRepudiation
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   229
                // keyUsage[5]: KeyUsage.keyCertSign
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   230
                // keyUsage[6]: KeyUsage.cRLSign
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   231
                primitives.add(CryptoPrimitive.SIGNATURE);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   232
            }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   233
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   234
            if (keyUsage[2]) {      // KeyUsage.keyEncipherment
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   235
                primitives.add(CryptoPrimitive.KEY_ENCAPSULATION);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   236
            }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   237
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   238
            if (keyUsage[3]) {      // KeyUsage.dataEncipherment
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   239
                primitives.add(CryptoPrimitive.PUBLIC_KEY_ENCRYPTION);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   240
            }
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   241
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   242
            if (keyUsage[4]) {      // KeyUsage.keyAgreement
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   243
                primitives.add(CryptoPrimitive.KEY_AGREEMENT);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   244
            }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   245
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   246
            // KeyUsage.encipherOnly and KeyUsage.decipherOnly are
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   247
            // undefined in the absence of the keyAgreement bit.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   248
33297
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
   249
            if (primitives.isEmpty()) {
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
   250
                throw new CertPathValidatorException(
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   251
                    "incorrect KeyUsage extension bits",
33297
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
   252
                    null, null, -1, PKIXReason.INVALID_KEY_USAGE);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   253
            }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   254
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   255
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   256
        PublicKey currPubKey = cert.getPublicKey();
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   257
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   258
        // Check against DisabledAlgorithmConstraints certpath constraints.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   259
        // permits() will throw exception on failure.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   260
        certPathDefaultConstraints.permits(primitives,
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   261
                new CertConstraintParameters((X509Certificate)cert,
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   262
                        trustedMatch));
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   263
                // new CertConstraintParameters(x509Cert, trustedMatch));
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   264
        // If there is no previous key, set one and exit
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   265
        if (prevPubKey == null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   266
            prevPubKey = currPubKey;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   267
            return;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   268
        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   269
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   270
        X509CertImpl x509Cert;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   271
        AlgorithmId algorithmId;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   272
        try {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   273
            x509Cert = X509CertImpl.toImpl((X509Certificate)cert);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   274
            algorithmId = (AlgorithmId)x509Cert.get(X509CertImpl.SIG_ALG);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   275
        } catch (CertificateException ce) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   276
            throw new CertPathValidatorException(ce);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   277
        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   278
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   279
        AlgorithmParameters currSigAlgParams = algorithmId.getParameters();
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   280
        String currSigAlg = x509Cert.getSigAlgName();
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   281
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   282
        // If 'constraints' is not of DisabledAlgorithmConstraints, check all
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   283
        // everything individually
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   284
        if (!(constraints instanceof DisabledAlgorithmConstraints)) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   285
            // Check the current signature algorithm
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   286
            if (!constraints.permits(
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   287
                    SIGNATURE_PRIMITIVE_SET,
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   288
                    currSigAlg, currSigAlgParams)) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   289
                throw new CertPathValidatorException(
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   290
                        "Algorithm constraints check failed on signature " +
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   291
                                "algorithm: " + currSigAlg, null, null, -1,
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   292
                        BasicReason.ALGORITHM_CONSTRAINED);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   293
            }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   294
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   295
            if (!constraints.permits(primitives, currPubKey)) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   296
                throw new CertPathValidatorException(
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   297
                        "Algorithm constraints check failed on keysize: " +
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   298
                                sun.security.util.KeyUtil.getKeySize(currPubKey),
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   299
                        null, null, -1, BasicReason.ALGORITHM_CONSTRAINED);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   300
            }
33297
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
   301
        }
5970d160cbc0 8131291: Perfect parameter patterning
vinnie
parents: 32649
diff changeset
   302
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   303
        // Check with previous cert for signature algorithm and public key
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   304
        if (prevPubKey != null) {
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   305
            if (!constraints.permits(
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   306
                    SIGNATURE_PRIMITIVE_SET,
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   307
                    currSigAlg, prevPubKey, currSigAlgParams)) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   308
                throw new CertPathValidatorException(
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   309
                    "Algorithm constraints check failed on " +
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   310
                            "signature algorithm: " + currSigAlg,
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   311
                    null, null, -1, BasicReason.ALGORITHM_CONSTRAINED);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   312
            }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   313
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   314
            // Inherit key parameters from previous key
13806
b18118646a65 7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents: 8163
diff changeset
   315
            if (PKIX.isDSAPublicKeyWithoutParams(currPubKey)) {
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   316
                // Inherit DSA parameters from previous key
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   317
                if (!(prevPubKey instanceof DSAPublicKey)) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   318
                    throw new CertPathValidatorException("Input key is not " +
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   319
                        "of a appropriate type for inheriting parameters");
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   320
                }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   321
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   322
                DSAParams params = ((DSAPublicKey)prevPubKey).getParams();
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   323
                if (params == null) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   324
                    throw new CertPathValidatorException(
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   325
                        "Key parameters missing from public key.");
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   326
                }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   327
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   328
                try {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   329
                    BigInteger y = ((DSAPublicKey)currPubKey).getY();
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   330
                    KeyFactory kf = KeyFactory.getInstance("DSA");
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   331
                    DSAPublicKeySpec ks = new DSAPublicKeySpec(y,
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   332
                                                       params.getP(),
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   333
                                                       params.getQ(),
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   334
                                                       params.getG());
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   335
                    currPubKey = kf.generatePublic(ks);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   336
                } catch (GeneralSecurityException e) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   337
                    throw new CertPathValidatorException("Unable to generate " +
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   338
                        "key with inherited parameters: " + e.getMessage(), e);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   339
                }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   340
            }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   341
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   342
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   343
        // reset the previous public key
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   344
        prevPubKey = currPubKey;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   345
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   346
        // check the extended key usage, ignore the check now
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   347
        // List<String> extendedKeyUsages = x509Cert.getExtendedKeyUsage();
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   348
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   349
        // DO NOT remove any unresolved critical extensions
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   350
    }
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   351
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   352
    /**
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   353
     * Try to set the trust anchor of the checker.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   354
     * <p>
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   355
     * If there is no trust anchor specified and the checker has not started,
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   356
     * set the trust anchor.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   357
     *
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   358
     * @param anchor the trust anchor selected to validate the target
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   359
     *     certificate
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   360
     */
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   361
    void trySetTrustAnchor(TrustAnchor anchor) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   362
        // Don't bother if the check has started or trust anchor has already
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   363
        // specified.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   364
        if (prevPubKey == null) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   365
            if (anchor == null) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   366
                throw new IllegalArgumentException(
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   367
                        "The trust anchor cannot be null");
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   368
            }
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   369
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   370
            // Don't bother to change the trustedPubKey.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   371
            if (anchor.getTrustedCert() != null) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   372
                prevPubKey = anchor.getTrustedCert().getPublicKey();
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   373
                // Check for anchor certificate restrictions
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   374
                trustedMatch = checkFingerprint(anchor.getTrustedCert());
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   375
                if (trustedMatch && debug != null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   376
                    debug.println("trustedMatch = true");
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   377
                }
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   378
            } else {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   379
                prevPubKey = anchor.getCAPublicKey();
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   380
            }
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   381
        }
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   382
    }
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   383
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   384
    /**
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   385
     * Check the signature algorithm with the specified public key.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   386
     *
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   387
     * @param key the public key to verify the CRL signature
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   388
     * @param crl the target CRL
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   389
     */
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   390
    static void check(PublicKey key, X509CRL crl)
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   391
                        throws CertPathValidatorException {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   392
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   393
        X509CRLImpl x509CRLImpl = null;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   394
        try {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   395
            x509CRLImpl = X509CRLImpl.toImpl(crl);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   396
        } catch (CRLException ce) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   397
            throw new CertPathValidatorException(ce);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   398
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   399
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   400
        AlgorithmId algorithmId = x509CRLImpl.getSigAlgId();
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   401
        check(key, algorithmId);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   402
    }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   403
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   404
    /**
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   405
     * Check the signature algorithm with the specified public key.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   406
     *
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   407
     * @param key the public key to verify the CRL signature
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   408
     * @param crl the target CRL
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   409
     */
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   410
    static void check(PublicKey key, AlgorithmId algorithmId)
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   411
                        throws CertPathValidatorException {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   412
        String sigAlgName = algorithmId.getName();
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   413
        AlgorithmParameters sigAlgParams = algorithmId.getParameters();
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   414
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   415
        if (!certPathDefaultConstraints.permits(
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   416
                SIGNATURE_PRIMITIVE_SET, sigAlgName, key, sigAlgParams)) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   417
            throw new CertPathValidatorException(
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   418
                "Algorithm constraints check failed on signature algorithm: " +
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 33297
diff changeset
   419
                sigAlgName + " is disabled",
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   420
                null, null, -1, BasicReason.ALGORITHM_CONSTRAINED);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   421
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   422
    }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   423
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents:
diff changeset
   424
}
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   425