jdk/src/java.base/share/classes/sun/security/util/DisabledAlgorithmConstraints.java
author ascarpino
Thu, 21 Jul 2016 10:33:56 -0700
changeset 41580 cc479488428c
parent 41562 1e040ccac110
child 41956 69deb06bb8f1
permissions -rw-r--r--
8155973: Tighten jar checks Reviewed-by: mullan, igerasim, ahgross
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
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: 32649
diff changeset
     2
 * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
     4
 *
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    10
 *
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    15
 * accompanied this code).
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    16
 *
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    20
 *
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    23
 * questions.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    24
 */
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    25
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    26
package sun.security.util;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    27
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    28
import java.security.CryptoPrimitive;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    29
import java.security.AlgorithmParameters;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    30
import java.security.Key;
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
    31
import java.security.cert.CertPathValidatorException;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
    32
import java.security.cert.CertPathValidatorException.BasicReason;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
    33
import java.security.cert.X509Certificate;
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
    34
import java.text.SimpleDateFormat;
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
    35
import java.util.Calendar;
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
    36
import java.util.Date;
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
    37
import java.util.HashMap;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
    38
import java.util.HashSet;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    39
import java.util.Locale;
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
    40
import java.util.Map;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    41
import java.util.Set;
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
    42
import java.util.TimeZone;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    43
import java.util.regex.Pattern;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    44
import java.util.regex.Matcher;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    45
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    46
/**
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    47
 * Algorithm constraints for disabled algorithms property
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    48
 *
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    49
 * See the "jdk.certpath.disabledAlgorithms" specification in java.security
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    50
 * for the syntax of the disabled algorithm string.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    51
 */
31689
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 28974
diff changeset
    52
