1 |
769 |
jeremybenn |
/* SRP.java --
|
2 |
|
|
Copyright (C) 2003, 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.crypto.sasl.srp;
|
40 |
|
|
|
41 |
|
|
import gnu.java.security.hash.HashFactory;
|
42 |
|
|
import gnu.java.security.hash.IMessageDigest;
|
43 |
|
|
import gnu.java.security.util.Util;
|
44 |
|
|
|
45 |
|
|
import java.io.UnsupportedEncodingException;
|
46 |
|
|
import java.math.BigInteger;
|
47 |
|
|
import java.util.HashMap;
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* A Factory class that returns SRP Singletons that know all SRP-related
|
51 |
|
|
* mathematical computations and protocol-related operations for both the
|
52 |
|
|
* client- and server-sides.
|
53 |
|
|
*/
|
54 |
|
|
public final class SRP
|
55 |
|
|
{
|
56 |
|
|
/** The map of already instantiated SRP algorithm instances. */
|
57 |
|
|
private static final HashMap algorithms = new HashMap();
|
58 |
|
|
private static final byte COLON = (byte) 0x3A;
|
59 |
|
|
/** The underlying message digest algorithm used for all SRP calculations. */
|
60 |
|
|
private IMessageDigest mda;
|
61 |
|
|
|
62 |
|
|
/** Trivial private constructor to enforce Singleton pattern. */
|
63 |
|
|
private SRP(final IMessageDigest mda)
|
64 |
|
|
{
|
65 |
|
|
super();
|
66 |
|
|
|
67 |
|
|
this.mda = mda;
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
/**
|
71 |
|
|
* Returns an instance of this object that uses the designated message digest
|
72 |
|
|
* algorithm as its digest function.
|
73 |
|
|
*
|
74 |
|
|
* @return an instance of this object for the designated digest name.
|
75 |
|
|
*/
|
76 |
|
|
public static synchronized SRP instance(String mdName)
|
77 |
|
|
{
|
78 |
|
|
if (mdName != null)
|
79 |
|
|
mdName = mdName.trim().toLowerCase();
|
80 |
|
|
if (mdName == null || mdName.equals(""))
|
81 |
|
|
mdName = SRPRegistry.SRP_DEFAULT_DIGEST_NAME;
|
82 |
|
|
SRP result = (SRP) algorithms.get(mdName);
|
83 |
|
|
if (result == null)
|
84 |
|
|
{
|
85 |
|
|
final IMessageDigest mda = HashFactory.getInstance(mdName);
|
86 |
|
|
result = new SRP(mda);
|
87 |
|
|
algorithms.put(mdName, result);
|
88 |
|
|
}
|
89 |
|
|
return result;
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
private static final byte[] xor(final byte[] b1, final byte[] b2,
|
93 |
|
|
final int length)
|
94 |
|
|
{
|
95 |
|
|
final byte[] result = new byte[length];
|
96 |
|
|
for (int i = 0; i < length; ++i)
|
97 |
|
|
result[i] = (byte)(b1[i] ^ b2[i]);
|
98 |
|
|
return result;
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
/** @return the message digest algorithm name used by this instance. */
|
102 |
|
|
public String getAlgorithm()
|
103 |
|
|
{
|
104 |
|
|
return mda.name();
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
/**
|
108 |
|
|
* Returns a new instance of the SRP message digest algorithm --which is
|
109 |
|
|
* SHA-160 by default, but could be anything else provided the proper
|
110 |
|
|
* conditions as specified in the SRP specifications.
|
111 |
|
|
*
|
112 |
|
|
* @return a new instance of the underlying SRP message digest algorithm.
|
113 |
|
|
* @throws RuntimeException if the implementation of the message digest
|
114 |
|
|
* algorithm does not support cloning.
|
115 |
|
|
*/
|
116 |
|
|
public IMessageDigest newDigest()
|
117 |
|
|
{
|
118 |
|
|
return (IMessageDigest) mda.clone();
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
/**
|
122 |
|
|
* Convenience method to return the result of digesting the designated input
|
123 |
|
|
* with a new instance of the SRP message digest algorithm.
|
124 |
|
|
*
|
125 |
|
|
* @param src some bytes to digest.
|
126 |
|
|
* @return the bytes constituting the result of digesting the designated input
|
127 |
|
|
* with a new instance of the SRP message digest algorithm.
|
128 |
|
|
*/
|
129 |
|
|
public byte[] digest(final byte[] src)
|
130 |
|
|
{
|
131 |
|
|
final IMessageDigest hash = (IMessageDigest) mda.clone();
|
132 |
|
|
hash.update(src, 0, src.length);
|
133 |
|
|
return hash.digest();
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
/**
|
137 |
|
|
* Convenience method to return the result of digesting the designated input
|
138 |
|
|
* with a new instance of the SRP message digest algorithm.
|
139 |
|
|
*
|
140 |
|
|
* @param src a String whose bytes (using US-ASCII encoding) are to be
|
141 |
|
|
* digested.
|
142 |
|
|
* @return the bytes constituting the result of digesting the designated input
|
143 |
|
|
* with a new instance of the SRP message digest algorithm.
|
144 |
|
|
* @throws UnsupportedEncodingException if US-ASCII charset is not found.
|
145 |
|
|
*/
|
146 |
|
|
public byte[] digest(final String src) throws UnsupportedEncodingException
|
147 |
|
|
{
|
148 |
|
|
return digest(src.getBytes("US-ASCII"));
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
/**
|
152 |
|
|
* Convenience method to XOR N bytes from two arrays; N being the output size
|
153 |
|
|
* of the SRP message digest algorithm.
|
154 |
|
|
*
|
155 |
|
|
* @param a the first byte array.
|
156 |
|
|
* @param b the second one.
|
157 |
|
|
* @return N bytes which are the result of the XOR operations on the first N
|
158 |
|
|
* bytes from the designated arrays. N is the size of the SRP message
|
159 |
|
|
* digest algorithm; eg. 20 for SHA-160.
|
160 |
|
|
*/
|
161 |
|
|
public byte[] xor(final byte[] a, final byte[] b)
|
162 |
|
|
{
|
163 |
|
|
return xor(a, b, mda.hashSize());
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
public byte[] generateM1(final BigInteger N, final BigInteger g,
|
167 |
|
|
final String U, final byte[] s, final BigInteger A,
|
168 |
|
|
final BigInteger B, final byte[] K, final String I,
|
169 |
|
|
final String L, final byte[] cn, final byte[] cCB)
|
170 |
|
|
throws UnsupportedEncodingException
|
171 |
|
|
{
|
172 |
|
|
final IMessageDigest hash = (IMessageDigest) mda.clone();
|
173 |
|
|
byte[] b;
|
174 |
|
|
b = xor(digest(Util.trim(N)), digest(Util.trim(g)));
|
175 |
|
|
hash.update(b, 0, b.length);
|
176 |
|
|
b = digest(U);
|
177 |
|
|
hash.update(b, 0, b.length);
|
178 |
|
|
hash.update(s, 0, s.length);
|
179 |
|
|
b = Util.trim(A);
|
180 |
|
|
hash.update(b, 0, b.length);
|
181 |
|
|
b = Util.trim(B);
|
182 |
|
|
hash.update(b, 0, b.length);
|
183 |
|
|
hash.update(K, 0, K.length);
|
184 |
|
|
b = digest(I);
|
185 |
|
|
hash.update(b, 0, b.length);
|
186 |
|
|
b = digest(L);
|
187 |
|
|
hash.update(b, 0, b.length);
|
188 |
|
|
hash.update(cn, 0, cn.length);
|
189 |
|
|
hash.update(cCB, 0, cCB.length);
|
190 |
|
|
return hash.digest();
|
191 |
|
|
}
|
192 |
|
|
|
193 |
|
|
public byte[] generateM2(final BigInteger A, final byte[] M1, final byte[] K,
|
194 |
|
|
final String U, final String I, final String o,
|
195 |
|
|
final byte[] sid, final int ttl, final byte[] cIV,
|
196 |
|
|
final byte[] sIV, final byte[] sCB)
|
197 |
|
|
throws UnsupportedEncodingException
|
198 |
|
|
{
|
199 |
|
|
final IMessageDigest hash = (IMessageDigest) mda.clone();
|
200 |
|
|
byte[] b;
|
201 |
|
|
b = Util.trim(A);
|
202 |
|
|
hash.update(b, 0, b.length);
|
203 |
|
|
hash.update(M1, 0, M1.length);
|
204 |
|
|
hash.update(K, 0, K.length);
|
205 |
|
|
b = digest(U);
|
206 |
|
|
hash.update(b, 0, b.length);
|
207 |
|
|
b = digest(I);
|
208 |
|
|
hash.update(b, 0, b.length);
|
209 |
|
|
b = digest(o);
|
210 |
|
|
hash.update(b, 0, b.length);
|
211 |
|
|
hash.update(sid, 0, sid.length);
|
212 |
|
|
hash.update((byte)(ttl >>> 24));
|
213 |
|
|
hash.update((byte)(ttl >>> 16));
|
214 |
|
|
hash.update((byte)(ttl >>> 8));
|
215 |
|
|
hash.update((byte) ttl);
|
216 |
|
|
hash.update(cIV, 0, cIV.length);
|
217 |
|
|
hash.update(sIV, 0, sIV.length);
|
218 |
|
|
hash.update(sCB, 0, sCB.length);
|
219 |
|
|
return hash.digest();
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
public byte[] generateKn(final byte[] K, final byte[] cn, final byte[] sn)
|
223 |
|
|
{
|
224 |
|
|
final IMessageDigest hash = (IMessageDigest) mda.clone();
|
225 |
|
|
hash.update(K, 0, K.length);
|
226 |
|
|
hash.update(cn, 0, cn.length);
|
227 |
|
|
hash.update(sn, 0, sn.length);
|
228 |
|
|
return hash.digest();
|
229 |
|
|
}
|
230 |
|
|
|
231 |
|
|
public byte[] computeX(final byte[] s, final String user,
|
232 |
|
|
final String password)
|
233 |
|
|
throws UnsupportedEncodingException
|
234 |
|
|
{
|
235 |
|
|
return computeX(s, user.getBytes("US-ASCII"), password.getBytes("US-ASCII"));
|
236 |
|
|
}
|
237 |
|
|
|
238 |
|
|
public byte[] computeX(final byte[] s, final String user, final byte[] p)
|
239 |
|
|
throws UnsupportedEncodingException
|
240 |
|
|
{
|
241 |
|
|
return computeX(s, user.getBytes("US-ASCII"), p);
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
private byte[] computeX(final byte[] s, final byte[] user, final byte[] p)
|
245 |
|
|
{
|
246 |
|
|
final IMessageDigest hash = (IMessageDigest) mda.clone();
|
247 |
|
|
hash.update(user, 0, user.length);
|
248 |
|
|
hash.update(COLON);
|
249 |
|
|
hash.update(p, 0, p.length);
|
250 |
|
|
final byte[] up = hash.digest();
|
251 |
|
|
hash.update(s, 0, s.length);
|
252 |
|
|
hash.update(up, 0, up.length);
|
253 |
|
|
return hash.digest();
|
254 |
|
|
}
|
255 |
|
|
}
|