001: package org.bouncycastle.asn1.cms;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1OctetString;
006: import org.bouncycastle.asn1.ASN1Sequence;
007: import org.bouncycastle.asn1.ASN1TaggedObject;
008: import org.bouncycastle.asn1.DERInteger;
009: import org.bouncycastle.asn1.DERObject;
010: import org.bouncycastle.asn1.DERSequence;
011: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
012:
013: public class KEKRecipientInfo extends ASN1Encodable {
014: private DERInteger version;
015: private KEKIdentifier kekid;
016: private AlgorithmIdentifier keyEncryptionAlgorithm;
017: private ASN1OctetString encryptedKey;
018:
019: public KEKRecipientInfo(KEKIdentifier kekid,
020: AlgorithmIdentifier keyEncryptionAlgorithm,
021: ASN1OctetString encryptedKey) {
022: this .version = new DERInteger(4);
023: this .kekid = kekid;
024: this .keyEncryptionAlgorithm = keyEncryptionAlgorithm;
025: this .encryptedKey = encryptedKey;
026: }
027:
028: public KEKRecipientInfo(ASN1Sequence seq) {
029: version = (DERInteger) seq.getObjectAt(0);
030: kekid = KEKIdentifier.getInstance(seq.getObjectAt(1));
031: keyEncryptionAlgorithm = AlgorithmIdentifier.getInstance(seq
032: .getObjectAt(2));
033: encryptedKey = (ASN1OctetString) seq.getObjectAt(3);
034: }
035:
036: /**
037: * return a KEKRecipientInfo object from a tagged object.
038: *
039: * @param obj the tagged object holding the object we want.
040: * @param explicit true if the object is meant to be explicitly
041: * tagged false otherwise.
042: * @exception IllegalArgumentException if the object held by the
043: * tagged object cannot be converted.
044: */
045: public static KEKRecipientInfo getInstance(ASN1TaggedObject obj,
046: boolean explicit) {
047: return getInstance(ASN1Sequence.getInstance(obj, explicit));
048: }
049:
050: /**
051: * return a KEKRecipientInfo object from the given object.
052: *
053: * @param obj the object we want converted.
054: * @exception IllegalArgumentException if the object cannot be converted.
055: */
056: public static KEKRecipientInfo getInstance(Object obj) {
057: if (obj == null || obj instanceof KEKRecipientInfo) {
058: return (KEKRecipientInfo) obj;
059: }
060:
061: if (obj instanceof ASN1Sequence) {
062: return new KEKRecipientInfo((ASN1Sequence) obj);
063: }
064:
065: throw new IllegalArgumentException("Invalid KEKRecipientInfo: "
066: + obj.getClass().getName());
067: }
068:
069: public DERInteger getVersion() {
070: return version;
071: }
072:
073: public KEKIdentifier getKekid() {
074: return kekid;
075: }
076:
077: public AlgorithmIdentifier getKeyEncryptionAlgorithm() {
078: return keyEncryptionAlgorithm;
079: }
080:
081: public ASN1OctetString getEncryptedKey() {
082: return encryptedKey;
083: }
084:
085: /**
086: * Produce an object suitable for an ASN1OutputStream.
087: * <pre>
088: * KEKRecipientInfo ::= SEQUENCE {
089: * version CMSVersion, -- always set to 4
090: * kekid KEKIdentifier,
091: * keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,
092: * encryptedKey EncryptedKey
093: * }
094: * </pre>
095: */
096: public DERObject toASN1Object() {
097: ASN1EncodableVector v = new ASN1EncodableVector();
098:
099: v.add(version);
100: v.add(kekid);
101: v.add(keyEncryptionAlgorithm);
102: v.add(encryptedKey);
103:
104: return new DERSequence(v);
105: }
106: }
|