public class DisabledAlgorithmConstraints extends AbstractAlgorithmConstraints {
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
    53
    private static final Debug debug = Debug.getInstance("certpath");
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    54
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    55
    // the known security property, jdk.certpath.disabledAlgorithms
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31689
diff changeset
    56
    public static final String PROPERTY_CERTPATH_DISABLED_ALGS =
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    57
            "jdk.certpath.disabledAlgorithms";
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    58
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    59
    // the known security property, jdk.tls.disabledAlgorithms
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31689
diff changeset
    60
    public static final String PROPERTY_TLS_DISABLED_ALGS =
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    61
            "jdk.tls.disabledAlgorithms";
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    62
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 41562
diff changeset
    63
    // the known security property, jdk.jar.disabledAlgorithms
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 41562
diff changeset
    64
    public static final String PROPERTY_JAR_DISABLED_ALGS =
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 41562
diff changeset
    65
            "jdk.jar.disabledAlgorithms";
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 41562
diff changeset
    66
31689
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 28974
diff changeset
    67
    private final String[] disabledAlgorithms;
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
    68
    private final Constraints algorithmConstraints;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    69
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    70
    /**
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    71
     * Initialize algorithm constraints with the specified security property.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    72
     *
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    73
     * @param propertyName the security property name that define the disabled
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    74
     *        algorithm constraints
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    75
     */
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    76
    public DisabledAlgorithmConstraints(String propertyName) {
31689
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 28974
diff changeset
    77
        this(propertyName, new AlgorithmDecomposer());
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 28974
diff changeset
    78
    }
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    79
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 41562
diff changeset
    80
    /**
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 41562
diff changeset
    81
     * Initialize algorithm constraints with the specified security property
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 41562
diff changeset
    82
     * for a specific usage type.
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 41562
diff changeset
    83
     *
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 41562
diff changeset
    84
     * @param propertyName the security property name that define the disabled
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 41562
diff changeset
    85
     *        algorithm constraints
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 41562
diff changeset
    86
     * @param decomposer an alternate AlgorithmDecomposer.
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 41562
diff changeset
    87
     */
31689
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 28974
diff changeset
    88
    public DisabledAlgorithmConstraints(String propertyName,
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 28974
diff changeset
    89
            AlgorithmDecomposer decomposer) {
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 28974
diff changeset
    90
        super(decomposer);
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
    91
        disabledAlgorithms = getAlgorithms(propertyName);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
    92
        algorithmConstraints = new Constraints(disabledAlgorithms);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    93
    }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    94
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
    95
    /*
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
    96
     * This only checks if the algorithm has been completely disabled.  If
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
    97
     * there are keysize or other limit, this method allow the algorithm.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
    98
     */
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
    99
    @Override
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31689
diff changeset
   100
    public final boolean permits(Set<CryptoPrimitive> primitives,
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   101
            String algorithm, AlgorithmParameters parameters) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   102
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   103
        if (primitives == null || primitives.isEmpty()) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   104
            throw new IllegalArgumentException(
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   105
                        "No cryptographic primitive specified");
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   106
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   107
31689
1201792aa3a3 8043201: Deprecate RC4 in SunJSSE provider
asmotrak
parents: 28974
diff changeset
   108
        return checkAlgorithm(disabledAlgorithms, algorithm, decomposer);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   109
    }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   110
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   111
    /*
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   112
     * Checks if the key algorithm has been disabled or constraints have been
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   113
     * placed on the key.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   114
     */
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   115
    @Override
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31689
diff changeset
   116
    public final boolean permits(Set<CryptoPrimitive> primitives, Key key) {
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   117
        return checkConstraints(primitives, "", key, null);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   118
    }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   119
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   120
    /*
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   121
     * Checks if the key algorithm has been disabled or if constraints have
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   122
     * been placed on the key.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   123
     */
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   124
    @Override
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31689
diff changeset
   125
    public final boolean permits(Set<CryptoPrimitive> primitives,
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   126
            String algorithm, Key key, AlgorithmParameters parameters) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   127
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   128
        if (algorithm == null || algorithm.length() == 0) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   129
            throw new IllegalArgumentException("No algorithm name specified");
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   130
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   131
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   132
        return checkConstraints(primitives, algorithm, key, parameters);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   133
    }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   134
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   135
    /*
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   136
     * Check if a x509Certificate object is permitted.  Check if all
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   137
     * algorithms are allowed, certificate constraints, and the
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   138
     * public key against key constraints.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   139
     *
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   140
     * Uses new style permit() which throws exceptions.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   141
     */
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   142
    public final void permits(Set<CryptoPrimitive> primitives,
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   143
            CertConstraintParameters cp) throws CertPathValidatorException {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   144
        checkConstraints(primitives, cp);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   145
    }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   146
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   147
    /*
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   148
     * Check if Certificate object is within the constraints.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   149
     * Uses new style permit() which throws exceptions.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   150
     */
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   151
    public final void permits(Set<CryptoPrimitive> primitives,
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   152
            X509Certificate cert) throws CertPathValidatorException {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   153
        checkConstraints(primitives, new CertConstraintParameters(cert));
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   154
    }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   155
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   156
    // Check if a string is contained inside the property
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   157
    public boolean checkProperty(String param) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   158
        param = param.toLowerCase(Locale.ENGLISH);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   159
        for (String block : disabledAlgorithms) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   160
            if (block.toLowerCase(Locale.ENGLISH).indexOf(param) >= 0) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   161
                return true;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   162
            }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   163
        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   164
        return false;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   165
    }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   166
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   167
    // Check algorithm constraints with key and algorithm
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   168
    private boolean checkConstraints(Set<CryptoPrimitive> primitives,
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   169
            String algorithm, Key key, AlgorithmParameters parameters) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   170
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   171
        // check the key parameter, it cannot be null.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   172
        if (key == null) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   173
            throw new IllegalArgumentException("The key cannot be null");
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   174
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   175
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   176
        // check the signature algorithm
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   177
        if (algorithm != null && algorithm.length() != 0) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   178
            if (!permits(primitives, algorithm, parameters)) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   179
                return false;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   180
            }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   181
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   182
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   183
        // check the key algorithm
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   184
        if (!permits(primitives, key.getAlgorithm(), null)) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   185
            return false;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   186
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   187
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   188
        // check the key constraints
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   189
        return algorithmConstraints.permits(key);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   190
    }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   191
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   192
    /*
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   193
     * Check algorithm constraints with Certificate
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   194
     * Uses new style permit() which throws exceptions.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   195
     */
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   196
    private void checkConstraints(Set<CryptoPrimitive> primitives,
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   197
            CertConstraintParameters cp) throws CertPathValidatorException {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   198
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   199
        X509Certificate cert = cp.getCertificate();
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   200
        String algorithm = cert.getSigAlgName();
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   201
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   202
        // Check signature algorithm is not disabled
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   203
        if (!permits(primitives, algorithm, null)) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   204
            throw new CertPathValidatorException(
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   205
                    "Algorithm constraints check failed on disabled "+
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   206
                            "signature algorithm: " + algorithm,
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   207
                    null, null, -1, BasicReason.ALGORITHM_CONSTRAINED);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   208
        }
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   209
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   210
        // Check key algorithm is not disabled
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   211
        if (!permits(primitives, cert.getPublicKey().getAlgorithm(), null)) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   212
            throw new CertPathValidatorException(
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   213
                    "Algorithm constraints check failed on disabled "+
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   214
                            "public key algorithm: " + algorithm,
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   215
                    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: 32649
diff changeset
   216
        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   217
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   218
        // Check the certificate and key constraints
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   219
        algorithmConstraints.permits(cp);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   220
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   221
    }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   222
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   223
    /**
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   224
     * Key and Certificate Constraints
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   225
     *
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   226
     * The complete disabling of an algorithm is not handled by Constraints or
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   227
     * Constraint classes.  That is addressed with
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   228
     *   permit(Set<CryptoPrimitive>, String, AlgorithmParameters)
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   229
     *
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   230
     * When passing a Key to permit(), the boolean return values follow the
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   231
     * same as the interface class AlgorithmConstraints.permit().  This is to
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   232
     * maintain compatibility:
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   233
     * 'true' means the operation is allowed.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   234
     * 'false' means it failed the constraints and is disallowed.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   235
     *
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   236
     * When passing CertConstraintParameters through permit(), an exception
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   237
     * will be thrown on a failure to better identify why the operation was
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   238
     * disallowed.
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   239
     */
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   240
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   241
    private static class Constraints {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   242
        private Map<String, Set<Constraint>> constraintsMap = new HashMap<>();
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   243
        private static final Pattern keySizePattern = Pattern.compile(
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   244
                "keySize\\s*(<=|<|==|!=|>|>=)\\s*(\\d+)");
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   245
        private static final Pattern denyAfterPattern = Pattern.compile(
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   246
                "denyAfter\\s+(\\d{4})-(\\d{2})-(\\d{2})");
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   247
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   248
        public Constraints(String[] constraintArray) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   249
            for (String constraintEntry : constraintArray) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   250
                if (constraintEntry == null || constraintEntry.isEmpty()) {
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   251
                    continue;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   252
                }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   253
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   254
                constraintEntry = constraintEntry.trim();
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   255
                if (debug != null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   256
                    debug.println("Constraints: " + constraintEntry);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   257
                }
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   258
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   259
                // Check if constraint is a complete disabling of an
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   260
                // algorithm or has conditions.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   261
                String algorithm;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   262
                String policy;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   263
                int space = constraintEntry.indexOf(' ');
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   264
                if (space > 0) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   265
                    algorithm = AlgorithmDecomposer.hashName(
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   266
                            constraintEntry.substring(0, space).
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   267
                                    toUpperCase(Locale.ENGLISH));
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   268
                    policy = constraintEntry.substring(space + 1);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   269
                } else {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   270
                    constraintsMap.computeIfAbsent(
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   271
                            constraintEntry.toUpperCase(Locale.ENGLISH),
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   272
                            k -> new HashSet<>());
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   273
                    continue;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   274
                }
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   275
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   276
                // Convert constraint conditions into Constraint classes
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   277
                Constraint c, lastConstraint = null;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   278
                // Allow only one jdkCA entry per constraint entry
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   279
                boolean jdkCALimit = false;
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   280
                // Allow only one denyAfter entry per constraint entry
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   281
                boolean denyAfterLimit = false;
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   282
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   283
                for (String entry : policy.split("&")) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   284
                    entry = entry.trim();
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   285
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   286
                    Matcher matcher = keySizePattern.matcher(entry);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   287
                    if (matcher.matches()) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   288
                        if (debug != null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   289
                            debug.println("Constraints set to keySize: " +
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   290
                                    entry);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   291
                        }
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   292
                        c = new KeySizeConstraint(algorithm,
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   293
                                KeySizeConstraint.Operator.of(matcher.group(1)),
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   294
                                Integer.parseInt(matcher.group(2)));
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   295
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   296
                    } else if (entry.equalsIgnoreCase("jdkCA")) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   297
                        if (debug != null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   298
                            debug.println("Constraints set to jdkCA.");
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   299
                        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   300
                        if (jdkCALimit) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   301
                            throw new IllegalArgumentException("Only one " +
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   302
                                    "jdkCA entry allowed in property. " +
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   303
                                    "Constraint: " + constraintEntry);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   304
                        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   305
                        c = new jdkCAConstraint(algorithm);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   306
                        jdkCALimit = true;
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   307
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   308
                    } else if(matcher.usePattern(denyAfterPattern).matches()) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   309
                        if (debug != null) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   310
                            debug.println("Constraints set to denyAfter");
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   311
                        }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   312
                        if (denyAfterLimit) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   313
                            throw new IllegalArgumentException("Only one " +
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   314
                                    "denyAfter entry allowed in property. " +
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   315
                                    "Constraint: " + constraintEntry);
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   316
                        }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   317
                        int year = Integer.parseInt(matcher.group(1));
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   318
                        int month = Integer.parseInt(matcher.group(2));
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   319
                        int day = Integer.parseInt(matcher.group(3));
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   320
                        c = new DenyAfterConstraint(algorithm, year, month,
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   321
                                day);
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   322
                        denyAfterLimit = true;
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   323
                    } else {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   324
                        throw new IllegalArgumentException("Error in security" +
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   325
                                " property. Constraint unknown: " + entry);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   326
                    }
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   327
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   328
                    // Link multiple conditions for a single constraint
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   329
                    // into a linked list.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   330
                    if (lastConstraint == null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   331
                        if (!constraintsMap.containsKey(algorithm)) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   332
                            constraintsMap.putIfAbsent(algorithm,
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   333
                                    new HashSet<>());
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   334
                        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   335
                        constraintsMap.get(algorithm).add(c);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   336
                    } else {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   337
                        lastConstraint.nextConstraint = c;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   338
                    }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   339
                    lastConstraint = c;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   340
                }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   341
            }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   342
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   343
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   344
        // Get applicable constraints based off the signature algorithm
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   345
        private Set<Constraint> getConstraints(String algorithm) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   346
            return constraintsMap.get(algorithm);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   347
        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   348
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   349
        // Check if KeySizeConstraints permit the specified key
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   350
        public boolean permits(Key key) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   351
            Set<Constraint> set = getConstraints(key.getAlgorithm());
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   352
            if (set == null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   353
                return true;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   354
            }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   355
            for (Constraint constraint : set) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   356
                if (!constraint.permits(key)) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   357
                    if (debug != null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   358
                        debug.println("keySizeConstraint: failed key " +
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   359
                                "constraint check " + KeyUtil.getKeySize(key));
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   360
                    }
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   361
                    return false;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   362
                }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   363
            }
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   364
            return true;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   365
        }
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   366
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   367
        // Check if constraints permit this cert.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   368
        public void permits(CertConstraintParameters cp)
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   369
                throws CertPathValidatorException {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   370
            X509Certificate cert = cp.getCertificate();
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   371
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   372
            if (debug != null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   373
                debug.println("Constraints.permits(): " + cert.getSigAlgName());
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   374
            }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   375
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   376
            // Get all signature algorithms to check for constraints
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   377
            Set<String> algorithms =
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   378
                    AlgorithmDecomposer.decomposeOneHash(cert.getSigAlgName());
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   379
            if (algorithms == null || algorithms.isEmpty()) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   380
                return;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   381
            }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   382
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   383
            // Attempt to add the public key algorithm to the set
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   384
            algorithms.add(cert.getPublicKey().getAlgorithm());
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   385
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   386
            // Check all applicable constraints
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   387
            for (String algorithm : algorithms) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   388
                Set<Constraint> set = getConstraints(algorithm);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   389
                if (set == null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   390
                    continue;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   391
                }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   392
                for (Constraint constraint : set) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   393
                    constraint.permits(cp);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   394
                }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   395
            }
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   396
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   397
    }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   398
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   399
    /**
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   400
     * This abstract Constraint class for algorithm-based checking
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   401
     * may contain one or more constraints.  If the '&' on the {@Security}
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   402
     * property is used, multiple constraints have been grouped together
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   403
     * requiring all the constraints to fail for the check to be disallowed.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   404
     *
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   405
     * If the class contains multiple constraints, the next constraint
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   406
     * is stored in {@code nextConstraint} in linked-list fashion.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   407
     */
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   408
    private abstract static class Constraint {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   409
        String algorithm;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   410
        Constraint nextConstraint = null;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   411
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   412
        // operator
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   413
        enum Operator {
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   414
            EQ,         // "=="
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   415
            NE,         // "!="
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   416
            LT,         // "<"
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   417
            LE,         // "<="
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   418
            GT,         // ">"
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   419
            GE;         // ">="
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   420
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   421
            static Operator of(String s) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   422
                switch (s) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   423
                    case "==":
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   424
                        return EQ;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   425
                    case "!=":
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   426
                        return NE;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   427
                    case "<":
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   428
                        return LT;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   429
                    case "<=":
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   430
                        return LE;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   431
                    case ">":
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   432
                        return GT;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   433
                    case ">=":
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   434
                        return GE;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   435
                }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   436
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   437
                throw new IllegalArgumentException("Error in security " +
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   438
                        "property. " + s + " is not a legal Operator");
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   439
            }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   440
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   441
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   442
        /**
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   443
         * Check if an algorithm constraint is permitted with a given key.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   444
         *
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   445
         * If the check inside of {@code permit()} fails, it must call
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   446
         * {@code next()} with the same {@code Key} parameter passed if
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   447
         * multiple constraints need to be checked.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   448
         *
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   449
         * @param key Public key
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   450
         * @return 'true' if constraint is allowed, 'false' if disallowed.
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   451
         */
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   452
        public boolean permits(Key key) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   453
            return true;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   454
        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   455
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   456
        /**
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   457
         * Check if an algorithm constraint is permitted with a given
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   458
         * CertConstraintParameters.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   459
         *
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   460
         * If the check inside of {@code permits()} fails, it must call
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   461
         * {@code next()} with the same {@code CertConstraintParameters}
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   462
         * parameter passed if multiple constraints need to be checked.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   463
         *
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   464
         * @param cp CertConstraintParameter containing certificate info
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   465
         * @throws CertPathValidatorException if constraint disallows.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   466
         *
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   467
         */
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   468
        public abstract void permits(CertConstraintParameters cp)
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   469
                throws CertPathValidatorException;
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   470
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   471
        /**
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   472
         * Recursively check if the constraints are allowed.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   473
         *
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   474
         * If {@code nextConstraint} is non-null, this method will
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   475
         * call {@code nextConstraint}'s {@code permits()} to check if the
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   476
         * constraint is allowed or denied.  If the constraint's
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   477
         * {@code permits()} is allowed, this method will exit this and any
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   478
         * recursive next() calls, returning 'true'.  If the constraints called
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   479
         * were disallowed, the last constraint will throw
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   480
         * {@code CertPathValidatorException}.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   481
         *
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   482
         * @param cp CertConstraintParameters
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   483
         * @return 'true' if constraint allows the operation, 'false' if
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   484
         * we are at the end of the constraint list or,
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   485
         * {@code nextConstraint} is null.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   486
         */
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   487
        boolean next(CertConstraintParameters cp)
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   488
                throws CertPathValidatorException {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   489
            if (nextConstraint != null) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   490
                nextConstraint.permits(cp);
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   491
                return true;
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   492
            }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   493
            return false;
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   494
        }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   495
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   496
        /**
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   497
         * Recursively check if this constraint is allowed,
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   498
         *
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   499
         * If {@code nextConstraint} is non-null, this method will
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   500
         * call {@code nextConstraint}'s {@code permit()} to check if the
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   501
         * constraint is allowed or denied.  If the constraint's
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   502
         * {@code permit()} is allowed, this method will exit this and any
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   503
         * recursive next() calls, returning 'true'.  If the constraints
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   504
         * called were disallowed the check will exit with 'false'.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   505
         *
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   506
         * @param key Public key
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   507
         * @return 'true' if constraint allows the operation, 'false' if
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   508
         * the constraint denies the operation.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   509
         */
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   510
        boolean next(Key key) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   511
            if (nextConstraint != null && nextConstraint.permits(key)) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   512
                return true;
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   513
            }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   514
            return false;
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   515
        }
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   516
    }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   517
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   518
    /*
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   519
     * This class contains constraints dealing with the certificate chain
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   520
     * of the certificate.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   521
     */
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   522
    private static class jdkCAConstraint extends Constraint {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   523
        jdkCAConstraint(String algo) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   524
            algorithm = algo;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   525
        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   526
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   527
        /*
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   528
         * Check if CertConstraintParameters has a trusted match, if it does
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   529
         * call next() for any following constraints. If it does not, exit
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   530
         * as this constraint(s) does not restrict the operation.
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   531
         */
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   532
        public void permits(CertConstraintParameters cp)
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   533
                throws CertPathValidatorException {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   534
            if (debug != null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   535
                debug.println("jdkCAConstraints.permits(): " + algorithm);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   536
            }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   537
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   538
            // Check chain has a trust anchor in cacerts
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   539
            if (cp.isTrustedMatch()) {
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   540
                if (next(cp)) {
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   541
                    return;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   542
                }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   543
                throw new CertPathValidatorException(
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   544
                        "Algorithm constraints check failed on certificate " +
41562
1e040ccac110 8165274: SHA1 certpath constraint check fails with OCSP certificate
ascarpino
parents: 41381
diff changeset
   545
                                "anchor limits. " + algorithm + " used with " +
1e040ccac110 8165274: SHA1 certpath constraint check fails with OCSP certificate
ascarpino
parents: 41381
diff changeset
   546
                                cp.getCertificate().getSubjectX500Principal(),
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   547
                        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: 32649
diff changeset
   548
            }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   549
        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   550
    }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   551
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   552
    /*
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   553
     * This class handles the denyAfter constraint.  The date is in the UTC/GMT
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   554
     * timezone.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   555
     */
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   556
     private static class DenyAfterConstraint extends Constraint {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   557
         private Date denyAfterDate;
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   558
         private static final SimpleDateFormat dateFormat =
41381
3e7e2ee8ba73 8165103: Update to "denyAfter constraint check" exception message
ascarpino
parents: 39465
diff changeset
   559
                 new SimpleDateFormat("EEE, MMM d HH:mm:ss z yyyy");
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   560
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   561
         DenyAfterConstraint(String algo, int year, int month, int day) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   562
             Calendar c;
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   563
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   564
             algorithm = algo;
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   565
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   566
             if (debug != null) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   567
                 debug.println("DenyAfterConstraint read in as:  year " +
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   568
                         year + ", month = " + month + ", day = " + day);
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   569
             }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   570
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   571
             c = new Calendar.Builder().setTimeZone(TimeZone.getTimeZone("GMT"))
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   572
                     .setDate(year, month - 1, day).build();
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   573
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   574
             if (year > c.getActualMaximum(Calendar.YEAR) ||
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   575
                     year < c.getActualMinimum(Calendar.YEAR)) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   576
                 throw new IllegalArgumentException(
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   577
                         "Invalid year given in constraint: " + year);
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   578
             }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   579
             if ((month - 1) > c.getActualMaximum(Calendar.MONTH) ||
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   580
                     (month - 1) < c.getActualMinimum(Calendar.MONTH)) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   581
                 throw new IllegalArgumentException(
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   582
                         "Invalid month given in constraint: " + month);
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   583
             }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   584
             if (day > c.getActualMaximum(Calendar.DAY_OF_MONTH) ||
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   585
                     day < c.getActualMinimum(Calendar.DAY_OF_MONTH)) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   586
                 throw new IllegalArgumentException(
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   587
                         "Invalid Day of Month given in constraint: " + day);
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   588
             }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   589
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   590
             denyAfterDate = c.getTime();
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   591
             if (debug != null) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   592
                 debug.println("DenyAfterConstraint date set to: " +
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   593
                         dateFormat.format(denyAfterDate));
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   594
             }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   595
         }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   596
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   597
         /*
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   598
          * Checking that the provided date is not beyond the constraint date.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   599
          * The provided date can be the PKIXParameter date if given,
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   600
          * otherwise it is the current date.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   601
          *
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   602
          * If the constraint disallows, call next() for any following
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   603
          * constraints. Throw an exception if this is the last constraint.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   604
          */
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   605
         @Override
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   606
         public void permits(CertConstraintParameters cp)
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   607
                 throws CertPathValidatorException {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   608
             Date currentDate;
41381
3e7e2ee8ba73 8165103: Update to "denyAfter constraint check" exception message
ascarpino
parents: 39465
diff changeset
   609
             String errmsg;
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   610
39465
8c06651d9138 8154015: Apply algorithm constraints to timestamped code
ascarpino
parents: 38576
diff changeset
   611
             if (cp.getJARTimestamp() != null) {
8c06651d9138 8154015: Apply algorithm constraints to timestamped code
ascarpino
parents: 38576
diff changeset
   612
                 currentDate = cp.getJARTimestamp().getTimestamp();
41381
3e7e2ee8ba73 8165103: Update to "denyAfter constraint check" exception message
ascarpino
parents: 39465
diff changeset
   613
                 errmsg = "JAR Timestamp date: ";
39465
8c06651d9138 8154015: Apply algorithm constraints to timestamped code
ascarpino
parents: 38576
diff changeset
   614
             } else if (cp.getPKIXParamDate() != null) {
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   615
                 currentDate = cp.getPKIXParamDate();
41381
3e7e2ee8ba73 8165103: Update to "denyAfter constraint check" exception message
ascarpino
parents: 39465
diff changeset
   616
                 errmsg = "PKIXParameter date: ";
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   617
             } else {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   618
                 currentDate = new Date();
41381
3e7e2ee8ba73 8165103: Update to "denyAfter constraint check" exception message
ascarpino
parents: 39465
diff changeset
   619
                 errmsg = "Certificate date: ";
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   620
             }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   621
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   622
             if (!denyAfterDate.after(currentDate)) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   623
                 if (next(cp)) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   624
                     return;
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   625
                 }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   626
                 throw new CertPathValidatorException(
41562
1e040ccac110 8165274: SHA1 certpath constraint check fails with OCSP certificate
ascarpino
parents: 41381
diff changeset
   627
                         "denyAfter constraint check failed: " + algorithm +
1e040ccac110 8165274: SHA1 certpath constraint check fails with OCSP certificate
ascarpino
parents: 41381
diff changeset
   628
                                 " used with Constraint date: " +
41381
3e7e2ee8ba73 8165103: Update to "denyAfter constraint check" exception message
ascarpino
parents: 39465
diff changeset
   629
                                 dateFormat.format(denyAfterDate) + "; "
3e7e2ee8ba73 8165103: Update to "denyAfter constraint check" exception message
ascarpino
parents: 39465
diff changeset
   630
                                 + errmsg + dateFormat.format(currentDate),
3e7e2ee8ba73 8165103: Update to "denyAfter constraint check" exception message
ascarpino
parents: 39465
diff changeset
   631
                         null, null, -1, BasicReason.ALGORITHM_CONSTRAINED);
