001: package org.bouncycastle.asn1.x509;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1Sequence;
006: import org.bouncycastle.asn1.ASN1TaggedObject;
007: import org.bouncycastle.asn1.DERObject;
008: import org.bouncycastle.asn1.DERSequence;
009: import org.bouncycastle.asn1.DERTaggedObject;
010:
011: /**
012: * The Holder object.
013: * <p>
014: * For an v2 attribute certificate this is:
015: *
016: * <pre>
017: * Holder ::= SEQUENCE {
018: * baseCertificateID [0] IssuerSerial OPTIONAL,
019: * -- the issuer and serial number of
020: * -- the holder's Public Key Certificate
021: * entityName [1] GeneralNames OPTIONAL,
022: * -- the name of the claimant or role
023: * objectDigestInfo [2] ObjectDigestInfo OPTIONAL
024: * -- used to directly authenticate the holder,
025: * -- for example, an executable
026: * }
027: * </pre>
028: *
029: * <p>
030: * For an v1 attribute certificate this is:
031: *
032: * <pre>
033: * subject CHOICE {
034: * baseCertificateID [0] IssuerSerial,
035: * -- associated with a Public Key Certificate
036: * subjectName [1] GeneralNames },
037: * -- associated with a name
038: * </pre>
039: */
040: public class Holder extends ASN1Encodable {
041: IssuerSerial baseCertificateID;
042:
043: GeneralNames entityName;
044:
045: ObjectDigestInfo objectDigestInfo;
046:
047: private int version = 1;
048:
049: public static Holder getInstance(Object obj) {
050: if (obj instanceof Holder) {
051: return (Holder) obj;
052: } else if (obj instanceof ASN1Sequence) {
053: return new Holder((ASN1Sequence) obj);
054: } else if (obj instanceof ASN1TaggedObject) {
055: return new Holder((ASN1TaggedObject) obj);
056: }
057:
058: throw new IllegalArgumentException("unknown object in factory");
059: }
060:
061: /**
062: * Constructor for a holder for an v1 attribute certificate.
063: *
064: * @param tagObj The ASN.1 tagged holder object.
065: */
066: public Holder(ASN1TaggedObject tagObj) {
067: switch (tagObj.getTagNo()) {
068: case 0:
069: baseCertificateID = IssuerSerial.getInstance(tagObj, false);
070: break;
071: case 1:
072: entityName = GeneralNames.getInstance(tagObj, false);
073: break;
074: default:
075: throw new IllegalArgumentException("unknown tag in Holder");
076: }
077: version = 0;
078: }
079:
080: /**
081: * Constructor for a holder for an v2 attribute certificate. *
082: *
083: * @param seq The ASN.1 sequence.
084: */
085: public Holder(ASN1Sequence seq) {
086: if (seq.size() > 3) {
087: throw new IllegalArgumentException("Bad sequence size: "
088: + seq.size());
089: }
090:
091: for (int i = 0; i != seq.size(); i++) {
092: ASN1TaggedObject tObj = ASN1TaggedObject.getInstance(seq
093: .getObjectAt(i));
094:
095: switch (tObj.getTagNo()) {
096: case 0:
097: baseCertificateID = IssuerSerial.getInstance(tObj,
098: false);
099: break;
100: case 1:
101: entityName = GeneralNames.getInstance(tObj, false);
102: break;
103: case 2:
104: objectDigestInfo = ObjectDigestInfo.getInstance(tObj,
105: false);
106: break;
107: default:
108: throw new IllegalArgumentException(
109: "unknown tag in Holder");
110: }
111: }
112: version = 1;
113: }
114:
115: public Holder(IssuerSerial baseCertificateID) {
116: this .baseCertificateID = baseCertificateID;
117: }
118:
119: /**
120: * Constructs a holder from a IssuerSerial.
121: * @param baseCertificateID The IssuerSerial.
122: * @param version The version of the attribute certificate.
123: */
124: public Holder(IssuerSerial baseCertificateID, int version) {
125: this .baseCertificateID = baseCertificateID;
126: this .version = version;
127: }
128:
129: /**
130: * Returns 1 for v2 attribute certificates or 0 for v1 attribute
131: * certificates.
132: * @return The version of the attribute certificate.
133: */
134: public int getVersion() {
135: return version;
136: }
137:
138: /**
139: * Constructs a holder with an entityName for v2 attribute certificates or
140: * with a subjectName for v1 attribute certificates.
141: *
142: * @param entityName The entity or subject name.
143: */
144: public Holder(GeneralNames entityName) {
145: this .entityName = entityName;
146: }
147:
148: /**
149: * Constructs a holder with an entityName for v2 attribute certificates or
150: * with a subjectName for v1 attribute certificates.
151: *
152: * @param entityName The entity or subject name.
153: * @param version The version of the attribute certificate.
154: */
155: public Holder(GeneralNames entityName, int version) {
156: this .entityName = entityName;
157: this .version = version;
158: }
159:
160: /**
161: * Constructs a holder from an object digest info.
162: *
163: * @param objectDigestInfo The object digest info object.
164: */
165: public Holder(ObjectDigestInfo objectDigestInfo) {
166: this .objectDigestInfo = objectDigestInfo;
167: }
168:
169: public IssuerSerial getBaseCertificateID() {
170: return baseCertificateID;
171: }
172:
173: /**
174: * Returns the entityName for an v2 attribute certificate or the subjectName
175: * for an v1 attribute certificate.
176: *
177: * @return The entityname or subjectname.
178: */
179: public GeneralNames getEntityName() {
180: return entityName;
181: }
182:
183: public ObjectDigestInfo getObjectDigestInfo() {
184: return objectDigestInfo;
185: }
186:
187: public DERObject toASN1Object() {
188: if (version == 1) {
189: ASN1EncodableVector v = new ASN1EncodableVector();
190:
191: if (baseCertificateID != null) {
192: v.add(new DERTaggedObject(false, 0, baseCertificateID));
193: }
194:
195: if (entityName != null) {
196: v.add(new DERTaggedObject(false, 1, entityName));
197: }
198:
199: if (objectDigestInfo != null) {
200: v.add(new DERTaggedObject(false, 2, objectDigestInfo));
201: }
202:
203: return new DERSequence(v);
204: } else {
205: if (entityName != null) {
206: return new DERTaggedObject(false, 1, entityName);
207: } else {
208: return new DERTaggedObject(false, 0, baseCertificateID);
209: }
210: }
211: }
212: }
|