01: package org.bouncycastle.asn1.cmp;
02:
03: import org.bouncycastle.asn1.ASN1Encodable;
04: import org.bouncycastle.asn1.ASN1EncodableVector;
05: import org.bouncycastle.asn1.ASN1Sequence;
06: import org.bouncycastle.asn1.ASN1TaggedObject;
07: import org.bouncycastle.asn1.DERBitString;
08: import org.bouncycastle.asn1.DERObject;
09: import org.bouncycastle.asn1.DERSequence;
10: import org.bouncycastle.asn1.DERTaggedObject;
11: import org.bouncycastle.asn1.crmf.CertId;
12: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
13:
14: public class OOBCertHash extends ASN1Encodable {
15: private AlgorithmIdentifier hashAlg;
16: private CertId certId;
17: private DERBitString hashVal;
18:
19: private OOBCertHash(ASN1Sequence seq) {
20: int index = seq.size() - 1;
21:
22: hashVal = DERBitString.getInstance(seq.getObjectAt(index--));
23:
24: for (int i = index; i >= 0; i--) {
25: ASN1TaggedObject tObj = (ASN1TaggedObject) seq
26: .getObjectAt(i);
27:
28: if (tObj.getTagNo() == 0) {
29: hashAlg = AlgorithmIdentifier.getInstance(tObj, true);
30: } else {
31: certId = CertId.getInstance(tObj, true);
32: }
33: }
34:
35: }
36:
37: public static OOBCertHash getInstance(Object o) {
38: if (o instanceof OOBCertHash) {
39: return (OOBCertHash) o;
40: }
41:
42: if (o instanceof ASN1Sequence) {
43: return new OOBCertHash((ASN1Sequence) o);
44: }
45:
46: throw new IllegalArgumentException("Invalid object: "
47: + o.getClass().getName());
48: }
49:
50: public AlgorithmIdentifier getHashAlg() {
51: return hashAlg;
52: }
53:
54: public CertId getCertId() {
55: return certId;
56: }
57:
58: /**
59: * <pre>
60: * OOBCertHash ::= SEQUENCE {
61: * hashAlg [0] AlgorithmIdentifier OPTIONAL,
62: * certId [1] CertId OPTIONAL,
63: * hashVal BIT STRING
64: * -- hashVal is calculated over the DER encoding of the
65: * -- self-signed certificate with the identifier certID.
66: * }
67: * </pre>
68: * @return a basic ASN.1 object representation.
69: */
70: public DERObject toASN1Object() {
71: ASN1EncodableVector v = new ASN1EncodableVector();
72:
73: addOptional(v, 0, hashAlg);
74: addOptional(v, 1, certId);
75:
76: v.add(hashVal);
77:
78: return new DERSequence(v);
79: }
80:
81: private void addOptional(ASN1EncodableVector v, int tagNo,
82: ASN1Encodable obj) {
83: if (obj != null) {
84: v.add(new DERTaggedObject(true, tagNo, obj));
85: }
86: }
87: }
|