Removed all incidents of e.printStackTrace, wrapped errors with

NoDashFatalExceptions instead of overwriting
This commit is contained in:
Dave
2014-12-26 17:15:54 +02:00
parent 3b97efec74
commit ee72415e63
10 changed files with 69 additions and 55 deletions

View File

@@ -19,7 +19,7 @@ public abstract class NoConfigBase implements NoConfigInterface {
keyGenerator.init(NoUtil.AES_STRENGTH);
this.secretKey = keyGenerator.generateKey();
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for CIPHER_KEY_SPEC not valid.");
throw new NoDashFatalException("Value for CIPHER_KEY_SPEC not valid.", e);
}
}

View File

@@ -63,7 +63,7 @@ public final class NoConfigDefault extends NoConfigBase implements Serializable
Files.write(file.toPath(), data, StandardOpenOption.CREATE_NEW);
} catch (IOException e) {
throw new NoDashFatalException("Unable to save config, including generated secret key.");
throw new NoDashFatalException("Unable to save config, including generated secret key.", e);
}
}
@@ -77,7 +77,7 @@ public final class NoConfigDefault extends NoConfigBase implements Serializable
try {
noConfig = (NoConfigDefault) ois.readObject();
} catch (ClassNotFoundException e) {
throw new NoDashFatalException("Given bytestream does not compile into a configuration object.");
throw new NoDashFatalException("Given bytestream does not compile into a configuration object.", e);
}
return noConfig;
}

View File

@@ -84,14 +84,14 @@ public final class NoUtil {
try {
skf = SecretKeyFactory.getInstance(NoUtil.PBE_TYPE);
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for PBE_TYPE is not valid.");
throw new NoDashFatalException("Value for PBE_TYPE is not valid.", e);
}
KeySpec spec = new PBEKeySpec(password, NoCore.config.getSecretKey().getEncoded(), 65536, 256);
SecretKey key;
try {
key = skf.generateSecret(spec);
} catch (InvalidKeySpecException e) {
throw new NoDashFatalException("PBE manager unable to derive key from password.");
throw new NoDashFatalException("PBE manager unable to derive key from password.", e);
}
NoUtil.wipeChars(password);
return key.getEncoded();
@@ -102,9 +102,8 @@ public final class NoUtil {
MessageDigest messageDigest = MessageDigest.getInstance(NoUtil.DIGEST_TYPE);
return messageDigest.digest(bytes);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new NoDashFatalException("Value for DIGEST_TYPE not valid.", e);
}
return null;
}
public static byte[] decryptByteArray(byte[] data, char[] password) throws IllegalBlockSizeException, BadPaddingException {
@@ -126,24 +125,23 @@ public final class NoUtil {
try {
cipher = Cipher.getInstance(NoUtil.CIPHER_TYPE);
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for CIPHER_TYPE is not valid (no such algorithm).");
throw new NoDashFatalException("Value for CIPHER_TYPE is not valid (no such algorithm).", e);
} catch (NoSuchPaddingException e) {
throw new NoDashFatalException("Value for CIPHER_TYPE is not valid (no such padding).");
throw new NoDashFatalException("Value for CIPHER_TYPE is not valid (no such padding).", e);
}
SecretKeySpec secretKey = new SecretKeySpec(key, NoUtil.CIPHER_KEY_SPEC);
try {
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new NoDashFatalException("Secret key is invalid.");
throw new NoDashFatalException("Secret key is invalid.", e);
}
try {
return cipher.doFinal(data);
} catch (IllegalBlockSizeException e) {
throw new NoDashFatalException("Block size exception encountered during encryption.");
throw new NoDashFatalException("Block size exception encountered during encryption.", e);
} catch (BadPaddingException e) {
throw new NoDashFatalException("Bad padding exception encountered during encryption.");
throw new NoDashFatalException("Bad padding exception encountered during encryption.", e);
}
}
@@ -156,16 +154,15 @@ public final class NoUtil {
try {
cipher = Cipher.getInstance(NoUtil.CIPHER_TYPE);
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for CIPHER_TYPE is not valid (no such algorithm).");
throw new NoDashFatalException("Value for CIPHER_TYPE is not valid (no such algorithm).", e);
} catch (NoSuchPaddingException e) {
throw new NoDashFatalException("Value for CIPHER_TYPE is not valid (no such padding).");
throw new NoDashFatalException("Value for CIPHER_TYPE is not valid (no such padding).", e);
}
SecretKeySpec secretKey = new SecretKeySpec(key, NoUtil.CIPHER_KEY_SPEC);
try {
cipher.init(Cipher.DECRYPT_MODE, secretKey);
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new NoDashFatalException("Secret key is invalid.");
throw new NoDashFatalException("Secret key is invalid.", e);
}
return cipher.doFinal(data);
@@ -180,19 +177,19 @@ public final class NoUtil {
try {
cipher = Cipher.getInstance(NoUtil.CIPHER_RSA_TYPE);
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for CIPHER_RSA_TYPE is not valid (no such algorithm).");
throw new NoDashFatalException("Value for CIPHER_RSA_TYPE is not valid (no such algorithm).", e);
} catch (NoSuchPaddingException e) {
throw new NoDashFatalException("Value for CIPHER_RSA_TYPE is not valid (no such padding).");
throw new NoDashFatalException("Value for CIPHER_RSA_TYPE is not valid (no such padding).", e);
}
try {
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
} catch (InvalidKeyException e){
throw new NoDashFatalException("Public key invalid.");
throw new NoDashFatalException("Public key invalid.", e);
} catch (IllegalBlockSizeException e) {
throw new NoDashFatalException("Unable to encrypt data stream with public key.");
throw new NoDashFatalException("Unable to encrypt data stream with public key.", e);
} catch (BadPaddingException e) {
throw new NoDashFatalException("Unable to encrypt data stream with public key.");
throw new NoDashFatalException("Unable to encrypt data stream with public key.", e);
}
}
@@ -201,9 +198,9 @@ public final class NoUtil {
try {
cipher = Cipher.getInstance(NoUtil.CIPHER_RSA_TYPE);
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for CIPHER_RSA_TYPE is not valid (no such algorithm).");
throw new NoDashFatalException("Value for CIPHER_RSA_TYPE is not valid (no such algorithm).", e);
} catch (NoSuchPaddingException e) {
throw new NoDashFatalException("Value for CIPHER_RSA_TYPE is not valid (no such padding).");
throw new NoDashFatalException("Value for CIPHER_RSA_TYPE is not valid (no such padding).", e);
}
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);

View File

@@ -62,9 +62,9 @@ public final class NoHashSphereDefault implements NoHashSphereInterface {
ois.close();
bais.close();
} catch (IOException e){
throw new NoDashFatalException("Unable to load up given database file.");
throw new NoDashFatalException("Unable to load up given database file.", e);
} catch (ClassNotFoundException e) {
throw new NoDashFatalException("Database file not in a verifiable format.");
throw new NoDashFatalException("Database file not in a verifiable format.", e);
}
}
}

