-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8261355: No data buffering in SunPKCS11 Cipher encryption when the underlying mechanism has no padding #2510
Conversation
👋 Welcome back mbalao! A progress list of the required criteria for merging this PR into |
Webrevs
|
I will take a look. |
if (padBufferLen != 0) { | ||
// NSS throws up when called with data not in multiple | ||
// of blocks. Try to work around this by holding the | ||
// extra data in padBuffer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I am not sure if other PKCS#11 libraries are like NSS which requires input size to be multiple of blocks for every multi-part encryption/decryption calls. We are paying the cost of buffering non-blocksize data ourselves and the associated byte copying as a result. Oh-well.
With this change, you should also update the implDoFinal() impl which calls paddingObj.setPaddingBytes(byte[], int) for encryption and writes the padding bytes "after" the existing buffered bytes, i.e. padBufferLen. Otherwise, the existing buffered bytes may be overwritten w/ padding bytes and things will fail. The new regression test should cover this scenario also. It currently only tests the changes made to update() calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've pushed a new proposal to limit the performance impact of Java-side buffering to the NSS library. This adds to the previous conditions: the operation has to be encryption and the mechanism must not have native padding. If we realize in the future that other libraries are affected as well, we can easily extend the scope.
In regards to the implDoFinal bug, well spotted! Fixed in this new proposal and the test has been enhanced to cover not only this case but also different padding sizes and different block numbers.
Branch rebased (today) to the latest master.
Look forward to your comments.
…derlying mechanism has no padding
char[] tokenLabel = token.tokenInfo.label; | ||
// NSS requires block-sized updates in multi-part operations. | ||
reqBlockUpdates = ((tokenLabel[0] == 'N' && tokenLabel[1] == 'S' | ||
&& tokenLabel[2] == 'S') ? true : false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, depending on how the impl is registered, engineSetPadding(String) may not always be called. It's probably safer to set this in engineInit(...)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks to me that engineSetPadding is always called from the P11Cipher constructor. I thought that was a good location to set the reqBlockUpdates variable because it's next to the paddingObj initialization; which is a pre-requisite for reqBlockUpdates to be used. In other words, if we have no Java-side padding (paddingObj == null), reqBlockUpdates won't be used and we don't even pay the price of setting it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.
// While decrypting with implUpdate, a block-sized buffer with | ||
// encrypted data is always held instead of being unencrypted | ||
// and returned to the caller. This is because the block may | ||
// contain padding bytes, in case it's the last one (unknown | ||
// at this point). In implDoFinal, where we know it's the | ||
// last one, this buffer is unencrypted and unpadded before | ||
// returned to the caller. None of this is necessary for | ||
// encryption: encrypted data can be safely returned upon a | ||
// implUpdate call. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: all of the "unencrypted" -> "decrypted". I think this is a bit too verbose? Could we trim it down more, e.g. for decrypting with update() calls, up to a block of input is held inside padBuffer as it may contain padding bytes when no more data is supplied when doFinal() is called.
It should be clear that this does not apply for encryption, so there should be no need to state that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll replace "unencrypted" with "decrypted" and remove the comment about this not being necessary for encryption. I've also trimmed and improved my comment a bit: "While decrypting with implUpdate, the current encrypted block is always held in a buffer. If it's the last one (unknown at this point), it may contain padding bytes and need further processing. In implDoFinal (where we know it's the last one) the buffer is decrypted, unpadded and returned.". One comment about your suggestion: it's block-sized, not 'up to a block size'. But sounded a bit confusing to me overall, so if possible I'd stick to something along the lines above.
} | ||
} | ||
// update 'padBuffer' if using our own padding impl. | ||
if (paddingObj != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: if (paddingObj != null && newPadBufferLen > 0)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, makes sense. I've replaced the other " newPadBufferLen != 0" with "newPadBufferLen > 0" to be consistent.
// NSS throws up when called with data not in multiple | ||
// of blocks. Try to work around this by holding the | ||
// extra data in padBuffer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: The comment looks a little bit strange. This particular block of code is for handling existing buffered data buffered in earlier update() calls. The comment however is more about 'reqBlockUpdates' itself. How about merging this with the comment for 'reqBlockUpdates' field and then changing this comment to what this particular block of code does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right. I merged the comment with the field description. I believe there is not much to say about that block, though. At least there is nothing new there, except that we may buffer for reqBlockUpdates reasons. If you still want a comment there, let me know and I try to figure out something.
@@ -779,10 +814,18 @@ private int implDoFinal(byte[] out, int outOfs, int outLen) | |||
int k = 0; | |||
if (encrypt) { | |||
if (paddingObj != null) { | |||
int startOff = 0; | |||
if (reqBlockUpdates) { | |||
startOff = bytesBuffered; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the starting offset be the number of bytes in padBuffer, i.e. padBufferLen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then no need for the assert(...) to check the starting offset value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
padBufferLen and bytesBuffered look a bit confusing to me. My suspicion is that they have the same value every time we need them. I'll make the change you suggested and check that we have no regressions. If you believe the assertions are trivial, I'll remove them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I checked this with the debugger now. I'll make the change anyways.
@@ -864,7 +907,7 @@ private int implDoFinal(ByteBuffer outBuffer) | |||
if (encrypt) { | |||
if (paddingObj != null) { | |||
int actualPadLen = paddingObj.setPaddingBytes(padBuffer, | |||
requiredOutLen - bytesBuffered); | |||
0, requiredOutLen - bytesBuffered); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the starting offset be 'padBufferLen'?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yes, well spotted.. I forgot to synchronize with the byte[] path.
@@ -864,7 +907,7 @@ private int implDoFinal(ByteBuffer outBuffer) | |||
if (encrypt) { | |||
if (paddingObj != null) { | |||
int actualPadLen = paddingObj.setPaddingBytes(padBuffer, | |||
requiredOutLen - bytesBuffered); | |||
0, requiredOutLen - bytesBuffered); | |||
k = token.p11.C_EncryptUpdate(session.id(), | |||
0, padBuffer, 0, actualPadLen, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actualPadLen => actualPadLen + startOfs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I forgot to synchronize with the byte[] path. Thanks.
Arrays.fill(plainText, (byte)(inputSize & 0xFF)); | ||
ByteBuffer cipherText = | ||
ByteBuffer.allocate(((inputSize / 16 ) + 1) * 16); | ||
byte[] tmp = new byte[16]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems no need to do new byte[] given how it's used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. That was probably a vestige of an intermediate version.
if (tmp != null) | ||
cipherText.put(tmp); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: either use "{ }" or move cipherText.put() call to the same line as if-check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok
tmp = sunPKCS11cipher.doFinal(); | ||
if (tmp != null) | ||
cipherText.put(tmp); | ||
|
||
Cipher sunJCECipher = Cipher.getInstance(transformation, "SunJCE"); | ||
sunJCECipher.init(Cipher.DECRYPT_MODE, key); | ||
byte[] sunJCEPlain = sunJCECipher.doFinal(cipherText.array()); | ||
|
||
if (!Arrays.equals(plainText, sunJCEPlain)) { | ||
throw new Exception("Cross-provider cipher test failed."); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just use the byte[] forms for the Cipher.doFinal() and simplify this part, i.e. line 86-96?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are accumulating cipher text in the cipherText local variable while doing updates, both for the 'update(byte[]...' and 'update(ByteBuffer...' cases. The last call to doFinal will return the last block of cipher text, which we need to append. In regards to Cipher::doFinal, we are using the byte[] form of it. Please let me know if I'm not understanding your comment correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just realized that the test is not exercising the 'doFinal(ByteBuffer..' path. Thus, why it did not catch the previous sync bugs. I'll fix that.
@valeriepeng please take a look at my comments in-line and the new proposal here: b47c03e Thanks, |
Sure, will take another look. Thanks! |
Rest of changes look good. |
@martinuy This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 206 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine, thanks~
/integrate |
@martinuy Since your change was applied there have been 225 commits pushed to the
Your commit was automatically rebased without conflicts. Pushed as commit 1ee80e0. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
Hi,
I'd like to propose a fix for JDK-8261355 [1].
The scheme used for holding data and padding while performing encryption operations is almost the same than the existing one for decryption. The only difference is that encryption does not require a block-sized buffer to be always held because there is no need, upon an update call, to determine which bytes are real output for the caller and which are padding -as it's required for decryption-. I added a couple of comments in implUpdate to explain this.
No regressions observed in jdk/sun/security/pkcs11.
Thanks,
Martin.-
--
[1] - https://bugs.openjdk.java.net/browse/JDK-8261355
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/2510/head:pull/2510
$ git checkout pull/2510
Update a local copy of the PR:
$ git checkout pull/2510
$ git pull https://git.openjdk.java.net/jdk pull/2510/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 2510
View PR using the GUI difftool:
$ git pr show -t 2510
Using diff file
Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/2510.diff