Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/x509/GeneralName.java
38831 views
/*1* Copyright (c) 1997, 2004, 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;2829import sun.security.util.*;3031/**32* This class implements the ASN.1 GeneralName object class.33* <p>34* The ASN.1 syntax for this is:35* <pre>36* GeneralName ::= CHOICE {37* otherName [0] OtherName,38* rfc822Name [1] IA5String,39* dNSName [2] IA5String,40* x400Address [3] ORAddress,41* directoryName [4] Name,42* ediPartyName [5] EDIPartyName,43* uniformResourceIdentifier [6] IA5String,44* iPAddress [7] OCTET STRING,45* registeredID [8] OBJECT IDENTIFIER46* }47* </pre>48* @author Amit Kapoor49* @author Hemma Prafullchandra50*/51public class GeneralName {5253// Private data members54private GeneralNameInterface name = null;5556/**57* Default constructor for the class.58*59* @param name the selected CHOICE from the list.60* @throws NullPointerException if name is null61*/62public GeneralName(GeneralNameInterface name) {63if (name == null) {64throw new NullPointerException("GeneralName must not be null");65}66this.name = name;67}6869/**70* Create the object from its DER encoded value.71*72* @param encName the DER encoded GeneralName.73*/74public GeneralName(DerValue encName) throws IOException {75this(encName, false);76}7778/**79* Create the object from its DER encoded value.80*81* @param encName the DER encoded GeneralName.82* @param nameConstraint true if general name is a name constraint83*/84public GeneralName(DerValue encName, boolean nameConstraint)85throws IOException {86short tag = (byte)(encName.tag & 0x1f);8788// All names except for NAME_DIRECTORY should be encoded with the89// IMPLICIT tag.90switch (tag) {91case GeneralNameInterface.NAME_ANY:92if (encName.isContextSpecific() && encName.isConstructed()) {93encName.resetTag(DerValue.tag_Sequence);94name = new OtherName(encName);95} else {96throw new IOException("Invalid encoding of Other-Name");97}98break;99100case GeneralNameInterface.NAME_RFC822:101if (encName.isContextSpecific() && !encName.isConstructed()) {102encName.resetTag(DerValue.tag_IA5String);103name = new RFC822Name(encName);104} else {105throw new IOException("Invalid encoding of RFC822 name");106}107break;108109case GeneralNameInterface.NAME_DNS:110if (encName.isContextSpecific() && !encName.isConstructed()) {111encName.resetTag(DerValue.tag_IA5String);112name = new DNSName(encName);113} else {114throw new IOException("Invalid encoding of DNSName");115}116break;117118case GeneralNameInterface.NAME_URI:119if (encName.isContextSpecific() && !encName.isConstructed()) {120encName.resetTag(DerValue.tag_IA5String);121name = (nameConstraint ? URIName.nameConstraint(encName) :122new URIName(encName));123} else {124throw new IOException("Invalid encoding of URI");125}126break;127128case GeneralNameInterface.NAME_IP:129if (encName.isContextSpecific() && !encName.isConstructed()) {130encName.resetTag(DerValue.tag_OctetString);131name = new IPAddressName(encName);132} else {133throw new IOException("Invalid encoding of IP address");134}135break;136137case GeneralNameInterface.NAME_OID:138if (encName.isContextSpecific() && !encName.isConstructed()) {139encName.resetTag(DerValue.tag_ObjectId);140name = new OIDName(encName);141} else {142throw new IOException("Invalid encoding of OID name");143}144break;145146case GeneralNameInterface.NAME_DIRECTORY:147if (encName.isContextSpecific() && encName.isConstructed()) {148name = new X500Name(encName.getData());149} else {150throw new IOException("Invalid encoding of Directory name");151}152break;153154case GeneralNameInterface.NAME_EDI:155if (encName.isContextSpecific() && encName.isConstructed()) {156encName.resetTag(DerValue.tag_Sequence);157name = new EDIPartyName(encName);158} else {159throw new IOException("Invalid encoding of EDI name");160}161break;162163default:164throw new IOException("Unrecognized GeneralName tag, ("165+ tag +")");166}167}168169/**170* Return the type of the general name.171*/172public int getType() {173return name.getType();174}175176/**177* Return the GeneralNameInterface name.178*/179public GeneralNameInterface getName() {180//XXXX May want to consider cloning this181return name;182}183184/**185* Return the name as user readable string186*/187public String toString() {188return name.toString();189}190191/**192* Compare this GeneralName with another193*194* @param other GeneralName to compare to this195* @returns true if match196*/197public boolean equals(Object other) {198if (this == other) {199return true;200}201if (!(other instanceof GeneralName))202return false;203GeneralNameInterface otherGNI = ((GeneralName)other).name;204try {205return name.constrains(otherGNI) == GeneralNameInterface.NAME_MATCH;206} catch (UnsupportedOperationException ioe) {207return false;208}209}210211/**212* Returns the hash code for this GeneralName.213*214* @return a hash code value.215*/216public int hashCode() {217return name.hashCode();218}219220/**221* Encode the name to the specified DerOutputStream.222*223* @param out the DerOutputStream to encode the the GeneralName to.224* @exception IOException on encoding errors.225*/226public void encode(DerOutputStream out) throws IOException {227DerOutputStream tmp = new DerOutputStream();228name.encode(tmp);229int nameType = name.getType();230if (nameType == GeneralNameInterface.NAME_ANY ||231nameType == GeneralNameInterface.NAME_X400 ||232nameType == GeneralNameInterface.NAME_EDI) {233234// implicit, constructed form235out.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,236true, (byte)nameType), tmp);237} else if (nameType == GeneralNameInterface.NAME_DIRECTORY) {238// explicit, constructed form since underlying tag is CHOICE239// (see X.680 section 30.6, part c)240out.write(DerValue.createTag(DerValue.TAG_CONTEXT,241true, (byte)nameType), tmp);242} else {243// implicit, primitive form244out.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,245false, (byte)nameType), tmp);246}247}248}249250251