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.util.asn1.x509;
019    
020    import org.apache.geronimo.util.asn1.ASN1Encodable;
021    import org.apache.geronimo.util.asn1.ASN1EncodableVector;
022    import org.apache.geronimo.util.asn1.ASN1Sequence;
023    import org.apache.geronimo.util.asn1.ASN1TaggedObject;
024    import org.apache.geronimo.util.asn1.DERBitString;
025    import org.apache.geronimo.util.asn1.DEREnumerated;
026    import org.apache.geronimo.util.asn1.DERObject;
027    import org.apache.geronimo.util.asn1.DERObjectIdentifier;
028    import org.apache.geronimo.util.asn1.DERSequence;
029    import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
030    
031    
032    public class ObjectDigestInfo
033        extends ASN1Encodable
034    {
035        DEREnumerated digestedObjectType;
036    
037        DERObjectIdentifier otherObjectTypeID;
038    
039        AlgorithmIdentifier digestAlgorithm;
040    
041        DERBitString objectDigest;
042    
043        public static ObjectDigestInfo getInstance(
044                Object  obj)
045        {
046            if (obj == null || obj instanceof ObjectDigestInfo)
047            {
048                return (ObjectDigestInfo)obj;
049            }
050    
051            if (obj instanceof ASN1Sequence)
052            {
053                return new ObjectDigestInfo((ASN1Sequence)obj);
054            }
055    
056            throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
057        }
058    
059        public static ObjectDigestInfo getInstance(
060            ASN1TaggedObject obj,
061            boolean          explicit)
062        {
063            return getInstance(ASN1Sequence.getInstance(obj, explicit));
064        }
065    
066        public ObjectDigestInfo(ASN1Sequence seq)
067        {
068            digestedObjectType = DEREnumerated.getInstance(seq.getObjectAt(0));
069    
070            int offset = 0;
071    
072            if (seq.size() == 4)
073            {
074                otherObjectTypeID = DERObjectIdentifier.getInstance(seq.getObjectAt(1));
075                offset++;
076            }
077    
078            digestAlgorithm = AlgorithmIdentifier.getInstance(seq.getObjectAt(1 + offset));
079    
080            objectDigest = new DERBitString(seq.getObjectAt(2 + offset));
081        }
082    
083        public DEREnumerated getDigestedObjectType()
084        {
085            return digestedObjectType;
086        }
087    
088        public DERObjectIdentifier getOtherObjectTypeID()
089        {
090            return otherObjectTypeID;
091        }
092    
093        public AlgorithmIdentifier getDigestAlgorithm()
094        {
095            return digestAlgorithm;
096        }
097    
098        public DERBitString getObjectDigest()
099        {
100            return objectDigest;
101        }
102    
103        /**
104         * Produce an object suitable for an ASN1OutputStream.
105         *
106         * <pre>
107         *
108         *   ObjectDigestInfo ::= SEQUENCE {
109         *        digestedObjectType  ENUMERATED {
110         *                publicKey            (0),
111         *                publicKeyCert        (1),
112         *                otherObjectTypes     (2) },
113         *                        -- otherObjectTypes MUST NOT
114         *                        -- be used in this profile
115         *        otherObjectTypeID   OBJECT IDENTIFIER OPTIONAL,
116         *        digestAlgorithm     AlgorithmIdentifier,
117         *        objectDigest        BIT STRING
118         *   }
119         *
120         * </pre>
121         */
122        public DERObject toASN1Object()
123        {
124            ASN1EncodableVector v = new ASN1EncodableVector();
125    
126            v.add(digestedObjectType);
127    
128            if (otherObjectTypeID != null)
129            {
130                v.add(otherObjectTypeID);
131            }
132    
133            v.add(digestAlgorithm);
134            v.add(objectDigest);
135    
136            return new DERSequence(v);
137        }
138    }