< prev index next >

src/java.base/share/classes/sun/security/ssl/RSAClientKeyExchange.java

Print this page




  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.useTLS12PlusSpec() ?
  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.useTLS10PlusSpec()) {
 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         byte[] encoded = null;
 117         try {
 118             boolean needFailover = false;
 119             Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
 120             try {
 121                 // Try UNWRAP_MODE mode firstly.
 122                 cipher.init(Cipher.UNWRAP_MODE, privateKey,
 123                         new TlsRsaPremasterSecretParameterSpec(


 144                 boolean failed = false;
 145                 try {
 146                     encoded = cipher.doFinal(encrypted);
 147                 } catch (BadPaddingException bpe) {
 148                     // Note: encoded == null
 149                     failed = true;
 150                 }
 151                 encoded = KeyUtil.checkTlsPreMasterSecretKey(
 152                                 maxVersion.v, currentVersion.v,
 153                                 generator, encoded, failed);
 154                 preMaster = generatePreMasterSecret(
 155                                 maxVersion.v, currentVersion.v,
 156                                 encoded, generator);
 157             } else {
 158                 // the cipher should have been initialized
 159                 preMaster = (SecretKey)cipher.unwrap(encrypted,
 160                         "TlsRsaPremasterSecret", Cipher.SECRET_KEY);
 161             }
 162         } catch (InvalidKeyException ibk) {
 163             // the message is too big to process with RSA
 164             throw new SSLProtocolException(
 165                 "Unable to process PreMasterSecret, may be too big");
 166         } catch (Exception e) {
 167             // unlikely to happen, otherwise, must be a provider exception
 168             if (debug != null && Debug.isOn("handshake")) {
 169                 System.out.println("RSA premaster secret decryption error:");
 170                 e.printStackTrace(System.out);
 171             }
 172             throw new RuntimeException("Could not generate dummy secret", e);
 173         }
 174     }
 175 
 176     // generate a premaster secret with the specified version number
 177     @SuppressWarnings("deprecation")
 178     private static SecretKey generatePreMasterSecret(
 179             int clientVersion, int serverVersion,
 180             byte[] encodedSecret, SecureRandom generator) {
 181 
 182         if (debug != null && Debug.isOn("handshake")) {
 183             System.out.println("Generating a premaster secret");
 184         }
 185 




  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                 publicKey.getAlgorithm());
  73         }
  74         this.protocolVersion = protocolVersion;
  75 
  76         try {
  77             String s = protocolVersion.useTLS12PlusSpec() ?
  78                 "SunTls12RsaPremasterSecret" : "SunTlsRsaPremasterSecret";
  79             KeyGenerator kg = JsseJce.getKeyGenerator(s);
  80             kg.init(new TlsRsaPremasterSecretParameterSpec(
  81                     maxVersion.v, protocolVersion.v), generator);
  82             preMaster = kg.generateKey();
  83 
  84             Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
  85             cipher.init(Cipher.WRAP_MODE, publicKey, generator);
  86             encrypted = cipher.wrap(preMaster);
  87         } catch (GeneralSecurityException e) {
  88             throw (SSLKeyException)new SSLKeyException
  89                                 ("RSA premaster secret error").initCause(e);
  90         }
  91     }
  92 
  93     /*
  94      * Server gets the PKCS #1 (block format 02) data, decrypts
  95      * it with its private key.
  96      */
  97     @SuppressWarnings("deprecation")
  98     RSAClientKeyExchange(ProtocolVersion currentVersion,
  99             ProtocolVersion maxVersion,
 100             SecureRandom generator, HandshakeInStream input,
 101             int messageSize, PrivateKey privateKey) throws IOException {
 102 
 103         if (privateKey.getAlgorithm().equals("RSA") == false) {
 104             throw new SSLKeyException("Private key not of type RSA: " + 
 105                  privateKey.getAlgorithm());
 106         }
 107 
 108         if (currentVersion.useTLS10PlusSpec()) {
 109             encrypted = input.getBytes16();
 110         } else {
 111             encrypted = new byte [messageSize];
 112             if (input.read(encrypted) != messageSize) {
 113                 throw new SSLProtocolException(
 114                         "SSL: read PreMasterSecret: short read");
 115             }
 116         }
 117 
 118         byte[] encoded = null;
 119         try {
 120             boolean needFailover = false;
 121             Cipher cipher = JsseJce.getCipher(JsseJce.CIPHER_RSA_PKCS1);
 122             try {
 123                 // Try UNWRAP_MODE mode firstly.
 124                 cipher.init(Cipher.UNWRAP_MODE, privateKey,
 125                         new TlsRsaPremasterSecretParameterSpec(


 146                 boolean failed = false;
 147                 try {
 148                     encoded = cipher.doFinal(encrypted);
 149                 } catch (BadPaddingException bpe) {
 150                     // Note: encoded == null
 151                     failed = true;
 152                 }
 153                 encoded = KeyUtil.checkTlsPreMasterSecretKey(
 154                                 maxVersion.v, currentVersion.v,
 155                                 generator, encoded, failed);
 156                 preMaster = generatePreMasterSecret(
 157                                 maxVersion.v, currentVersion.v,
 158                                 encoded, generator);
 159             } else {
 160                 // the cipher should have been initialized
 161                 preMaster = (SecretKey)cipher.unwrap(encrypted,
 162                         "TlsRsaPremasterSecret", Cipher.SECRET_KEY);
 163             }
 164         } catch (InvalidKeyException ibk) {
 165             // the message is too big to process with RSA
 166             throw new SSLException(
 167                 "Unable to process PreMasterSecret", ibk);
 168         } catch (Exception e) {
 169             // unlikely to happen, otherwise, must be a provider exception
 170             if (debug != null && Debug.isOn("handshake")) {
 171                 System.out.println("RSA premaster secret decryption error:");
 172                 e.printStackTrace(System.out);
 173             }
 174             throw new RuntimeException("Could not generate dummy secret", e);
 175         }
 176     }
 177 
 178     // generate a premaster secret with the specified version number
 179     @SuppressWarnings("deprecation")
 180     private static SecretKey generatePreMasterSecret(
 181             int clientVersion, int serverVersion,
 182             byte[] encodedSecret, SecureRandom generator) {
 183 
 184         if (debug != null && Debug.isOn("handshake")) {
 185             System.out.println("Generating a premaster secret");
 186         }
 187 


< prev index next >