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

The following examples show how to use sun.security.util.KeyUtil#checkTlsPreMasterSecretKey() . 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: RSAClientKeyExchange.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
RSAClientKeyExchange(ProtocolVersion currentVersion,
        ProtocolVersion maxVersion,
        SecureRandom generator, HandshakeInStream input,
        int messageSize, PrivateKey privateKey) throws IOException {

    if (privateKey.getAlgorithm().equals("RSA") == false) {
        throw new SSLKeyException("Private key not of type RSA: " +
             privateKey.getAlgorithm());
    }

    if (currentVersion.v >= ProtocolVersion.TLS10.v) {
        encrypted = input.getBytes16();
    } else {
        encrypted = new byte [messageSize];
        if (input.read(encrypted) != messageSize) {
            throw new SSLProtocolException(
                    "SSL: read PreMasterSecret: short read");
        }
    }

    byte[] encoded = null;
    try {
        boolean needFailover = false;
        Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
        try {
            // Try UNWRAP_MODE mode firstly.
            cipher.init(Cipher.UNWRAP_MODE, privateKey,
                    new TlsRsaPremasterSecretParameterSpec(
                            maxVersion.v, currentVersion.v),
                    generator);

            // The provider selection can be delayed, please don't call
            // any Cipher method before the call to Cipher.init().
            needFailover = !KeyUtil.isOracleJCEProvider(
                    cipher.getProvider().getName());
        } catch (InvalidKeyException | UnsupportedOperationException iue) {
            if (debug != null && Debug.isOn("handshake")) {
                System.out.println("The Cipher provider "
                        + safeProviderName(cipher)
                        + " caused exception: " + iue.getMessage());
            }

            needFailover = true;
        }

        if (needFailover) {
            // The cipher might be spoiled by unsuccessful call to init(),
            // so request a fresh instance
            cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);

            // Use DECRYPT_MODE and dispose the previous initialization.
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            boolean failed = false;
            try {
                encoded = cipher.doFinal(encrypted);
            } catch (BadPaddingException bpe) {
                // Note: encoded == null
                failed = true;
            }
            encoded = KeyUtil.checkTlsPreMasterSecretKey(
                            maxVersion.v, currentVersion.v,
                            generator, encoded, failed);
            preMaster = generatePreMasterSecret(
                            maxVersion.v, currentVersion.v,
                            encoded, generator);
        } else {
            // the cipher should have been initialized
            preMaster = (SecretKey)cipher.unwrap(encrypted,
                    "TlsRsaPremasterSecret", Cipher.SECRET_KEY);
        }
    } catch (InvalidKeyException ibk) {
        // the message is too big to process with RSA
        throw new SSLException(
            "Unable to process PreMasterSecret", ibk);
    } catch (Exception e) {
        // unlikely to happen, otherwise, must be a provider exception
        if (debug != null && Debug.isOn("handshake")) {
            System.out.println("RSA premaster secret decryption error:");
            e.printStackTrace(System.out);
        }
        throw new RuntimeException("Could not generate dummy secret", e);
    }
}
 
