当前位置: 首页>>代码示例>>Java>>正文


Java KeyUtil.checkTlsPreMasterSecretKey方法代码示例

本文整理汇总了Java中sun.security.util.KeyUtil.checkTlsPreMasterSecretKey方法的典型用法代码示例。如果您正苦于以下问题:Java KeyUtil.checkTlsPreMasterSecretKey方法的具体用法?Java KeyUtil.checkTlsPreMasterSecretKey怎么用?Java KeyUtil.checkTlsPreMasterSecretKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sun.security.util.KeyUtil的用法示例。


在下文中一共展示了KeyUtil.checkTlsPreMasterSecretKey方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: engineUnwrap

import sun.security.util.KeyUtil; //导入方法依赖的package包/类
@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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:46,代码来源:NativeRSACipher.java

示例2: RSAClientKeyExchange

import sun.security.util.KeyUtil; //导入方法依赖的package包/类
@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 " +
                    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);
    }
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:81,代码来源:RSAClientKeyExchange.java


注:本文中的sun.security.util.KeyUtil.checkTlsPreMasterSecretKey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。