~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/asn1/cmp/PKIMessage.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.asn1.cmp;
 
2
 
 
3
import java.util.Enumeration;
 
4
 
 
5
import org.bouncycastle.asn1.ASN1Encodable;
 
6
import org.bouncycastle.asn1.ASN1EncodableVector;
 
7
import org.bouncycastle.asn1.ASN1Sequence;
 
8
import org.bouncycastle.asn1.ASN1TaggedObject;
 
9
import org.bouncycastle.asn1.DERBitString;
 
10
import org.bouncycastle.asn1.DERObject;
 
11
import org.bouncycastle.asn1.DERSequence;
 
12
import org.bouncycastle.asn1.DERTaggedObject;
 
13
 
 
14
public class PKIMessage
 
15
    extends ASN1Encodable
 
16
{
 
17
    private PKIHeader header;
 
18
    private PKIBody body;
 
19
    private DERBitString protection;
 
20
    private ASN1Sequence extraCerts;
 
21
 
 
22
    private PKIMessage(ASN1Sequence seq)
 
23
    {
 
24
        Enumeration en = seq.getObjects();
 
25
 
 
26
        header = PKIHeader.getInstance(en.nextElement());
 
27
        body = PKIBody.getInstance(en.nextElement());
 
28
 
 
29
        while (en.hasMoreElements())
 
30
        {
 
31
            ASN1TaggedObject tObj = (ASN1TaggedObject)en.nextElement();
 
32
 
 
33
            if (tObj.getTagNo() == 0)
 
34
            {
 
35
                protection = DERBitString.getInstance(tObj, true);
 
36
            }
 
37
            else
 
38
            {
 
39
                extraCerts = ASN1Sequence.getInstance(tObj, true);
 
40
            }
 
41
        }
 
42
    }
 
43
 
 
44
    public static PKIMessage getInstance(Object o)
 
45
    {
 
46
        if (o instanceof PKIMessage)
 
47
        {
 
48
            return (PKIMessage)o;
 
49
        }
 
50
        else if (o != null)
 
51
        {
 
52
            return new PKIMessage(ASN1Sequence.getInstance(o));
 
53
        }
 
54
 
 
55
        return null;
 
56
    }
 
57
 
 
58
    /**
 
59
     * Creates a new PKIMessage.
 
60
     *
 
61
     * @param header message header
 
62
     * @param body message body
 
63
     * @param protection message protection (may be null)
 
64
     * @param extraCerts extra certificates (may be null)
 
65
     */
 
66
    public PKIMessage(
 
67
        PKIHeader header,
 
68
        PKIBody body,
 
69
        DERBitString protection,
 
70
        CMPCertificate[] extraCerts)
 
71
    {
 
72
        this.header = header;
 
73
        this.body = body;
 
74
        this.protection = protection;
 
75
        if (extraCerts != null) {
 
76
            ASN1EncodableVector v = new ASN1EncodableVector();
 
77
            for (int i = 0; i < extraCerts.length; i++) {
 
78
                v.add(extraCerts[i]);
 
79
            }
 
80
            this.extraCerts = new DERSequence(v);
 
81
        }
 
82
    }
 
83
 
 
84
    public PKIMessage(
 
85
        PKIHeader header,
 
86
        PKIBody body,
 
87
        DERBitString protection)
 
88
    {
 
89
        this(header, body, protection, null);
 
90
    }
 
91
 
 
92
    public PKIMessage(
 
93
        PKIHeader header,
 
94
        PKIBody   body)
 
95
    {
 
96
        this(header, body, null, null);
 
97
    }
 
98
 
 
99
    public PKIHeader getHeader()
 
100
    {
 
101
        return header;
 
102
    }
 
103
 
 
104
    public PKIBody getBody()
 
105
    {
 
106
        return body;
 
107
    }
 
108
 
 
109
    public DERBitString getProtection()
 
110
    {
 
111
        return protection;
 
112
    }
 
113
 
 
114
    public CMPCertificate[] getExtraCerts()
 
115
    {
 
116
        if (extraCerts == null)
 
117
        {
 
118
            return null;
 
119
        }
 
120
 
 
121
        CMPCertificate[] results = new CMPCertificate[extraCerts.size()];
 
122
 
 
123
        for (int i = 0; i < results.length; i++)
 
124
        {
 
125
            results[i] = CMPCertificate.getInstance(extraCerts.getObjectAt(i));
 
126
        }
 
127
        return results;
 
128
    }
 
129
 
 
130
    /**
 
131
     * <pre>
 
132
     * PKIMessage ::= SEQUENCE {
 
133
     *                  header           PKIHeader,
 
134
     *                  body             PKIBody,
 
135
     *                  protection   [0] PKIProtection OPTIONAL,
 
136
     *                  extraCerts   [1] SEQUENCE SIZE (1..MAX) OF CMPCertificate
 
137
     *                                                                     OPTIONAL
 
138
     * }
 
139
     * </pre>
 
140
     * @return a basic ASN.1 object representation.
 
141
     */
 
142
    public DERObject toASN1Object()
 
143
    {
 
144
        ASN1EncodableVector v = new ASN1EncodableVector();
 
145
 
 
146
        v.add(header);
 
147
        v.add(body);
 
148
 
 
149
        addOptional(v, 0, protection);
 
150
        addOptional(v, 1, extraCerts);
 
151
 
 
152
        return new DERSequence(v);
 
153
    }
 
154
    
 
155
    private void addOptional(ASN1EncodableVector v, int tagNo, ASN1Encodable obj)
 
156
    {
 
157
        if (obj != null)
 
158
        {
 
159
            v.add(new DERTaggedObject(true, tagNo, obj));
 
160
        }
 
161
    }
 
162
}