38576
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   632
             }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   633
         }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   634
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   635
         /*
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   636
          * Return result if the constraint's date is beyond the current date
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   637
          * in UTC timezone.
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   638
          */
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   639
         public boolean permits(Key key) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   640
             if (next(key)) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   641
                 return true;
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   642
             }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   643
             if (debug != null) {
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   644
                 debug.println("DenyAfterConstraints.permits(): " + algorithm);
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   645
             }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   646
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   647
             return denyAfterDate.after(new Date());
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   648
         }
ccaac80108c5 8154005: Add algorithm constraint that specifies the restriction date
ascarpino
parents: 37726
diff changeset
   649
     }
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   650
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   651
    /*
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   652
     * This class contains constraints dealing with the key size
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   653
     * support limits per algorithm.   e.g.  "keySize <= 1024"
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   654
     */
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   655
    private static class KeySizeConstraint extends Constraint {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   656
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   657
        private int minSize;            // the minimal available key size
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   658
        private int maxSize;            // the maximal available key size
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   659
        private int prohibitedSize = -1;    // unavailable key sizes
41562
1e040ccac110 8165274: SHA1 certpath constraint check fails with OCSP certificate
ascarpino
parents: 41381
diff changeset
   660
        private int size;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   661
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   662
        public KeySizeConstraint(String algo, Operator operator, int length) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   663
            algorithm = algo;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   664
            switch (operator) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   665
                case EQ:      // an unavailable key size
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   666
                    this.minSize = 0;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   667
                    this.maxSize = Integer.MAX_VALUE;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   668
                    prohibitedSize = length;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   669
                    break;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   670
                case NE:
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   671
                    this.minSize = length;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   672
                    this.maxSize = length;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   673
                    break;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   674
                case LT:
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   675
                    this.minSize = length;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   676
                    this.maxSize = Integer.MAX_VALUE;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   677
                    break;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   678
                case LE:
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   679
                    this.minSize = length + 1;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   680
                    this.maxSize = Integer.MAX_VALUE;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   681
                    break;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   682
                case GT:
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   683
                    this.minSize = 0;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   684
                    this.maxSize = length;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   685
                    break;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   686
                case GE:
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   687
                    this.minSize = 0;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   688
                    this.maxSize = length > 1 ? (length - 1) : 0;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   689
                    break;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   690
                default:
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   691
                    // unlikely to happen
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   692
                    this.minSize = Integer.MAX_VALUE;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   693
                    this.maxSize = -1;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   694
            }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   695
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   696
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   697
        /*
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   698
         * If we are passed a certificate, extract the public key and use it.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   699
         *
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   700
         * Check if each constraint fails and check if there is a linked
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   701
         * constraint  Any permitted constraint will exit the linked list
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   702
         * to allow the operation.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   703
         */
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   704
        public void permits(CertConstraintParameters cp)
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   705
                throws CertPathValidatorException {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   706
            if (!permitsImpl(cp.getCertificate().getPublicKey())) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   707
                if (nextConstraint != null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   708
                    nextConstraint.permits(cp);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   709
                    return;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   710
                }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   711
                throw new CertPathValidatorException(
41562
1e040ccac110 8165274: SHA1 certpath constraint check fails with OCSP certificate
ascarpino
parents: 41381
diff changeset
   712
                        "Algorithm constraints check failed on keysize limits. "
1e040ccac110 8165274: SHA1 certpath constraint check fails with OCSP certificate
ascarpino
parents: 41381
diff changeset
   713
                                + algorithm + " " + size + "bit key used with "
1e040ccac110 8165274: SHA1 certpath constraint check fails with OCSP certificate
ascarpino
parents: 41381
diff changeset
   714
                                + cp.getCertificate().getSubjectX500Principal(),
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   715
                        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: 32649
diff changeset
   716
            }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   717
        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   718
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   719
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   720
        // Check if key constraint disable the specified key
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   721
        // Uses old style permit()
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   722
        public boolean permits(Key key) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   723
            // If we recursively find a constraint that permits us to use
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   724
            // this key, return true and skip any other constraint checks.
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   725
            if (nextConstraint != null && nextConstraint.permits(key)) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   726
                return true;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   727
            }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   728
            if (debug != null) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   729
                debug.println("KeySizeConstraints.permits(): " + algorithm);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   730
            }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   731
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   732
            return permitsImpl(key);
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   733
        }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   734
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   735
        private boolean permitsImpl(Key key) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   736
            // Verify this constraint is for this public key algorithm
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   737
            if (algorithm.compareToIgnoreCase(key.getAlgorithm()) != 0) {
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   738
                return true;
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   739
            }
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   740
41562
1e040ccac110 8165274: SHA1 certpath constraint check fails with OCSP certificate
ascarpino
parents: 41381
diff changeset
   741
            size = KeyUtil.getKeySize(key);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   742
            if (size == 0) {
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   743
                return false;    // we don't allow any key of size 0.
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 7040
diff changeset
   744
            } else if (size > 0) {
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   745
                return !((size < minSize) || (size > maxSize) ||
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   746
                    (prohibitedSize == size));
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 7040
diff changeset
   747
            }   // Otherwise, the key size is not accessible. Conservatively,
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 7040
diff changeset
   748
                // please don't disable such keys.
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   749
37726
bbecfff95ec3 8140422: Add mechanism to allow non default root CAs to be not subject to algorithm restrictions
ascarpino
parents: 32649
diff changeset
   750
            return true;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   751
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   752
    }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   753
}
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
diff changeset
   754