diff --git a/src/nodash/core/NoConfigBase.java b/src/nodash/core/NoConfigBase.java index abf6799..1e30424 100644 --- a/src/nodash/core/NoConfigBase.java +++ b/src/nodash/core/NoConfigBase.java @@ -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); } } diff --git a/src/nodash/core/NoConfigDefault.java b/src/nodash/core/NoConfigDefault.java index 01dca54..8d06c99 100644 --- a/src/nodash/core/NoConfigDefault.java +++ b/src/nodash/core/NoConfigDefault.java @@ -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; } diff --git a/src/nodash/core/NoUtil.java b/src/nodash/core/NoUtil.java index 9103abb..231a915 100644 --- a/src/nodash/core/NoUtil.java +++ b/src/nodash/core/NoUtil.java @@ -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); diff --git a/src/nodash/core/spheres/NoHashSphereDefault.java b/src/nodash/core/spheres/NoHashSphereDefault.java index b5d63dc..6a18cf1 100644 --- a/src/nodash/core/spheres/NoHashSphereDefault.java +++ b/src/nodash/core/spheres/NoHashSphereDefault.java @@ -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); } } } diff --git a/src/nodash/core/spheres/NoSessionSphere.java b/src/nodash/core/spheres/NoSessionSphere.java index 5652a81..3217068 100644 --- a/src/nodash/core/spheres/NoSessionSphere.java +++ b/src/nodash/core/spheres/NoSessionSphere.java @@ -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; } diff --git a/src/nodash/exceptions/NoDashFatalException.java b/src/nodash/exceptions/NoDashFatalException.java index faab5b8..24674fa 100644 --- a/src/nodash/exceptions/NoDashFatalException.java +++ b/src/nodash/exceptions/NoDashFatalException.java @@ -28,4 +28,8 @@ public class NoDashFatalException extends RuntimeException { public NoDashFatalException(String string) { super(string); } + + public NoDashFatalException(String string, Exception e) { + super(string, e); + } } diff --git a/src/nodash/models/NoInfluence.java b/src/nodash/models/NoInfluence.java index 582a013..96729bf 100644 --- a/src/nodash/models/NoInfluence.java +++ b/src/nodash/models/NoInfluence.java @@ -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); } } diff --git a/src/nodash/models/NoSession.java b/src/nodash/models/NoSession.java index c31f72f..80b8b35 100644 --- a/src/nodash/models/NoSession.java +++ b/src/nodash/models/NoSession.java @@ -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() { diff --git a/src/nodash/models/NoUser.java b/src/nodash/models/NoUser.java index 0f46d0d..4477fcf 100644 --- a/src/nodash/models/NoUser.java +++ b/src/nodash/models/NoUser.java @@ -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 outgoing = new ArrayList(); 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() { diff --git a/src/nodash/models/noactiontypes/NoTargetedAction.java b/src/nodash/models/noactiontypes/NoTargetedAction.java index 62dcf9f..e6dfb1f 100644 --- a/src/nodash/models/noactiontypes/NoTargetedAction.java +++ b/src/nodash/models/noactiontypes/NoTargetedAction.java @@ -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); } } }