~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/asn1/cms/KEKRecipientInfo.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.ASN1Encodable;
 
4
import org.bouncycastle.asn1.ASN1EncodableVector;
 
5
import org.bouncycastle.asn1.ASN1OctetString;
 
6
import org.bouncycastle.asn1.ASN1Sequence;
 
7
import org.bouncycastle.asn1.ASN1TaggedObject;
 
8
import org.bouncycastle.asn1.DERInteger;
 
9
import org.bouncycastle.asn1.DERObject;
 
10
import org.bouncycastle.asn1.DERSequence;
 
11
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
 
12
 
 
13
public class KEKRecipientInfo
 
14
    extends ASN1Encodable
 
15
{
 
16
    private DERInteger          version;
 
17
    private KEKIdentifier       kekid;
 
18
    private AlgorithmIdentifier keyEncryptionAlgorithm;
 
19
    private ASN1OctetString     encryptedKey;
 
20
 
 
21
    public KEKRecipientInfo(
 
22
        KEKIdentifier       kekid,
 
23
        AlgorithmIdentifier keyEncryptionAlgorithm,
 
24
        ASN1OctetString     encryptedKey)
 
25
    {
 
26
        this.version = new DERInteger(4);
 
27
        this.kekid = kekid;
 
28
        this.keyEncryptionAlgorithm = keyEncryptionAlgorithm;
 
29
        this.encryptedKey = encryptedKey;
 
30
    }
 
31
    
 
32
    public KEKRecipientInfo(
 
33
        ASN1Sequence seq)
 
34
    {
 
35
        version = (DERInteger)seq.getObjectAt(0);
 
36
        kekid = KEKIdentifier.getInstance(seq.getObjectAt(1));
 
37
        keyEncryptionAlgorithm = AlgorithmIdentifier.getInstance(seq.getObjectAt(2));
 
38
        encryptedKey = (ASN1OctetString)seq.getObjectAt(3);
 
39
    }
 
40
 
 
41
    /**
 
42
     * return a KEKRecipientInfo object from a tagged object.
 
43
     *
 
44
     * @param obj the tagged object holding the object we want.
 
45
     * @param explicit true if the object is meant to be explicitly
 
46
     *              tagged false otherwise.
 
47
     * @exception IllegalArgumentException if the object held by the
 
48
     *          tagged object cannot be converted.
 
49
     */
 
50
    public static KEKRecipientInfo getInstance(
 
51
        ASN1TaggedObject    obj,
 
52
        boolean             explicit)
 
53
    {
 
54
        return getInstance(ASN1Sequence.getInstance(obj, explicit));
 
55
    }
 
56
    
 
57
    /**
 
58
     * return a KEKRecipientInfo object from the given object.
 
59
     *
 
60
     * @param obj the object we want converted.
 
61
     * @exception IllegalArgumentException if the object cannot be converted.
 
62
     */
 
63
    public static KEKRecipientInfo getInstance(
 
64
        Object obj)
 
65
    {
 
66
        if (obj == null || obj instanceof KEKRecipientInfo)
 
67
        {
 
68
            return (KEKRecipientInfo)obj;
 
69
        }
 
70
        
 
71
        if(obj instanceof ASN1Sequence)
 
72
        {
 
73
            return new KEKRecipientInfo((ASN1Sequence)obj);
 
74
        }
 
75
        
 
76
        throw new IllegalArgumentException("Invalid KEKRecipientInfo: " + obj.getClass().getName());
 
77
    }
 
78
 
 
79
    public DERInteger getVersion()
 
80
    {
 
81
        return version;
 
82
    }
 
83
    
 
84
    public KEKIdentifier getKekid()
 
85
    {
 
86
        return kekid;
 
87
    }
 
88
 
 
89
    public AlgorithmIdentifier getKeyEncryptionAlgorithm()
 
90
    {
 
91
        return keyEncryptionAlgorithm;
 
92
    }
 
93
 
 
94
    public ASN1OctetString getEncryptedKey()
 
95
    {
 
96
        return encryptedKey;
 
97
    }
 
98
 
 
99
    /** 
 
100
     * Produce an object suitable for an ASN1OutputStream.
 
101
     * <pre>
 
102
     * KEKRecipientInfo ::= SEQUENCE {
 
103
     *     version CMSVersion,  -- always set to 4
 
104
     *     kekid KEKIdentifier,
 
105
     *     keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,
 
106
     *     encryptedKey EncryptedKey 
 
107
     * }
 
108
     * </pre>
 
109
     */
 
110
    public DERObject toASN1Object()
 
111
    {
 
112
        ASN1EncodableVector  v = new ASN1EncodableVector();
 
113
 
 
114
        v.add(version);
 
115
        v.add(kekid);
 
116
        v.add(keyEncryptionAlgorithm);
 
117
        v.add(encryptedKey);
 
118
 
 
119
        return new DERSequence(v);
 
120
    }
 
121
}