提交 6774b980 编写于 作者: I igerasim

8150530: Improve javax.crypto.BadPaddingException messages

Reviewed-by: xuelei
上级 8954331d
...@@ -988,8 +988,9 @@ final class CipherCore { ...@@ -988,8 +988,9 @@ final class CipherCore {
if (padding != null) { if (padding != null) {
int padStart = padding.unpad(outWithPadding, 0, outLen); int padStart = padding.unpad(outWithPadding, 0, outLen);
if (padStart < 0) { if (padStart < 0) {
throw new BadPaddingException("Given final block not " throw new BadPaddingException("Given final block not " +
+ "properly padded"); "properly padded. Such issues can arise if a bad key " +
"is used during decryption.");
} }
outLen = padStart; outLen = padStart;
} }
......
...@@ -357,7 +357,9 @@ final class P11RSACipher extends CipherSpi { ...@@ -357,7 +357,9 @@ final class P11RSACipher extends CipherSpi {
System.arraycopy(buffer, 0, tmpBuffer, 0, bufOfs); System.arraycopy(buffer, 0, tmpBuffer, 0, bufOfs);
tmpBuffer = p11.C_Sign(session.id(), tmpBuffer); tmpBuffer = p11.C_Sign(session.id(), tmpBuffer);
if (tmpBuffer.length > outLen) { if (tmpBuffer.length > outLen) {
throw new BadPaddingException("Output buffer too small"); throw new BadPaddingException(
"Output buffer (" + outLen + ") is too small to " +
"hold the produced data (" + tmpBuffer.length + ")");
} }
System.arraycopy(tmpBuffer, 0, out, outOfs, tmpBuffer.length); System.arraycopy(tmpBuffer, 0, out, outOfs, tmpBuffer.length);
n = tmpBuffer.length; n = tmpBuffer.length;
......
...@@ -253,7 +253,8 @@ public final class RSAPadding { ...@@ -253,7 +253,8 @@ public final class RSAPadding {
public byte[] pad(byte[] data) throws BadPaddingException { public byte[] pad(byte[] data) throws BadPaddingException {
if (data.length > maxDataSize) { if (data.length > maxDataSize) {
throw new BadPaddingException("Data must be shorter than " throw new BadPaddingException("Data must be shorter than "
+ (maxDataSize + 1) + " bytes"); + (maxDataSize + 1) + " bytes but received "
+ data.length + " bytes.");
} }
switch (type) { switch (type) {
case PAD_NONE: case PAD_NONE:
...@@ -281,7 +282,9 @@ public final class RSAPadding { ...@@ -281,7 +282,9 @@ public final class RSAPadding {
*/ */
public byte[] unpad(byte[] padded) throws BadPaddingException { public byte[] unpad(byte[] padded) throws BadPaddingException {
if (padded.length != paddedSize) { if (padded.length != paddedSize) {
throw new BadPaddingException("Decryption error"); throw new BadPaddingException("Decryption error." +
"The padded array length (" + padded.length +
") is not the specified padded size (" + paddedSize + ")");
} }
switch (type) { switch (type) {
case PAD_NONE: case PAD_NONE:
......
...@@ -493,7 +493,9 @@ final class CipherBox { ...@@ -493,7 +493,9 @@ final class CipherBox {
if (protocolVersion.v >= ProtocolVersion.TLS11.v) { if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
if (newLen < blockSize) { if (newLen < blockSize) {
throw new BadPaddingException("invalid explicit IV"); throw new BadPaddingException("The length after " +
"padding removal (" + newLen + ") should be larger " +
"than <" + blockSize + "> since explicit IV used");
} }
} }
} }
...@@ -504,7 +506,6 @@ final class CipherBox { ...@@ -504,7 +506,6 @@ final class CipherBox {
} }
} }
/* /*
* Decrypts a block of data, returning the size of the * Decrypts a block of data, returning the size of the
* resulting block if padding was required. position and limit * resulting block if padding was required. position and limit
...@@ -575,7 +576,9 @@ final class CipherBox { ...@@ -575,7 +576,9 @@ final class CipherBox {
// check the explicit IV of TLS v1.1 or later // check the explicit IV of TLS v1.1 or later
if (protocolVersion.v >= ProtocolVersion.TLS11.v) { if (protocolVersion.v >= ProtocolVersion.TLS11.v) {
if (newLen < blockSize) { if (newLen < blockSize) {
throw new BadPaddingException("invalid explicit IV"); throw new BadPaddingException("The length after " +
"padding removal (" + newLen + ") should be larger " +
"than <" + blockSize + "> since explicit IV used");
} }
// reset the position to the end of the decrypted data // reset the position to the end of the decrypted data
...@@ -756,7 +759,9 @@ final class CipherBox { ...@@ -756,7 +759,9 @@ final class CipherBox {
// so accept that as well // so accept that as well
// v3 does not require any particular value for the other bytes // v3 does not require any particular value for the other bytes
if (padLen > blockSize) { if (padLen > blockSize) {
throw new BadPaddingException("Invalid SSLv3 padding"); throw new BadPaddingException("Padding length (" +
padLen + ") of SSLv3 message should not be bigger " +
"than the block size (" + blockSize + ")");
} }
} }
return newLen; return newLen;
...@@ -802,7 +807,9 @@ final class CipherBox { ...@@ -802,7 +807,9 @@ final class CipherBox {
// so accept that as well // so accept that as well
// v3 does not require any particular value for the other bytes // v3 does not require any particular value for the other bytes
if (padLen > blockSize) { if (padLen > blockSize) {
throw new BadPaddingException("Invalid SSLv3 padding"); throw new BadPaddingException("Padding length (" +
padLen + ") of SSLv3 message should not be bigger " +
"than the block size (" + blockSize + ")");
} }
} }
...@@ -925,7 +932,10 @@ final class CipherBox { ...@@ -925,7 +932,10 @@ final class CipherBox {
case AEAD_CIPHER: case AEAD_CIPHER:
if (bb.remaining() < (recordIvSize + tagSize)) { if (bb.remaining() < (recordIvSize + tagSize)) {
throw new BadPaddingException( throw new BadPaddingException(
"invalid AEAD cipher fragment"); "Insufficient buffer remaining for AEAD cipher " +
"fragment (" + bb.remaining() + "). Needs to be " +
"more than or equal to IV size (" + recordIvSize +
") + tag size (" + tagSize + ")");
} }
// initialize the AEAD cipher for the unique IV // initialize the AEAD cipher for the unique IV
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册