Skip to content

Commit 08df2c2

Browse files
committed
Restore the Java 1.4 source floor in the EC point-decoding exception messages and sync the hex formatting and empty-encoding guard into the jdk1.2 and j2me ECCurve overlays.
1 parent e38a548 commit 08df2c2

4 files changed

Lines changed: 27 additions & 4 deletions

File tree

core/src/main/j2me/org/bouncycastle/math/ec/ECCurve.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.bouncycastle.util.BigIntegers;
1919
import org.bouncycastle.util.Integers;
2020
import org.bouncycastle.util.Properties;
21+
import org.bouncycastle.util.Strings;
2122

2223
/**
2324
* base class for an elliptic curve
@@ -381,6 +382,14 @@ public ECPoint decodePoint(byte[] encoded)
381382
ECPoint p = null;
382383
int expectedLength = (this.getFieldSize() + 7) / 8;
383384

385+
if (encoded == null || encoded.length < 1)
386+
{
387+
// An empty (or null) encoding must be reported as a malformed encoding, not leak an
388+
// ArrayIndexOutOfBoundsException out of the point decoder to every caller that decodes
389+
// an untrusted point (EC key parse, ECDH/ECIES/TLS ephemeral points).
390+
throw new IllegalArgumentException("Invalid point encoding: empty");
391+
}
392+
384393
byte type = encoded[0];
385394
switch (type)
386395
{
@@ -446,7 +455,8 @@ public ECPoint decodePoint(byte[] encoded)
446455
break;
447456
}
448457
default:
449-
throw new IllegalArgumentException("Invalid point encoding type: 0x" + Integer.toString(type, 16));
458+
throw new IllegalArgumentException("Invalid point encoding type: 0x"
459+
+ Strings.toUpperCase(Integer.toHexString(0x100 | (type & 0xFF)).substring(1)));
450460
}
451461

452462
if (type != 0x00 && p.isInfinity())

core/src/main/java/org/bouncycastle/crypto/parsers/ECIESPublicKeyParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.bouncycastle.crypto.params.ECDomainParameters;
1010
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
1111
import org.bouncycastle.math.ec.ECCurve;
12+
import org.bouncycastle.util.Strings;
1213
import org.bouncycastle.util.io.Streams;
1314

1415
public class ECIESPublicKeyParser
@@ -50,7 +51,7 @@ public AsymmetricKeyParameter readKey(InputStream stream)
5051

5152
default:
5253
throw new IOException("Sender's public key has invalid point encoding type: 0x"
53-
+ String.format("%02X", first));
54+
+ Strings.toUpperCase(Integer.toHexString(0x100 | first).substring(1)));
5455
}
5556

5657
ECCurve curve = ecParams.getCurve();

core/src/main/java/org/bouncycastle/math/ec/ECCurve.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.bouncycastle.util.BigIntegers;
1919
import org.bouncycastle.util.Integers;
2020
import org.bouncycastle.util.Properties;
21+
import org.bouncycastle.util.Strings;
2122

2223
/**
2324
* base class for an elliptic curve
@@ -467,7 +468,8 @@ public ECPoint decodePoint(byte[] encoded)
467468
break;
468469
}
469470
default:
470-
throw new IllegalArgumentException("Invalid point encoding type: 0x" + String.format("%02X", type));
471+
throw new IllegalArgumentException("Invalid point encoding type: 0x"
472+
+ Strings.toUpperCase(Integer.toHexString(0x100 | (type & 0xFF)).substring(1)));
471473
}
472474

473475
if (type != 0x00 && p.isInfinity())

core/src/main/jdk1.2/org/bouncycastle/math/ec/ECCurve.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.bouncycastle.util.BigIntegers;
1919
import org.bouncycastle.util.Integers;
2020
import org.bouncycastle.util.Properties;
21+
import org.bouncycastle.util.Strings;
2122

2223
/**
2324
* base class for an elliptic curve
@@ -394,6 +395,14 @@ public ECPoint decodePoint(byte[] encoded)
394395
ECPoint p = null;
395396
int expectedLength = getFieldElementEncodingLength();
396397

398+
if (encoded == null || encoded.length < 1)
399+
{
400+
// An empty (or null) encoding must be reported as a malformed encoding, not leak an
401+
// ArrayIndexOutOfBoundsException out of the point decoder to every caller that decodes
402+
// an untrusted point (EC key parse, ECDH/ECIES/TLS ephemeral points).
403+
throw new IllegalArgumentException("Invalid point encoding: empty");
404+
}
405+
397406
byte type = encoded[0];
398407
switch (type)
399408
{
@@ -459,7 +468,8 @@ public ECPoint decodePoint(byte[] encoded)
459468
break;
460469
}
461470
default:
462-
throw new IllegalArgumentException("Invalid point encoding type: 0x" + Integer.toString(type, 16));
471+
throw new IllegalArgumentException("Invalid point encoding type: 0x"
472+
+ Strings.toUpperCase(Integer.toHexString(0x100 | (type & 0xFF)).substring(1)));
463473
}
464474

465475
if (type != 0x00 && p.isInfinity())

0 commit comments

Comments
 (0)