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.DEROctetString;
011: import org.bouncycastle.asn1.DERSequence;
012:
013: public class KEKIdentifier extends ASN1Encodable {
014: private ASN1OctetString keyIdentifier;
015: private DERGeneralizedTime date;
016: private OtherKeyAttribute other;
017:
018: public KEKIdentifier(byte[] keyIdentifier, DERGeneralizedTime date,
019: OtherKeyAttribute other) {
020: this .keyIdentifier = new DEROctetString(keyIdentifier);
021: this .date = date;
022: this .other = other;
023: }
024:
025: public KEKIdentifier(ASN1Sequence seq) {
026: keyIdentifier = (ASN1OctetString) seq.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(1));
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 KEKIdentifier object from a tagged object.
050: *
051: * @param obj 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 KEKIdentifier getInstance(ASN1TaggedObject obj,
058: boolean explicit) {
059: return getInstance(ASN1Sequence.getInstance(obj, explicit));
060: }
061:
062: /**
063: * return a KEKIdentifier 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 KEKIdentifier getInstance(Object obj) {
069: if (obj == null || obj instanceof KEKIdentifier) {
070: return (KEKIdentifier) obj;
071: }
072:
073: if (obj instanceof ASN1Sequence) {
074: return new KEKIdentifier((ASN1Sequence) obj);
075: }
076:
077: throw new IllegalArgumentException("Invalid KEKIdentifier: "
078: + obj.getClass().getName());
079: }
080:
081: public ASN1OctetString getKeyIdentifier() {
082: return keyIdentifier;
083: }
084:
085: public DERGeneralizedTime getDate() {
086: return date;
087: }
088:
089: public OtherKeyAttribute getOther() {
090: return other;
091: }
092:
093: /**
094: * Produce an object suitable for an ASN1OutputStream.
095: * <pre>
096: * KEKIdentifier ::= SEQUENCE {
097: * keyIdentifier OCTET STRING,
098: * date GeneralizedTime OPTIONAL,
099: * other OtherKeyAttribute OPTIONAL
100: * }
101: * </pre>
102: */
103: public DERObject toASN1Object() {
104: ASN1EncodableVector v = new ASN1EncodableVector();
105:
106: v.add(keyIdentifier);
107:
108: if (date != null) {
109: v.add(date);
110: }
111:
112: if (other != null) {
113: v.add(other);
114: }
115:
116: return new DERSequence(v);
117: }
118: }
|