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