Java Code Examples for sun.security.util.KeyUtil#validate()

The following examples show how to use sun.security.util.KeyUtil#validate() . 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: HandshakeMessage.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
DH_ServerKeyExchange(HandshakeInStream input,
        ProtocolVersion protocolVersion)
        throws IOException, GeneralSecurityException {

    this.protocolVersion = protocolVersion;
    this.preferableSignatureAlgorithm = null;

    dh_p = input.getBytes16();
    dh_g = input.getBytes16();
    dh_Ys = input.getBytes16();
    KeyUtil.validate(new DHPublicKeySpec(new BigInteger(1, dh_Ys),
                                         new BigInteger(1, dh_p),
                                         new BigInteger(1, dh_g)));

    signature = null;
}
 
Example 2
Source File: HandshakeMessage.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
DH_ServerKeyExchange(HandshakeInStream input,
        ProtocolVersion protocolVersion)
        throws IOException, GeneralSecurityException {

    this.protocolVersion = protocolVersion;
    this.preferableSignatureAlgorithm = null;

    dh_p = input.getBytes16();
    dh_g = input.getBytes16();
    dh_Ys = input.getBytes16();
    KeyUtil.validate(new DHPublicKeySpec(new BigInteger(1, dh_Ys),
                                         new BigInteger(1, dh_p),
                                         new BigInteger(1, dh_g)));

    signature = null;
}
 
Example 3
Source File: HandshakeMessage.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
DH_ServerKeyExchange(HandshakeInStream input,
        ProtocolVersion protocolVersion)
        throws IOException, GeneralSecurityException {

    this.protocolVersion = protocolVersion;
    this.preferableSignatureAlgorithm = null;

    dh_p = input.getBytes16();
    dh_g = input.getBytes16();
    dh_Ys = input.getBytes16();
    KeyUtil.validate(new DHPublicKeySpec(new BigInteger(1, dh_Ys),
                                         new BigInteger(1, dh_p),
                                         new BigInteger(1, dh_g)));

    signature = null;
}
 
Example 4
Source File: HandshakeMessage.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
DH_ServerKeyExchange(HandshakeInStream input,
        ProtocolVersion protocolVersion)
        throws IOException, GeneralSecurityException {

    this.protocolVersion = protocolVersion;
    this.preferableSignatureAlgorithm = null;

    dh_p = input.getBytes16();
    dh_g = input.getBytes16();
    dh_Ys = input.getBytes16();
    KeyUtil.validate(new DHPublicKeySpec(new BigInteger(1, dh_Ys),
                                         new BigInteger(1, dh_p),
                                         new BigInteger(1, dh_g)));

    signature = null;
}
 
Example 5
Source File: DHCrypt.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private DHPublicKeySpec generateDHPublicKeySpec(KeyPairGenerator kpg)
        throws GeneralSecurityException {

    boolean doExtraValiadtion =
                (!KeyUtil.isOracleJCEProvider(kpg.getProvider().getName()));
    for (int i = 0; i <= MAX_FAILOVER_TIMES; i++) {
        KeyPair kp = kpg.generateKeyPair();
        privateKey = kp.getPrivate();
        DHPublicKeySpec spec = getDHPublicKeySpec(kp.getPublic());

        // validate the Diffie-Hellman public key
        if (doExtraValiadtion) {
            try {
                KeyUtil.validate(spec);
            } catch (InvalidKeyException ivke) {
                if (i == MAX_FAILOVER_TIMES) {
                    throw ivke;
                }
                // otherwise, ignore the exception and try the next one
                continue;
            }
        }

        return spec;
    }

    return null;
}
 
Example 6
Source File: DHCrypt.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the secret data that has been agreed on through Diffie-Hellman
 * key agreement protocol.  Note that in the two party protocol, if
 * the peer keys are already known, no other data needs to be sent in
 * order to agree on a secret.  That is, a secured message may be
 * sent without any mandatory round-trip overheads.
 *
 * <P>It is illegal to call this member function if the private key
 * has not been set (or generated).
 *
 * @param  peerPublicKey the peer's public key.
 * @param  keyIsValidated whether the {@code peerPublicKey} has beed
 *         validated
 * @return the secret, which is an unsigned big-endian integer
 *         the same size as the Diffie-Hellman modulus.
 */
