~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/asn1/cms/RecipientIdentifier.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 org.bouncycastle.asn1.ASN1Choice;
 
4
import org.bouncycastle.asn1.ASN1Encodable;
 
5
import org.bouncycastle.asn1.ASN1OctetString;
 
6
import org.bouncycastle.asn1.ASN1TaggedObject;
 
7
import org.bouncycastle.asn1.DEREncodable;
 
8
import org.bouncycastle.asn1.DERObject;
 
9
import org.bouncycastle.asn1.DERTaggedObject;
 
10
 
 
11
public class RecipientIdentifier
 
12
    extends ASN1Encodable
 
13
    implements ASN1Choice
 
14
{
 
15
    private DEREncodable id;
 
16
    
 
17
    public RecipientIdentifier(
 
18
        IssuerAndSerialNumber id)
 
19
    {
 
20
        this.id = id;
 
21
    }
 
22
    
 
23
    public RecipientIdentifier(
 
24
        ASN1OctetString id)
 
25
    {
 
26
        this.id = new DERTaggedObject(false, 0, id);
 
27
    }
 
28
    
 
29
    public RecipientIdentifier(
 
30
        DERObject id)
 
31
    {
 
32
        this.id = id;
 
33
    }
 
34
    
 
35
    /**
 
36
     * return a RecipientIdentifier object from the given object.
 
37
     *
 
38
     * @param o the object we want converted.
 
39
     * @exception IllegalArgumentException if the object cannot be converted.
 
40
     */
 
41
    public static RecipientIdentifier getInstance(
 
42
        Object o)
 
43
    {
 
44
        if (o == null || o instanceof RecipientIdentifier)
 
45
        {
 
46
            return (RecipientIdentifier)o;
 
47
        }
 
48
        
 
49
        if (o instanceof IssuerAndSerialNumber)
 
50
        {
 
51
            return new RecipientIdentifier((IssuerAndSerialNumber)o);
 
52
        }
 
53
        
 
54
        if (o instanceof ASN1OctetString)
 
55
        {
 
56
            return new RecipientIdentifier((ASN1OctetString)o);
 
57
        }
 
58
        
 
59
        if (o instanceof DERObject)
 
60
        {
 
61
            return new RecipientIdentifier((DERObject)o);
 
62
        }
 
63
        
 
64
        throw new IllegalArgumentException(
 
65
          "Illegal object in RecipientIdentifier: " + o.getClass().getName());
 
66
    } 
 
67
 
 
68
    public boolean isTagged()
 
69
    {
 
70
        return (id instanceof ASN1TaggedObject);
 
71
    }
 
72
 
 
73
    public DEREncodable getId()
 
74
    {
 
75
        if (id instanceof ASN1TaggedObject)
 
76
        {
 
77
            return ASN1OctetString.getInstance((ASN1TaggedObject)id, false);
 
78
        }
 
79
 
 
80
        return IssuerAndSerialNumber.getInstance(id);
 
81
    }
 
82
 
 
83
    /** 
 
84
     * Produce an object suitable for an ASN1OutputStream.
 
85
     * <pre>
 
86
     * RecipientIdentifier ::= CHOICE {
 
87
     *     issuerAndSerialNumber IssuerAndSerialNumber,
 
88
     *     subjectKeyIdentifier [0] SubjectKeyIdentifier 
 
89
     * }
 
90
     *
 
91
     * SubjectKeyIdentifier ::= OCTET STRING
 
92
     * </pre>
 
93
     */
 
94
    public DERObject toASN1Object()
 
95
    {
 
96
        return id.getDERObject();
 
97
    }
 
98
}