View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.asn1.x509;
20  
21  import org.apache.geronimo.util.asn1.ASN1Encodable;
22  import org.apache.geronimo.util.asn1.ASN1EncodableVector;
23  import org.apache.geronimo.util.asn1.ASN1Sequence;
24  import org.apache.geronimo.util.asn1.ASN1TaggedObject;
25  import org.apache.geronimo.util.asn1.DERBitString;
26  import org.apache.geronimo.util.asn1.DEREnumerated;
27  import org.apache.geronimo.util.asn1.DERObject;
28  import org.apache.geronimo.util.asn1.DERObjectIdentifier;
29  import org.apache.geronimo.util.asn1.DERSequence;
30  import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
31  
32  
33  public class ObjectDigestInfo
34      extends ASN1Encodable
35  {
36      DEREnumerated digestedObjectType;
37  
38      DERObjectIdentifier otherObjectTypeID;
39  
40      AlgorithmIdentifier digestAlgorithm;
41  
42      DERBitString objectDigest;
43  
44      public static ObjectDigestInfo getInstance(
45              Object  obj)
46      {
47          if (obj == null || obj instanceof ObjectDigestInfo)
48          {
49              return (ObjectDigestInfo)obj;
50          }
51  
52          if (obj instanceof ASN1Sequence)
53          {
54              return new ObjectDigestInfo((ASN1Sequence)obj);
55          }
56  
57          throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
58      }
59  
60      public static ObjectDigestInfo getInstance(
61          ASN1TaggedObject obj,
62          boolean          explicit)
63      {
64          return getInstance(ASN1Sequence.getInstance(obj, explicit));
65      }
66  
67      public ObjectDigestInfo(ASN1Sequence seq)
68      {
69          digestedObjectType = DEREnumerated.getInstance(seq.getObjectAt(0));
70  
71          int offset = 0;
72  
73          if (seq.size() == 4)
74          {
75              otherObjectTypeID = DERObjectIdentifier.getInstance(seq.getObjectAt(1));
76              offset++;
77          }
78  
79          digestAlgorithm = AlgorithmIdentifier.getInstance(seq.getObjectAt(1 + offset));
80  
81          objectDigest = new DERBitString(seq.getObjectAt(2 + offset));
82      }
83  
84      public DEREnumerated getDigestedObjectType()
85      {
86          return digestedObjectType;
87      }
88  
89      public DERObjectIdentifier getOtherObjectTypeID()
90      {
91          return otherObjectTypeID;
92      }
93  
94      public AlgorithmIdentifier getDigestAlgorithm()
95      {
96          return digestAlgorithm;
97      }
98  
99      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 }