0N/A/*
2362N/A * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.x509;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.lang.reflect.Constructor;
0N/Aimport java.util.Arrays;
0N/A
0N/Aimport sun.security.util.*;
0N/A
0N/A/**
0N/A * This class represents the OtherName as required by the GeneralNames
0N/A * ASN.1 object. It supplies the generic framework to allow specific
0N/A * Other Name types, and also provides minimal support for unrecognized
0N/A * Other Name types.
0N/A *
0N/A * The ASN.1 definition for OtherName is:
0N/A * <pre>
0N/A * OtherName ::= SEQUENCE {
0N/A * type-id OBJECT IDENTIFIER,
0N/A * value [0] EXPLICIT ANY DEFINED BY type-id
0N/A * }
0N/A * </pre>
0N/A * @author Hemma Prafullchandra
0N/A */
0N/Apublic class OtherName implements GeneralNameInterface {
0N/A
0N/A private String name;
0N/A private ObjectIdentifier oid;
0N/A private byte[] nameValue = null;
0N/A private GeneralNameInterface gni = null;
0N/A
0N/A private static final byte TAG_VALUE = 0;
0N/A
0N/A private int myhash = -1;
0N/A
0N/A /**
0N/A * Create the OtherName object from a passed ObjectIdentfier and
0N/A * byte array name value
0N/A *
0N/A * @param oid ObjectIdentifier of this OtherName object
0N/A * @param value the DER-encoded value of the OtherName
0N/A * @throws IOException on error
0N/A */
0N/A public OtherName(ObjectIdentifier oid, byte[] value) throws IOException {
0N/A if (oid == null || value == null) {
0N/A throw new NullPointerException("parameters may not be null");
0N/A }
0N/A this.oid = oid;
0N/A this.nameValue = value;
0N/A gni = getGNI(oid, value);
0N/A if (gni != null) {
0N/A name = gni.toString();
0N/A } else {
0N/A name = "Unrecognized ObjectIdentifier: " + oid.toString();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Create the OtherName object from the passed encoded Der value.
0N/A *
0N/A * @param derValue the encoded DER OtherName.
0N/A * @exception IOException on error.
0N/A */
0N/A public OtherName(DerValue derValue) throws IOException {
0N/A DerInputStream in = derValue.toDerInputStream();
0N/A
0N/A oid = in.getOID();
0N/A DerValue val = in.getDerValue();
0N/A nameValue = val.toByteArray();
0N/A gni = getGNI(oid, nameValue);
0N/A if (gni != null) {
0N/A name = gni.toString();
0N/A } else {
0N/A name = "Unrecognized ObjectIdentifier: " + oid.toString();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get ObjectIdentifier
0N/A */
0N/A public ObjectIdentifier getOID() {
0N/A //XXXX May want to consider cloning this
0N/A return oid;
0N/A }
0N/A
0N/A /**
0N/A * Get name value
0N/A */
0N/A public byte[] getNameValue() {
0N/A return nameValue.clone();
0N/A }
0N/A
0N/A /**
0N/A * Get GeneralNameInterface
0N/A */
0N/A private GeneralNameInterface getGNI(ObjectIdentifier oid, byte[] nameValue)
0N/A throws IOException {
0N/A try {
0N/A Class extClass = OIDMap.getClass(oid);
0N/A if (extClass == null) { // Unsupported OtherName
0N/A return null;
0N/A }
0N/A Class[] params = { Object.class };
0N/A Constructor cons = ((Class<?>)extClass).getConstructor(params);
0N/A
0N/A Object[] passed = new Object[] { nameValue };
0N/A GeneralNameInterface gni =
0N/A (GeneralNameInterface)cons.newInstance(passed);
0N/A return gni;
0N/A } catch (Exception e) {
0N/A throw (IOException)new IOException("Instantiation error: " + e).initCause(e);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return the type of the GeneralName.
0N/A */
0N/A public int getType() {
0N/A return GeneralNameInterface.NAME_ANY;
0N/A }
0N/A
0N/A /**
0N/A * Encode the Other name into the DerOutputStream.
0N/A *
0N/A * @param out the DER stream to encode the Other-Name to.
0N/A * @exception IOException on encoding errors.
0N/A */
0N/A public void encode(DerOutputStream out) throws IOException {
0N/A if (gni != null) {
0N/A // This OtherName has a supported class
0N/A gni.encode(out);
0N/A return;
0N/A } else {
0N/A // This OtherName has no supporting class
0N/A DerOutputStream tmp = new DerOutputStream();
0N/A tmp.putOID(oid);
0N/A tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, TAG_VALUE), nameValue);
0N/A out.write(DerValue.tag_Sequence, tmp);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Compares this name with another, for equality.
0N/A *
0N/A * @return true iff the names are identical.
0N/A */
0N/A public boolean equals(Object other) {
0N/A if (this == other) {
0N/A return true;
0N/A }
0N/A if (!(other instanceof OtherName)) {
0N/A return false;
0N/A }
0N/A OtherName otherOther = (OtherName)other;
0N/A if (!(otherOther.oid.equals(oid))) {
0N/A return false;
0N/A }
0N/A GeneralNameInterface otherGNI = null;
0N/A try {
0N/A otherGNI = getGNI(otherOther.oid, otherOther.nameValue);
0N/A } catch (IOException ioe) {
0N/A return false;
0N/A }
0N/A
0N/A boolean result;
0N/A if (otherGNI != null) {
0N/A try {
0N/A result = (otherGNI.constrains(this) == NAME_MATCH);
0N/A } catch (UnsupportedOperationException ioe) {
0N/A result = false;
0N/A }
0N/A } else {
0N/A result = Arrays.equals(nameValue, otherOther.nameValue);
0N/A }
0N/A
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Returns the hash code for this OtherName.
0N/A *
0N/A * @return a hash code value.
0N/A */
0N/A public int hashCode() {
0N/A if (myhash == -1) {
0N/A myhash = 37 + oid.hashCode();
0N/A for (int i = 0; i < nameValue.length; i++) {
0N/A myhash = 37 * myhash + nameValue[i];
0N/A }
0N/A }
0N/A return myhash;
0N/A }
0N/A
0N/A /**
0N/A * Convert the name into user readable string.
0N/A */
0N/A public String toString() {
0N/A return "Other-Name: " + name;
0N/A }
0N/A
0N/A /**
0N/A * Return type of constraint inputName places on this name:<ul>
0N/A * <li>NAME_DIFF_TYPE = -1: input name is different type from name
0N/A * (i.e. does not constrain).
0N/A * <li>NAME_MATCH = 0: input name matches name.
0N/A * <li>NAME_NARROWS = 1: input name narrows name (is lower in the
0N/A * naming subtree)
0N/A * <li>NAME_WIDENS = 2: input name widens name (is higher in the
0N/A * naming subtree)
0N/A * <li>NAME_SAME_TYPE = 3: input name does not match or narrow name,
0N/A * but is same type.
0N/A * </ul>. These results are used in checking NameConstraints during
0N/A * certification path verification.
0N/A *
0N/A * @param inputName to be checked for being constrained
0N/A * @returns constraint type above
0N/A * @throws UnsupportedOperationException if name is same type, but
0N/A * comparison operations are not supported for this name type.
0N/A */
0N/A public int constrains(GeneralNameInterface inputName) {
0N/A int constraintType;
0N/A if (inputName == null) {
0N/A constraintType = NAME_DIFF_TYPE;
0N/A } else if (inputName.getType() != NAME_ANY) {
0N/A constraintType = NAME_DIFF_TYPE;
0N/A } else {
0N/A throw new UnsupportedOperationException("Narrowing, widening, "
0N/A + "and matching are not supported for OtherName.");
0N/A }
0N/A return constraintType;
0N/A }
0N/A
0N/A /**
0N/A * Return subtree depth of this name for purposes of determining
0N/A * NameConstraints minimum and maximum bounds.
0N/A *
0N/A * @returns distance of name from root
0N/A * @throws UnsupportedOperationException if not supported for this name type
0N/A */
0N/A public int subtreeDepth() {
0N/A throw new UnsupportedOperationException
0N/A ("subtreeDepth() not supported for generic OtherName");
0N/A }
0N/A
0N/A}