Blame SOURCES/jdk8236512-pkcs11_incorrrect_session_closure.patch

2a959b
# HG changeset patch
2a959b
# User valeriep
2a959b
# Date 1581468987 0
2a959b
#      Wed Feb 12 00:56:27 2020 +0000
2a959b
# Node ID e47d22d82b0464720ccb7641e290080972b6ce88
2a959b
# Parent  5c41dc4c48f85e5a1e1ce6e3836b54674f273367
2a959b
8236512: PKCS11 Connection closed after Cipher.doFinal and NoPadding
2a959b
Summary: Removed killSession() calls in certain impl classes when cancelling operations
2a959b
Reviewed-by: xuelei
2a959b
2a959b
diff --git a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11AEADCipher.java b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11AEADCipher.java
2a959b
--- a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11AEADCipher.java
2a959b
+++ b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11AEADCipher.java
2a959b
@@ -1,4 +1,5 @@
2a959b
-/* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2a959b
+/*
2a959b
+ * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
2a959b
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2a959b
  *
2a959b
  * This code is free software; you can redistribute it and/or modify it
2a959b
@@ -334,25 +335,25 @@
2a959b
     }
2a959b
 
2a959b
     private void cancelOperation() {
2a959b
+        // cancel operation by finishing it; avoid killSession as some
2a959b
+        // hardware vendors may require re-login
2a959b
+        int bufLen = doFinalLength(0);
2a959b
+        byte[] buffer = new byte[bufLen];
2a959b
+        byte[] in = dataBuffer.toByteArray();
2a959b
+        int inLen = in.length;
2a959b
         try {
2a959b
-            if (session.hasObjects() == false) {
2a959b
-                session = token.killSession(session);
2a959b
-                return;
2a959b
+            if (encrypt) {
2a959b
+                token.p11.C_Encrypt(session.id(), 0, in, 0, inLen,
2a959b
+                        0, buffer, 0, bufLen);
2a959b
             } else {
2a959b
-                // cancel operation by finishing it
2a959b
-                int bufLen = doFinalLength(0);
2a959b
-                byte[] buffer = new byte[bufLen];
2a959b
-
2a959b
-                if (encrypt) {
2a959b
-                    token.p11.C_Encrypt(session.id(), 0, buffer, 0, bufLen,
2a959b
-                            0, buffer, 0, bufLen);
2a959b
-                } else {
2a959b
-                    token.p11.C_Decrypt(session.id(), 0, buffer, 0, bufLen,
2a959b
-                            0, buffer, 0, bufLen);
2a959b
-                }
2a959b
+                token.p11.C_Decrypt(session.id(), 0, in, 0, inLen,
2a959b
+                        0, buffer, 0, bufLen);
2a959b
             }
2a959b
         } catch (PKCS11Exception e) {
2a959b
-            throw new ProviderException("Cancel failed", e);
2a959b
+            if (encrypt) {
2a959b
+                throw new ProviderException("Cancel failed", e);
2a959b
+            }
2a959b
+            // ignore failure for decryption
2a959b
         }
2a959b
     }
2a959b
 
2a959b
@@ -434,18 +435,21 @@
2a959b
         if (!initialized) {
2a959b
             return;
2a959b
         }
2a959b
+        initialized = false;
2a959b
+
2a959b
         try {
2a959b
             if (session == null) {
2a959b
                 return;
2a959b
             }
2a959b
+
2a959b
             if (doCancel && token.explicitCancel) {
2a959b
                 cancelOperation();
2a959b
             }
2a959b
         } finally {
2a959b
             p11Key.releaseKeyID();
2a959b
             session = token.releaseSession(session);
2a959b
+            dataBuffer.reset();
2a959b
         }
2a959b
-        initialized = false;
2a959b
     }
2a959b
 
2a959b
     // see JCE spec
2a959b
diff --git a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Cipher.java b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Cipher.java
2a959b
--- a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Cipher.java
2a959b
+++ b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Cipher.java
2a959b
@@ -1,5 +1,5 @@
2a959b
 /*
2a959b
- * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2a959b
+ * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
2a959b
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2a959b
  *
2a959b
  * This code is free software; you can redistribute it and/or modify it
2a959b
@@ -409,10 +409,12 @@
2a959b
             return;
2a959b
         }
2a959b
         initialized = false;
2a959b
+
2a959b
         try {
2a959b
             if (session == null) {
2a959b
                 return;
2a959b
             }
2a959b
+
2a959b
             if (doCancel && token.explicitCancel) {
2a959b
                 cancelOperation();
2a959b
             }
2a959b
@@ -426,22 +428,21 @@
2a959b
 
2a959b
     private void cancelOperation() {
2a959b
         token.ensureValid();
2a959b
-        if (session.hasObjects() == false) {
2a959b
-            session = token.killSession(session);
2a959b
-            return;
2a959b
-        } else {
2a959b
-            try {
2a959b
-                // cancel operation by finishing it
2a959b
-                int bufLen = doFinalLength(0);
2a959b
-                byte[] buffer = new byte[bufLen];
2a959b
-                if (encrypt) {
2a959b
-                    token.p11.C_EncryptFinal(session.id(), 0, buffer, 0, bufLen);
2a959b
-                } else {
2a959b
-                    token.p11.C_DecryptFinal(session.id(), 0, buffer, 0, bufLen);
2a959b
-                }
2a959b
-            } catch (PKCS11Exception e) {
2a959b
+        // cancel operation by finishing it; avoid killSession as some
2a959b
+        // hardware vendors may require re-login
2a959b
+        try {
2a959b
+            int bufLen = doFinalLength(0);
2a959b
+            byte[] buffer = new byte[bufLen];
2a959b
+            if (encrypt) {
2a959b
+                token.p11.C_EncryptFinal(session.id(), 0, buffer, 0, bufLen);
2a959b
+            } else {
2a959b
+                token.p11.C_DecryptFinal(session.id(), 0, buffer, 0, bufLen);
2a959b
+            }
2a959b
+        } catch (PKCS11Exception e) {
2a959b
+            if (encrypt) {
2a959b
                 throw new ProviderException("Cancel failed", e);
2a959b
             }
2a959b
+            // ignore failure for decryption
2a959b
         }
2a959b
     }
2a959b
 
2a959b
diff --git a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Mac.java b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Mac.java
2a959b
--- a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Mac.java
2a959b
+++ b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Mac.java
2a959b
@@ -1,5 +1,5 @@
2a959b
 /*
2a959b
- * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
2a959b
+ * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
2a959b
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2a959b
  *
2a959b
  * This code is free software; you can redistribute it and/or modify it
2a959b
@@ -124,10 +124,12 @@
2a959b
             return;
2a959b
         }
2a959b
         initialized = false;
2a959b
+
2a959b
         try {
2a959b
             if (session == null) {
2a959b
                 return;
2a959b
             }
2a959b
+
2a959b
             if (doCancel && token.explicitCancel) {
2a959b
                 cancelOperation();
2a959b
             }
2a959b
@@ -139,15 +141,12 @@
2a959b
 
2a959b
     private void cancelOperation() {
2a959b
         token.ensureValid();
2a959b
-        if (session.hasObjects() == false) {
2a959b
-            session = token.killSession(session);
2a959b
-            return;
2a959b
-        } else {
2a959b
-            try {
2a959b
-                token.p11.C_SignFinal(session.id(), 0);
2a959b
-            } catch (PKCS11Exception e) {
2a959b
-                throw new ProviderException("Cancel failed", e);
2a959b
-            }
2a959b
+        // cancel operation by finishing it; avoid killSession as some
2a959b
+        // hardware vendors may require re-login
2a959b
+        try {
2a959b
+            token.p11.C_SignFinal(session.id(), 0);
2a959b
+        } catch (PKCS11Exception e) {
2a959b
+            throw new ProviderException("Cancel failed", e);
2a959b
         }
2a959b
     }
2a959b
 
2a959b
@@ -209,7 +208,6 @@
2a959b
             ensureInitialized();
2a959b
             return token.p11.C_SignFinal(session.id(), 0);
2a959b
         } catch (PKCS11Exception e) {
2a959b
-            reset(true);
2a959b
             throw new ProviderException("doFinal() failed", e);
2a959b
         } finally {
2a959b
             reset(false);
2a959b
diff --git a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11PSSSignature.java b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11PSSSignature.java
2a959b
--- a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11PSSSignature.java
2a959b
+++ b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11PSSSignature.java
2a959b
@@ -1,5 +1,5 @@
2a959b
 /*
2a959b
- * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2a959b
+ * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
2a959b
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2a959b
  *
2a959b
  * This code is free software; you can redistribute it and/or modify it
2a959b
@@ -223,10 +223,12 @@
2a959b
             return;
2a959b
         }
2a959b
         initialized = false;
2a959b
+
2a959b
         try {
2a959b
             if (session == null) {
2a959b
                 return;
2a959b
             }
2a959b
+
2a959b
             if (doCancel && token.explicitCancel) {
2a959b
                 cancelOperation();
2a959b
             }
2a959b
@@ -242,14 +244,10 @@
2a959b
         token.ensureValid();
2a959b
         if (DEBUG) System.out.print("Cancelling operation");
2a959b
 
2a959b
-        if (session.hasObjects() == false) {
2a959b
-            if (DEBUG) System.out.println(" by killing session");
2a959b
-            session = token.killSession(session);
2a959b
-            return;
2a959b
-        }
2a959b
-        // "cancel" operation by finishing it
2a959b
-        if (mode == M_SIGN) {
2a959b
-            try {
2a959b
+        // cancel operation by finishing it; avoid killSession as some
2a959b
+        // hardware vendors may require re-login
2a959b
+        try {
2a959b
+            if (mode == M_SIGN) {
2a959b
                 if (type == T_UPDATE) {
2a959b
                     if (DEBUG) System.out.println(" by C_SignFinal");
2a959b
                     token.p11.C_SignFinal(session.id(), 0);
2a959b
@@ -259,11 +257,7 @@
2a959b
                     if (DEBUG) System.out.println(" by C_Sign");
2a959b
                     token.p11.C_Sign(session.id(), digest);
2a959b
                 }
2a959b
-            } catch (PKCS11Exception e) {
2a959b
-                throw new ProviderException("cancel failed", e);
2a959b
-            }
2a959b
-        } else { // M_VERIFY
2a959b
-            try {
2a959b
+            } else { // M_VERIFY
2a959b
                 byte[] signature =
2a959b
                     new byte[(p11Key.length() + 7) >> 3];
2a959b
                 if (type == T_UPDATE) {
2a959b
@@ -275,10 +269,12 @@
2a959b
                     if (DEBUG) System.out.println(" by C_Verify");
2a959b
                     token.p11.C_Verify(session.id(), digest, signature);
2a959b
                 }
2a959b
-            } catch (PKCS11Exception e) {
2a959b
-                // will fail since the signature is incorrect
2a959b
-                // XXX check error code
2a959b
             }
2a959b
+        } catch (PKCS11Exception e) {
2a959b
+            if (mode == M_SIGN) {
2a959b
+                throw new ProviderException("cancel failed", e);
2a959b
+            }
2a959b
+            // ignore failure for verification
2a959b
         }
2a959b
     }
2a959b
 
2a959b
diff --git a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11RSACipher.java b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11RSACipher.java
2a959b
--- a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11RSACipher.java
2a959b
+++ b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11RSACipher.java
2a959b
@@ -1,5 +1,5 @@
2a959b
 /*
2a959b
- * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
2a959b
+ * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
2a959b
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2a959b
  *
2a959b
  * This code is free software; you can redistribute it and/or modify it
2a959b
@@ -247,10 +247,12 @@
2a959b
             return;
2a959b
         }
2a959b
         initialized = false;
2a959b
+
2a959b
         try {
2a959b
             if (session == null) {
2a959b
                 return;
2a959b
             }
2a959b
+
2a959b
             if (doCancel && token.explicitCancel) {
2a959b
                 cancelOperation();
2a959b
             }
2a959b
@@ -264,36 +266,33 @@
2a959b
     // state variables such as "initialized"
2a959b
     private void cancelOperation() {
2a959b
         token.ensureValid();
2a959b
-        if (session.hasObjects() == false) {
2a959b
-            session = token.killSession(session);
2a959b
-            return;
2a959b
-        } else {
2a959b
-            try {
2a959b
-                PKCS11 p11 = token.p11;
2a959b
-                int inLen = maxInputSize;
2a959b
-                int outLen = buffer.length;
2a959b
-                long sessId = session.id();
2a959b
-                switch (mode) {
2a959b
-                case MODE_ENCRYPT:
2a959b
-                    p11.C_Encrypt(sessId, 0, buffer, 0, inLen, 0, buffer, 0, outLen);
2a959b
-                    break;
2a959b
-                case MODE_DECRYPT:
2a959b
-                    p11.C_Decrypt(sessId, 0, buffer, 0, inLen, 0, buffer, 0, outLen);
2a959b
-                    break;
2a959b
-                case MODE_SIGN:
2a959b
-                    byte[] tmpBuffer = new byte[maxInputSize];
2a959b
-                    p11.C_Sign(sessId, tmpBuffer);
2a959b
-                    break;
2a959b
-                case MODE_VERIFY:
2a959b
-                    p11.C_VerifyRecover(sessId, buffer, 0, inLen, buffer,
2a959b
-                            0, outLen);
2a959b
-                    break;
2a959b
-                default:
2a959b
-                    throw new ProviderException("internal error");
2a959b
-                }
2a959b
-            } catch (PKCS11Exception e) {
2a959b
-                // XXX ensure this always works, ignore error
2a959b
+        // cancel operation by finishing it; avoid killSession as some
2a959b
+        // hardware vendors may require re-login
2a959b
+        try {
2a959b
+            PKCS11 p11 = token.p11;
2a959b
+            int inLen = maxInputSize;
2a959b
+            int outLen = buffer.length;
2a959b
+            long sessId = session.id();
2a959b
+            switch (mode) {
2a959b
+            case MODE_ENCRYPT:
2a959b
+                p11.C_Encrypt(sessId, 0, buffer, 0, inLen, 0, buffer, 0, outLen);
2a959b
+                break;
2a959b
+            case MODE_DECRYPT:
2a959b
+                p11.C_Decrypt(sessId, 0, buffer, 0, inLen, 0, buffer, 0, outLen);
2a959b
+                break;
2a959b
+            case MODE_SIGN:
2a959b
+                byte[] tmpBuffer = new byte[maxInputSize];
2a959b
+                p11.C_Sign(sessId, tmpBuffer);
2a959b
+                break;
2a959b
+            case MODE_VERIFY:
2a959b
+                p11.C_VerifyRecover(sessId, buffer, 0, inLen, buffer,
2a959b
+                        0, outLen);
2a959b
+                break;
2a959b
+            default:
2a959b
+                throw new ProviderException("internal error");
2a959b
             }
2a959b
+        } catch (PKCS11Exception e) {
2a959b
+            // XXX ensure this always works, ignore error
2a959b
         }
2a959b
     }
2a959b
 
2a959b
@@ -362,6 +361,7 @@
2a959b
     private int implDoFinal(byte[] out, int outOfs, int outLen)
2a959b
             throws BadPaddingException, IllegalBlockSizeException {
2a959b
         if (bufOfs > maxInputSize) {
2a959b
+            reset(true);
2a959b
             throw new IllegalBlockSizeException("Data must not be longer "
2a959b
                 + "than " + maxInputSize + " bytes");
2a959b
         }
2a959b
diff --git a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Signature.java b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Signature.java
2a959b
--- a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Signature.java
2a959b
+++ b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Signature.java
2a959b
@@ -1,5 +1,5 @@
2a959b
 /*
2a959b
- * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
2a959b
+ * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
2a959b
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2a959b
  *
2a959b
  * This code is free software; you can redistribute it and/or modify it
2a959b
@@ -270,10 +270,12 @@
2a959b
             return;
2a959b
         }
2a959b
         initialized = false;
2a959b
+
2a959b
         try {
2a959b
             if (session == null) {
2a959b
                 return;
2a959b
             }
2a959b
+
2a959b
             if (doCancel && token.explicitCancel) {
2a959b
                 cancelOperation();
2a959b
             }
2a959b
@@ -284,59 +286,51 @@
2a959b
     }
2a959b
 
2a959b
     private void cancelOperation() {
2a959b
-
2a959b
         token.ensureValid();
2a959b
-        if (session.hasObjects() == false) {
2a959b
-            session = token.killSession(session);
2a959b
-            return;
2a959b
-        } else {
2a959b
-            // "cancel" operation by finishing it
2a959b
-            // XXX make sure all this always works correctly
2a959b
+        // cancel operation by finishing it; avoid killSession as some
2a959b
+        // hardware vendors may require re-login
2a959b
+        try {
2a959b
             if (mode == M_SIGN) {
2a959b
-                try {
2a959b
-                    if (type == T_UPDATE) {
2a959b
-                        token.p11.C_SignFinal(session.id(), 0);
2a959b
-                    } else {
2a959b
-                        byte[] digest;
2a959b
-                        if (type == T_DIGEST) {
2a959b
-                            digest = md.digest();
2a959b
-                        } else { // T_RAW
2a959b
-                            digest = buffer;
2a959b
-                        }
2a959b
-                        token.p11.C_Sign(session.id(), digest);
2a959b
+                if (type == T_UPDATE) {
2a959b
+                    token.p11.C_SignFinal(session.id(), 0);
2a959b
+                } else {
2a959b
+                    byte[] digest;
2a959b
+                    if (type == T_DIGEST) {
2a959b
+                        digest = md.digest();
2a959b
+                    } else { // T_RAW
2a959b
+                        digest = buffer;
2a959b
                     }
2a959b
-                } catch (PKCS11Exception e) {
2a959b
-                    throw new ProviderException("cancel failed", e);
2a959b
+                    token.p11.C_Sign(session.id(), digest);
2a959b
                 }
2a959b
             } else { // M_VERIFY
2a959b
                 byte[] signature;
2a959b
-                try {
2a959b
-                    if (keyAlgorithm.equals("DSA")) {
2a959b
-                        signature = new byte[40];
2a959b
-                    } else {
2a959b
-                        signature = new byte[(p11Key.length() + 7) >> 3];
2a959b
+                if (keyAlgorithm.equals("DSA")) {
2a959b
+                    signature = new byte[40];
2a959b
+                } else {
2a959b
+                    signature = new byte[(p11Key.length() + 7) >> 3];
2a959b
+                }
2a959b
+                if (type == T_UPDATE) {
2a959b
+                    token.p11.C_VerifyFinal(session.id(), signature);
2a959b
+                } else {
2a959b
+                    byte[] digest;
2a959b
+                    if (type == T_DIGEST) {
2a959b
+                        digest = md.digest();
2a959b
+                    } else { // T_RAW
2a959b
+                        digest = buffer;
2a959b
                     }
2a959b
-                    if (type == T_UPDATE) {
2a959b
-                        token.p11.C_VerifyFinal(session.id(), signature);
2a959b
-                    } else {
2a959b
-                        byte[] digest;
2a959b
-                        if (type == T_DIGEST) {
2a959b
-                            digest = md.digest();
2a959b
-                        } else { // T_RAW
2a959b
-                            digest = buffer;
2a959b
-                        }
2a959b
-                        token.p11.C_Verify(session.id(), digest, signature);
2a959b
-                    }
2a959b
-                } catch (PKCS11Exception e) {
2a959b
-                    long errorCode = e.getErrorCode();
2a959b
-                    if ((errorCode == CKR_SIGNATURE_INVALID) ||
2a959b
-                        (errorCode == CKR_SIGNATURE_LEN_RANGE)) {
2a959b
-                        // expected since signature is incorrect
2a959b
-                        return;
2a959b
-                    }
2a959b
-                    throw new ProviderException("cancel failed", e);
2a959b
+                    token.p11.C_Verify(session.id(), digest, signature);
2a959b
                 }
2a959b
             }
2a959b
+        } catch (PKCS11Exception e) {
2a959b
+            if (mode == M_VERIFY) {
2a959b
+                long errorCode = e.getErrorCode();
2a959b
+                if ((errorCode == CKR_SIGNATURE_INVALID) ||
2a959b
+                     (errorCode == CKR_SIGNATURE_LEN_RANGE)) {
2a959b
+                     // expected since signature is incorrect
2a959b
+                     return;
2a959b
+                }
2a959b
+            }
2a959b
+            throw new ProviderException("cancel failed", e);
2a959b
         }
2a959b
     }
2a959b