001: package org.bouncycastle.asn1.x509;
002:
003: import java.util.Enumeration;
004: import java.util.Vector;
005:
006: import org.bouncycastle.asn1.ASN1Encodable;
007: import org.bouncycastle.asn1.ASN1EncodableVector;
008: import org.bouncycastle.asn1.ASN1Sequence;
009: import org.bouncycastle.asn1.DERObject;
010: import org.bouncycastle.asn1.DERSequence;
011:
012: /**
013: * This extension may contain further X.500 attributes of the subject. See also
014: * RFC 3039.
015: *
016: * <pre>
017: * SubjectDirectoryAttributes ::= Attributes
018: * Attributes ::= SEQUENCE SIZE (1..MAX) OF Attribute
019: * Attribute ::= SEQUENCE
020: * {
021: * type AttributeType
022: * values SET OF AttributeValue
023: * }
024: *
025: * AttributeType ::= OBJECT IDENTIFIER
026: * AttributeValue ::= ANY DEFINED BY AttributeType
027: * </pre>
028: *
029: * @see org.bouncycastle.asn1.x509.X509Name for AttributeType ObjectIdentifiers.
030: */
031: public class SubjectDirectoryAttributes extends ASN1Encodable {
032: private Vector attributes = new Vector();
033:
034: public static SubjectDirectoryAttributes getInstance(Object obj) {
035: if (obj == null || obj instanceof SubjectDirectoryAttributes) {
036: return (SubjectDirectoryAttributes) obj;
037: }
038:
039: if (obj instanceof ASN1Sequence) {
040: return new SubjectDirectoryAttributes((ASN1Sequence) obj);
041: }
042:
043: throw new IllegalArgumentException(
044: "illegal object in getInstance: "
045: + obj.getClass().getName());
046: }
047:
048: /**
049: * Constructor from ASN1Sequence.
050: *
051: * The sequence is of type SubjectDirectoryAttributes:
052: *
053: * <pre>
054: * SubjectDirectoryAttributes ::= Attributes
055: * Attributes ::= SEQUENCE SIZE (1..MAX) OF Attribute
056: * Attribute ::= SEQUENCE
057: * {
058: * type AttributeType
059: * values SET OF AttributeValue
060: * }
061: *
062: * AttributeType ::= OBJECT IDENTIFIER
063: * AttributeValue ::= ANY DEFINED BY AttributeType
064: * </pre>
065: *
066: * @param seq
067: * The ASN.1 sequence.
068: */
069: public SubjectDirectoryAttributes(ASN1Sequence seq) {
070: Enumeration e = seq.getObjects();
071:
072: while (e.hasMoreElements()) {
073: ASN1Sequence s = ASN1Sequence.getInstance(e.nextElement());
074: attributes.addElement(new Attribute(s));
075: }
076: }
077:
078: /**
079: * Constructor from a vector of attributes.
080: *
081: * The vector consists of attributes of type {@link Attribute Attribute}
082: *
083: * @param attributes
084: * The attributes.
085: *
086: */
087: public SubjectDirectoryAttributes(Vector attributes) {
088: Enumeration e = attributes.elements();
089:
090: while (e.hasMoreElements()) {
091: this .attributes.addElement(e.nextElement());
092: }
093: }
094:
095: /**
096: * Produce an object suitable for an ASN1OutputStream.
097: *
098: * Returns:
099: *
100: * <pre>
101: * SubjectDirectoryAttributes ::= Attributes
102: * Attributes ::= SEQUENCE SIZE (1..MAX) OF Attribute
103: * Attribute ::= SEQUENCE
104: * {
105: * type AttributeType
106: * values SET OF AttributeValue
107: * }
108: *
109: * AttributeType ::= OBJECT IDENTIFIER
110: * AttributeValue ::= ANY DEFINED BY AttributeType
111: * </pre>
112: *
113: * @return a DERObject
114: */
115: public DERObject toASN1Object() {
116: ASN1EncodableVector vec = new ASN1EncodableVector();
117: Enumeration e = attributes.elements();
118:
119: while (e.hasMoreElements()) {
120:
121: vec.add((Attribute) e.nextElement());
122: }
123:
124: return new DERSequence(vec);
125: }
126:
127: /**
128: * @return Returns the attributes.
129: */
130: public Vector getAttributes() {
131: return attributes;
132: }
133: }
|