| 1 |
769 |
jeremybenn |
/* TLSRandom.java -- The TLS pseudo-random function.
|
| 2 |
|
|
Copyright (C) 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.net.ssl.provider;
|
| 40 |
|
|
|
| 41 |
|
|
import java.security.InvalidKeyException;
|
| 42 |
|
|
import java.util.HashMap;
|
| 43 |
|
|
import java.util.Map;
|
| 44 |
|
|
|
| 45 |
|
|
import gnu.java.security.hash.HashFactory;
|
| 46 |
|
|
import gnu.javax.crypto.mac.IMac;
|
| 47 |
|
|
import gnu.java.security.prng.IRandom;
|
| 48 |
|
|
|
| 49 |
|
|
class TLSRandom implements IRandom
|
| 50 |
|
|
{
|
| 51 |
|
|
|
| 52 |
|
|
// Fields.
|
| 53 |
|
|
// -------------------------------------------------------------------------
|
| 54 |
|
|
|
| 55 |
|
|
/**
|
| 56 |
|
|
* Property name for the secret that will be used to initialize the HMACs.
|
| 57 |
|
|
*/
|
| 58 |
|
|
static final String SECRET = "jessie.tls.prng.secret";
|
| 59 |
|
|
|
| 60 |
|
|
/**
|
| 61 |
|
|
* Property name for the seed.
|
| 62 |
|
|
*/
|
| 63 |
|
|
static final String SEED = "jessie.tls.prng.seed";
|
| 64 |
|
|
|
| 65 |
|
|
private final IMac hmac_sha, hmac_md5;
|
| 66 |
|
|
private byte[] sha_a, md5_a;
|
| 67 |
|
|
private byte[] seed;
|
| 68 |
|
|
private final byte[] buffer;
|
| 69 |
|
|
private int idx;
|
| 70 |
|
|
private boolean init;
|
| 71 |
|
|
|
| 72 |
|
|
// Constructors.
|
| 73 |
|
|
// -------------------------------------------------------------------------
|
| 74 |
|
|
|
| 75 |
|
|
TLSRandom()
|
| 76 |
|
|
{
|
| 77 |
|
|
hmac_sha = new TLSHMac(HashFactory.getInstance("SHA1"));
|
| 78 |
|
|
hmac_md5 = new TLSHMac(HashFactory.getInstance("MD5"));
|
| 79 |
|
|
buffer = new byte[80]; // 80 == LCM of 16 and 20.
|
| 80 |
|
|
idx = 0;
|
| 81 |
|
|
init = false;
|
| 82 |
|
|
}
|
| 83 |
|
|
|
| 84 |
|
|
// Instance methods.
|
| 85 |
|
|
// -------------------------------------------------------------------------
|
| 86 |
|
|
|
| 87 |
|
|
public Object clone()
|
| 88 |
|
|
{
|
| 89 |
|
|
try
|
| 90 |
|
|
{
|
| 91 |
|
|
return super.clone();
|
| 92 |
|
|
}
|
| 93 |
|
|
catch (CloneNotSupportedException shouldNotHappen)
|
| 94 |
|
|
{
|
| 95 |
|
|
throw new Error();
|
| 96 |
|
|
}
|
| 97 |
|
|
}
|
| 98 |
|
|
|
| 99 |
|
|
public void init(Map attributes)
|
| 100 |
|
|
{
|
| 101 |
|
|
HashMap sha_attr = new HashMap();
|
| 102 |
|
|
HashMap md5_attr = new HashMap();
|
| 103 |
|
|
byte[] secret = (byte[]) attributes.get(SECRET);
|
| 104 |
|
|
if (secret != null)
|
| 105 |
|
|
{
|
| 106 |
|
|
int l = (secret.length >>> 1) + (secret.length & 1);
|
| 107 |
|
|
byte[] s1 = Util.trim(secret, 0, l);
|
| 108 |
|
|
byte[] s2 = Util.trim(secret, secret.length - l, l);
|
| 109 |
|
|
md5_attr.put(IMac.MAC_KEY_MATERIAL, s1);
|
| 110 |
|
|
sha_attr.put(IMac.MAC_KEY_MATERIAL, s2);
|
| 111 |
|
|
try
|
| 112 |
|
|
{
|
| 113 |
|
|
hmac_md5.init(md5_attr);
|
| 114 |
|
|
hmac_sha.init(sha_attr);
|
| 115 |
|
|
}
|
| 116 |
|
|
catch (InvalidKeyException ike)
|
| 117 |
|
|
{
|
| 118 |
|
|
throw new Error(ike.toString());
|
| 119 |
|
|
}
|
| 120 |
|
|
}
|
| 121 |
|
|
else if (!init)
|
| 122 |
|
|
{
|
| 123 |
|
|
throw new IllegalArgumentException("no secret supplied");
|
| 124 |
|
|
}
|
| 125 |
|
|
// else re-use
|
| 126 |
|
|
|
| 127 |
|
|
byte[] seeed = (byte[]) attributes.get(SEED);
|
| 128 |
|
|
if (seeed != null)
|
| 129 |
|
|
{
|
| 130 |
|
|
seed = (byte[]) seeed.clone();
|
| 131 |
|
|
}
|
| 132 |
|
|
else if (!init)
|
| 133 |
|
|
{
|
| 134 |
|
|
throw new IllegalArgumentException("no seed supplied");
|
| 135 |
|
|
}
|
| 136 |
|
|
// else re-use
|
| 137 |
|
|
|
| 138 |
|
|
// A(0) is the seed, A(1) = HMAC_hash(secret, A(0)).
|
| 139 |
|
|
hmac_md5.update(seed, 0, seed.length);
|
| 140 |
|
|
md5_a = hmac_md5.digest();
|
| 141 |
|
|
hmac_md5.reset();
|
| 142 |
|
|
hmac_sha.update(seed, 0, seed.length);
|
| 143 |
|
|
sha_a = hmac_sha.digest();
|
| 144 |
|
|
hmac_sha.reset();
|
| 145 |
|
|
fillBuffer();
|
| 146 |
|
|
init = true;
|
| 147 |
|
|
}
|
| 148 |
|
|
|
| 149 |
|
|
public String name()
|
| 150 |
|
|
{
|
| 151 |
|
|
return "TLSRandom";
|
| 152 |
|
|
}
|
| 153 |
|
|
|
| 154 |
|
|
public byte nextByte()
|
| 155 |
|
|
{
|
| 156 |
|
|
if (!init)
|
| 157 |
|
|
throw new IllegalStateException();
|
| 158 |
|
|
if (idx >= buffer.length)
|
| 159 |
|
|
fillBuffer();
|
| 160 |
|
|
return buffer[idx++];
|
| 161 |
|
|
}
|
| 162 |
|
|
|
| 163 |
|
|
public void nextBytes(byte[] buf, int off, int len)
|
| 164 |
|
|
{
|
| 165 |
|
|
if (!init)
|
| 166 |
|
|
throw new IllegalStateException();
|
| 167 |
|
|
if (buf == null)
|
| 168 |
|
|
throw new NullPointerException();
|
| 169 |
|
|
if (off < 0 || off > buf.length || off + len > buf.length)
|
| 170 |
|
|
throw new ArrayIndexOutOfBoundsException();
|
| 171 |
|
|
int count = 0;
|
| 172 |
|
|
if (idx >= buffer.length)
|
| 173 |
|
|
fillBuffer();
|
| 174 |
|
|
while (count < len)
|
| 175 |
|
|
{
|
| 176 |
|
|
int l = Math.min(buffer.length-idx, len-count);
|
| 177 |
|
|
System.arraycopy(buffer, idx, buf, off+count, l);
|
| 178 |
|
|
idx += l;
|
| 179 |
|
|
count += l;
|
| 180 |
|
|
if (count < len && idx >= buffer.length)
|
| 181 |
|
|
fillBuffer();
|
| 182 |
|
|
}
|
| 183 |
|
|
}
|
| 184 |
|
|
|
| 185 |
|
|
// For future versions of GNU Crypto. No-ops.
|
| 186 |
|
|
public void addRandomByte (byte b)
|
| 187 |
|
|
{
|
| 188 |
|
|
}
|
| 189 |
|
|
|
| 190 |
|
|
public void addRandomBytes(byte[] buffer) {
|
| 191 |
|
|
addRandomBytes(buffer, 0, buffer.length);
|
| 192 |
|
|
}
|
| 193 |
|
|
|
| 194 |
|
|
public void addRandomBytes (byte[] b, int i, int j)
|
| 195 |
|
|
{
|
| 196 |
|
|
}
|
| 197 |
|
|
|
| 198 |
|
|
// Own methods.
|
| 199 |
|
|
// -------------------------------------------------------------------------
|
| 200 |
|
|
|
| 201 |
|
|
/*
|
| 202 |
|
|
* The PRF is defined as:
|
| 203 |
|
|
*
|
| 204 |
|
|
* PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
|
| 205 |
|
|
* P_SHA-1(S2, label + seed);
|
| 206 |
|
|
*
|
| 207 |
|
|
* P_hash is defined as:
|
| 208 |
|
|
*
|
| 209 |
|
|
* P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
|
| 210 |
|
|
* HMAC_hash(secret, A(2) + seed) +
|
| 211 |
|
|
* HMAC_hash(secret, A(3) + seed) + ...
|
| 212 |
|
|
*
|
| 213 |
|
|
* And A() is defined as:
|
| 214 |
|
|
*
|
| 215 |
|
|
* A(0) = seed
|
| 216 |
|
|
* A(i) = HMAC_hash(secret, A(i-1))
|
| 217 |
|
|
*
|
| 218 |
|
|
* For simplicity, we compute an 80-byte block on each call, which
|
| 219 |
|
|
* corresponds to five iterations of MD5, and four of SHA-1.
|
| 220 |
|
|
*/
|
| 221 |
|
|
private synchronized void fillBuffer()
|
| 222 |
|
|
{
|
| 223 |
|
|
int len = hmac_md5.macSize();
|
| 224 |
|
|
for (int i = 0; i < buffer.length; i += len)
|
| 225 |
|
|
{
|
| 226 |
|
|
hmac_md5.update(md5_a, 0, md5_a.length);
|
| 227 |
|
|
hmac_md5.update(seed, 0, seed.length);
|
| 228 |
|
|
byte[] b = hmac_md5.digest();
|
| 229 |
|
|
hmac_md5.reset();
|
| 230 |
|
|
System.arraycopy(b, 0, buffer, i, len);
|
| 231 |
|
|
hmac_md5.update(md5_a, 0, md5_a.length);
|
| 232 |
|
|
md5_a = hmac_md5.digest();
|
| 233 |
|
|
hmac_md5.reset();
|
| 234 |
|
|
}
|
| 235 |
|
|
len = hmac_sha.macSize();
|
| 236 |
|
|
for (int i = 0; i < buffer.length; i += len)
|
| 237 |
|
|
{
|
| 238 |
|
|
hmac_sha.update(sha_a, 0, sha_a.length);
|
| 239 |
|
|
hmac_sha.update(seed, 0, seed.length);
|
| 240 |
|
|
byte[] b = hmac_sha.digest();
|
| 241 |
|
|
hmac_sha.reset();
|
| 242 |
|
|
for (int j = 0; j < len; j++)
|
| 243 |
|
|
{
|
| 244 |
|
|
buffer[j + i] ^= b[j];
|
| 245 |
|
|
}
|
| 246 |
|
|
hmac_sha.update(sha_a, 0, sha_a.length);
|
| 247 |
|
|
sha_a = hmac_sha.digest();
|
| 248 |
|
|
hmac_sha.reset();
|
| 249 |
|
|
}
|
| 250 |
|
|
idx = 0;
|
| 251 |
|
|
}
|
| 252 |
|
|
}
|