Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/x509/IssuerAlternativeNameExtension.java
38831 views
/*1* Copyright (c) 1997, 2011, 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.OutputStream;29import java.util.Enumeration;3031import sun.security.util.*;3233/**34* This represents the Issuer Alternative Name Extension.35*36* This extension, if present, allows the issuer to specify multiple37* alternative names.38*39* <p>Extensions are represented as a sequence of the extension identifier40* (Object Identifier), a boolean flag stating whether the extension is to41* be treated as being critical and the extension value itself (this is again42* a DER encoding of the extension value).43*44* @author Amit Kapoor45* @author Hemma Prafullchandra46* @see Extension47* @see CertAttrSet48*/49public class IssuerAlternativeNameExtension50extends Extension implements CertAttrSet<String> {51/**52* Identifier for this attribute, to be used with the53* get, set, delete methods of Certificate, x509 type.54*/55public static final String IDENT =56"x509.info.extensions.IssuerAlternativeName";57/**58* Attribute names.59*/60public static final String NAME = "IssuerAlternativeName";61public static final String ISSUER_NAME = "issuer_name";6263// private data members64GeneralNames names = null;6566// Encode this extension67private void encodeThis() throws IOException {68if (names == null || names.isEmpty()) {69this.extensionValue = null;70return;71}72DerOutputStream os = new DerOutputStream();73names.encode(os);74this.extensionValue = os.toByteArray();75}7677/**78* Create a IssuerAlternativeNameExtension with the passed GeneralNames.79*80* @param names the GeneralNames for the issuer.81* @exception IOException on error.82*/83public IssuerAlternativeNameExtension(GeneralNames names)84throws IOException {85this.names = names;86this.extensionId = PKIXExtensions.IssuerAlternativeName_Id;87this.critical = false;88encodeThis();89}9091/**92* Create a IssuerAlternativeNameExtension with the passed criticality93* and GeneralNames.94*95* @param critical true if the extension is to be treated as critical.96* @param names the GeneralNames for the issuer.97* @exception IOException on error.98*/99public IssuerAlternativeNameExtension(Boolean critical, GeneralNames names)100throws IOException {101this.names = names;102this.extensionId = PKIXExtensions.IssuerAlternativeName_Id;103this.critical = critical.booleanValue();104encodeThis();105}106107/**108* Create a default IssuerAlternativeNameExtension.109*/110public IssuerAlternativeNameExtension() {111extensionId = PKIXExtensions.IssuerAlternativeName_Id;112critical = false;113names = new GeneralNames();114}115116/**117* Create the extension from the passed DER encoded value.118*119* @param critical true if the extension is to be treated as critical.120* @param value an array of DER encoded bytes of the actual value.121* @exception ClassCastException if value is not an array of bytes122* @exception IOException on error.123*/124public IssuerAlternativeNameExtension(Boolean critical, Object value)125throws IOException {126this.extensionId = PKIXExtensions.IssuerAlternativeName_Id;127this.critical = critical.booleanValue();128this.extensionValue = (byte[]) value;129DerValue val = new DerValue(this.extensionValue);130if (val.data == null) {131names = new GeneralNames();132return;133}134135names = new GeneralNames(val);136}137138/**139* Returns a printable representation of the IssuerAlternativeName.140*/141public String toString() {142143String result = super.toString() + "IssuerAlternativeName [\n";144if(names == null) {145result += " null\n";146} else {147for(GeneralName name: names.names()) {148result += " "+name+"\n";149}150}151result += "]\n";152return result;153}154155/**156* Write the extension to the OutputStream.157*158* @param out the OutputStream to write the extension to.159* @exception IOException on encoding error.160*/161public void encode(OutputStream out) throws IOException {162DerOutputStream tmp = new DerOutputStream();163if (extensionValue == null) {164extensionId = PKIXExtensions.IssuerAlternativeName_Id;165critical = false;166encodeThis();167}168super.encode(tmp);169out.write(tmp.toByteArray());170}171172/**173* Set the attribute value.174*/175public void set(String name, Object obj) throws IOException {176if (name.equalsIgnoreCase(ISSUER_NAME)) {177if (!(obj instanceof GeneralNames)) {178throw new IOException("Attribute value should be of" +179" type GeneralNames.");180}181names = (GeneralNames)obj;182} else {183throw new IOException("Attribute name not recognized by " +184"CertAttrSet:IssuerAlternativeName.");185}186encodeThis();187}188189/**190* Get the attribute value.191*/192public GeneralNames get(String name) throws IOException {193if (name.equalsIgnoreCase(ISSUER_NAME)) {194return (names);195} else {196throw new IOException("Attribute name not recognized by " +197"CertAttrSet:IssuerAlternativeName.");198}199}200201/**202* Delete the attribute value.203*/204public void delete(String name) throws IOException {205if (name.equalsIgnoreCase(ISSUER_NAME)) {206names = null;207} else {208throw new IOException("Attribute name not recognized by " +209"CertAttrSet:IssuerAlternativeName.");210}211encodeThis();212}213214/**215* Return an enumeration of names of attributes existing within this216* attribute.217*/218public Enumeration<String> getElements() {219AttributeNameEnumeration elements = new AttributeNameEnumeration();220elements.addElement(ISSUER_NAME);221222return (elements.elements());223}224225/**226* Return the name of this attribute.227*/228public String getName() {229return (NAME);230}231}232233234