jdk/src/java.base/share/classes/sun/security/provider/certpath/AdaptableX509CertSelector.java
author juh
Tue, 03 Mar 2015 14:16:49 -0800
changeset 29264 5172066a2da6
parent 25859 3317bb8137f4
child 33820 be91931ea4b2
permissions -rw-r--r--
8054037: Improve tracing for java.security.debug=certpath 8055207: keystore and truststore debug output could be much better Reviewed-by: mullan, coffeys, jnimeh
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
     1
/*
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
     4
 *
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    10
 *
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    15
 * accompanied this code).
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    16
 *
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    20
 *
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    23
 * questions.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    24
 */
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    25
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    26
package sun.security.provider.certpath;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    27
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    28
import java.io.IOException;
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    29
import java.math.BigInteger;
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    30
import java.security.cert.Certificate;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    31
import java.security.cert.X509Certificate;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    32
import java.security.cert.X509CertSelector;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    33
import java.security.cert.CertificateException;
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    34
import java.util.Arrays;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    35
import java.util.Date;
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    36
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    37
import sun.security.util.Debug;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    38
import sun.security.util.DerInputStream;
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    39
import sun.security.util.DerOutputStream;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    40
import sun.security.x509.SerialNumber;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    41
import sun.security.x509.KeyIdentifier;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    42
import sun.security.x509.AuthorityKeyIdentifierExtension;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    43
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    44
/**
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    45
 * An adaptable X509 certificate selector for forward certification path
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    46
 * building. This selector overrides the default X509CertSelector matching
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    47
 * rules for the subjectKeyIdentifier and serialNumber criteria, and adds
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    48
 * additional rules for certificate validity.
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    49
 *
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    50
 * @since 1.7
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    51
 */
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    52
class AdaptableX509CertSelector extends X509CertSelector {
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    53
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    54
    private static final Debug debug = Debug.getInstance("certpath");
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    55
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    56
    // The start date of a validity period.
8790
2a8d836ee007 7025073: Stricter check on trust anchor makes VerifyCACerts.java test fail
xuelei
parents: 8163
diff changeset
    57
    private Date startDate;
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    58
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    59
    // The end date of a validity period.
8790
2a8d836ee007 7025073: Stricter check on trust anchor makes VerifyCACerts.java test fail
xuelei
parents: 8163
diff changeset
    60
    private Date endDate;
2a8d836ee007 7025073: Stricter check on trust anchor makes VerifyCACerts.java test fail
xuelei
parents: 8163
diff changeset
    61
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    62
    // The subject key identifier
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    63
    private byte[] ski;
8790
2a8d836ee007 7025073: Stricter check on trust anchor makes VerifyCACerts.java test fail
xuelei
parents: 8163
diff changeset
    64
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    65
    // The serial number
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    66
    private BigInteger serial;
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    67
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    68
    /**
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    69
     * Sets the criterion of the X509Certificate validity period.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    70
     *
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    71
     * Normally, we may not have to check that a certificate validity period
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    72
     * must fall within its issuer's certificate validity period. However,
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    73
     * when we face root CA key updates for version 1 certificates, according
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    74
     * to scheme of RFC 4210 or 2510, the validity periods should be checked
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    75
     * to determine the right issuer's certificate.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    76
     *
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    77
     * Conservatively, we will only check the validity periods for version
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    78
     * 1 and version 2 certificates. For version 3 certificates, we can
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    79
     * determine the right issuer by authority and subject key identifier
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    80
     * extensions.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    81
     *
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    82
     * @param startDate the start date of a validity period that must fall
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    83
     *        within the certificate validity period for the X509Certificate
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    84
     * @param endDate the end date of a validity period that must fall
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    85
     *        within the certificate validity period for the X509Certificate
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    86
     */
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    87
    void setValidityPeriod(Date startDate, Date endDate) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    88
        this.startDate = startDate;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    89
        this.endDate = endDate;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    90
    }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    91
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    92
    /**
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    93
     * This selector overrides the subjectKeyIdentifier matching rules of
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    94
     * X509CertSelector, so it throws IllegalArgumentException if this method
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    95
     * is ever called.
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    96
     */
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    97
    @Override
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    98
    public void setSubjectKeyIdentifier(byte[] subjectKeyID) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    99
        throw new IllegalArgumentException();
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   100
    }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   101
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   102
    /**
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   103
     * This selector overrides the serialNumber matching rules of
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   104
     * X509CertSelector, so it throws IllegalArgumentException if this method
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   105
     * is ever called.
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   106
     */
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   107
    @Override
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   108
    public void setSerialNumber(BigInteger serial) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   109
        throw new IllegalArgumentException();
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   110
    }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   111
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   112
    /**
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   113
     * Sets the subjectKeyIdentifier and serialNumber criteria from the
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   114
     * authority key identifier extension.
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   115
     *
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   116
     * The subjectKeyIdentifier criterion is set to the keyIdentifier field
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   117
     * of the extension, or null if it is empty. The serialNumber criterion
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   118
     * is set to the authorityCertSerialNumber field, or null if it is empty.
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   119
     *
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   120
     * Note that we do not set the subject criterion to the
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   121
     * authorityCertIssuer field of the extension. The caller MUST set
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   122
     * the subject criterion before calling match().
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   123
     *
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   124
     * @param ext the authorityKeyIdentifier extension
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   125
     * @throws IOException if there is an error parsing the extension
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   126
     */
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   127
    void setSkiAndSerialNumber(AuthorityKeyIdentifierExtension ext)
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   128
        throws IOException {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   129
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   130
        ski = null;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   131
        serial = null;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   132
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   133
        if (ext != null) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   134
            KeyIdentifier akid = (KeyIdentifier)ext.get(
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   135
                AuthorityKeyIdentifierExtension.KEY_ID);
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   136
            if (akid != null) {
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   137
                DerOutputStream derout = new DerOutputStream();
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   138
                derout.putOctetString(akid.getIdentifier());
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   139
                ski = derout.toByteArray();
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   140
            }
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   141
            SerialNumber asn = (SerialNumber)ext.get(
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   142
                AuthorityKeyIdentifierExtension.SERIAL_NUMBER);
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   143
            if (asn != null) {
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   144
                serial = asn.getNumber();
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   145
            }
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   146
            // the subject criterion should be set by the caller
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   147
        }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   148
    }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   149
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   150
    /**
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   151
     * Decides whether a <code>Certificate</code> should be selected.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   152
     *
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   153
     * This method overrides the matching rules for the subjectKeyIdentifier
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   154
     * and serialNumber criteria and adds additional rules for certificate
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   155
     * validity.
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   156
     *
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   157
     * For the purpose of compatibility, when a certificate is of
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   158
     * version 1 and version 2, or the certificate does not include
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   159
     * a subject key identifier extension, the selection criterion
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   160
     * of subjectKeyIdentifier will be disabled.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   161
     */
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   162
    @Override
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   163
    public boolean match(Certificate cert) {
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   164
        X509Certificate xcert = (X509Certificate)cert;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   165
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   166
        // match subject key identifier
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   167
        if (!matchSubjectKeyID(xcert)) {
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   168
            return false;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   169
        }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   170
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   171
        // In practice, a CA may replace its root certificate and require that
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   172
        // the existing certificate is still valid, even if the AKID extension
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   173
        // does not match the replacement root certificate fields.
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   174
        //
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   175
        // Conservatively, we only support the replacement for version 1 and
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   176
        // version 2 certificate. As for version 3, the certificate extension
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   177
        // may contain sensitive information (for example, policies), the
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   178
        // AKID need to be respected to seek the exact certificate in case
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   179
        // of key or certificate abuse.
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   180
        int version = xcert.getVersion();
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   181
        if (serial != null && version > 2) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   182
            if (!serial.equals(xcert.getSerialNumber())) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   183
                return false;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   184
            }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   185
        }
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   186
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   187
        // Check the validity period for version 1 and 2 certificate.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   188
        if (version < 3) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   189
            if (startDate != null) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   190
                try {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   191
                    xcert.checkValidity(startDate);
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   192
                } catch (CertificateException ce) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   193
                    return false;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   194
                }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   195
            }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   196
            if (endDate != null) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   197
                try {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   198
                    xcert.checkValidity(endDate);
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   199
                } catch (CertificateException ce) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   200
                    return false;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   201
                }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   202
            }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   203
        }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   204
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   205
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   206
        if (!super.match(cert)) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   207
            return false;
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   208
        }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   209
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   210
        return true;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   211
    }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   212
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   213
    /*
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   214
     * Match on subject key identifier extension value. These matching rules
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   215
     * are identical to X509CertSelector except that if the certificate does
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   216
     * not have a subject key identifier extension, it returns true.
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   217
     */
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   218
    private boolean matchSubjectKeyID(X509Certificate xcert) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   219
        if (ski == null) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   220
            return true;
