Skip to content

Commit c45325d

Browse files
committed
Change assertions into what should have been explicit runtime checks
and throw an appropriate exception if violated.
1 parent 02aa0c7 commit c45325d

2 files changed

Lines changed: 51 additions & 8 deletions

File tree

‎src/main/java/org/owasp/esapi/crypto/CipherSpec.java‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public final class CipherSpec implements Serializable {
4545
private int keySize_ = ESAPI.securityConfiguration().getEncryptionKeyLength(); // In bits
4646
private int blockSize_ = 16; // In bytes! I.e., 128 bits!!!
4747
private byte[] iv_ = null;
48+
49+
private boolean blockSizeExplicitlySet = false; // Used for check in setIV().
4850

4951
// Cipher transformation component. Format is ALG/MODE/PADDING
5052
private enum CipherTransformationComponent { ALG, MODE, PADDING }
@@ -72,7 +74,7 @@ public CipherSpec(String cipherXform, int keySize, int blockSize, final byte[] i
7274
public CipherSpec(String cipherXform, int keySize, int blockSize) {
7375
// Note: Do NOT use
7476
// this(cipherXform, keySize, blockSize, null);
75-
// because of assertion in setIV().
77+
// because of checks in setIV().
7678
//
7779
setCipherTransformation(cipherXform);
7880
setKeySize(keySize);
@@ -194,7 +196,9 @@ public String getCipherTransformation() {
194196
* @return This current {@code CipherSpec} object.
195197
*/
196198
public CipherSpec setKeySize(int keySize) {
197-
assert keySize > 0 : "keySize must be > 0; keySize=" + keySize;
199+
if ( keySize <= 0 ) {
200+
throw new IllegalArgumentException("keySize must be > 0; keySize=" + keySize);
201+
}
198202
this.keySize_ = keySize;
199203
return this;
200204
}
@@ -209,12 +213,16 @@ public int getKeySize() {
209213

210214
/**
211215
* Set the block size for this {@code CipherSpec}.
212-
* @param blockSize The block size, in bytes. Must be positive integer.
216+
* @param blockSize The block size, in bytes. Must be positive integer appropriate
217+
* for the specified cipher algorithm.
213218
* @return This current {@code CipherSpec} object.
214219
*/
215220
public CipherSpec setBlockSize(int blockSize) {
216-
assert blockSize > 0 : "blockSize must be > 0; blockSize=" + blockSize;
221+
if ( blockSize <= 0 ) {
222+
throw new IllegalArgumentException("blockSize must be > 0; blockSize=" + blockSize);
223+
}
217224
this.blockSize_ = blockSize;
225+
blockSizeExplicitlySet = true;
218226
return this;
219227
}
220228

@@ -266,9 +274,23 @@ public byte[] getIV() {
266274
* @return This current {@code CipherSpec} object.
267275
*/
268276
public CipherSpec setIV(final byte[] iv) {
269-
assert requiresIV() && (iv != null && iv.length != 0) : "Required IV cannot be null or 0 length";
270-
// Don't store a reference, but make a copy!
277+
if ( ! ( requiresIV() && (iv != null && iv.length != 0) ) ) {
278+
throw new IllegalArgumentException("Required IV cannot be null or 0 length.");
279+
}
280+
281+
// Don't store a reference, but make a copy! When an IV is provided, it generally should
282+
// be the same length as the block size of the cipher.
271283
if ( iv != null ) { // Allow null IV for ECB mode.
284+
// TODO: FIXME: As per email from Jeff Walton to Kevin Wall dated 12/03/2013,
285+
// this is not always true. E.g., for CCM, the IV length is supposed
286+
// to be 7, 8, 9, 10, 11, 12, or 13 octets because of
287+
// it's formatting function.
288+
/***
289+
if ( iv.length != this.getBlockSize() && blockSizeExplicitlySet ) {
290+
throw new IllegalArgumentException("IV must be same length as cipher block size (" +
291+
this.getBlockSize() + " bytes)");
292+
}
293+
***/
272294
iv_ = new byte[ iv.length ];
273295
CryptoHelper.copyByteArray(iv, iv_);
274296
}

‎src/main/java/org/owasp/esapi/crypto/CipherText.java‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.owasp.esapi.Encryptor;
2727
import org.owasp.esapi.Logger;
2828
import org.owasp.esapi.errors.EncryptionException;
29+
import org.owasp.esapi.errors.EnterpriseSecurityRuntimeException;
2930

3031
// CHECKME: Some of these assertions probably should be actual runtime checks
3132
// with suitable exceptions to account for cases where programmers
@@ -457,7 +458,18 @@ public boolean validateMAC(SecretKey authKey) {
457458
// compare to stored value (separate_mac_). If same, then return true,
458459
// else return false.
459460
byte[] mac = computeMAC(authKey);
460-
assert mac.length == separate_mac_.length : "MACs are of differnt lengths. Should both be the same.";
461+
if ( mac.length != separate_mac_.length ) {
462+
// Note: We want some type of unchecked exception
463+
// here so this will not require code changes.
464+
// Unfortunately, EncryptionException, which might
465+
// make more sense here, is not a RuntimeException.
466+
String exm = "MACs are of different lengths. " +
467+
"Should both be the same length";
468+
throw new EnterpriseSecurityRuntimeException(exm,
469+
"Possible tampering of MAC? " + exm +
470+
"computed MAC len: " + mac.length +
471+
", received MAC len: " + separate_mac_.length);
472+
}
461473
return CryptoHelper.arrayCompare(mac, separate_mac_); // Safe compare!!!
462474
} else if ( ! requiresMAC ) { // Doesn't require a MAC
463475
return true;
@@ -593,6 +605,9 @@ int kdfPRFAsInt() {
593605

594606
public void setKDF_PRF(int prfSelection) {
595607
assert prfSelection >= 0 && prfSelection <= 15 : "kdfPrf == " + prfSelection + " must be between 0 and 15.";
608+
if ( prfSelection < 0 || prfSelection > 15 ) {
609+
throw new IllegalArgumentException("kdfPrf == " + prfSelection + " must be between 0 and 15, inclusive.");
610+
}
596611
kdfPrfSelection_ = prfSelection;
597612
}
598613

@@ -629,7 +644,9 @@ private void setEncryptionTimestamp() {
629644
* January 1, 1970 GMT).
630645
*/ // Package level access. ESAPI jar should be sealed and signed.
631646
void setEncryptionTimestamp(long timestamp) {
632-
assert timestamp > 0 : "Timestamp must be greater than zero.";
647+
if ( timestamp <= 0 ) {
648+
throw new IllegalArgumentException("Timestamp must be greater than zero.");
649+
}
633650
if ( encryption_timestamp_ == 0 ) { // Only set it if it's not yet been set.
634651
logger.warning(Logger.EVENT_FAILURE, "Attempt to reset non-zero " +
635652
"CipherText encryption timestamp to " + new Date(timestamp) + "!");
@@ -767,6 +784,10 @@ protected boolean canEqual(Object other) {
767784
* @return The value for the MAC.
768785
*/
769786
private byte[] computeMAC(SecretKey authKey) {
787+
// These assertions are okay and leaving them as assertions rather than
788+
// changing the to conditional statements that throw should be all right
789+
// because this is private method and presumably we should have already
790+
// checked things in the public or protected methods where appropriate.
770791
assert raw_ciphertext_ != null && raw_ciphertext_.length != 0 : "Raw ciphertext may not be null or empty.";
771792
assert authKey != null && authKey.getEncoded().length != 0 : "Authenticity secret key may not be null or zero length.";
772793
try {

0 commit comments

Comments
 (0)