src/java.base/share/classes/sun/security/provider/certpath/AdaptableX509CertSelector.java
author weijun
Wed, 01 Aug 2018 13:35:08 +0800
changeset 51272 9d92ff04a29c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8208602: Cannot read PEM X.509 cert if there is whitespace after the header or footer Reviewed-by: xuelei
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
/*
44920
5b66112437ba 8176457: Add verbose option to java.security.debug
ascarpino
parents: 33820
diff changeset
     2
 * Copyright (c) 2011, 2017, 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.x509.SerialNumber;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    40
import sun.security.x509.AuthorityKeyIdentifierExtension;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    41
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    42
/**
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    43
 * 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
    44
 * building. This selector overrides the default X509CertSelector matching
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    45
 * rules for the subjectKeyIdentifier and serialNumber criteria, and adds
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    46
 * additional rules for certificate validity.
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    47
 *
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    48
 * @since 1.7
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
class AdaptableX509CertSelector extends X509CertSelector {
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    51
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    52
    private static final Debug debug = Debug.getInstance("certpath");
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    53
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    54
    // 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
    55
    private Date startDate;
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    56
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    57
    // 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
    58
    private Date endDate;
2a8d836ee007 7025073: Stricter check on trust anchor makes VerifyCACerts.java test fail
xuelei
parents: 8163
diff changeset
    59
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    60
    // The subject key identifier
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    61
    private byte[] ski;
8790
2a8d836ee007 7025073: Stricter check on trust anchor makes VerifyCACerts.java test fail
xuelei
parents: 8163
diff changeset
    62
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    63
    // The serial number
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    64
    private BigInteger serial;
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    65
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    66
    /**
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    67
     * Sets the criterion of the X509Certificate validity period.
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
     * 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
    70
     * must fall within its issuer's certificate validity period. However,
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    71
     * 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
    72
     * 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
    73
     * to determine the right issuer's certificate.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    74
     *
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    75
     * Conservatively, we will only check the validity periods for version
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    76
     * 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
    77
     * determine the right issuer by authority and subject key identifier
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    78
     * extensions.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    79
     *
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    80
     * @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
    81
     *        within the certificate validity period for the X509Certificate
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    82
     * @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
    83
     *        within the certificate validity period for the X509Certificate
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    84
     */
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    85
    void setValidityPeriod(Date startDate, Date endDate) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    86
        this.startDate = startDate;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    87
        this.endDate = endDate;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    88
    }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    89
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
    90
    /**
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    91
     * This selector overrides the subjectKeyIdentifier matching rules of
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    92
     * X509CertSelector, so it throws IllegalArgumentException if this method
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    93
     * is ever called.
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    94
     */
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    95
    @Override
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    96
    public void setSubjectKeyIdentifier(byte[] subjectKeyID) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    97
        throw new IllegalArgumentException();
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    98
    }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
    99
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
     * This selector overrides the serialNumber matching rules of
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   102
     * X509CertSelector, so it throws IllegalArgumentException if this method
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   103
     * is ever called.
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   104
     */
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   105
    @Override
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   106
    public void setSerialNumber(BigInteger serial) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   107
        throw new IllegalArgumentException();
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   108
    }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   109
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
     * Sets the subjectKeyIdentifier and serialNumber criteria from the
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   112
     * authority key identifier extension.
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   113
     *
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   114
     * The subjectKeyIdentifier criterion is set to the keyIdentifier field
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   115
     * 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
   116
     * 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
   117
     *
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   118
     * 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
   119
     * 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
   120
     * the subject criterion before calling match().
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   121
     *
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   122
     * @param ext the authorityKeyIdentifier extension
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   123
     * @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
   124
     */
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   125
    void setSkiAndSerialNumber(AuthorityKeyIdentifierExtension ext)
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   126
        throws IOException {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   127
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   128
        ski = null;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   129
        serial = null;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   130
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   131
        if (ext != null) {
33820
be91931ea4b2 8072463: Remove requirement that AKID and SKID have to match when building certificate chain
mullan
parents: 29264
diff changeset
   132
            ski = ext.getEncodedKeyIdentifier();
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   133
            SerialNumber asn = (SerialNumber)ext.get(
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   134
                AuthorityKeyIdentifierExtension.SERIAL_NUMBER);
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   135
            if (asn != null) {
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   136
                serial = asn.getNumber();
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   137
            }
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   138
            // the subject criterion should be set by the caller
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   139
        }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   140
    }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   141
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   142
    /**
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   143
     * Decides whether a <code>Certificate</code> should be selected.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   144
     *
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   145
     * This method overrides the matching rules for the subjectKeyIdentifier
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   146
     * and serialNumber criteria and adds additional rules for certificate
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   147
     * validity.
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   148
     *
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   149
     * For the purpose of compatibility, when a certificate is of
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   150
     * 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
   151
     * a subject key identifier extension, the selection criterion
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   152
     * of subjectKeyIdentifier will be disabled.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   153
     */
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   154
    @Override
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   155
    public boolean match(Certificate cert) {
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   156
        X509Certificate xcert = (X509Certificate)cert;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   157
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   158
        // match subject key identifier
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   159
        if (!matchSubjectKeyID(xcert)) {
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   160
            return false;
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
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   163
        // 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
   164
        // 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
   165
        // does not match the replacement root certificate fields.
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   166
        //
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   167
        // 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
   168
        // 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
   169
        // may contain sensitive information (for example, policies), the
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   170
        // 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
   171
        // of key or certificate abuse.
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   172
        int version = xcert.getVersion();
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   173
        if (serial != null && version > 2) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   174
            if (!serial.equals(xcert.getSerialNumber())) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   175
                return false;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   176
            }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   177
        }
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   178
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   179
        // Check the validity period for version 1 and 2 certificate.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   180
        if (version < 3) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   181
            if (startDate != null) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   182
                try {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   183
                    xcert.checkValidity(startDate);
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   184
                } catch (CertificateException ce) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   185
                    return false;
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
            }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   188
            if (endDate != null) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   189
                try {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   190
                    xcert.checkValidity(endDate);
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   191
                } catch (CertificateException ce) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   192
                    return false;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   193
                }
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
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   197
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   198
        if (!super.match(cert)) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   199
            return false;
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   200
        }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   201
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   202
        return true;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   203
    }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   204
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
     * 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
   207
     * 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
   208
     * 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
   209
     */
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   210
    private boolean matchSubjectKeyID(X509Certificate xcert) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   211
        if (ski == null) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   212
            return true;
