001: package org.bouncycastle.asn1.cms;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1Sequence;
005: import org.bouncycastle.asn1.ASN1TaggedObject;
006: import org.bouncycastle.asn1.DEREncodable;
007: import org.bouncycastle.asn1.DERInteger;
008: import org.bouncycastle.asn1.DERObject;
009: import org.bouncycastle.asn1.DERTaggedObject;
010:
011: public class RecipientInfo extends ASN1Encodable {
012: DEREncodable info;
013:
014: public RecipientInfo(KeyTransRecipientInfo info) {
015: this .info = info;
016: }
017:
018: public RecipientInfo(KeyAgreeRecipientInfo info) {
019: this .info = new DERTaggedObject(false, 1, info);
020: }
021:
022: public RecipientInfo(KEKRecipientInfo info) {
023: this .info = new DERTaggedObject(false, 2, info);
024: }
025:
026: public RecipientInfo(PasswordRecipientInfo info) {
027: this .info = new DERTaggedObject(false, 3, info);
028: }
029:
030: public RecipientInfo(OtherRecipientInfo info) {
031: this .info = new DERTaggedObject(false, 4, info);
032: }
033:
034: public RecipientInfo(DERObject info) {
035: this .info = info;
036: }
037:
038: public static RecipientInfo getInstance(Object o) {
039: if (o == null || o instanceof RecipientInfo) {
040: return (RecipientInfo) o;
041: } else if (o instanceof ASN1Sequence) {
042: return new RecipientInfo((ASN1Sequence) o);
043: } else if (o instanceof ASN1TaggedObject) {
044: return new RecipientInfo((ASN1TaggedObject) o);
045: }
046:
047: throw new IllegalArgumentException(
048: "unknown object in factory: " + o.getClass().getName());
049: }
050:
051: public DERInteger getVersion() {
052: if (info instanceof ASN1TaggedObject) {
053: ASN1TaggedObject o = (ASN1TaggedObject) info;
054:
055: switch (o.getTagNo()) {
056: case 1:
057: return KeyAgreeRecipientInfo.getInstance(o, false)
058: .getVersion();
059: case 2:
060: return getKEKInfo(o).getVersion();
061: case 3:
062: return PasswordRecipientInfo.getInstance(o, false)
063: .getVersion();
064: case 4:
065: return new DERInteger(0); // no syntax version for OtherRecipientInfo
066: default:
067: throw new IllegalStateException("unknown tag");
068: }
069: }
070:
071: return KeyTransRecipientInfo.getInstance(info).getVersion();
072: }
073:
074: public boolean isTagged() {
075: return (info instanceof ASN1TaggedObject);
076: }
077:
078: public DEREncodable getInfo() {
079: if (info instanceof ASN1TaggedObject) {
080: ASN1TaggedObject o = (ASN1TaggedObject) info;
081:
082: switch (o.getTagNo()) {
083: case 1:
084: return KeyAgreeRecipientInfo.getInstance(o, false);
085: case 2:
086: return getKEKInfo(o);
087: case 3:
088: return PasswordRecipientInfo.getInstance(o, false);
089: case 4:
090: return OtherRecipientInfo.getInstance(o, false);
091: default:
092: throw new IllegalStateException("unknown tag");
093: }
094: }
095:
096: return KeyTransRecipientInfo.getInstance(info);
097: }
098:
099: private KEKRecipientInfo getKEKInfo(ASN1TaggedObject o) {
100: if (o.isExplicit()) { // compatibilty with erroneous version
101: return KEKRecipientInfo.getInstance(o, true);
102: } else {
103: return KEKRecipientInfo.getInstance(o, false);
104: }
105: }
106:
107: /**
108: * Produce an object suitable for an ASN1OutputStream.
109: * <pre>
110: * RecipientInfo ::= CHOICE {
111: * ktri KeyTransRecipientInfo,
112: * kari [1] KeyAgreeRecipientInfo,
113: * kekri [2] KEKRecipientInfo,
114: * pwri [3] PasswordRecipientInfo,
115: * ori [4] OtherRecipientInfo }
116: * </pre>
117: */
118: public DERObject toASN1Object() {
119: return info.getDERObject();
120: }
121: }
|