Password.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* Password.java -- opaque wrapper around a password.
  2. Copyright (C) 2004, 2006 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.security.auth;
  32. import gnu.java.security.util.ExpirableObject;
  33. /**
  34. * Immutible, though destroyable, password class.
  35. *
  36. * <p>Extends {@link ExpirableObject}, implementing {@link doDestroy()}
  37. * in which encapsulated {@link char[]}, and {@link byte[]} password fields
  38. * are cleared (elements set to zero) in order to thwart memory heap
  39. * snooping.
  40. */
  41. public final class Password extends ExpirableObject
  42. {
  43. // Constants and variables
  44. // -------------------------------------------------------------------------
  45. /**
  46. * Password stored in {@link char[]} format.
  47. */
  48. private final char[] password;
  49. /**
  50. * Password stored in {@link byte[]} format.
  51. */
  52. private final byte[] bPassword;
  53. /**
  54. * Indicates whether this Password object's {@link doDestroy()} method has
  55. * been called. See also, {@link ExpirableObject#Destroy()}.
  56. */
  57. private boolean mIsDestroyed = false;
  58. // Constructor(s)
  59. // -------------------------------------------------------------------------
  60. /**
  61. * Create a new expirable Password object that will expire after the
  62. * default timeout {@link ExpirableObject#DEFAULT_TIMEOUT}.
  63. *
  64. * @param password The character array password to associate with this
  65. * Password object.
  66. */
  67. public Password (char[] password)
  68. {
  69. this (password, 0, password.length, DEFAULT_TIMEOUT);
  70. }
  71. /**
  72. * Create a new expirable Password object that will expire after the
  73. * timeout denoted by constructor parameter, <i>delay</i>.
  74. *
  75. * @param password The character array password to associate with this
  76. * Password object.
  77. * @param delay The number of miliseconds before this Password object
  78. * will be automatically destroyed.
  79. */
  80. public Password (char[] password, long delay)
  81. {
  82. this (password, 0, password.length, delay);
  83. }
  84. /**
  85. * Create a new expirable Password object that will expire after the
  86. * default timeout {@link ExpirableObject#DEFAULT_TIMEOUT}.
  87. *
  88. * @param password The character array password to associate with this
  89. * Password object.
  90. * @param offset The <i>password</i> character array parameter element
  91. * marking the beginning of the contained password string.
  92. * @param length The number of characters, beginning at <i>offset</i>,
  93. * to be copied into this object's {@link password} field.
  94. */
  95. public Password (char[] password, int offset, int length)
  96. {
  97. this (password, offset, length, DEFAULT_TIMEOUT);
  98. }
  99. /**
  100. * Create a new expirable Password object that will expire after the
  101. * timeout denoted by constructor parameter, <i>delay</i>.
  102. *
  103. * @param password The character array password to associate with this
  104. * Password object.
  105. * @param offset The <i>password</i> character array parameter element
  106. * marking the beginning of the contained password string.
  107. * @param length The number of characters, beginning at <i>offset</i>,
  108. * to be copied into this object's {@link password} field.
  109. * @param delay The number of miliseconds before this Password object
  110. * will be automatically destroyed.
  111. */
  112. public Password (char[] password, int offset, int length, long delay)
  113. {
  114. super (delay);
  115. if (offset < 0 || length < 0 || offset + length > password.length)
  116. throw new ArrayIndexOutOfBoundsException ("off=" + offset + " length=" +
  117. length + " array.length=" +
  118. password.length);
  119. int i, j;
  120. this.password = new char[length];
  121. bPassword = new byte[length];
  122. for(i = 0, j = offset; i < length; i++, j++)
  123. {
  124. this.password[i] = password[j];
  125. // XXX this should use character encodings, other than ASCII.
  126. bPassword[i] = (byte) (password[j] & 0x7F);
  127. }
  128. }
  129. /**
  130. * Create a new expirable Password object that will expire after the
  131. * default timeout {@link ExpirableObject#DEFAULT_TIMEOUT}.
  132. *
  133. * @param password The byte array password to associate with this
  134. * Password object.
  135. */
  136. public Password (byte[] password)
  137. {
  138. this (password, 0, password.length, DEFAULT_TIMEOUT);
  139. }
  140. /**
  141. * Create a new expirable Password object that will expire after the
  142. * timeout denoted by constructor parameter, <i>delay</i>.
  143. *
  144. * @param password The byte array password to associate with this
  145. * Password object.
  146. * @param delay The number of miliseconds before this Password object
  147. * will be automatically destroyed.
  148. */
  149. public Password (byte[] password, long delay)
  150. {
  151. this (password, 0, password.length, delay);
  152. }
  153. /**
  154. * Create a new expirable Password object that will expire after the
  155. * default timeout {@link ExpirableObject#DEFAULT_TIMEOUT}.
  156. *
  157. * @param password The byte array password to associate with this
  158. * Password object.
  159. * @param offset The <i>password</i> byte array parameter element
  160. * marking the beginning of the contained password string.
  161. * @param length The number of bytes, beginning at <i>offset</i>,
  162. * to be copied into this object's {@link password} field.
  163. */
  164. public Password (byte[] password, int offset, int length)
  165. {
  166. this (password, offset, length, DEFAULT_TIMEOUT);
  167. }
  168. /**
  169. * Create a new expirable Password object that will expire after the
  170. * timeout denoted by constructor parameter, <i>delay</i>.
  171. *
  172. * @param password The byte array password to associate with this
  173. * Password object.
  174. * @param offset The <i>password</i> byte array parameter element
  175. * marking the beginning of the contained password string.
  176. * @param length The number of bytes, beginning at <i>offset</i>,
  177. * to be copied into this object's {@link bPassword} field.
  178. * @param delay The number of miliseconds before this Password object
  179. * will be automatically destroyed.
  180. */
  181. public Password (byte[] password, int offset, int length, long delay)
  182. {
  183. super (delay);
  184. if (offset < 0 || length < 0 || offset + length > password.length)
  185. throw new ArrayIndexOutOfBoundsException ("off=" + offset + " length=" +
  186. length + " array.length=" +
  187. password.length);
  188. int i, j;
  189. this.password = new char[length];
  190. bPassword = new byte[length];
  191. for (i = 0, j = offset; i < length; i++, j++)
  192. {
  193. this.password[i] = (char) password[j];
  194. bPassword[i] = password[j];
  195. }
  196. }
  197. // Instance methods
  198. // -------------------------------------------------------------------------
  199. /**
  200. * Returns a reference to the {@link char[]} password storage field,
  201. * {@link password}.
  202. */
  203. public synchronized char[] getPassword()
  204. {
  205. if (mIsDestroyed)
  206. throw new IllegalStateException ("Attempted destroyed password access.");
  207. return password;
  208. }
  209. /**
  210. * Returns a reference to the {@link byte[]} password storage field,
  211. * {@link bPassword}.
  212. */
  213. public synchronized byte[] getBytes()
  214. {
  215. if (mIsDestroyed)
  216. throw new IllegalStateException ("Attempted destroyed password access.");
  217. return bPassword;
  218. }
  219. /**
  220. * Sets password field char[], and byte[] array elements to zero.
  221. * This method implements base class {@link ExpirableObject} abstract
  222. * method, {@link ExpirableObject#doDestroy()}. See also,
  223. * {@link ExpirableObject#destroy()}.
  224. */
  225. protected synchronized void doDestroy()
  226. {
  227. if (isDestroyed())
  228. return;
  229. else
  230. {
  231. for (int i = 0; i < password.length; i++)
  232. password[i] = 0;
  233. for (int i = 0; i < bPassword.length; i++)
  234. bPassword[i] = 0;
  235. mIsDestroyed = true;
  236. }
  237. }
  238. /**
  239. * Returns true, or false relative to whether, or not this object's
  240. * {@link doDestroy()} method has been called. See also,
  241. * {@ExpirableObject#destroy()}.
  242. */
  243. public synchronized boolean isDestroyed()
  244. {
  245. return (mIsDestroyed);
  246. }
  247. }