< prev index next >

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

Print this page

        

@@ -24,11 +24,10 @@
  */
 
 package sun.security.ssl;
 
 import java.security.*;
-import java.util.concurrent.locks.ReentrantLock;
 
 /**
  * The "KeyManager" for ephemeral RSA keys. Ephemeral DH and ECDH keys
  * are handled by the DHCrypt and ECDHCrypt classes, respectively.
  *

@@ -47,12 +46,10 @@
     private final EphemeralKeyPair[] keys = new EphemeralKeyPair[] {
         new EphemeralKeyPair(null),
         new EphemeralKeyPair(null),
     };
 
-    private final ReentrantLock cachedKeysLock = new ReentrantLock();
-
     EphemeralKeyManager() {
         // empty
     }
 
     /*

@@ -66,36 +63,24 @@
         } else {
             length = 1024;
             index = INDEX_RSA1024;
         }
 
-        KeyPair kp = keys[index].getKeyPair();
-        if (kp != null) {
-            return kp;
-        }
-
-        cachedKeysLock.lock();
-        try {
-            // double check
-            kp = keys[index].getKeyPair();
-            if (kp != null) {
-                return kp;
+        synchronized (keys) {
+            KeyPair kp = keys[index].getKeyPair();
+            if (kp == null) {
+                try {
+                    KeyPairGenerator kgen = KeyPairGenerator.getInstance("RSA");
+                    kgen.initialize(length, random);
+                    keys[index] = new EphemeralKeyPair(kgen.genKeyPair());
+                    kp = keys[index].getKeyPair();
+                } catch (Exception e) {
+                    // ignore
+                }
             }
-
-            try {
-                KeyPairGenerator kgen = KeyPairGenerator.getInstance("RSA");
-                kgen.initialize(length, random);
-                keys[index] = new EphemeralKeyPair(kgen.genKeyPair());
-                kp = keys[index].getKeyPair();
-            } catch (Exception e) {
-                // ignore
-            }
-        } finally {
-            cachedKeysLock.unlock();
+            return kp;
         }
-
-        return kp;
     }
 
     /**
      * Inner class to handle storage of ephemeral KeyPairs.
      */
< prev index next >