Path: blob/master/src/java.base/share/classes/sun/security/x509/IssuerAlternativeNameExtension.java
41159 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() {142StringBuilder sb = new StringBuilder();143sb.append(super.toString())144.append("IssuerAlternativeName [\n");145if (names == null) {146sb.append(" null\n");147} else {148for (GeneralName name : names.names()) {149sb.append(" ")150.append(name)151.append('\n');152}153}154sb.append("]\n");155return sb.toString();156}157158/**159* Write the extension to the OutputStream.160*161* @param out the OutputStream to write the extension to.162* @exception IOException on encoding error.163*/164public void encode(OutputStream out) throws IOException {165DerOutputStream tmp = new DerOutputStream();166if (extensionValue == null) {167extensionId = PKIXExtensions.IssuerAlternativeName_Id;168critical = false;169encodeThis();170}171super.encode(tmp);172out.write(tmp.toByteArray());173}174175/**176* Set the attribute value.177*/178public void set(String name, Object obj) throws IOException {179if (name.equalsIgnoreCase(ISSUER_NAME)) {180if (!(obj instanceof GeneralNames)) {181throw new IOException("Attribute value should be of" +182" type GeneralNames.");183}184names = (GeneralNames)obj;185} else {186throw new IOException("Attribute name not recognized by " +187"CertAttrSet:IssuerAlternativeName.");188}189encodeThis();190}191192/**193* Get the attribute value.194*/195public GeneralNames get(String name) throws IOException {196if (name.equalsIgnoreCase(ISSUER_NAME)) {197return (names);198} else {199throw new IOException("Attribute name not recognized by " +200"CertAttrSet:IssuerAlternativeName.");201}202}203204/**205* Delete the attribute value.206*/207public void delete(String name) throws IOException {208if (name.equalsIgnoreCase(ISSUER_NAME)) {209names = null;210} else {211throw new IOException("Attribute name not recognized by " +212"CertAttrSet:IssuerAlternativeName.");213}214encodeThis();215}216217/**218* Return an enumeration of names of attributes existing within this219* attribute.220*/221public Enumeration<String> getElements() {222AttributeNameEnumeration elements = new AttributeNameEnumeration();223elements.addElement(ISSUER_NAME);224225return (elements.elements());226}227228/**229* Return the name of this attribute.230*/231public String getName() {232return (NAME);233}234}235236237