SecretKey getAgreedSecret(BigInteger peerPublicValue,
        boolean keyIsValidated) throws SSLHandshakeException {
    try {
        KeyFactory kf = JsseJce.getKeyFactory("DiffieHellman");
        DHPublicKeySpec spec =
                    new DHPublicKeySpec(peerPublicValue, modulus, base);
        PublicKey publicKey = kf.generatePublic(spec);
        KeyAgreement ka = JsseJce.getKeyAgreement("DiffieHellman");

        // validate the Diffie-Hellman public key
        if (!keyIsValidated &&
                !KeyUtil.isOracleJCEProvider(ka.getProvider().getName())) {
            try {
                KeyUtil.validate(spec);
            } catch (InvalidKeyException ike) {
                // prefer handshake_failure alert to internal_error alert
                throw new SSLHandshakeException(ike.getMessage());
            }
        }

        ka.init(privateKey);
        ka.doPhase(publicKey, true);
        return ka.generateSecret("TlsPremasterSecret");
    } catch (GeneralSecurityException e) {
        throw (SSLHandshakeException) new SSLHandshakeException(
            "Could not generate secret").initCause(e);
    }
}
 
Example 7
Source File: DHCrypt.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private DHPublicKeySpec generateDHPublicKeySpec(KeyPairGenerator kpg)
        throws GeneralSecurityException {

    boolean doExtraValiadtion =
                (!KeyUtil.isOracleJCEProvider(kpg.getProvider().getName()));
    for (int i = 0; i <= MAX_FAILOVER_TIMES; i++) {
        KeyPair kp = kpg.generateKeyPair();
        privateKey = kp.getPrivate();
        DHPublicKeySpec spec = getDHPublicKeySpec(kp.getPublic());

        // validate the Diffie-Hellman public key
        if (doExtraValiadtion) {
            try {
                KeyUtil.validate(spec);
            } catch (InvalidKeyException ivke) {
                if (i == MAX_FAILOVER_TIMES) {
                    throw ivke;
                }
                // otherwise, ignore the exception and try the next one
                continue;
            }
        }

        return spec;
    }

    return null;
}
 
Example 8
Source File: DHCrypt.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private DHPublicKeySpec generateDHPublicKeySpec(KeyPairGenerator kpg)
        throws GeneralSecurityException {

    boolean doExtraValiadtion =
                (!KeyUtil.isOracleJCEProvider(kpg.getProvider().getName()));
    for (int i = 0; i <= MAX_FAILOVER_TIMES; i++) {
        KeyPair kp = kpg.generateKeyPair();
        privateKey = kp.getPrivate();
        DHPublicKeySpec spec = getDHPublicKeySpec(kp.getPublic());

        // validate the Diffie-Hellman public key
        if (doExtraValiadtion) {
            try {
                KeyUtil.validate(spec);
            } catch (InvalidKeyException ivke) {
                if (i == MAX_FAILOVER_TIMES) {
                    throw ivke;
                }
                // otherwise, ignore the exception and try the next one
                continue;
            }
        }

        return spec;
    }

    return null;
}
 
Example 9
Source File: DHCrypt.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the secret data that has been agreed on through Diffie-Hellman
 * key agreement protocol.  Note that in the two party protocol, if
 * the peer keys are already known, no other data needs to be sent in
 * order to agree on a secret.  That is, a secured message may be
 * sent without any mandatory round-trip overheads.
 *
 * <P>It is illegal to call this member function if the private key
 * has not been set (or generated).
 *
 * @param  peerPublicKey the peer's public key.
 * @param  keyIsValidated whether the {@code peerPublicKey} has beed
 *         validated
 * @return the secret, which is an unsigned big-endian integer
 *         the same size as the Diffie-Hellman modulus.
 */
SecretKey getAgreedSecret(BigInteger peerPublicValue,
        boolean keyIsValidated) throws SSLHandshakeException {
    try {
        KeyFactory kf = JsseJce.getKeyFactory("DiffieHellman");
        DHPublicKeySpec spec =
                    new DHPublicKeySpec(peerPublicValue, modulus, base);
        PublicKey publicKey = kf.generatePublic(spec);
        KeyAgreement ka = JsseJce.getKeyAgreement("DiffieHellman");

        // validate the Diffie-Hellman public key
        if (!keyIsValidated &&
                !KeyUtil.isOracleJCEProvider(ka.getProvider().getName())) {
            try {
                KeyUtil.validate(spec);
            } catch (InvalidKeyException ike) {
                // prefer handshake_failure alert to internal_error alert
                throw new SSLHandshakeException(ike.getMessage());
            }
        }

        ka.init(privateKey);
        ka.doPhase(publicKey, true);
        return ka.generateSecret("TlsPremasterSecret");
    } catch (GeneralSecurityException e) {
        throw (SSLHandshakeException) new SSLHandshakeException(
            "Could not generate secret").initCause(e);
    }
}
 
