提交 a864276e 编写于 作者: V valeriep

6924489: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_OPERATION_NOT_INITIALIZED

Summary: Reset cipher state to un-initialized wherever necessary.
Reviewed-by: weijun
上级 28e9e523
...@@ -395,6 +395,8 @@ final class P11Cipher extends CipherSpi { ...@@ -395,6 +395,8 @@ final class P11Cipher extends CipherSpi {
} }
} catch (PKCS11Exception e) { } catch (PKCS11Exception e) {
throw new ProviderException("Cancel failed", e); throw new ProviderException("Cancel failed", e);
} finally {
reset();
} }
} }
...@@ -408,12 +410,18 @@ final class P11Cipher extends CipherSpi { ...@@ -408,12 +410,18 @@ final class P11Cipher extends CipherSpi {
if (session == null) { if (session == null) {
session = token.getOpSession(); session = token.getOpSession();
} }
if (encrypt) { try {
token.p11.C_EncryptInit(session.id(), if (encrypt) {
new CK_MECHANISM(mechanism, iv), p11Key.keyID); token.p11.C_EncryptInit(session.id(),
} else { new CK_MECHANISM(mechanism, iv), p11Key.keyID);
token.p11.C_DecryptInit(session.id(), } else {
new CK_MECHANISM(mechanism, iv), p11Key.keyID); token.p11.C_DecryptInit(session.id(),
new CK_MECHANISM(mechanism, iv), p11Key.keyID);
}
} catch (PKCS11Exception ex) {
// release session when initialization failed
session = token.releaseSession(session);
throw ex;
} }
bytesBuffered = 0; bytesBuffered = 0;
padBufferLen = 0; padBufferLen = 0;
...@@ -448,6 +456,16 @@ final class P11Cipher extends CipherSpi { ...@@ -448,6 +456,16 @@ final class P11Cipher extends CipherSpi {
return result; return result;
} }
// reset the states to the pre-initialized values
private void reset() {
initialized = false;
bytesBuffered = 0;
padBufferLen = 0;
if (session != null) {
session = token.releaseSession(session);
}
}
// see JCE spec // see JCE spec
protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) { protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
try { try {
...@@ -566,6 +584,7 @@ final class P11Cipher extends CipherSpi { ...@@ -566,6 +584,7 @@ final class P11Cipher extends CipherSpi {
throw (ShortBufferException) throw (ShortBufferException)
(new ShortBufferException().initCause(e)); (new ShortBufferException().initCause(e));
} }
reset();
throw new ProviderException("update() failed", e); throw new ProviderException("update() failed", e);
} }
} }
...@@ -683,6 +702,7 @@ final class P11Cipher extends CipherSpi { ...@@ -683,6 +702,7 @@ final class P11Cipher extends CipherSpi {
throw (ShortBufferException) throw (ShortBufferException)
(new ShortBufferException().initCause(e)); (new ShortBufferException().initCause(e));
} }
reset();
throw new ProviderException("update() failed", e); throw new ProviderException("update() failed", e);
} }
} }
...@@ -729,10 +749,7 @@ final class P11Cipher extends CipherSpi { ...@@ -729,10 +749,7 @@ final class P11Cipher extends CipherSpi {
handleException(e); handleException(e);
throw new ProviderException("doFinal() failed", e); throw new ProviderException("doFinal() failed", e);
} finally { } finally {
initialized = false; reset();
bytesBuffered = 0;
padBufferLen = 0;
session = token.releaseSession(session);
} }
} }
...@@ -806,9 +823,7 @@ final class P11Cipher extends CipherSpi { ...@@ -806,9 +823,7 @@ final class P11Cipher extends CipherSpi {
handleException(e); handleException(e);
throw new ProviderException("doFinal() failed", e); throw new ProviderException("doFinal() failed", e);
} finally { } finally {
initialized = false; reset();
bytesBuffered = 0;
session = token.releaseSession(session);
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册