ECC serializers now explicitly throw KryoException

This commit is contained in:
nathan 2018-01-24 23:51:50 +01:00
parent 6eea431108
commit 687f5d4aae
2 changed files with 17 additions and 15 deletions

View File

@ -26,6 +26,7 @@ import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.math.ec.ECPoint;
import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.Serializer; import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input; import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output; import com.esotericsoftware.kryo.io.Output;
@ -46,7 +47,7 @@ class EccPrivateKeySerializer extends Serializer<ECPrivateKeyParameters> {
private static final byte usesOid = (byte) 2; private static final byte usesOid = (byte) 2;
public static public static
void write(Output output, ECPrivateKeyParameters key) { void write(Output output, ECPrivateKeyParameters key) throws KryoException {
byte[] bytes; byte[] bytes;
int length; int length;
@ -78,7 +79,7 @@ class EccPrivateKeySerializer extends Serializer<ECPrivateKeyParameters> {
} }
public static public static
ECPrivateKeyParameters read(Input input) { ECPrivateKeyParameters read(Input input) throws KryoException {
byte[] bytes; byte[] bytes;
int length; int length;
@ -114,7 +115,7 @@ class EccPrivateKeySerializer extends Serializer<ECPrivateKeyParameters> {
} }
static static
void serializeCurve(Output output, ECCurve curve) { void serializeCurve(Output output, ECCurve curve) throws KryoException {
byte[] bytes; byte[] bytes;
int length; int length;
// save out if it's a NAMED curve, or a UN-NAMED curve. If it is named, we can do less work. // save out if it's a NAMED curve, or a UN-NAMED curve. If it is named, we can do less work.
@ -193,22 +194,23 @@ class EccPrivateKeySerializer extends Serializer<ECPrivateKeyParameters> {
} }
static static
ECCurve deserializeCurve(Input input) { ECCurve deserializeCurve(Input input) throws KryoException {
byte[] bytes; byte[] bytes;
int length; int length;
ECCurve curve; ECCurve curve;
int serializatioType = input.readInt(true);
int serializationType = input.readInt(true);
// lookup via name // lookup via name
if (serializatioType == usesName) { if (serializationType == usesName) {
String curveName = input.readString(); String curveName = input.readString();
X9ECParameters x9Curve = CustomNamedCurves.getByName(curveName); X9ECParameters x9Curve = CustomNamedCurves.getByName(curveName);
curve = x9Curve.getCurve(); curve = x9Curve.getCurve();
} }
// this means we just lookup the curve via the OID // this means we just lookup the curve via the OID
else if (serializatioType == usesOid) { else if (serializationType == usesOid) {
String oid = input.readString(); String oid = input.readString();
X9ECParameters x9Curve = CustomNamedCurves.getByOID(new ASN1ObjectIdentifier(oid)); X9ECParameters x9Curve = CustomNamedCurves.getByOID(new ASN1ObjectIdentifier(oid));
curve = x9Curve.getCurve(); curve = x9Curve.getCurve();
@ -257,7 +259,7 @@ class EccPrivateKeySerializer extends Serializer<ECPrivateKeyParameters> {
} }
static static
void serializeECPoint(ECPoint point, Output output) { void serializeECPoint(ECPoint point, Output output) throws KryoException {
if (point.isInfinity()) { if (point.isInfinity()) {
return; return;
} }
@ -279,14 +281,14 @@ class EccPrivateKeySerializer extends Serializer<ECPrivateKeyParameters> {
@Override @Override
public public
void write(Kryo kryo, Output output, ECPrivateKeyParameters key) { void write(Kryo kryo, Output output, ECPrivateKeyParameters key) throws KryoException {
write(output, key); write(output, key);
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@Override @Override
public public
ECPrivateKeyParameters read(Kryo kryo, Input input, Class type) { ECPrivateKeyParameters read(Kryo kryo, Input input, Class type) throws KryoException {
return read(input); return read(input);
} }
} }

View File

@ -23,6 +23,7 @@ import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.math.ec.ECPoint;
import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.Serializer; import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input; import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output; import com.esotericsoftware.kryo.io.Output;
@ -34,7 +35,7 @@ public
class EccPublicKeySerializer extends Serializer<ECPublicKeyParameters> { class EccPublicKeySerializer extends Serializer<ECPublicKeyParameters> {
public static public static
void write(Output output, ECPublicKeyParameters key) { void write(Output output, ECPublicKeyParameters key) throws KryoException {
byte[] bytes; byte[] bytes;
int length; int length;
@ -60,7 +61,7 @@ class EccPublicKeySerializer extends Serializer<ECPublicKeyParameters> {
} }
public static public static
ECPublicKeyParameters read(Input input) { ECPublicKeyParameters read(Input input) throws KryoException {
byte[] bytes; byte[] bytes;
int length; int length;
@ -85,7 +86,6 @@ class EccPublicKeySerializer extends Serializer<ECPublicKeyParameters> {
ECDomainParameters ecDomainParameters = new ECDomainParameters(curve, g, n); ECDomainParameters ecDomainParameters = new ECDomainParameters(curve, g, n);
// Q // Q
///////////// /////////////
length = input.readInt(true); length = input.readInt(true);
@ -98,14 +98,14 @@ class EccPublicKeySerializer extends Serializer<ECPublicKeyParameters> {
@Override @Override
public public
void write(Kryo kryo, Output output, ECPublicKeyParameters key) { void write(Kryo kryo, Output output, ECPublicKeyParameters key) throws KryoException {
write(output, key); write(output, key);
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@Override @Override
public public
ECPublicKeyParameters read(Kryo kryo, Input input, Class type) { ECPublicKeyParameters read(Kryo kryo, Input input, Class type) throws KryoException {
return read(input); return read(input);
} }
} }