01: package org.bouncycastle.asn1.pkcs;
02:
03: import org.bouncycastle.asn1.*;
04: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
05:
06: /**
07: * The EncryptedData object.
08: * <pre>
09: * EncryptedData ::= SEQUENCE {
10: * version Version,
11: * encryptedContentInfo EncryptedContentInfo
12: * }
13: *
14: *
15: * EncryptedContentInfo ::= SEQUENCE {
16: * contentType ContentType,
17: * contentEncryptionAlgorithm ContentEncryptionAlgorithmIdentifier,
18: * encryptedContent [0] IMPLICIT EncryptedContent OPTIONAL
19: * }
20: *
21: * EncryptedContent ::= OCTET STRING
22: * </pre>
23: */
24: public class EncryptedData extends ASN1Encodable {
25: ASN1Sequence data;
26: DERObjectIdentifier bagId;
27: DERObject bagValue;
28:
29: public static EncryptedData getInstance(Object obj) {
30: if (obj instanceof EncryptedData) {
31: return (EncryptedData) obj;
32: } else if (obj instanceof ASN1Sequence) {
33: return new EncryptedData((ASN1Sequence) obj);
34: }
35:
36: throw new IllegalArgumentException("unknown object in factory");
37: }
38:
39: public EncryptedData(ASN1Sequence seq) {
40: int version = ((DERInteger) seq.getObjectAt(0)).getValue()
41: .intValue();
42:
43: if (version != 0) {
44: throw new IllegalArgumentException("sequence not version 0");
45: }
46:
47: this .data = (ASN1Sequence) seq.getObjectAt(1);
48: }
49:
50: public EncryptedData(DERObjectIdentifier contentType,
51: AlgorithmIdentifier encryptionAlgorithm,
52: DEREncodable content) {
53: ASN1EncodableVector v = new ASN1EncodableVector();
54:
55: v.add(contentType);
56: v.add(encryptionAlgorithm.getDERObject());
57: v.add(new BERTaggedObject(false, 0, content));
58:
59: data = new BERSequence(v);
60: }
61:
62: public DERObjectIdentifier getContentType() {
63: return (DERObjectIdentifier) data.getObjectAt(0);
64: }
65:
66: public AlgorithmIdentifier getEncryptionAlgorithm() {
67: return AlgorithmIdentifier.getInstance(data.getObjectAt(1));
68: }
69:
70: public ASN1OctetString getContent() {
71: if (data.size() == 3) {
72: DERTaggedObject o = (DERTaggedObject) data.getObjectAt(2);
73:
74: return ASN1OctetString.getInstance(o.getObject());
75: }
76:
77: return null;
78: }
79:
80: public DERObject toASN1Object() {
81: ASN1EncodableVector v = new ASN1EncodableVector();
82:
83: v.add(new DERInteger(0));
84: v.add(data);
85:
86: return new BERSequence(v);
87: }
88: }
|