View File

@@ -98,7 +98,7 @@ public final class NoSessionSphere {
try {
session.consume(nbs);
} catch (NoByteSetBadDecryptionException e) {
e.printStackTrace();
throw new NoDashFatalException("Bad byte sets on consumption.", e);
}
} /* 2.2 Alternatively, no NoByteSets to consume */
@@ -173,15 +173,15 @@ public final class NoSessionSphere {
try {
result.data = NoSessionSphere.save(result.cookie, password);
} catch (NoDashSessionBadUUIDException e) {
throw new NoDashFatalException("Immediately generated cookie throwing bad cookie error.");
throw new NoDashFatalException("Immediately generated cookie throwing bad cookie error.", e);
} catch (NoSessionExpiredException e) {
throw new NoDashFatalException("Session expired before it was even returned to client.");
throw new NoDashFatalException("Session expired before it was even returned to client.", e);
} catch (NoSessionConfirmedException e) {
throw new NoDashFatalException("Session is in confirmed state before it was returned to client.");
throw new NoDashFatalException("Session is in confirmed state before it was returned to client.", e);
} catch (NoSessionNotChangedException e) {
throw new NoDashFatalException("Session claims to be unchanged but user is newly registered.");
throw new NoDashFatalException("Session claims to be unchanged but user is newly registered.", e);
} catch (NoSessionAlreadyAwaitingConfirmationException e) {
throw new NoDashFatalException("Session claims to be awaiting confirmation before returning data to the user.");
throw new NoDashFatalException("Session claims to be awaiting confirmation before returning data to the user.", e);
}
return result;
}

View File

@@ -28,4 +28,8 @@ public class NoDashFatalException extends RuntimeException {
public NoDashFatalException(String string) {
super(string);
}
public NoDashFatalException(String string, Exception e) {
super(string, e);
}
}

View File

@@ -50,7 +50,7 @@ public abstract class NoInfluence implements Serializable {
try {
keyGen = KeyGenerator.getInstance(NoUtil.CIPHER_KEY_SPEC);
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for CIPHER_KEY_SPEC is not valid.");
throw new NoDashFatalException("Value for CIPHER_KEY_SPEC is not valid.", e);
}
keyGen.init(NoUtil.AES_STRENGTH);
SecretKey secretKey = keyGen.generateKey();
@@ -72,7 +72,7 @@ public abstract class NoInfluence implements Serializable {
baos.close();
return encrypted;
} catch (IOException e) {
throw new NoDashFatalException("Unable to write NoInfluence object to byte stream.");
throw new NoDashFatalException("Unable to write NoInfluence object to byte stream.", e);
}
}
@@ -86,7 +86,7 @@ public abstract class NoInfluence implements Serializable {
bais.close();
return noInfluence;
} catch (IOException e) {
throw new NoDashFatalException("Unable to read out provided data stream.");
throw new NoDashFatalException("Unable to read out provided data stream.", e);
}
}

View File

