001: /*
002: * @(#)CertificateSubjectName.java 1.17 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.security.x509;
029:
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.util.Enumeration;
034:
035: import javax.security.auth.x500.X500Principal;
036:
037: import sun.security.util.*;
038:
039: /**
040: * This class defines the X500Name attribute for the Certificate.
041: *
042: * @author Amit Kapoor
043: * @author Hemma Prafullchandra
044: * @version 1.10
045: * @see CertAttrSet
046: */
047: public class CertificateSubjectName implements CertAttrSet {
048: /**
049: * Identifier for this attribute, to be used with the
050: * get, set, delete methods of Certificate, x509 type.
051: */
052: public static final String IDENT = "x509.info.subject";
053: /**
054: * Sub attributes name for this CertAttrSet.
055: */
056: public static final String NAME = "subject";
057: public static final String DN_NAME = "dname";
058:
059: // accessor name for cached X500Principal only
060: // do not allow a set() of this value, do not advertise with getElements()
061: public static final String DN_PRINCIPAL = "x500principal";
062:
063: // Private data member
064: private X500Name dnName;
065:
066: // cached X500Principal version of the name
067: private X500Principal dnPrincipal;
068:
069: /**
070: * Default constructor for the certificate attribute.
071: *
072: * @param name the X500Name
073: */
074: public CertificateSubjectName(X500Name name) {
075: this .dnName = name;
076: }
077:
078: /**
079: * Create the object, decoding the values from the passed DER stream.
080: *
081: * @param in the DerInputStream to read the X500Name from.
082: * @exception IOException on decoding errors.
083: */
084: public CertificateSubjectName(DerInputStream in) throws IOException {
085: dnName = new X500Name(in);
086: }
087:
088: /**
089: * Create the object, decoding the values from the passed stream.
090: *
091: * @param in the InputStream to read the X500Name from.
092: * @exception IOException on decoding errors.
093: */
094: public CertificateSubjectName(InputStream in) throws IOException {
095: DerValue derVal = new DerValue(in);
096: dnName = new X500Name(derVal);
097: }
098:
099: /**
100: * Return the name as user readable string.
101: */
102: public String toString() {
103: if (dnName == null)
104: return "";
105: return (dnName.toString());
106: }
107:
108: /**
109: * Encode the name in DER form to the stream.
110: *
111: * @param out the DerOutputStream to marshal the contents to.
112: * @exception IOException on errors.
113: */
114: public void encode(OutputStream out) throws IOException {
115: DerOutputStream tmp = new DerOutputStream();
116: dnName.encode(tmp);
117:
118: out.write(tmp.toByteArray());
119: }
120:
121: /**
122: * Decode the name in DER form from the stream.
123: *
124: * @param in the InputStream to marshal the contents from.
125: * @exception IOException on errors.
126: */
127: public void decode(InputStream in) throws IOException {
128: DerValue derVal = new DerValue(in);
129: dnName = new X500Name(derVal);
130: dnPrincipal = null;
131: }
132:
133: /**
134: * Set the attribute value.
135: */
136: public void set(String name, Object obj) throws IOException {
137: if (!(obj instanceof X500Name)) {
138: throw new IOException("Attribute must be of type X500Name.");
139: }
140: if (name.equalsIgnoreCase(DN_NAME)) {
141: this .dnName = (X500Name) obj;
142: this .dnPrincipal = null;
143: } else {
144: throw new IOException("Attribute name not recognized by "
145: + "CertAttrSet:CertificateSubjectName.");
146: }
147: }
148:
149: /**
150: * Get the attribute value.
151: */
152: public Object get(String name) throws IOException {
153: if (name.equalsIgnoreCase(DN_NAME)) {
154: return (dnName);
155: } else if (name.equalsIgnoreCase(DN_PRINCIPAL)) {
156: if ((dnPrincipal == null) && (dnName != null)) {
157: dnPrincipal = dnName.asX500Principal();
158: }
159: return dnPrincipal;
160: } else {
161: throw new IOException("Attribute name not recognized by "
162: + "CertAttrSet:CertificateSubjectName.");
163: }
164: }
165:
166: /**
167: * Delete the attribute value.
168: */
169: public void delete(String name) throws IOException {
170: if (name.equalsIgnoreCase(DN_NAME)) {
171: dnName = null;
172: dnPrincipal = null;
173: } else {
174: throw new IOException("Attribute name not recognized by "
175: + "CertAttrSet:CertificateSubjectName.");
176: }
177: }
178:
179: /**
180: * Return an enumeration of names of attributes existing within this
181: * attribute.
182: */
183: public Enumeration getElements() {
184: AttributeNameEnumeration elements = new AttributeNameEnumeration();
185: elements.addElement(DN_NAME);
186:
187: return (elements.elements());
188: }
189:
190: /**
191: * Return the name of this attribute.
192: */
193: public String getName() {
194: return (NAME);
195: }
196: }
|