Path: blob/master/src/java.base/share/classes/sun/security/x509/DistributionPointName.java
41159 views
/*1* Copyright (c) 2005, 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.util.*;2930import sun.security.util.BitArray;31import sun.security.util.DerOutputStream;32import sun.security.util.DerValue;3334/**35* Represents the DistributionPointName ASN.1 type.36*37* It is used in the CRL Distribution Points Extension (OID = 2.5.29.31)38* and the Issuing Distribution Point Extension (OID = 2.5.29.28).39* <p>40* Its ASN.1 definition is:41* <pre>42*43* DistributionPointName ::= CHOICE {44* fullName [0] GeneralNames,45* nameRelativeToCRLIssuer [1] RelativeDistinguishedName }46*47* GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName48*49* GeneralName ::= CHOICE {50* otherName [0] INSTANCE OF OTHER-NAME,51* rfc822Name [1] IA5String,52* dNSName [2] IA5String,53* x400Address [3] ORAddress,54* directoryName [4] Name,55* ediPartyName [5] EDIPartyName,56* uniformResourceIdentifier [6] IA5String,57* iPAddress [7] OCTET STRING,58* registeredID [8] OBJECT IDENTIFIER }59*60* RelativeDistinguishedName ::= SET OF AttributeTypeAndValue61*62* AttributeTypeAndValue ::= SEQUENCE {63* type AttributeType,64* value AttributeValue }65*66* AttributeType ::= OBJECT IDENTIFIER67*68* AttributeValue ::= ANY DEFINED BY AttributeType69*70* </pre>71* <p>72* Instances of this class are designed to be immutable. However, since this73* is an internal API we do not use defensive cloning for values for74* performance reasons. It is the responsibility of the consumer to ensure75* that no mutable elements are modified.76*77* @see CRLDistributionPointsExtension78* @see IssuingDistributionPointExtension79* @since 1.680*/81public class DistributionPointName {8283// ASN.1 context specific tag values84private static final byte TAG_FULL_NAME = 0;85private static final byte TAG_RELATIVE_NAME = 1;8687// Only one of fullName and relativeName can be set88private GeneralNames fullName = null;89private RDN relativeName = null;9091// Cached hashCode value92private volatile int hashCode;9394/**95* Creates a distribution point name using a full name.96*97* @param fullName the name for the distribution point.98* @exception IllegalArgumentException if <code>fullName</code> is null.99*/100public DistributionPointName(GeneralNames fullName) {101102if (fullName == null) {103throw new IllegalArgumentException("fullName must not be null");104}105this.fullName = fullName;106}107108/**109* Creates a distribution point name using a relative name.110*111* @param relativeName the name of the distribution point relative to112* the name of the issuer of the CRL.113* @exception IllegalArgumentException if <code>relativeName</code> is null.114*/115public DistributionPointName(RDN relativeName) {116117if (relativeName == null) {118throw new IllegalArgumentException("relativeName must not be null");119}120this.relativeName = relativeName;121}122123/**124* Creates a distribution point name from its DER-encoded form.125*126* @param encoding the DER-encoded value.127* @throws IOException on decoding error.128*/129public DistributionPointName(DerValue encoding) throws IOException {130131if (encoding.isContextSpecific(TAG_FULL_NAME) &&132encoding.isConstructed()) {133134encoding.resetTag(DerValue.tag_Sequence);135fullName = new GeneralNames(encoding);136137} else if (encoding.isContextSpecific(TAG_RELATIVE_NAME) &&138encoding.isConstructed()) {139140encoding.resetTag(DerValue.tag_Set);141relativeName = new RDN(encoding);142143} else {144throw new IOException("Invalid encoding for DistributionPointName");145}146147}148149/**150* Returns the full name for the distribution point or null if not set.151*/152public GeneralNames getFullName() {153return fullName;154}155156/**157* Returns the relative name for the distribution point or null if not set.158*/159public RDN getRelativeName() {160return relativeName;161}162163/**164* Encodes the distribution point name and writes it to the DerOutputStream.165*166* @param out the output stream.167* @exception IOException on encoding error.168*/169public void encode(DerOutputStream out) throws IOException {170171DerOutputStream theChoice = new DerOutputStream();172173if (fullName != null) {174fullName.encode(theChoice);175out.writeImplicit(176DerValue.createTag(DerValue.TAG_CONTEXT, true, TAG_FULL_NAME),177theChoice);178179} else {180relativeName.encode(theChoice);181out.writeImplicit(182DerValue.createTag(DerValue.TAG_CONTEXT, true,183TAG_RELATIVE_NAME),184theChoice);185}186}187188/**189* Compare an object to this distribution point name for equality.190*191* @param obj Object to be compared to this192* @return true if objects match; false otherwise193*/194public boolean equals(Object obj) {195if (this == obj) {196return true;197}198if (obj instanceof DistributionPointName == false) {199return false;200}201DistributionPointName other = (DistributionPointName)obj;202203return Objects.equals(this.fullName, other.fullName) &&204Objects.equals(this.relativeName, other.relativeName);205}206207/**208* Returns the hash code for this distribution point name.209*210* @return the hash code.211*/212public int hashCode() {213int hash = hashCode;214if (hash == 0) {215hash = 1;216if (fullName != null) {217hash += fullName.hashCode();218219} else {220hash += relativeName.hashCode();221}222hashCode = hash;223}224return hash;225}226227/**228* Returns a printable string of the distribution point name.229*/230public String toString() {231StringBuilder sb = new StringBuilder();232sb.append("DistributionPointName:\n ");233if (fullName != null) {234sb.append(fullName);235} else {236sb.append(relativeName);237}238sb.append('\n');239return sb.toString();240}241}242243244