@@ -8,6 +8,9 @@ import java.util.UUID;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import nodash.core.NoCore;
import nodash.core.NoUtil;
import nodash.exceptions.NoByteSetBadDecryptionException;
@@ -109,7 +112,8 @@ public final class NoSession implements Serializable {
return file;
}
public void confirmSave(byte[] confirmData, char[] password) throws NoSessionConfirmedException, NoSessionExpiredException, NoSessionNotAwaitingConfirmationException, NoUserNotValidException {
public void confirmSave(byte[] confirmData, char[] password) throws NoSessionConfirmedException, NoSessionExpiredException,
NoSessionNotAwaitingConfirmationException, NoUserNotValidException {
this.check();
if (this.state != NoState.AWAITING_CONFIRMATION) {
throw new NoSessionNotAwaitingConfirmationException();
@@ -138,15 +142,14 @@ public final class NoSession implements Serializable {
try {
NoCore.hashSphere.removeHash(this.original.createHashString());
} catch (IOException e) {
throw new NoDashFatalException("Unable to remove hash on confirm.");
throw new NoDashFatalException("Unable to remove hash on confirm.", e);
}
}
/* 5.2.2: add new hash to array */
try {
NoCore.hashSphere.insertHash(this.current.createHashString());
} catch (IOException e) {
e.printStackTrace();
throw new NoDashFatalException("Unable to remove hash on confirm.");
throw new NoDashFatalException("Unable to remove hash on confirm.", e);
}
/* 5.2.3: clear influences as they will not need to be re-applied */
@@ -185,7 +188,11 @@ public final class NoSession implements Serializable {
}
public byte[] getEncryptedUUID() {
return NoUtil.encrypt(this.uuid.toString().getBytes());
try {
return NoUtil.encrypt(Base64.decode(this.uuid.toString()));
} catch (Base64DecodingException e) {
throw new NoDashFatalException("Base64DecodingException while decoding session UUID.", e);
}
}
public String getEncryptedUUIDAsString() {

View File

@@ -45,6 +45,7 @@ import javax.crypto.spec.SecretKeySpec;
import sun.security.rsa.RSAPublicKeyImpl;
import nodash.core.NoUtil;
import nodash.exceptions.NoByteSetBadDecryptionException;
import nodash.exceptions.NoDashFatalException;
public class NoUser implements Serializable {
private static final long serialVersionUID = 7132405837081692211L;
@@ -57,19 +58,26 @@ public class NoUser implements Serializable {
private ArrayList<NoAction> outgoing = new ArrayList<NoAction>();
public NoUser() {
KeyPairGenerator kpg;
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(NoUtil.KEYPAIR_ALGORITHM);
kpg.initialize(NoUtil.RSA_STRENGTH, SecureRandom.getInstance(NoUtil.SECURERANDOM_ALGORITHM, NoUtil.SECURERANDOM_PROVIDER));
KeyPair keyPair = kpg.generateKeyPair();
this.publicKey = keyPair.getPublic();
this.privateKey = keyPair.getPrivate();
this.influences = 0;
this.actions = 0;
kpg = KeyPairGenerator.getInstance(NoUtil.KEYPAIR_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
throw new NoDashFatalException("Value for KEYPAIR_ALGORITHM is not valid.", e);
}
try {
kpg.initialize(NoUtil.RSA_STRENGTH, SecureRandom.getInstance(NoUtil.SECURERANDOM_ALGORITHM, NoUtil.SECURERANDOM_PROVIDER));
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for SECURERANDOM_ALGORITHM not valid.", e);
} catch (NoSuchProviderException e) {
throw new NoDashFatalException("Value for SECURERANDOM_PROVIDER not valid.", e);
}
KeyPair keyPair = kpg.generateKeyPair();
this.publicKey = keyPair.getPublic();
this.privateKey = keyPair.getPrivate();
this.influences = 0;
this.actions = 0;
}
public final byte[] createFile(char[] password) {
@@ -84,11 +92,10 @@ public class NoUser implements Serializable {
baos.close();
return encrypted;
} catch (IOException e) {
e.printStackTrace();
throw new NoDashFatalException("IO Exception encountered while generating encrypted user file byte stream.", e);
} finally {
this.outgoing = temp;
}
return null;
}
public final byte[] createHash() {
@@ -101,11 +108,10 @@ public class NoUser implements Serializable {
byte[] userBytes = baos.toByteArray();
return NoUtil.getHashFromByteArray(userBytes);
} catch (IOException e) {
e.printStackTrace();
throw new NoDashFatalException("IO Exception encountered while generating user hash.", e);
} finally {
this.outgoing = temp;
}
return null;
}
public final String createHashString() {

View File

@@ -48,7 +48,7 @@ public abstract class NoTargetedAction extends NoAction {
}
} catch (NoCannotGetInfluenceException e) {
if (e.getResponseInfluence() != null) {
throw new NoDashFatalException("Unsourced action has generated an error with an undeliverable influence.");
throw new NoDashFatalException("Unsourced action has generated an error with an undeliverable influence.", e);
}
}
}