2004-11-10 jrandom

* Allow loading the (mini)streaming connection options from the
      environment.
    * More defensive programming in the DSA implementation.
This commit is contained in:
jrandom
2004-11-10 12:33:01 +00:00
committed by zzz
parent ffc405138d
commit 881524a5e4
4 changed files with 88 additions and 37 deletions

View File

@ -1,5 +1,6 @@
package net.i2p.client.streaming; package net.i2p.client.streaming;
import java.util.Iterator;
import java.util.Properties; import java.util.Properties;
/** /**
@ -14,15 +15,19 @@ public class I2PSocketOptions {
public static final int DEFAULT_BUFFER_SIZE = 1024*64; public static final int DEFAULT_BUFFER_SIZE = 1024*64;
public static final int DEFAULT_WRITE_TIMEOUT = 60*1000; public static final int DEFAULT_WRITE_TIMEOUT = 60*1000;
public static final int DEFAULT_CONNECT_TIMEOUT = 60*1000;
public static final String PROP_BUFFER_SIZE = "i2p.streaming.bufferSize";
public static final String PROP_CONNECT_TIMEOUT = "i2p.streaming.connectTimeout";
public static final String PROP_READ_TIMEOUT = "i2p.streaming.readTimeout";
public static final String PROP_WRITE_TIMEOUT = "i2p.streaming.writeTimeout";
public I2PSocketOptions() { public I2PSocketOptions() {
_connectTimeout = -1; this(System.getProperties());
_readTimeout = -1;
_writeTimeout = DEFAULT_WRITE_TIMEOUT;
_maxBufferSize = DEFAULT_BUFFER_SIZE;
} }
public I2PSocketOptions(I2PSocketOptions opts) { public I2PSocketOptions(I2PSocketOptions opts) {
this(System.getProperties());
_connectTimeout = opts.getConnectTimeout(); _connectTimeout = opts.getConnectTimeout();
_readTimeout = opts.getReadTimeout(); _readTimeout = opts.getReadTimeout();
_writeTimeout = opts.getWriteTimeout(); _writeTimeout = opts.getWriteTimeout();
@ -30,7 +35,44 @@ public class I2PSocketOptions {
} }
public I2PSocketOptions(Properties opts) { public I2PSocketOptions(Properties opts) {
init(opts);
}
protected void init(Properties opts) {
_maxBufferSize = getInt(opts, PROP_BUFFER_SIZE, DEFAULT_BUFFER_SIZE);
_connectTimeout = getInt(opts, PROP_CONNECT_TIMEOUT, DEFAULT_CONNECT_TIMEOUT);
_readTimeout = getInt(opts, PROP_READ_TIMEOUT, -1);
_writeTimeout = getInt(opts, PROP_WRITE_TIMEOUT, DEFAULT_WRITE_TIMEOUT);
}
/*
protected Properties getEnvProps() {
Properties rv = new Properties();
for (Iterator iter = System.getProperties().keySet().iterator(); iter.hasNext(); ) {
String name = (String)iter.next();
rv.setProperty(name, System.getProperty(name));
}
return rv;
}
public static void main(String args[]) {
System.out.println("System props: " + System.getProperties());
System.out.println("Env props: " + new I2PSocketOptions().getEnvProps());
}
*/
protected int getInt(Properties opts, String name, int defaultVal) {
if (opts == null) return defaultVal;
String val = opts.getProperty(name);
if (val == null) {
return defaultVal;
} else {
try {
return Integer.parseInt(val);
} catch (NumberFormatException nfe) {
return defaultVal;
}
}
} }
/** /**

View File

@ -57,37 +57,41 @@ public class DSAEngine {
public boolean verifySignature(Signature signature, byte signedData[], int offset, int size, SigningPublicKey verifyingKey) { public boolean verifySignature(Signature signature, byte signedData[], int offset, int size, SigningPublicKey verifyingKey) {
long start = _context.clock().now(); long start = _context.clock().now();
byte[] sigbytes = signature.getData(); try {
byte rbytes[] = new byte[20]; byte[] sigbytes = signature.getData();
byte sbytes[] = new byte[20]; byte rbytes[] = new byte[20];
for (int x = 0; x < 40; x++) { byte sbytes[] = new byte[20];
if (x < 20) { for (int x = 0; x < 40; x++) {
rbytes[x] = sigbytes[x]; if (x < 20) {
} else { rbytes[x] = sigbytes[x];
sbytes[x - 20] = sigbytes[x]; } else {
sbytes[x - 20] = sigbytes[x];
}
} }
BigInteger s = new NativeBigInteger(1, sbytes);
BigInteger r = new NativeBigInteger(1, rbytes);
BigInteger y = new NativeBigInteger(1, verifyingKey.getData());
BigInteger w = s.modInverse(CryptoConstants.dsaq);
byte data[] = calculateHash(signedData, offset, size).getData();
NativeBigInteger bi = new NativeBigInteger(1, data);
BigInteger u1 = bi.multiply(w).mod(CryptoConstants.dsaq);
BigInteger u2 = r.multiply(w).mod(CryptoConstants.dsaq);
BigInteger modval = CryptoConstants.dsag.modPow(u1, CryptoConstants.dsap);
BigInteger modmulval = modval.multiply(y.modPow(u2,CryptoConstants.dsap));
BigInteger v = (modmulval).mod(CryptoConstants.dsap).mod(CryptoConstants.dsaq);
boolean ok = v.compareTo(r) == 0;
long diff = _context.clock().now() - start;
if (diff > 1000) {
if (_log.shouldLog(Log.WARN))
_log.warn("Took too long to verify the signature (" + diff + "ms)");
}
return ok;
} catch (Exception e) {
_log.log(Log.CRIT, "Error verifying the signature", e);
return false;
} }
BigInteger s = new NativeBigInteger(1, sbytes);
BigInteger r = new NativeBigInteger(1, rbytes);
BigInteger y = new NativeBigInteger(1, verifyingKey.getData());
BigInteger w = s.modInverse(CryptoConstants.dsaq);
byte data[] = calculateHash(signedData, offset, size).getData();
NativeBigInteger bi = new NativeBigInteger(1, data);
BigInteger u1 = bi.multiply(w).mod(CryptoConstants.dsaq);
BigInteger u2 = r.multiply(w).mod(CryptoConstants.dsaq);
BigInteger modval = CryptoConstants.dsag.modPow(u1, CryptoConstants.dsap);
BigInteger modmulval = modval.multiply(y.modPow(u2,CryptoConstants.dsap));
BigInteger v = (modmulval).mod(CryptoConstants.dsap).mod(CryptoConstants.dsaq);
boolean ok = v.compareTo(r) == 0;
long diff = _context.clock().now() - start;
if (diff > 1000) {
if (_log.shouldLog(Log.WARN))
_log.warn("Took too long to verify the signature (" + diff + "ms)");
}
return ok;
} }
public Signature sign(byte data[], SigningPrivateKey signingKey) { public Signature sign(byte data[], SigningPrivateKey signingKey) {

View File

@ -1,4 +1,9 @@
$Id: history.txt,v 1.67 2004/11/07 22:18:01 jrandom Exp $ $Id: history.txt,v 1.68 2004/11/08 00:40:21 jrandom Exp $
2004-11-10 jrandom
* Allow loading the (mini)streaming connection options from the
environment.
* More defensive programming in the DSA implementation.
2004-11-08 jrandom 2004-11-08 jrandom
* Remove spurious flush calls from I2PTunnel, and work with the * Remove spurious flush calls from I2PTunnel, and work with the

View File

@ -15,9 +15,9 @@ import net.i2p.CoreVersion;
* *
*/ */
public class RouterVersion { public class RouterVersion {
public final static String ID = "$Revision: 1.72 $ $Date: 2004/11/06 22:00:57 $"; public final static String ID = "$Revision: 1.73 $ $Date: 2004/11/08 00:40:20 $";
public final static String VERSION = "0.4.1.4"; public final static String VERSION = "0.4.1.4";
public final static long BUILD = 1; public final static long BUILD = 2;
public static void main(String args[]) { public static void main(String args[]) {
System.out.println("I2P Router version: " + VERSION); System.out.println("I2P Router version: " + VERSION);
System.out.println("Router ID: " + RouterVersion.ID); System.out.println("Router ID: " + RouterVersion.ID);