Example 10
Source File: DHCrypt.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the secret data that has been agreed on through Diffie-Hellman
 * key agreement protocol.  Note that in the two party protocol, if
 * the peer keys are already known, no other data needs to be sent in
 * order to agree on a secret.  That is, a secured message may be
 * sent without any mandatory round-trip overheads.
 *
 * <P>It is illegal to call this member function if the private key
 * has not been set (or generated).
 *
 * @param  peerPublicKey the peer's public key.
 * @param  keyIsValidated whether the {@code peerPublicKey} has beed
 *         validated
 * @return the secret, which is an unsigned big-endian integer
 *         the same size as the Diffie-Hellman modulus.
 */
SecretKey getAgreedSecret(BigInteger peerPublicValue,
        boolean keyIsValidated) throws SSLHandshakeException {
    try {
        KeyFactory kf = JsseJce.getKeyFactory("DiffieHellman");
        DHPublicKeySpec spec =
                    new DHPublicKeySpec(peerPublicValue, modulus, base);
        PublicKey publicKey = kf.generatePublic(spec);
        KeyAgreement ka = JsseJce.getKeyAgreement("DiffieHellman");

        // validate the Diffie-Hellman public key
        if (!keyIsValidated &&
                !KeyUtil.isOracleJCEProvider(ka.getProvider().getName())) {
            try {
                KeyUtil.validate(spec);
            } catch (InvalidKeyException ike) {
                // prefer handshake_failure alert to internal_error alert
                throw new SSLHandshakeException(ike.getMessage());
            }
        }

        ka.init(privateKey);
        ka.doPhase(publicKey, true);
        return ka.generateSecret("TlsPremasterSecret");
    } catch (GeneralSecurityException e) {
        throw (SSLHandshakeException) new SSLHandshakeException(
            "Could not generate secret").initCause(e);
    }
}
 
Example 11
Source File: DHCrypt.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private DHPublicKeySpec generateDHPublicKeySpec(KeyPairGenerator kpg)
        throws GeneralSecurityException {

    boolean doExtraValiadtion =
                (!KeyUtil.isOracleJCEProvider(kpg.getProvider().getName()));
    for (int i = 0; i <= MAX_FAILOVER_TIMES; i++) {
        KeyPair kp = kpg.generateKeyPair();
        privateKey = kp.getPrivate();
        DHPublicKeySpec spec = getDHPublicKeySpec(kp.getPublic());

        // validate the Diffie-Hellman public key
        if (doExtraValiadtion) {
            try {
                KeyUtil.validate(spec);
            } catch (InvalidKeyException ivke) {
                if (i == MAX_FAILOVER_TIMES) {
                    throw ivke;
                }
                // otherwise, ignore the exception and try the next one
                continue;
            }
        }

        return spec;
    }

    return null;
}
 
Example 12
Source File: DHCrypt.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the secret data that has been agreed on through Diffie-Hellman
 * key agreement protocol.  Note that in the two party protocol, if
 * the peer keys are already known, no other data needs to be sent in
 * order to agree on a secret.  That is, a secured message may be
 * sent without any mandatory round-trip overheads.
 *
 * <P>It is illegal to call this member function if the private key
 * has not been set (or generated).
 *
 * @param  peerPublicKey the peer's public key.
 * @param  keyIsValidated whether the {@code peerPublicKey} has beed
 *         validated
 * @return the secret, which is an unsigned big-endian integer
 *         the same size as the Diffie-Hellman modulus.
 */
