SignerInfo.java revision a198e1ecc615e26a167d0f2dca9fa7e5fc62de10
1package org.bouncycastle.asn1.cms;
2
3import java.util.Enumeration;
4
5import org.bouncycastle.asn1.ASN1EncodableVector;
6import org.bouncycastle.asn1.ASN1Integer;
7import org.bouncycastle.asn1.ASN1Object;
8import org.bouncycastle.asn1.ASN1OctetString;
9import org.bouncycastle.asn1.ASN1Primitive;
10import org.bouncycastle.asn1.ASN1Sequence;
11import org.bouncycastle.asn1.ASN1Set;
12import org.bouncycastle.asn1.ASN1TaggedObject;
13import org.bouncycastle.asn1.DEROctetString;
14import org.bouncycastle.asn1.DERSequence;
15import org.bouncycastle.asn1.DERTaggedObject;
16import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
17
18public class SignerInfo
19    extends ASN1Object
20{
21    private ASN1Integer              version;
22    private SignerIdentifier        sid;
23    private AlgorithmIdentifier     digAlgorithm;
24    private ASN1Set                 authenticatedAttributes;
25    private AlgorithmIdentifier     digEncryptionAlgorithm;
26    private ASN1OctetString         encryptedDigest;
27    private ASN1Set                 unauthenticatedAttributes;
28
29    public static SignerInfo getInstance(
30        Object  o)
31        throws IllegalArgumentException
32    {
33        if (o == null || o instanceof SignerInfo)
34        {
35            return (SignerInfo)o;
36        }
37        else if (o instanceof ASN1Sequence)
38        {
39            return new SignerInfo((ASN1Sequence)o);
40        }
41
42        throw new IllegalArgumentException("unknown object in factory: " + o.getClass().getName());
43    }
44
45    public SignerInfo(
46        SignerIdentifier        sid,
47        AlgorithmIdentifier     digAlgorithm,
48        ASN1Set                 authenticatedAttributes,
49        AlgorithmIdentifier     digEncryptionAlgorithm,
50        ASN1OctetString         encryptedDigest,
51        ASN1Set                 unauthenticatedAttributes)
52    {
53        if (sid.isTagged())
54        {
55            this.version = new ASN1Integer(3);
56        }
57        else
58        {
59            this.version = new ASN1Integer(1);
60        }
61
62        this.sid = sid;
63        this.digAlgorithm = digAlgorithm;
64        this.authenticatedAttributes = authenticatedAttributes;
65        this.digEncryptionAlgorithm = digEncryptionAlgorithm;
66        this.encryptedDigest = encryptedDigest;
67        this.unauthenticatedAttributes = unauthenticatedAttributes;
68    }
69
70    public SignerInfo(
71        SignerIdentifier        sid,
72        AlgorithmIdentifier     digAlgorithm,
73        Attributes              authenticatedAttributes,
74        AlgorithmIdentifier     digEncryptionAlgorithm,
75        ASN1OctetString         encryptedDigest,
76        Attributes              unauthenticatedAttributes)
77    {
78        if (sid.isTagged())
79        {
80            this.version = new ASN1Integer(3);
81        }
82        else
83        {
84            this.version = new ASN1Integer(1);
85        }
86
87        this.sid = sid;
88        this.digAlgorithm = digAlgorithm;
89        this.authenticatedAttributes = ASN1Set.getInstance(authenticatedAttributes);
90        this.digEncryptionAlgorithm = digEncryptionAlgorithm;
91        this.encryptedDigest = encryptedDigest;
92        this.unauthenticatedAttributes = ASN1Set.getInstance(unauthenticatedAttributes);
93    }
94
95    /**
96     * @deprecated use getInstance() method.
97     */
98    public SignerInfo(
99        ASN1Sequence seq)
100    {
101        Enumeration     e = seq.getObjects();
102
103        version = (ASN1Integer)e.nextElement();
104        sid = SignerIdentifier.getInstance(e.nextElement());
105        digAlgorithm = AlgorithmIdentifier.getInstance(e.nextElement());
106
107        Object obj = e.nextElement();
108
109        if (obj instanceof ASN1TaggedObject)
110        {
111            authenticatedAttributes = ASN1Set.getInstance((ASN1TaggedObject)obj, false);
112
113            digEncryptionAlgorithm = AlgorithmIdentifier.getInstance(e.nextElement());
114        }
115        else
116        {
117            authenticatedAttributes = null;
118            digEncryptionAlgorithm = AlgorithmIdentifier.getInstance(obj);
119        }
120
121        encryptedDigest = DEROctetString.getInstance(e.nextElement());
122
123        if (e.hasMoreElements())
124        {
125            unauthenticatedAttributes = ASN1Set.getInstance((ASN1TaggedObject)e.nextElement(), false);
126        }
127        else
128        {
129            unauthenticatedAttributes = null;
130        }
131    }
132
133    public ASN1Integer getVersion()
134    {
135        return version;
136    }
137
138    public SignerIdentifier getSID()
139    {
140        return sid;
141    }
142
143    public ASN1Set getAuthenticatedAttributes()
144    {
145        return authenticatedAttributes;
146    }
147
148    public AlgorithmIdentifier getDigestAlgorithm()
149    {
150        return digAlgorithm;
151    }
152
153    public ASN1OctetString getEncryptedDigest()
154    {
155        return encryptedDigest;
156    }
157
158    public AlgorithmIdentifier getDigestEncryptionAlgorithm()
159    {
160        return digEncryptionAlgorithm;
161    }
162
163    public ASN1Set getUnauthenticatedAttributes()
164    {
165        return unauthenticatedAttributes;
166    }
167
168    /**
169     * Produce an object suitable for an ASN1OutputStream.
170     * <pre>
171     *  SignerInfo ::= SEQUENCE {
172     *      version Version,
173     *      SignerIdentifier sid,
174     *      digestAlgorithm DigestAlgorithmIdentifier,
175     *      authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL,
176     *      digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier,
177     *      encryptedDigest EncryptedDigest,
178     *      unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
179     *  }
180     *
181     *  EncryptedDigest ::= OCTET STRING
182     *
183     *  DigestAlgorithmIdentifier ::= AlgorithmIdentifier
184     *
185     *  DigestEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
186     * </pre>
187     */
188    public ASN1Primitive toASN1Primitive()
189    {
190        ASN1EncodableVector v = new ASN1EncodableVector();
191
192        v.add(version);
193        v.add(sid);
194        v.add(digAlgorithm);
195
196        if (authenticatedAttributes != null)
197        {
198            v.add(new DERTaggedObject(false, 0, authenticatedAttributes));
199        }
200
201        v.add(digEncryptionAlgorithm);
202        v.add(encryptedDigest);
203
204        if (unauthenticatedAttributes != null)
205        {
206            v.add(new DERTaggedObject(false, 1, unauthenticatedAttributes));
207        }
208
209        return new DERSequence(v);
210    }
211}
212