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:
12: import java.util.Enumeration;
13:
14: public class PKIMessage extends ASN1Encodable {
15: private PKIHeader header;
16: private PKIBody body;
17: private DERBitString protection;
18: private ASN1Sequence extraCerts;
19:
20: private PKIMessage(ASN1Sequence seq) {
21: Enumeration en = seq.getObjects();
22:
23: header = PKIHeader.getInstance(en.nextElement());
24: body = PKIBody.getInstance(en.nextElement());
25:
26: while (en.hasMoreElements()) {
27: ASN1TaggedObject tObj = (ASN1TaggedObject) en.nextElement();
28:
29: if (tObj.getTagNo() == 0) {
30: protection = DERBitString.getInstance(tObj, true);
31: } else {
32: extraCerts = ASN1Sequence.getInstance(tObj, true);
33: }
34: }
35: }
36:
37: public static PKIMessage getInstance(Object o) {
38: if (o instanceof PKIMessage) {
39: return (PKIMessage) o;
40: }
41:
42: if (o instanceof ASN1Sequence) {
43: return new PKIMessage((ASN1Sequence) o);
44: }
45:
46: throw new IllegalArgumentException("Invalid object: "
47: + o.getClass().getName());
48: }
49:
50: public PKIHeader getHeader() {
51: return header;
52: }
53:
54: public PKIBody getBody() {
55: return body;
56: }
57:
58: /**
59: * <pre>
60: * PKIMessage ::= SEQUENCE {
61: * header PKIHeader,
62: * body PKIBody,
63: * protection [0] PKIProtection OPTIONAL,
64: * extraCerts [1] SEQUENCE SIZE (1..MAX) OF CMPCertificate
65: * OPTIONAL
66: * }
67: * </pre>
68: * @return a basic ASN.1 object representation.
69: */
70: public DERObject toASN1Object() {
71: ASN1EncodableVector v = new ASN1EncodableVector();
72:
73: v.add(header);
74: v.add(body);
75:
76: addOptional(v, 0, protection);
77: addOptional(v, 1, extraCerts);
78:
79: return new DERSequence(v);
80: }
81:
82: private void addOptional(ASN1EncodableVector v, int tagNo,
83: ASN1Encodable obj) {
84: if (obj != null) {
85: v.add(new DERTaggedObject(true, tagNo, obj));
86: }
87: }
88: }
|