jdk/src/java.base/share/classes/java/security/cert/X509CertSelector.java
author juh
Tue, 07 Oct 2014 22:23:19 -0700
changeset 26967 c182469301ee
parent 25859 3317bb8137f4
child 29264 5172066a2da6
permissions -rw-r--r--
8037550: Update RFC references in javadoc to RFC 5280 Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security.cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.util.DerInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.util.ObjectIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
    42
 * A {@code CertSelector} that selects {@code X509Certificates} that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * match all specified criteria. This class is particularly useful when
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
    44
 * selecting certificates from a {@code CertStore} to build a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * PKIX-compliant certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
    47
 * When first constructed, an {@code X509CertSelector} has no criteria
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
    48
 * enabled and each of the {@code get} methods return a default value
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
    49
 * ({@code null}, or {@code -1} for the {@link #getBasicConstraints
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * getBasicConstraints} method). Therefore, the {@link #match match}
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
    51
 * method would return {@code true} for any {@code X509Certificate}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Typically, several criteria are enabled (by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@link #setIssuer setIssuer} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * {@link #setKeyUsage setKeyUsage}, for instance) and then the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
    55
 * {@code X509CertSelector} is passed to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * {@link CertStore#getCertificates CertStore.getCertificates} or some similar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Several criteria can be enabled (by calling {@link #setIssuer setIssuer}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * and {@link #setSerialNumber setSerialNumber},
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
    61
 * for example) such that the {@code match} method
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
    62
 * usually uniquely matches a single {@code X509Certificate}. We say
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * usually, since it is possible for two issuing CAs to have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * distinguished name and each issue a certificate with the same serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * number. Other unique combinations include the issuer, subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * subjectKeyIdentifier and/or the subjectPublicKey criteria.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
    68
 * Please refer to <a href="http://tools.ietf.org/html/rfc5280">RFC 5280:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Internet X.509 Public Key Infrastructure Certificate and CRL Profile</a> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * definitions of the X.509 certificate extensions mentioned below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <b>Concurrent Access</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Unless otherwise specified, the methods defined in this class are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * thread-safe. Multiple threads that need to access a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * object concurrently should synchronize amongst themselves and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * provide the necessary locking. Multiple threads each manipulating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * separate objects need not synchronize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @see CertSelector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @see X509Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @author      Steve Hanna
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
public class X509CertSelector implements CertSelector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private final static ObjectIdentifier ANY_EXTENDED_KEY_USAGE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        ObjectIdentifier.newInternal(new int[] {2, 5, 29, 37, 0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        CertPathHelperImpl.initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private BigInteger serialNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private X500Principal issuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private X500Principal subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private byte[] subjectKeyID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private byte[] authorityKeyID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private Date certificateValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private Date privateKeyValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private ObjectIdentifier subjectPublicKeyAlgID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private PublicKey subjectPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private byte[] subjectPublicKeyBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private boolean[] keyUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private Set<String> keyPurposeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private Set<ObjectIdentifier> keyPurposeOIDSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private Set<List<?>> subjectAlternativeNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private Set<GeneralNameInterface> subjectAlternativeGeneralNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private CertificatePolicySet policy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private Set<String> policySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private Set<List<?>> pathToNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private Set<GeneralNameInterface> pathToGeneralNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private NameConstraintsExtension nc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private byte[] ncBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private int basicConstraints = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private X509Certificate x509Cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private boolean matchAllSubjectAltNames = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static final Boolean FALSE = Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private static final int PRIVATE_KEY_USAGE_ID = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private static final int SUBJECT_ALT_NAME_ID = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static final int NAME_CONSTRAINTS_ID = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private static final int CERT_POLICIES_ID = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private static final int EXTENDED_KEY_USAGE_ID = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static final int NUM_OF_EXTENSIONS = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static final String[] EXTENSION_OIDS = new String[NUM_OF_EXTENSIONS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        EXTENSION_OIDS[PRIVATE_KEY_USAGE_ID]  = "2.5.29.16";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        EXTENSION_OIDS[SUBJECT_ALT_NAME_ID]   = "2.5.29.17";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        EXTENSION_OIDS[NAME_CONSTRAINTS_ID]   = "2.5.29.30";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        EXTENSION_OIDS[CERT_POLICIES_ID]      = "2.5.29.32";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        EXTENSION_OIDS[EXTENDED_KEY_USAGE_ID] = "2.5.29.37";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /* Constants representing the GeneralName types */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static final int NAME_ANY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static final int NAME_RFC822 = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    static final int NAME_DNS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    static final int NAME_X400 = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    static final int NAME_DIRECTORY = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    static final int NAME_EDI = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    static final int NAME_URI = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    static final int NAME_IP = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    static final int NAME_OID = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   152
     * Creates an {@code X509CertSelector}. Initially, no criteria are set
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   153
     * so any {@code X509Certificate} will match.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public X509CertSelector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Sets the certificateEquals criterion. The specified
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   161
     * {@code X509Certificate} must be equal to the
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   162
     * {@code X509Certificate} passed to the {@code match} method.
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   163
     * If {@code null}, then this check is not applied.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <p>This method is particularly useful when it is necessary to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * match a single certificate. Although other criteria can be specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * in conjunction with the certificateEquals criterion, it is usually not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * practical or necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   170
     * @param cert the {@code X509Certificate} to match (or
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   171
     * {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @see #getCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public void setCertificate(X509Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        x509Cert = cert;
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
     * Sets the serialNumber criterion. The specified serial number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * must match the certificate serial number in the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   181
     * {@code X509Certificate}. If {@code null}, any certificate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * serial number will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param serial the certificate serial number to match
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   185
     *        (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @see #getSerialNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public void setSerialNumber(BigInteger serial) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        serialNumber = serial;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Sets the issuer criterion. The specified distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * must match the issuer distinguished name in the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   195
     * {@code X509Certificate}. If {@code null}, any issuer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * distinguished name will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param issuer a distinguished name as X500Principal
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   199
     *                 (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public void setIssuer(X500Principal issuer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        this.issuer = issuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <strong>Denigrated</strong>, use {@linkplain #setIssuer(X500Principal)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * or {@linkplain #setIssuer(byte[])} instead. This method should not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * relied on as it can fail to match some certificates because of a loss of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * encoding information in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a> String form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * of some distinguished names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Sets the issuer criterion. The specified distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * must match the issuer distinguished name in the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   216
     * {@code X509Certificate}. If {@code null}, any issuer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * distinguished name will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   219
     * If {@code issuerDN} is not {@code null}, it should contain a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * distinguished name, in RFC 2253 format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param issuerDN a distinguished name in RFC 2253 format
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   223
     *                 (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @throws IOException if a parsing error occurs (incorrect form for DN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public void setIssuer(String issuerDN) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (issuerDN == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            issuer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            issuer = new X500Name(issuerDN).asX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Sets the issuer criterion. The specified distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * must match the issuer distinguished name in the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   237
     * {@code X509Certificate}. If {@code null} is specified,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * the issuer criterion is disabled and any issuer distinguished name will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   241
     * If {@code issuerDN} is not {@code null}, it should contain a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * single DER encoded distinguished name, as defined in X.501. The ASN.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * notation for this structure is as follows.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   244
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Name ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *   RDNSequence }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * RelativeDistinguishedName ::=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *   SET SIZE (1 .. MAX) OF AttributeTypeAndValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * AttributeTypeAndValue ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *   type     AttributeType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *   value    AttributeValue }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * AttributeType ::= OBJECT IDENTIFIER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * AttributeValue ::= ANY DEFINED BY AttributeType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * ....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * DirectoryString ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *       teletexString           TeletexString (SIZE (1..MAX)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *       printableString         PrintableString (SIZE (1..MAX)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *       universalString         UniversalString (SIZE (1..MAX)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *       utf8String              UTF8String (SIZE (1.. MAX)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *       bmpString               BMPString (SIZE (1..MAX)) }
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   267
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Note that the byte array specified here is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param issuerDN a byte array containing the distinguished name
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   273
     *                 in ASN.1 DER encoded form (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @throws IOException if an encoding error occurs (incorrect form for DN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public void setIssuer(byte[] issuerDN) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            issuer = (issuerDN == null ? null : new X500Principal(issuerDN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } catch (IllegalArgumentException e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   280
            throw new IOException("Invalid name", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Sets the subject criterion. The specified distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * must match the subject distinguished name in the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   287
     * {@code X509Certificate}. If {@code null}, any subject
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * distinguished name will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param subject a distinguished name as X500Principal
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   291
     *                  (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public void setSubject(X500Principal subject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        this.subject = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * <strong>Denigrated</strong>, use {@linkplain #setSubject(X500Principal)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * or {@linkplain #setSubject(byte[])} instead. This method should not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * relied on as it can fail to match some certificates because of a loss of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * encoding information in the RFC 2253 String form of some distinguished
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Sets the subject criterion. The specified distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * must match the subject distinguished name in the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   307
     * {@code X509Certificate}. If {@code null}, any subject
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * distinguished name will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   310
     * If {@code subjectDN} is not {@code null}, it should contain a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * distinguished name, in RFC 2253 format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @param subjectDN a distinguished name in RFC 2253 format
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   314
     *                  (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @throws IOException if a parsing error occurs (incorrect form for DN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public void setSubject(String subjectDN) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (subjectDN == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            subject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            subject = new X500Name(subjectDN).asX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Sets the subject criterion. The specified distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * must match the subject distinguished name in the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   328
     * {@code X509Certificate}. If {@code null}, any subject
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * distinguished name will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   331
     * If {@code subjectDN} is not {@code null}, it should contain a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * single DER encoded distinguished name, as defined in X.501. For the ASN.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * notation for this structure, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * {@link #setIssuer(byte [] issuerDN) setIssuer(byte [] issuerDN)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param subjectDN a byte array containing the distinguished name in
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   337
     *                  ASN.1 DER format (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @throws IOException if an encoding error occurs (incorrect form for DN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public void setSubject(byte[] subjectDN) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            subject = (subjectDN == null ? null : new X500Principal(subjectDN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        } catch (IllegalArgumentException e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   344
            throw new IOException("Invalid name", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Sets the subjectKeyIdentifier criterion. The
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   350
     * {@code X509Certificate} must contain a SubjectKeyIdentifier
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * extension for which the contents of the extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * matches the specified criterion value.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   353
     * If the criterion value is {@code null}, no
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * subjectKeyIdentifier check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   356
     * If {@code subjectKeyID} is not {@code null}, it
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * should contain a single DER encoded value corresponding to the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * of the extension value (not including the object identifier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * criticality setting, and encapsulating OCTET STRING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * for a SubjectKeyIdentifier extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * The ASN.1 notation for this structure follows.
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18551
diff changeset
   362
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   363
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * SubjectKeyIdentifier ::= KeyIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * KeyIdentifier ::= OCTET STRING
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   367
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Since the format of subject key identifiers is not mandated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * any standard, subject key identifiers are not parsed by the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   371
     * {@code X509CertSelector}. Instead, the values are compared using
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * a byte-by-byte comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Note that the byte array supplied here is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   377
     * @param subjectKeyID the subject key identifier (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @see #getSubjectKeyIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public void setSubjectKeyIdentifier(byte[] subjectKeyID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (subjectKeyID == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            this.subjectKeyID = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   384
            this.subjectKeyID = subjectKeyID.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Sets the authorityKeyIdentifier criterion. The
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   390
     * {@code X509Certificate} must contain an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * AuthorityKeyIdentifier extension for which the contents of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * extension value matches the specified criterion value.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   393
     * If the criterion value is {@code null}, no
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * authorityKeyIdentifier check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   396
     * If {@code authorityKeyID} is not {@code null}, it
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * should contain a single DER encoded value corresponding to the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * of the extension value (not including the object identifier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * criticality setting, and encapsulating OCTET STRING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * for an AuthorityKeyIdentifier extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * The ASN.1 notation for this structure follows.
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18551
diff changeset
   402
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   403
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * AuthorityKeyIdentifier ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *    keyIdentifier             [0] KeyIdentifier           OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *    authorityCertIssuer       [1] GeneralNames            OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *    authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * KeyIdentifier ::= OCTET STRING
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   410
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * Authority key identifiers are not parsed by the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   413
     * {@code X509CertSelector}.  Instead, the values are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * compared using a byte-by-byte comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   416
     * When the {@code keyIdentifier} field of
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   417
     * {@code AuthorityKeyIdentifier} is populated, the value is
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   418
     * usually taken from the {@code SubjectKeyIdentifier} extension
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * in the issuer's certificate.  Note, however, that the result of
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   420
     * {@code X509Certificate.getExtensionValue(<SubjectKeyIdentifier Object
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   421
     * Identifier>)} on the issuer's certificate may NOT be used
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   422
     * directly as the input to {@code setAuthorityKeyIdentifier}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * This is because the SubjectKeyIdentifier contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * only a KeyIdentifier OCTET STRING, and not a SEQUENCE of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * KeyIdentifier, GeneralNames, and CertificateSerialNumber.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * In order to use the extension value of the issuer certificate's
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   427
     * {@code SubjectKeyIdentifier}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * extension, it will be necessary to extract the value of the embedded
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   429
     * {@code KeyIdentifier} OCTET STRING, then DER encode this OCTET
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * STRING inside a SEQUENCE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * For more details on SubjectKeyIdentifier, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * {@link #setSubjectKeyIdentifier(byte[] subjectKeyID)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * Note also that the byte array supplied here is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @param authorityKeyID the authority key identifier
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   438
     *        (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @see #getAuthorityKeyIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public void setAuthorityKeyIdentifier(byte[] authorityKeyID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (authorityKeyID == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            this.authorityKeyID = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   445
            this.authorityKeyID = authorityKeyID.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * Sets the certificateValid criterion. The specified date must fall
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * within the certificate validity period for the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   452
     * {@code X509Certificate}. If {@code null}, no certificateValid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   455
     * Note that the {@code Date} supplied here is cloned to protect
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   458
     * @param certValid the {@code Date} to check (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @see #getCertificateValid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    public void setCertificateValid(Date certValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (certValid == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            certificateValid = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            certificateValid = (Date)certValid.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Sets the privateKeyValid criterion. The specified date must fall
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * within the private key validity period for the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   472
     * {@code X509Certificate}. If {@code null}, no privateKeyValid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   475
     * Note that the {@code Date} supplied here is cloned to protect
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   478
     * @param privateKeyValid the {@code Date} to check (or
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   479
     *                        {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @see #getPrivateKeyValid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public void setPrivateKeyValid(Date privateKeyValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        if (privateKeyValid == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            this.privateKeyValid = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            this.privateKeyValid = (Date)privateKeyValid.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * Sets the subjectPublicKeyAlgID criterion. The
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   492
     * {@code X509Certificate} must contain a subject public key
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   493
     * with the specified algorithm. If {@code null}, no
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * subjectPublicKeyAlgID check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @param oid The object identifier (OID) of the algorithm to check
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   497
     *            for (or {@code null}). An OID is represented by a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *            set of nonnegative integers separated by periods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @throws IOException if the OID is invalid, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * the first component being not 0, 1 or 2 or the second component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * being greater than 39.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @see #getSubjectPublicKeyAlgID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    public void setSubjectPublicKeyAlgID(String oid) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if (oid == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            subjectPublicKeyAlgID = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            subjectPublicKeyAlgID = new ObjectIdentifier(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * Sets the subjectPublicKey criterion. The
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   515
     * {@code X509Certificate} must contain the specified subject public
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   516
     * key. If {@code null}, no subjectPublicKey check will be done.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   518
     * @param key the subject public key to check for (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @see #getSubjectPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public void setSubjectPublicKey(PublicKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            subjectPublicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            subjectPublicKeyBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            subjectPublicKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            subjectPublicKeyBytes = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   532
     * Sets the subjectPublicKey criterion. The {@code X509Certificate}
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   533
     * must contain the specified subject public key. If {@code null},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * no subjectPublicKey check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * Because this method allows the public key to be specified as a byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * array, it may be used for unknown key types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   539
     * If {@code key} is not {@code null}, it should contain a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * single DER encoded SubjectPublicKeyInfo structure, as defined in X.509.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * The ASN.1 notation for this structure is as follows.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   542
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * SubjectPublicKeyInfo  ::=  SEQUENCE  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *   algorithm            AlgorithmIdentifier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *   subjectPublicKey     BIT STRING  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * AlgorithmIdentifier  ::=  SEQUENCE  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *   algorithm               OBJECT IDENTIFIER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *   parameters              ANY DEFINED BY algorithm OPTIONAL  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *                              -- contains a value of the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *                              -- registered for use with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *                              -- algorithm object identifier value
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   553
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Note that the byte array supplied here is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @param key a byte array containing the subject public key in ASN.1 DER
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   559
     *            form (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @throws IOException if an encoding error occurs (incorrect form for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * subject public key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @see #getSubjectPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    public void setSubjectPublicKey(byte[] key) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            subjectPublicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            subjectPublicKeyBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   569
            subjectPublicKeyBytes = key.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            subjectPublicKey = X509Key.parse(new DerValue(subjectPublicKeyBytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   575
     * Sets the keyUsage criterion. The {@code X509Certificate}
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   576
     * must allow the specified keyUsage values. If {@code null}, no
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   577
     * keyUsage check will be done. Note that an {@code X509Certificate}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * that has no keyUsage extension implicitly allows all keyUsage values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Note that the boolean array supplied here is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @param keyUsage a boolean array in the same format as the boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *                 array returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * {@link X509Certificate#getKeyUsage() X509Certificate.getKeyUsage()}.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   586
     *                 Or {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @see #getKeyUsage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    public void setKeyUsage(boolean[] keyUsage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        if (keyUsage == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            this.keyUsage = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   593
            this.keyUsage = keyUsage.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   598
     * Sets the extendedKeyUsage criterion. The {@code X509Certificate}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * must allow the specified key purposes in its extended key usage
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   600
     * extension. If {@code keyPurposeSet} is empty or {@code null},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * no extendedKeyUsage check will be done. Note that an
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   602
     * {@code X509Certificate} that has no extendedKeyUsage extension
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * implicitly allows all key purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   605
     * Note that the {@code Set} is cloned to protect against
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   608
     * @param keyPurposeSet a {@code Set} of key purpose OIDs in string
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   609
     * format (or {@code null}). Each OID is represented by a set of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * nonnegative integers separated by periods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @throws IOException if the OID is invalid, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * the first component being not 0, 1 or 2 or the second component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * being greater than 39.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @see #getExtendedKeyUsage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    public void setExtendedKeyUsage(Set<String> keyPurposeSet) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        if ((keyPurposeSet == null) || keyPurposeSet.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            this.keyPurposeSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            keyPurposeOIDSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            this.keyPurposeSet =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                Collections.unmodifiableSet(new HashSet<String>(keyPurposeSet));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            keyPurposeOIDSet = new HashSet<ObjectIdentifier>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            for (String s : this.keyPurposeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                keyPurposeOIDSet.add(new ObjectIdentifier(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * Enables/disables matching all of the subjectAlternativeNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * specified in the {@link #setSubjectAlternativeNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * setSubjectAlternativeNames} or {@link #addSubjectAlternativeName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * addSubjectAlternativeName} methods. If enabled,
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   635
     * the {@code X509Certificate} must contain all of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * specified subject alternative names. If disabled, the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   637
     * {@code X509Certificate} must contain at least one of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * specified subject alternative names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   640
     * <p>The matchAllNames flag is {@code true} by default.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   642
     * @param matchAllNames if {@code true}, the flag is enabled;
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   643
     * if {@code false}, the flag is disabled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @see #getMatchAllSubjectAltNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    public void setMatchAllSubjectAltNames(boolean matchAllNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        this.matchAllSubjectAltNames = matchAllNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * Sets the subjectAlternativeNames criterion. The
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   652
     * {@code X509Certificate} must contain all or at least one of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * specified subjectAlternativeNames, depending on the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * the matchAllNames flag (see {@link #setMatchAllSubjectAltNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * setMatchAllSubjectAltNames}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * This method allows the caller to specify, with a single method call,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * the complete set of subject alternative names for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * subjectAlternativeNames criterion. The specified value replaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * the previous value for the subjectAlternativeNames criterion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   662
     * The {@code names} parameter (if not {@code null}) is a
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   663
     * {@code Collection} with one
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * entry for each name to be included in the subject alternative name
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   665
     * criterion. Each entry is a {@code List} whose first entry is an
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   666
     * {@code Integer} (the name type, 0-8) and whose second
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   667
     * entry is a {@code String} or a byte array (the name, in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * string or ASN.1 DER encoded form, respectively).
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   669
     * There can be multiple names of the same type. If {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * is supplied as the value for this argument, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * subjectAlternativeNames check will be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   673
     * Each subject alternative name in the {@code Collection}
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   674
     * may be specified either as a {@code String} or as an ASN.1 encoded
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * byte array. For more details about the formats used, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * {@link #addSubjectAlternativeName(int type, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * addSubjectAlternativeName(int type, String name)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * {@link #addSubjectAlternativeName(int type, byte [] name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * addSubjectAlternativeName(int type, byte [] name)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * <strong>Note:</strong> for distinguished names, specify the byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * array form instead of the String form. See the note in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * {@link #addSubjectAlternativeName(int, String)} for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   685
     * Note that the {@code names} parameter can contain duplicate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * names (same name and name type), but they may be removed from the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   687
     * {@code Collection} of names returned by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * {@link #getSubjectAlternativeNames getSubjectAlternativeNames} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   690
     * Note that a deep copy is performed on the {@code Collection} to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * protect against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   693
     * @param names a {@code Collection} of names (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @throws IOException if a parsing error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @see #getSubjectAlternativeNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    public void setSubjectAlternativeNames(Collection<List<?>> names)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        if (names == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            subjectAlternativeNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            subjectAlternativeGeneralNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            if (names.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                subjectAlternativeNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                subjectAlternativeGeneralNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            Set<List<?>> tempNames = cloneAndCheckNames(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            // Ensure that we either set both of these or neither
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            subjectAlternativeGeneralNames = parseNames(tempNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            subjectAlternativeNames = tempNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * Adds a name to the subjectAlternativeNames criterion. The
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   717
     * {@code X509Certificate} must contain all or at least one
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * of the specified subjectAlternativeNames, depending on the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * the matchAllNames flag (see {@link #setMatchAllSubjectAltNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * setMatchAllSubjectAltNames}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * This method allows the caller to add a name to the set of subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * alternative names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * The specified name is added to any previous value for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * subjectAlternativeNames criterion. If the specified name is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * duplicate, it may be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * The name is provided in string format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * <a href="http://www.ietf.org/rfc/rfc822.txt">RFC 822</a>, DNS, and URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * names use the well-established string formats for those types (subject to
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   731
     * the restrictions included in RFC 5280). IPv4 address names are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * supplied using dotted quad notation. OID address names are represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * as a series of nonnegative integers separated by periods. And
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * directory names (distinguished names) are supplied in RFC 2253 format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * No standard string format is defined for otherNames, X.400 names,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * EDI party names, IPv6 address names, or any other type of names. They
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * should be specified using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * {@link #addSubjectAlternativeName(int type, byte [] name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * addSubjectAlternativeName(int type, byte [] name)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * <strong>Note:</strong> for distinguished names, use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * {@linkplain #addSubjectAlternativeName(int, byte[])} instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * This method should not be relied on as it can fail to match some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * certificates because of a loss of encoding information in the RFC 2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * String form of some distinguished names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * @param type the name type (0-8, as specified in
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   749
     *             RFC 5280, section 4.2.1.6)
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   750
     * @param name the name in string form (not {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * @throws IOException if a parsing error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    public void addSubjectAlternativeName(int type, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        addSubjectAlternativeNameInternal(type, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * Adds a name to the subjectAlternativeNames criterion. The
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   760
     * {@code X509Certificate} must contain all or at least one
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * of the specified subjectAlternativeNames, depending on the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * the matchAllNames flag (see {@link #setMatchAllSubjectAltNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * setMatchAllSubjectAltNames}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * This method allows the caller to add a name to the set of subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * alternative names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * The specified name is added to any previous value for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * subjectAlternativeNames criterion. If the specified name is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * duplicate, it may be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * The name is provided as a byte array. This byte array should contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * the DER encoded name, as it would appear in the GeneralName structure
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   773
     * defined in RFC 5280 and X.509. The encoded byte array should only contain
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * the encoded value of the name, and should not include the tag associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * with the name in the GeneralName structure. The ASN.1 definition of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * structure appears below.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   777
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     *  GeneralName ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *       otherName                       [0]     OtherName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     *       rfc822Name                      [1]     IA5String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *       dNSName                         [2]     IA5String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *       x400Address                     [3]     ORAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *       directoryName                   [4]     Name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *       ediPartyName                    [5]     EDIPartyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *       uniformResourceIdentifier       [6]     IA5String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *       iPAddress                       [7]     OCTET STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *       registeredID                    [8]     OBJECT IDENTIFIER}
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   788
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * Note that the byte array supplied here is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * @param type the name type (0-8, as listed above)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * @param name a byte array containing the name in ASN.1 DER encoded form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @throws IOException if a parsing error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    public void addSubjectAlternativeName(int type, byte[] name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        // clone because byte arrays are modifiable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        addSubjectAlternativeNameInternal(type, name.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * A private method that adds a name (String or byte array) to the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   805
     * subjectAlternativeNames criterion. The {@code X509Certificate}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * must contain the specified subjectAlternativeName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * @param type the name type (0-8, as specified in
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   809
     *             RFC 5280, section 4.2.1.6)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @param name the name in string or byte array form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @throws IOException if a parsing error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    private void addSubjectAlternativeNameInternal(int type, Object name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        // First, ensure that the name parses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        GeneralNameInterface tempName = makeGeneralNameInterface(type, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        if (subjectAlternativeNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            subjectAlternativeNames = new HashSet<List<?>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        if (subjectAlternativeGeneralNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            subjectAlternativeGeneralNames = new HashSet<GeneralNameInterface>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        List<Object> list = new ArrayList<Object>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        list.add(Integer.valueOf(type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        list.add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        subjectAlternativeNames.add(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        subjectAlternativeGeneralNames.add(tempName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * Parse an argument of the form passed to setSubjectAlternativeNames,
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   832
     * returning a {@code Collection} of
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   833
     * {@code GeneralNameInterface}s.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * Throw an IllegalArgumentException or a ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * if the argument is malformed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @param names a Collection with one entry per name.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   838
     *              Each entry is a {@code List} whose first entry
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *              is an Integer (the name type, 0-8) and whose second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *              entry is a String or a byte array (the name, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     *              string or ASN.1 DER encoded form, respectively).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *              There can be multiple names of the same type. Null is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *              not an acceptable value.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   844
     * @return a Set of {@code GeneralNameInterface}s
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @throws IOException if a parsing error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    private static Set<GeneralNameInterface> parseNames(Collection<List<?>> names) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        Set<GeneralNameInterface> genNames = new HashSet<GeneralNameInterface>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        for (List<?> nameList : names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            if (nameList.size() != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                throw new IOException("name list size not 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            Object o =  nameList.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            if (!(o instanceof Integer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                throw new IOException("expected an Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            int nameType = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            o = nameList.get(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            genNames.add(makeGeneralNameInterface(nameType, o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        return genNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * Compare for equality two objects of the form passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * setSubjectAlternativeNames (or X509CRLSelector.setIssuerNames).
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   868
     * Throw an {@code IllegalArgumentException} or a
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   869
     * {@code ClassCastException} if one of the objects is malformed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * @param object1 a Collection containing the first object to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @param object2 a Collection containing the second object to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * @return true if the objects are equal, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   875
    static boolean equalNames(Collection<?> object1, Collection<?> object2) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        if ((object1 == null) || (object2 == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            return object1 == object2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        return object1.equals(object2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   883
     * Make a {@code GeneralNameInterface} out of a name type (0-8) and an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * Object that may be a byte array holding the ASN.1 DER encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * name or a String form of the name.  Except for X.509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * Distinguished Names, the String form of the name must not be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * result from calling toString on an existing GeneralNameInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * implementing class.  The output of toString is not compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * with the String constructors for names other than Distinguished
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * Names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @param type name type (0-8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @param name name as ASN.1 Der-encoded byte array or String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @return a GeneralNameInterface name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @throws IOException if a parsing error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    static GeneralNameInterface makeGeneralNameInterface(int type, Object name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        GeneralNameInterface result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            debug.println("X509CertSelector.makeGeneralNameInterface("
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                + type + ")...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        if (name instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                debug.println("X509CertSelector.makeGeneralNameInterface() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                    + "name is String: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            case NAME_RFC822:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                result = new RFC822Name((String)name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            case NAME_DNS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                result = new DNSName((String)name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            case NAME_DIRECTORY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                result = new X500Name((String)name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            case NAME_URI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                result = new URIName((String)name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            case NAME_IP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                result = new IPAddressName((String)name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            case NAME_OID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                result = new OIDName((String)name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                throw new IOException("unable to parse String names of type "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                                      + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                debug.println("X509CertSelector.makeGeneralNameInterface() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                    + "result: " + result.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        } else if (name instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            DerValue val = new DerValue((byte[]) name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                debug.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                    ("X509CertSelector.makeGeneralNameInterface() is byte[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            case NAME_ANY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                result = new OtherName(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            case NAME_RFC822:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                result = new RFC822Name(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            case NAME_DNS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                result = new DNSName(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            case NAME_X400:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                result = new X400Address(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            case NAME_DIRECTORY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                result = new X500Name(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            case NAME_EDI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                result = new EDIPartyName(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            case NAME_URI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                result = new URIName(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            case NAME_IP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                result = new IPAddressName(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            case NAME_OID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                result = new OIDName(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                throw new IOException("unable to parse byte array names of "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                    + "type " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                debug.println("X509CertSelector.makeGeneralNameInterface() result: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                    + result.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                debug.println("X509CertSelector.makeGeneralName() input name "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                    + "not String or byte array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            throw new IOException("name not String or byte array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
   992
     * Sets the name constraints criterion. The {@code X509Certificate}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * must have subject and subject alternative names that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * meet the specified name constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * The name constraints are specified as a byte array. This byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * should contain the DER encoded form of the name constraints, as they
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   998
     * would appear in the NameConstraints structure defined in RFC 5280
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * and X.509. The ASN.1 definition of this structure appears below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1001
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *  NameConstraints ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     *       permittedSubtrees       [0]     GeneralSubtrees OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     *       excludedSubtrees        [1]     GeneralSubtrees OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     *  GeneralSubtrees ::= SEQUENCE SIZE (1..MAX) OF GeneralSubtree
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     *  GeneralSubtree ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *       base                    GeneralName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *       minimum         [0]     BaseDistance DEFAULT 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     *       maximum         [1]     BaseDistance OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     *  BaseDistance ::= INTEGER (0..MAX)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     *  GeneralName ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     *       otherName                       [0]     OtherName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     *       rfc822Name                      [1]     IA5String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     *       dNSName                         [2]     IA5String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     *       x400Address                     [3]     ORAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *       directoryName                   [4]     Name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     *       ediPartyName                    [5]     EDIPartyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *       uniformResourceIdentifier       [6]     IA5String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *       iPAddress                       [7]     OCTET STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     *       registeredID                    [8]     OBJECT IDENTIFIER}
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1025
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * Note that the byte array supplied here is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * @param bytes a byte array containing the ASN.1 DER encoding of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     *              a NameConstraints extension to be used for checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     *              name constraints. Only the value of the extension is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *              included, not the OID or criticality flag. Can be
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1034
     *              {@code null},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     *              in which case no name constraints check will be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * @throws IOException if a parsing error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * @see #getNameConstraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    public void setNameConstraints(byte[] bytes) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        if (bytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            ncBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            nc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1044
            ncBytes = bytes.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            nc = new NameConstraintsExtension(FALSE, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * Sets the basic constraints constraint. If the value is greater than or
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1051
     * equal to zero, {@code X509Certificates} must include a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * basicConstraints extension with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * a pathLen of at least this value. If the value is -2, only end-entity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * certificates are accepted. If the value is -1, no check is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * This constraint is useful when building a certification path forward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * (from the target toward the trust anchor. If a partial path has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * built, any candidate certificate must have a maxPathLen value greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * than or equal to the number of certificates in the partial path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * @param minMaxPathLen the value for the basic constraints constraint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @throws IllegalArgumentException if the value is less than -2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * @see #getBasicConstraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    public void setBasicConstraints(int minMaxPathLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        if (minMaxPathLen < -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            throw new IllegalArgumentException("basic constraints less than -2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        basicConstraints = minMaxPathLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1073
     * Sets the policy constraint. The {@code X509Certificate} must
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * include at least one of the specified policies in its certificate
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1075
     * policies extension. If {@code certPolicySet} is empty, then the
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1076
     * {@code X509Certificate} must include at least some specified policy
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1077
     * in its certificate policies extension. If {@code certPolicySet} is
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1078
     * {@code null}, no policy check will be performed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1080
     * Note that the {@code Set} is cloned to protect against
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1083
     * @param certPolicySet a {@code Set} of certificate policy OIDs in
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1084
     *                      string format (or {@code null}). Each OID is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     *                      represented by a set of nonnegative integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     *                    separated by periods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * @throws IOException if a parsing error occurs on the OID such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * the first component is not 0, 1 or 2 or the second component is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * greater than 39.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * @see #getPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    public void setPolicy(Set<String> certPolicySet) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        if (certPolicySet == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            policySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            policy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            // Snapshot set and parse it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            Set<String> tempSet = Collections.unmodifiableSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                                        (new HashSet<String>(certPolicySet));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            /* Convert to Vector of ObjectIdentifiers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            Iterator<String> i = tempSet.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            Vector<CertificatePolicyId> polIdVector = new Vector<CertificatePolicyId>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                Object o = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                if (!(o instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                    throw new IOException("non String in certPolicySet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                polIdVector.add(new CertificatePolicyId(new ObjectIdentifier(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                  (String)o)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            // If everything went OK, make the changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            policySet = tempSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            policy = new CertificatePolicySet(polIdVector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1118
     * Sets the pathToNames criterion. The {@code X509Certificate} must
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * not include name constraints that would prohibit building a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * path to the specified names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * This method allows the caller to specify, with a single method call,
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1123
     * the complete set of names which the {@code X509Certificates}'s
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * name constraints must permit. The specified value replaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * the previous value for the pathToNames criterion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * This constraint is useful when building a certification path forward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * (from the target toward the trust anchor. If a partial path has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * built, any candidate certificate must not include name constraints that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * would prohibit building a path to any of the names in the partial path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1132
     * The {@code names} parameter (if not {@code null}) is a
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1133
     * {@code Collection} with one
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * entry for each name to be included in the pathToNames
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1135
     * criterion. Each entry is a {@code List} whose first entry is an
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1136
     * {@code Integer} (the name type, 0-8) and whose second
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1137
     * entry is a {@code String} or a byte array (the name, in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * string or ASN.1 DER encoded form, respectively).
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1139
     * There can be multiple names of the same type. If {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * is supplied as the value for this argument, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * pathToNames check will be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1143
     * Each name in the {@code Collection}
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1144
     * may be specified either as a {@code String} or as an ASN.1 encoded
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * byte array. For more details about the formats used, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * {@link #addPathToName(int type, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * addPathToName(int type, String name)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * {@link #addPathToName(int type, byte [] name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * addPathToName(int type, byte [] name)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * <strong>Note:</strong> for distinguished names, specify the byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * array form instead of the String form. See the note in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * {@link #addPathToName(int, String)} for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1155
     * Note that the {@code names} parameter can contain duplicate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * names (same name and name type), but they may be removed from the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1157
     * {@code Collection} of names returned by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * {@link #getPathToNames getPathToNames} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1160
     * Note that a deep copy is performed on the {@code Collection} to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * protect against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1163
     * @param names a {@code Collection} with one entry per name
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1164
     *              (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * @throws IOException if a parsing error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * @see #getPathToNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    public void setPathToNames(Collection<List<?>> names) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        if ((names == null) || names.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            pathToNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            pathToGeneralNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            Set<List<?>> tempNames = cloneAndCheckNames(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            pathToGeneralNames = parseNames(tempNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            // Ensure that we either set both of these or neither
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            pathToNames = tempNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    // called from CertPathHelper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    void setPathToNamesInternal(Set<GeneralNameInterface> names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        // set names to non-null dummy value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        // this breaks getPathToNames()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        pathToNames = Collections.<List<?>>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        pathToGeneralNames = names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1189
     * Adds a name to the pathToNames criterion. The {@code X509Certificate}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * must not include name constraints that would prohibit building a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * path to the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * This method allows the caller to add a name to the set of names which
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1194
     * the {@code X509Certificates}'s name constraints must permit.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * The specified name is added to any previous value for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * pathToNames criterion.  If the name is a duplicate, it may be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * The name is provided in string format. RFC 822, DNS, and URI names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * use the well-established string formats for those types (subject to
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
  1200
     * the restrictions included in RFC 5280). IPv4 address names are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * supplied using dotted quad notation. OID address names are represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * as a series of nonnegative integers separated by periods. And
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * directory names (distinguished names) are supplied in RFC 2253 format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * No standard string format is defined for otherNames, X.400 names,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * EDI party names, IPv6 address names, or any other type of names. They
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * should be specified using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * {@link #addPathToName(int type, byte [] name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * addPathToName(int type, byte [] name)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * <strong>Note:</strong> for distinguished names, use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * {@linkplain #addPathToName(int, byte[])} instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * This method should not be relied on as it can fail to match some
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * certificates because of a loss of encoding information in the RFC 2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * String form of some distinguished names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * @param type the name type (0-8, as specified in
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
  1217
     *             RFC 5280, section 4.2.1.6)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * @param name the name in string form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * @throws IOException if a parsing error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    public void addPathToName(int type, String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        addPathToNameInternal(type, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1226
     * Adds a name to the pathToNames criterion. The {@code X509Certificate}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * must not include name constraints that would prohibit building a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * path to the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * This method allows the caller to add a name to the set of names which
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1231
     * the {@code X509Certificates}'s name constraints must permit.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * The specified name is added to any previous value for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * pathToNames criterion. If the name is a duplicate, it may be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * The name is provided as a byte array. This byte array should contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * the DER encoded name, as it would appear in the GeneralName structure
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
  1237
     * defined in RFC 5280 and X.509. The ASN.1 definition of this structure
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * appears in the documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * {@link #addSubjectAlternativeName(int type, byte [] name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * addSubjectAlternativeName(int type, byte [] name)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * Note that the byte array supplied here is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * @param type the name type (0-8, as specified in
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
  1246
     *             RFC 5280, section 4.2.1.6)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * @param name a byte array containing the name in ASN.1 DER encoded form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * @throws IOException if a parsing error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    public void addPathToName(int type, byte [] name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        // clone because byte arrays are modifiable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        addPathToNameInternal(type, name.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * A private method that adds a name (String or byte array) to the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1257
     * pathToNames criterion. The {@code X509Certificate} must contain
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * the specified pathToName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * @param type the name type (0-8, as specified in
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
  1261
     *             RFC 5280, section 4.2.1.6)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * @param name the name in string or byte array form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * @throws IOException if an encoding error occurs (incorrect form for DN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    private void addPathToNameInternal(int type, Object name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        // First, ensure that the name parses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        GeneralNameInterface tempName = makeGeneralNameInterface(type, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        if (pathToGeneralNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            pathToNames = new HashSet<List<?>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            pathToGeneralNames = new HashSet<GeneralNameInterface>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        List<Object> list = new ArrayList<Object>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        list.add(Integer.valueOf(type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        list.add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        pathToNames.add(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        pathToGeneralNames.add(tempName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * Returns the certificateEquals criterion. The specified
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1282
     * {@code X509Certificate} must be equal to the
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1283
     * {@code X509Certificate} passed to the {@code match} method.
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1284
     * If {@code null}, this check is not applied.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1286
     * @return the {@code X509Certificate} to match (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * @see #setCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    public X509Certificate getCertificate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        return x509Cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * Returns the serialNumber criterion. The specified serial number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * must match the certificate serial number in the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1296
     * {@code X509Certificate}. If {@code null}, any certificate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * serial number will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * @return the certificate serial number to match
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1300
     *                (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * @see #setSerialNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    public BigInteger getSerialNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        return serialNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1308
     * Returns the issuer criterion as an {@code X500Principal}. This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * distinguished name must match the issuer distinguished name in the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1310
     * {@code X509Certificate}. If {@code null}, the issuer criterion
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * is disabled and any issuer distinguished name will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * @return the required issuer distinguished name as X500Principal
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1314
     *         (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    public X500Principal getIssuer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        return issuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * <strong>Denigrated</strong>, use {@linkplain #getIssuer()} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * {@linkplain #getIssuerAsBytes()} instead. This method should not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * relied on as it can fail to match some certificates because of a loss of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * encoding information in the RFC 2253 String form of some distinguished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1328
     * Returns the issuer criterion as a {@code String}. This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * distinguished name must match the issuer distinguished name in the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1330
     * {@code X509Certificate}. If {@code null}, the issuer criterion
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * is disabled and any issuer distinguished name will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1333
     * If the value returned is not {@code null}, it is a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * distinguished name, in RFC 2253 format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * @return the required issuer distinguished name in RFC 2253 format
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1337
     *         (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    public String getIssuerAsString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        return (issuer == null ? null : issuer.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * Returns the issuer criterion as a byte array. This distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * must match the issuer distinguished name in the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1346
     * {@code X509Certificate}. If {@code null}, the issuer criterion
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * is disabled and any issuer distinguished name will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1349
     * If the value returned is not {@code null}, it is a byte
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * array containing a single DER encoded distinguished name, as defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * X.501. The ASN.1 notation for this structure is supplied in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * {@link #setIssuer(byte [] issuerDN) setIssuer(byte [] issuerDN)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * Note that the byte array returned is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * @return a byte array containing the required issuer distinguished name
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1359
     *         in ASN.1 DER format (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * @throws IOException if an encoding error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    public byte[] getIssuerAsBytes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        return (issuer == null ? null: issuer.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1367
     * Returns the subject criterion as an {@code X500Principal}. This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * distinguished name must match the subject distinguished name in the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1369
     * {@code X509Certificate}. If {@code null}, the subject criterion
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * is disabled and any subject distinguished name will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * @return the required subject distinguished name as X500Principal
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1373
     *         (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    public X500Principal getSubject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        return subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * <strong>Denigrated</strong>, use {@linkplain #getSubject()} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     * {@linkplain #getSubjectAsBytes()} instead. This method should not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * relied on as it can fail to match some certificates because of a loss of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * encoding information in the RFC 2253 String form of some distinguished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1387
     * Returns the subject criterion as a {@code String}. This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * distinguished name must match the subject distinguished name in the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1389
     * {@code X509Certificate}. If {@code null}, the subject criterion
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * is disabled and any subject distinguished name will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1392
     * If the value returned is not {@code null}, it is a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * distinguished name, in RFC 2253 format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * @return the required subject distinguished name in RFC 2253 format
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1396
     *         (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
    public String getSubjectAsString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        return (subject == null ? null : subject.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * Returns the subject criterion as a byte array. This distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * must match the subject distinguished name in the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1405
     * {@code X509Certificate}. If {@code null}, the subject criterion
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * is disabled and any subject distinguished name will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1408
     * If the value returned is not {@code null}, it is a byte
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     * array containing a single DER encoded distinguished name, as defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     * X.501. The ASN.1 notation for this structure is supplied in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * {@link #setSubject(byte [] subjectDN) setSubject(byte [] subjectDN)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * Note that the byte array returned is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * @return a byte array containing the required subject distinguished name
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1418
     *         in ASN.1 DER format (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * @throws IOException if an encoding error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    public byte[] getSubjectAsBytes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        return (subject == null ? null : subject.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * Returns the subjectKeyIdentifier criterion. The
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1427
     * {@code X509Certificate} must contain a SubjectKeyIdentifier
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1428
     * extension with the specified value. If {@code null}, no
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * subjectKeyIdentifier check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * Note that the byte array returned is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1434
     * @return the key identifier (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * @see #setSubjectKeyIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
    public byte[] getSubjectKeyIdentifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        if (subjectKeyID == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1441
        return subjectKeyID.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * Returns the authorityKeyIdentifier criterion. The
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1446
     * {@code X509Certificate} must contain a AuthorityKeyIdentifier
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1447
     * extension with the specified value. If {@code null}, no
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * authorityKeyIdentifier check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * Note that the byte array returned is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1453
     * @return the key identifier (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * @see #setAuthorityKeyIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    public byte[] getAuthorityKeyIdentifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        if (authorityKeyID == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
          return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1460
        return authorityKeyID.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * Returns the certificateValid criterion. The specified date must fall
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * within the certificate validity period for the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1466
     * {@code X509Certificate}. If {@code null}, no certificateValid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1469
     * Note that the {@code Date} returned is cloned to protect against
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1472
     * @return the {@code Date} to check (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * @see #setCertificateValid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    public Date getCertificateValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        if (certificateValid == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        return (Date)certificateValid.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * Returns the privateKeyValid criterion. The specified date must fall
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * within the private key validity period for the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1485
     * {@code X509Certificate}. If {@code null}, no privateKeyValid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1488
     * Note that the {@code Date} returned is cloned to protect against
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1491
     * @return the {@code Date} to check (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * @see #setPrivateKeyValid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    public Date getPrivateKeyValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        if (privateKeyValid == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        return (Date)privateKeyValid.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     * Returns the subjectPublicKeyAlgID criterion. The
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1503
     * {@code X509Certificate} must contain a subject public key
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1504
     * with the specified algorithm. If {@code null}, no
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * subjectPublicKeyAlgID check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     * @return the object identifier (OID) of the signature algorithm to check
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1508
     *         for (or {@code null}). An OID is represented by a set of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     *         nonnegative integers separated by periods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * @see #setSubjectPublicKeyAlgID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    public String getSubjectPublicKeyAlgID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        if (subjectPublicKeyAlgID == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        return subjectPublicKeyAlgID.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * Returns the subjectPublicKey criterion. The
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1521
     * {@code X509Certificate} must contain the specified subject
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1522
     * public key. If {@code null}, no subjectPublicKey check will be done.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1524
     * @return the subject public key to check for (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * @see #setSubjectPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    public PublicKey getSubjectPublicKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        return subjectPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1532
     * Returns the keyUsage criterion. The {@code X509Certificate}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * must allow the specified keyUsage values. If null, no keyUsage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * Note that the boolean array returned is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * @return a boolean array in the same format as the boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     *                 array returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * {@link X509Certificate#getKeyUsage() X509Certificate.getKeyUsage()}.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1542
     *                 Or {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     * @see #setKeyUsage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
    public boolean[] getKeyUsage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        if (keyUsage == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1549
        return keyUsage.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1553
     * Returns the extendedKeyUsage criterion. The {@code X509Certificate}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * must allow the specified key purposes in its extended key usage
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1555
     * extension. If the {@code keyPurposeSet} returned is empty or
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1556
     * {@code null}, no extendedKeyUsage check will be done. Note that an
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1557
     * {@code X509Certificate} that has no extendedKeyUsage extension
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * implicitly allows all key purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1560
     * @return an immutable {@code Set} of key purpose OIDs in string
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1561
     * format (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * @see #setExtendedKeyUsage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    public Set<String> getExtendedKeyUsage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        return keyPurposeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1569
     * Indicates if the {@code X509Certificate} must contain all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * or at least one of the subjectAlternativeNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * specified in the {@link #setSubjectAlternativeNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * setSubjectAlternativeNames} or {@link #addSubjectAlternativeName
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1573
     * addSubjectAlternativeName} methods. If {@code true},
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1574
     * the {@code X509Certificate} must contain all of the
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1575
     * specified subject alternative names. If {@code false}, the
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1576
     * {@code X509Certificate} must contain at least one of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * specified subject alternative names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1579
     * @return {@code true} if the flag is enabled;
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1580
     * {@code false} if the flag is disabled. The flag is
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1581
     * {@code true} by default.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     * @see #setMatchAllSubjectAltNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
    public boolean getMatchAllSubjectAltNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        return matchAllSubjectAltNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * Returns a copy of the subjectAlternativeNames criterion.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1590
     * The {@code X509Certificate} must contain all or at least one
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * of the specified subjectAlternativeNames, depending on the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * of the matchAllNames flag (see {@link #getMatchAllSubjectAltNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * getMatchAllSubjectAltNames}). If the value returned is
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1594
     * {@code null}, no subjectAlternativeNames check will be performed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1596
     * If the value returned is not {@code null}, it is a
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1597
     * {@code Collection} with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     * one entry for each name to be included in the subject alternative name
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1599
     * criterion. Each entry is a {@code List} whose first entry is an
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1600
     * {@code Integer} (the name type, 0-8) and whose second
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1601
     * entry is a {@code String} or a byte array (the name, in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * string or ASN.1 DER encoded form, respectively).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * There can be multiple names of the same type.  Note that the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1604
     * {@code Collection} returned may contain duplicate names (same name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * and name type).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1607
     * Each subject alternative name in the {@code Collection}
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1608
     * may be specified either as a {@code String} or as an ASN.1 encoded
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * byte array. For more details about the formats used, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * {@link #addSubjectAlternativeName(int type, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * addSubjectAlternativeName(int type, String name)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     * {@link #addSubjectAlternativeName(int type, byte [] name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     * addSubjectAlternativeName(int type, byte [] name)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1615
     * Note that a deep copy is performed on the {@code Collection} to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * protect against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1618
     * @return a {@code Collection} of names (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * @see #setSubjectAlternativeNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
    public Collection<List<?>> getSubjectAlternativeNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        if (subjectAlternativeNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        return cloneNames(subjectAlternativeNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * Clone an object of the form passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * setSubjectAlternativeNames and setPathToNames.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1631
     * Throw a {@code RuntimeException} if the argument is malformed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * This method wraps cloneAndCheckNames, changing any
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1634
     * {@code IOException} into a {@code RuntimeException}. This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     * method should be used when the object being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * cloned has already been checked, so there should never be any exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1638
     * @param names a {@code Collection} with one entry per name.
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1639
     *              Each entry is a {@code List} whose first entry
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     *              is an Integer (the name type, 0-8) and whose second
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     *              entry is a String or a byte array (the name, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     *              string or ASN.1 DER encoded form, respectively).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     *              There can be multiple names of the same type. Null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     *              is not an acceptable value.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1645
     * @return a deep copy of the specified {@code Collection}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     * @throws RuntimeException if a parsing error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    private static Set<List<?>> cloneNames(Collection<List<?>> names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            return cloneAndCheckNames(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            throw new RuntimeException("cloneNames encountered IOException: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                                       e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     * Clone and check an argument of the form passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     * setSubjectAlternativeNames and setPathToNames.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1660
     * Throw an {@code IOException} if the argument is malformed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1662
     * @param names a {@code Collection} with one entry per name.
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1663
     *              Each entry is a {@code List} whose first entry
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     *              is an Integer (the name type, 0-8) and whose second
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     *              entry is a String or a byte array (the name, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     *              string or ASN.1 DER encoded form, respectively).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     *              There can be multiple names of the same type.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1668
     *              {@code null} is not an acceptable value.
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1669
     * @return a deep copy of the specified {@code Collection}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * @throws IOException if a parsing error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    private static Set<List<?>> cloneAndCheckNames(Collection<List<?>> names) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        // Copy the Lists and Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        Set<List<?>> namesCopy = new HashSet<List<?>>();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1675
        for (List<?> o : names)
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1676
        {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1677
            namesCopy.add(new ArrayList<Object>(o));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        // Check the contents of the Lists and clone any byte arrays
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1681
        for (List<?> list : namesCopy) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1682
            @SuppressWarnings("unchecked") // See javadoc for parameter "names".
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1683
            List<Object> nameList = (List<Object>)list;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            if (nameList.size() != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                throw new IOException("name list size not 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            Object o = nameList.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            if (!(o instanceof Integer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                throw new IOException("expected an Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            int nameType = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            if ((nameType < 0) || (nameType > 8)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                throw new IOException("name type not 0-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            Object nameObject = nameList.get(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            if (!(nameObject instanceof byte[]) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                !(nameObject instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                    debug.println("X509CertSelector.cloneAndCheckNames() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                        + "name not byte array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                throw new IOException("name not byte array or String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            if (nameObject instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                nameList.set(1, ((byte[]) nameObject).clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        return namesCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1712
     * Returns the name constraints criterion. The {@code X509Certificate}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * must have subject and subject alternative names that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * meet the specified name constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * The name constraints are returned as a byte array. This byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     * contains the DER encoded form of the name constraints, as they
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
  1718
     * would appear in the NameConstraints structure defined in RFC 5280
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     * and X.509. The ASN.1 notation for this structure is supplied in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     * {@link #setNameConstraints(byte [] bytes) setNameConstraints(byte [] bytes)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     * Note that the byte array returned is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     * @return a byte array containing the ASN.1 DER encoding of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     *         a NameConstraints extension used for checking name constraints.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1728
     *         {@code null} if no name constraints check will be performed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     * @see #setNameConstraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
    public byte[] getNameConstraints() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        if (ncBytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1735
            return ncBytes.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     * Returns the basic constraints constraint. If the value is greater than
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1741
     * or equal to zero, the {@code X509Certificates} must include a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * basicConstraints extension with a pathLen of at least this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     * If the value is -2, only end-entity certificates are accepted. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * the value is -1, no basicConstraints check is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * @return the value for the basic constraints constraint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     * @see #setBasicConstraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
    public int getBasicConstraints() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        return basicConstraints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1754
     * Returns the policy criterion. The {@code X509Certificate} must
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * include at least one of the specified policies in its certificate policies
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1756
     * extension. If the {@code Set} returned is empty, then the
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1757
     * {@code X509Certificate} must include at least some specified policy
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1758
     * in its certificate policies extension. If the {@code Set} returned is
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1759
     * {@code null}, no policy check will be performed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1761
     * @return an immutable {@code Set} of certificate policy OIDs in
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1762
     *         string format (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * @see #setPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
    public Set<String> getPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        return policySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * Returns a copy of the pathToNames criterion. The
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1771
     * {@code X509Certificate} must not include name constraints that would
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * prohibit building a path to the specified names. If the value
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1773
     * returned is {@code null}, no pathToNames check will be performed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1775
     * If the value returned is not {@code null}, it is a
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1776
     * {@code Collection} with one
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * entry for each name to be included in the pathToNames
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1778
     * criterion. Each entry is a {@code List} whose first entry is an
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1779
     * {@code Integer} (the name type, 0-8) and whose second
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1780
     * entry is a {@code String} or a byte array (the name, in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     * string or ASN.1 DER encoded form, respectively).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     * There can be multiple names of the same type. Note that the
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1783
     * {@code Collection} returned may contain duplicate names (same
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     * name and name type).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1786
     * Each name in the {@code Collection}
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1787
     * may be specified either as a {@code String} or as an ASN.1 encoded
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     * byte array. For more details about the formats used, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * {@link #addPathToName(int type, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * addPathToName(int type, String name)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     * {@link #addPathToName(int type, byte [] name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * addPathToName(int type, byte [] name)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * <p>
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1794
     * Note that a deep copy is performed on the {@code Collection} to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * protect against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1797
     * @return a {@code Collection} of names (or {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * @see #setPathToNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
    public Collection<List<?>> getPathToNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        if (pathToNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        return cloneNames(pathToNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1808
     * Return a printable representation of the {@code CertSelector}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1810
     * @return a {@code String} describing the contents of the
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1811
     *         {@code CertSelector}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    public String toString() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 21334
diff changeset
  1814
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        sb.append("X509CertSelector: [\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        if (x509Cert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
            sb.append("  Certificate: " + x509Cert.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        if (serialNumber != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            sb.append("  Serial Number: " + serialNumber.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        if (issuer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
            sb.append("  Issuer: " + getIssuerAsString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        if (subject != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            sb.append("  Subject: " + getSubjectAsString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        sb.append("  matchAllSubjectAltNames flag: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                  + String.valueOf(matchAllSubjectAltNames) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        if (subjectAlternativeNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
            sb.append("  SubjectAlternativeNames:\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            Iterator<List<?>> i = subjectAlternativeNames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                List<?> list = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                sb.append("    type " + list.get(0) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                          ", name " + list.get(1) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        if (subjectKeyID != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            HexDumpEncoder enc = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            sb.append("  Subject Key Identifier: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                      enc.encodeBuffer(subjectKeyID) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        if (authorityKeyID != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
            HexDumpEncoder enc = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
            sb.append("  Authority Key Identifier: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                      enc.encodeBuffer(authorityKeyID) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        if (certificateValid != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
            sb.append("  Certificate Valid: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                      certificateValid.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        if (privateKeyValid != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
            sb.append("  Private Key Valid: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                      privateKeyValid.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        if (subjectPublicKeyAlgID != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
            sb.append("  Subject Public Key AlgID: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                      subjectPublicKeyAlgID.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        if (subjectPublicKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            sb.append("  Subject Public Key: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                      subjectPublicKey.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
        if (keyUsage != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            sb.append("  Key Usage: " + keyUsageToString(keyUsage) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        if (keyPurposeSet != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            sb.append("  Extended Key Usage: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                      keyPurposeSet.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        if (policy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            sb.append("  Policy: " + policy.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        if (pathToGeneralNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            sb.append("  Path to names:\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            Iterator<GeneralNameInterface> i = pathToGeneralNames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                sb.append("    " + i.next() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        sb.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    // Copied from sun.security.x509.KeyUsageExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
    // (without calling the superclass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * Returns a printable representation of the KeyUsage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
    private static String keyUsageToString(boolean[] k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        String s = "KeyUsage [\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            if (k[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                s += "  DigitalSignature\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            if (k[1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                s += "  Non_repudiation\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            if (k[2]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
                s += "  Key_Encipherment\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            if (k[3]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                s += "  Data_Encipherment\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            if (k[4]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                s += "  Key_Agreement\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            if (k[5]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                s += "  Key_CertSign\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            if (k[6]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                s += "  Crl_Sign\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            if (k[7]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                s += "  Encipher_Only\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            if (k[8]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                s += "  Decipher_Only\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        } catch (ArrayIndexOutOfBoundsException ex) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        s += "]\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        return (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     * Returns an Extension object given any X509Certificate and extension oid.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1930
     * Throw an {@code IOException} if the extension byte value is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * malformed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1933
     * @param cert a {@code X509Certificate}
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1934
     * @param extId an {@code integer} which specifies the extension index.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * Currently, the supported extensions are as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     * index 0 - PrivateKeyUsageExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * index 1 - SubjectAlternativeNameExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * index 2 - NameConstraintsExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     * index 3 - CertificatePoliciesExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     * index 4 - ExtendedKeyUsageExtension
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1941
     * @return an {@code Extension} object whose real type is as specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * by the extension oid.
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1943
     * @throws IOException if cannot construct the {@code Extension}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * object with the extension encoding retrieved from the passed in
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1945
     * {@code X509Certificate}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
    private static Extension getExtensionObject(X509Certificate cert, int extId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        if (cert instanceof X509CertImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            X509CertImpl impl = (X509CertImpl)cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
            switch (extId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            case PRIVATE_KEY_USAGE_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                return impl.getPrivateKeyUsageExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            case SUBJECT_ALT_NAME_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                return impl.getSubjectAlternativeNameExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
            case NAME_CONSTRAINTS_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                return impl.getNameConstraintsExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            case CERT_POLICIES_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                return impl.getCertificatePoliciesExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            case EXTENDED_KEY_USAGE_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                return impl.getExtendedKeyUsageExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        byte[] rawExtVal = cert.getExtensionValue(EXTENSION_OIDS[extId]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        if (rawExtVal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        DerInputStream in = new DerInputStream(rawExtVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        byte[] encoded = in.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        switch (extId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        case PRIVATE_KEY_USAGE_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                return new PrivateKeyUsageExtension(FALSE, encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            } catch (CertificateException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                throw new IOException(ex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        case SUBJECT_ALT_NAME_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            return new SubjectAlternativeNameExtension(FALSE, encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        case NAME_CONSTRAINTS_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            return new NameConstraintsExtension(FALSE, encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
        case CERT_POLICIES_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            return new CertificatePoliciesExtension(FALSE, encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        case EXTENDED_KEY_USAGE_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
            return new ExtendedKeyUsageExtension(FALSE, encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
    /**
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1993
     * Decides whether a {@code Certificate} should be selected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
     *
18551
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1995
     * @param cert the {@code Certificate} to be checked
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1996
     * @return {@code true} if the {@code Certificate} should be
882a3948c6e6 8017325: Cleanup of the javadoc <code> tag in java.security.cert
juh
parents: 10419
diff changeset
  1997
     *         selected, {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
    public boolean match(Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        if (!(cert instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        X509Certificate xcert = (X509Certificate)cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
            debug.println("X509CertSelector.match(SN: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                + (xcert.getSerialNumber()).toString(16) + "\n  Issuer: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                + xcert.getIssuerDN() + "\n  Subject: " + xcert.getSubjectDN()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
                + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        /* match on X509Certificate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
        if (x509Cert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            if (!x509Cert.equals(xcert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                        + "certs don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
        /* match on serial number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        if (serialNumber != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
            if (!serialNumber.equals(xcert.getSerialNumber())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                        + "serial numbers don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        /* match on issuer name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        if (issuer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            if (!issuer.equals(xcert.getIssuerX500Principal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                        + "issuer DNs don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        /* match on subject name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        if (subject != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            if (!subject.equals(xcert.getSubjectX500Principal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                        + "subject DNs don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
        /* match on certificate validity range */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
        if (certificateValid != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                xcert.checkValidity(certificateValid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                        + "certificate not within validity period");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
        /* match on subject public key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
        if (subjectPublicKeyBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
            byte[] certKey = xcert.getPublicKey().getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
            if (!Arrays.equals(subjectPublicKeyBytes, certKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                        + "subject public keys don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        boolean result = matchBasicConstraints(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                      && matchKeyUsage(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                      && matchExtendedKeyUsage(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                      && matchSubjectKeyID(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                      && matchAuthorityKeyID(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                      && matchPrivateKeyValid(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                      && matchSubjectPublicKeyAlgID(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                      && matchPolicy(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                      && matchSubjectAlternativeNames(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                      && matchPathToNames(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                      && matchNameConstraints(xcert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
        if (result && (debug != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
            debug.println("X509CertSelector.match returning: true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
    /* match on subject key identifier extension value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
    private boolean matchSubjectKeyID(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
        if (subjectKeyID == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
            byte[] extVal = xcert.getExtensionValue("2.5.29.14");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
            if (extVal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                        + "no subject key ID extension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
            DerInputStream in = new DerInputStream(extVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
            byte[] certSubjectKeyID = in.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
            if (certSubjectKeyID == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                    !Arrays.equals(subjectKeyID, certSubjectKeyID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                        + "subject key IDs don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                    + "exception in subject key ID check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
    /* match on authority key identifier extension value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
    private boolean matchAuthorityKeyID(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
        if (authorityKeyID == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
            byte[] extVal = xcert.getExtensionValue("2.5.29.35");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
            if (extVal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                        + "no authority key ID extension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
            DerInputStream in = new DerInputStream(extVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
            byte[] certAuthKeyID = in.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
            if (certAuthKeyID == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                    !Arrays.equals(authorityKeyID, certAuthKeyID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                        + "authority key IDs don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                    + "exception in authority key ID check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
    /* match on private key usage range */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
    private boolean matchPrivateKeyValid(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
        if (privateKeyValid == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
        PrivateKeyUsageExtension ext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
            ext = (PrivateKeyUsageExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                getExtensionObject(xcert, PRIVATE_KEY_USAGE_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
            if (ext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                ext.valid(privateKeyValid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
        } catch (CertificateExpiredException e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                String time = "n/a";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2183
                    Date notAfter = ext.get(PrivateKeyUsageExtension.NOT_AFTER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                    time = notAfter.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                } catch (CertificateException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                    // not able to retrieve notAfter value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                debug.println("X509CertSelector.match: private key usage not "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                    + "within validity date; ext.NOT_After: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                    + time + "; X509CertSelector: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                    + this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                e1.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
        } catch (CertificateNotYetValidException e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                String time = "n/a";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2199
                    Date notBefore = ext.get(PrivateKeyUsageExtension.NOT_BEFORE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                    time = notBefore.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                } catch (CertificateException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                    // not able to retrieve notBefore value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                debug.println("X509CertSelector.match: private key usage not "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                    + "within validity date; ext.NOT_BEFORE: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                    + time + "; X509CertSelector: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                    + this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                e2.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
        } catch (IOException e4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                debug.println("X509CertSelector.match: IOException in "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                    + "private key usage check; X509CertSelector: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                    + this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                e4.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
    /* match on subject public key algorithm OID */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
    private boolean matchSubjectPublicKeyAlgID(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        if (subjectPublicKeyAlgID == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
            byte[] encodedKey = xcert.getPublicKey().getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
            DerValue val = new DerValue(encodedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
            if (val.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                throw new IOException("invalid key format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
            AlgorithmId algID = AlgorithmId.parse(val.data.getDerValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                debug.println("X509CertSelector.match: subjectPublicKeyAlgID = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                    + subjectPublicKeyAlgID + ", xcert subjectPublicKeyAlgID = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                    + algID.getOID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
            }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2241
            if (!subjectPublicKeyAlgID.equals((Object)algID.getOID())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                        + "subject public key alg IDs don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
        } catch (IOException e5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                debug.println("X509CertSelector.match: IOException in subject "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                    + "public key algorithm OID check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
    /* match on key usage extension value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
    private boolean matchKeyUsage(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
        if (keyUsage == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        boolean[] certKeyUsage = xcert.getKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
        if (certKeyUsage != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
            for (int keyBit = 0; keyBit < keyUsage.length; keyBit++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
                if (keyUsage[keyBit] &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                    ((keyBit >= certKeyUsage.length) || !certKeyUsage[keyBit])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                        debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                            + "key usage bits don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
    /* match on extended key usage purpose OIDs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
    private boolean matchExtendedKeyUsage(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
        if ((keyPurposeSet == null) || keyPurposeSet.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
            ExtendedKeyUsageExtension ext =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                (ExtendedKeyUsageExtension)getExtensionObject(xcert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                                                EXTENDED_KEY_USAGE_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            if (ext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                Vector<ObjectIdentifier> certKeyPurposeVector =
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2290
                    ext.get(ExtendedKeyUsageExtension.USAGES);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                if (!certKeyPurposeVector.contains(ANY_EXTENDED_KEY_USAGE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                        && !certKeyPurposeVector.containsAll(keyPurposeOIDSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                        debug.println("X509CertSelector.match: cert failed "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                            + "extendedKeyUsage criterion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
                    + "IOException in extended key usage check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
    /* match on subject alternative name extension names */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
    private boolean matchSubjectAlternativeNames(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
        if ((subjectAlternativeNames == null) || subjectAlternativeNames.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            SubjectAlternativeNameExtension sanExt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                (SubjectAlternativeNameExtension) getExtensionObject(xcert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                                                      SUBJECT_ALT_NAME_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
            if (sanExt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                  debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                      + "no subject alternative name extension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
            }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2326
            GeneralNames certNames =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2327
                    sanExt.get(SubjectAlternativeNameExtension.SUBJECT_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            Iterator<GeneralNameInterface> i =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
                                subjectAlternativeGeneralNames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                GeneralNameInterface matchName = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                for (Iterator<GeneralName> t = certNames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                                                t.hasNext() && !found; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                    GeneralNameInterface certName = (t.next()).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                    found = certName.equals(matchName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                if (!found && (matchAllSubjectAltNames || !i.hasNext())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                      debug.println("X509CertSelector.match: subject alternative "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                          + "name " + matchName + " not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                } else if (found && !matchAllSubjectAltNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                debug.println("X509CertSelector.match: IOException in subject "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                    + "alternative name check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
    /* match on name constraints */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
    private boolean matchNameConstraints(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
        if (nc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
            if (!nc.verify(xcert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                        + "name constraints not satisfied");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
                debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
                    + "IOException in name constraints check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
    /* match on policy OIDs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
    private boolean matchPolicy(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
        if (policy == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
            CertificatePoliciesExtension ext = (CertificatePoliciesExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
                getExtensionObject(xcert, CERT_POLICIES_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
            if (ext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
                  debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                      + "no certificate policy extension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
            }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2395
            List<PolicyInformation> policies = ext.get(CertificatePoliciesExtension.POLICIES);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
             * Convert the Vector of PolicyInformation to a Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
             * of CertificatePolicyIds for easier comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
            List<CertificatePolicyId> policyIDs = new ArrayList<CertificatePolicyId>(policies.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
            for (PolicyInformation info : policies) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
                policyIDs.add(info.getPolicyIdentifier());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
            if (policy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
                boolean foundOne = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
                 * if the user passes in an empty policy Set, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
                 * we just want to make sure that the candidate certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
                 * has some policy OID in its CertPoliciesExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
                if (policy.getCertPolicyIds().isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
                    if (policyIDs.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
                            debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
                                + "cert failed policyAny criterion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
                    for (CertificatePolicyId id : policy.getCertPolicyIds()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
                        if (policyIDs.contains(id)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
                            foundOne = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
                    if (!foundOne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
                            debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
                                + "cert failed policyAny criterion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
                debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
                    + "IOException in certificate policy ID check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
    /* match on pathToNames */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
    private boolean matchPathToNames(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
        if (pathToGeneralNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
            NameConstraintsExtension ext = (NameConstraintsExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
                getExtensionObject(xcert, NAME_CONSTRAINTS_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
            if (ext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
            }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2456
            if ((debug != null) && Debug.isOn("certpath")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
                debug.println("X509CertSelector.match pathToNames:\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
                Iterator<GeneralNameInterface> i =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
                                        pathToGeneralNames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
                while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
                    debug.println("    " + i.next() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2465
            GeneralSubtrees permitted =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2466
                    ext.get(NameConstraintsExtension.PERMITTED_SUBTREES);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2467
            GeneralSubtrees excluded =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2468
                    ext.get(NameConstraintsExtension.EXCLUDED_SUBTREES);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
            if (excluded != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
                if (matchExcluded(excluded) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
            if (permitted != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
                if (matchPermitted(permitted) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
                debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
                    + "IOException in name constraints check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
    private boolean matchExcluded(GeneralSubtrees excluded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
         * Enumerate through excluded and compare each entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
         * to all pathToNames. If any pathToName is within any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
         * subtrees listed in excluded, return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
        for (Iterator<GeneralSubtree> t = excluded.iterator(); t.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
            GeneralSubtree tree = t.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
            GeneralNameInterface excludedName = tree.getName().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
            Iterator<GeneralNameInterface> i = pathToGeneralNames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
                GeneralNameInterface pathToName = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
                if (excludedName.getType() == pathToName.getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
                    switch (pathToName.constrains(excludedName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
                    case GeneralNameInterface.NAME_WIDENS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
                    case GeneralNameInterface.NAME_MATCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
                            debug.println("X509CertSelector.match: name constraints "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
                                + "inhibit path to specified name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
                            debug.println("X509CertSelector.match: excluded name: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
                                pathToName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
    private boolean matchPermitted(GeneralSubtrees permitted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
         * Enumerate through pathToNames, checking that each pathToName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
         * is in at least one of the subtrees listed in permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
         * If not, return false. However, if no subtrees of a given type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
         * are listed, all names of that type are permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
        Iterator<GeneralNameInterface> i = pathToGeneralNames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
            GeneralNameInterface pathToName = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
            Iterator<GeneralSubtree> t = permitted.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
            boolean permittedNameFound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
            boolean nameTypeFound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
            String names = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
            while (t.hasNext() && !permittedNameFound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
                GeneralSubtree tree = t.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
                GeneralNameInterface permittedName = tree.getName().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                if (permittedName.getType() == pathToName.getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
                    nameTypeFound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
                    names = names + "  " + permittedName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
                    switch (pathToName.constrains(permittedName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
                    case GeneralNameInterface.NAME_WIDENS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
                    case GeneralNameInterface.NAME_MATCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
                        permittedNameFound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
            if (!permittedNameFound && nameTypeFound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
                  debug.println("X509CertSelector.match: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
                            "name constraints inhibit path to specified name; " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
                            "permitted names of type " + pathToName.getType() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
                            ": " + names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
    /* match on basic constraints */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
    private boolean matchBasicConstraints(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
        if (basicConstraints == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
        int maxPathLen = xcert.getBasicConstraints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
        if (basicConstraints == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
            if (maxPathLen != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                    debug.println("X509CertSelector.match: not an EE cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
            if (maxPathLen < basicConstraints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
                    debug.println("X509CertSelector.match: maxPathLen too small ("
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
                        + maxPathLen + " < " + basicConstraints + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2586
    @SuppressWarnings("unchecked") // Safe casts assuming clone() works correctly
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2587
    private static <T> Set<T> cloneSet(Set<T> set) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
        if (set instanceof HashSet) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2589
            Object clone = ((HashSet<T>)set).clone();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2590
            return (Set<T>)clone;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
        } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2592
            return new HashSet<T>(set);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
     * Returns a copy of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
     * @return the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
            X509CertSelector copy = (X509CertSelector)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
            // Must clone these because addPathToName et al. modify them
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
            if (subjectAlternativeNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
                copy.subjectAlternativeNames =
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2607
                        cloneSet(subjectAlternativeNames);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
                copy.subjectAlternativeGeneralNames =
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2609
                        cloneSet(subjectAlternativeGeneralNames);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
            if (pathToGeneralNames != null) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2612
                copy.pathToNames = cloneSet(pathToNames);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2613
                copy.pathToGeneralNames = cloneSet(pathToGeneralNames);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
            return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
            /* Cannot happen */
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 10336
diff changeset
  2618
            throw new InternalError(e.toString(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
}