Example 2
Source File: RSAClientKeyExchange.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
RSAClientKeyExchange(ProtocolVersion currentVersion,
        ProtocolVersion maxVersion,
        SecureRandom generator, HandshakeInStream input,
        int messageSize, PrivateKey privateKey) throws IOException {

    if (privateKey.getAlgorithm().equals("RSA") == false) {
        throw new SSLKeyException("Private key not of type RSA: " +
             privateKey.getAlgorithm());
    }

    if (currentVersion.v >= ProtocolVersion.TLS10.v) {
        encrypted = input.getBytes16();
    } else {
        encrypted = new byte [messageSize];
        if (input.read(encrypted) != messageSize) {
            throw new SSLProtocolException(
                    "SSL: read PreMasterSecret: short read");
        }
    }

    byte[] encoded = null;
    try {
        boolean needFailover = false;
        Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
        try {
            // Try UNWRAP_MODE mode firstly.
            cipher.init(Cipher.UNWRAP_MODE, privateKey,
                    new TlsRsaPremasterSecretParameterSpec(
                            maxVersion.v, currentVersion.v),
                    generator);

            // The provider selection can be delayed, please don't call
            // any Cipher method before the call to Cipher.init().
            needFailover = !KeyUtil.isOracleJCEProvider(
                    cipher.getProvider().getName());
        } catch (InvalidKeyException | UnsupportedOperationException iue) {
            if (debug != null && Debug.isOn("handshake")) {
                System.out.println("The Cipher provider "
                        + safeProviderName(cipher)
                        + " caused exception: " + iue.getMessage());
            }

            needFailover = true;
        }

        if (needFailover) {
            // The cipher might be spoiled by unsuccessful call to init(),
            // so request a fresh instance
            cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);

            // Use DECRYPT_MODE and dispose the previous initialization.
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            boolean failed = false;
            try {
                encoded = cipher.doFinal(encrypted);
            } catch (BadPaddingException bpe) {
                // Note: encoded == null
                failed = true;
            }
            encoded = KeyUtil.checkTlsPreMasterSecretKey(
                            maxVersion.v, currentVersion.v,
                            generator, encoded, failed);
            preMaster = generatePreMasterSecret(
                            maxVersion.v, currentVersion.v,
                            encoded, generator);
        } else {
            // the cipher should have been initialized
            preMaster = (SecretKey)cipher.unwrap(encrypted,
                    "TlsRsaPremasterSecret", Cipher.SECRET_KEY);
        }
    } catch (InvalidKeyException ibk) {
        // the message is too big to process with RSA
        throw new SSLException(
            "Unable to process PreMasterSecret", ibk);
    } catch (Exception e) {
        // unlikely to happen, otherwise, must be a provider exception
        if (debug != null && Debug.isOn("handshake")) {
            System.out.println("RSA premaster secret decryption error:");
            e.printStackTrace(System.out);
        }
        throw new RuntimeException("Could not generate dummy secret", e);
    }
}
 
Example 3
Source File: RSAClientKeyExchange.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
RSAClientKeyExchange(ProtocolVersion currentVersion,
        ProtocolVersion maxVersion,
        SecureRandom generator, HandshakeInStream input,
        int messageSize, PrivateKey privateKey) throws IOException {

    if (privateKey.getAlgorithm().equals("RSA") == false) {
        throw new SSLKeyException("Private key not of type RSA: " +
             privateKey.getAlgorithm());
    }

    if (currentVersion.v >= ProtocolVersion.TLS10.v) {
        encrypted = input.getBytes16();
    } else {
        encrypted = new byte [messageSize];
        if (input.read(encrypted) != messageSize) {
            throw new SSLProtocolException(
                    "SSL: read PreMasterSecret: short read");
        }
    }

    byte[] encoded = null;
    try {
        boolean needFailover = false;
        Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
        try {
            // Try UNWRAP_MODE mode firstly.
            cipher.init(Cipher.UNWRAP_MODE, privateKey,
                    new TlsRsaPremasterSecretParameterSpec(
                            maxVersion.v, currentVersion.v),
                    generator);

            // The provider selection can be delayed, please don't call
            // any Cipher method before the call to Cipher.init().
            needFailover = !KeyUtil.isOracleJCEProvider(
                    cipher.getProvider().getName());
        } catch (InvalidKeyException | UnsupportedOperationException iue) {
            if (debug != null && Debug.isOn("handshake")) {
                System.out.println("The Cipher provider "
                        + safeProviderName(cipher)
                        + " caused exception: " + iue.getMessage());
            }

            needFailover = true;
        }

        if (needFailover) {
            // The cipher might be spoiled by unsuccessful call to init(),
            // so request a fresh instance
            cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);

            // Use DECRYPT_MODE and dispose the previous initialization.
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            boolean failed = false;
            try {
                encoded = cipher.doFinal(encrypted);
            } catch (BadPaddingException bpe) {
                // Note: encoded == null
                failed = true;
            }
            encoded = KeyUtil.checkTlsPreMasterSecretKey(
                            maxVersion.v, currentVersion.v,
                            generator, encoded, failed);
            preMaster = generatePreMasterSecret(
                            maxVersion.v, currentVersion.v,
                            encoded, generator);
        } else {
            // the cipher should have been initialized
            preMaster = (SecretKey)cipher.unwrap(encrypted,
                    "TlsRsaPremasterSecret", Cipher.SECRET_KEY);
        }
    } catch (InvalidKeyException ibk) {
        // the message is too big to process with RSA
        throw new SSLException(
            "Unable to process PreMasterSecret", ibk);
    } catch (Exception e) {
        // unlikely to happen, otherwise, must be a provider exception
        if (debug != null && Debug.isOn("handshake")) {
            System.out.println("RSA premaster secret decryption error:");
            e.printStackTrace(System.out);
        }
        throw new RuntimeException("Could not generate dummy secret", e);
    }
}
 
