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: * Targets structure used in target information extension for attribute
012: * certificates from RFC 3281.
013: *
014: * <pre>
015: * Targets ::= SEQUENCE OF Target
016: *
017: * Target ::= CHOICE {
018: * targetName [0] GeneralName,
019: * targetGroup [1] GeneralName,
020: * targetCert [2] TargetCert
021: * }
022: *
023: * TargetCert ::= SEQUENCE {
024: * targetCertificate IssuerSerial,
025: * targetName GeneralName OPTIONAL,
026: * certDigestInfo ObjectDigestInfo OPTIONAL
027: * }
028: * </pre>
029: *
030: * @see org.bouncycastle.asn1.x509.Target
031: * @see org.bouncycastle.asn1.x509.TargetInformation
032: */
033: public class Targets extends ASN1Encodable {
034: private ASN1Sequence targets;
035:
036: /**
037: * Creates an instance of a Targets from the given object.
038: * <p>
039: * <code>obj</code> can be a Targets or a {@link ASN1Sequence}
040: *
041: * @param obj The object.
042: * @return A Targets instance.
043: * @throws IllegalArgumentException if the given object cannot be
044: * interpreted as Target.
045: */
046: public static Targets getInstance(Object obj) {
047: if (obj instanceof Targets) {
048: return (Targets) obj;
049: } else if (obj instanceof ASN1Sequence) {
050: return new Targets((ASN1Sequence) obj);
051: }
052:
053: throw new IllegalArgumentException(
054: "unknown object in factory: " + obj.getClass());
055: }
056:
057: /**
058: * Constructor from ASN1Sequence.
059: *
060: * @param targets The ASN.1 SEQUENCE.
061: * @throws IllegalArgumentException if the contents of the sequence are
062: * invalid.
063: */
064: private Targets(ASN1Sequence targets) {
065: this .targets = targets;
066: }
067:
068: /**
069: * Constructor from given targets.
070: * <p>
071: * The vector is copied.
072: *
073: * @param targets A <code>Vector</code> of {@link Target}s.
074: * @see Target
075: * @throws IllegalArgumentException if the vector contains not only Targets.
076: */
077: public Targets(Target[] targets) {
078: this .targets = new DERSequence(targets);
079: }
080:
081: /**
082: * Returns the targets in a <code>Vector</code>.
083: * <p>
084: * The vector is cloned before it is returned.
085: *
086: * @return Returns the targets.
087: */
088: public Target[] getTargets() {
089: Target[] targs = new Target[targets.size()];
090: int count = 0;
091: for (Enumeration e = targets.getObjects(); e.hasMoreElements();) {
092: targs[count++] = Target.getInstance(e.nextElement());
093: }
094: return targs;
095: }
096:
097: /**
098: * Produce an object suitable for an ASN1OutputStream.
099: *
100: * Returns:
101: *
102: * <pre>
103: * Targets ::= SEQUENCE OF Target
104: * </pre>
105: *
106: * @return a DERObject
107: */
108: public DERObject toASN1Object() {
109: return targets;
110: }
111: }
|