001: package org.bouncycastle.asn1.x509;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1Sequence;
005: import org.bouncycastle.asn1.DERObject;
006: import org.bouncycastle.asn1.DERSequence;
007:
008: import java.util.Enumeration;
009:
010: /**
011: * Target information extension for attributes certificates according to RFC
012: * 3281.
013: *
014: * <pre>
015: * SEQUENCE OF Targets
016: * </pre>
017: *
018: */
019: public class TargetInformation extends ASN1Encodable {
020: private ASN1Sequence targets;
021:
022: /**
023: * Creates an instance of a TargetInformation from the given object.
024: * <p>
025: * <code>obj</code> can be a TargetInformation or a {@link ASN1Sequence}
026: *
027: * @param obj The object.
028: * @return A TargetInformation instance.
029: * @throws IllegalArgumentException if the given object cannot be
030: * interpreted as TargetInformation.
031: */
032: public static TargetInformation getInstance(Object obj) {
033: if (obj instanceof TargetInformation) {
034: return (TargetInformation) obj;
035: } else if (obj instanceof ASN1Sequence) {
036: return new TargetInformation((ASN1Sequence) obj);
037: }
038:
039: throw new IllegalArgumentException(
040: "unknown object in factory: " + obj.getClass());
041: }
042:
043: /**
044: * Constructor from a ASN1Sequence.
045: *
046: * @param seq The ASN1Sequence.
047: * @throws IllegalArgumentException if the sequence does not contain
048: * correctly encoded Targets elements.
049: */
050: private TargetInformation(ASN1Sequence seq) {
051: targets = seq;
052: }
053:
054: /**
055: * Returns the targets in this target information extension.
056: *
057: * @return Returns the targets.
058: */
059: public Targets[] getTargetsObjects() {
060: Targets[] copy = new Targets[targets.size()];
061: int count = 0;
062: for (Enumeration e = targets.getObjects(); e.hasMoreElements();) {
063: copy[count++] = Targets.getInstance(e.nextElement());
064: }
065: return copy;
066: }
067:
068: /**
069: * Constructs a target information from a single targets element.
070: * According to RFC 3281 only one targets element must be produced.
071: *
072: * @param targets A Targets instance.
073: */
074: public TargetInformation(Targets targets) {
075: this .targets = new DERSequence(targets);
076: }
077:
078: /**
079: * According to RFC 3281 only one targets element must be produced. If
080: * multiple targets are given they must be merged in
081: * into one targets element.
082: *
083: * @param targets An array with {@link Targets}.
084: */
085: public TargetInformation(Target[] targets) {
086: this (new Targets(targets));
087: }
088:
089: /**
090: * Produce an object suitable for an ASN1OutputStream.
091: *
092: * Returns:
093: *
094: * <pre>
095: * SEQUENCE OF Targets
096: * </pre>
097: *
098: * <p>
099: * According to RFC 3281 only one targets element must be produced. If
100: * multiple targets are given in the constructor they are merged into one
101: * targets element. If this was produced from a
102: * {@link org.bouncycastle.asn1.ASN1Sequence} the encoding is kept.
103: *
104: * @return a DERObject
105: */
106: public DERObject toASN1Object() {
107: return targets;
108: }
109: }
|