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