jdk/src/java.base/share/classes/sun/security/validator/EndEntityChecker.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 29901 7288f904fbe8
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29901
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.validator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.x509.NetscapeCertTypeExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Class to check if an end entity cert is suitable for use in some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * context.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class is used internally by the validator. Currently, seven variants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * are supported defined as VAR_XXX constants in the Validator class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <li>Generic. No additional requirements, all certificates are ok.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <li>TLS server. Requires that a String parameter is passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * validate that specifies the name of the TLS key exchange algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * in use. See the JSSE X509TrustManager spec for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <li>TLS client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <li>Code signing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <li>JCE code signing. Some early JCE code signing certs issued to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * providers had incorrect extensions. In this mode the checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * are relaxed compared to standard code signing checks in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * allow these certificates to pass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <li>Plugin code signing. WebStart and Plugin require their own variant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * which is equivalent to VAR_CODE_SIGNING with additional checks for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * compatibility/special cases. See also PKIXValidator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <li>TSA Server (see RFC 3161, section 2.3).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
class EndEntityChecker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // extended key usage OIDs for TLS server, TLS client, code signing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // and any usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
    71
    private static final String OID_EXTENDED_KEY_USAGE =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                SimpleValidator.OID_EXTENDED_KEY_USAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
    74
    private static final String OID_EKU_TLS_SERVER = "1.3.6.1.5.5.7.3.1";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
    76
    private static final String OID_EKU_TLS_CLIENT = "1.3.6.1.5.5.7.3.2";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
    78
    private static final String OID_EKU_CODE_SIGNING = "1.3.6.1.5.5.7.3.3";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
    80
    private static final String OID_EKU_TIME_STAMPING = "1.3.6.1.5.5.7.3.8";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
    82
    private static final String OID_EKU_ANY_USAGE = "2.5.29.37.0";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // the Netscape Server-Gated-Cryptography EKU extension OID
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
    85
    private static final String OID_EKU_NS_SGC = "2.16.840.1.113730.4.1";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // the Microsoft Server-Gated-Cryptography EKU extension OID
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
    88
    private static final String OID_EKU_MS_SGC = "1.3.6.1.4.1.311.10.3.3";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
299
413c5467beb7 6668231: Presence of a critical subjectAltName causes JSSE's SunX509 to fail trusted checks
xuelei
parents: 2
diff changeset
    90
    // the recognized extension OIDs
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
    91
    private static final String OID_SUBJECT_ALT_NAME = "2.5.29.17";
