jdk/src/java.base/share/classes/sun/security/validator/PKIXValidator.java
author ascarpino
Wed, 08 Feb 2017 12:08:28 -0800
changeset 43701 fe8c324ba97c
parent 39465 8c06651d9138
child 44158 49deb8a1ed3f
permissions -rw-r--r--
8160655: Fix denyAfter and usage types for security properties Reviewed-by: mullan, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43701
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 39465
diff changeset
     2
 * Copyright (c) 2002, 2017, 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: 4326
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: 4326
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: 4326
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4326
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4326
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.security.auth.x500.X500Principal;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    34
import sun.security.action.GetBooleanAction;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    35
import sun.security.provider.certpath.AlgorithmChecker;
43701
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 39465
diff changeset
    36
import sun.security.provider.certpath.PKIXExtendedParameters;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Validator implementation built on the PKIX CertPath API. This
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
    40
 * implementation will be emphasized going forward.
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    41
 * <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Note that the validate() implementation tries to use a PKIX validator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * if that appears possible and a PKIX builder otherwise. This increases
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * performance and currently also leads to better exception messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * in case of failures.
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    46
 * <p>
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    47
 * {@code PKIXValidator} objects are immutable once they have been created.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    48
 * Please DO NOT add methods that can change the state of an instance once
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    49
 * it has been created.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public final class PKIXValidator extends Validator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    55
    /**
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    56
     * Flag indicating whether to enable revocation check for the PKIX trust
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    57
     * manager. Typically, this will only work if the PKIX implementation
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    58
     * supports CRL distribution points as we do not manually setup CertStores.
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    59
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32032
diff changeset
    60
    private static final boolean checkTLSRevocation =
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    61
        AccessController.doPrivileged
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    62
            (new GetBooleanAction("com.sun.net.ssl.checkRevocation"));
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    63
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final Set<X509Certificate> trustedCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final PKIXBuilderParameters parameterTemplate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private int certPathLength = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // needed only for the validator
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    69
    private final Map<X500Principal, List<PublicKey>> trustedSubjects;
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    70
    private final CertificateFactory factory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
    72
    private final boolean plugin;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    PKIXValidator(String variant, Collection<X509Certificate> trustedCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        super(TYPE_PKIX, variant);
24370
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
    76
        this.trustedCerts = (trustedCerts instanceof Set) ?
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
    77
                            (Set<X509Certificate>)trustedCerts :
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
    78
                            new HashSet<X509Certificate>(trustedCerts);
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
    79
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
    80
        Set<TrustAnchor> trustAnchors = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        for (X509Certificate cert : trustedCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            trustAnchors.add(new TrustAnchor(cert, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
24370
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
    84
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            parameterTemplate = new PKIXBuilderParameters(trustAnchors, null);
24370
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
    87
            factory = CertificateFactory.getInstance("X.509");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } catch (InvalidAlgorithmParameterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throw new RuntimeException("Unexpected error: " + e.toString(), e);
24704
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
    90
        } catch (CertificateException e) {
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
    91
            throw new RuntimeException("Internal error", e);
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
    92
        }
24370
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
    93
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
    94
        setDefaultParameters(variant);
24704
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
    95
        plugin = variant.equals(VAR_PLUGIN_CODE_SIGNING);
24370
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
    96
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
    97
        trustedSubjects = setTrustedSubjects();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    PKIXValidator(String variant, PKIXBuilderParameters params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        super(TYPE_PKIX, variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        trustedCerts = new HashSet<X509Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        for (TrustAnchor anchor : params.getTrustAnchors()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            X509Certificate cert = anchor.getTrustedCert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (cert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                trustedCerts.add(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        parameterTemplate = params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
24704
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   111
        try {
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   112
            factory = CertificateFactory.getInstance("X.509");
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   113
        } catch (CertificateException e) {
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   114
            throw new RuntimeException("Internal error", e);
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   115
        }
24370
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   116
24704
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   117
        plugin = variant.equals(VAR_PLUGIN_CODE_SIGNING);
24370
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   118
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   119
        trustedSubjects = setTrustedSubjects();
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   120
    }
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   121
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   122
    /**
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   123
     * Populate the trustedSubjects Map using the DN and public keys from
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   124
     * the list of trusted certificates
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   125
     *
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   126
     * @return Map containing each subject DN and one or more public keys
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   127
     *    tied to those DNs.
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   128
     */
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   129
    private Map<X500Principal, List<PublicKey>> setTrustedSubjects() {
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   130
        Map<X500Principal, List<PublicKey>> subjectMap = new HashMap<>();
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   131
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   132
        for (X509Certificate cert : trustedCerts) {
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   133
            X500Principal dn = cert.getSubjectX500Principal();
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   134
            List<PublicKey> keys;
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   135
            if (subjectMap.containsKey(dn)) {
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   136
                keys = subjectMap.get(dn);
24704
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   137
            } else {
24370
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   138
                keys = new ArrayList<PublicKey>();
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   139
                subjectMap.put(dn, keys);
24704
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   140
            }
24370
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   141
            keys.add(cert.getPublicKey());
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   142
        }
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   143
1398d972eef1 6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents: 14342
diff changeset
   144
        return subjectMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public Collection<X509Certificate> getTrustedCertificates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return trustedCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Returns the length of the last certification path that is validated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * CertPathValidator. This is intended primarily as a callback mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * for PKIXCertPathCheckers to determine the length of the certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * path that is being validated. It is necessary since engineValidate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * may modify the length of the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @return the length of the last certification path passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *   CertPathValidator.validate, or -1 if it has not been invoked yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   161
    public int getCertPathLength() { // mutable, should be private
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return certPathLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Set J2SE global default PKIX parameters. Currently, hardcoded to disable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * revocation checking. In the future, this should be configurable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private void setDefaultParameters(String variant) {
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   170
        if ((variant == Validator.VAR_TLS_SERVER) ||
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   171
                (variant == Validator.VAR_TLS_CLIENT)) {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   172
            parameterTemplate.setRevocationEnabled(checkTLSRevocation);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   173
        } else {
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   174
            parameterTemplate.setRevocationEnabled(false);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   175
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Return the PKIX parameters used by this instance. An application may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * modify the parameters but must make sure not to perform any concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * validations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   183
    public PKIXBuilderParameters getParameters() { // mutable, should be private
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return parameterTemplate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   187
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    X509Certificate[] engineValidate(X509Certificate[] chain,
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   189
            Collection<X509Certificate> otherCerts,
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   190
            List<byte[]> responseList,
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   191
            AlgorithmConstraints constraints,
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   192
            Object parameter) throws CertificateException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if ((chain == null) || (chain.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                ("null or zero-length certificate chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   197
39465
8c06651d9138 8154015: Apply algorithm constraints to timestamped code
ascarpino
parents: 32649
diff changeset
   198
        // Check if 'parameter' affects 'pkixParameters'
8c06651d9138 8154015: Apply algorithm constraints to timestamped code
ascarpino
parents: 32649
diff changeset
   199
        PKIXBuilderParameters pkixParameters = null;
8c06651d9138 8154015: Apply algorithm constraints to timestamped code
ascarpino
parents: 32649
diff changeset
   200
        if (parameter instanceof Timestamp && plugin) {
8c06651d9138 8154015: Apply algorithm constraints to timestamped code
ascarpino
parents: 32649
diff changeset
   201
            try {
43701
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 39465
diff changeset
   202
                pkixParameters = new PKIXExtendedParameters(
39465
8c06651d9138 8154015: Apply algorithm constraints to timestamped code
ascarpino
parents: 32649
diff changeset
   203
                        (PKIXBuilderParameters) parameterTemplate.clone(),
43701
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 39465
diff changeset
   204
                        (Timestamp) parameter, variant);
39465
8c06651d9138 8154015: Apply algorithm constraints to timestamped code
ascarpino
parents: 32649
diff changeset
   205
            } catch (InvalidAlgorithmParameterException e) {
8c06651d9138 8154015: Apply algorithm constraints to timestamped code
ascarpino
parents: 32649
diff changeset
   206
                // ignore exception
8c06651d9138 8154015: Apply algorithm constraints to timestamped code
ascarpino
parents: 32649
diff changeset
   207
            }
8c06651d9138 8154015: Apply algorithm constraints to timestamped code
ascarpino
parents: 32649
diff changeset
   208
        } else {
8c06651d9138 8154015: Apply algorithm constraints to timestamped code
ascarpino
parents: 32649
diff changeset
   209
            pkixParameters = (PKIXBuilderParameters) parameterTemplate.clone();
8c06651d9138 8154015: Apply algorithm constraints to timestamped code
ascarpino
parents: 32649
diff changeset
   210
        }
8c06651d9138 8154015: Apply algorithm constraints to timestamped code
ascarpino
parents: 32649
diff changeset
   211
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   212
        // add new algorithm constraints checker
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   213
        if (constraints != null) {
43701
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 39465
diff changeset
   214
            pkixParameters.addCertPathChecker(
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 39465
diff changeset
   215
                    new AlgorithmChecker(constraints, null, variant));
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   216
        }
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   217
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   218
        // attach it to the PKIXBuilderParameters.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   219
        if (!responseList.isEmpty()) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   220
            addResponses(pkixParameters, chain, responseList);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   221
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   222
24704
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   223
        // check that chain is in correct order and check if chain contains
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   224
        // trust anchor
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   225
        X500Principal prevIssuer = null;
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   226
        for (int i = 0; i < chain.length; i++) {
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   227
            X509Certificate cert = chain[i];
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   228
            X500Principal dn = cert.getSubjectX500Principal();
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   229
            if (i != 0 && !dn.equals(prevIssuer)) {
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   230
                // chain is not ordered correctly, call builder instead
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   231
                return doBuild(chain, otherCerts, pkixParameters);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
24704
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   234
            // Check if chain[i] is already trusted. It may be inside
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   235
            // trustedCerts, or has the same dn and public key as a cert
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   236
            // inside trustedCerts. The latter happens when a CA has
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   237
            // updated its cert with a stronger signature algorithm in JRE
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   238
            // but the weak one is still in circulation.
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   239
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   240
            if (trustedCerts.contains(cert) ||          // trusted cert
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   241
                    (trustedSubjects.containsKey(dn) && // replacing ...
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   242
                     trustedSubjects.get(dn).contains(  // ... weak cert
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   243
                        cert.getPublicKey()))) {
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   244
                if (i == 0) {
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   245
                    return new X509Certificate[] {chain[0]};
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   246
                }
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   247
                // Remove and call validator on partial chain [0 .. i-1]
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   248
                X509Certificate[] newChain = new X509Certificate[i];
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   249
                System.arraycopy(chain, 0, newChain, 0, i);
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   250
                return doValidate(newChain, pkixParameters);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
24704
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   252
            prevIssuer = cert.getIssuerX500Principal();
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   253
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
24704
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   255
        // apparently issued by trust anchor?
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   256
        X509Certificate last = chain[chain.length - 1];
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   257
        X500Principal issuer = last.getIssuerX500Principal();
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   258
        X500Principal subject = last.getSubjectX500Principal();
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   259
        if (trustedSubjects.containsKey(issuer) &&
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   260
                isSignatureValid(trustedSubjects.get(issuer), last)) {
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   261
            return doValidate(chain, pkixParameters);
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   262
        }
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   263
24704
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   264
        // don't fallback to builder if called from plugin/webstart
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   265
        if (plugin) {
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   266
            // Validate chain even if no trust anchor is found. This
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   267
            // allows plugin/webstart to make sure the chain is
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   268
            // otherwise valid
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   269
            if (chain.length > 1) {
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   270
                X509Certificate[] newChain =
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   271
                    new X509Certificate[chain.length-1];
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   272
                System.arraycopy(chain, 0, newChain, 0, newChain.length);
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   273
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   274
                // temporarily set last cert as sole trust anchor
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   275
                try {
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   276
                    pkixParameters.setTrustAnchors
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   277
                        (Collections.singleton(new TrustAnchor
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   278
                            (chain[chain.length-1], null)));
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   279
                } catch (InvalidAlgorithmParameterException iape) {
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   280
                    // should never occur, but ...
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   281
                    throw new CertificateException(iape);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                }
24704
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   283
                doValidate(newChain, pkixParameters);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
24704
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   285
            // if the rest of the chain is valid, throw exception
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   286
            // indicating no trust anchor was found
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   287
            throw new ValidatorException
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   288
                (ValidatorException.T_NO_TRUST_ANCHOR);
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   289
        }
76581302d209 8044771: PKIXValidator indent cleanup
xuelei
parents: 24370
diff changeset
   290
        // otherwise, fall back to builder
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   292
        return doBuild(chain, otherCerts, pkixParameters);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   295
    private boolean isSignatureValid(List<PublicKey> keys,
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   296
            X509Certificate sub) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (plugin) {
5782
50575882b36f 6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents: 5627
diff changeset
   298
            for (PublicKey key: keys) {
50575882b36f 6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents: 5627
diff changeset
   299
                try {
50575882b36f 6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents: 5627
diff changeset
   300
                    sub.verify(key);
50575882b36f 6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents: 5627
diff changeset
   301
                    return true;
50575882b36f 6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents: 5627
diff changeset
   302
                } catch (Exception ex) {
50575882b36f 6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents: 5627
diff changeset
   303
                    continue;
50575882b36f 6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents: 5627
diff changeset
   304
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
5782
50575882b36f 6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents: 5627
diff changeset
   306
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return true; // only check if PLUGIN is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    private static X509Certificate[] toArray(CertPath path, TrustAnchor anchor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        List<? extends java.security.cert.Certificate> list =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                                path.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        X509Certificate[] chain = new X509Certificate[list.size() + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        list.toArray(chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        X509Certificate trustedCert = anchor.getTrustedCert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (trustedCert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                ("TrustAnchor must be specified as certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        chain[chain.length - 1] = trustedCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Set the check date (for debugging).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    private void setDate(PKIXBuilderParameters params) {
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 7040
diff changeset
   330
        @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        Date date = validationDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (date != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            params.setDate(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private X509Certificate[] doValidate(X509Certificate[] chain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            PKIXBuilderParameters params) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            setDate(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            // do the validation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            CertPathValidator validator = CertPathValidator.getInstance("PKIX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            CertPath path = factory.generateCertPath(Arrays.asList(chain));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            certPathLength = chain.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            PKIXCertPathValidatorResult result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                (PKIXCertPathValidatorResult)validator.validate(path, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return toArray(path, result.getTrustAnchor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                ("PKIX path validation failed: " + e.toString(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    private X509Certificate[] doBuild(X509Certificate[] chain,
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   357
        Collection<X509Certificate> otherCerts,
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5782
diff changeset
   358
        PKIXBuilderParameters params) throws CertificateException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            setDate(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            // setup target constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            X509CertSelector selector = new X509CertSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            selector.setCertificate(chain[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            params.setTargetCertConstraints(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            // setup CertStores
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            Collection<X509Certificate> certs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                                        new ArrayList<X509Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            certs.addAll(Arrays.asList(chain));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (otherCerts != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                certs.addAll(otherCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            CertStore store = CertStore.getInstance("Collection",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                new CollectionCertStoreParameters(certs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            params.addCertStore(store);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            // do the build
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            CertPathBuilder builder = CertPathBuilder.getInstance("PKIX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            PKIXCertPathBuilderResult result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                (PKIXCertPathBuilderResult)builder.build(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            return toArray(result.getCertPath(), result.getTrustAnchor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                ("PKIX path building failed: " + e.toString(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   390
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   391
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   392
     * For OCSP Stapling, add responses that came in during the handshake
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   393
     * into a {@code PKIXRevocationChecker} so we can evaluate them.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   394
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   395
     * @param pkixParams the pkixParameters object that will be used in
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   396
     * path validation.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   397
     * @param chain the chain of certificates to verify
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   398
     * @param responseList a {@code List} of zero or more byte arrays, each
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   399
     * one being a DER-encoded OCSP response (per RFC 6960).  Entries
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   400
     * in the List must match the order of the certificates in the
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   401
     * chain parameter.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   402
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   403
    private static void addResponses(PKIXBuilderParameters pkixParams,
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   404
            X509Certificate[] chain, List<byte[]> responseList) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   405
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   406
        if (pkixParams.isRevocationEnabled()) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   407
            try {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   408
                // Make a modifiable copy of the CertPathChecker list
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   409
                PKIXRevocationChecker revChecker = null;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   410
                List<PKIXCertPathChecker> checkerList =
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   411
                        new ArrayList<>(pkixParams.getCertPathCheckers());
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   412
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   413
                // Find the first PKIXRevocationChecker in the list
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   414
                for (PKIXCertPathChecker checker : checkerList) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   415
                    if (checker instanceof PKIXRevocationChecker) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   416
                        revChecker = (PKIXRevocationChecker)checker;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   417
                        break;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   418
                    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   419
                }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   420
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   421
                // If we still haven't found one, make one
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   422
                if (revChecker == null) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   423
                    revChecker = (PKIXRevocationChecker)CertPathValidator.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   424
                            getInstance("PKIX").getRevocationChecker();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   425
                    checkerList.add(revChecker);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   426
                }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   427
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   428
                // Each response in the list should be in parallel with
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   429
                // the certificate list.  If there is a zero-length response
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   430
                // treat it as being absent.  If the user has provided their
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   431
                // own PKIXRevocationChecker with pre-populated responses, do
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   432
                // not overwrite them with the ones from the handshake.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   433
                Map<X509Certificate, byte[]> responseMap =
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   434
                        revChecker.getOcspResponses();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   435
                int limit = Integer.min(chain.length, responseList.size());
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   436
                for (int idx = 0; idx < limit; idx++) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   437
                    byte[] respBytes = responseList.get(idx);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   438
                    if (respBytes != null && respBytes.length > 0 &&
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   439
                            !responseMap.containsKey(chain[idx])) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   440
                        responseMap.put(chain[idx], respBytes);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   441
                    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   442
                }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   443
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   444
                // Add the responses and push it all back into the
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   445
                // PKIXBuilderParameters
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   446
                revChecker.setOcspResponses(responseMap);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   447
                pkixParams.setCertPathCheckers(checkerList);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   448
            } catch (NoSuchAlgorithmException exc) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   449
                // This should not occur, but if it does happen then
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   450
                // stapled OCSP responses won't be part of revocation checking.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   451
                // Clients can still fall back to other means of revocation
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   452
                // checking.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   453
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   454
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30374
diff changeset
   455
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
}