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.DERGeneralizedTime;
009: import org.bouncycastle.asn1.DERObject;
010: import org.bouncycastle.asn1.DERSequence;
011:
012: public class RecipientKeyIdentifier extends ASN1Encodable {
013: private ASN1OctetString subjectKeyIdentifier;
014: private DERGeneralizedTime date;
015: private OtherKeyAttribute other;
016:
017: public RecipientKeyIdentifier(ASN1OctetString subjectKeyIdentifier,
018: DERGeneralizedTime date, OtherKeyAttribute other) {
019: this .subjectKeyIdentifier = subjectKeyIdentifier;
020: this .date = date;
021: this .other = other;
022: }
023:
024: public RecipientKeyIdentifier(ASN1Sequence seq) {
025: subjectKeyIdentifier = ASN1OctetString.getInstance(seq
026: .getObjectAt(0));
027:
028: switch (seq.size()) {
029: case 1:
030: break;
031: case 2:
032: if (seq.getObjectAt(1) instanceof DERGeneralizedTime) {
033: date = (DERGeneralizedTime) seq.getObjectAt(1);
034: } else {
035: other = OtherKeyAttribute.getInstance(seq
036: .getObjectAt(2));
037: }
038: break;
039: case 3:
040: date = (DERGeneralizedTime) seq.getObjectAt(1);
041: other = OtherKeyAttribute.getInstance(seq.getObjectAt(2));
042: break;
043: default:
044: throw new IllegalArgumentException("Invalid KEKIdentifier");
045: }
046: }
047:
048: /**
049: * return a RecipientKeyIdentifier object from a tagged object.
050: *
051: * @param _ato the tagged object holding the object we want.
052: * @param _explicit true if the object is meant to be explicitly
053: * tagged false otherwise.
054: * @exception IllegalArgumentException if the object held by the
055: * tagged object cannot be converted.
056: */
057: public static RecipientKeyIdentifier getInstance(
058: ASN1TaggedObject _ato, boolean _explicit) {
059: return getInstance(ASN1Sequence.getInstance(_ato, _explicit));
060: }
061:
062: /**
063: * return a RecipientKeyIdentifier object from the given object.
064: *
065: * @param _obj the object we want converted.
066: * @exception IllegalArgumentException if the object cannot be converted.
067: */
068: public static RecipientKeyIdentifier getInstance(Object _obj) {
069: if (_obj == null || _obj instanceof RecipientKeyIdentifier) {
070: return (RecipientKeyIdentifier) _obj;
071: }
072:
073: if (_obj instanceof ASN1Sequence) {
074: return new RecipientKeyIdentifier((ASN1Sequence) _obj);
075: }
076:
077: throw new IllegalArgumentException(
078: "Invalid RecipientKeyIdentifier: "
079: + _obj.getClass().getName());
080: }
081:
082: public ASN1OctetString getSubjectKeyIdentifier() {
083: return subjectKeyIdentifier;
084: }
085:
086: public DERGeneralizedTime getDate() {
087: return date;
088: }
089:
090: public OtherKeyAttribute getOtherKeyAttribute() {
091: return other;
092: }
093:
094: /**
095: * Produce an object suitable for an ASN1OutputStream.
096: * <pre>
097: * RecipientKeyIdentifier ::= SEQUENCE {
098: * subjectKeyIdentifier SubjectKeyIdentifier,
099: * date GeneralizedTime OPTIONAL,
100: * other OtherKeyAttribute OPTIONAL
101: * }
102: *
103: * SubjectKeyIdentifier ::= OCTET STRING
104: * </pre>
105: */
106: public DERObject toASN1Object() {
107: ASN1EncodableVector v = new ASN1EncodableVector();
108:
109: v.add(subjectKeyIdentifier);
110:
111: if (date != null) {
112: v.add(date);
113: }
114:
115: if (other != null) {
116: v.add(other);
117: }
118:
119: return new DERSequence(v);
120: }
121: }
|