javax.crypto.spec.DHPublicKeySpec Java Examples

The following examples show how to use javax.crypto.spec.DHPublicKeySpec. 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 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 #2
Source File: HandshakeMessage.java    From TencentKona-8 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: DHKeyFactory.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a public key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the public key
 *
 * @return the public key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected PublicKey engineGeneratePublic(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPublicKeySpec) {
            DHPublicKeySpec dhPubKeySpec = (DHPublicKeySpec)keySpec;
            return new DHPublicKey(dhPubKeySpec.getY(),
                                   dhPubKeySpec.getP(),
                                   dhPubKeySpec.getG());

        } else if (keySpec instanceof X509EncodedKeySpec) {
            return new DHPublicKey
                (((X509EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #4
Source File: DHPublicKeySpecTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * DHPublicKeySpec class testing. Tests the equivalence of parameters
 * specified in the constructor with the values returned by getters.
 */
public void testDHPrivateKeySpec() {
    BigInteger[] ys = {new BigInteger("-1000000000000"), BigInteger.ZERO,
                        BigInteger.ONE, new BigInteger("1000000000000")};
    BigInteger[] ps = {new BigInteger("-1000000000000"), BigInteger.ZERO,
                        BigInteger.ONE, new BigInteger("1000000000000")};
    BigInteger[] gs = {new BigInteger("-1000000000000"), BigInteger.ZERO,
                        BigInteger.ONE, new BigInteger("1000000000000")};
    for (int i=0; i<ps.length; i++) {
        DHPublicKeySpec dhpks = new DHPublicKeySpec(ys[i], ps[i], gs[i]);
        assertEquals("The value returned by getY() must be "
                    + "equal to the value specified in the constructor",
                    dhpks.getY(), ys[i]);
        assertEquals("The value returned by getP() must be "
                    + "equal to the value specified in the constructor",
                    dhpks.getP(), ps[i]);
        assertEquals("The value returned by getG() must be "
                    + "equal to the value specified in the constructor",
                    dhpks.getG(), gs[i]);
    }
}
 
Example #5
Source File: DHKeyFactory.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a public key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the public key
 *
 * @return the public key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected PublicKey engineGeneratePublic(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPublicKeySpec) {
            DHPublicKeySpec dhPubKeySpec = (DHPublicKeySpec)keySpec;
            return new DHPublicKey(dhPubKeySpec.getY(),
                                   dhPubKeySpec.getP(),
                                   dhPubKeySpec.getG());

        } else if (keySpec instanceof X509EncodedKeySpec) {
            return new DHPublicKey
                (((X509EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #6
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 #7
Source File: DHKeyExchange.java    From openjsse with GNU General Public License v2.0 6 votes vote down vote up
static DHECredentials valueOf(NamedGroup ng,
    byte[] encodedPublic) throws IOException, GeneralSecurityException {

    if (ng.type != NamedGroupType.NAMED_GROUP_FFDHE) {
        throw new RuntimeException(
                "Credentials decoding:  Not FFDHE named group");
    }

    if (encodedPublic == null || encodedPublic.length == 0) {
        return null;
    }

    DHParameterSpec params = (DHParameterSpec)ng.getParameterSpec();
    if (params == null) {
        return null;
    }

    KeyFactory kf = JsseJce.getKeyFactory("DiffieHellman");
    DHPublicKeySpec spec = new DHPublicKeySpec(
            new BigInteger(1, encodedPublic),
            params.getP(), params.getG());
    DHPublicKey publicKey =
            (DHPublicKey)kf.generatePublic(spec);

    return new DHECredentials(publicKey, ng);
}
 
Example #8
Source File: HandshakeMessage.java    From jdk8u60 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 #9
Source File: DHKeyFactory.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a public key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the public key
 *
 * @return the public key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected PublicKey engineGeneratePublic(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPublicKeySpec) {
            DHPublicKeySpec dhPubKeySpec = (DHPublicKeySpec)keySpec;
            return new DHPublicKey(dhPubKeySpec.getY(),
                                   dhPubKeySpec.getP(),
                                   dhPubKeySpec.getG());

        } else if (keySpec instanceof X509EncodedKeySpec) {
            return new DHPublicKey
                (((X509EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #10
Source File: DHKeyFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a public key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the public key
 *
 * @return the public key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected PublicKey engineGeneratePublic(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPublicKeySpec) {
            DHPublicKeySpec dhPubKeySpec = (DHPublicKeySpec)keySpec;
            return new DHPublicKey(dhPubKeySpec.getY(),
                                   dhPubKeySpec.getP(),
                                   dhPubKeySpec.getG());

        } else if (keySpec instanceof X509EncodedKeySpec) {
            return new DHPublicKey
                (((X509EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #11
Source File: HandshakeMessage.java    From openjdk-jdk8u-backup 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 #12
Source File: DHKeyFactory.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a public key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the public key
 *
 * @return the public key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected PublicKey engineGeneratePublic(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPublicKeySpec) {
            DHPublicKeySpec dhPubKeySpec = (DHPublicKeySpec)keySpec;
            return new DHPublicKey(dhPubKeySpec.getY(),
                                   dhPubKeySpec.getP(),
                                   dhPubKeySpec.getG());

        } else if (keySpec instanceof X509EncodedKeySpec) {
            return new DHPublicKey
                (((X509EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #13
Source File: DiffieHellmanSession.java    From openid4java with Apache License 2.0 6 votes vote down vote up
protected DHPublicKey stringToPublicKey(String publicKeyBase64)
{
    try
    {
        byte[] yBinary = Base64.decodeBase64(publicKeyBase64.getBytes());
        BigInteger y = new BigInteger(yBinary);

        DHPublicKeySpec dhPublicKeySpec = new DHPublicKeySpec(
                y, _dhParameterSpec.getP(), _dhParameterSpec.getG() );

        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);

        return (DHPublicKey) keyFactory.generatePublic(dhPublicKeySpec);
    }
    catch (GeneralSecurityException e)
    {
        _log.error("Cannot create PublicKey object from: " + publicKeyBase64, e);

        return null;
    }
}
 
Example #14
Source File: DHKeyFactory.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a public key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the public key
 *
 * @return the public key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected PublicKey engineGeneratePublic(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPublicKeySpec) {
            DHPublicKeySpec dhPubKeySpec = (DHPublicKeySpec)keySpec;
            return new DHPublicKey(dhPubKeySpec.getY(),
                                   dhPubKeySpec.getP(),
                                   dhPubKeySpec.getG());

        } else if (keySpec instanceof X509EncodedKeySpec) {
            return new DHPublicKey
                (((X509EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #15
Source File: HandshakeMessage.java    From openjdk-8-source 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 #16
Source File: DHKeyFactory.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a public key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the public key
 *
 * @return the public key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected PublicKey engineGeneratePublic(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPublicKeySpec) {
            DHPublicKeySpec dhPubKeySpec = (DHPublicKeySpec)keySpec;
            return new DHPublicKey(dhPubKeySpec.getY(),
                                   dhPubKeySpec.getP(),
                                   dhPubKeySpec.getG());

        } else if (keySpec instanceof X509EncodedKeySpec) {
            return new DHPublicKey
                (((X509EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #17
Source File: HandshakeMessage.java    From hottub 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 #18
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 #19
Source File: DHKeyFactory.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a public key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the public key
 *
 * @return the public key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected PublicKey engineGeneratePublic(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPublicKeySpec) {
            DHPublicKeySpec dhPubKeySpec = (DHPublicKeySpec)keySpec;
            return new DHPublicKey(dhPubKeySpec.getY(),
                                   dhPubKeySpec.getP(),
                                   dhPubKeySpec.getG());

        } else if (keySpec instanceof X509EncodedKeySpec) {
            return new DHPublicKey
                (((X509EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #20
Source File: DHKeyFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a public key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the public key
 *
 * @return the public key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected PublicKey engineGeneratePublic(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPublicKeySpec) {
            DHPublicKeySpec dhPubKeySpec = (DHPublicKeySpec)keySpec;
            return new DHPublicKey(dhPubKeySpec.getY(),
                                   dhPubKeySpec.getP(),
                                   dhPubKeySpec.getG());

        } else if (keySpec instanceof X509EncodedKeySpec) {
            return new DHPublicKey
                (((X509EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #21
Source File: HandshakeMessage.java    From openjdk-jdk9 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 #22
Source File: DHKeyFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a public key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the public key
 *
 * @return the public key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected PublicKey engineGeneratePublic(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPublicKeySpec) {
            DHPublicKeySpec dhPubKeySpec = (DHPublicKeySpec)keySpec;
            return new DHPublicKey(dhPubKeySpec.getY(),
                                   dhPubKeySpec.getP(),
                                   dhPubKeySpec.getG());

        } else if (keySpec instanceof X509EncodedKeySpec) {
            return new DHPublicKey
                (((X509EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
Example #23
Source File: DHKeyFactory.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = engineGetKeySpec(key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = engineGetKeySpec(key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key", e);
    }
}
 
Example #24
Source File: KeyUtil.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the key spec is valid or not.
 * <P>
 * Note that this method is only apply to DHPublicKeySpec at present.
 *
 * @param  keySpec
 *         the key spec object, cannot be null
 *
 * @throws NullPointerException if {@code keySpec} is null
 * @throws InvalidKeyException if {@code keySpec} is invalid
 */
public static final void validate(KeySpec keySpec)
        throws InvalidKeyException {
    if (keySpec == null) {
        throw new NullPointerException(
            "The key spec to be validated cannot be null");
    }

    if (keySpec instanceof DHPublicKeySpec) {
        validateDHPublicKey((DHPublicKeySpec)keySpec);
    }
}
 
Example #25
Source File: KeyUtil.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the key spec is valid or not.
 * <P>
 * Note that this method is only apply to DHPublicKeySpec at present.
 *
 * @param  keySpec
 *         the key spec object, cannot be null
 *
 * @throws NullPointerException if {@code keySpec} is null
 * @throws InvalidKeyException if {@code keySpec} is invalid
 */
public static final void validate(KeySpec keySpec)
        throws InvalidKeyException {
    if (keySpec == null) {
        throw new NullPointerException(
            "The key spec to be validated cannot be null");
    }

    if (keySpec instanceof DHPublicKeySpec) {
        validateDHPublicKey((DHPublicKeySpec)keySpec);
    }
}
 
Example #26
Source File: KeyUtil.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the key spec is valid or not.
 * <P>
 * Note that this method is only apply to DHPublicKeySpec at present.
 *
 * @param  keySpec
 *         the key spec object, cannot be null
 *
 * @throws NullPointerException if {@code keySpec} is null
 * @throws InvalidKeyException if {@code keySpec} is invalid
 */
public static final void validate(KeySpec keySpec)
        throws InvalidKeyException {
    if (keySpec == null) {
        throw new NullPointerException(
            "The key spec to be validated cannot be null");
    }

    if (keySpec instanceof DHPublicKeySpec) {
        validateDHPublicKey((DHPublicKeySpec)keySpec);
    }
}
 
Example #27
Source File: KeyUtil.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the key spec is valid or not.
 * <P>
 * Note that this method is only apply to DHPublicKeySpec at present.
 *
 * @param  keySpec
 *         the key spec object, cannot be null
 *
 * @throws NullPointerException if {@code keySpec} is null
 * @throws InvalidKeyException if {@code keySpec} is invalid
 */
public static final void validate(KeySpec keySpec)
        throws InvalidKeyException {
    if (keySpec == null) {
        throw new NullPointerException(
            "The key spec to be validated cannot be null");
    }

    if (keySpec instanceof DHPublicKeySpec) {
        validateDHPublicKey((DHPublicKeySpec)keySpec);
    }
}
 
Example #28
Source File: DHKeyFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = engineGetKeySpec(key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = engineGetKeySpec(key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key", e);
    }
}
 
Example #29
Source File: DHKeyFactory.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = engineGetKeySpec(key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = engineGetKeySpec(key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key", e);
    }
}
 
Example #30
Source File: KeyFactorySpi.java    From ripple-lib-java with ISC License 5 votes vote down vote up
protected PublicKey engineGeneratePublic(
    KeySpec keySpec)
    throws InvalidKeySpecException
{
    if (keySpec instanceof ElGamalPublicKeySpec)
    {
        return new BCElGamalPublicKey((ElGamalPublicKeySpec)keySpec);
    }
    else if (keySpec instanceof DHPublicKeySpec)
    {
        return new BCElGamalPublicKey((DHPublicKeySpec)keySpec);
    }
    return super.engineGeneratePublic(keySpec);
}