SecretKey getAgreedSecret(BigInteger peerPublicValue,
        boolean keyIsValidated) throws SSLHandshakeException {
    try {
        KeyFactory kf = JsseJce.getKeyFactory("DiffieHellman");
        DHPublicKeySpec spec =
                    new DHPublicKeySpec(peerPublicValue, modulus, base);
        PublicKey publicKey = kf.generatePublic(spec);
        KeyAgreement ka = JsseJce.getKeyAgreement("DiffieHellman");

        // validate the Diffie-Hellman public key
        if (!keyIsValidated &&
                !KeyUtil.isOracleJCEProvider(ka.getProvider().getName())) {
            try {
                KeyUtil.validate(spec);
            } catch (InvalidKeyException ike) {
                // prefer handshake_failure alert to internal_error alert
                throw new SSLHandshakeException(ike.getMessage());
            }
        }

        ka.init(privateKey);
        ka.doPhase(publicKey, true);
        return ka.generateSecret("TlsPremasterSecret");
    } catch (GeneralSecurityException e) {
        throw (SSLHandshakeException) new SSLHandshakeException(
            "Could not generate secret").initCause(e);
    }
}
 
Example 13
Source File: DHCrypt.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the secret data that has been agreed on through Diffie-Hellman
 * key agreement protocol.  Note that in the two party protocol, if
 * the peer keys are already known, no other data needs to be sent in
 * order to agree on a secret.  That is, a secured message may be
 * sent without any mandatory round-trip overheads.
 *
 * <P>It is illegal to call this member function if the private key
 * has not been set (or generated).
 *
 * @param  peerPublicKey the peer's public key.
 * @param  keyIsValidated whether the {@code peerPublicKey} has beed
 *         validated
 * @return the secret, which is an unsigned big-endian integer
 *         the same size as the Diffie-Hellman modulus.
 */
SecretKey getAgreedSecret(BigInteger peerPublicValue,
        boolean keyIsValidated) throws SSLHandshakeException {
    try {
        KeyFactory kf = JsseJce.getKeyFactory("DiffieHellman");
        DHPublicKeySpec spec =
                    new DHPublicKeySpec(peerPublicValue, modulus, base);
        PublicKey publicKey = kf.generatePublic(spec);
        KeyAgreement ka = JsseJce.getKeyAgreement("DiffieHellman");

        // validate the Diffie-Hellman public key
        if (!keyIsValidated &&
                !KeyUtil.isOracleJCEProvider(ka.getProvider().getName())) {
            try {
                KeyUtil.validate(spec);
            } catch (InvalidKeyException ike) {
                // prefer handshake_failure alert to internal_error alert
                throw new SSLHandshakeException(ike.getMessage());
            }
        }

        ka.init(privateKey);
        ka.doPhase(publicKey, true);
        return ka.generateSecret("TlsPremasterSecret");
    } catch (GeneralSecurityException e) {
        throw (SSLHandshakeException) new SSLHandshakeException(
            "Could not generate secret").initCause(e);
    }
}
 
Example 14
Source File: DHCrypt.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private DHPublicKeySpec generateDHPublicKeySpec(KeyPairGenerator kpg)
        throws GeneralSecurityException {

    boolean doExtraValiadtion =
                (!KeyUtil.isOracleJCEProvider(kpg.getProvider().getName()));
    for (int i = 0; i <= MAX_FAILOVER_TIMES; i++) {
        KeyPair kp = kpg.generateKeyPair();
        privateKey = kp.getPrivate();
        DHPublicKeySpec spec = getDHPublicKeySpec(kp.getPublic());

        // validate the Diffie-Hellman public key
        if (doExtraValiadtion) {
            try {
                KeyUtil.validate(spec);
            } catch (InvalidKeyException ivke) {
                if (i == MAX_FAILOVER_TIMES) {
                    throw ivke;
                }
                // otherwise, ignore the exception and try the next one
                continue;
            }
        }

        return spec;
    }

    return null;
}
 
Example 15
Source File: DHKeyAgreement.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Executes the next phase of this key agreement with the given
 * key that was received from one of the other parties involved in this key
 * agreement.
 *
 * @param key the key for this phase. For example, in the case of
 * Diffie-Hellman between 2 parties, this would be the other party's
 * Diffie-Hellman public key.
 * @param lastPhase flag which indicates whether or not this is the last
 * phase of this key agreement.
 *
 * @return the (intermediate) key resulting from this phase, or null if
 * this phase does not yield a key
 *
 * @exception InvalidKeyException if the given key is inappropriate for
 * this phase.
 * @exception IllegalStateException if this key agreement has not been
 * initialized.
 */