Example 4
Source File: RSAClientKeyExchange.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
RSAClientKeyExchange(ProtocolVersion currentVersion,
        ProtocolVersion maxVersion,
        SecureRandom generator, HandshakeInStream input,
        int messageSize, PrivateKey privateKey) throws IOException {

    if (privateKey.getAlgorithm().equals("RSA") == false) {
        throw new SSLKeyException("Private key not of type RSA: " +
             privateKey.getAlgorithm());
    }

    if (currentVersion.v >= ProtocolVersion.TLS10.v) {
        encrypted = input.getBytes16();
    } else {
        encrypted = new byte [messageSize];
        if (input.read(encrypted) != messageSize) {
            throw new SSLProtocolException(
                    "SSL: read PreMasterSecret: short read");
        }
    }

    byte[] encoded = null;
    try {
        boolean needFailover = false;
        Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
        try {
            // Try UNWRAP_MODE mode firstly.
            cipher.init(Cipher.UNWRAP_MODE, privateKey,
                    new TlsRsaPremasterSecretParameterSpec(
                            maxVersion.v, currentVersion.v),
                    generator);

            // The provider selection can be delayed, please don't call
            // any Cipher method before the call to Cipher.init().
            needFailover = !KeyUtil.isOracleJCEProvider(
                    cipher.getProvider().getName());
        } catch (InvalidKeyException | UnsupportedOperationException iue) {
            if (debug != null && Debug.isOn("handshake")) {
                System.out.println("The Cipher provider "
                        + safeProviderName(cipher)
                        + " caused exception: " + iue.getMessage());
            }

            needFailover = true;
        }

        if (needFailover) {
            // The cipher might be spoiled by unsuccessful call to init(),
            // so request a fresh instance
            cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);

            // Use DECRYPT_MODE and dispose the previous initialization.
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            boolean failed = false;
            try {
                encoded = cipher.doFinal(encrypted);
            } catch (BadPaddingException bpe) {
                // Note: encoded == null
                failed = true;
            }
            encoded = KeyUtil.checkTlsPreMasterSecretKey(
                            maxVersion.v, currentVersion.v,
                            generator, encoded, failed);
            preMaster = generatePreMasterSecret(
                            maxVersion.v, currentVersion.v,
                            encoded, generator);
        } else {
            // the cipher should have been initialized
            preMaster = (SecretKey)cipher.unwrap(encrypted,
                    "TlsRsaPremasterSecret", Cipher.SECRET_KEY);
        }
    } catch (InvalidKeyException ibk) {
        // the message is too big to process with RSA
        throw new SSLException(
            "Unable to process PreMasterSecret", ibk);
    } catch (Exception e) {
        // unlikely to happen, otherwise, must be a provider exception
        if (debug != null && Debug.isOn("handshake")) {
            System.out.println("RSA premaster secret decryption error:");
            e.printStackTrace(System.out);
        }
        throw new RuntimeException("Could not generate dummy secret", e);
    }
}
 
