Java Code Examples for javax.crypto.interfaces.DHPublicKey#getParams()

The following examples show how to use javax.crypto.interfaces.DHPublicKey#getParams() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DHClientKeyExchange.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
DHClientKeyExchangeMessage(
        HandshakeContext handshakeContext) throws IOException {
    super(handshakeContext);
    // This happens in client side only.
    ClientHandshakeContext chc =
            (ClientHandshakeContext)handshakeContext;

    DHEPossession dhePossession = null;
    for (SSLPossession possession : chc.handshakePossessions) {
        if (possession instanceof DHEPossession) {
            dhePossession = (DHEPossession)possession;
            break;
        }
    }

    if (dhePossession == null) {
        // unlikely
        throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
            "No DHE credentials negotiated for client key exchange");
    }

    DHPublicKey publicKey = dhePossession.publicKey;
    DHParameterSpec params = publicKey.getParams();
    this.y = Utilities.toByteArray(publicKey.getY());
}
 
Example 2
Source File: DHClientKeyExchange.java    From openjsse with GNU General Public License v2.0 6 votes vote down vote up
DHClientKeyExchangeMessage(
        HandshakeContext handshakeContext) throws IOException {
    super(handshakeContext);
    // This happens in client side only.
    ClientHandshakeContext chc =
            (ClientHandshakeContext)handshakeContext;

    DHEPossession dhePossession = null;
    for (SSLPossession possession : chc.handshakePossessions) {
        if (possession instanceof DHEPossession) {
            dhePossession = (DHEPossession)possession;
            break;
        }
    }

    if (dhePossession == null) {
        // unlikely
        throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
            "No DHE credentials negotiated for client key exchange");
    }

    DHPublicKey publicKey = dhePossession.publicKey;
    DHParameterSpec params = publicKey.getParams();
    this.y = Utilities.toByteArray(publicKey.getY());
}
 
Example 3
Source File: KeyUtil.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the Diffie-Hellman public key is valid or not.
 *
 * Per RFC 2631 and NIST SP800-56A, the following algorithm is used to
 * validate Diffie-Hellman public keys:
 * 1. Verify that y lies within the interval [2,p-1]. If it does not,
 *    the key is invalid.
 * 2. Compute y^q mod p. If the result == 1, the key is valid.
 *    Otherwise the key is invalid.
 */
private static void validateDHPublicKey(DHPublicKey publicKey)
        throws InvalidKeyException {
    DHParameterSpec paramSpec = publicKey.getParams();

    BigInteger p = paramSpec.getP();
    BigInteger g = paramSpec.getG();
    BigInteger y = publicKey.getY();

    validateDHPublicKey(p, g, y);
}
 
