jdk/src/share/classes/sun/security/x509/DistributionPoint.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2006 Sun Microsystems, Inc.  All Rights Reserved.
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 sun.security.x509;
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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.util.BitArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.DerOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Represent the DistributionPoint sequence used in the CRL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Distribution Points Extension (OID = 2.5.29.31).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The ASN.1 definition for this is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * DistributionPoint ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *      distributionPoint       [0]     DistributionPointName OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *      reasons                 [1]     ReasonFlags OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *      cRLIssuer               [2]     GeneralNames OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * DistributionPointName ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      fullName                [0]     GeneralNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *      nameRelativeToCRLIssuer [1]     RelativeDistinguishedName }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * ReasonFlags ::= BIT STRING {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *      unused                  (0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *      keyCompromise           (1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *      cACompromise            (2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *      affiliationChanged      (3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *      superseded              (4),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *      cessationOfOperation    (5),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *      certificateHold         (6),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *      privilegeWithdrawn      (7),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *      aACompromise            (8) }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * GeneralName ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *         otherName                   [0] INSTANCE OF OTHER-NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *         rfc822Name                  [1] IA5String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *         dNSName                     [2] IA5String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *         x400Address                 [3] ORAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *         directoryName               [4] Name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *         ediPartyName                [5] EDIPartyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *         uniformResourceIdentifier   [6] IA5String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *         iPAddress                   [7] OCTET STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *         registeredID                [8] OBJECT IDENTIFIER }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * RelativeDistinguishedName ::=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *   SET OF AttributeTypeAndValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * AttributeTypeAndValue ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *   type     AttributeType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *   value    AttributeValue }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * AttributeType ::= OBJECT IDENTIFIER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * AttributeValue ::= ANY DEFINED BY AttributeType
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * Instances of this class are designed to be immutable. However, since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * is an internal API we do not use defensive cloning for values for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * performance reasons. It is the responsibility of the consumer to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * that no mutable elements are modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * @author Anne Anderson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @since 1.4.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * @see CRLDistributionPointsExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
public class DistributionPoint {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // reason flag bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // NOTE that these are NOT quite the same as the CRL reason code extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public final static int KEY_COMPROMISE         = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public final static int CA_COMPROMISE          = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public final static int AFFILIATION_CHANGED    = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public final static int SUPERSEDED             = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public final static int CESSATION_OF_OPERATION = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public final static int CERTIFICATE_HOLD       = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public final static int PRIVILEGE_WITHDRAWN    = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public final static int AA_COMPROMISE          = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final String[] REASON_STRINGS = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        "key compromise",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        "CA compromise",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        "affiliation changed",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        "superseded",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        "cessation of operation",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        "certificate hold",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        "privilege withdrawn",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        "AA compromise",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    // context specific tag values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static final byte TAG_DIST_PT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private static final byte TAG_REASONS = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private static final byte TAG_ISSUER = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static final byte TAG_FULL_NAME = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private static final byte TAG_REL_NAME = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    // only one of fullName and relativeName can be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private GeneralNames fullName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private RDN relativeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // reasonFlags or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private boolean[] reasonFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // crlIssuer or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private GeneralNames crlIssuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    // cached hashCode value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private volatile int hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Constructor for the class using GeneralNames for DistributionPointName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param fullName the GeneralNames of the distribution point; may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param reasons the CRL reasons included in the CRL at this distribution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *        point; may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param issuer the name(s) of the CRL issuer for the CRL at this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *        distribution point; may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public DistributionPoint(GeneralNames fullName, boolean[] reasonFlags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            GeneralNames crlIssuer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if ((fullName == null) && (crlIssuer == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        ("fullName and crlIssuer may not both be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        this.fullName = fullName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this.reasonFlags = reasonFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this.crlIssuer = crlIssuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Constructor for the class using RelativeDistinguishedName for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * DistributionPointName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param relativeName the RelativeDistinguishedName of the distribution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *        point; may not be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param reasons the CRL reasons included in the CRL at this distribution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *        point; may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param issuer the name(s) of the CRL issuer for the CRL at this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *        distribution point; may not be null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public DistributionPoint(RDN relativeName, boolean[] reasonFlags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            GeneralNames crlIssuer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if ((relativeName == null) && (crlIssuer == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        ("relativeName and crlIssuer may not both be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        this.relativeName = relativeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        this.reasonFlags = reasonFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        this.crlIssuer = crlIssuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Create the object from the passed DER encoded form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param val the DER encoded form of the DistributionPoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public DistributionPoint(DerValue val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (val.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new IOException("Invalid encoding of DistributionPoint.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // Note that all the fields in DistributionPoint are defined as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // being OPTIONAL, i.e., there could be an empty SEQUENCE, resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // in val.data being null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        while ((val.data != null) && (val.data.available() != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            DerValue opt = val.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (opt.isContextSpecific(TAG_DIST_PT) && opt.isConstructed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                if ((fullName != null) || (relativeName != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    throw new IOException("Duplicate DistributionPointName in "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                          + "DistributionPoint.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                DerValue distPnt = opt.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                if (distPnt.isContextSpecific(TAG_FULL_NAME)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        && distPnt.isConstructed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    distPnt.resetTag(DerValue.tag_Sequence);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    fullName = new GeneralNames(distPnt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                } else if (distPnt.isContextSpecific(TAG_REL_NAME)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        && distPnt.isConstructed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    distPnt.resetTag(DerValue.tag_Set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    relativeName = new RDN(distPnt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    throw new IOException("Invalid DistributionPointName in "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                          + "DistributionPoint");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            } else if (opt.isContextSpecific(TAG_REASONS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                                && !opt.isConstructed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if (reasonFlags != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    throw new IOException("Duplicate Reasons in " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                          "DistributionPoint.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                opt.resetTag(DerValue.tag_BitString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                reasonFlags = (opt.getUnalignedBitString()).toBooleanArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            } else if (opt.isContextSpecific(TAG_ISSUER)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                                && opt.isConstructed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                if (crlIssuer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    throw new IOException("Duplicate CRLIssuer in " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                          "DistributionPoint.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                opt.resetTag(DerValue.tag_Sequence);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                crlIssuer = new GeneralNames(opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                throw new IOException("Invalid encoding of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                      "DistributionPoint.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if ((crlIssuer == null) && (fullName == null) && (relativeName == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throw new IOException("One of fullName, relativeName, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                + " and crlIssuer has to be set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Return the full distribution point name or null if not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public GeneralNames getFullName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return fullName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Return the relative distribution point name or null if not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public RDN getRelativeName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return relativeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Return the reason flags or null if not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public boolean[] getReasonFlags() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return reasonFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Return the CRL issuer name or null if not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public GeneralNames getCRLIssuer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return crlIssuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Write the DistributionPoint value to the DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param out the DerOutputStream to write the extension to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @exception IOException on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public void encode(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        DerOutputStream tagged = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // NOTE: only one of pointNames and pointRDN can be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if ((fullName != null) || (relativeName != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            DerOutputStream distributionPoint = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (fullName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                DerOutputStream derOut = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                fullName.encode(derOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                distributionPoint.writeImplicit(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    DerValue.createTag(DerValue.TAG_CONTEXT, true, TAG_FULL_NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    derOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            } else if (relativeName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                DerOutputStream derOut = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                relativeName.encode(derOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                distributionPoint.writeImplicit(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    DerValue.createTag(DerValue.TAG_CONTEXT, true, TAG_REL_NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    derOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            tagged.write(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                DerValue.createTag(DerValue.TAG_CONTEXT, true, TAG_DIST_PT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                distributionPoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (reasonFlags != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            DerOutputStream reasons = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            BitArray rf = new BitArray(reasonFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            reasons.putTruncatedUnalignedBitString(rf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            tagged.writeImplicit(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                DerValue.createTag(DerValue.TAG_CONTEXT, false, TAG_REASONS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                reasons);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (crlIssuer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            DerOutputStream issuer = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            crlIssuer.encode(issuer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            tagged.writeImplicit(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                DerValue.createTag(DerValue.TAG_CONTEXT, true, TAG_ISSUER),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                issuer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        out.write(DerValue.tag_Sequence, tagged);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Utility function for a.equals(b) where both a and b may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    private static boolean equals(Object a, Object b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return (a == null) ? (b == null) : a.equals(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Compare an object to this DistributionPoint for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param obj Object to be compared to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @return true if objects match; false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (obj instanceof DistributionPoint == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        DistributionPoint other = (DistributionPoint)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        boolean equal = equals(this.fullName, other.fullName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                     && equals(this.relativeName, other.relativeName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                     && equals(this.crlIssuer, other.crlIssuer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                     && Arrays.equals(this.reasonFlags, other.reasonFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return equal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        int hash = hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (hash == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            hash = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if (fullName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                hash += fullName.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (relativeName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                hash += relativeName.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if (crlIssuer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                hash += crlIssuer.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            if (reasonFlags != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                for (int i = 0; i < reasonFlags.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    if (reasonFlags[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        hash += i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            hashCode = hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * Return a string representation for reasonFlag bit 'reason'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    private static String reasonToString(int reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if ((reason > 0) && (reason < REASON_STRINGS.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            return REASON_STRINGS[reason];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return "Unknown reason " + reason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Return a printable string of the Distribution Point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (fullName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            sb.append("DistributionPoint:\n     " + fullName + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (relativeName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            sb.append("DistributionPoint:\n     " + relativeName + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (reasonFlags != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            sb.append("   ReasonFlags:\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            for (int i = 0; i < reasonFlags.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                if (reasonFlags[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    sb.append("    " + reasonToString(i) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (crlIssuer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            sb.append("   CRLIssuer:" + crlIssuer + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
}