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