299
413c5467beb7 6668231: Presence of a critical subjectAltName causes JSSE's SunX509 to fail trusted checks
xuelei
parents: 2
diff changeset
    92
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
    93
    private static final String NSCT_SSL_CLIENT =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                NetscapeCertTypeExtension.SSL_CLIENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
    96
    private static final String NSCT_SSL_SERVER =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                NetscapeCertTypeExtension.SSL_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
    99
    private static final String NSCT_CODE_SIGNING =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                NetscapeCertTypeExtension.OBJECT_SIGNING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    // bit numbers in the key usage extension
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
   103
    private static final int KU_SIGNATURE = 0;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
   104
    private static final int KU_KEY_ENCIPHERMENT = 2;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
   105
    private static final int KU_KEY_AGREEMENT = 4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // TLS key exchange algorithms requiring digitalSignature key usage
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
   108
    private static final Collection<String> KU_SERVER_SIGNATURE =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        Arrays.asList("DHE_DSS", "DHE_RSA", "ECDHE_ECDSA", "ECDHE_RSA",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            "RSA_EXPORT", "UNKNOWN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // TLS key exchange algorithms requiring keyEncipherment key usage
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
   113
    private static final Collection<String> KU_SERVER_ENCRYPTION =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        Arrays.asList("RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // TLS key exchange algorithms requiring keyAgreement key usage
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29901
diff changeset
   117
    private static final Collection<String> KU_SERVER_KEY_AGREEMENT =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        Arrays.asList("DH_DSS", "DH_RSA", "ECDH_ECDSA", "ECDH_RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // variant of this end entity cert checker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private final String variant;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // type of the validator this checker belongs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private final String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private EndEntityChecker(String type, String variant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        this.variant = variant;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    static EndEntityChecker getInstance(String type, String variant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return new EndEntityChecker(type, variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
29901
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   135
    void check(X509Certificate cert, Object parameter,
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   136
            boolean checkUnresolvedCritExts) throws CertificateException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (variant.equals(Validator.VAR_GENERIC)) {
29901
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   138
            return; // no checks
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   139
        }
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   140
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   141
        Set<String> exts = getCriticalExtensions(cert);
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   142
        if (variant.equals(Validator.VAR_TLS_SERVER)) {
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   143
            checkTLSServer(cert, (String)parameter, exts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } else if (variant.equals(Validator.VAR_TLS_CLIENT)) {
29901
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   145
            checkTLSClient(cert, exts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } else if (variant.equals(Validator.VAR_CODE_SIGNING)) {
29901
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   147
            checkCodeSigning(cert, exts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } else if (variant.equals(Validator.VAR_JCE_SIGNING)) {
29901
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   149
            checkCodeSigning(cert, exts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } else if (variant.equals(Validator.VAR_PLUGIN_CODE_SIGNING)) {
29901
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   151
            checkCodeSigning(cert, exts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } else if (variant.equals(Validator.VAR_TSA_SERVER)) {
29901
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   153
            checkTSAServer(cert, exts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new CertificateException("Unknown variant: " + variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
29901
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   157
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   158
        // if neither VAR_GENERIC variant nor unknown variant
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   159
        if (checkUnresolvedCritExts) {
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   160
            checkRemainingExtensions(exts);
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   161
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Utility method returning the Set of critical extensions for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * certificate cert (never null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private Set<String> getCriticalExtensions(X509Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Set<String> exts = cert.getCriticalExtensionOIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (exts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            exts = Collections.emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return exts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Utility method checking if there are any unresolved critical extensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @throws CertificateException if so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private void checkRemainingExtensions(Set<String> exts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        // basic constraints irrelevant in EE certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        exts.remove(SimpleValidator.OID_BASIC_CONSTRAINTS);
299
413c5467beb7 6668231: Presence of a critical subjectAltName causes JSSE's SunX509 to fail trusted checks
xuelei
parents: 2
diff changeset
   184
413c5467beb7 6668231: Presence of a critical subjectAltName causes JSSE's SunX509 to fail trusted checks
xuelei
parents: 2
diff changeset
   185
        // If the subject field contains an empty sequence, the subjectAltName
413c5467beb7 6668231: Presence of a critical subjectAltName causes JSSE's SunX509 to fail trusted checks
xuelei
parents: 2
diff changeset
   186
        // extension MUST be marked critical.
413c5467beb7 6668231: Presence of a critical subjectAltName causes JSSE's SunX509 to fail trusted checks
xuelei
parents: 2
diff changeset
   187
        // We do not check the validity of the critical extension, just mark
413c5467beb7 6668231: Presence of a critical subjectAltName causes JSSE's SunX509 to fail trusted checks
xuelei
parents: 2
diff changeset
   188
        // it recognizable here.
413c5467beb7 6668231: Presence of a critical subjectAltName causes JSSE's SunX509 to fail trusted checks
xuelei
parents: 2
diff changeset
   189
        exts.remove(OID_SUBJECT_ALT_NAME);
413c5467beb7 6668231: Presence of a critical subjectAltName causes JSSE's SunX509 to fail trusted checks
xuelei
parents: 2
diff changeset
   190
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (!exts.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new CertificateException("Certificate contains unsupported "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                + "critical extensions: " + exts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Utility method checking if the extended key usage extension in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * certificate cert allows use for expectedEKU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private boolean checkEKU(X509Certificate cert, Set<String> exts,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            String expectedEKU) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        List<String> eku = cert.getExtendedKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (eku == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return eku.contains(expectedEKU) || eku.contains(OID_EKU_ANY_USAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Utility method checking if bit 'bit' is set in this certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * key usage extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @throws CertificateException if not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private boolean checkKeyUsage(X509Certificate cert, int bit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        boolean[] keyUsage = cert.getKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (keyUsage == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return (keyUsage.length > bit) && keyUsage[bit];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Check whether this certificate can be used for TLS client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @throws CertificateException if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
29901
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   229
    private void checkTLSClient(X509Certificate cert, Set<String> exts)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (checkKeyUsage(cert, KU_SIGNATURE) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                ("KeyUsage does not allow digital signatures",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                ValidatorException.T_EE_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (checkEKU(cert, exts, OID_EKU_TLS_CLIENT) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new ValidatorException("Extended key usage does not "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                + "permit use for TLS client authentication",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                ValidatorException.T_EE_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (!SimpleValidator.getNetscapeCertTypeBit(cert, NSCT_SSL_CLIENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                ("Netscape cert type does not permit use for SSL client",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                ValidatorException.T_EE_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // remove extensions we checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        exts.remove(SimpleValidator.OID_KEY_USAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        exts.remove(SimpleValidator.OID_EXTENDED_KEY_USAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        exts.remove(SimpleValidator.OID_NETSCAPE_CERT_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Check whether this certificate can be used for TLS server authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * using the specified authentication type parameter. See X509TrustManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * specification for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @throws CertificateException if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
29901
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   261
    private void checkTLSServer(X509Certificate cert, String parameter,
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   262
            Set<String> exts) throws CertificateException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (KU_SERVER_ENCRYPTION.contains(parameter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            if (checkKeyUsage(cert, KU_KEY_ENCIPHERMENT) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        ("KeyUsage does not allow key encipherment",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        ValidatorException.T_EE_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } else if (KU_SERVER_SIGNATURE.contains(parameter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            if (checkKeyUsage(cert, KU_SIGNATURE) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        ("KeyUsage does not allow digital signatures",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        ValidatorException.T_EE_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        } else if (KU_SERVER_KEY_AGREEMENT.contains(parameter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (checkKeyUsage(cert, KU_KEY_AGREEMENT) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        ("KeyUsage does not allow key agreement",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        ValidatorException.T_EE_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            throw new CertificateException("Unknown authType: " + parameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (checkEKU(cert, exts, OID_EKU_TLS_SERVER) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // check for equivalent but now obsolete Server-Gated-Cryptography
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // (aka Step-Up, 128 bit) EKU OIDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if ((checkEKU(cert, exts, OID_EKU_MS_SGC) == false) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                (checkEKU(cert, exts, OID_EKU_NS_SGC) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    ("Extended key usage does not permit use for TLS "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    + "server authentication",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    ValidatorException.T_EE_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (!SimpleValidator.getNetscapeCertTypeBit(cert, NSCT_SSL_SERVER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                ("Netscape cert type does not permit use for SSL server",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                ValidatorException.T_EE_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        // remove extensions we checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        exts.remove(SimpleValidator.OID_KEY_USAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        exts.remove(SimpleValidator.OID_EXTENDED_KEY_USAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        exts.remove(SimpleValidator.OID_NETSCAPE_CERT_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Check whether this certificate can be used for code signing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @throws CertificateException if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
29901
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   313
    private void checkCodeSigning(X509Certificate cert, Set<String> exts)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (checkKeyUsage(cert, KU_SIGNATURE) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                ("KeyUsage does not allow digital signatures",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                ValidatorException.T_EE_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (checkEKU(cert, exts, OID_EKU_CODE_SIGNING) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                ("Extended key usage does not permit use for code signing",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                ValidatorException.T_EE_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // do not check Netscape cert type for JCE code signing checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // (some certs were issued with incorrect extensions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (variant.equals(Validator.VAR_JCE_SIGNING) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (!SimpleValidator.getNetscapeCertTypeBit(cert, NSCT_CODE_SIGNING)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    ("Netscape cert type does not permit use for code signing",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    ValidatorException.T_EE_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            exts.remove(SimpleValidator.OID_NETSCAPE_CERT_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        // remove extensions we checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        exts.remove(SimpleValidator.OID_KEY_USAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        exts.remove(SimpleValidator.OID_EXTENDED_KEY_USAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Check whether this certificate can be used by a time stamping authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * server (see RFC 3161, section 2.3).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @throws CertificateException if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
29901
7288f904fbe8 8076117: EndEntityChecker should not process custom extensions after PKIX validation
juh
parents: 25859
diff changeset
   348
    private void checkTSAServer(X509Certificate cert, Set<String> exts)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (checkKeyUsage(cert, KU_SIGNATURE) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                ("KeyUsage does not allow digital signatures",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                ValidatorException.T_EE_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (cert.getExtendedKeyUsage() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                ("Certificate does not contain an extended key usage " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                "extension required for a TSA server",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                ValidatorException.T_EE_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (checkEKU(cert, exts, OID_EKU_TIME_STAMPING) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                ("Extended key usage does not permit use for TSA server",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                ValidatorException.T_EE_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        // remove extensions we checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        exts.remove(SimpleValidator.OID_KEY_USAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        exts.remove(SimpleValidator.OID_EXTENDED_KEY_USAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
}