提交 2a4e4b16 编写于 作者: V valeriep

6682411: JCK test failed w/ ArrayIndexOutOfBoundException (-1) when decrypting with no data

Summary: Fixed PKCS5Padding class with additional check and throw BadPaddingException if the check failed
Reviewed-by: wetmore
上级 d03debec
...@@ -72,7 +72,7 @@ final class P11Cipher extends CipherSpi { ...@@ -72,7 +72,7 @@ final class P11Cipher extends CipherSpi {
// DEC: return the length of trailing padding bytes given the specified // DEC: return the length of trailing padding bytes given the specified
// padded data // padded data
int unpad(byte[] paddedData, int ofs, int len) int unpad(byte[] paddedData, int len)
throws BadPaddingException; throws BadPaddingException;
} }
...@@ -94,14 +94,17 @@ final class P11Cipher extends CipherSpi { ...@@ -94,14 +94,17 @@ final class P11Cipher extends CipherSpi {
return padLen; return padLen;
} }
public int unpad(byte[] paddedData, int ofs, int len) public int unpad(byte[] paddedData, int len)
throws BadPaddingException { throws BadPaddingException {
byte padValue = paddedData[ofs + len - 1]; if (len < 1 || len > paddedData.length) {
throw new BadPaddingException("Invalid pad array length!");
}
byte padValue = paddedData[len - 1];
if (padValue < 1 || padValue > blockSize) { if (padValue < 1 || padValue > blockSize) {
throw new BadPaddingException("Invalid pad value!"); throw new BadPaddingException("Invalid pad value!");
} }
// sanity check padding bytes // sanity check padding bytes
int padStartIndex = ofs + len - padValue; int padStartIndex = len - padValue;
for (int i = padStartIndex; i < len; i++) { for (int i = padStartIndex; i < len; i++) {
if (paddedData[i] != padValue) { if (paddedData[i] != padValue) {
throw new BadPaddingException("Invalid pad bytes!"); throw new BadPaddingException("Invalid pad bytes!");
...@@ -712,7 +715,7 @@ final class P11Cipher extends CipherSpi { ...@@ -712,7 +715,7 @@ final class P11Cipher extends CipherSpi {
} }
k += token.p11.C_DecryptFinal(session.id(), 0, padBuffer, k, k += token.p11.C_DecryptFinal(session.id(), 0, padBuffer, k,
padBuffer.length - k); padBuffer.length - k);
int actualPadLen = paddingObj.unpad(padBuffer, 0, k); int actualPadLen = paddingObj.unpad(padBuffer, k);
k -= actualPadLen; k -= actualPadLen;
System.arraycopy(padBuffer, 0, out, outOfs, k); System.arraycopy(padBuffer, 0, out, outOfs, k);
} else { } else {
...@@ -781,7 +784,7 @@ final class P11Cipher extends CipherSpi { ...@@ -781,7 +784,7 @@ final class P11Cipher extends CipherSpi {
} }
k += token.p11.C_DecryptFinal(session.id(), k += token.p11.C_DecryptFinal(session.id(),
0, padBuffer, k, padBuffer.length - k); 0, padBuffer, k, padBuffer.length - k);
int actualPadLen = paddingObj.unpad(padBuffer, 0, k); int actualPadLen = paddingObj.unpad(padBuffer, k);
k -= actualPadLen; k -= actualPadLen;
outArray = padBuffer; outArray = padBuffer;
outOfs = 0; outOfs = 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册