01: package org.bouncycastle.asn1.cms;
02:
03: import org.bouncycastle.asn1.ASN1Encodable;
04: import org.bouncycastle.asn1.ASN1EncodableVector;
05: import org.bouncycastle.asn1.ASN1OctetString;
06: import org.bouncycastle.asn1.ASN1Sequence;
07: import org.bouncycastle.asn1.ASN1TaggedObject;
08: import org.bouncycastle.asn1.DERInteger;
09: import org.bouncycastle.asn1.DERObject;
10: import org.bouncycastle.asn1.DERSequence;
11: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
12:
13: public class KeyTransRecipientInfo extends ASN1Encodable {
14: private DERInteger version;
15: private RecipientIdentifier rid;
16: private AlgorithmIdentifier keyEncryptionAlgorithm;
17: private ASN1OctetString encryptedKey;
18:
19: public KeyTransRecipientInfo(RecipientIdentifier rid,
20: AlgorithmIdentifier keyEncryptionAlgorithm,
21: ASN1OctetString encryptedKey) {
22: if (rid.getDERObject() instanceof ASN1TaggedObject) {
23: this .version = new DERInteger(2);
24: } else {
25: this .version = new DERInteger(0);
26: }
27:
28: this .rid = rid;
29: this .keyEncryptionAlgorithm = keyEncryptionAlgorithm;
30: this .encryptedKey = encryptedKey;
31: }
32:
33: public KeyTransRecipientInfo(ASN1Sequence seq) {
34: this .version = (DERInteger) seq.getObjectAt(0);
35: this .rid = RecipientIdentifier.getInstance(seq.getObjectAt(1));
36: this .keyEncryptionAlgorithm = AlgorithmIdentifier
37: .getInstance(seq.getObjectAt(2));
38: this .encryptedKey = (ASN1OctetString) seq.getObjectAt(3);
39: }
40:
41: /**
42: * return a KeyTransRecipientInfo object from the given object.
43: *
44: * @param obj the object we want converted.
45: * @exception IllegalArgumentException if the object cannot be converted.
46: */
47: public static KeyTransRecipientInfo getInstance(Object obj) {
48: if (obj == null || obj instanceof KeyTransRecipientInfo) {
49: return (KeyTransRecipientInfo) obj;
50: }
51:
52: if (obj instanceof ASN1Sequence) {
53: return new KeyTransRecipientInfo((ASN1Sequence) obj);
54: }
55:
56: throw new IllegalArgumentException(
57: "Illegal object in KeyTransRecipientInfo: "
58: + obj.getClass().getName());
59: }
60:
61: public DERInteger getVersion() {
62: return version;
63: }
64:
65: public RecipientIdentifier getRecipientIdentifier() {
66: return rid;
67: }
68:
69: public AlgorithmIdentifier getKeyEncryptionAlgorithm() {
70: return keyEncryptionAlgorithm;
71: }
72:
73: public ASN1OctetString getEncryptedKey() {
74: return encryptedKey;
75: }
76:
77: /**
78: * Produce an object suitable for an ASN1OutputStream.
79: * <pre>
80: * KeyTransRecipientInfo ::= SEQUENCE {
81: * version CMSVersion, -- always set to 0 or 2
82: * rid RecipientIdentifier,
83: * keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,
84: * encryptedKey EncryptedKey
85: * }
86: * </pre>
87: */
88: public DERObject toASN1Object() {
89: ASN1EncodableVector v = new ASN1EncodableVector();
90:
91: v.add(version);
92: v.add(rid);
93: v.add(keyEncryptionAlgorithm);
94: v.add(encryptedKey);
95:
96: return new DERSequence(v);
97: }
98: }
|