8790
2a8d836ee007 7025073: Stricter check on trust anchor makes VerifyCACerts.java test fail
xuelei
parents: 8163
diff changeset
   221
        }
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   222
        try {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   223
            byte[] extVal = xcert.getExtensionValue("2.5.29.14");
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   224
            if (extVal == null) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   225
                if (debug != null) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   226
                    debug.println("AdaptableX509CertSelector.match: "
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   227
                        + "no subject key ID extension. Subject: "
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   228
                        + xcert.getSubjectX500Principal());
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   229
                }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   230
                return true;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   231
            }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   232
            DerInputStream in = new DerInputStream(extVal);
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   233
            byte[] certSubjectKeyID = in.getOctetString();
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   234
            if (certSubjectKeyID == null ||
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   235
                    !Arrays.equals(ski, certSubjectKeyID)) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   236
                if (debug != null) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   237
                    debug.println("AdaptableX509CertSelector.match: "
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   238
                        + "subject key IDs don't match. "
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   239
                        + "Expected: " + Arrays.toString(ski) + " "
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   240
                        + "Cert's: " + Arrays.toString(certSubjectKeyID));
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   241
                }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   242
                return false;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   243
            }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   244
        } catch (IOException ex) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   245
            if (debug != null) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   246
                debug.println("AdaptableX509CertSelector.match: "
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   247
                    + "exception in subject key ID check");
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   248
            }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   249
            return false;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   250
        }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   251
        return true;
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   252
    }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   253
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   254
    @Override
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   255
    public Object clone() {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   256
        AdaptableX509CertSelector copy =
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   257
                        (AdaptableX509CertSelector)super.clone();
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   258
        if (startDate != null) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   259
            copy.startDate = (Date)startDate.clone();
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   260
        }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   261
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   262
        if (endDate != null) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   263
            copy.endDate = (Date)endDate.clone();
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   264
        }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   265
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   266
        if (ski != null) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   267
            copy.ski = ski.clone();
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   268
        }
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   269
        return copy;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   270
    }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   271
}