protected Key engineDoPhase(Key key, boolean lastPhase)
    throws InvalidKeyException, IllegalStateException
{
    if (!(key instanceof javax.crypto.interfaces.DHPublicKey)) {
        throw new InvalidKeyException("Diffie-Hellman public key "
                                      + "expected");
    }
    javax.crypto.interfaces.DHPublicKey dhPubKey;
    dhPubKey = (javax.crypto.interfaces.DHPublicKey)key;

    if (init_p == null || init_g == null) {
        throw new IllegalStateException("Not initialized");
    }

    // check if public key parameters are compatible with
    // initialized ones
    BigInteger pub_p = dhPubKey.getParams().getP();
    BigInteger pub_g = dhPubKey.getParams().getG();
    if (pub_p != null && !(init_p.equals(pub_p))) {
        throw new InvalidKeyException("Incompatible parameters");
    }
    if (pub_g != null && !(init_g.equals(pub_g))) {
        throw new InvalidKeyException("Incompatible parameters");
    }

    // validate the Diffie-Hellman public key
    KeyUtil.validate(dhPubKey);

    // store the y value
    this.y = dhPubKey.getY();

    // we've received a public key (from one of the other parties),
    // so we are ready to create the secret, which may be an
    // intermediate secret, in which case we wrap it into a
    // Diffie-Hellman public key object and return it.
    generateSecret = true;
    if (lastPhase == false) {
        byte[] intermediate = engineGenerateSecret();
        return new DHPublicKey(new BigInteger(1, intermediate),
                               init_p, init_g);
    } else {
        return null;
    }
}
 
Example 16
Source File: DHKeyAgreement.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Executes the next phase of this key agreement with the given
 * key that was received from one of the other parties involved in this key
 * agreement.
 *
 * @param key the key for this phase. For example, in the case of
 * Diffie-Hellman between 2 parties, this would be the other party's
 * Diffie-Hellman public key.
 * @param lastPhase flag which indicates whether or not this is the last
 * phase of this key agreement.
 *
 * @return the (intermediate) key resulting from this phase, or null if
 * this phase does not yield a key
 *
 * @exception InvalidKeyException if the given key is inappropriate for
 * this phase.
 * @exception IllegalStateException if this key agreement has not been
 * initialized.
 */
protected Key engineDoPhase(Key key, boolean lastPhase)
    throws InvalidKeyException, IllegalStateException
{
    if (!(key instanceof javax.crypto.interfaces.DHPublicKey)) {
        throw new InvalidKeyException("Diffie-Hellman public key "
                                      + "expected");
    }
    javax.crypto.interfaces.DHPublicKey dhPubKey;
    dhPubKey = (javax.crypto.interfaces.DHPublicKey)key;

    if (init_p == null || init_g == null) {
        throw new IllegalStateException("Not initialized");
    }

    // check if public key parameters are compatible with
    // initialized ones
    BigInteger pub_p = dhPubKey.getParams().getP();
    BigInteger pub_g = dhPubKey.getParams().getG();
    if (pub_p != null && !(init_p.equals(pub_p))) {
        throw new InvalidKeyException("Incompatible parameters");
    }
    if (pub_g != null && !(init_g.equals(pub_g))) {
        throw new InvalidKeyException("Incompatible parameters");
    }

    // validate the Diffie-Hellman public key
    KeyUtil.validate(dhPubKey);

    // store the y value
    this.y = dhPubKey.getY();

    // we've received a public key (from one of the other parties),
    // so we are ready to create the secret, which may be an
    // intermediate secret, in which case we wrap it into a
    // Diffie-Hellman public key object and return it.
    generateSecret = true;
    if (lastPhase == false) {
        byte[] intermediate = engineGenerateSecret();
        return new DHPublicKey(new BigInteger(1, intermediate),
                               init_p, init_g);
    } else {
        return null;
    }
}
 
Example 17
Source File: DHKeyAgreement.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Executes the next phase of this key agreement with the given
 * key that was received from one of the other parties involved in this key
 * agreement.
 *
 * @param key the key for this phase. For example, in the case of
 * Diffie-Hellman between 2 parties, this would be the other party's
 * Diffie-Hellman public key.
 * @param lastPhase flag which indicates whether or not this is the last
 * phase of this key agreement.
 *
 * @return the (intermediate) key resulting from this phase, or null if
 * this phase does not yield a key
 *
 * @exception InvalidKeyException if the given key is inappropriate for
 * this phase.
 * @exception IllegalStateException if this key agreement has not been
 * initialized.
 */
