Skip to content
66 changes: 2 additions & 64 deletions src/freenet/crypt/CryptoKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
* http://www.gnu.org/ for further details of the GPL. */
package freenet.crypt;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.security.MessageDigest;

import freenet.support.HexUtil;
import freenet.support.Logger;

public abstract class CryptoKey implements CryptoElement, Serializable {

Expand All @@ -21,46 +16,19 @@ public abstract class CryptoKey implements CryptoElement, Serializable {
CryptoKey() {
}

public static CryptoKey read(InputStream i) throws IOException, CryptFormatException {
DataInputStream dis = new DataInputStream(i);
String type = dis.readUTF();
try {
Class<?> keyClass = Class.forName(type);
Method m =
keyClass.getMethod("read", new Class<?>[] { InputStream.class });
return (CryptoKey) m.invoke(null, dis);
} catch (Exception e) {
e.printStackTrace();
if (e instanceof CryptFormatException)
throw (CryptFormatException) e;
if (e instanceof IOException)
throw (IOException) e;
Logger.error(CryptoKey.class, "Unknown exception while reading CryptoKey", e);
return null;
}
}

// public abstract void write(OutputStream o) throws IOException;

public abstract String keyType();
public abstract byte[] fingerprint();
public abstract byte[] asBytes();

protected byte[] fingerprint(BigInteger[] quantities) {
static byte[] fingerprint(BigInteger... quantities) {
MessageDigest shactx = HashType.SHA1.get();
for (BigInteger quantity: quantities) {
for (BigInteger quantity : quantities) {
byte[] mpi = Util.MPIbytes(quantity);
shactx.update(mpi, 0, mpi.length);
}
return shactx.digest();
}

public String verboseToString() {
StringBuilder b = new StringBuilder();
b.append(toString()).append('\t').append(fingerprintToString());
return b.toString();
}

@Override
public String toString() {
StringBuilder b = new StringBuilder(keyType().length() + 1 + 4);
Expand All @@ -69,34 +37,4 @@ public String toString() {
return b.toString();
}

// protected void write(OutputStream o, String clazz) throws IOException {
// UTF8.writeWithLength(o, clazz);
// }
//
public String fingerprintToString() {
String fphex = HexUtil.bytesToHex(fingerprint());
StringBuilder b = new StringBuilder(40 + 10);
b
.append(fphex.substring(0, 4))
.append(' ')
.append(fphex.substring(4, 8))
.append(' ')
.append(fphex.substring(8, 12))
.append(' ')
.append(fphex.substring(12, 16))
.append(' ')
.append(fphex.substring(16, 20))
.append(" ")
.append(fphex.substring(20, 24))
.append(' ')
.append(fphex.substring(24, 28))
.append(' ')
.append(fphex.substring(28, 32))
.append(' ')
.append(fphex.substring(32, 36))
.append(' ')
.append(fphex.substring(36, 40));
return b.toString();
}

}
56 changes: 9 additions & 47 deletions src/freenet/crypt/DSAGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.Objects;

import freenet.node.FSParseException;
import freenet.support.Base64;
Expand All @@ -19,8 +20,6 @@
*/
public class DSAGroup extends CryptoKey {
private static final long serialVersionUID = -1;

protected static final int Q_BIT_LENGTH = 256;

private final BigInteger p, q, g;

Expand All @@ -32,33 +31,7 @@ public DSAGroup(BigInteger p, BigInteger q, BigInteger g) {
throw new IllegalArgumentException();
}

private DSAGroup(DSAGroup group) {
this.p = new BigInteger(1, group.p.toByteArray());
this.q = new BigInteger(1, group.q.toByteArray());
this.g = new BigInteger(1, group.g.toByteArray());
}

protected DSAGroup() {
// For serialization.
p = null;
q = null;
g = null;
}

/**
* Parses a DSA Group from a string, where p, q, and g are in unsigned
* hex-strings, separated by a commas
*/
// see readFromField() below
//public static DSAGroup parse(String grp) {
// StringTokenizer str=new StringTokenizer(grp, ",");
// BigInteger p,q,g;
// p = new BigInteger(str.nextToken(), 16);
// q = new BigInteger(str.nextToken(), 16);
// g = new BigInteger(str.nextToken(), 16);
// return new DSAGroup(p,q,g);
//}
public static CryptoKey read(InputStream i) throws IOException, CryptFormatException {
public static DSAGroup read(InputStream i) throws IOException, CryptFormatException {
BigInteger p, q, g;
p = Util.readMPI(i);
q = Util.readMPI(i);
Expand Down Expand Up @@ -91,11 +64,7 @@ public BigInteger getG() {

@Override
public byte[] fingerprint() {
BigInteger fp[] = new BigInteger[3];
fp[0] = p;
fp[1] = q;
fp[2] = g;
return fingerprint(fp);
return fingerprint(p, q, g);
}

@Override
Expand All @@ -112,21 +81,19 @@ public byte[] asBytes() {

@Override
public boolean equals(Object o) {
if (this == o) // Not necessary, but a very cheap optimization
return true;
return (o instanceof DSAGroup) && p.equals(((DSAGroup) o).p)
&& q.equals(((DSAGroup) o).q) && g.equals(((DSAGroup) o).g);
return (o instanceof DSAGroup) && equals((DSAGroup) o);
}

public boolean equals(DSAGroup o) {
if (this == o) // Not necessary, but a very cheap optimization
return true;
return p.equals(o.p) && q.equals(o.q) && g.equals(o.g);
if (this == o) {
return true;
}
return Objects.equals(p, o.p) && Objects.equals(q, o.q) && Objects.equals(g, o.g);
}

@Override
public int hashCode() {
return p.hashCode() ^ q.hashCode() ^ g.hashCode();
return Objects.hash(p, q, g);
}

public SimpleFieldSet asFieldSet() {
Expand Down Expand Up @@ -164,9 +131,4 @@ public String toLongString() {
return "p="+HexUtil.biToHex(p)+", q="+HexUtil.biToHex(q)+", g="+HexUtil.biToHex(g);
}

public DSAGroup cloneKey() {
if(this == Global.DSAgroupBigA) return this;
return new DSAGroup(this);
}

}
27 changes: 5 additions & 22 deletions src/freenet/crypt/DSAPrivateKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,13 @@ public DSAPrivateKey(BigInteger x, DSAGroup g) {
throw new IllegalArgumentException();
}

// this is dangerous... better to force people to construct the
// BigInteger themselves so they know what is going on with the sign
//public DSAPrivateKey(byte[] x) {
// this.x = new BigInteger(1, x);
//}

public DSAPrivateKey(DSAGroup g, Random r) {
BigInteger tempX;
do {
tempX = new BigInteger(256, r);
} while (tempX.compareTo(g.getQ()) > -1 || tempX.compareTo(BigInteger.ZERO) < 1);
this.x = tempX;
}

protected DSAPrivateKey() {
// For serialization.
x = null;
}

@Override
public String keyType() {
Expand All @@ -61,20 +50,14 @@ public String toLongString() {
return "x="+HexUtil.biToHex(x);
}

// what? why is DSAGroup passed in?
//public static CryptoKey readFromField(DSAGroup group, String field) {
// //BigInteger x=Util.byteArrayToMPI(Util.hexToBytes(field));
// return new DSAPrivateKey(new BigInteger(field, 16));
//}

@Override
public byte[] asBytes() {
return Util.MPIbytes(x);
}

@Override
public byte[] fingerprint() {
return fingerprint(new BigInteger[] {x});
return fingerprint(x);
}

public SimpleFieldSet asFieldSet() {
Expand All @@ -84,10 +67,10 @@ public SimpleFieldSet asFieldSet() {
}

public static DSAPrivateKey create(SimpleFieldSet fs, DSAGroup group) throws IllegalBase64Exception {
BigInteger y = new BigInteger(1, Base64.decode(fs.get("x")));
if(y.bitLength() > 512)
BigInteger x = new BigInteger(1, Base64.decode(fs.get("x")));
if (x.bitLength() > 512) {
throw new IllegalBase64Exception("Probably a pubkey");
return new DSAPrivateKey(y, group);
}
return new DSAPrivateKey(x, group);
}
}

Loading