001: package org.bouncycastle.asn1.x509;
002:
003: import org.bouncycastle.asn1.ASN1Choice;
004: import org.bouncycastle.asn1.ASN1Encodable;
005: import org.bouncycastle.asn1.ASN1TaggedObject;
006: import org.bouncycastle.asn1.DERObject;
007: import org.bouncycastle.asn1.DERTaggedObject;
008:
009: /**
010: * Target structure used in target information extension for attribute
011: * certificates from RFC 3281.
012: *
013: * <pre>
014: * Target ::= CHOICE {
015: * targetName [0] GeneralName,
016: * targetGroup [1] GeneralName,
017: * targetCert [2] TargetCert
018: * }
019: * </pre>
020: *
021: * <p>
022: * The targetCert field is currently not supported and must not be used
023: * according to RFC 3281.
024: */
025: public class Target extends ASN1Encodable implements ASN1Choice {
026: public static final int targetName = 0;
027: public static final int targetGroup = 1;
028:
029: private GeneralName targName;
030: private GeneralName targGroup;
031:
032: /**
033: * Creates an instance of a Target from the given object.
034: * <p>
035: * <code>obj</code> can be a Target or a {@link ASN1TaggedObject}
036: *
037: * @param obj The object.
038: * @return A Target instance.
039: * @throws IllegalArgumentException if the given object cannot be
040: * interpreted as Target.
041: */
042: public static Target getInstance(Object obj) {
043: if (obj instanceof Target) {
044: return (Target) obj;
045: } else if (obj instanceof ASN1TaggedObject) {
046: return new Target((ASN1TaggedObject) obj);
047: }
048:
049: throw new IllegalArgumentException(
050: "unknown object in factory: " + obj.getClass());
051: }
052:
053: /**
054: * Constructor from ASN1TaggedObject.
055: *
056: * @param tagObj The tagged object.
057: * @throws IllegalArgumentException if the encoding is wrong.
058: */
059: private Target(ASN1TaggedObject tagObj) {
060: switch (tagObj.getTagNo()) {
061: case targetName: // GeneralName is already a choice so explicit
062: targName = GeneralName.getInstance(tagObj, true);
063: break;
064: case targetGroup:
065: targGroup = GeneralName.getInstance(tagObj, true);
066: break;
067: default:
068: throw new IllegalArgumentException("unknown tag: "
069: + tagObj.getTagNo());
070: }
071: }
072:
073: /**
074: * Constructor from given details.
075: * <p>
076: * Exactly one of the parameters must be not <code>null</code>.
077: *
078: * @param type the choice type to apply to the name.
079: * @param name the general name.
080: * @throws IllegalArgumentException if type is invalid.
081: */
082: public Target(int type, GeneralName name) {
083: this (new DERTaggedObject(type, name));
084: }
085:
086: /**
087: * @return Returns the targetGroup.
088: */
089: public GeneralName getTargetGroup() {
090: return targGroup;
091: }
092:
093: /**
094: * @return Returns the targetName.
095: */
096: public GeneralName getTargetName() {
097: return targName;
098: }
099:
100: /**
101: * Produce an object suitable for an ASN1OutputStream.
102: *
103: * Returns:
104: *
105: * <pre>
106: * Target ::= CHOICE {
107: * targetName [0] GeneralName,
108: * targetGroup [1] GeneralName,
109: * targetCert [2] TargetCert
110: * }
111: * </pre>
112: *
113: * @return a DERObject
114: */
115: public DERObject toASN1Object() {
116: // GeneralName is a choice already so most be explicitly tagged
117: if (targName != null) {
118: return new DERTaggedObject(true, 0, targName);
119: } else {
120: return new DERTaggedObject(true, 1, targGroup);
121: }
122: }
123: }
|