jdk/src/share/classes/sun/security/x509/GeneralSubtree.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 1997-2004 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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Represent the GeneralSubtree ASN.1 object, whose syntax is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * GeneralSubtree ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *    base             GeneralName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *    minimum  [0]     BaseDistance DEFAULT 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *    maximum  [1]     BaseDistance OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * BaseDistance ::= INTEGER (0..MAX)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class GeneralSubtree {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final byte TAG_MIN = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final byte TAG_MAX = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final int  MIN_DEFAULT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private GeneralName name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private int         minimum = MIN_DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int         maximum = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private int myhash = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * The default constructor for the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @params name the GeneralName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @params min the minimum BaseDistance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @params max the maximum BaseDistance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public GeneralSubtree(GeneralName name, int min, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.minimum = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.maximum = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Create the object from its DER encoded form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param val the DER encoded from of the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public GeneralSubtree(DerValue val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (val.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new IOException("Invalid encoding for GeneralSubtree.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        name = new GeneralName(val.data.getDerValue(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // NB. this is always encoded with the IMPLICIT tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // The checks only make sense if we assume implicit tagging,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // with explicit tagging the form is always constructed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        while (val.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            DerValue opt = val.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (opt.isContextSpecific(TAG_MIN) && !opt.isConstructed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                opt.resetTag(DerValue.tag_Integer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                minimum = opt.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            } else if (opt.isContextSpecific(TAG_MAX) && !opt.isConstructed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                opt.resetTag(DerValue.tag_Integer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                maximum = opt.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                throw new IOException("Invalid encoding of GeneralSubtree.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Return the GeneralName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @return the GeneralName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public GeneralName getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        //XXXX May want to consider cloning this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Return the minimum BaseDistance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @return the minimum BaseDistance. Default is 0 if not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public int getMinimum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Return the maximum BaseDistance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @return the maximum BaseDistance, or -1 if not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public int getMaximum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Return a printable string of the GeneralSubtree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        String s = "\n   GeneralSubtree: [\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            "    GeneralName: " + ((name == null) ? "" : name.toString()) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            "\n    Minimum: " + minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (maximum == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                s += "\t    Maximum: undefined";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                s += "\t    Maximum: " + maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            s += "    ]\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Compare this GeneralSubtree with another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param other GeneralSubtree to compare to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @returns true if match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (!(other instanceof GeneralSubtree))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        GeneralSubtree otherGS = (GeneralSubtree)other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (this.name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (otherGS.name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (!((this.name).equals(otherGS.name)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (this.minimum != otherGS.minimum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (this.maximum != otherGS.maximum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Returns the hash code for this GeneralSubtree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @return a hash code value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (myhash == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            myhash = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                myhash = 37 * myhash + name.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            if (minimum != MIN_DEFAULT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                myhash = 37 * myhash + minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (maximum != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                myhash = 37 * myhash + maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return myhash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Encode the GeneralSubtree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @params out the DerOutputStream to encode this object to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public void encode(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        DerOutputStream seq = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        name.encode(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (minimum != MIN_DEFAULT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            tmp.putInteger(minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            seq.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                              false, TAG_MIN), tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (maximum != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            tmp.putInteger(maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            seq.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                              false, TAG_MAX), tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        out.write(DerValue.tag_Sequence, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}