1/*
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.security.x509;
27
28import java.io.IOException;
29
30import sun.security.util.*;
31
32/**
33 * This class implements the OIDName as required by the GeneralNames
34 * ASN.1 object.
35 *
36 * @author Amit Kapoor
37 * @author Hemma Prafullchandra
38 * @see GeneralName
39 * @see GeneralNames
40 * @see GeneralNameInterface
41 */
42public class OIDName implements GeneralNameInterface {
43     private ObjectIdentifier oid;
44
45    /**
46     * Create the OIDName object from the passed encoded Der value.
47     *
48     * @param derValue the encoded DER OIDName.
49     * @exception IOException on error.
50     */
51    public OIDName(DerValue derValue) throws IOException {
52        oid = derValue.getOID();
53    }
54
55    /**
56     * Create the OIDName object with the specified name.
57     *
58     * @param name the OIDName.
59     */
60    public OIDName(ObjectIdentifier oid) {
61        this.oid = oid;
62    }
63
64    /**
65     * Create the OIDName from the String form of the OID
66     *
67     * @param name the OIDName in form "x.y.z..."
68     * @throws IOException on error
69     */
70    public OIDName(String name) throws IOException {
71        try {
72            oid = new ObjectIdentifier(name);
73        } catch (Exception e) {
74            throw new IOException("Unable to create OIDName: " + e);
75        }
76    }
77
78    /**
79     * Return the type of the GeneralName.
80     */
81    public int getType() {
82        return (GeneralNameInterface.NAME_OID);
83    }
84
85    /**
86     * Encode the OID name into the DerOutputStream.
87     *
88     * @param out the DER stream to encode the OIDName to.
89     * @exception IOException on encoding errors.
90     */
91    public void encode(DerOutputStream out) throws IOException {
92        out.putOID(oid);
93    }
94
95    /**
96     * Convert the name into user readable string.
97     */
98    public String toString() {
99        return ("OIDName: " + oid.toString());
100    }
101
102    /**
103     * Returns this OID name.
104     */
105    public ObjectIdentifier getOID() {
106        return oid;
107    }
108
109    /**
110     * Compares this name with another, for equality.
111     *
112     * @return true iff the names are identical
113     */
114    public boolean equals(Object obj) {
115        if (this == obj)
116            return true;
117
118        if (!(obj instanceof OIDName))
119            return false;
120
121        OIDName other = (OIDName)obj;
122
123        return oid.equals((Object)other.oid);
124    }
125
126    /**
127     * Returns the hash code value for this object.
128     *
129     * @return a hash code value for this object.
130     */
131    public int hashCode() {
132        return oid.hashCode();
133    }
134
135    /**
136     * Return type of constraint inputName places on this name:<ul>
137     *   <li>NAME_DIFF_TYPE = -1: input name is different type from name (i.e. does not constrain).
138     *   <li>NAME_MATCH = 0: input name matches name.
139     *   <li>NAME_NARROWS = 1: input name narrows name (is lower in the naming subtree)
140     *   <li>NAME_WIDENS = 2: input name widens name (is higher in the naming subtree)
141     *   <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but is same type.
142     * </ul>.  These results are used in checking NameConstraints during
143     * certification path verification.
144     *
145     * @param inputName to be checked for being constrained
146     * @returns constraint type above
147     * @throws UnsupportedOperationException if name is not exact match, but narrowing and widening are
148     *          not supported for this name type.
149     */
150    public int constrains(GeneralNameInterface inputName) throws UnsupportedOperationException {
151        int constraintType;
152        if (inputName == null)
153            constraintType = NAME_DIFF_TYPE;
154        else if (inputName.getType() != NAME_OID)
155            constraintType = NAME_DIFF_TYPE;
156        else if (this.equals((OIDName)inputName))
157            constraintType = NAME_MATCH;
158        else
159            //widens and narrows not defined in RFC2459 for OIDName (aka registeredID)
160            throw new UnsupportedOperationException("Narrowing and widening are not supported for OIDNames");
161        return constraintType;
162    }
163
164    /**
165     * Return subtree depth of this name for purposes of determining
166     * NameConstraints minimum and maximum bounds and for calculating
167     * path lengths in name subtrees.
168     *
169     * @returns distance of name from root
170     * @throws UnsupportedOperationException if not supported for this name type
171     */
172    public int subtreeDepth() throws UnsupportedOperationException {
173        throw new UnsupportedOperationException("subtreeDepth() not supported for OIDName.");
174   }
175}
176