1 /*
2  * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.security.x509;
27 
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
31 import java.util.Enumeration;
32 
33 import javax.security.auth.x500.X500Principal;
34 
35 import sun.security.util.*;
36 
37 /**
38  * This class defines the X500Name attribute for the Certificate.
39  *
40  * @author Amit Kapoor
41  * @author Hemma Prafullchandra
42  * @see CertAttrSet
43  */
44 public class CertificateSubjectName implements CertAttrSet<String> {
45     /**
46      * Identifier for this attribute, to be used with the
47      * get, set, delete methods of Certificate, x509 type.
48      */
49     public static final String IDENT = "x509.info.subject";
50     /**
51      * Sub attributes name for this CertAttrSet.
52      */
53     public static final String NAME = "subject";
54     public static final String DN_NAME = "dname";
55 
56     // accessor name for cached X500Principal only
57     // do not allow a set() of this value, do not advertise with getElements()
58     public static final String DN_PRINCIPAL = "x500principal";
59 
60     // Private data member
61     private X500Name    dnName;
62 
63     // cached X500Principal version of the name
64     private X500Principal dnPrincipal;
65 
66     /**
67      * Default constructor for the certificate attribute.
68      *
69      * @param name the X500Name
70      */
CertificateSubjectName(X500Name name)71     public CertificateSubjectName(X500Name name) {
72         this.dnName = name;
73     }
74 
75     /**
76      * Create the object, decoding the values from the passed DER stream.
77      *
78      * @param in the DerInputStream to read the X500Name from.
79      * @exception IOException on decoding errors.
80      */
CertificateSubjectName(DerInputStream in)81     public CertificateSubjectName(DerInputStream in) throws IOException {
82         dnName = new X500Name(in);
83     }
84 
85     /**
86      * Create the object, decoding the values from the passed stream.
87      *
88      * @param in the InputStream to read the X500Name from.
89      * @exception IOException on decoding errors.
90      */
CertificateSubjectName(InputStream in)91     public CertificateSubjectName(InputStream in) throws IOException {
92         DerValue derVal = new DerValue(in);
93         dnName = new X500Name(derVal);
94     }
95 
96     /**
97      * Return the name as user readable string.
98      */
toString()99     public String toString() {
100         if (dnName == null) return "";
101         return(dnName.toString());
102     }
103 
104     /**
105      * Encode the name in DER form to the stream.
106      *
107      * @param out the DerOutputStream to marshal the contents to.
108      * @exception IOException on errors.
109      */
encode(OutputStream out)110     public void encode(OutputStream out) throws IOException {
111         DerOutputStream tmp = new DerOutputStream();
112         dnName.encode(tmp);
113 
114         out.write(tmp.toByteArray());
115     }
116 
117     /**
118      * Set the attribute value.
119      */
set(String name, Object obj)120     public void set(String name, Object obj) throws IOException {
121         if (!(obj instanceof X500Name)) {
122             throw new IOException("Attribute must be of type X500Name.");
123         }
124         if (name.equalsIgnoreCase(DN_NAME)) {
125             this.dnName = (X500Name)obj;
126             this.dnPrincipal = null;
127         } else {
128             throw new IOException("Attribute name not recognized by " +
129                                   "CertAttrSet:CertificateSubjectName.");
130         }
131     }
132 
133     /**
134      * Get the attribute value.
135      */
get(String name)136     public Object get(String name) throws IOException {
137         if (name.equalsIgnoreCase(DN_NAME)) {
138             return(dnName);
139         } else if (name.equalsIgnoreCase(DN_PRINCIPAL)) {
140             if ((dnPrincipal == null) && (dnName != null)) {
141                 dnPrincipal = dnName.asX500Principal();
142             }
143             return dnPrincipal;
144         } else {
145             throw new IOException("Attribute name not recognized by " +
146                                   "CertAttrSet:CertificateSubjectName.");
147         }
148     }
149 
150     /**
151      * Delete the attribute value.
152      */
delete(String name)153     public void delete(String name) throws IOException {
154         if (name.equalsIgnoreCase(DN_NAME)) {
155             dnName = null;
156             dnPrincipal = null;
157         } else {
158             throw new IOException("Attribute name not recognized by " +
159                                   "CertAttrSet:CertificateSubjectName.");
160         }
161     }
162 
163     /**
164      * Return an enumeration of names of attributes existing within this
165      * attribute.
166      */
getElements()167     public Enumeration<String> getElements() {
168         AttributeNameEnumeration elements = new AttributeNameEnumeration();
169         elements.addElement(DN_NAME);
170 
171         return(elements.elements());
172     }
173 
174     /**
175      * Return the name of this attribute.
176      */
getName()177     public String getName() {
178         return(NAME);
179     }
180 }
181