46 * always exactly 48 bytes. 47 * 48 */ 49 final class RSAClientKeyExchange extends HandshakeMessage { 50 51 /* 52 * The following field values were encrypted with the server's public 53 * key (or temp key from server key exchange msg) and are presented 54 * here in DECRYPTED form. 55 */ 56 private ProtocolVersion protocolVersion; // preMaster [0,1] 57 SecretKey preMaster; 58 private byte[] encrypted; // same size as public modulus 59 60 /* 61 * Client randomly creates a pre-master secret and encrypts it 62 * using the server's RSA public key; only the server can decrypt 63 * it, using its RSA private key. Result is the same size as the 64 * server's public key, and uses PKCS #1 block format 02. 65 */ 66 RSAClientKeyExchange(ProtocolVersion protocolVersion, 67 ProtocolVersion maxVersion, 68 SecureRandom generator, PublicKey publicKey) throws IOException { 69 if (publicKey.getAlgorithm().equals("RSA") == false) { 70 throw new SSLKeyException("Public key not of type RSA"); 71 } 72 this.protocolVersion = protocolVersion; 73 74 try { 75 String s = ((protocolVersion.v >= ProtocolVersion.TLS12.v) ? 76 "SunTls12RsaPremasterSecret" : "SunTlsRsaPremasterSecret"); 77 KeyGenerator kg = JsseJce.getKeyGenerator(s); 78 kg.init(new TlsRsaPremasterSecretParameterSpec( 79 maxVersion.v, protocolVersion.v), generator); 80 preMaster = kg.generateKey(); 81 82 Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1); 83 cipher.init(Cipher.WRAP_MODE, publicKey, generator); 84 encrypted = cipher.wrap(preMaster); 85 } catch (GeneralSecurityException e) { 86 throw (SSLKeyException)new SSLKeyException 87 ("RSA premaster secret error").initCause(e); 88 } 89 } 90 91 /* 92 * Server gets the PKCS #1 (block format 02) data, decrypts 93 * it with its private key. 94 */ 95 RSAClientKeyExchange(ProtocolVersion currentVersion, 96 ProtocolVersion maxVersion, 97 SecureRandom generator, HandshakeInStream input, 98 int messageSize, PrivateKey privateKey) throws IOException { 99 100 if (privateKey.getAlgorithm().equals("RSA") == false) { 101 throw new SSLKeyException("Private key not of type RSA"); 102 } 103 104 if (currentVersion.v >= ProtocolVersion.TLS10.v) { 105 encrypted = input.getBytes16(); 106 } else { 107 encrypted = new byte [messageSize]; 108 if (input.read(encrypted) != messageSize) { 109 throw new SSLProtocolException( 110 "SSL: read PreMasterSecret: short read"); 111 } 112 } 113 114 try { | 46 * always exactly 48 bytes. 47 * 48 */ 49 final class RSAClientKeyExchange extends HandshakeMessage { 50 51 /* 52 * The following field values were encrypted with the server's public 53 * key (or temp key from server key exchange msg) and are presented 54 * here in DECRYPTED form. 55 */ 56 private ProtocolVersion protocolVersion; // preMaster [0,1] 57 SecretKey preMaster; 58 private byte[] encrypted; // same size as public modulus 59 60 /* 61 * Client randomly creates a pre-master secret and encrypts it 62 * using the server's RSA public key; only the server can decrypt 63 * it, using its RSA private key. Result is the same size as the 64 * server's public key, and uses PKCS #1 block format 02. 65 */ 66 @SuppressWarnings("deprecation") 67 RSAClientKeyExchange(ProtocolVersion protocolVersion, 68 ProtocolVersion maxVersion, 69 SecureRandom generator, PublicKey publicKey) throws IOException { 70 if (publicKey.getAlgorithm().equals("RSA") == false) { 71 throw new SSLKeyException("Public key not of type RSA"); 72 } 73 this.protocolVersion = protocolVersion; 74 75 try { 76 String s = ((protocolVersion.v >= ProtocolVersion.TLS12.v) ? 77 "SunTls12RsaPremasterSecret" : "SunTlsRsaPremasterSecret"); 78 KeyGenerator kg = JsseJce.getKeyGenerator(s); 79 kg.init(new TlsRsaPremasterSecretParameterSpec( 80 maxVersion.v, protocolVersion.v), generator); 81 preMaster = kg.generateKey(); 82 83 Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1); 84 cipher.init(Cipher.WRAP_MODE, publicKey, generator); 85 encrypted = cipher.wrap(preMaster); 86 } catch (GeneralSecurityException e) { 87 throw (SSLKeyException)new SSLKeyException 88 ("RSA premaster secret error").initCause(e); 89 } 90 } 91 92 /* 93 * Server gets the PKCS #1 (block format 02) data, decrypts 94 * it with its private key. 95 */ 96 @SuppressWarnings("deprecation") 97 RSAClientKeyExchange(ProtocolVersion currentVersion, 98 ProtocolVersion maxVersion, 99 SecureRandom generator, HandshakeInStream input, 100 int messageSize, PrivateKey privateKey) throws IOException { 101 102 if (privateKey.getAlgorithm().equals("RSA") == false) { 103 throw new SSLKeyException("Private key not of type RSA"); 104 } 105 106 if (currentVersion.v >= ProtocolVersion.TLS10.v) { 107 encrypted = input.getBytes16(); 108 } else { 109 encrypted = new byte [messageSize]; 110 if (input.read(encrypted) != messageSize) { 111 throw new SSLProtocolException( 112 "SSL: read PreMasterSecret: short read"); 113 } 114 } 115 116 try { |