8171252: Improve exception checking
Reviewed-by: ascarpino, mschoene
This commit is contained in:
parent
ab51d9296c
commit
2473f1cf41
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -156,7 +156,7 @@ abstract class AESCipher extends CipherSpi {
|
|||||||
throw new InvalidKeyException("Key encoding must not be null");
|
throw new InvalidKeyException("Key encoding must not be null");
|
||||||
} else if (value.length != fixedKeySize) {
|
} else if (value.length != fixedKeySize) {
|
||||||
throw new InvalidKeyException("The key must be " +
|
throw new InvalidKeyException("The key must be " +
|
||||||
fixedKeySize*8 + " bits");
|
fixedKeySize + " bytes");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -509,7 +509,7 @@ abstract class AESCipher extends CipherSpi {
|
|||||||
throw new InvalidKeyException("Invalid AES key length: " +
|
throw new InvalidKeyException("Invalid AES key length: " +
|
||||||
encoded.length + " bytes");
|
encoded.length + " bytes");
|
||||||
}
|
}
|
||||||
return encoded.length * 8;
|
return Math.multiplyExact(encoded.length, 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -628,9 +628,9 @@ abstract class AESCipher extends CipherSpi {
|
|||||||
}
|
}
|
||||||
if (src != null) {
|
if (src != null) {
|
||||||
int aadLen = src.limit() - src.position();
|
int aadLen = src.limit() - src.position();
|
||||||
if (aadLen != 0) {
|
if (aadLen > 0) {
|
||||||
if (src.hasArray()) {
|
if (src.hasArray()) {
|
||||||
int aadOfs = src.arrayOffset() + src.position();
|
int aadOfs = Math.addExact(src.arrayOffset(), src.position());
|
||||||
core.updateAAD(src.array(), aadOfs, aadLen);
|
core.updateAAD(src.array(), aadOfs, aadLen);
|
||||||
src.position(src.limit());
|
src.position(src.limit());
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2004, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -156,7 +156,7 @@ abstract class AESWrapCipher extends CipherSpi {
|
|||||||
if (decrypting) {
|
if (decrypting) {
|
||||||
result = inputLen - 8;
|
result = inputLen - 8;
|
||||||
} else {
|
} else {
|
||||||
result = inputLen + 8;
|
result = Math.addExact(inputLen, 8);
|
||||||
}
|
}
|
||||||
return (result < 0? 0:result);
|
return (result < 0? 0:result);
|
||||||
}
|
}
|
||||||
@ -378,7 +378,7 @@ abstract class AESWrapCipher extends CipherSpi {
|
|||||||
throw new InvalidKeyException("Invalid key length: " +
|
throw new InvalidKeyException("Invalid key length: " +
|
||||||
encoded.length + " bytes");
|
encoded.length + " bytes");
|
||||||
}
|
}
|
||||||
return encoded.length * 8;
|
return Math.multiplyExact(encoded.length, 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -404,7 +404,7 @@ abstract class AESWrapCipher extends CipherSpi {
|
|||||||
throw new InvalidKeyException("Cannot get an encoding of " +
|
throw new InvalidKeyException("Cannot get an encoding of " +
|
||||||
"the key to be wrapped");
|
"the key to be wrapped");
|
||||||
}
|
}
|
||||||
byte[] out = new byte[keyVal.length + 8];
|
byte[] out = new byte[Math.addExact(keyVal.length, 8)];
|
||||||
|
|
||||||
if (keyVal.length == 8) {
|
if (keyVal.length == 8) {
|
||||||
System.arraycopy(IV, 0, out, 0, IV.length);
|
System.arraycopy(IV, 0, out, 0, IV.length);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -257,7 +257,7 @@ public final class ARCFOURCipher extends CipherSpi {
|
|||||||
// see JCE spec
|
// see JCE spec
|
||||||
protected int engineGetKeySize(Key key) throws InvalidKeyException {
|
protected int engineGetKeySize(Key key) throws InvalidKeyException {
|
||||||
byte[] encodedKey = getEncodedKey(key);
|
byte[] encodedKey = getEncodedKey(key);
|
||||||
return encodedKey.length << 3;
|
return Math.multiplyExact(encodedKey.length, 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1998, 2009, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -373,7 +373,7 @@ public final class BlowfishCipher extends CipherSpi {
|
|||||||
* @exception InvalidKeyException if <code>key</code> is invalid.
|
* @exception InvalidKeyException if <code>key</code> is invalid.
|
||||||
*/
|
*/
|
||||||
protected int engineGetKeySize(Key key) throws InvalidKeyException {
|
protected int engineGetKeySize(Key key) throws InvalidKeyException {
|
||||||
return (key.getEncoded().length * 8);
|
return Math.multiplyExact(key.getEncoded().length, 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -324,13 +324,14 @@ final class CipherCore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int getOutputSizeByOperation(int inputLen, boolean isDoFinal) {
|
private int getOutputSizeByOperation(int inputLen, boolean isDoFinal) {
|
||||||
int totalLen = buffered + inputLen + cipher.getBufferedLength();
|
int totalLen = Math.addExact(buffered, cipher.getBufferedLength());
|
||||||
|
totalLen = Math.addExact(totalLen, inputLen);
|
||||||
switch (cipherMode) {
|
switch (cipherMode) {
|
||||||
case GCM_MODE:
|
case GCM_MODE:
|
||||||
if (isDoFinal) {
|
if (isDoFinal) {
|
||||||
int tagLen = ((GaloisCounterMode) cipher).getTagLen();
|
int tagLen = ((GaloisCounterMode) cipher).getTagLen();
|
||||||
if (!decrypting) {
|
if (!decrypting) {
|
||||||
totalLen += tagLen;
|
totalLen = Math.addExact(totalLen, tagLen);
|
||||||
} else {
|
} else {
|
||||||
totalLen -= tagLen;
|
totalLen -= tagLen;
|
||||||
}
|
}
|
||||||
@ -346,10 +347,10 @@ final class CipherCore {
|
|||||||
totalLen = diffBlocksize;
|
totalLen = diffBlocksize;
|
||||||
} else {
|
} else {
|
||||||
int residue = (totalLen - diffBlocksize) % blockSize;
|
int residue = (totalLen - diffBlocksize) % blockSize;
|
||||||
totalLen += (blockSize - residue);
|
totalLen = Math.addExact(totalLen, (blockSize - residue));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
totalLen += padding.padLength(totalLen);
|
totalLen = Math.addExact(totalLen, padding.padLength(totalLen));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -711,7 +712,8 @@ final class CipherCore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// figure out how much can be sent to crypto function
|
// figure out how much can be sent to crypto function
|
||||||
int len = buffered + inputLen - minBytes;
|
int len = Math.addExact(buffered, inputLen);
|
||||||
|
len -= minBytes;
|
||||||
if (padding != null && decrypting) {
|
if (padding != null && decrypting) {
|
||||||
// do not include the padding bytes when decrypting
|
// do not include the padding bytes when decrypting
|
||||||
len -= blockSize;
|
len -= blockSize;
|
||||||
@ -730,12 +732,12 @@ final class CipherCore {
|
|||||||
int outLen = 0;
|
int outLen = 0;
|
||||||
if (len != 0) { // there is some work to do
|
if (len != 0) { // there is some work to do
|
||||||
if ((input == output)
|
if ((input == output)
|
||||||
&& (outputOffset < (inputOffset + inputLen))
|
&& (outputOffset - inputOffset < inputLen)
|
||||||
&& (inputOffset < (outputOffset + buffer.length))) {
|
&& (inputOffset - outputOffset < buffer.length)) {
|
||||||
// copy 'input' out to avoid its content being
|
// copy 'input' out to avoid its content being
|
||||||
// overwritten prematurely.
|
// overwritten prematurely.
|
||||||
input = Arrays.copyOfRange(input, inputOffset,
|
input = Arrays.copyOfRange(input, inputOffset,
|
||||||
inputOffset + inputLen);
|
Math.addExact(inputOffset, inputLen));
|
||||||
inputOffset = 0;
|
inputOffset = 0;
|
||||||
}
|
}
|
||||||
if (len <= buffered) {
|
if (len <= buffered) {
|
||||||
@ -757,13 +759,13 @@ final class CipherCore {
|
|||||||
if (bufferCapacity != 0) {
|
if (bufferCapacity != 0) {
|
||||||
temp = Math.min(bufferCapacity, inputConsumed);
|
temp = Math.min(bufferCapacity, inputConsumed);
|
||||||
if (unitBytes != blockSize) {
|
if (unitBytes != blockSize) {
|
||||||
temp -= ((buffered + temp) % unitBytes);
|
temp -= (Math.addExact(buffered, temp) % unitBytes);
|
||||||
}
|
}
|
||||||
System.arraycopy(input, inputOffset, buffer, buffered, temp);
|
System.arraycopy(input, inputOffset, buffer, buffered, temp);
|
||||||
inputOffset += temp;
|
inputOffset = Math.addExact(inputOffset, temp);
|
||||||
inputConsumed -= temp;
|
inputConsumed -= temp;
|
||||||
inputLen -= temp;
|
inputLen -= temp;
|
||||||
buffered += temp;
|
buffered = Math.addExact(buffered, temp);
|
||||||
}
|
}
|
||||||
// process 'buffer'
|
// process 'buffer'
|
||||||
if (decrypting) {
|
if (decrypting) {
|
||||||
@ -771,7 +773,7 @@ final class CipherCore {
|
|||||||
} else {
|
} else {
|
||||||
outLen = cipher.encrypt(buffer, 0, buffered, output, outputOffset);
|
outLen = cipher.encrypt(buffer, 0, buffered, output, outputOffset);
|
||||||
}
|
}
|
||||||
outputOffset += outLen;
|
outputOffset = Math.addExact(outputOffset, outLen);
|
||||||
buffered = 0;
|
buffered = 0;
|
||||||
}
|
}
|
||||||
if (inputConsumed > 0) { // still has input to process
|
if (inputConsumed > 0) { // still has input to process
|
||||||
@ -802,7 +804,7 @@ final class CipherCore {
|
|||||||
if (inputLen > 0) {
|
if (inputLen > 0) {
|
||||||
System.arraycopy(input, inputOffset, buffer, buffered,
|
System.arraycopy(input, inputOffset, buffer, buffered,
|
||||||
inputLen);
|
inputLen);
|
||||||
buffered += inputLen;
|
buffered = Math.addExact(buffered, inputLen);
|
||||||
}
|
}
|
||||||
return outLen;
|
return outLen;
|
||||||
}
|
}
|
||||||
@ -912,10 +914,10 @@ final class CipherCore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// calculate total input length
|
// calculate total input length
|
||||||
int len = buffered + inputLen;
|
int len = Math.addExact(buffered, inputLen);
|
||||||
|
|
||||||
// calculate padding length
|
// calculate padding length
|
||||||
int totalLen = len + cipher.getBufferedLength();
|
int totalLen = Math.addExact(len, cipher.getBufferedLength());
|
||||||
int paddingLen = 0;
|
int paddingLen = 0;
|
||||||
// will the total input length be a multiple of blockSize?
|
// will the total input length be a multiple of blockSize?
|
||||||
if (unitBytes != blockSize) {
|
if (unitBytes != blockSize) {
|
||||||
@ -948,12 +950,12 @@ final class CipherCore {
|
|||||||
int finalBufLen = inputLen;
|
int finalBufLen = inputLen;
|
||||||
if ((buffered != 0) || (!decrypting && padding != null) ||
|
if ((buffered != 0) || (!decrypting && padding != null) ||
|
||||||
((input == output)
|
((input == output)
|
||||||
&& (outputOffset < (inputOffset + inputLen))
|
&& (outputOffset - inputOffset < inputLen)
|
||||||
&& (inputOffset < (outputOffset + buffer.length)))) {
|
&& (inputOffset - outputOffset < buffer.length))) {
|
||||||
if (decrypting || padding == null) {
|
if (decrypting || padding == null) {
|
||||||
paddingLen = 0;
|
paddingLen = 0;
|
||||||
}
|
}
|
||||||
finalBuf = new byte[len + paddingLen];
|
finalBuf = new byte[Math.addExact(len, paddingLen)];
|
||||||
finalOffset = 0;
|
finalOffset = 0;
|
||||||
if (buffered != 0) {
|
if (buffered != 0) {
|
||||||
System.arraycopy(buffer, 0, finalBuf, 0, buffered);
|
System.arraycopy(buffer, 0, finalBuf, 0, buffered);
|
||||||
@ -963,7 +965,7 @@ final class CipherCore {
|
|||||||
buffered, inputLen);
|
buffered, inputLen);
|
||||||
}
|
}
|
||||||
if (paddingLen != 0) {
|
if (paddingLen != 0) {
|
||||||
padding.padWithLen(finalBuf, (buffered+inputLen), paddingLen);
|
padding.padWithLen(finalBuf, Math.addExact(buffered, inputLen), paddingLen);
|
||||||
}
|
}
|
||||||
finalBufLen = finalBuf.length;
|
finalBufLen = finalBuf.length;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2004, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -140,7 +140,7 @@ public final class DESedeWrapCipher extends CipherSpi {
|
|||||||
if (decrypting) {
|
if (decrypting) {
|
||||||
result = inputLen - 16; // CHECKSUM_LEN + IV_LEN;
|
result = inputLen - 16; // CHECKSUM_LEN + IV_LEN;
|
||||||
} else {
|
} else {
|
||||||
result = inputLen + 16;
|
result = Math.addExact(inputLen, 16);
|
||||||
}
|
}
|
||||||
return (result < 0? 0:result);
|
return (result < 0? 0:result);
|
||||||
}
|
}
|
||||||
@ -449,11 +449,11 @@ public final class DESedeWrapCipher extends CipherSpi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
byte[] cks = getChecksum(keyVal);
|
byte[] cks = getChecksum(keyVal);
|
||||||
byte[] in = new byte[keyVal.length + CHECKSUM_LEN];
|
byte[] in = new byte[Math.addExact(keyVal.length, CHECKSUM_LEN)];
|
||||||
System.arraycopy(keyVal, 0, in, 0, keyVal.length);
|
System.arraycopy(keyVal, 0, in, 0, keyVal.length);
|
||||||
System.arraycopy(cks, 0, in, keyVal.length, CHECKSUM_LEN);
|
System.arraycopy(cks, 0, in, keyVal.length, CHECKSUM_LEN);
|
||||||
|
|
||||||
byte[] out = new byte[iv.length + in.length];
|
byte[] out = new byte[Math.addExact(iv.length, in.length)];
|
||||||
System.arraycopy(iv, 0, out, 0, iv.length);
|
System.arraycopy(iv, 0, out, 0, iv.length);
|
||||||
|
|
||||||
cipher.encrypt(in, 0, in.length, out, iv.length);
|
cipher.encrypt(in, 0, in.length, out, iv.length);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -63,7 +63,8 @@ final class ISO10126Padding implements Padding {
|
|||||||
if (in == null)
|
if (in == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if ((off + len) > in.length) {
|
int idx = Math.addExact(off, len);
|
||||||
|
if (idx > in.length) {
|
||||||
throw new ShortBufferException("Buffer too small to hold padding");
|
throw new ShortBufferException("Buffer too small to hold padding");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +72,7 @@ final class ISO10126Padding implements Padding {
|
|||||||
byte[] padding = new byte[len - 1];
|
byte[] padding = new byte[len - 1];
|
||||||
SunJCE.getRandom().nextBytes(padding);
|
SunJCE.getRandom().nextBytes(padding);
|
||||||
System.arraycopy(padding, 0, in, off, len - 1);
|
System.arraycopy(padding, 0, in, off, len - 1);
|
||||||
in[off + len - 1] = paddingOctet;
|
in[idx - 1] = paddingOctet;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,14 +95,15 @@ final class ISO10126Padding implements Padding {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte lastByte = in[off + len - 1];
|
int idx = Math.addExact(off, len);
|
||||||
|
byte lastByte = in[idx - 1];
|
||||||
int padValue = (int)lastByte & 0x0ff;
|
int padValue = (int)lastByte & 0x0ff;
|
||||||
if ((padValue < 0x01)
|
if ((padValue < 0x01)
|
||||||
|| (padValue > blockSize)) {
|
|| (padValue > blockSize)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int start = off + len - padValue;
|
int start = idx - padValue;
|
||||||
if (start < off) {
|
if (start < off) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -1,535 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
|
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
||||||
*
|
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
|
||||||
* under the terms of the GNU General Public License version 2 only, as
|
|
||||||
* published by the Free Software Foundation. Oracle designates this
|
|
||||||
* particular file as subject to the "Classpath" exception as provided
|
|
||||||
* by Oracle in the LICENSE file that accompanied this code.
|
|
||||||
*
|
|
||||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
||||||
* version 2 for more details (a copy is included in the LICENSE file that
|
|
||||||
* accompanied this code).
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License version
|
|
||||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
||||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
||||||
*
|
|
||||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
||||||
* or visit www.oracle.com if you need additional information or have any
|
|
||||||
* questions.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.sun.crypto.provider;
|
|
||||||
|
|
||||||
import java.security.*;
|
|
||||||
import java.security.spec.*;
|
|
||||||
import javax.crypto.*;
|
|
||||||
import javax.crypto.spec.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class represents password-based encryption as defined by the PKCS #5
|
|
||||||
* standard.
|
|
||||||
*
|
|
||||||
* @author Jan Luehe
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @see javax.crypto.Cipher
|
|
||||||
*/
|
|
||||||
final class PBECipherCore {
|
|
||||||
|
|
||||||
// the encapsulated DES cipher
|
|
||||||
private CipherCore cipher;
|
|
||||||
private MessageDigest md;
|
|
||||||
private int blkSize;
|
|
||||||
private String algo = null;
|
|
||||||
private byte[] salt = null;
|
|
||||||
private int iCount = 10;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates an instance of PBE Cipher using the specified CipherSpi
|
|
||||||
* instance.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
PBECipherCore(String cipherAlg) throws NoSuchAlgorithmException,
|
|
||||||
NoSuchPaddingException {
|
|
||||||
algo = cipherAlg;
|
|
||||||
if (algo.equals("DES")) {
|
|
||||||
cipher = new CipherCore(new DESCrypt(),
|
|
||||||
DESConstants.DES_BLOCK_SIZE);
|
|
||||||
} else if (algo.equals("DESede")) {
|
|
||||||
|
|
||||||
cipher = new CipherCore(new DESedeCrypt(),
|
|
||||||
DESConstants.DES_BLOCK_SIZE);
|
|
||||||
} else {
|
|
||||||
throw new NoSuchAlgorithmException("No Cipher implementation " +
|
|
||||||
"for PBEWithMD5And" + algo);
|
|
||||||
}
|
|
||||||
cipher.setMode("CBC");
|
|
||||||
cipher.setPadding("PKCS5Padding");
|
|
||||||
// get instance of MD5
|
|
||||||
md = MessageDigest.getInstance("MD5");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the mode of this cipher. This algorithm can only be run in CBC
|
|
||||||
* mode.
|
|
||||||
*
|
|
||||||
* @param mode the cipher mode
|
|
||||||
*
|
|
||||||
* @exception NoSuchAlgorithmException if the requested cipher mode is
|
|
||||||
* invalid
|
|
||||||
*/
|
|
||||||
void setMode(String mode) throws NoSuchAlgorithmException {
|
|
||||||
cipher.setMode(mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the padding mechanism of this cipher. This algorithm only uses
|
|
||||||
* PKCS #5 padding.
|
|
||||||
*
|
|
||||||
* @param padding the padding mechanism
|
|
||||||
*
|
|
||||||
* @exception NoSuchPaddingException if the requested padding mechanism
|
|
||||||
* is invalid
|
|
||||||
*/
|
|
||||||
void setPadding(String paddingScheme) throws NoSuchPaddingException {
|
|
||||||
cipher.setPadding(paddingScheme);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the block size (in bytes).
|
|
||||||
*
|
|
||||||
* @return the block size (in bytes)
|
|
||||||
*/
|
|
||||||
int getBlockSize() {
|
|
||||||
return DESConstants.DES_BLOCK_SIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the length in bytes that an output buffer would need to be in
|
|
||||||
* order to hold the result of the next <code>update</code> or
|
|
||||||
* <code>doFinal</code> operation, given the input length
|
|
||||||
* <code>inputLen</code> (in bytes).
|
|
||||||
*
|
|
||||||
* <p>This call takes into account any unprocessed (buffered) data from a
|
|
||||||
* previous <code>update</code> call, and padding.
|
|
||||||
*
|
|
||||||
* <p>The actual output length of the next <code>update</code> or
|
|
||||||
* <code>doFinal</code> call may be smaller than the length returned by
|
|
||||||
* this method.
|
|
||||||
*
|
|
||||||
* @param inputLen the input length (in bytes)
|
|
||||||
*
|
|
||||||
* @return the required output buffer size (in bytes)
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
int getOutputSize(int inputLen) {
|
|
||||||
return cipher.getOutputSize(inputLen);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the initialization vector (IV) in a new buffer.
|
|
||||||
*
|
|
||||||
* <p> This is useful in the case where a random IV has been created
|
|
||||||
* (see <a href = "#init">init</a>),
|
|
||||||
* or in the context of password-based encryption or
|
|
||||||
* decryption, where the IV is derived from a user-supplied password.
|
|
||||||
*
|
|
||||||
* @return the initialization vector in a new buffer, or null if the
|
|
||||||
* underlying algorithm does not use an IV, or if the IV has not yet
|
|
||||||
* been set.
|
|
||||||
*/
|
|
||||||
byte[] getIV() {
|
|
||||||
return cipher.getIV();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the parameters used with this cipher.
|
|
||||||
*
|
|
||||||
* <p>The returned parameters may be the same that were used to initialize
|
|
||||||
* this cipher, or may contain the default set of parameters or a set of
|
|
||||||
* randomly generated parameters used by the underlying cipher
|
|
||||||
* implementation (provided that the underlying cipher implementation
|
|
||||||
* uses a default set of parameters or creates new parameters if it needs
|
|
||||||
* parameters but was not initialized with any).
|
|
||||||
*
|
|
||||||
* @return the parameters used with this cipher, or null if this cipher
|
|
||||||
* does not use any parameters.
|
|
||||||
*/
|
|
||||||
AlgorithmParameters getParameters() {
|
|
||||||
AlgorithmParameters params = null;
|
|
||||||
if (salt == null) {
|
|
||||||
salt = new byte[8];
|
|
||||||
SunJCE.getRandom().nextBytes(salt);
|
|
||||||
}
|
|
||||||
PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount);
|
|
||||||
try {
|
|
||||||
params = AlgorithmParameters.getInstance("PBEWithMD5And" +
|
|
||||||
(algo.equalsIgnoreCase("DES")? "DES":"TripleDES"),
|
|
||||||
SunJCE.getInstance());
|
|
||||||
params.init(pbeSpec);
|
|
||||||
} catch (NoSuchAlgorithmException nsae) {
|
|
||||||
// should never happen
|
|
||||||
throw new RuntimeException("SunJCE called, but not configured");
|
|
||||||
} catch (InvalidParameterSpecException ipse) {
|
|
||||||
// should never happen
|
|
||||||
throw new RuntimeException("PBEParameterSpec not supported");
|
|
||||||
}
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes this cipher with a key, a set of
|
|
||||||
* algorithm parameters, and a source of randomness.
|
|
||||||
* The cipher is initialized for one of the following four operations:
|
|
||||||
* encryption, decryption, key wrapping or key unwrapping, depending on
|
|
||||||
* the value of <code>opmode</code>.
|
|
||||||
*
|
|
||||||
* <p>If this cipher (including its underlying feedback or padding scheme)
|
|
||||||
* requires any random bytes, it will get them from <code>random</code>.
|
|
||||||
*
|
|
||||||
* @param opmode the operation mode of this cipher (this is one of
|
|
||||||
* the following:
|
|
||||||
* <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>),
|
|
||||||
* <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
|
|
||||||
* @param key the encryption key
|
|
||||||
* @param params the algorithm parameters
|
|
||||||
* @param random the source of randomness
|
|
||||||
*
|
|
||||||
* @exception InvalidKeyException if the given key is inappropriate for
|
|
||||||
* initializing this cipher
|
|
||||||
* @exception InvalidAlgorithmParameterException if the given algorithm
|
|
||||||
* parameters are inappropriate for this cipher
|
|
||||||
*/
|
|
||||||
void init(int opmode, Key key, AlgorithmParameterSpec params,
|
|
||||||
SecureRandom random)
|
|
||||||
throws InvalidKeyException, InvalidAlgorithmParameterException {
|
|
||||||
if (((opmode == Cipher.DECRYPT_MODE) ||
|
|
||||||
(opmode == Cipher.UNWRAP_MODE)) && (params == null)) {
|
|
||||||
throw new InvalidAlgorithmParameterException("Parameters "
|
|
||||||
+ "missing");
|
|
||||||
}
|
|
||||||
if ((key == null) ||
|
|
||||||
(key.getEncoded() == null) ||
|
|
||||||
!(key.getAlgorithm().regionMatches(true, 0, "PBE", 0, 3))) {
|
|
||||||
throw new InvalidKeyException("Missing password");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (params == null) {
|
|
||||||
// create random salt and use default iteration count
|
|
||||||
salt = new byte[8];
|
|
||||||
random.nextBytes(salt);
|
|
||||||
} else {
|
|
||||||
if (!(params instanceof PBEParameterSpec)) {
|
|
||||||
throw new InvalidAlgorithmParameterException
|
|
||||||
("Wrong parameter type: PBE expected");
|
|
||||||
}
|
|
||||||
salt = ((PBEParameterSpec) params).getSalt();
|
|
||||||
// salt must be 8 bytes long (by definition)
|
|
||||||
if (salt.length != 8) {
|
|
||||||
throw new InvalidAlgorithmParameterException
|
|
||||||
("Salt must be 8 bytes long");
|
|
||||||
}
|
|
||||||
iCount = ((PBEParameterSpec) params).getIterationCount();
|
|
||||||
if (iCount <= 0) {
|
|
||||||
throw new InvalidAlgorithmParameterException
|
|
||||||
("IterationCount must be a positive number");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] derivedKey = deriveCipherKey(key);
|
|
||||||
// use all but the last 8 bytes as the key value
|
|
||||||
SecretKeySpec cipherKey = new SecretKeySpec(derivedKey, 0,
|
|
||||||
derivedKey.length-8, algo);
|
|
||||||
// use the last 8 bytes as the IV
|
|
||||||
IvParameterSpec ivSpec = new IvParameterSpec(derivedKey,
|
|
||||||
derivedKey.length-8,
|
|
||||||
8);
|
|
||||||
// initialize the underlying cipher
|
|
||||||
cipher.init(opmode, cipherKey, ivSpec, random);
|
|
||||||
}
|
|
||||||
|
|
||||||
private byte[] deriveCipherKey(Key key) {
|
|
||||||
|
|
||||||
byte[] result = null;
|
|
||||||
byte[] passwdBytes = key.getEncoded();
|
|
||||||
|
|
||||||
if (algo.equals("DES")) {
|
|
||||||
// P || S (password concatenated with salt)
|
|
||||||
byte[] concat = new byte[passwdBytes.length + salt.length];
|
|
||||||
System.arraycopy(passwdBytes, 0, concat, 0, passwdBytes.length);
|
|
||||||
java.util.Arrays.fill(passwdBytes, (byte)0x00);
|
|
||||||
System.arraycopy(salt, 0, concat, passwdBytes.length, salt.length);
|
|
||||||
|
|
||||||
// digest P || S with c iterations
|
|
||||||
byte[] toBeHashed = concat;
|
|
||||||
for (int i = 0; i < iCount; i++) {
|
|
||||||
md.update(toBeHashed);
|
|
||||||
toBeHashed = md.digest(); // this resets the digest
|
|
||||||
}
|
|
||||||
java.util.Arrays.fill(concat, (byte)0x00);
|
|
||||||
result = toBeHashed;
|
|
||||||
} else if (algo.equals("DESede")) {
|
|
||||||
// if the 2 salt halves are the same, invert one of them
|
|
||||||
int i;
|
|
||||||
for (i=0; i<4; i++) {
|
|
||||||
if (salt[i] != salt[i+4])
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (i==4) { // same, invert 1st half
|
|
||||||
for (i=0; i<2; i++) {
|
|
||||||
byte tmp = salt[i];
|
|
||||||
salt[i] = salt[3-i];
|
|
||||||
salt[3-1] = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now digest each half (concatenated with password). For each
|
|
||||||
// half, go through the loop as many times as specified by the
|
|
||||||
// iteration count parameter (inner for loop).
|
|
||||||
// Concatenate the output from each digest round with the
|
|
||||||
// password, and use the result as the input to the next digest
|
|
||||||
// operation.
|
|
||||||
byte[] kBytes = null;
|
|
||||||
IvParameterSpec iv = null;
|
|
||||||
byte[] toBeHashed = null;
|
|
||||||
result = new byte[DESedeKeySpec.DES_EDE_KEY_LEN +
|
|
||||||
DESConstants.DES_BLOCK_SIZE];
|
|
||||||
for (i = 0; i < 2; i++) {
|
|
||||||
toBeHashed = new byte[salt.length/2];
|
|
||||||
System.arraycopy(salt, i*(salt.length/2), toBeHashed, 0,
|
|
||||||
toBeHashed.length);
|
|
||||||
for (int j=0; j < iCount; j++) {
|
|
||||||
md.update(toBeHashed);
|
|
||||||
md.update(passwdBytes);
|
|
||||||
toBeHashed = md.digest(); // this resets the digest
|
|
||||||
}
|
|
||||||
System.arraycopy(toBeHashed, 0, result, i*16,
|
|
||||||
toBeHashed.length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void init(int opmode, Key key, AlgorithmParameters params,
|
|
||||||
SecureRandom random)
|
|
||||||
throws InvalidKeyException, InvalidAlgorithmParameterException {
|
|
||||||
PBEParameterSpec pbeSpec = null;
|
|
||||||
if (params != null) {
|
|
||||||
try {
|
|
||||||
pbeSpec = params.getParameterSpec(PBEParameterSpec.class);
|
|
||||||
} catch (InvalidParameterSpecException ipse) {
|
|
||||||
throw new InvalidAlgorithmParameterException("Wrong parameter "
|
|
||||||
+ "type: PBE "
|
|
||||||
+ "expected");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
init(opmode, key, pbeSpec, random);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Continues a multiple-part encryption or decryption operation
|
|
||||||
* (depending on how this cipher was initialized), processing another data
|
|
||||||
* part.
|
|
||||||
*
|
|
||||||
* <p>The first <code>inputLen</code> bytes in the <code>input</code>
|
|
||||||
* buffer, starting at <code>inputOffset</code>, are processed, and the
|
|
||||||
* result is stored in a new buffer.
|
|
||||||
*
|
|
||||||
* @param input the input buffer
|
|
||||||
* @param inputOffset the offset in <code>input</code> where the input
|
|
||||||
* starts
|
|
||||||
* @param inputLen the input length
|
|
||||||
*
|
|
||||||
* @return the new buffer with the result
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
byte[] update(byte[] input, int inputOffset, int inputLen) {
|
|
||||||
return cipher.update(input, inputOffset, inputLen);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Continues a multiple-part encryption or decryption operation
|
|
||||||
* (depending on how this cipher was initialized), processing another data
|
|
||||||
* part.
|
|
||||||
*
|
|
||||||
* <p>The first <code>inputLen</code> bytes in the <code>input</code>
|
|
||||||
* buffer, starting at <code>inputOffset</code>, are processed, and the
|
|
||||||
* result is stored in the <code>output</code> buffer, starting at
|
|
||||||
* <code>outputOffset</code>.
|
|
||||||
*
|
|
||||||
* @param input the input buffer
|
|
||||||
* @param inputOffset the offset in <code>input</code> where the input
|
|
||||||
* starts
|
|
||||||
* @param inputLen the input length
|
|
||||||
* @param output the buffer for the result
|
|
||||||
* @param outputOffset the offset in <code>output</code> where the result
|
|
||||||
* is stored
|
|
||||||
*
|
|
||||||
* @return the number of bytes stored in <code>output</code>
|
|
||||||
*
|
|
||||||
* @exception ShortBufferException if the given output buffer is too small
|
|
||||||
* to hold the result
|
|
||||||
*/
|
|
||||||
int update(byte[] input, int inputOffset, int inputLen,
|
|
||||||
byte[] output, int outputOffset)
|
|
||||||
throws ShortBufferException {
|
|
||||||
return cipher.update(input, inputOffset, inputLen,
|
|
||||||
output, outputOffset);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encrypts or decrypts data in a single-part operation,
|
|
||||||
* or finishes a multiple-part operation.
|
|
||||||
* The data is encrypted or decrypted, depending on how this cipher was
|
|
||||||
* initialized.
|
|
||||||
*
|
|
||||||
* <p>The first <code>inputLen</code> bytes in the <code>input</code>
|
|
||||||
* buffer, starting at <code>inputOffset</code>, and any input bytes that
|
|
||||||
* may have been buffered during a previous <code>update</code> operation,
|
|
||||||
* are processed, with padding (if requested) being applied.
|
|
||||||
* The result is stored in a new buffer.
|
|
||||||
*
|
|
||||||
* <p>The cipher is reset to its initial state (uninitialized) after this
|
|
||||||
* call.
|
|
||||||
*
|
|
||||||
* @param input the input buffer
|
|
||||||
* @param inputOffset the offset in <code>input</code> where the input
|
|
||||||
* starts
|
|
||||||
* @param inputLen the input length
|
|
||||||
*
|
|
||||||
* @return the new buffer with the result
|
|
||||||
*
|
|
||||||
* @exception IllegalBlockSizeException if this cipher is a block cipher,
|
|
||||||
* no padding has been requested (only in encryption mode), and the total
|
|
||||||
* input length of the data processed by this cipher is not a multiple of
|
|
||||||
* block size
|
|
||||||
* @exception BadPaddingException if decrypting and padding is chosen,
|
|
||||||
* but the last input data does not have proper padding bytes.
|
|
||||||
*/
|
|
||||||
byte[] doFinal(byte[] input, int inputOffset, int inputLen)
|
|
||||||
throws IllegalBlockSizeException, BadPaddingException {
|
|
||||||
return cipher.doFinal(input, inputOffset, inputLen);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encrypts or decrypts data in a single-part operation,
|
|
||||||
* or finishes a multiple-part operation.
|
|
||||||
* The data is encrypted or decrypted, depending on how this cipher was
|
|
||||||
* initialized.
|
|
||||||
*
|
|
||||||
* <p>The first <code>inputLen</code> bytes in the <code>input</code>
|
|
||||||
* buffer, starting at <code>inputOffset</code>, and any input bytes that
|
|
||||||
* may have been buffered during a previous <code>update</code> operation,
|
|
||||||
* are processed, with padding (if requested) being applied.
|
|
||||||
* The result is stored in the <code>output</code> buffer, starting at
|
|
||||||
* <code>outputOffset</code>.
|
|
||||||
*
|
|
||||||
* <p>The cipher is reset to its initial state (uninitialized) after this
|
|
||||||
* call.
|
|
||||||
*
|
|
||||||
* @param input the input buffer
|
|
||||||
* @param inputOffset the offset in <code>input</code> where the input
|
|
||||||
* starts
|
|
||||||
* @param inputLen the input length
|
|
||||||
* @param output the buffer for the result
|
|
||||||
* @param outputOffset the offset in <code>output</code> where the result
|
|
||||||
* is stored
|
|
||||||
*
|
|
||||||
* @return the number of bytes stored in <code>output</code>
|
|
||||||
*
|
|
||||||
* @exception IllegalBlockSizeException if this cipher is a block cipher,
|
|
||||||
* no padding has been requested (only in encryption mode), and the total
|
|
||||||
* input length of the data processed by this cipher is not a multiple of
|
|
||||||
* block size
|
|
||||||
* @exception ShortBufferException if the given output buffer is too small
|
|
||||||
* to hold the result
|
|
||||||
* @exception BadPaddingException if decrypting and padding is chosen,
|
|
||||||
* but the last input data does not have proper padding bytes.
|
|
||||||
*/
|
|
||||||
int doFinal(byte[] input, int inputOffset, int inputLen,
|
|
||||||
byte[] output, int outputOffset)
|
|
||||||
throws ShortBufferException, IllegalBlockSizeException,
|
|
||||||
BadPaddingException {
|
|
||||||
return cipher.doFinal(input, inputOffset, inputLen,
|
|
||||||
output, outputOffset);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wrap a key.
|
|
||||||
*
|
|
||||||
* @param key the key to be wrapped.
|
|
||||||
*
|
|
||||||
* @return the wrapped key.
|
|
||||||
*
|
|
||||||
* @exception IllegalBlockSizeException if this cipher is a block
|
|
||||||
* cipher, no padding has been requested, and the length of the
|
|
||||||
* encoding of the key to be wrapped is not a
|
|
||||||
* multiple of the block size.
|
|
||||||
*
|
|
||||||
* @exception InvalidKeyException if it is impossible or unsafe to
|
|
||||||
* wrap the key with this cipher (e.g., a hardware protected key is
|
|
||||||
* being passed to a software only cipher).
|
|
||||||
*/
|
|
||||||
byte[] wrap(Key key)
|
|
||||||
throws IllegalBlockSizeException, InvalidKeyException {
|
|
||||||
byte[] result = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
byte[] encodedKey = key.getEncoded();
|
|
||||||
if ((encodedKey == null) || (encodedKey.length == 0)) {
|
|
||||||
throw new InvalidKeyException("Cannot get an encoding of " +
|
|
||||||
"the key to be wrapped");
|
|
||||||
}
|
|
||||||
|
|
||||||
result = doFinal(encodedKey, 0, encodedKey.length);
|
|
||||||
} catch (BadPaddingException e) {
|
|
||||||
// Should never happen
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unwrap a previously wrapped key.
|
|
||||||
*
|
|
||||||
* @param wrappedKey the key to be unwrapped.
|
|
||||||
*
|
|
||||||
* @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
|
|
||||||
*
|
|
||||||
* @param wrappedKeyType the type of the wrapped key.
|
|
||||||
* This is one of <code>Cipher.SECRET_KEY</code>,
|
|
||||||
* <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
|
|
||||||
*
|
|
||||||
* @return the unwrapped key.
|
|
||||||
*
|
|
||||||
* @exception NoSuchAlgorithmException if no installed providers
|
|
||||||
* can create keys of type <code>wrappedKeyType</code> for the
|
|
||||||
* <code>wrappedKeyAlgorithm</code>.
|
|
||||||
*
|
|
||||||
* @exception InvalidKeyException if <code>wrappedKey</code> does not
|
|
||||||
* represent a wrapped key of type <code>wrappedKeyType</code> for
|
|
||||||
* the <code>wrappedKeyAlgorithm</code>.
|
|
||||||
*/
|
|
||||||
Key unwrap(byte[] wrappedKey,
|
|
||||||
String wrappedKeyAlgorithm,
|
|
||||||
int wrappedKeyType)
|
|
||||||
throws InvalidKeyException, NoSuchAlgorithmException {
|
|
||||||
byte[] encodedKey;
|
|
||||||
try {
|
|
||||||
encodedKey = doFinal(wrappedKey, 0, wrappedKey.length);
|
|
||||||
} catch (BadPaddingException ePadding) {
|
|
||||||
throw new InvalidKeyException("The wrapped key is not padded " +
|
|
||||||
"correctly");
|
|
||||||
} catch (IllegalBlockSizeException eBlockSize) {
|
|
||||||
throw new InvalidKeyException("The wrapped key does not have " +
|
|
||||||
"the correct length");
|
|
||||||
}
|
|
||||||
return ConstructKeys.constructKey(encodedKey, wrappedKeyAlgorithm,
|
|
||||||
wrappedKeyType);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -260,7 +260,7 @@ final class PBES1Core {
|
|||||||
|
|
||||||
if (algo.equals("DES")) {
|
if (algo.equals("DES")) {
|
||||||
// P || S (password concatenated with salt)
|
// P || S (password concatenated with salt)
|
||||||
byte[] concat = new byte[passwdBytes.length + salt.length];
|
byte[] concat = new byte[Math.addExact(passwdBytes.length, salt.length)];
|
||||||
System.arraycopy(passwdBytes, 0, concat, 0, passwdBytes.length);
|
System.arraycopy(passwdBytes, 0, concat, 0, passwdBytes.length);
|
||||||
java.util.Arrays.fill(passwdBytes, (byte)0x00);
|
java.util.Arrays.fill(passwdBytes, (byte)0x00);
|
||||||
System.arraycopy(salt, 0, concat, passwdBytes.length, salt.length);
|
System.arraycopy(salt, 0, concat, passwdBytes.length, salt.length);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -26,6 +26,7 @@
|
|||||||
package com.sun.crypto.provider;
|
package com.sun.crypto.provider;
|
||||||
|
|
||||||
import javax.crypto.ShortBufferException;
|
import javax.crypto.ShortBufferException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class implements padding as specified in the PKCS#5 standard.
|
* This class implements padding as specified in the PKCS#5 standard.
|
||||||
@ -63,14 +64,13 @@ final class PKCS5Padding implements Padding {
|
|||||||
if (in == null)
|
if (in == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if ((off + len) > in.length) {
|
int idx = Math.addExact(off, len);
|
||||||
|
if (idx > in.length) {
|
||||||
throw new ShortBufferException("Buffer too small to hold padding");
|
throw new ShortBufferException("Buffer too small to hold padding");
|
||||||
}
|
}
|
||||||
|
|
||||||
byte paddingOctet = (byte) (len & 0xff);
|
byte paddingOctet = (byte) (len & 0xff);
|
||||||
for (int i = 0; i < len; i++) {
|
Arrays.fill(in, off, idx, paddingOctet);
|
||||||
in[i + off] = paddingOctet;
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,25 +92,24 @@ final class PKCS5Padding implements Padding {
|
|||||||
(len == 0)) { // this can happen if input is really a padded buffer
|
(len == 0)) { // this can happen if input is really a padded buffer
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
int idx = Math.addExact(off, len);
|
||||||
byte lastByte = in[off + len - 1];
|
byte lastByte = in[idx - 1];
|
||||||
int padValue = (int)lastByte & 0x0ff;
|
int padValue = (int)lastByte & 0x0ff;
|
||||||
if ((padValue < 0x01)
|
if ((padValue < 0x01)
|
||||||
|| (padValue > blockSize)) {
|
|| (padValue > blockSize)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int start = off + len - ((int)lastByte & 0x0ff);
|
int start = idx - padValue;
|
||||||
if (start < off) {
|
if (start < off) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < ((int)lastByte & 0x0ff); i++) {
|
for (int i = start; i < idx; i++) {
|
||||||
if (in[start+i] != lastByte) {
|
if (in[i] != lastByte) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return start;
|
return start;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user