<div dir="ltr"><div dir="ltr"><div>Hi Valerie,</div><div><br></div><div>Thanks for your comments.</div><div><br></div><div>Here it is Webrev.11:</div><div><br></div><div> * <a href="http://cr.openjdk.java.net/~mbalao/webrevs/6913047/6913047.webrev.11/">http://cr.openjdk.java.net/~mbalao/webrevs/6913047/6913047.webrev.11/</a></div><div> * <a href="http://cr.openjdk.java.net/~mbalao/webrevs/6913047/6913047.webrev.11.zip">http://cr.openjdk.java.net/~mbalao/webrevs/6913047/6913047.webrev.11.zip</a></div><div><br></div><div><src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Cipher.java></div><div>L397: That's right. I was trying to simplify the code but missed this. Thanks.</div><div>L471: The key reference counter has to be decremented under any exception (P11Key.decNativeKeyRef method call). But, yes, no exception different than PKCS11Exception should be thrown. Reverted this change.</div><div><br></div><div><src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Key.java></div><div>L99: Comment changed. It should be better now.</div><div>L148-L149: In fact, I'd enforce this and disable the feature for all token keys. Token keys are permanent and extracting them is risky. This criteria was already applied when dealing with key stores (P11Keystore class).</div><div><br></div><div>Yes, this feature is enabled for NSS only because it's the only backend we currently know that is affected by this memory "leak" issue. If there were any other software-token backend affected, we can try this feature there too. HSMs shouldn't have any problem. I prefer to take a more conservative approach and enable the feature only in those cases in which it's really necessary. All other cases, default to the previous mechanism for freeing memory.</div><div><br></div><div>This does not replace the PhantomReference approach; both work together and are complementary. In cases where temporary keys feature is disabled or when a temporary key client is not behaving correctly (i.e.: leaking stateful operations like "cipher" or "signature" in an intermediate state with the native key initialized), PhantomReference approach will be the last chance to free memory. The native key object can be destroyed (C_DestroyObject call) either from the PhantomReference mechanism or from the temporary keys mechanism. There shouldn't be any conflict between them. If it's destroyed through temporary keys mechanism, then we know that the P11Key object is alive (refereced) and thus PhantomReference destruction won't be taking place at the same time. Once the key is deleted, keyID is set to 0 and session to null. Thus, PhantomReference destruction won't have any effect when executed later. If we think of the other case (when the key is freed by PhantomReference), we have a P11Key object with a native key initialized but with no references to it. Thus, destroyNativeKey method won't be called and SessionKeyRef.disposeNative is the only method that will delete the key.</div><div><br></div><div>L157: that's right, synchronization has to be at class level. Fixed.</div><div><br></div><div>L1343: It's not the same session: this.session was assigned a new value (this.session = session;) before calling addObject.</div><div><br></div><div>L1363: removeObject is called for the session, inside setKeyIDAndSession: "this.session.removeObject();". Null is set to this.session instance variable after this call.</div><div><br></div><div>In regards to the refactorings you proposed, the problem I see with moving key reference incrementing/decrementing to PKCS11.java is that some operations are stateful. I.e.: encryption. When we initialize the operation with C_EncryptInit, the key id is the 3rd parameter. Destroying the key id and then doing C_EncryptUpdate sounds incorrect to me. Have you tried the regression testing suite after this refactoring? (I see some parts commented). In regards to removing the tmpNativeKey parameter (used to explicitly disable the feature for new P11Key objects), how do you handle the P11KeyStore case? We don't want temporary keys there.</div><div><br></div><div>Kind regards,</div><div>Martin.-</div><div><br></div></div></div>