X500Principal.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package javax.security.auth.x500;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.ObjectInputStream;
  21. import java.io.ObjectOutputStream;
  22. import java.io.Serializable;
  23. import java.security.Principal;
  24. import org.apache.harmony.auth.internal.nls.Messages;
  25. import org.apache.harmony.security.x501.Name;
  26. /**
  27. * Represents an X.500 principal, which holds the distinguished name of some
  28. * network entity. An example of a distinguished name is {@code "O=SomeOrg,
  29. * OU=SomeOrgUnit, C=US"}. The class can be instantiated from a byte representation
  30. * of an object identifier (OID), an ASN.1 DER-encoded version, or a simple
  31. * string holding the distinguished name. The representations must follow either
  32. * RFC 2253, RFC 1779, or RFC2459.
  33. */
  34. public final class X500Principal implements Serializable, Principal {
  35. private static final long serialVersionUID = -500463348111345721L;
  36. /**
  37. * Defines a constant for the canonical string format of distinguished
  38. * names.
  39. */
  40. public static final String CANONICAL = "CANONICAL"; //$NON-NLS-1$
  41. /**
  42. * Defines a constant for the RFC 1779 string format of distinguished
  43. * names.
  44. */
  45. public static final String RFC1779 = "RFC1779"; //$NON-NLS-1$
  46. /**
  47. * Defines a constant for the RFC 2253 string format of distinguished
  48. * names.
  49. */
  50. public static final String RFC2253 = "RFC2253"; //$NON-NLS-1$
  51. //Distinguished Name
  52. private transient Name dn;
  53. /**
  54. * Creates a new X500Principal from a given ASN.1 DER encoding of a
  55. * distinguished name.
  56. *
  57. * @param name
  58. * the ASN.1 DER-encoded distinguished name
  59. *
  60. * @throws IllegalArgumentException
  61. * if the ASN.1 DER-encoded distinguished name is incorrect
  62. */
  63. public X500Principal(byte[] name) {
  64. super();
  65. if (name == null) {
  66. throw new IllegalArgumentException(Messages.getString("auth.00")); //$NON-NLS-1$
  67. }
  68. try {
  69. // FIXME dn = new Name(name);
  70. dn = (Name) Name.ASN1.decode(name);
  71. } catch (IOException e) {
  72. IllegalArgumentException iae = new IllegalArgumentException(Messages
  73. .getString("auth.2B")); //$NON-NLS-1$
  74. iae.initCause(e);
  75. throw iae;
  76. }
  77. }
  78. /**
  79. * Creates a new X500Principal from a given ASN.1 DER encoding of a
  80. * distinguished name.
  81. *
  82. * @param in
  83. * an {@code InputStream} holding the ASN.1 DER-encoded
  84. * distinguished name
  85. *
  86. * @throws IllegalArgumentException
  87. * if the ASN.1 DER-encoded distinguished name is incorrect
  88. */
  89. public X500Principal(InputStream in) {
  90. super();
  91. if (in == null) {
  92. throw new NullPointerException(Messages.getString("auth.2C")); //$NON-NLS-1$
  93. }
  94. try {
  95. // FIXME dn = new Name(is);
  96. dn = (Name) Name.ASN1.decode(in);
  97. } catch (IOException e) {
  98. IllegalArgumentException iae = new IllegalArgumentException(Messages
  99. .getString("auth.2B")); //$NON-NLS-1$
  100. iae.initCause(e);
  101. throw iae;
  102. }
  103. }
  104. /**
  105. * Creates a new X500Principal from a string representation of a
  106. * distinguished name.
  107. *
  108. * @param name
  109. * the string representation of the distinguished name
  110. *
  111. * @throws IllegalArgumentException
  112. * if the string representation of the distinguished name is
  113. * incorrect
  114. */
  115. public X500Principal(String name) {
  116. super();
  117. if (name == null) {
  118. throw new NullPointerException(Messages.getString("auth.00")); //$NON-NLS-1$
  119. }
  120. try {
  121. dn = new Name(name);
  122. } catch (IOException e) {
  123. IllegalArgumentException iae = new IllegalArgumentException(Messages
  124. .getString("auth.2D")); //$NON-NLS-1$
  125. iae.initCause(e);
  126. throw iae;
  127. }
  128. }
  129. @Override
  130. public boolean equals(Object o) {
  131. if (this == o) {
  132. return true;
  133. }
  134. if (o == null || this.getClass() != o.getClass()) {
  135. return false;
  136. }
  137. X500Principal principal = (X500Principal) o;
  138. return dn.getName(CANONICAL).equals(principal.dn.getName(CANONICAL));
  139. }
  140. /**
  141. * Returns an ASN.1 DER-encoded representation of the distinguished name
  142. * contained in this X.500 principal.
  143. *
  144. * @return the ASN.1 DER-encoded representation
  145. */
  146. public byte[] getEncoded() {
  147. byte[] src = dn.getEncoded();
  148. byte[] dst = new byte[src.length];
  149. System.arraycopy(src, 0, dst, 0, dst.length);
  150. return dst;
  151. }
  152. /**
  153. * Returns a human-readable string representation of the distinguished name
  154. * contained in this X.500 principal.
  155. *
  156. * @return the string representation
  157. */
  158. public String getName() {
  159. return dn.getName(RFC2253);
  160. }
  161. /**
  162. * Returns a string representation of the distinguished name contained in
  163. * this X.500 principal. The format of the representation can be chosen.
  164. * Valid arguments are {@link #RFC1779}, {@link #RFC2253}, and
  165. * {@link #CANONICAL}. The representations are specified in RFC 1779 and RFC
  166. * 2253, respectively. The canonical form is based on RFC 2253, but adds
  167. * some canonicalizing operations like removing leading and trailing
  168. * whitespace, lower-casing the whole name, and bringing it into a
  169. * normalized Unicode representation.
  170. *
  171. * @param format
  172. * the name of the format to use for the representation
  173. *
  174. * @return the string representation
  175. *
  176. * @throws IllegalArgumentException
  177. * if the {@code format} argument is not one of the three
  178. * mentioned above
  179. */
  180. public String getName(String format) {
  181. return dn.getName(format);
  182. }
  183. @Override
  184. public int hashCode() {
  185. return dn.getName(CANONICAL).hashCode();
  186. }
  187. @Override
  188. public String toString() {
  189. return dn.getName(RFC1779);
  190. }
  191. private void writeObject(ObjectOutputStream out) throws IOException {
  192. out.writeObject(dn.getEncoded());
  193. }
  194. private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
  195. dn = (Name) Name.ASN1.decode((byte[]) in.readObject());
  196. }
  197. }