~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

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