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: import java.util.Enumeration;
020:
021: /**
022: * BER TaggedObject - in ASN.1 nottation this is any object proceeded by
023: * a [n] where n is some number - these are assume to follow the construction
024: * rules (as with sequences).
025: */
026: public class BERTaggedObject extends DERTaggedObject {
027: /**
028: * @param tagNo the tag number for this object.
029: * @param obj the tagged object.
030: */
031: public BERTaggedObject(int tagNo, DEREncodable obj) {
032: super (tagNo, obj);
033: }
034:
035: /**
036: * @param explicit true if an explicitly tagged object.
037: * @param tagNo the tag number for this object.
038: * @param obj the tagged object.
039: */
040: public BERTaggedObject(boolean explicit, int tagNo, DEREncodable obj) {
041: super (explicit, tagNo, obj);
042: }
043:
044: /**
045: * create an implicitly tagged object that contains a zero
046: * length sequence.
047: */
048: public BERTaggedObject(int tagNo) {
049: super (false, tagNo, new BERSequence());
050: }
051:
052: void encode(DEROutputStream out) throws IOException {
053: if (out instanceof ASN1OutputStream
054: || out instanceof BEROutputStream) {
055: out.write(CONSTRUCTED | TAGGED | tagNo);
056: out.write(0x80);
057:
058: if (!empty) {
059: if (!explicit) {
060: if (obj instanceof ASN1OctetString) {
061: Enumeration e;
062:
063: if (obj instanceof BERConstructedOctetString) {
064: e = ((BERConstructedOctetString) obj)
065: .getObjects();
066: } else {
067: ASN1OctetString octs = (ASN1OctetString) obj;
068: BERConstructedOctetString berO = new BERConstructedOctetString(
069: octs.getOctets());
070:
071: e = berO.getObjects();
072: }
073:
074: while (e.hasMoreElements()) {
075: out.writeObject(e.nextElement());
076: }
077: } else if (obj instanceof ASN1Sequence) {
078: Enumeration e = ((ASN1Sequence) obj)
079: .getObjects();
080:
081: while (e.hasMoreElements()) {
082: out.writeObject(e.nextElement());
083: }
084: } else if (obj instanceof ASN1Set) {
085: Enumeration e = ((ASN1Set) obj).getObjects();
086:
087: while (e.hasMoreElements()) {
088: out.writeObject(e.nextElement());
089: }
090: } else {
091: throw new RuntimeException("not implemented: "
092: + obj.getClass().getName());
093: }
094: } else {
095: out.writeObject(obj);
096: }
097: }
098:
099: out.write(0x00);
100: out.write(0x00);
101: } else {
102: super.encode(out);
103: }
104: }
105: }
|