Example 5
Source File: NativeRSACipher.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
protected synchronized Key engineUnwrap(byte[] wrappedKey,
        String wrappedKeyAlgorithm, int wrappedKeyType)
        throws InvalidKeyException, NoSuchAlgorithmException {

    if (wrappedKey.length > buffer.length) {
        throw new InvalidKeyException("Key is too long for unwrapping." +
            " wrappedKey.length: " + wrappedKey.length +
            ". buffer.length: " + buffer.length);
    }

    boolean isTlsRsaPremasterSecret =
            wrappedKeyAlgorithm.equals("TlsRsaPremasterSecret");
    Exception failover = null;

    byte[] encodedKey = null;
    try {
        encodedKey = engineDoFinal(wrappedKey, 0, wrappedKey.length);
    } catch (BadPaddingException bpe) {
        if (isTlsRsaPremasterSecret) {
            failover = bpe;
        } else {
            throw new InvalidKeyException("Unwrapping failed", bpe);
        }
    } catch (Exception e) {
        throw new InvalidKeyException("Unwrapping failed", e);
    }

    if (isTlsRsaPremasterSecret) {
        if (!(spec instanceof TlsRsaPremasterSecretParameterSpec)) {
            throw new IllegalStateException(
                    "No TlsRsaPremasterSecretParameterSpec specified");
        }

        // polish the TLS premaster secret
        encodedKey = KeyUtil.checkTlsPreMasterSecretKey(
            ((TlsRsaPremasterSecretParameterSpec)spec).getClientVersion(),
            ((TlsRsaPremasterSecretParameterSpec)spec).getServerVersion(),
            random, encodedKey, (failover != null));
    }

    return NativeCipher.constructKey(wrappedKeyType,
            encodedKey, wrappedKeyAlgorithm);
}
 
Example 6
Source File: RSAClientKeyExchange.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
RSAClientKeyExchange(ProtocolVersion currentVersion,
        ProtocolVersion maxVersion,
        SecureRandom generator, HandshakeInStream input,
        int messageSize, PrivateKey privateKey) throws IOException {

    if (privateKey.getAlgorithm().equals("RSA") == false) {
        throw new SSLKeyException("Private key not of type RSA: " +
             privateKey.getAlgorithm());
    }

    if (currentVersion.useTLS10PlusSpec()) {
        encrypted = input.getBytes16();
    } else {
        encrypted = new byte [messageSize];
        if (input.read(encrypted) != messageSize) {
            throw new SSLProtocolException(
                    "SSL: read PreMasterSecret: short read");
        }
    }

    byte[] encoded = null;
    try {
        boolean needFailover = false;
        Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
        try {
            // Try UNWRAP_MODE mode firstly.
            cipher.init(Cipher.UNWRAP_MODE, privateKey,
                    new TlsRsaPremasterSecretParameterSpec(
                            maxVersion.v, currentVersion.v),
                    generator);

            // The provider selection can be delayed, please don't call
            // any Cipher method before the call to Cipher.init().
            needFailover = !KeyUtil.isOracleJCEProvider(
                    cipher.getProvider().getName());
        } catch (InvalidKeyException | UnsupportedOperationException iue) {
            if (debug != null && Debug.isOn("handshake")) {
                System.out.println("The Cipher provider "
                        + safeProviderName(cipher)
                        + " caused exception: " + iue.getMessage());
            }

            needFailover = true;
        }

        if (needFailover) {
            // The cipher might be spoiled by unsuccessful call to init(),
            // so request a fresh instance
            cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);

            // Use DECRYPT_MODE and dispose the previous initialization.
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            boolean failed = false;
            try {
                encoded = cipher.doFinal(encrypted);
            } catch (BadPaddingException bpe) {
                // Note: encoded == null
                failed = true;
            }
            encoded = KeyUtil.checkTlsPreMasterSecretKey(
                            maxVersion.v, currentVersion.v,
                            generator, encoded, failed);
            preMaster = generatePreMasterSecret(
                            maxVersion.v, currentVersion.v,
                            encoded, generator);
        } else {
            // the cipher should have been initialized
            preMaster = (SecretKey)cipher.unwrap(encrypted,
                    "TlsRsaPremasterSecret", Cipher.SECRET_KEY);
        }
    } catch (InvalidKeyException ibk) {
        // the message is too big to process with RSA
        throw new SSLException(
            "Unable to process PreMasterSecret", ibk);
    } catch (Exception e) {
        // unlikely to happen, otherwise, must be a provider exception
        if (debug != null && Debug.isOn("handshake")) {
            System.out.println("RSA premaster secret decryption error:");
            e.printStackTrace(System.out);
        }
        throw new RuntimeException("Could not generate dummy secret", e);
    }
}
 
