001: package org.bouncycastle.asn1.x509;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1Sequence;
006: import org.bouncycastle.asn1.ASN1TaggedObject;
007: import org.bouncycastle.asn1.DERBitString;
008: import org.bouncycastle.asn1.DEREnumerated;
009: import org.bouncycastle.asn1.DERObject;
010: import org.bouncycastle.asn1.DERObjectIdentifier;
011: import org.bouncycastle.asn1.DERSequence;
012:
013: /**
014: * ObjectDigestInfo ASN.1 structure used in v2 attribute certificates.
015: *
016: * <pre>
017: *
018: * ObjectDigestInfo ::= SEQUENCE {
019: * digestedObjectType ENUMERATED {
020: * publicKey (0),
021: * publicKeyCert (1),
022: * otherObjectTypes (2) },
023: * -- otherObjectTypes MUST NOT
024: * -- be used in this profile
025: * otherObjectTypeID OBJECT IDENTIFIER OPTIONAL,
026: * digestAlgorithm AlgorithmIdentifier,
027: * objectDigest BIT STRING
028: * }
029: *
030: * </pre>
031: *
032: */
033: public class ObjectDigestInfo extends ASN1Encodable {
034: /**
035: * The public key is hashed.
036: */
037: public final static int publicKey = 0;
038:
039: /**
040: * The public key certificate is hashed.
041: */
042: public final static int publicKeyCert = 1;
043:
044: /**
045: * An other object is hashed.
046: */
047: public final static int otherObjectDigest = 2;
048:
049: DEREnumerated digestedObjectType;
050:
051: DERObjectIdentifier otherObjectTypeID;
052:
053: AlgorithmIdentifier digestAlgorithm;
054:
055: DERBitString objectDigest;
056:
057: public static ObjectDigestInfo getInstance(Object obj) {
058: if (obj == null || obj instanceof ObjectDigestInfo) {
059: return (ObjectDigestInfo) obj;
060: }
061:
062: if (obj instanceof ASN1Sequence) {
063: return new ObjectDigestInfo((ASN1Sequence) obj);
064: }
065:
066: throw new IllegalArgumentException(
067: "illegal object in getInstance: "
068: + obj.getClass().getName());
069: }
070:
071: public static ObjectDigestInfo getInstance(ASN1TaggedObject obj,
072: boolean explicit) {
073: return getInstance(ASN1Sequence.getInstance(obj, explicit));
074: }
075:
076: /**
077: * Constructor from given details.
078: * <p>
079: * If <code>digestedObjectType</code> is not {@link #publicKeyCert} or
080: * {@link #publicKey} <code>otherObjectTypeID</code> must be given,
081: * otherwise it is ignored.
082: *
083: * @param digestedObjectType The digest object type.
084: * @param otherObjectTypeID The object type ID for
085: * <code>otherObjectDigest</code>.
086: * @param digestAlgorithm The algorithm identifier for the hash.
087: * @param objectDigest The hash value.
088: */
089: public ObjectDigestInfo(int digestedObjectType,
090: String otherObjectTypeID,
091: AlgorithmIdentifier digestAlgorithm, byte[] objectDigest) {
092: this .digestedObjectType = new DEREnumerated(digestedObjectType);
093: if (digestedObjectType == otherObjectDigest) {
094: this .otherObjectTypeID = new DERObjectIdentifier(
095: otherObjectTypeID);
096: }
097:
098: this .digestAlgorithm = digestAlgorithm;
099:
100: this .objectDigest = new DERBitString(objectDigest);
101: }
102:
103: private ObjectDigestInfo(ASN1Sequence seq) {
104: if (seq.size() > 4 || seq.size() < 3) {
105: throw new IllegalArgumentException("Bad sequence size: "
106: + seq.size());
107: }
108:
109: digestedObjectType = DEREnumerated.getInstance(seq
110: .getObjectAt(0));
111:
112: int offset = 0;
113:
114: if (seq.size() == 4) {
115: otherObjectTypeID = DERObjectIdentifier.getInstance(seq
116: .getObjectAt(1));
117: offset++;
118: }
119:
120: digestAlgorithm = AlgorithmIdentifier.getInstance(seq
121: .getObjectAt(1 + offset));
122:
123: objectDigest = DERBitString.getInstance(seq
124: .getObjectAt(2 + offset));
125: }
126:
127: public DEREnumerated getDigestedObjectType() {
128: return digestedObjectType;
129: }
130:
131: public DERObjectIdentifier getOtherObjectTypeID() {
132: return otherObjectTypeID;
133: }
134:
135: public AlgorithmIdentifier getDigestAlgorithm() {
136: return digestAlgorithm;
137: }
138:
139: public DERBitString getObjectDigest() {
140: return objectDigest;
141: }
142:
143: /**
144: * Produce an object suitable for an ASN1OutputStream.
145: *
146: * <pre>
147: *
148: * ObjectDigestInfo ::= SEQUENCE {
149: * digestedObjectType ENUMERATED {
150: * publicKey (0),
151: * publicKeyCert (1),
152: * otherObjectTypes (2) },
153: * -- otherObjectTypes MUST NOT
154: * -- be used in this profile
155: * otherObjectTypeID OBJECT IDENTIFIER OPTIONAL,
156: * digestAlgorithm AlgorithmIdentifier,
157: * objectDigest BIT STRING
158: * }
159: *
160: * </pre>
161: */
162: public DERObject toASN1Object() {
163: ASN1EncodableVector v = new ASN1EncodableVector();
164:
165: v.add(digestedObjectType);
166:
167: if (otherObjectTypeID != null) {
168: v.add(otherObjectTypeID);
169: }
170:
171: v.add(digestAlgorithm);
172: v.add(objectDigest);
173:
174: return new DERSequence(v);
175: }
176: }
|