8790
2a8d836ee007 7025073: Stricter check on trust anchor makes VerifyCACerts.java test fail
xuelei
parents: 8163
diff changeset
   213
        }
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   214
        try {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   215
            byte[] extVal = xcert.getExtensionValue("2.5.29.14");
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   216
            if (extVal == null) {
44920
5b66112437ba 8176457: Add verbose option to java.security.debug
ascarpino
parents: 33820
diff changeset
   217
                if (debug != null && Debug.isVerbose()) {
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   218
                    debug.println("AdaptableX509CertSelector.match: "
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   219
                        + "no subject key ID extension. Subject: "
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   220
                        + xcert.getSubjectX500Principal());
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   221
                }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   222
                return true;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   223
            }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   224
            DerInputStream in = new DerInputStream(extVal);
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   225
            byte[] certSubjectKeyID = in.getOctetString();
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   226
            if (certSubjectKeyID == null ||
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   227
                    !Arrays.equals(ski, certSubjectKeyID)) {
44920
5b66112437ba 8176457: Add verbose option to java.security.debug
ascarpino
parents: 33820
diff changeset
   228
                if (debug != null && Debug.isVerbose()) {
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   229
                    debug.println("AdaptableX509CertSelector.match: "
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   230
                        + "subject key IDs don't match. "
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   231
                        + "Expected: " + Arrays.toString(ski) + " "
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   232
                        + "Cert's: " + Arrays.toString(certSubjectKeyID));
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   233
                }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   234
                return false;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   235
            }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   236
        } catch (IOException ex) {
44920
5b66112437ba 8176457: Add verbose option to java.security.debug
ascarpino
parents: 33820
diff changeset
   237
            if (debug != null && Debug.isVerbose()) {
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   238
                debug.println("AdaptableX509CertSelector.match: "
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   239
                    + "exception in subject key ID check");
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   240
            }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   241
            return false;
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   242
        }
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   243
        return true;
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   244
    }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   245
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   246
    @Override
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   247
    public Object clone() {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   248
        AdaptableX509CertSelector copy =
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   249
                        (AdaptableX509CertSelector)super.clone();
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   250
        if (startDate != null) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   251
            copy.startDate = (Date)startDate.clone();
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
        if (endDate != null) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   255
            copy.endDate = (Date)endDate.clone();
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   256
        }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   257
22977
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   258
        if (ski != null) {
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   259
            copy.ski = ski.clone();
f8f315760941 8025708: Certificate Path Building problem with AKI serial number
mullan
parents: 10336
diff changeset
   260
        }
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   261
        return copy;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   262
    }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents:
diff changeset
   263
}