protected Key engineDoPhase(Key key, boolean lastPhase)
    throws InvalidKeyException, IllegalStateException
{
    if (!(key instanceof javax.crypto.interfaces.DHPublicKey)) {
        throw new InvalidKeyException("Diffie-Hellman public key "
                                      + "expected");
    }
    javax.crypto.interfaces.DHPublicKey dhPubKey;
    dhPubKey = (javax.crypto.interfaces.DHPublicKey)key;

    if (init_p == null || init_g == null) {
        throw new IllegalStateException("Not initialized");
    }

    // check if public key parameters are compatible with
    // initialized ones
    BigInteger pub_p = dhPubKey.getParams().getP();
    BigInteger pub_g = dhPubKey.getParams().getG();
    if (pub_p != null && !(init_p.equals(pub_p))) {
        throw new InvalidKeyException("Incompatible parameters");
    }
    if (pub_g != null && !(init_g.equals(pub_g))) {
        throw new InvalidKeyException("Incompatible parameters");
    }

    // validate the Diffie-Hellman public key
    KeyUtil.validate(dhPubKey);

    // store the y value
    this.y = dhPubKey.getY();

    // we've received a public key (from one of the other parties),
    // so we are ready to create the secret, which may be an
    // intermediate secret, in which case we wrap it into a
    // Diffie-Hellman public key object and return it.
    generateSecret = true;
    if (lastPhase == false) {
        byte[] intermediate = engineGenerateSecret();
        return new DHPublicKey(new BigInteger(1, intermediate),
                               init_p, init_g);
    } else {
        return null;
    }
}
 
Example 18
Source File: DHKeyAgreement.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Executes the next phase of this key agreement with the given
 * key that was received from one of the other parties involved in this key
 * agreement.
 *
 * @param key the key for this phase. For example, in the case of
 * Diffie-Hellman between 2 parties, this would be the other party's
 * Diffie-Hellman public key.
 * @param lastPhase flag which indicates whether or not this is the last
 * phase of this key agreement.
 *
 * @return the (intermediate) key resulting from this phase, or null if
 * this phase does not yield a key
 *
 * @exception InvalidKeyException if the given key is inappropriate for
 * this phase.
 * @exception IllegalStateException if this key agreement has not been
 * initialized.
 */
protected Key engineDoPhase(Key key, boolean lastPhase)
    throws InvalidKeyException, IllegalStateException
{
    if (!(key instanceof javax.crypto.interfaces.DHPublicKey)) {
        throw new InvalidKeyException("Diffie-Hellman public key "
                                      + "expected");
    }
    javax.crypto.interfaces.DHPublicKey dhPubKey;
    dhPubKey = (javax.crypto.interfaces.DHPublicKey)key;

    if (init_p == null || init_g == null) {
        throw new IllegalStateException("Not initialized");
    }

    // check if public key parameters are compatible with
    // initialized ones
    BigInteger pub_p = dhPubKey.getParams().getP();
    BigInteger pub_g = dhPubKey.getParams().getG();
    if (pub_p != null && !(init_p.equals(pub_p))) {
        throw new InvalidKeyException("Incompatible parameters");
    }
    if (pub_g != null && !(init_g.equals(pub_g))) {
        throw new InvalidKeyException("Incompatible parameters");
    }

    // validate the Diffie-Hellman public key
    KeyUtil.validate(dhPubKey);

    // store the y value
    this.y = dhPubKey.getY();

    // we've received a public key (from one of the other parties),
    // so we are ready to create the secret, which may be an
    // intermediate secret, in which case we wrap it into a
    // Diffie-Hellman public key object and return it.
    generateSecret = true;
    if (lastPhase == false) {
        byte[] intermediate = engineGenerateSecret();
        return new DHPublicKey(new BigInteger(1, intermediate),
                               init_p, init_g);
    } else {
        return null;
    }
}
 
