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