Example 7
Source File: RSAClientKeyExchange.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
RSAClientKeyExchange(ProtocolVersion currentVersion,
        ProtocolVersion maxVersion,
        SecureRandom generator, HandshakeInStream input,
        int messageSize, PrivateKey privateKey) throws IOException {

    if (privateKey.getAlgorithm().equals("RSA") == false) {
        throw new SSLKeyException("Private key not of type RSA: " +
             privateKey.getAlgorithm());
    }

    if (currentVersion.v >= ProtocolVersion.TLS10.v) {
        encrypted = input.getBytes16();
    } else {
        encrypted = new byte [messageSize];
        if (input.read(encrypted) != messageSize) {
            throw new SSLProtocolException(
                    "SSL: read PreMasterSecret: short read");
        }
    }

    byte[] encoded = null;
    try {
        boolean needFailover = false;
        Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
        try {
            // Try UNWRAP_MODE mode firstly.
            cipher.init(Cipher.UNWRAP_MODE, privateKey,
                    new TlsRsaPremasterSecretParameterSpec(
                            maxVersion.v, currentVersion.v),
                    generator);

            // The provider selection can be delayed, please don't call
            // any Cipher method before the call to Cipher.init().
            needFailover = !KeyUtil.isOracleJCEProvider(
                    cipher.getProvider().getName());
        } catch (InvalidKeyException | UnsupportedOperationException iue) {
            if (debug != null && Debug.isOn("handshake")) {
                System.out.println("The Cipher provider "
                        + safeProviderName(cipher)
                        + " caused exception: " + iue.getMessage());
            }

            needFailover = true;
        }

        if (needFailover) {
            // The cipher might be spoiled by unsuccessful call to init(),
            // so request a fresh instance
            cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);

            // Use DECRYPT_MODE and dispose the previous initialization.
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            boolean failed = false;
            try {
                encoded = cipher.doFinal(encrypted);
            } catch (BadPaddingException bpe) {
                // Note: encoded == null
                failed = true;
            }
            encoded = KeyUtil.checkTlsPreMasterSecretKey(
                            maxVersion.v, currentVersion.v,
                            generator, encoded, failed);
            preMaster = generatePreMasterSecret(
                            maxVersion.v, currentVersion.v,
                            encoded, generator);
        } else {
            // the cipher should have been initialized
            preMaster = (SecretKey)cipher.unwrap(encrypted,
                    "TlsRsaPremasterSecret", Cipher.SECRET_KEY);
        }
    } catch (InvalidKeyException ibk) {
        // the message is too big to process with RSA
        throw new SSLException(
            "Unable to process PreMasterSecret", ibk);
    } catch (Exception e) {
        // unlikely to happen, otherwise, must be a provider exception
        if (debug != null && Debug.isOn("handshake")) {
            System.out.println("RSA premaster secret decryption error:");
            e.printStackTrace(System.out);
        }
        throw new RuntimeException("Could not generate dummy secret", e);
    }
}
 
Example 8
Source File: RSAClientKeyExchange.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
RSAClientKeyExchange(ProtocolVersion currentVersion,
        ProtocolVersion maxVersion,
        SecureRandom generator, HandshakeInStream input,
        int messageSize, PrivateKey privateKey) throws IOException {

    if (privateKey.getAlgorithm().equals("RSA") == false) {
        throw new SSLKeyException("Private key not of type RSA: " +
             privateKey.getAlgorithm());
    }

    if (currentVersion.v >= ProtocolVersion.TLS10.v) {
        encrypted = input.getBytes16();
    } else {
        encrypted = new byte [messageSize];
        if (input.read(encrypted) != messageSize) {
            throw new SSLProtocolException(
                    "SSL: read PreMasterSecret: short read");
        }
    }

    byte[] encoded = null;
    try {
        boolean needFailover = false;
        Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
        try {
            // Try UNWRAP_MODE mode firstly.
            cipher.init(Cipher.UNWRAP_MODE, privateKey,
                    new TlsRsaPremasterSecretParameterSpec(
                            maxVersion.v, currentVersion.v),
                    generator);

            // The provider selection can be delayed, please don't call
            // any Cipher method before the call to Cipher.init().
            needFailover = !KeyUtil.isOracleJCEProvider(
                    cipher.getProvider().getName());
        } catch (InvalidKeyException | UnsupportedOperationException iue) {
            if (debug != null && Debug.isOn("handshake")) {
                System.out.println("The Cipher provider " +
                    cipher.getProvider().getName() +
                    " caused exception: " + iue.getMessage());
            }

            needFailover = true;
        }

        if (needFailover) {
            // Use DECRYPT_MODE and dispose the previous initialization.
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            boolean failed = false;
            try {
                encoded = cipher.doFinal(encrypted);
            } catch (BadPaddingException bpe) {
                // Note: encoded == null
                failed = true;
            }
            encoded = KeyUtil.checkTlsPreMasterSecretKey(
                            maxVersion.v, currentVersion.v,
                            generator, encoded, failed);
            preMaster = generatePreMasterSecret(
                            maxVersion.v, currentVersion.v,
                            encoded, generator);
        } else {
            // the cipher should have been initialized
            preMaster = (SecretKey)cipher.unwrap(encrypted,
                    "TlsRsaPremasterSecret", Cipher.SECRET_KEY);
        }
    } catch (InvalidKeyException ibk) {
        // the message is too big to process with RSA
        throw new SSLException(
            "Unable to process PreMasterSecret", ibk);
    } catch (Exception e) {
        // unlikely to happen, otherwise, must be a provider exception
        if (debug != null && Debug.isOn("handshake")) {
            System.out.println("RSA premaster secret decryption error:");
            e.printStackTrace(System.out);
        }
        throw new RuntimeException("Could not generate dummy secret", e);
    }
}
 
