~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/asn1/cms/EnvelopedData.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.cms;
 
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.ASN1Set;
 
9
import org.bouncycastle.asn1.ASN1TaggedObject;
 
10
import org.bouncycastle.asn1.BERSequence;
 
11
import org.bouncycastle.asn1.DERInteger;
 
12
import org.bouncycastle.asn1.DERObject;
 
13
import org.bouncycastle.asn1.DERTaggedObject;
 
14
 
 
15
public class EnvelopedData
 
16
    extends ASN1Encodable
 
17
{
 
18
    private DERInteger              version;
 
19
    private OriginatorInfo          originatorInfo;
 
20
    private ASN1Set                 recipientInfos;
 
21
    private EncryptedContentInfo    encryptedContentInfo;
 
22
    private ASN1Set                 unprotectedAttrs;
 
23
 
 
24
    public EnvelopedData(
 
25
        OriginatorInfo          originatorInfo,
 
26
        ASN1Set                 recipientInfos,
 
27
        EncryptedContentInfo    encryptedContentInfo,
 
28
        ASN1Set                 unprotectedAttrs)
 
29
    {
 
30
        if (originatorInfo != null || unprotectedAttrs != null)
 
31
        {
 
32
            version = new DERInteger(2);
 
33
        }
 
34
        else
 
35
        {
 
36
            version = new DERInteger(0);
 
37
 
 
38
            Enumeration e = recipientInfos.getObjects();
 
39
 
 
40
            while (e.hasMoreElements())
 
41
            {
 
42
                RecipientInfo   ri = RecipientInfo.getInstance(e.nextElement());
 
43
 
 
44
                if (!ri.getVersion().equals(version))
 
45
                {
 
46
                    version = new DERInteger(2);
 
47
                    break;
 
48
                }
 
49
            }
 
50
        }
 
51
 
 
52
        this.originatorInfo = originatorInfo;
 
53
        this.recipientInfos = recipientInfos;
 
54
        this.encryptedContentInfo = encryptedContentInfo;
 
55
        this.unprotectedAttrs = unprotectedAttrs;
 
56
    }
 
57
                         
 
58
    public EnvelopedData(
 
59
        ASN1Sequence seq)
 
60
    {
 
61
        int     index = 0;
 
62
        
 
63
        version = (DERInteger)seq.getObjectAt(index++);
 
64
        
 
65
        Object  tmp = seq.getObjectAt(index++);
 
66
 
 
67
        if (tmp instanceof ASN1TaggedObject)
 
68
        {
 
69
            originatorInfo = OriginatorInfo.getInstance((ASN1TaggedObject)tmp, false);
 
70
            tmp = seq.getObjectAt(index++);
 
71
        }
 
72
 
 
73
        recipientInfos = ASN1Set.getInstance(tmp);
 
74
        
 
75
        encryptedContentInfo = EncryptedContentInfo.getInstance(seq.getObjectAt(index++));
 
76
        
 
77
        if(seq.size() > index)
 
78
        {
 
79
            unprotectedAttrs = ASN1Set.getInstance((ASN1TaggedObject)seq.getObjectAt(index), false);
 
80
        }
 
81
    }
 
82
    
 
83
    /**
 
84
     * return an EnvelopedData object from a tagged object.
 
85
     *
 
86
     * @param obj the tagged object holding the object we want.
 
87
     * @param explicit true if the object is meant to be explicitly
 
88
     *              tagged false otherwise.
 
89
     * @exception IllegalArgumentException if the object held by the
 
90
     *          tagged object cannot be converted.
 
91
     */
 
92
    public static EnvelopedData getInstance(
 
93
        ASN1TaggedObject obj,
 
94
        boolean explicit)
 
95
    {
 
96
        return getInstance(ASN1Sequence.getInstance(obj, explicit));
 
97
    }
 
98
    
 
99
    /**
 
100
     * return an EnvelopedData object from the given object.
 
101
     *
 
102
     * @param obj the object we want converted.
 
103
     * @exception IllegalArgumentException if the object cannot be converted.
 
104
     */
 
105
    public static EnvelopedData getInstance(
 
106
        Object obj)
 
107
    {
 
108
        if (obj == null || obj instanceof EnvelopedData)
 
109
        {
 
110
            return (EnvelopedData)obj;
 
111
        }
 
112
        
 
113
        if (obj instanceof ASN1Sequence)
 
114
        {
 
115
            return new EnvelopedData((ASN1Sequence)obj);
 
116
        }
 
117
        
 
118
        throw new IllegalArgumentException("Invalid EnvelopedData: " + obj.getClass().getName());
 
119
    }
 
120
 
 
121
    public DERInteger getVersion()
 
122
    {
 
123
        return version;
 
124
    }
 
125
    
 
126
    public OriginatorInfo getOriginatorInfo()
 
127
    {
 
128
        return originatorInfo;
 
129
    }
 
130
 
 
131
    public ASN1Set getRecipientInfos()
 
132
    {
 
133
        return recipientInfos;
 
134
    }
 
135
 
 
136
    public EncryptedContentInfo getEncryptedContentInfo()
 
137
    {
 
138
        return encryptedContentInfo;
 
139
    }
 
140
 
 
141
    public ASN1Set getUnprotectedAttrs()
 
142
    {
 
143
        return unprotectedAttrs;
 
144
    }
 
145
 
 
146
    /** 
 
147
     * Produce an object suitable for an ASN1OutputStream.
 
148
     * <pre>
 
149
     * EnvelopedData ::= SEQUENCE {
 
150
     *     version CMSVersion,
 
151
     *     originatorInfo [0] IMPLICIT OriginatorInfo OPTIONAL,
 
152
     *     recipientInfos RecipientInfos,
 
153
     *     encryptedContentInfo EncryptedContentInfo,
 
154
     *     unprotectedAttrs [1] IMPLICIT UnprotectedAttributes OPTIONAL 
 
155
     * }
 
156
     * </pre>
 
157
     */
 
158
    public DERObject toASN1Object()
 
159
    {
 
160
        ASN1EncodableVector  v = new ASN1EncodableVector();
 
161
        
 
162
        v.add(version);
 
163
 
 
164
        if (originatorInfo != null)
 
165
        {
 
166
            v.add(new DERTaggedObject(false, 0, originatorInfo));
 
167
        }
 
168
 
 
169
        v.add(recipientInfos);
 
170
        v.add(encryptedContentInfo);
 
171
 
 
172
        if (unprotectedAttrs != null)
 
173
        {
 
174
            v.add(new DERTaggedObject(false, 1, unprotectedAttrs));
 
175
        }
 
176
        
 
177
        return new BERSequence(v);
 
178
    }
 
179
}