EllipticCurves { public static boolean isValidDerEncoding(final byte[] sig) { if (sig.length < 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 ) { return false; } if (sig[0] != 0x30) { return false; } int totalLen = sig[1] & 0xff; int totalLenLen = 1; if (totalLen == 129) { totalLenLen = 2; totalLen = sig[2] & 0xff; if (totalLen < 128) { return false; } } else if (totalLen == 128 || totalLen > 129) { return false; } if (totalLen != sig.length - 1 - totalLenLen) { return false; } if (sig[1 + totalLenLen] != 0x02) { return false; } int rLen = sig[1 + totalLenLen + 1 ] & 0xff; if (1 + totalLenLen + 1 + 1 + rLen + 1 >= sig.length) { return false; } if (rLen == 0) { return false; } if ((sig[3 + totalLenLen] & 0xff) >= 128) { return false; } if (rLen > 1 && (sig[3 + totalLenLen] == 0x00) && ((sig[4 + totalLenLen] & 0xff) < 128)) { return false; } if (sig[3 + totalLenLen + rLen] != 0x02) { return false; } int sLen = sig[1 + totalLenLen + 1 + 1 + rLen + 1 ] & 0xff; if (1 + totalLenLen + 1 + 1 + rLen + 1 + 1 + sLen != sig.length) { return false; } if (sLen == 0) { return false; } if ((sig[5 + totalLenLen + rLen] & 0xff) >= 128) { return false; } if (sLen > 1 && (sig[5 + totalLenLen + rLen] == 0x00) && ((sig[6 + totalLenLen + rLen] & 0xff) < 128)) { return false; } return true; }  static ECParameterSpec getNistP256Params(); static ECParameterSpec getNistP384Params(); static ECParameterSpec getNistP521Params(); static boolean isNistEcParameterSpec(ECParameterSpec spec); static boolean isSameEcParameterSpec(ECParameterSpec one, ECParameterSpec two); static void validatePublicKey(ECPublicKey publicKey, ECPrivateKey privateKey); static BigInteger getModulus(EllipticCurve curve); static int fieldSizeInBytes(EllipticCurve curve); static BigInteger getY(BigInteger x, boolean lsb, EllipticCurve curve); static byte[] ecdsaIeee2Der(byte[] ieee); static byte[] ecdsaDer2Ieee(byte[] der, int ieeeLength); static boolean isValidDerEncoding(final byte[] sig); static int encodingSizeInBytes(EllipticCurve curve, PointFormatType format); @Deprecated static ECPoint ecPointDecode(EllipticCurve curve, PointFormatType format, byte[] encoded); static ECPoint pointDecode(CurveType curveType, PointFormatType format, byte[] encoded); static ECPoint pointDecode(EllipticCurve curve, PointFormatType format, byte[] encoded); static byte[] pointEncode(CurveType curveType, PointFormatType format, ECPoint point); static byte[] pointEncode(EllipticCurve curve, PointFormatType format, ECPoint point); static ECParameterSpec getCurveSpec(CurveType curve); static ECPublicKey getEcPublicKey(final byte[] x509PublicKey); static ECPublicKey getEcPublicKey(
      CurveType curve, PointFormatType pointFormat, final byte[] publicKey); static ECPublicKey getEcPublicKey(
      ECParameterSpec spec, PointFormatType pointFormat, final byte[] publicKey); static ECPublicKey getEcPublicKey(CurveType curve, final byte[] x, final byte[] y); static ECPrivateKey getEcPrivateKey(final byte[] pkcs8PrivateKey); static ECPrivateKey getEcPrivateKey(CurveType curve, final byte[] keyValue); static KeyPair generateKeyPair(CurveType curve); static KeyPair generateKeyPair(ECParameterSpec spec); static byte[] computeSharedSecret(ECPrivateKey myPrivateKey, ECPublicKey peerPublicKey); static byte[] computeSharedSecret(ECPrivateKey myPrivateKey, ECPoint publicPoint);  }
@Test public void testIsValidDerEncoding() throws Exception { for (String der : invalidEcdsaDers) { assertFalse(EllipticCurves.isValidDerEncoding(Hex.decode(der))); } }