Blame jdk8203182-pr3603-rh1568033_release_session_if_initialization_of_sunpkcs11_signature_fails.patch

baf0cab
# HG changeset patch
baf0cab
# User igerasim
baf0cab
# Date 1528992969 25200
baf0cab
#      Thu Jun 14 09:16:09 2018 -0700
baf0cab
# Node ID d9b0b4bd2526818afa73b60da77403245554caa8
baf0cab
# Parent  1f4b038b9550afaf88a70cee4cf9c1422ecd86d6
baf0cab
8203182, PR3603: Release session if initialization of SunPKCS11 Signature fails
baf0cab
Summary: Ensure session is properly released in P11Signature class
baf0cab
Reviewed-by: valeriep
baf0cab
Contributed-by: Martin Balao <mbalao@redhat.com>
baf0cab
baf0cab
diff --git openjdk.orig/jdk/src/share/classes/sun/security/pkcs11/P11Signature.java openjdk/jdk/src/share/classes/sun/security/pkcs11/P11Signature.java
baf0cab
--- openjdk.orig/jdk/src/share/classes/sun/security/pkcs11/P11Signature.java
baf0cab
+++ openjdk/jdk/src/share/classes/sun/security/pkcs11/P11Signature.java
baf0cab
@@ -309,47 +309,51 @@
baf0cab
             session = token.killSession(session);
baf0cab
             return;
baf0cab
         }
baf0cab
-        // "cancel" operation by finishing it
baf0cab
-        // XXX make sure all this always works correctly
baf0cab
-        if (mode == M_SIGN) {
baf0cab
-            try {
baf0cab
-                if (type == T_UPDATE) {
baf0cab
-                    token.p11.C_SignFinal(session.id(), 0);
baf0cab
-                } else {
baf0cab
-                    byte[] digest;
baf0cab
-                    if (type == T_DIGEST) {
baf0cab
-                        digest = md.digest();
baf0cab
-                    } else { // T_RAW
baf0cab
-                        digest = buffer;
baf0cab
+        try {
baf0cab
+            // "cancel" operation by finishing it
baf0cab
+            // XXX make sure all this always works correctly
baf0cab
+            if (mode == M_SIGN) {
baf0cab
+                try {
baf0cab
+                    if (type == T_UPDATE) {
baf0cab
+                        token.p11.C_SignFinal(session.id(), 0);
baf0cab
+                    } else {
baf0cab
+                        byte[] digest;
baf0cab
+                        if (type == T_DIGEST) {
baf0cab
+                            digest = md.digest();
baf0cab
+                        } else { // T_RAW
baf0cab
+                            digest = buffer;
baf0cab
+                        }
baf0cab
+                        token.p11.C_Sign(session.id(), digest);
baf0cab
                     }
baf0cab
-                    token.p11.C_Sign(session.id(), digest);
baf0cab
+                } catch (PKCS11Exception e) {
baf0cab
+                    throw new ProviderException("cancel failed", e);
baf0cab
                 }
baf0cab
-            } catch (PKCS11Exception e) {
baf0cab
-                throw new ProviderException("cancel failed", e);
baf0cab
+            } else { // M_VERIFY
baf0cab
+                try {
baf0cab
+                    byte[] signature;
baf0cab
+                    if (keyAlgorithm.equals("DSA")) {
baf0cab
+                        signature = new byte[40];
baf0cab
+                    } else {
baf0cab
+                        signature = new byte[(p11Key.length() + 7) >> 3];
baf0cab
+                    }
baf0cab
+                    if (type == T_UPDATE) {
baf0cab
+                        token.p11.C_VerifyFinal(session.id(), signature);
baf0cab
+                    } else {
baf0cab
+                        byte[] digest;
baf0cab
+                        if (type == T_DIGEST) {
baf0cab
+                            digest = md.digest();
baf0cab
+                        } else { // T_RAW
baf0cab
+                            digest = buffer;
baf0cab
+                        }
baf0cab
+                        token.p11.C_Verify(session.id(), digest, signature);
baf0cab
+                    }
baf0cab
+                } catch (PKCS11Exception e) {
baf0cab
+                    // will fail since the signature is incorrect
baf0cab
+                    // XXX check error code
baf0cab
+                }
baf0cab
             }
baf0cab
-        } else { // M_VERIFY
baf0cab
-            try {
baf0cab
-                byte[] signature;
baf0cab
-                if (keyAlgorithm.equals("DSA")) {
baf0cab
-                    signature = new byte[40];
baf0cab
-                } else {
baf0cab
-                    signature = new byte[(p11Key.length() + 7) >> 3];
baf0cab
-                }
baf0cab
-                if (type == T_UPDATE) {
baf0cab
-                    token.p11.C_VerifyFinal(session.id(), signature);
baf0cab
-                } else {
baf0cab
-                    byte[] digest;
baf0cab
-                    if (type == T_DIGEST) {
baf0cab
-                        digest = md.digest();
baf0cab
-                    } else { // T_RAW
baf0cab
-                        digest = buffer;
baf0cab
-                    }
baf0cab
-                    token.p11.C_Verify(session.id(), digest, signature);
baf0cab
-                }
baf0cab
-            } catch (PKCS11Exception e) {
baf0cab
-                // will fail since the signature is incorrect
baf0cab
-                // XXX check error code
baf0cab
-            }
baf0cab
+        } finally {
baf0cab
+            session = token.releaseSession(session);
baf0cab
         }
baf0cab
     }
baf0cab
 
baf0cab
@@ -368,6 +372,8 @@
baf0cab
             }
baf0cab
             initialized = true;
baf0cab
         } catch (PKCS11Exception e) {
baf0cab
+            // release session when initialization failed
baf0cab
+            session = token.releaseSession(session);
baf0cab
             throw new ProviderException("Initialization failed", e);
baf0cab
         }
baf0cab
         if (bytesProcessed != 0) {
baf0cab
@@ -529,6 +535,8 @@
baf0cab
                 }
baf0cab
                 bytesProcessed += len;
baf0cab
             } catch (PKCS11Exception e) {
baf0cab
+                initialized = false;
baf0cab
+                session = token.releaseSession(session);
baf0cab
                 throw new ProviderException(e);
baf0cab
             }
baf0cab
             break;
baf0cab
@@ -576,6 +584,8 @@
baf0cab
                 bytesProcessed += len;
baf0cab
                 byteBuffer.position(ofs + len);
baf0cab
             } catch (PKCS11Exception e) {
baf0cab
+                initialized = false;
baf0cab
+                session = token.releaseSession(session);
baf0cab
                 throw new ProviderException("Update failed", e);
baf0cab
             }
baf0cab
             break;