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