Path: blob/master/src/java.base/share/classes/sun/security/x509/GeneralSubtree.java
41159 views
/*1* Copyright (c) 1997, 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.x509;2627import java.io.*;2829import sun.security.util.*;3031/**32* Represent the GeneralSubtree ASN.1 object, whose syntax is:33* <pre>34* GeneralSubtree ::= SEQUENCE {35* base GeneralName,36* minimum [0] BaseDistance DEFAULT 0,37* maximum [1] BaseDistance OPTIONAL38* }39* BaseDistance ::= INTEGER (0..MAX)40* </pre>41* @author Amit Kapoor42* @author Hemma Prafullchandra43*/44public class GeneralSubtree {45private static final byte TAG_MIN = 0;46private static final byte TAG_MAX = 1;47private static final int MIN_DEFAULT = 0;4849private GeneralName name;50private int minimum = MIN_DEFAULT;51private int maximum = -1;5253private int myhash = -1;5455/**56* The default constructor for the class.57*58* @param name the GeneralName59* @param min the minimum BaseDistance60* @param max the maximum BaseDistance61*/62public GeneralSubtree(GeneralName name, int min, int max) {63this.name = name;64this.minimum = min;65this.maximum = max;66}6768/**69* Create the object from its DER encoded form.70*71* @param val the DER encoded from of the same.72*/73public GeneralSubtree(DerValue val) throws IOException {74if (val.tag != DerValue.tag_Sequence) {75throw new IOException("Invalid encoding for GeneralSubtree.");76}77name = new GeneralName(val.data.getDerValue(), true);7879// NB. this is always encoded with the IMPLICIT tag80// The checks only make sense if we assume implicit tagging,81// with explicit tagging the form is always constructed.82while (val.data.available() != 0) {83DerValue opt = val.data.getDerValue();8485if (opt.isContextSpecific(TAG_MIN) && !opt.isConstructed()) {86opt.resetTag(DerValue.tag_Integer);87minimum = opt.getInteger();8889} else if (opt.isContextSpecific(TAG_MAX) && !opt.isConstructed()) {90opt.resetTag(DerValue.tag_Integer);91maximum = opt.getInteger();92} else93throw new IOException("Invalid encoding of GeneralSubtree.");94}95}9697/**98* Return the GeneralName.99*100* @return the GeneralName101*/102public GeneralName getName() {103//XXXX May want to consider cloning this104return name;105}106107/**108* Return the minimum BaseDistance.109*110* @return the minimum BaseDistance. Default is 0 if not set.111*/112public int getMinimum() {113return minimum;114}115116/**117* Return the maximum BaseDistance.118*119* @return the maximum BaseDistance, or -1 if not set.120*/121public int getMaximum() {122return maximum;123}124125/**126* Return a printable string of the GeneralSubtree.127*/128public String toString() {129StringBuilder sb = new StringBuilder();130sb.append("\n GeneralSubtree: [")131.append("\n GeneralName: ");132if (name != null) {133sb.append(name);134}135sb.append("\n Minimum: ")136.append(minimum)137.append("\n Maximum: ");138if (maximum == -1) {139sb.append("undefined");140} else {141sb.append(maximum);142}143sb.append(" ]\n");144return sb.toString();145}146147/**148* Compare this GeneralSubtree with another149*150* @param other GeneralSubtree to compare to this151* @return true if match152*/153public boolean equals(Object other) {154if (!(other instanceof GeneralSubtree))155return false;156GeneralSubtree otherGS = (GeneralSubtree)other;157if (this.name == null) {158if (otherGS.name != null) {159return false;160}161} else {162if (!((this.name).equals(otherGS.name)))163return false;164}165if (this.minimum != otherGS.minimum)166return false;167if (this.maximum != otherGS.maximum)168return false;169return true;170}171172/**173* Returns the hash code for this GeneralSubtree.174*175* @return a hash code value.176*/177public int hashCode() {178if (myhash == -1) {179myhash = 17;180if (name != null) {181myhash = 37 * myhash + name.hashCode();182}183if (minimum != MIN_DEFAULT) {184myhash = 37 * myhash + minimum;185}186if (maximum != -1) {187myhash = 37 * myhash + maximum;188}189}190return myhash;191}192193/**194* Encode the GeneralSubtree.195*196* @param out the DerOutputStream to encode this object to.197*/198public void encode(DerOutputStream out) throws IOException {199DerOutputStream seq = new DerOutputStream();200201name.encode(seq);202203if (minimum != MIN_DEFAULT) {204DerOutputStream tmp = new DerOutputStream();205tmp.putInteger(minimum);206seq.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,207false, TAG_MIN), tmp);208}209if (maximum != -1) {210DerOutputStream tmp = new DerOutputStream();211tmp.putInteger(maximum);212seq.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,213false, TAG_MAX), tmp);214}215out.write(DerValue.tag_Sequence, seq);216}217}218219220