1. /*
  2. * @(#)Oid.java 1.7 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package org.ietf.jgss;
  8. import java.io.InputStream;
  9. import java.io.IOException;
  10. import sun.security.util.DerValue;
  11. import sun.security.util.DerOutputStream;
  12. import sun.security.util.ObjectIdentifier;
  13. /**
  14. * This class represents Universal Object Identifiers (Oids) and their
  15. * associated operations.<p>
  16. *
  17. * Oids are hierarchically globally-interpretable identifiers used
  18. * within the GSS-API framework to identify mechanisms and name formats.<p>
  19. *
  20. * The structure and encoding of Oids is defined in ISOIEC-8824 and
  21. * ISOIEC-8825. For example the Oid representation of Kerberos V5
  22. * mechanism is "1.2.840.113554.1.2.2"<p>
  23. *
  24. * The GSSName name class contains public static Oid objects
  25. * representing the standard name types defined in GSS-API.
  26. *
  27. * @author Mayank Upadhyay
  28. * @version 1.7, 01/23/03
  29. * @since 1.4
  30. */
  31. public class Oid {
  32. private ObjectIdentifier oid;
  33. private byte[] derEncoding;
  34. /**
  35. * Constructs an Oid object from a string representation of its
  36. * integer components.
  37. *
  38. * @param strOid the dot separated string representation of the oid.
  39. * For instance, "1.2.840.113554.1.2.2".
  40. * @exception GSSException may be thrown when the string is incorrectly
  41. * formatted
  42. */
  43. public Oid(String strOid) throws GSSException {
  44. try {
  45. oid = new ObjectIdentifier(strOid);
  46. derEncoding = null;
  47. } catch (Exception e) {
  48. throw new GSSException(GSSException.FAILURE,
  49. "Improperly formatted Object Identifier String - "
  50. + strOid);
  51. }
  52. }
  53. /**
  54. * Creates an Oid object from its ASN.1 DER encoding. This refers to
  55. * the full encoding including tag and length. The structure and
  56. * encoding of Oids is defined in ISOIEC-8824 and ISOIEC-8825. This
  57. * method is identical in functionality to its byte array counterpart.
  58. *
  59. * @param derOid stream containing the DER encoded oid
  60. * @exception GSSException may be thrown when the DER encoding does not
  61. * follow the prescribed format.
  62. */
  63. public Oid(InputStream derOid) throws GSSException {
  64. try {
  65. DerValue derVal = new DerValue(derOid);
  66. derEncoding = derVal.toByteArray();
  67. oid = derVal.getOID();
  68. } catch (IOException e) {
  69. throw new GSSException(GSSException.FAILURE,
  70. "Improperly formatted ASN.1 DER encoding for Oid");
  71. }
  72. }
  73. /**
  74. * Creates an Oid object from its ASN.1 DER encoding. This refers to
  75. * the full encoding including tag and length. The structure and
  76. * encoding of Oids is defined in ISOIEC-8824 and ISOIEC-8825. This
  77. * method is identical in functionality to its InputStream conterpart.
  78. *
  79. * @param data byte array containing the DER encoded oid
  80. * @exception GSSException may be thrown when the DER encoding does not
  81. * follow the prescribed format.
  82. */
  83. public Oid(byte [] data) throws GSSException {
  84. try {
  85. DerValue derVal = new DerValue(data);
  86. derEncoding = derVal.toByteArray();
  87. oid = derVal.getOID();
  88. } catch (IOException e) {
  89. throw new GSSException(GSSException.FAILURE,
  90. "Improperly formatted ASN.1 DER encoding for Oid");
  91. }
  92. }
  93. /**
  94. * Only for calling by initializators used with declarations.
  95. *
  96. * @param strOid
  97. */
  98. static Oid getInstance(String strOid) {
  99. Oid retVal = null;
  100. try {
  101. retVal = new Oid(strOid);
  102. } catch (GSSException e) {
  103. // squelch it!
  104. }
  105. return retVal;
  106. }
  107. /**
  108. * Returns a string representation of the oid's integer components
  109. * in dot separated notation.
  110. *
  111. * @return string representation in the following format: "1.2.3.4.5"
  112. */
  113. public String toString() {
  114. return oid.toString();
  115. }
  116. /**
  117. * Tests if two Oid objects represent the same Object identifier
  118. * value.
  119. *
  120. * @return <code>true</code> if the two Oid objects represent the same
  121. * value, <code>false</code> otherwise.
  122. * @param other the Oid object that has to be compared to this one
  123. */
  124. public boolean equals(Object other) {
  125. //check if both reference the same object
  126. if (this == other)
  127. return (true);
  128. if (other instanceof Oid)
  129. return this.oid.equals(((Oid) other).oid);
  130. else if (other instanceof ObjectIdentifier)
  131. return this.oid.equals(other);
  132. else
  133. return false;
  134. }
  135. /**
  136. * Returns the full ASN.1 DER encoding for this oid object, which
  137. * includes the tag and length.
  138. *
  139. * @return byte array containing the DER encoding of this oid object.
  140. * @exception GSSException may be thrown when the oid can't be encoded
  141. */
  142. public byte[] getDER() throws GSSException {
  143. if (derEncoding == null) {
  144. DerOutputStream dout = new DerOutputStream();
  145. try {
  146. dout.putOID(oid);
  147. } catch (IOException e) {
  148. throw new GSSException(GSSException.FAILURE, e.getMessage());
  149. }
  150. derEncoding = dout.toByteArray();
  151. }
  152. return derEncoding;
  153. }
  154. /**
  155. * A utility method to test if this Oid value is contained within the
  156. * supplied Oid array.
  157. *
  158. * @param oids the array of Oid's to search
  159. * @return true if the array contains this Oid value, false otherwise
  160. */
  161. public boolean containedIn(Oid[] oids) {
  162. for (int i = 0; i < oids.length; i++) {
  163. if (oids[i].equals(this))
  164. return (true);
  165. }
  166. return (false);
  167. }
  168. /**
  169. * Returns a hashcode value for this Oid.
  170. *
  171. * @return a hashCode value
  172. */
  173. public int hashCode() {
  174. return oid.hashCode();
  175. }
  176. }