jdk/src/share/classes/java/security/cert/X509CertSelector.java
author xuelei
Wed, 20 Jan 2010 21:38:37 +0800
changeset 4807 2521b7dcf505
parent 715 f16baef3a20e
child 5506 202f599c92aa
permissions -rw-r--r--
6862064: incorrect implementation of PKIXParameters.clone() Reviewed-by: weijun, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 2000-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A <code>CertSelector</code> that selects <code>X509Certificates</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * match all specified criteria. This class is particularly useful when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * selecting certificates from a <code>CertStore</code> to build a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * PKIX-compliant certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * When first constructed, an <code>X509CertSelector</code> has no criteria
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * enabled and each of the <code>get</code> methods return a default value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * (<code>null</code>, or <code>-1</code> for the {@link #getBasicConstraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * getBasicConstraints} method). Therefore, the {@link #match match}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * method would return <code>true</code> for any <code>X509Certificate</code>.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <code>X509CertSelector</code> is passed to
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},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * for example) such that the <code>match</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * usually uniquely matches a single <code>X509Certificate</code>. We say
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Please refer to <a href="http://www.ietf.org/rfc/rfc3280.txt">RFC 3280:
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Creates an <code>X509CertSelector</code>. Initially, no criteria are set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * so any <code>X509Certificate</code> will match.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <code>X509Certificate</code> must be equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <code>X509Certificate</code> passed to the <code>match</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * If <code>null</code>, then this check is not applied.
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param cert the <code>X509Certificate</code> to match (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <code>X509Certificate</code>. If <code>null</code>, any certificate
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *        (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <code>X509Certificate</code>. If <code>null</code>, any issuer
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *                 (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <code>X509Certificate</code>. If <code>null</code>, any issuer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * distinguished name will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * If <code>issuerDN</code> is not <code>null</code>, it should contain a
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *                 (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <code>X509Certificate</code>. If <code>null</code> is specified,
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * If <code>issuerDN</code> is not <code>null</code>, it should contain a
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * <pre><code>
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)) }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * </code></pre>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *                 in ASN.1 DER encoded form (or <code>null</code>)
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            throw (IOException)new IOException("Invalid name").initCause(e);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * <code>X509Certificate</code>. If <code>null</code>, any subject
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *                  (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <code>X509Certificate</code>. If <code>null</code>, any subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * distinguished name will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * If <code>subjectDN</code> is not <code>null</code>, it should contain a
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *                  (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * <code>X509Certificate</code>. If <code>null</code>, any subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * distinguished name will do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * If <code>subjectDN</code> is not <code>null</code>, it should contain a
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *                  ASN.1 DER format (or <code>null</code>)
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            throw (IOException)new IOException("Invalid name").initCause(e);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * <code>X509Certificate</code> must contain a SubjectKeyIdentifier
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * If the criterion value is <code>null</code>, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * subjectKeyIdentifier check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * If <code>subjectKeyID</code> is not <code>null</code>, it
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <pre><code>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * </code></pre>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * <code>X509CertSelector</code>. Instead, the values are compared using
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @param subjectKeyID the subject key identifier (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * <code>X509Certificate</code> must contain an
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * If the criterion value is <code>null</code>, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * authorityKeyIdentifier check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * If <code>authorityKeyID</code> is not <code>null</code>, it
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * <pre><code>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * </code></pre>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * <code>X509CertSelector</code>.  Instead, the values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * compared using a byte-by-byte comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * When the <code>keyIdentifier</code> field of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <code>AuthorityKeyIdentifier</code> is populated, the value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * usually taken from the <code>SubjectKeyIdentifier</code> extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * in the issuer's certificate.  Note, however, that the result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <code>X509Certificate.getExtensionValue(&lt;SubjectKeyIdentifier Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Identifier&gt;)</code> on the issuer's certificate may NOT be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * directly as the input to <code>setAuthorityKeyIdentifier</code>.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * <code>SubjectKeyIdentifier</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * extension, it will be necessary to extract the value of the embedded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * <code>KeyIdentifier</code> OCTET STRING, then DER encode this OCTET
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *        (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * <code>X509Certificate</code>. If <code>null</code>, no certificateValid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Note that the <code>Date</code> supplied here is cloned to protect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @param certValid the <code>Date</code> to check (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * <code>X509Certificate</code>. If <code>null</code>, no privateKeyValid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Note that the <code>Date</code> supplied here is cloned to protect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @param privateKeyValid the <code>Date</code> to check (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *                        <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * <code>X509Certificate</code> must contain a subject public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * with the specified algorithm. If <code>null</code>, no
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *            for (or <code>null</code>). An OID is represented by a
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * <code>X509Certificate</code> must contain the specified subject public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * key. If <code>null</code>, no subjectPublicKey check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @param key the subject public key to check for (or <code>null</code>)
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * Sets the subjectPublicKey criterion. The <code>X509Certificate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * must contain the specified subject public key. If <code>null</code>,
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * If <code>key</code> is not <code>null</code>, it should contain a
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * <pre><code>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * </code></pre>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *            form (or <code>null</code>)
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * Sets the keyUsage criterion. The <code>X509Certificate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * must allow the specified keyUsage values. If <code>null</code>, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * keyUsage check will be done. Note that an <code>X509Certificate</code>
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()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *                 Or <code>null</code>.
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Sets the extendedKeyUsage criterion. The <code>X509Certificate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * must allow the specified key purposes in its extended key usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * extension. If <code>keyPurposeSet</code> is empty or <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * no extendedKeyUsage check will be done. Note that an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * <code>X509Certificate</code> that has no extendedKeyUsage extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * implicitly allows all key purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * Note that the <code>Set</code> is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @param keyPurposeSet a <code>Set</code> of key purpose OIDs in string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * format (or <code>null</code>). Each OID is represented by a set of
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,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * the <code>X509Certificate</code> must contain all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * specified subject alternative names. If disabled, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * <code>X509Certificate</code> must contain at least one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * specified subject alternative names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * <p>The matchAllNames flag is <code>true</code> by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @param matchAllNames if <code>true</code>, the flag is enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * if <code>false</code>, the flag is disabled.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * <code>X509Certificate</code> must contain all or at least one of the
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * The <code>names</code> parameter (if not <code>null</code>) is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * <code>Collection</code> with one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * entry for each name to be included in the subject alternative name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * criterion. Each entry is a <code>List</code> whose first entry is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * <code>Integer</code> (the name type, 0-8) and whose second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * entry is a <code>String</code> or a byte array (the name, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * string or ASN.1 DER encoded form, respectively).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * There can be multiple names of the same type. If <code>null</code>
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * Each subject alternative name in the <code>Collection</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * may be specified either as a <code>String</code> or as an ASN.1 encoded
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * Note that the <code>names</code> parameter can contain duplicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * names (same name and name type), but they may be removed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * <code>Collection</code> of names returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * {@link #getSubjectAlternativeNames getSubjectAlternativeNames} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * Note that a deep copy is performed on the <code>Collection</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * protect against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @param names a <code>Collection</code> of names (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * <code>X509Certificate</code> must contain all or at least one
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * the restrictions included in RFC 3280). IPv4 address names are
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *             RFC 3280, section 4.2.1.7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @param name the name in string form (not <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * <code>X509Certificate</code> must contain all or at least one
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * defined in RFC 3280 and X.509. The encoded byte array should only contain
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * <pre><code>
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}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * </code></pre>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * subjectAlternativeNames criterion. The <code>X509Certificate</code>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *             RFC 3280, section 4.2.1.7)
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,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * returning a <code>Collection</code> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * <code>GeneralNameInterface</code>s.
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *              Each entry is a <code>List</code> whose first entry
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * @return a Set of <code>GeneralNameInterface</code>s
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).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * Throw an <code>IllegalArgumentException</code> or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * <code>ClassCastException</code> if one of the objects is malformed.
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    static boolean equalNames(Collection object1, Collection object2) {
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * Make a <code>GeneralNameInterface</code> out of a name type (0-8) and an
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * Sets the name constraints criterion. The <code>X509Certificate</code>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * would appear in the NameConstraints structure defined in RFC 3280
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * <pre><code>
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}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * </code></pre>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *              <code>null</code>,
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * equal to zero, <code>X509Certificates</code> must include a
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * Sets the policy constraint. The <code>X509Certificate</code> must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * include at least one of the specified policies in its certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * policies extension. If <code>certPolicySet</code> is empty, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * <code>X509Certificate</code> must include at least some specified policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * in its certificate policies extension. If <code>certPolicySet</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * <code>null</code>, no policy check will be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * Note that the <code>Set</code> is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * @param certPolicySet a <code>Set</code> of certificate policy OIDs in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     *                      string format (or <code>null</code>). Each OID is
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * Sets the pathToNames criterion. The <code>X509Certificate</code> must
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,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * the complete set of names which the <code>X509Certificates</code>'s
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * The <code>names</code> parameter (if not <code>null</code>) is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * <code>Collection</code> with one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * entry for each name to be included in the pathToNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * criterion. Each entry is a <code>List</code> whose first entry is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * <code>Integer</code> (the name type, 0-8) and whose second
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * entry is a <code>String</code> or a byte array (the name, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * string or ASN.1 DER encoded form, respectively).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * There can be multiple names of the same type. If <code>null</code>
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * Each name in the <code>Collection</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * may be specified either as a <code>String</code> or as an ASN.1 encoded
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * Note that the <code>names</code> parameter can contain duplicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * names (same name and name type), but they may be removed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * <code>Collection</code> of names returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * {@link #getPathToNames getPathToNames} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * Note that a deep copy is performed on the <code>Collection</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * protect against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * @param names a <code>Collection</code> with one entry per name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     *              (or <code>null</code>)
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * Adds a name to the pathToNames criterion. The <code>X509Certificate</code>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * the <code>X509Certificates</code>'s name constraints must permit.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * the restrictions included in RFC 3280). IPv4 address names are
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     *             RFC 3280, section 4.2.1.7)
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * Adds a name to the pathToNames criterion. The <code>X509Certificate</code>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * the <code>X509Certificates</code>'s name constraints must permit.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * defined in RFC 3280 and X.509. The ASN.1 definition of this structure
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     *             RFC 3280, section 4.2.1.7)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * pathToNames criterion. The <code>X509Certificate</code> must contain
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     *             RFC 3280, section 4.2.1.7)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * <code>X509Certificate</code> must be equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * <code>X509Certificate</code> passed to the <code>match</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * If <code>null</code>, this check is not applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * @return the <code>X509Certificate</code> to match (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * <code>X509Certificate</code>. If <code>null</code>, any certificate
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     *                (or <code>null</code>)
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * Returns the issuer criterion as an <code>X500Principal</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * distinguished name must match the issuer distinguished name in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * <code>X509Certificate</code>. If <code>null</code>, the issuer criterion
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     *         (or <code>null</code>)
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * Returns the issuer criterion as a <code>String</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * distinguished name must match the issuer distinguished name in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * <code>X509Certificate</code>. If <code>null</code>, the issuer criterion
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * If the value returned is not <code>null</code>, it is a
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     *         (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * <code>X509Certificate</code>. If <code>null</code>, the issuer criterion
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * If the value returned is not <code>null</code>, it is a byte
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     *         in ASN.1 DER format (or <code>null</code>)
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * Returns the subject criterion as an <code>X500Principal</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * distinguished name must match the subject distinguished name in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * <code>X509Certificate</code>. If <code>null</code>, the subject criterion
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     *         (or <code>null</code>)
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * Returns the subject criterion as a <code>String</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * distinguished name must match the subject distinguished name in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * <code>X509Certificate</code>. If <code>null</code>, the subject criterion
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * If the value returned is not <code>null</code>, it is a
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     *         (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * <code>X509Certificate</code>. If <code>null</code>, the subject criterion
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * If the value returned is not <code>null</code>, it is a byte
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     *         in ASN.1 DER format (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * <code>X509Certificate</code> must contain a SubjectKeyIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * extension with the specified value. If <code>null</code>, no
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * @return the key identifier (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     * <code>X509Certificate</code> must contain a AuthorityKeyIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * extension with the specified value. If <code>null</code>, no
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * @return the key identifier (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * <code>X509Certificate</code>. If <code>null</code>, no certificateValid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * Note that the <code>Date</code> returned is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * @return the <code>Date</code> to check (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * <code>X509Certificate</code>. If <code>null</code>, no privateKeyValid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * Note that the <code>Date</code> returned is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * @return the <code>Date</code> to check (or <code>null</code>)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     * <code>X509Certificate</code> must contain a subject public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     * with the specified algorithm. If <code>null</code>, no
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     *         for (or <code>null</code>). An OID is represented by a set of
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * <code>X509Certificate</code> must contain the specified subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * public key. If <code>null</code>, no subjectPublicKey check will be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     * @return the subject public key to check for (or <code>null</code>)
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * Returns the keyUsage criterion. The <code>X509Certificate</code>
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()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     *                 Or <code>null</code>.
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * Returns the extendedKeyUsage criterion. The <code>X509Certificate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * must allow the specified key purposes in its extended key usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * extension. If the <code>keyPurposeSet</code> returned is empty or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     * <code>null</code>, no extendedKeyUsage check will be done. Note that an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * <code>X509Certificate</code> that has no extendedKeyUsage extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * implicitly allows all key purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * @return an immutable <code>Set</code> of key purpose OIDs in string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * format (or <code>null</code>)
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * Indicates if the <code>X509Certificate</code> must contain all
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * addSubjectAlternativeName} methods. If <code>true</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * the <code>X509Certificate</code> must contain all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * specified subject alternative names. If <code>false</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * <code>X509Certificate</code> must contain at least one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * specified subject alternative names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     * @return <code>true</code> if the flag is enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * <code>false</code> if the flag is disabled. The flag is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * <code>true</code> by default.
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * The <code>X509Certificate</code> must contain all or at least one
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * <code>null</code>, no subjectAlternativeNames check will be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     * If the value returned is not <code>null</code>, it is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     * <code>Collection</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     * one entry for each name to be included in the subject alternative name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     * criterion. Each entry is a <code>List</code> whose first entry is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     * <code>Integer</code> (the name type, 0-8) and whose second
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     * entry is a <code>String</code> or a byte array (the name, in
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     * <code>Collection</code> returned may contain duplicate names (same name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * and name type).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * Each subject alternative name in the <code>Collection</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * may be specified either as a <code>String</code> or as an ASN.1 encoded
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * Note that a deep copy is performed on the <code>Collection</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * protect against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * @return a <code>Collection</code> of names (or <code>null</code>)
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * Throw a <code>RuntimeException</code> if the argument is malformed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * This method wraps cloneAndCheckNames, changing any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * <code>IOException</code> into a <code>RuntimeException</code>. This
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * @param names a <code>Collection</code> with one entry per name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     *              Each entry is a <code>List</code> whose first entry
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     * @return a deep copy of the specified <code>Collection</code>
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * Throw an <code>IOException</code> if the argument is malformed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * @param names a <code>Collection</code> with one entry per name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     *              Each entry is a <code>List</code> whose first entry
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     *              <code>null</code> is not an acceptable value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * @return a deep copy of the specified <code>Collection</code>
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<?>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        Iterator<List<?>> i = names.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            Object o = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
            if (!(o instanceof List)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                throw new IOException("expected a List");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            namesCopy.add(new ArrayList<Object>((List<?>)o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        // Check the contents of the Lists and clone any byte arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        i = namesCopy.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            List<Object> nameList = (List<Object>)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            if (nameList.size() != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                throw new IOException("name list size not 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            Object o = nameList.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            if (!(o instanceof Integer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                throw new IOException("expected an Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            int nameType = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            if ((nameType < 0) || (nameType > 8)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                throw new IOException("name type not 0-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
            Object nameObject = nameList.get(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            if (!(nameObject instanceof byte[]) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                !(nameObject instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                    debug.println("X509CertSelector.cloneAndCheckNames() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                        + "name not byte array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                throw new IOException("name not byte array or String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            if (nameObject instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                nameList.set(1, ((byte[]) nameObject).clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        return namesCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * Returns the name constraints criterion. The <code>X509Certificate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     * must have subject and subject alternative names that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     * meet the specified name constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * The name constraints are returned as a byte array. This byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     * contains the DER encoded form of the name constraints, as they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     * would appear in the NameConstraints structure defined in RFC 3280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     * and X.509. The ASN.1 notation for this structure is supplied in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * {@link #setNameConstraints(byte [] bytes) setNameConstraints(byte [] bytes)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * Note that the byte array returned is cloned to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     * subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     * @return a byte array containing the ASN.1 DER encoding of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     *         a NameConstraints extension used for checking name constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     *         <code>null</code> if no name constraints check will be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     * @see #setNameConstraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
    public byte[] getNameConstraints() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        if (ncBytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1739
            return ncBytes.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * Returns the basic constraints constraint. If the value is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * or equal to zero, the <code>X509Certificates</code> must include a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * basicConstraints extension with a pathLen of at least this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     * If the value is -2, only end-entity certificates are accepted. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     * the value is -1, no basicConstraints check is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * @return the value for the basic constraints constraint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     * @see #setBasicConstraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    public int getBasicConstraints() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        return basicConstraints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * Returns the policy criterion. The <code>X509Certificate</code> must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * include at least one of the specified policies in its certificate policies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     * extension. If the <code>Set</code> returned is empty, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * <code>X509Certificate</code> must include at least some specified policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * in its certificate policies extension. If the <code>Set</code> returned is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * <code>null</code>, no policy check will be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * @return an immutable <code>Set</code> of certificate policy OIDs in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     *         string format (or <code>null</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * @see #setPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
    public Set<String> getPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        return policySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     * Returns a copy of the pathToNames criterion. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     * <code>X509Certificate</code> must not include name constraints that would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     * prohibit building a path to the specified names. If the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * returned is <code>null</code>, no pathToNames check will be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * If the value returned is not <code>null</code>, it is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * <code>Collection</code> with one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     * entry for each name to be included in the pathToNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     * criterion. Each entry is a <code>List</code> whose first entry is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     * <code>Integer</code> (the name type, 0-8) and whose second
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     * entry is a <code>String</code> or a byte array (the name, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * string or ASN.1 DER encoded form, respectively).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     * There can be multiple names of the same type. Note that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     * <code>Collection</code> returned may contain duplicate names (same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     * name and name type).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * Each name in the <code>Collection</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     * may be specified either as a <code>String</code> or as an ASN.1 encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * byte array. For more details about the formats used, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * {@link #addPathToName(int type, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     * addPathToName(int type, String name)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * {@link #addPathToName(int type, byte [] name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     * addPathToName(int type, byte [] name)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * Note that a deep copy is performed on the <code>Collection</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     * protect against subsequent modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     * @return a <code>Collection</code> of names (or <code>null</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     * @see #setPathToNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    public Collection<List<?>> getPathToNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        if (pathToNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        return cloneNames(pathToNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * Return a printable representation of the <code>CertSelector</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * @return a <code>String</code> describing the contents of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     *         <code>CertSelector</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        sb.append("X509CertSelector: [\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        if (x509Cert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            sb.append("  Certificate: " + x509Cert.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        if (serialNumber != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            sb.append("  Serial Number: " + serialNumber.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        if (issuer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            sb.append("  Issuer: " + getIssuerAsString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        if (subject != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
            sb.append("  Subject: " + getSubjectAsString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        sb.append("  matchAllSubjectAltNames flag: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                  + String.valueOf(matchAllSubjectAltNames) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        if (subjectAlternativeNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            sb.append("  SubjectAlternativeNames:\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            Iterator<List<?>> i = subjectAlternativeNames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                List<?> list = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                sb.append("    type " + list.get(0) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                          ", name " + list.get(1) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        if (subjectKeyID != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            HexDumpEncoder enc = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
            sb.append("  Subject Key Identifier: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                      enc.encodeBuffer(subjectKeyID) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        if (authorityKeyID != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            HexDumpEncoder enc = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
            sb.append("  Authority Key Identifier: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                      enc.encodeBuffer(authorityKeyID) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        if (certificateValid != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
            sb.append("  Certificate Valid: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                      certificateValid.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        if (privateKeyValid != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
            sb.append("  Private Key Valid: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                      privateKeyValid.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        if (subjectPublicKeyAlgID != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            sb.append("  Subject Public Key AlgID: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                      subjectPublicKeyAlgID.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
        if (subjectPublicKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            sb.append("  Subject Public Key: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                      subjectPublicKey.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        if (keyUsage != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
            sb.append("  Key Usage: " + keyUsageToString(keyUsage) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        if (keyPurposeSet != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            sb.append("  Extended Key Usage: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                      keyPurposeSet.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        if (policy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            sb.append("  Policy: " + policy.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        if (pathToGeneralNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
            sb.append("  Path to names:\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            Iterator<GeneralNameInterface> i = pathToGeneralNames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                sb.append("    " + i.next() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        sb.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
    // Copied from sun.security.x509.KeyUsageExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
    // (without calling the superclass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     * Returns a printable representation of the KeyUsage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
    private static String keyUsageToString(boolean[] k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        String s = "KeyUsage [\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            if (k[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                s += "  DigitalSignature\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            if (k[1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                s += "  Non_repudiation\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
            if (k[2]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
                s += "  Key_Encipherment\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            if (k[3]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                s += "  Data_Encipherment\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            if (k[4]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                s += "  Key_Agreement\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            if (k[5]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                s += "  Key_CertSign\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            if (k[6]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                s += "  Crl_Sign\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
            if (k[7]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                s += "  Encipher_Only\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            if (k[8]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                s += "  Decipher_Only\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        } catch (ArrayIndexOutOfBoundsException ex) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        s += "]\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        return (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     * Returns an Extension object given any X509Certificate and extension oid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * Throw an <code>IOException</code> if the extension byte value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * malformed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * @param cert a <code>X509Certificate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * @param extId an <code>integer</code> which specifies the extension index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     * Currently, the supported extensions are as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     * index 0 - PrivateKeyUsageExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     * index 1 - SubjectAlternativeNameExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * index 2 - NameConstraintsExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     * index 3 - CertificatePoliciesExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * index 4 - ExtendedKeyUsageExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * @return an <code>Extension</code> object whose real type is as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     * by the extension oid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     * @throws IOException if cannot construct the <code>Extension</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     * object with the extension encoding retrieved from the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     * <code>X509Certificate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
    private static Extension getExtensionObject(X509Certificate cert, int extId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        if (cert instanceof X509CertImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            X509CertImpl impl = (X509CertImpl)cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
            switch (extId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
            case PRIVATE_KEY_USAGE_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                return impl.getPrivateKeyUsageExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            case SUBJECT_ALT_NAME_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                return impl.getSubjectAlternativeNameExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            case NAME_CONSTRAINTS_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                return impl.getNameConstraintsExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            case CERT_POLICIES_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                return impl.getCertificatePoliciesExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            case EXTENDED_KEY_USAGE_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                return impl.getExtendedKeyUsageExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        byte[] rawExtVal = cert.getExtensionValue(EXTENSION_OIDS[extId]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        if (rawExtVal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
        DerInputStream in = new DerInputStream(rawExtVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        byte[] encoded = in.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        switch (extId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        case PRIVATE_KEY_USAGE_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                return new PrivateKeyUsageExtension(FALSE, encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            } catch (CertificateException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                throw new IOException(ex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
        case SUBJECT_ALT_NAME_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            return new SubjectAlternativeNameExtension(FALSE, encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        case NAME_CONSTRAINTS_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
            return new NameConstraintsExtension(FALSE, encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        case CERT_POLICIES_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            return new CertificatePoliciesExtension(FALSE, encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        case EXTENDED_KEY_USAGE_ID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            return new ExtendedKeyUsageExtension(FALSE, encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     * Decides whether a <code>Certificate</code> should be selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     * @param cert the <code>Certificate</code> to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     * @return <code>true</code> if the <code>Certificate</code> should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     *         selected, <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
    public boolean match(Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        if (!(cert instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        X509Certificate xcert = (X509Certificate)cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
            debug.println("X509CertSelector.match(SN: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                + (xcert.getSerialNumber()).toString(16) + "\n  Issuer: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                + xcert.getIssuerDN() + "\n  Subject: " + xcert.getSubjectDN()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        /* match on X509Certificate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        if (x509Cert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            if (!x509Cert.equals(xcert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                        + "certs don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        /* match on serial number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        if (serialNumber != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            if (!serialNumber.equals(xcert.getSerialNumber())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                        + "serial numbers don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        /* match on issuer name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
        if (issuer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
            if (!issuer.equals(xcert.getIssuerX500Principal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                        + "issuer DNs don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
        /* match on subject name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        if (subject != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
            if (!subject.equals(xcert.getSubjectX500Principal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                        + "subject DNs don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        /* match on certificate validity range */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        if (certificateValid != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                xcert.checkValidity(certificateValid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
            } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                        + "certificate not within validity period");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
        /* match on subject public key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        if (subjectPublicKeyBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            byte[] certKey = xcert.getPublicKey().getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
            if (!Arrays.equals(subjectPublicKeyBytes, certKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                        + "subject public keys don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        boolean result = matchBasicConstraints(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                      && matchKeyUsage(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                      && matchExtendedKeyUsage(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                      && matchSubjectKeyID(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                      && matchAuthorityKeyID(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                      && matchPrivateKeyValid(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                      && matchSubjectPublicKeyAlgID(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                      && matchPolicy(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                      && matchSubjectAlternativeNames(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                      && matchPathToNames(xcert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                      && matchNameConstraints(xcert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        if (result && (debug != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
            debug.println("X509CertSelector.match returning: true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
    /* match on subject key identifier extension value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
    private boolean matchSubjectKeyID(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        if (subjectKeyID == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
            byte[] extVal = xcert.getExtensionValue("2.5.29.14");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
            if (extVal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                        + "no subject key ID extension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
            DerInputStream in = new DerInputStream(extVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
            byte[] certSubjectKeyID = in.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
            if (certSubjectKeyID == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                    !Arrays.equals(subjectKeyID, certSubjectKeyID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                        + "subject key IDs don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                    + "exception in subject key ID check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
    /* match on authority key identifier extension value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
    private boolean matchAuthorityKeyID(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
        if (authorityKeyID == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
            byte[] extVal = xcert.getExtensionValue("2.5.29.35");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
            if (extVal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                        + "no authority key ID extension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
            DerInputStream in = new DerInputStream(extVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
            byte[] certAuthKeyID = in.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
            if (certAuthKeyID == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                    !Arrays.equals(authorityKeyID, certAuthKeyID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                        + "authority key IDs don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                    + "exception in authority key ID check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
    /* match on private key usage range */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
    private boolean matchPrivateKeyValid(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
        if (privateKeyValid == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
        PrivateKeyUsageExtension ext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
            ext = (PrivateKeyUsageExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                getExtensionObject(xcert, PRIVATE_KEY_USAGE_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
            if (ext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                ext.valid(privateKeyValid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
        } catch (CertificateExpiredException e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                String time = "n/a";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                    Date notAfter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                        (Date)ext.get(PrivateKeyUsageExtension.NOT_AFTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                    time = notAfter.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                } catch (CertificateException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                    // not able to retrieve notAfter value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                debug.println("X509CertSelector.match: private key usage not "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                    + "within validity date; ext.NOT_After: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                    + time + "; X509CertSelector: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                    + this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                e1.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
        } catch (CertificateNotYetValidException e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                String time = "n/a";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                    Date notBefore = (Date)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                        ext.get(PrivateKeyUsageExtension.NOT_BEFORE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                    time = notBefore.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                } catch (CertificateException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                    // not able to retrieve notBefore value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                debug.println("X509CertSelector.match: private key usage not "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                    + "within validity date; ext.NOT_BEFORE: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                    + time + "; X509CertSelector: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                    + this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                e2.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
        } catch (CertificateException e3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                debug.println("X509CertSelector.match: CertificateException "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                    + "in private key usage check; X509CertSelector: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                    + this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                e3.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        } catch (IOException e4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                debug.println("X509CertSelector.match: IOException in "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                    + "private key usage check; X509CertSelector: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                    + this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                e4.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
    /* match on subject public key algorithm OID */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
    private boolean matchSubjectPublicKeyAlgID(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
        if (subjectPublicKeyAlgID == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            byte[] encodedKey = xcert.getPublicKey().getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            DerValue val = new DerValue(encodedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
            if (val.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                throw new IOException("invalid key format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
            AlgorithmId algID = AlgorithmId.parse(val.data.getDerValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                debug.println("X509CertSelector.match: subjectPublicKeyAlgID = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                    + subjectPublicKeyAlgID + ", xcert subjectPublicKeyAlgID = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                    + algID.getOID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
            if (!subjectPublicKeyAlgID.equals(algID.getOID())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
                        + "subject public key alg IDs don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
        } catch (IOException e5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                debug.println("X509CertSelector.match: IOException in subject "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                    + "public key algorithm OID check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
    /* match on key usage extension value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
    private boolean matchKeyUsage(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
        if (keyUsage == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
        boolean[] certKeyUsage = xcert.getKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
        if (certKeyUsage != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
            for (int keyBit = 0; keyBit < keyUsage.length; keyBit++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                if (keyUsage[keyBit] &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                    ((keyBit >= certKeyUsage.length) || !certKeyUsage[keyBit])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                        debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                            + "key usage bits don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
    /* match on extended key usage purpose OIDs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
    private boolean matchExtendedKeyUsage(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
        if ((keyPurposeSet == null) || keyPurposeSet.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
            ExtendedKeyUsageExtension ext =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                (ExtendedKeyUsageExtension)getExtensionObject(xcert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                                                EXTENDED_KEY_USAGE_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            if (ext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
                Vector<ObjectIdentifier> certKeyPurposeVector =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                    (Vector<ObjectIdentifier>)ext.get(ExtendedKeyUsageExtension.USAGES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
                if (!certKeyPurposeVector.contains(ANY_EXTENDED_KEY_USAGE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
                        && !certKeyPurposeVector.containsAll(keyPurposeOIDSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                        debug.println("X509CertSelector.match: cert failed "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
                            + "extendedKeyUsage criterion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
                debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                    + "IOException in extended key usage check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
    /* match on subject alternative name extension names */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
    private boolean matchSubjectAlternativeNames(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
        if ((subjectAlternativeNames == null) || subjectAlternativeNames.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
            SubjectAlternativeNameExtension sanExt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                (SubjectAlternativeNameExtension) getExtensionObject(xcert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                                                      SUBJECT_ALT_NAME_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
            if (sanExt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                  debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                      + "no subject alternative name extension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
            GeneralNames certNames = (GeneralNames)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                sanExt.get(SubjectAlternativeNameExtension.SUBJECT_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
            Iterator<GeneralNameInterface> i =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                                subjectAlternativeGeneralNames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                GeneralNameInterface matchName = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                for (Iterator<GeneralName> t = certNames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                                                t.hasNext() && !found; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
                    GeneralNameInterface certName = (t.next()).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                    found = certName.equals(matchName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                if (!found && (matchAllSubjectAltNames || !i.hasNext())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                      debug.println("X509CertSelector.match: subject alternative "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                          + "name " + matchName + " not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                } else if (found && !matchAllSubjectAltNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                debug.println("X509CertSelector.match: IOException in subject "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                    + "alternative name check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
    /* match on name constraints */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
    private boolean matchNameConstraints(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
        if (nc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
            if (!nc.verify(xcert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
                    debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
                        + "name constraints not satisfied");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
                debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
                    + "IOException in name constraints check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
    /* match on policy OIDs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
    private boolean matchPolicy(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
        if (policy == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
            CertificatePoliciesExtension ext = (CertificatePoliciesExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
                getExtensionObject(xcert, CERT_POLICIES_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
            if (ext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
                  debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
                      + "no certificate policy extension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
            List<PolicyInformation> policies = (List<PolicyInformation>)ext.get(CertificatePoliciesExtension.POLICIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
             * Convert the Vector of PolicyInformation to a Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
             * of CertificatePolicyIds for easier comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
            List<CertificatePolicyId> policyIDs = new ArrayList<CertificatePolicyId>(policies.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
            for (PolicyInformation info : policies) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
                policyIDs.add(info.getPolicyIdentifier());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
            if (policy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
                boolean foundOne = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
                 * if the user passes in an empty policy Set, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
                 * we just want to make sure that the candidate certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
                 * has some policy OID in its CertPoliciesExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
                if (policy.getCertPolicyIds().isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
                    if (policyIDs.isEmpty()) {
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
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
                    for (CertificatePolicyId id : policy.getCertPolicyIds()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
                        if (policyIDs.contains(id)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
                            foundOne = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
                    if (!foundOne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
                            debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
                                + "cert failed policyAny criterion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
                debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
                    + "IOException in certificate policy ID check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
    /* match on pathToNames */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
    private boolean matchPathToNames(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
        if (pathToGeneralNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
            NameConstraintsExtension ext = (NameConstraintsExtension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
                getExtensionObject(xcert, NAME_CONSTRAINTS_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
            if (ext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
            if ((debug != null) && debug.isOn("certpath")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
                debug.println("X509CertSelector.match pathToNames:\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
                Iterator<GeneralNameInterface> i =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
                                        pathToGeneralNames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
                while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
                    debug.println("    " + i.next() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
            GeneralSubtrees permitted = (GeneralSubtrees)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
                ext.get(NameConstraintsExtension.PERMITTED_SUBTREES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
            GeneralSubtrees excluded = (GeneralSubtrees)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
                ext.get(NameConstraintsExtension.EXCLUDED_SUBTREES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
            if (excluded != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
                if (matchExcluded(excluded) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
            if (permitted != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
                if (matchPermitted(permitted) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                debug.println("X509CertSelector.match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
                    + "IOException in name constraints check");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
    private boolean matchExcluded(GeneralSubtrees excluded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
         * Enumerate through excluded and compare each entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
         * to all pathToNames. If any pathToName is within any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
         * subtrees listed in excluded, return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
        for (Iterator<GeneralSubtree> t = excluded.iterator(); t.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
            GeneralSubtree tree = t.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
            GeneralNameInterface excludedName = tree.getName().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
            Iterator<GeneralNameInterface> i = pathToGeneralNames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
                GeneralNameInterface pathToName = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
                if (excludedName.getType() == pathToName.getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
                    switch (pathToName.constrains(excludedName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
                    case GeneralNameInterface.NAME_WIDENS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
                    case GeneralNameInterface.NAME_MATCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
                            debug.println("X509CertSelector.match: name constraints "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
                                + "inhibit path to specified name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
                            debug.println("X509CertSelector.match: excluded name: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
                                pathToName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
    private boolean matchPermitted(GeneralSubtrees permitted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
         * Enumerate through pathToNames, checking that each pathToName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
         * is in at least one of the subtrees listed in permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
         * If not, return false. However, if no subtrees of a given type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
         * are listed, all names of that type are permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
        Iterator<GeneralNameInterface> i = pathToGeneralNames.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
            GeneralNameInterface pathToName = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
            Iterator<GeneralSubtree> t = permitted.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
            boolean permittedNameFound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
            boolean nameTypeFound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
            String names = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
            while (t.hasNext() && !permittedNameFound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
                GeneralSubtree tree = t.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
                GeneralNameInterface permittedName = tree.getName().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
                if (permittedName.getType() == pathToName.getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
                    nameTypeFound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
                    names = names + "  " + permittedName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
                    switch (pathToName.constrains(permittedName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
                    case GeneralNameInterface.NAME_WIDENS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
                    case GeneralNameInterface.NAME_MATCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
                        permittedNameFound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
            if (!permittedNameFound && nameTypeFound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
                  debug.println("X509CertSelector.match: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
                            "name constraints inhibit path to specified name; " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                            "permitted names of type " + pathToName.getType() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
                            ": " + names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
    /* match on basic constraints */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
    private boolean matchBasicConstraints(X509Certificate xcert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
        if (basicConstraints == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
        int maxPathLen = xcert.getBasicConstraints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
        if (basicConstraints == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
            if (maxPathLen != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                    debug.println("X509CertSelector.match: not an EE cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
            if (maxPathLen < basicConstraints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                    debug.println("X509CertSelector.match: maxPathLen too small ("
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
                        + maxPathLen + " < " + basicConstraints + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
    private static Set<?> cloneSet(Set<?> set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
        if (set instanceof HashSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
            Object clone = ((HashSet<?>)set).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
            return (Set<?>)clone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
            return new HashSet<Object>(set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
     * Returns a copy of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
     * @return the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
            X509CertSelector copy = (X509CertSelector)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
            // Must clone these because addPathToName et al. modify them
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
            if (subjectAlternativeNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
                copy.subjectAlternativeNames =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
                        (Set<List<?>>)cloneSet(subjectAlternativeNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
                copy.subjectAlternativeGeneralNames =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
                        (Set<GeneralNameInterface>)cloneSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
                                (subjectAlternativeGeneralNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
            if (pathToGeneralNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
                copy.pathToNames =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
                        (Set<List<?>>)cloneSet(pathToNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
                copy.pathToGeneralNames =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
                        (Set<GeneralNameInterface>)cloneSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
                                (pathToGeneralNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
            return copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
            /* Cannot happen */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
            throw new InternalError(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
}