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.DERObject;
09: import org.bouncycastle.asn1.DERSequence;
10:
11: public class RecipientEncryptedKey extends ASN1Encodable {
12: private KeyAgreeRecipientIdentifier identifier;
13: private ASN1OctetString encryptedKey;
14:
15: private RecipientEncryptedKey(ASN1Sequence seq) {
16: identifier = KeyAgreeRecipientIdentifier.getInstance(seq
17: .getObjectAt(0));
18: encryptedKey = (ASN1OctetString) seq.getObjectAt(1);
19: }
20:
21: /**
22: * return an RecipientEncryptedKey object from a tagged object.
23: *
24: * @param obj the tagged object holding the object we want.
25: * @param explicit true if the object is meant to be explicitly
26: * tagged false otherwise.
27: * @exception IllegalArgumentException if the object held by the
28: * tagged object cannot be converted.
29: */
30: public static RecipientEncryptedKey getInstance(
31: ASN1TaggedObject obj, boolean explicit) {
32: return getInstance(ASN1Sequence.getInstance(obj, explicit));
33: }
34:
35: /**
36: * return a RecipientEncryptedKey object from the given object.
37: *
38: * @param obj the object we want converted.
39: * @exception IllegalArgumentException if the object cannot be converted.
40: */
41: public static RecipientEncryptedKey getInstance(Object obj) {
42: if (obj == null || obj instanceof RecipientEncryptedKey) {
43: return (RecipientEncryptedKey) obj;
44: }
45:
46: if (obj instanceof ASN1Sequence) {
47: return new RecipientEncryptedKey((ASN1Sequence) obj);
48: }
49:
50: throw new IllegalArgumentException(
51: "Invalid RecipientEncryptedKey: "
52: + obj.getClass().getName());
53: }
54:
55: public RecipientEncryptedKey(KeyAgreeRecipientIdentifier id,
56: ASN1OctetString encryptedKey) {
57: this .identifier = id;
58: this .encryptedKey = encryptedKey;
59: }
60:
61: public KeyAgreeRecipientIdentifier getIdentifier() {
62: return identifier;
63: }
64:
65: public ASN1OctetString getEncryptedKey() {
66: return encryptedKey;
67: }
68:
69: /**
70: * Produce an object suitable for an ASN1OutputStream.
71: * <pre>
72: * RecipientEncryptedKey ::= SEQUENCE {
73: * rid KeyAgreeRecipientIdentifier,
74: * encryptedKey EncryptedKey
75: * }
76: * </pre>
77: */
78: public DERObject toASN1Object() {
79: ASN1EncodableVector v = new ASN1EncodableVector();
80:
81: v.add(identifier);
82: v.add(encryptedKey);
83:
84: return new DERSequence(v);
85: }
86: }
|