Path: blob/master/src/java.base/share/classes/sun/security/x509/CertificateSubjectName.java
41159 views
/*1* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.x509;2627import java.io.IOException;28import java.io.InputStream;29import java.io.OutputStream;30import java.util.Enumeration;3132import javax.security.auth.x500.X500Principal;3334import sun.security.util.*;3536/**37* This class defines the X500Name attribute for the Certificate.38*39* @author Amit Kapoor40* @author Hemma Prafullchandra41* @see CertAttrSet42*/43public class CertificateSubjectName implements CertAttrSet<String> {44/**45* Identifier for this attribute, to be used with the46* get, set, delete methods of Certificate, x509 type.47*/48public static final String IDENT = "x509.info.subject";49/**50* Sub attributes name for this CertAttrSet.51*/52public static final String NAME = "subject";53public static final String DN_NAME = "dname";5455// accessor name for cached X500Principal only56// do not allow a set() of this value, do not advertise with getElements()57public static final String DN_PRINCIPAL = "x500principal";5859// Private data member60private X500Name dnName;6162// cached X500Principal version of the name63private X500Principal dnPrincipal;6465/**66* Default constructor for the certificate attribute.67*68* @param name the X500Name69*/70public CertificateSubjectName(X500Name name) {71this.dnName = name;72}7374/**75* Create the object, decoding the values from the passed DER stream.76*77* @param in the DerInputStream to read the X500Name from.78* @exception IOException on decoding errors.79*/80public CertificateSubjectName(DerInputStream in) throws IOException {81dnName = new X500Name(in);82}8384/**85* Create the object, decoding the values from the passed stream.86*87* @param in the InputStream to read the X500Name from.88* @exception IOException on decoding errors.89*/90public CertificateSubjectName(InputStream in) throws IOException {91DerValue derVal = new DerValue(in);92dnName = new X500Name(derVal);93}9495/**96* Return the name as user readable string.97*/98public String toString() {99if (dnName == null) return "";100return(dnName.toString());101}102103/**104* Encode the name in DER form to the stream.105*106* @param out the DerOutputStream to marshal the contents to.107* @exception IOException on errors.108*/109public void encode(OutputStream out) throws IOException {110DerOutputStream tmp = new DerOutputStream();111dnName.encode(tmp);112113out.write(tmp.toByteArray());114}115116/**117* Set the attribute value.118*/119public void set(String name, Object obj) throws IOException {120if (!(obj instanceof X500Name)) {121throw new IOException("Attribute must be of type X500Name.");122}123if (name.equalsIgnoreCase(DN_NAME)) {124this.dnName = (X500Name)obj;125this.dnPrincipal = null;126} else {127throw new IOException("Attribute name not recognized by " +128"CertAttrSet:CertificateSubjectName.");129}130}131132/**133* Get the attribute value.134*/135public Object get(String name) throws IOException {136if (name.equalsIgnoreCase(DN_NAME)) {137return(dnName);138} else if (name.equalsIgnoreCase(DN_PRINCIPAL)) {139if ((dnPrincipal == null) && (dnName != null)) {140dnPrincipal = dnName.asX500Principal();141}142return dnPrincipal;143} else {144throw new IOException("Attribute name not recognized by " +145"CertAttrSet:CertificateSubjectName.");146}147}148149/**150* Delete the attribute value.151*/152public void delete(String name) throws IOException {153if (name.equalsIgnoreCase(DN_NAME)) {154dnName = null;155dnPrincipal = null;156} else {157throw new IOException("Attribute name not recognized by " +158"CertAttrSet:CertificateSubjectName.");159}160}161162/**163* Return an enumeration of names of attributes existing within this164* attribute.165*/166public Enumeration<String> getElements() {167AttributeNameEnumeration elements = new AttributeNameEnumeration();168elements.addElement(DN_NAME);169170return(elements.elements());171}172173/**174* Return the name of this attribute.175*/176public String getName() {177return(NAME);178}179}180181182