Example 19
Source File: DHKeyAgreement.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Executes the next phase of this key agreement with the given
 * key that was received from one of the other parties involved in this key
 * agreement.
 *
 * @param key the key for this phase. For example, in the case of
 * Diffie-Hellman between 2 parties, this would be the other party's
 * Diffie-Hellman public key.
 * @param lastPhase flag which indicates whether or not this is the last
 * phase of this key agreement.
 *
 * @return the (intermediate) key resulting from this phase, or null if
 * this phase does not yield a key
 *
 * @exception InvalidKeyException if the given key is inappropriate for
 * this phase.
 * @exception IllegalStateException if this key agreement has not been
 * initialized.
 */
protected Key engineDoPhase(Key key, boolean lastPhase)
    throws InvalidKeyException, IllegalStateException
{
    if (!(key instanceof javax.crypto.interfaces.DHPublicKey)) {
        throw new InvalidKeyException("Diffie-Hellman public key "
                                      + "expected");
    }
    javax.crypto.interfaces.DHPublicKey dhPubKey;
    dhPubKey = (javax.crypto.interfaces.DHPublicKey)key;

    if (init_p == null || init_g == null) {
        throw new IllegalStateException("Not initialized");
    }

    // check if public key parameters are compatible with
    // initialized ones
    BigInteger pub_p = dhPubKey.getParams().getP();
    BigInteger pub_g = dhPubKey.getParams().getG();
    if (pub_p != null && !(init_p.equals(pub_p))) {
        throw new InvalidKeyException("Incompatible parameters");
    }
    if (pub_g != null && !(init_g.equals(pub_g))) {
        throw new InvalidKeyException("Incompatible parameters");
    }

    // validate the Diffie-Hellman public key
    KeyUtil.validate(dhPubKey);

    // store the y value
    this.y = dhPubKey.getY();

    // we've received a public key (from one of the other parties),
    // so we are ready to create the secret, which may be an
    // intermediate secret, in which case we wrap it into a
    // Diffie-Hellman public key object and return it.
    generateSecret = true;
    if (lastPhase == false) {
        byte[] intermediate = engineGenerateSecret();
        return new DHPublicKey(new BigInteger(1, intermediate),
                               init_p, init_g);
    } else {
        return null;
    }
}
 
Example 20
Source File: DHKeyAgreement.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Executes the next phase of this key agreement with the given
 * key that was received from one of the other parties involved in this key
 * agreement.
 *
 * @param key the key for this phase. For example, in the case of
 * Diffie-Hellman between 2 parties, this would be the other party's
 * Diffie-Hellman public key.
 * @param lastPhase flag which indicates whether or not this is the last
 * phase of this key agreement.
 *
 * @return the (intermediate) key resulting from this phase, or null if
 * this phase does not yield a key
 *
 * @exception InvalidKeyException if the given key is inappropriate for
 * this phase.
 * @exception IllegalStateException if this key agreement has not been
 * initialized.
 */
protected Key engineDoPhase(Key key, boolean lastPhase)
    throws InvalidKeyException, IllegalStateException
{
    if (!(key instanceof javax.crypto.interfaces.DHPublicKey)) {
        throw new InvalidKeyException("Diffie-Hellman public key "
                                      + "expected");
    }
    javax.crypto.interfaces.DHPublicKey dhPubKey;
    dhPubKey = (javax.crypto.interfaces.DHPublicKey)key;

    if (init_p == null || init_g == null) {
        throw new IllegalStateException("Not initialized");
    }

    // check if public key parameters are compatible with
    // initialized ones
    BigInteger pub_p = dhPubKey.getParams().getP();
    BigInteger pub_g = dhPubKey.getParams().getG();
    if (pub_p != null && !(init_p.equals(pub_p))) {
        throw new InvalidKeyException("Incompatible parameters");
    }
    if (pub_g != null && !(init_g.equals(pub_g))) {
        throw new InvalidKeyException("Incompatible parameters");
    }

    // validate the Diffie-Hellman public key
    KeyUtil.validate(dhPubKey);

    // store the y value
    this.y = dhPubKey.getY();

    // we've received a public key (from one of the other parties),
    // so we are ready to create the secret, which may be an
    // intermediate secret, in which case we wrap it into a
    // Diffie-Hellman public key object and return it.
    generateSecret = true;
    if (lastPhase == false) {
        byte[] intermediate = engineGenerateSecret();
        return new DHPublicKey(new BigInteger(1, intermediate),
                               init_p, init_g);
    } else {
        return null;
    }
}