001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.crypto.asn1;
017:
018: import java.io.IOException;
019:
020: /**
021: * ASN.1 TaggedObject - in ASN.1 nottation this is any object proceeded by
022: * a [n] where n is some number - these are assume to follow the construction
023: * rules (as with sequences).
024: */
025: public abstract class ASN1TaggedObject extends DERObject {
026: int tagNo;
027: boolean empty = false;
028: boolean explicit = true;
029: DEREncodable obj = null;
030:
031: static public ASN1TaggedObject getInstance(ASN1TaggedObject obj,
032: boolean explicit) {
033: if (explicit) {
034: return (ASN1TaggedObject) obj.getObject();
035: }
036:
037: throw new IllegalArgumentException(
038: "implicitly tagged tagged object");
039: }
040:
041: /**
042: * Create a tagged object in the explicit style.
043: *
044: * @param tagNo the tag number for this object.
045: * @param obj the tagged object.
046: */
047: public ASN1TaggedObject(int tagNo, DEREncodable obj) {
048: this .explicit = true;
049: this .tagNo = tagNo;
050: this .obj = obj;
051: }
052:
053: /**
054: * Create a tagged object with the style given by the value of explicit.
055: * <p>
056: * If the object implements ASN1Choice the tag style will always be changed
057: * to explicit in accordance with the ASN.1 encoding rules.
058: * </p>
059: * @param explicit true if the object is explicitly tagged.
060: * @param tagNo the tag number for this object.
061: * @param obj the tagged object.
062: */
063: public ASN1TaggedObject(boolean explicit, int tagNo,
064: DEREncodable obj) {
065: if (obj instanceof ASN1Choice) {
066: this .explicit = true;
067: } else {
068: this .explicit = explicit;
069: }
070:
071: this .tagNo = tagNo;
072: this .obj = obj;
073: }
074:
075: public boolean equals(Object o) {
076: if (o == null || !(o instanceof ASN1TaggedObject)) {
077: return false;
078: }
079:
080: ASN1TaggedObject other = (ASN1TaggedObject) o;
081:
082: if (tagNo != other.tagNo || empty != other.empty
083: || explicit != other.explicit) {
084: return false;
085: }
086:
087: if (obj == null) {
088: if (other.obj != null) {
089: return false;
090: }
091: } else {
092: if (!(obj.equals(other.obj))) {
093: return false;
094: }
095: }
096:
097: return true;
098: }
099:
100: public int hashCode() {
101: int code = tagNo;
102:
103: if (obj != null) {
104: code ^= obj.hashCode();
105: }
106:
107: return code;
108: }
109:
110: public int getTagNo() {
111: return tagNo;
112: }
113:
114: /**
115: * return whether or not the object may be explicitly tagged.
116: * <p>
117: * Note: if the object has been read from an input stream, the only
118: * time you can be sure if isExplicit is returning the true state of
119: * affairs is if it returns false. An implicitly tagged object may appear
120: * to be explicitly tagged, so you need to understand the context under
121: * which the reading was done as well, see getObject below.
122: */
123: public boolean isExplicit() {
124: return explicit;
125: }
126:
127: public boolean isEmpty() {
128: return empty;
129: }
130:
131: /**
132: * return whatever was following the tag.
133: * <p>
134: * Note: tagged objects are generally context dependent if you're
135: * trying to extract a tagged object you should be going via the
136: * appropriate getInstance method.
137: */
138: public DERObject getObject() {
139: if (obj != null) {
140: return obj.getDERObject();
141: }
142:
143: return null;
144: }
145:
146: abstract void encode(DEROutputStream out) throws IOException;
147: }
|