Path: blob/master/src/java.base/share/classes/sun/security/x509/EDIPartyName.java
41159 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;28import sun.security.util.*;2930/**31* This class defines the EDIPartyName of the GeneralName choice.32* The ASN.1 syntax for this is:33* <pre>34* EDIPartyName ::= SEQUENCE {35* nameAssigner [0] DirectoryString OPTIONAL,36* partyName [1] DirectoryString }37* </pre>38*39* @author Hemma Prafullchandra40* @see GeneralName41* @see GeneralNames42* @see GeneralNameInterface43*/44public class EDIPartyName implements GeneralNameInterface {4546// Private data members47private static final byte TAG_ASSIGNER = 0;48private static final byte TAG_PARTYNAME = 1;4950private String assigner = null;51private String party = null;5253private int myhash = -1;5455/**56* Create the EDIPartyName object from the specified names.57*58* @param assignerName the name of the assigner59* @param partyName the name of the EDI party.60*/61public EDIPartyName(String assignerName, String partyName) {62this.assigner = assignerName;63this.party = partyName;64}6566/**67* Create the EDIPartyName object from the specified name.68*69* @param partyName the name of the EDI party.70*/71public EDIPartyName(String partyName) {72this.party = partyName;73}7475/**76* Create the EDIPartyName object from the passed encoded Der value.77*78* @param derValue the encoded DER EDIPartyName.79* @exception IOException on error.80*/81public EDIPartyName(DerValue derValue) throws IOException {82DerInputStream in = new DerInputStream(derValue.toByteArray());83DerValue[] seq = in.getSequence(2);8485int len = seq.length;86if (len < 1 || len > 2)87throw new IOException("Invalid encoding of EDIPartyName");8889for (int i = 0; i < len; i++) {90DerValue opt = seq[i];91if (opt.isContextSpecific(TAG_ASSIGNER) &&92!opt.isConstructed()) {93if (assigner != null)94throw new IOException("Duplicate nameAssigner found in"95+ " EDIPartyName");96opt = opt.data.getDerValue();97assigner = opt.getAsString();98}99if (opt.isContextSpecific(TAG_PARTYNAME) &&100!opt.isConstructed()) {101if (party != null)102throw new IOException("Duplicate partyName found in"103+ " EDIPartyName");104opt = opt.data.getDerValue();105party = opt.getAsString();106}107}108}109110/**111* Return the type of the GeneralName.112*/113public int getType() {114return (GeneralNameInterface.NAME_EDI);115}116117/**118* Encode the EDI party name into the DerOutputStream.119*120* @param out the DER stream to encode the EDIPartyName to.121* @exception IOException on encoding errors.122*/123public void encode(DerOutputStream out) throws IOException {124DerOutputStream tagged = new DerOutputStream();125DerOutputStream tmp = new DerOutputStream();126127if (assigner != null) {128DerOutputStream tmp2 = new DerOutputStream();129// XXX - shd check is chars fit into PrintableString130tmp2.putPrintableString(assigner);131tagged.write(DerValue.createTag(DerValue.TAG_CONTEXT,132false, TAG_ASSIGNER), tmp2);133}134if (party == null)135throw new IOException("Cannot have null partyName");136137// XXX - shd check is chars fit into PrintableString138tmp.putPrintableString(party);139tagged.write(DerValue.createTag(DerValue.TAG_CONTEXT,140false, TAG_PARTYNAME), tmp);141142out.write(DerValue.tag_Sequence, tagged);143}144145/**146* Return the assignerName147*148* @return String assignerName149*/150public String getAssignerName() {151return assigner;152}153154/**155* Return the partyName156*157* @return String partyName158*/159public String getPartyName() {160return party;161}162163/**164* Compare this EDIPartyName with another. Does a byte-string165* comparison without regard to type of the partyName and166* the assignerName.167*168* @return true if the two names match169*/170public boolean equals(Object other) {171if (!(other instanceof EDIPartyName))172return false;173String otherAssigner = ((EDIPartyName)other).assigner;174if (this.assigner == null) {175if (otherAssigner != null)176return false;177} else {178if (!(this.assigner.equals(otherAssigner)))179return false;180}181String otherParty = ((EDIPartyName)other).party;182if (this.party == null) {183if (otherParty != null)184return false;185} else {186if (!(this.party.equals(otherParty)))187return false;188}189return true;190}191192/**193* Returns the hash code value for this EDIPartyName.194*195* @return a hash code value.196*/197public int hashCode() {198if (myhash == -1) {199myhash = 37 + (party == null ? 1 : party.hashCode());200if (assigner != null) {201myhash = 37 * myhash + assigner.hashCode();202}203}204return myhash;205}206207/**208* Return the printable string.209*/210public String toString() {211StringBuilder sb = new StringBuilder("EDIPartyName: ");212if (assigner != null) {213sb.append(" nameAssigner = ")214.append(assigner)215.append(',');216}217sb.append(" partyName = ")218.append(party);219return sb.toString();220}221222/**223* Return constraint type:<ul>224* <li>NAME_DIFF_TYPE = -1: input name is different type from name (i.e. does not constrain)225* <li>NAME_MATCH = 0: input name matches name226* <li>NAME_NARROWS = 1: input name narrows name227* <li>NAME_WIDENS = 2: input name widens name228* <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but is same type229* </ul>. These results are used in checking NameConstraints during230* certification path verification.231*232* @param inputName to be checked for being constrained233* @return constraint type above234* @throws UnsupportedOperationException if name is same type, but comparison operations are235* not supported for this name type.236*/237public int constrains(GeneralNameInterface inputName) throws UnsupportedOperationException {238int constraintType;239if (inputName == null)240constraintType = NAME_DIFF_TYPE;241else if (inputName.getType() != NAME_EDI)242constraintType = NAME_DIFF_TYPE;243else {244throw new UnsupportedOperationException("Narrowing, widening, and matching of names not supported for EDIPartyName");245}246return constraintType;247}248249/**250* Return subtree depth of this name for purposes of determining251* NameConstraints minimum and maximum bounds and for calculating252* path lengths in name subtrees.253*254* @return distance of name from root255* @throws UnsupportedOperationException if not supported for this name type256*/257public int subtreeDepth() throws UnsupportedOperationException {258throw new UnsupportedOperationException("subtreeDepth() not supported for EDIPartyName");259}260261}262263264