Example 4
Source File: DHCrypt.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static DHPublicKeySpec getDHPublicKeySpec(PublicKey key) {
    if (key instanceof DHPublicKey) {
        DHPublicKey dhKey = (DHPublicKey)key;
        DHParameterSpec params = dhKey.getParams();
        return new DHPublicKeySpec(dhKey.getY(),
                                params.getP(), params.getG());
    }
    try {
        KeyFactory factory = JsseJce.getKeyFactory("DH");
        return factory.getKeySpec(key, DHPublicKeySpec.class);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: KeyUtil.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the Diffie-Hellman public key is valid or not.
 *
 * Per RFC 2631 and NIST SP800-56A, the following algorithm is used to
 * validate Diffie-Hellman public keys:
 * 1. Verify that y lies within the interval [2,p-1]. If it does not,
 *    the key is invalid.
 * 2. Compute y^q mod p. If the result == 1, the key is valid.
 *    Otherwise the key is invalid.
 */
private static void validateDHPublicKey(DHPublicKey publicKey)
        throws InvalidKeyException {
    DHParameterSpec paramSpec = publicKey.getParams();

    BigInteger p = paramSpec.getP();
    BigInteger g = paramSpec.getG();
    BigInteger y = publicKey.getY();

    validateDHPublicKey(p, g, y);
}
 
Example 6
Source File: DHCrypt.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static DHPublicKeySpec getDHPublicKeySpec(PublicKey key) {
    if (key instanceof DHPublicKey) {
        DHPublicKey dhKey = (DHPublicKey)key;
        DHParameterSpec params = dhKey.getParams();
        return new DHPublicKeySpec(dhKey.getY(),
                                params.getP(), params.getG());
    }
    try {
        KeyFactory factory = JsseJce.getKeyFactory("DiffieHellman");
        return factory.getKeySpec(key, DHPublicKeySpec.class);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 7
Source File: KeyUtil.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the Diffie-Hellman public key is valid or not.
 *
 * Per RFC 2631 and NIST SP800-56A, the following algorithm is used to
 * validate Diffie-Hellman public keys:
 * 1. Verify that y lies within the interval [2,p-1]. If it does not,
 *    the key is invalid.
 * 2. Compute y^q mod p. If the result == 1, the key is valid.
 *    Otherwise the key is invalid.
 */
private static void validateDHPublicKey(DHPublicKey publicKey)
        throws InvalidKeyException {
    DHParameterSpec paramSpec = publicKey.getParams();

    BigInteger p = paramSpec.getP();
    BigInteger g = paramSpec.getG();
    BigInteger y = publicKey.getY();

    validateDHPublicKey(p, g, y);
}
 
Example 8
Source File: DHKeyExchange.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private static DHPublicKeySpec getDHPublicKeySpec(PublicKey key) {
    if (key instanceof DHPublicKey) {
        DHPublicKey dhKey = (DHPublicKey)key;
        DHParameterSpec params = dhKey.getParams();
        return new DHPublicKeySpec(dhKey.getY(),
                                params.getP(), params.getG());
    }
    try {
        KeyFactory factory = KeyFactory.getInstance("DiffieHellman");
        return factory.getKeySpec(key, DHPublicKeySpec.class);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        // unlikely
        throw new RuntimeException("Unable to get DHPublicKeySpec", e);
    }
}
 
Example 9
Source File: KeyUtil.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the Diffie-Hellman public key is valid or not.
 *
 * Per RFC 2631 and NIST SP800-56A, the following algorithm is used to
 * validate Diffie-Hellman public keys:
 * 1. Verify that y lies within the interval [2,p-1]. If it does not,
 *    the key is invalid.
 * 2. Compute y^q mod p. If the result == 1, the key is valid.
 *    Otherwise the key is invalid.
 */
private static void validateDHPublicKey(DHPublicKey publicKey)
        throws InvalidKeyException {
    DHParameterSpec paramSpec = publicKey.getParams();

    BigInteger p = paramSpec.getP();
    BigInteger g = paramSpec.getG();
    BigInteger y = publicKey.getY();

    validateDHPublicKey(p, g, y);
}
 
Example 10
Source File: DHCrypt.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static DHPublicKeySpec getDHPublicKeySpec(PublicKey key) {
    if (key instanceof DHPublicKey) {
        DHPublicKey dhKey = (DHPublicKey)key;
        DHParameterSpec params = dhKey.getParams();
        return new DHPublicKeySpec(dhKey.getY(),
                                params.getP(), params.getG());
    }
    try {
        KeyFactory factory = JsseJce.getKeyFactory("DiffieHellman");
        return factory.getKeySpec(key, DHPublicKeySpec.class);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 11
Source File: DHCrypt.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
static DHPublicKeySpec getDHPublicKeySpec(PublicKey key) {
    if (key instanceof DHPublicKey) {
        DHPublicKey dhKey = (DHPublicKey)key;
        DHParameterSpec params = dhKey.getParams();
        return new DHPublicKeySpec(dhKey.getY(),
                                params.getP(), params.getG());
    }
    try {
        KeyFactory factory = JsseJce.getKeyFactory("DH");
        return factory.getKeySpec(key, DHPublicKeySpec.class);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 12
Source File: KeyUtil.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the Diffie-Hellman public key is valid or not.
 *
 * Per RFC 2631 and NIST SP800-56A, the following algorithm is used to
 * validate Diffie-Hellman public keys:
 * 1. Verify that y lies within the interval [2,p-1]. If it does not,
 *    the key is invalid.
 * 2. Compute y^q mod p. If the result == 1, the key is valid.
 *    Otherwise the key is invalid.
 */
private static void validateDHPublicKey(DHPublicKey publicKey)
        throws InvalidKeyException {
    DHParameterSpec paramSpec = publicKey.getParams();

    BigInteger p = paramSpec.getP();
    BigInteger g = paramSpec.getG();
    BigInteger y = publicKey.getY();

    validateDHPublicKey(p, g, y);
}
 
Example 13
Source File: DHCrypt.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static DHPublicKeySpec getDHPublicKeySpec(PublicKey key) {
    if (key instanceof DHPublicKey) {
        DHPublicKey dhKey = (DHPublicKey)key;
        DHParameterSpec params = dhKey.getParams();
        return new DHPublicKeySpec(dhKey.getY(),
                                params.getP(), params.getG());
    }
    try {
        KeyFactory factory = JsseJce.getKeyFactory("DH");
        return factory.getKeySpec(key, DHPublicKeySpec.class);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 14
Source File: KeyUtil.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the Diffie-Hellman public key is valid or not.
 *
 * Per RFC 2631 and NIST SP800-56A, the following algorithm is used to
 * validate Diffie-Hellman public keys:
 * 1. Verify that y lies within the interval [2,p-1]. If it does not,
 *    the key is invalid.
 * 2. Compute y^q mod p. If the result == 1, the key is valid.
 *    Otherwise the key is invalid.
 */
private static void validateDHPublicKey(DHPublicKey publicKey)
        throws InvalidKeyException {
    DHParameterSpec paramSpec = publicKey.getParams();

    BigInteger p = paramSpec.getP();
    BigInteger g = paramSpec.getG();
    BigInteger y = publicKey.getY();

    validateDHPublicKey(p, g, y);
}
 
Example 15
Source File: KeyUtil.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the Diffie-Hellman public key is valid or not.
 *
 * Per RFC 2631 and NIST SP800-56A, the following algorithm is used to
 * validate Diffie-Hellman public keys:
 * 1. Verify that y lies within the interval [2,p-1]. If it does not,
 *    the key is invalid.
 * 2. Compute y^q mod p. If the result == 1, the key is valid.
 *    Otherwise the key is invalid.
 */
private static void validateDHPublicKey(DHPublicKey publicKey)
        throws InvalidKeyException {
    DHParameterSpec paramSpec = publicKey.getParams();

    BigInteger p = paramSpec.getP();
    BigInteger g = paramSpec.getG();
    BigInteger y = publicKey.getY();

    validateDHPublicKey(p, g, y);
}
 
Example 16
Source File: KeyUtil.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the Diffie-Hellman public key is valid or not.
 *
 * Per RFC 2631 and NIST SP800-56A, the following algorithm is used to
 * validate Diffie-Hellman public keys:
 * 1. Verify that y lies within the interval [2,p-1]. If it does not,
 *    the key is invalid.
 * 2. Compute y^q mod p. If the result == 1, the key is valid.
 *    Otherwise the key is invalid.
 */
private static void validateDHPublicKey(DHPublicKey publicKey)
        throws InvalidKeyException {
    DHParameterSpec paramSpec = publicKey.getParams();

    BigInteger p = paramSpec.getP();
    BigInteger g = paramSpec.getG();
    BigInteger y = publicKey.getY();

    validateDHPublicKey(p, g, y);
}
 
Example 17
Source File: DHCrypt.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static DHPublicKeySpec getDHPublicKeySpec(PublicKey key) {
    if (key instanceof DHPublicKey) {
        DHPublicKey dhKey = (DHPublicKey)key;
        DHParameterSpec params = dhKey.getParams();
        return new DHPublicKeySpec(dhKey.getY(),
                                params.getP(), params.getG());
    }
    try {
        KeyFactory factory = JsseJce.getKeyFactory("DiffieHellman");
        return factory.getKeySpec(key, DHPublicKeySpec.class);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 18
Source File: ValueLinkApi.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * Create a set of public/private keys using ValueLinks defined parameters
 * @return KeyPair object containing both public and private keys
 * @throws NoSuchAlgorithmException
 * @throws InvalidAlgorithmParameterException
 */
public KeyPair createKeys() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException {
    // initialize the parameter spec
    DHPublicKey publicKey = (DHPublicKey) this.getValueLinkPublicKey();
    DHParameterSpec dhParamSpec = publicKey.getParams();
    // create the public/private key pair using parameters defined by valuelink
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
    keyGen.initialize(dhParamSpec);
    KeyPair keyPair = keyGen.generateKeyPair();

    return keyPair;
}
 
Example 19
Source File: DHKeyExchange.java    From openjsse with GNU General Public License v2.0 5 votes vote down vote up
private static DHPublicKeySpec getDHPublicKeySpec(PublicKey key) {
    if (key instanceof DHPublicKey) {
        DHPublicKey dhKey = (DHPublicKey)key;
        DHParameterSpec params = dhKey.getParams();
        return new DHPublicKeySpec(dhKey.getY(),
                                params.getP(), params.getG());
    }
    try {
        KeyFactory factory = JsseJce.getKeyFactory("DiffieHellman");
        return factory.getKeySpec(key, DHPublicKeySpec.class);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        // unlikely
        throw new RuntimeException("Unable to get DHPublicKeySpec", e);
    }
}
 
Example 20
Source File: DHServerKeyExchange.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
DHServerKeyExchangeMessage(
        HandshakeContext handshakeContext) throws IOException {
    super(handshakeContext);

    // This happens in server side only.
    ServerHandshakeContext shc =
            (ServerHandshakeContext)handshakeContext;

    DHEPossession dhePossession = null;
    X509Possession x509Possession = null;
    for (SSLPossession possession : shc.handshakePossessions) {
        if (possession instanceof DHEPossession) {
            dhePossession = (DHEPossession)possession;
            if (x509Possession != null) {
                break;
            }
        } else if (possession instanceof X509Possession) {
            x509Possession = (X509Possession)possession;
            if (dhePossession != null) {
                break;
            }
        }
    }

    if (dhePossession == null) {
        // unlikely
        throw shc.conContext.fatal(Alert.ILLEGAL_PARAMETER,
            "No DHE credentials negotiated for server key exchange");
    }
    DHPublicKey publicKey = dhePossession.publicKey;
    DHParameterSpec params = publicKey.getParams();
    this.p = Utilities.toByteArray(params.getP());
    this.g = Utilities.toByteArray(params.getG());
    this.y = Utilities.toByteArray(publicKey.getY());

    if (x509Possession == null) {
        // anonymous, no authentication, no signature
        paramsSignature = null;
        signatureScheme = null;
        useExplicitSigAlgorithm = false;
    } else {
        useExplicitSigAlgorithm =
                shc.negotiatedProtocol.useTLS12PlusSpec();
        Signature signer = null;
        if (useExplicitSigAlgorithm) {
            Map.Entry<SignatureScheme, Signature> schemeAndSigner =
                    SignatureScheme.getSignerOfPreferableAlgorithm(
                            shc.algorithmConstraints,
                            shc.peerRequestedSignatureSchemes,
                            x509Possession,
                            shc.negotiatedProtocol);
            if (schemeAndSigner == null) {
                // Unlikely, the credentials generator should have
                // selected the preferable signature algorithm properly.
                throw shc.conContext.fatal(Alert.INTERNAL_ERROR,
                        "No supported signature algorithm for " +
                        x509Possession.popPrivateKey.getAlgorithm() +
                        "  key");
            } else {
                signatureScheme = schemeAndSigner.getKey();
                signer = schemeAndSigner.getValue();
            }
        } else {
            signatureScheme = null;
            try {
                signer = getSignature(
                        x509Possession.popPrivateKey.getAlgorithm(),
                        x509Possession.popPrivateKey);
            } catch (NoSuchAlgorithmException | InvalidKeyException e) {
                throw shc.conContext.fatal(Alert.INTERNAL_ERROR,
                    "Unsupported signature algorithm: " +
                    x509Possession.popPrivateKey.getAlgorithm(), e);
            }
        }

        byte[] signature = null;
        try {
            updateSignature(signer, shc.clientHelloRandom.randomBytes,
                    shc.serverHelloRandom.randomBytes);
            signature = signer.sign();
        } catch (SignatureException ex) {
            throw shc.conContext.fatal(Alert.INTERNAL_ERROR,
                "Failed to sign dhe parameters: " +
                x509Possession.popPrivateKey.getAlgorithm(), ex);
        }
        paramsSignature = signature;
    }
}