Example 9
Source File: RSAClientKeyExchange.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
RSAClientKeyExchange(ProtocolVersion currentVersion,
        ProtocolVersion maxVersion,
        SecureRandom generator, HandshakeInStream input,
        int messageSize, PrivateKey privateKey) throws IOException {

    if (privateKey.getAlgorithm().equals("RSA") == false) {
        throw new SSLKeyException("Private key not of type RSA: " +
             privateKey.getAlgorithm());
    }

    if (currentVersion.v >= ProtocolVersion.TLS10.v) {
        encrypted = input.getBytes16();
    } else {
        encrypted = new byte [messageSize];
        if (input.read(encrypted) != messageSize) {
            throw new SSLProtocolException(
                    "SSL: read PreMasterSecret: short read");
        }
    }

    byte[] encoded = null;
    try {
        boolean needFailover = false;
        Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
        try {
            // Try UNWRAP_MODE mode firstly.
            cipher.init(Cipher.UNWRAP_MODE, privateKey,
                    new TlsRsaPremasterSecretParameterSpec(
                            maxVersion.v, currentVersion.v),
                    generator);

            // The provider selection can be delayed, please don't call
            // any Cipher method before the call to Cipher.init().
            needFailover = !KeyUtil.isOracleJCEProvider(
                    cipher.getProvider().getName());
        } catch (InvalidKeyException | UnsupportedOperationException iue) {
            if (debug != null && Debug.isOn("handshake")) {
                System.out.println("The Cipher provider "
                        + safeProviderName(cipher)
                        + " caused exception: " + iue.getMessage());
            }

            needFailover = true;
        }

        if (needFailover) {
            // The cipher might be spoiled by unsuccessful call to init(),
            // so request a fresh instance
            cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);

            // Use DECRYPT_MODE and dispose the previous initialization.
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            boolean failed = false;
            try {
                encoded = cipher.doFinal(encrypted);
            } catch (BadPaddingException bpe) {
                // Note: encoded == null
                failed = true;
            }
            encoded = KeyUtil.checkTlsPreMasterSecretKey(
                            maxVersion.v, currentVersion.v,
                            generator, encoded, failed);
            preMaster = generatePreMasterSecret(
                            maxVersion.v, currentVersion.v,
                            encoded, generator);
        } else {
            // the cipher should have been initialized
            preMaster = (SecretKey)cipher.unwrap(encrypted,
                    "TlsRsaPremasterSecret", Cipher.SECRET_KEY);
        }
    } catch (InvalidKeyException ibk) {
        // the message is too big to process with RSA
        throw new SSLException(
            "Unable to process PreMasterSecret", ibk);
    } catch (Exception e) {
        // unlikely to happen, otherwise, must be a provider exception
        if (debug != null && Debug.isOn("handshake")) {
            System.out.println("RSA premaster secret decryption error:");
            e.printStackTrace(System.out);
        }
        throw new RuntimeException("Could not generate dummy secret", e);
    }
}