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.DEREncodable;
008: import org.bouncycastle.asn1.DERObject;
009: import org.bouncycastle.asn1.DERSequence;
010: import org.bouncycastle.asn1.DERString;
011: import org.bouncycastle.asn1.DERTaggedObject;
012:
013: /**
014: * Implementation of the RoleSyntax object as specified by the RFC3281.
015: *
016: * <pre>
017: * RoleSyntax ::= SEQUENCE {
018: * roleAuthority [0] GeneralNames OPTIONAL,
019: * roleName [1] GeneralName
020: * }
021: * </pre>
022: */
023: public class RoleSyntax extends ASN1Encodable {
024: private GeneralNames roleAuthority;
025: private GeneralName roleName;
026:
027: /**
028: * RoleSyntax factory method.
029: * @param obj the object used to construct an instance of <code>
030: * RoleSyntax</code>. It must be an instance of <code>RoleSyntax
031: * </code> or <code>ASN1Sequence</code>.
032: * @return the instance of <code>RoleSyntax</code> built from the
033: * supplied object.
034: * @throws java.lang.IllegalArgumentException if the object passed
035: * to the factory is not an instance of <code>RoleSyntax</code> or
036: * <code>ASN1Sequence</code>.
037: */
038: public static RoleSyntax getInstance(Object obj) {
039:
040: if (obj == null || obj instanceof RoleSyntax) {
041: return (RoleSyntax) obj;
042: } else if (obj instanceof ASN1Sequence) {
043: return new RoleSyntax((ASN1Sequence) obj);
044: }
045: throw new IllegalArgumentException(
046: "Unknown object in RoleSyntax factory.");
047: }
048:
049: /**
050: * Constructor.
051: * @param roleAuthority the role authority of this RoleSyntax.
052: * @param roleName the role name of this RoleSyntax.
053: */
054: public RoleSyntax(GeneralNames roleAuthority, GeneralName roleName) {
055: if (roleName == null
056: || roleName.getTagNo() != GeneralName.uniformResourceIdentifier
057: || ((DERString) roleName.getName()).getString().equals(
058: "")) {
059: throw new IllegalArgumentException(
060: "the role name MUST be non empty and MUST "
061: + "use the URI option of GeneralName");
062: }
063: this .roleAuthority = roleAuthority;
064: this .roleName = roleName;
065: }
066:
067: /**
068: * Constructor. Invoking this constructor is the same as invoking
069: * <code>new RoleSyntax(null, roleName)</code>.
070: * @param roleName the role name of this RoleSyntax.
071: */
072: public RoleSyntax(GeneralName roleName) {
073: this (null, roleName);
074: }
075:
076: /**
077: * Utility constructor. Takes a <code>String</code> argument representing
078: * the role name, builds a <code>GeneralName</code> to hold the role name
079: * and calls the constructor that takes a <code>GeneralName</code>.
080: * @param roleName
081: */
082: public RoleSyntax(String roleName) {
083: this (new GeneralName(GeneralName.uniformResourceIdentifier,
084: (roleName == null) ? "" : roleName));
085: }
086:
087: /**
088: * Constructor that builds an instance of <code>RoleSyntax</code> by
089: * extracting the encoded elements from the <code>ASN1Sequence</code>
090: * object supplied.
091: * @param seq an instance of <code>ASN1Sequence</code> that holds
092: * the encoded elements used to build this <code>RoleSyntax</code>.
093: */
094: public RoleSyntax(ASN1Sequence seq) {
095: if (seq.size() < 1 || seq.size() > 2) {
096: throw new IllegalArgumentException("Bad sequence size: "
097: + seq.size());
098: }
099:
100: for (int i = 0; i != seq.size(); i++) {
101: ASN1TaggedObject taggedObject = ASN1TaggedObject
102: .getInstance(seq.getObjectAt(i));
103: switch (taggedObject.getTagNo()) {
104: case 0:
105: roleAuthority = GeneralNames.getInstance(taggedObject,
106: false);
107: break;
108: case 1:
109: roleName = GeneralName.getInstance(taggedObject, false);
110: break;
111: default:
112: throw new IllegalArgumentException(
113: "Unknown tag in RoleSyntax");
114: }
115: }
116: }
117:
118: /**
119: * Gets the role authority of this RoleSyntax.
120: * @return an instance of <code>GeneralNames</code> holding the
121: * role authority of this RoleSyntax.
122: */
123: public GeneralNames getRoleAuthority() {
124: return this .roleAuthority;
125: }
126:
127: /**
128: * Gets the role name of this RoleSyntax.
129: * @return an instance of <code>GeneralName</code> holding the
130: * role name of this RoleSyntax.
131: */
132: public GeneralName getRoleName() {
133: return this .roleName;
134: }
135:
136: /**
137: * Gets the role name as a <code>java.lang.String</code> object.
138: * @return the role name of this RoleSyntax represented as a
139: * <code>java.lang.String</code> object.
140: */
141: public String getRoleNameAsString() {
142: DERString str = (DERString) this .roleName.getName();
143:
144: return str.getString();
145: }
146:
147: /**
148: * Gets the role authority as a <code>String[]</code> object.
149: * @return the role authority of this RoleSyntax represented as a
150: * <code>String[]</code> array.
151: */
152: public String[] getRoleAuthorityAsString() {
153: if (roleAuthority == null) {
154: return new String[0];
155: }
156:
157: GeneralName[] names = roleAuthority.getNames();
158: String[] namesString = new String[names.length];
159: for (int i = 0; i < names.length; i++) {
160: DEREncodable value = names[i].getName();
161: if (value instanceof DERString) {
162: namesString[i] = ((DERString) value).getString();
163: } else {
164: namesString[i] = value.toString();
165: }
166: }
167: return namesString;
168: }
169:
170: /**
171: * Implementation of the method <code>toASN1Object</code> as
172: * required by the superclass <code>ASN1Encodable</code>.
173: *
174: * <pre>
175: * RoleSyntax ::= SEQUENCE {
176: * roleAuthority [0] GeneralNames OPTIONAL,
177: * roleName [1] GeneralName
178: * }
179: * </pre>
180: */
181: public DERObject toASN1Object() {
182: ASN1EncodableVector v = new ASN1EncodableVector();
183: if (this .roleAuthority != null) {
184: v.add(new DERTaggedObject(false, 0, roleAuthority));
185: }
186: v.add(new DERTaggedObject(false, 1, roleName));
187:
188: return new DERSequence(v);
189: }
190:
191: public String toString() {
192: StringBuffer buff = new StringBuffer("Name: "
193: + this .getRoleNameAsString() + " - Auth: ");
194: if (this .roleAuthority == null
195: || roleAuthority.getNames().length == 0) {
196: buff.append("N/A");
197: } else {
198: String[] names = this .getRoleAuthorityAsString();
199: buff.append('[').append(names[0]);
200: for (int i = 1; i < names.length; i++) {
201: buff.append(", ").append(names[i]);
202: }
203: buff.append(']');
204: }
205: return buff.toString();
206: }
207: }
|