private static JsonWebKeyCurveName getCurveFromKeyPair(KeyPair keyPair, Provider provider) { try { ECPublicKey key = (ECPublicKey) keyPair.getPublic(); ECParameterSpec spec = key.getParams(); EllipticCurve crv = spec.getCurve(); List<JsonWebKeyCurveName> curveList = Arrays.asList(JsonWebKeyCurveName.P_256, JsonWebKeyCurveName.P_384, JsonWebKeyCurveName.P_521, JsonWebKeyCurveName.P_256K); for (JsonWebKeyCurveName curve : curveList) { ECGenParameterSpec gps = new ECGenParameterSpec(CURVE_TO_SPEC_NAME.get(curve)); KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", provider); kpg.initialize(gps); // Generate dummy keypair to get parameter spec. KeyPair apair = kpg.generateKeyPair(); ECPublicKey apub = (ECPublicKey) apair.getPublic(); ECParameterSpec aspec = apub.getParams(); EllipticCurve acurve = aspec.getCurve(); // Matches the parameter spec if (acurve.equals(crv)) { return curve; } } // Did not find a supported curve. throw new NoSuchAlgorithmException("Curve not supported."); } catch (GeneralSecurityException e) { throw new IllegalStateException(e); } }
/** Checks that the public key's params spec is the same as the private key's params spec. */ static void validatePublicKeySpec(ECPublicKey publicKey, ECPrivateKey privateKey) throws GeneralSecurityException { try { ECParameterSpec publicKeySpec = publicKey.getParams(); ECParameterSpec privateKeySpec = privateKey.getParams(); if (!publicKeySpec.getCurve().equals(privateKeySpec.getCurve()) || !publicKeySpec.getGenerator().equals(privateKeySpec.getGenerator()) || !publicKeySpec.getOrder().equals(privateKeySpec.getOrder()) || publicKeySpec.getCofactor() != privateKeySpec.getCofactor()) { throw new GeneralSecurityException("invalid public key spec"); } } catch (IllegalArgumentException | NullPointerException ex) { // The Java security providers on Android K and Android L might throw these unchecked // exceptions, converting them to a checked one to not crash the JVM. throw new GeneralSecurityException(ex.toString()); } }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPrivateKey) { OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o; return key.equals(other.key); } if (!(o instanceof ECPrivateKey)) { return false; } final ECPrivateKey other = (ECPrivateKey) o; if (!getPrivateKey().equals(other.getS())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPrivateKey) { OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o; return key.equals(other.key); } if (!(o instanceof ECPrivateKey)) { return false; } final ECPrivateKey other = (ECPrivateKey) o; if (!getPrivateKey().equals(other.getS())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPrivateKey) { OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o; return key.equals(other.key); } if (!(o instanceof ECPrivateKey)) { return false; } final ECPrivateKey other = (ECPrivateKey) o; if (!getPrivateKey().equals(other.getS())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
private static boolean matchCurve(ECParameterSpec params, Curve curve) { int fieldSize = params.getCurve().getField().getFieldSize(); if (curve.getCurve().getField().getFieldSize() == fieldSize && curve.getCurve().equals(params.getCurve()) && curve.getGenerator().equals(params.getGenerator()) && curve.getOrder().equals(params.getOrder()) && curve.getCofactor() == params.getCofactor()) { return true; } else { return false; } }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPrivateKey) { OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o; return key.equals(other.key); } if (!(o instanceof ECPrivateKey)) { return false; } final ECPrivateKey other = (ECPrivateKey) o; if (!getPrivateKey().equals(other.getS())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
private static void validateEcCurves(ECPublicKey publicKey, ECPrivateKey privateKey) throws JOSEException { final ECParameterSpec pubParams = publicKey.getParams(); final ECParameterSpec privParams = privateKey.getParams(); if (!pubParams.getCurve().equals(privParams.getCurve())) { throw new JOSEException("Public/private EC key curve mismatch: " + publicKey); } if (pubParams.getCofactor() != privParams.getCofactor()) { throw new JOSEException("Public/private EC key cofactor mismatch: " + publicKey); } if (!pubParams.getGenerator().equals(privParams.getGenerator())) { throw new JOSEException("Public/private EC key generator mismatch: " + publicKey); } if (!pubParams.getOrder().equals(privParams.getOrder())) { throw new JOSEException("Public/private EC key order mismatch: " + publicKey); } }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPrivateKey) { OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o; return key.equals(other.key); } if (!(o instanceof ECPublicKey)) { return false; } final ECPublicKey other = (ECPublicKey) o; if (!getPublicKey().equals(other.getW())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPublicKey) { OpenSSLECPublicKey other = (OpenSSLECPublicKey) o; return key.equals(other.key); } if (!(o instanceof ECPublicKey)) { return false; } final ECPublicKey other = (ECPublicKey) o; if (!getPublicKey().equals(other.getW())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPrivateKey) { OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o; return key.equals(other.key); } if (!(o instanceof ECPublicKey)) { return false; } final ECPublicKey other = (ECPublicKey) o; if (!getPublicKey().equals(other.getW())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OpenSSLECPublicKey) { OpenSSLECPublicKey other = (OpenSSLECPublicKey) o; return key.equals(other.key); } if (!(o instanceof ECPublicKey)) { return false; } final ECPublicKey other = (ECPublicKey) o; if (!getPublicKey().equals(other.getW())) { return false; } final ECParameterSpec spec = getParams(); final ECParameterSpec otherSpec = other.getParams(); return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor(); }