001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    package org.apache.geronimo.util.asn1.x509;
020    
021    import org.apache.geronimo.util.asn1.ASN1Encodable;
022    import org.apache.geronimo.util.asn1.ASN1EncodableVector;
023    import org.apache.geronimo.util.asn1.ASN1Sequence;
024    import org.apache.geronimo.util.asn1.ASN1TaggedObject;
025    import org.apache.geronimo.util.asn1.DERBitString;
026    import org.apache.geronimo.util.asn1.DEREnumerated;
027    import org.apache.geronimo.util.asn1.DERObject;
028    import org.apache.geronimo.util.asn1.DERObjectIdentifier;
029    import org.apache.geronimo.util.asn1.DERSequence;
030    import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
031    
032    
033    public class ObjectDigestInfo
034        extends ASN1Encodable
035    {
036        DEREnumerated digestedObjectType;
037    
038        DERObjectIdentifier otherObjectTypeID;
039    
040        AlgorithmIdentifier digestAlgorithm;
041    
042        DERBitString objectDigest;
043    
044        public static ObjectDigestInfo getInstance(
045                Object  obj)
046        {
047            if (obj == null || obj instanceof ObjectDigestInfo)
048            {
049                return (ObjectDigestInfo)obj;
050            }
051    
052            if (obj instanceof ASN1Sequence)
053            {
054                return new ObjectDigestInfo((ASN1Sequence)obj);
055            }
056    
057            throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
058        }
059    
060        public static ObjectDigestInfo getInstance(
061            ASN1TaggedObject obj,
062            boolean          explicit)
063        {
064            return getInstance(ASN1Sequence.getInstance(obj, explicit));
065        }
066    
067        public ObjectDigestInfo(ASN1Sequence seq)
068        {
069            digestedObjectType = DEREnumerated.getInstance(seq.getObjectAt(0));
070    
071            int offset = 0;
072    
073            if (seq.size() == 4)
074            {
075                otherObjectTypeID = DERObjectIdentifier.getInstance(seq.getObjectAt(1));
076                offset++;
077            }
078    
079            digestAlgorithm = AlgorithmIdentifier.getInstance(seq.getObjectAt(1 + offset));
080    
081            objectDigest = new DERBitString(seq.getObjectAt(2 + offset));
082        }
083    
084        public DEREnumerated getDigestedObjectType()
085        {
086            return digestedObjectType;
087        }
088    
089        public DERObjectIdentifier getOtherObjectTypeID()
090        {
091            return otherObjectTypeID;
092        }
093    
094        public AlgorithmIdentifier getDigestAlgorithm()
095        {
096            return digestAlgorithm;
097        }
098    
099        public DERBitString getObjectDigest()
100        {
101            return objectDigest;
102        }
103    
104        /**
105         * Produce an object suitable for an ASN1OutputStream.
106         *
107         * <pre>
108         *
109         *   ObjectDigestInfo ::= SEQUENCE {
110         *        digestedObjectType  ENUMERATED {
111         *                publicKey            (0),
112         *                publicKeyCert        (1),
113         *                otherObjectTypes     (2) },
114         *                        -- otherObjectTypes MUST NOT
115         *                        -- be used in this profile
116         *        otherObjectTypeID   OBJECT IDENTIFIER OPTIONAL,
117         *        digestAlgorithm     AlgorithmIdentifier,
118         *        objectDigest        BIT STRING
119         *   }
120         *
121         * </pre>
122         */
123        public DERObject toASN1Object()
124        {
125            ASN1EncodableVector v = new ASN1EncodableVector();
126    
127            v.add(digestedObjectType);
128    
129            if (otherObjectTypeID != null)
130            {
131                v.add(otherObjectTypeID);
132            }
133    
134            v.add(digestAlgorithm);
135            v.add(objectDigest);
136    
137            return new DERSequence(v);
138        }
139    }