01: package org.bouncycastle.asn1.cms;
02:
03: import org.bouncycastle.asn1.ASN1Encodable;
04: import org.bouncycastle.asn1.ASN1OctetString;
05: import org.bouncycastle.asn1.ASN1TaggedObject;
06: import org.bouncycastle.asn1.DEREncodable;
07: import org.bouncycastle.asn1.DERObject;
08: import org.bouncycastle.asn1.DERTaggedObject;
09:
10: public class OriginatorIdentifierOrKey extends ASN1Encodable {
11: private DEREncodable id;
12:
13: public OriginatorIdentifierOrKey(IssuerAndSerialNumber id) {
14: this .id = id;
15: }
16:
17: public OriginatorIdentifierOrKey(ASN1OctetString id) {
18: this .id = new DERTaggedObject(false, 0, id);
19: }
20:
21: public OriginatorIdentifierOrKey(OriginatorPublicKey id) {
22: this .id = new DERTaggedObject(false, 1, id);
23: }
24:
25: public OriginatorIdentifierOrKey(DERObject id) {
26: this .id = id;
27: }
28:
29: /**
30: * return an OriginatorIdentifierOrKey object from a tagged object.
31: *
32: * @param o the tagged object holding the object we want.
33: * @param explicit true if the object is meant to be explicitly
34: * tagged false otherwise.
35: * @exception IllegalArgumentException if the object held by the
36: * tagged object cannot be converted.
37: */
38: public static OriginatorIdentifierOrKey getInstance(
39: ASN1TaggedObject o, boolean explicit) {
40: if (!explicit) {
41: throw new IllegalArgumentException(
42: "Can't implicitly tag OriginatorIdentifierOrKey");
43: }
44:
45: return getInstance(o.getObject());
46: }
47:
48: /**
49: * return an OriginatorIdentifierOrKey object from the given object.
50: *
51: * @param o the object we want converted.
52: * @exception IllegalArgumentException if the object cannot be converted.
53: */
54: public static OriginatorIdentifierOrKey getInstance(Object o) {
55: if (o == null || o instanceof OriginatorIdentifierOrKey) {
56: return (OriginatorIdentifierOrKey) o;
57: }
58:
59: if (o instanceof DERObject) {
60: return new OriginatorIdentifierOrKey((DERObject) o);
61: }
62:
63: throw new IllegalArgumentException(
64: "Invalid OriginatorIdentifierOrKey: "
65: + o.getClass().getName());
66: }
67:
68: public DEREncodable getId() {
69: return id;
70: }
71:
72: public OriginatorPublicKey getOriginatorKey() {
73: if (id instanceof ASN1TaggedObject
74: && ((ASN1TaggedObject) id).getTagNo() == 1) {
75: return OriginatorPublicKey.getInstance(
76: (ASN1TaggedObject) id, false);
77: }
78:
79: return null;
80: }
81:
82: /**
83: * Produce an object suitable for an ASN1OutputStream.
84: * <pre>
85: * OriginatorIdentifierOrKey ::= CHOICE {
86: * issuerAndSerialNumber IssuerAndSerialNumber,
87: * subjectKeyIdentifier [0] SubjectKeyIdentifier,
88: * originatorKey [1] OriginatorPublicKey
89: * }
90: *
91: * SubjectKeyIdentifier ::= OCTET STRING
92: * </pre>
93: */
94: public DERObject toASN1Object() {
95: return id.getDERObject();
96: }
97: }
|