Initial commit

This commit is contained in:
Dave
2014-12-16 01:01:48 +02:00
commit 38c2d0de5d
30 changed files with 1377 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
package nodash.models;
import java.io.Serializable;
public abstract class NoAction implements Serializable {
private static final long serialVersionUID = -194752850197321803L;
public abstract void execute();
public abstract void purge();
}

View File

@@ -0,0 +1,12 @@
package nodash.models;
public final class NoByteSet {
public byte[] key;
public byte[] data;
public NoByteSet(byte[] key, byte[] data) {
this.key = key;
this.data = data;
}
}

View File

@@ -0,0 +1,70 @@
package nodash.models;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import nodash.core.NoUtil;
import nodash.exceptions.NoDashFatalException;
public abstract class NoInfluence implements Serializable {
private static final long serialVersionUID = -7509462039664862920L;
public abstract void applyTo(NoUser user);
public final NoByteSet getByteSet(PublicKey publicKey) {
KeyGenerator keyGen;
try {
keyGen = KeyGenerator.getInstance(NoUtil.CIPHER_KEY_SPEC);
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for CIPHER_KEY_SPEC is not valid.");
}
keyGen.init(NoUtil.AES_STRENGTH);
SecretKey secretKey = keyGen.generateKey();
System.out.println(secretKey.getClass().toString());
byte[] key = secretKey.getEncoded();
byte[] encryptedKey = NoUtil.encryptRSA(key, publicKey);
byte[] data = this.getEncrypted(key);
NoUtil.wipeBytes(key);
return new NoByteSet(encryptedKey, data);
}
private final byte[] getEncrypted(byte[] key) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
byte[] encrypted = NoUtil.encrypt(baos.toByteArray(), key);
oos.close();
baos.close();
return encrypted;
} catch (IOException e) {
throw new NoDashFatalException("Unable to write NoInfluence object to byte stream.");
}
}
public static NoInfluence decrypt(byte[] data, byte[] key) throws IllegalBlockSizeException, BadPaddingException, ClassNotFoundException {
byte[] decrypted = NoUtil.decrypt(data, key);
ByteArrayInputStream bais = new ByteArrayInputStream(decrypted);
try {
ObjectInputStream ois = new ObjectInputStream(bais);
NoInfluence noInfluence = (NoInfluence) ois.readObject();
ois.close();
bais.close();
return noInfluence;
} catch (IOException e) {
throw new NoDashFatalException("Unable to read out provided data stream.");
}
}
}

View File

@@ -0,0 +1,220 @@
package nodash.models;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.UUID;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import nodash.core.NoUtil;
import nodash.core.spheres.NoHashSphere;
import nodash.exceptions.NoByteSetBadDecryptionException;
import nodash.exceptions.NoDashFatalException;
import nodash.exceptions.NoDashSessionBadUUID;
import nodash.exceptions.NoSessionConfirmedException;
import nodash.exceptions.NoSessionExpiredException;
import nodash.exceptions.NoSessionNotAwaitingConfirmationException;
import nodash.exceptions.NoUserNotValidException;
public final class NoSession implements Serializable {
private static final long serialVersionUID = 1814807373427948931L;
public static final long SESSION_DURATION = 1000 * 60 * 30; //30 minute sessions
public static enum NoState {
IDLE, MODIFIED, AWAITING_CONFIRMATION, CONFIRMED, CLOSED;
};
private NoUser original;
private NoState state;
private final long expiry;
private boolean newUserSession;
public ArrayList<NoByteSet> incoming;
public NoUser current;
public UUID uuid;
public NoSession() {
this.state = NoState.IDLE;
this.expiry = System.currentTimeMillis() + NoSession.SESSION_DURATION;
this.uuid = UUID.randomUUID();
}
public NoSession(NoUser newUser) {
this();
this.state = NoState.MODIFIED;
this.original = null;
this.current = newUser;
this.newUserSession = true;
}
public NoSession(byte[] data, char[] password) throws NoUserNotValidException {
this();
this.newUserSession = false;
this.state = NoState.IDLE;
char[] passwordDupe = password.clone();
try {
this.original = NoUser.createUserFromFile(data, password);
if (NoHashSphere.checkHash(this.original.createHashString())) {
this.current = NoUser.createUserFromFile(data, passwordDupe);
this.uuid = UUID.randomUUID();
NoUtil.wipeBytes(data);
} else {
throw new NoUserNotValidException();
}
} catch (IOException e) {
throw new NoUserNotValidException();
} catch (IllegalBlockSizeException e) {
throw new NoUserNotValidException();
} catch (BadPaddingException e) {
throw new NoUserNotValidException();
} catch (ClassNotFoundException e) {
throw new NoUserNotValidException();
}
}
public void check() throws NoSessionConfirmedException, NoSessionExpiredException {
if (this.state == NoState.CONFIRMED) {
throw new NoSessionConfirmedException();
} else if (this.state == NoState.CLOSED || System.currentTimeMillis() > this.expiry) {
this.state = NoState.CLOSED;
throw new NoSessionExpiredException();
}
}
public NoState touchState() throws NoSessionConfirmedException, NoSessionExpiredException {
this.check();
if (this.newUserSession) {
if (this.state != NoState.AWAITING_CONFIRMATION) {
this.state = NoState.MODIFIED;
}
} else {
String originalHash = this.original.createHashString();
String currentHash = this.current.createHashString();
if (originalHash.equals(currentHash)) {
this.state = NoState.IDLE;
} else if (this.state != NoState.AWAITING_CONFIRMATION) {
this.state = NoState.MODIFIED;
}
}
return this.state;
}
public byte[] initiateSaveAttempt(char[] password) throws NoSessionConfirmedException, NoSessionExpiredException {
this.touchState();
this.state = NoState.AWAITING_CONFIRMATION;
byte[] file = this.current.createFile(password);
NoUtil.wipeChars(password);
return file;
}
public void confirmSave(byte[] confirmData, char[] password) throws NoSessionConfirmedException, NoSessionExpiredException, NoSessionNotAwaitingConfirmationException, NoUserNotValidException {
this.check();
if (this.state != NoState.AWAITING_CONFIRMATION) {
throw new NoSessionNotAwaitingConfirmationException();
}
NoUser confirmed;
try {
confirmed = NoUser.createUserFromFile(confirmData, password);
} catch (IOException e) {
throw new NoUserNotValidException();
} catch (IllegalBlockSizeException e) {
throw new NoUserNotValidException();
} catch (BadPaddingException e) {
throw new NoUserNotValidException();
} catch (ClassNotFoundException e) {
throw new NoUserNotValidException();
}
NoUtil.wipeBytes(confirmData);
NoUtil.wipeChars(password);
if (confirmed.createHashString().equals(this.current.createHashString())) {
this.state = NoState.CONFIRMED;
/* 5.2: confirmed! */
if (!this.newUserSession) {
/* 5.2.1: remove old hash from array */
try {
NoHashSphere.removeHash(this.original.createHashString());
} catch (IOException e) {
throw new NoDashFatalException("Unable to remove hash on confirm.");
}
}
/* 5.2.2: add new hash to array */
try {
NoHashSphere.insertHash(this.current.createHashString());
} catch (IOException e) {
e.printStackTrace();
throw new NoDashFatalException("Unable to remove hash on confirm.");
}
/* 5.2.3: clear influences as they will not need to be re-applied */
ArrayList<NoAction> actions = this.current.getNoActions();
this.incoming = null;
this.original = null;
this.current = null;
/* 5.2.4: execute NoActions */
for (NoAction action : actions) {
/* It is assumed that actions are not long-running tasks
* It is also assumed that actions have the information they need without the user objects */
action.execute();
action.purge();
}
} else {
throw new NoUserNotValidException();
}
}
public NoState getNoState() throws NoSessionConfirmedException, NoSessionExpiredException {
this.touchState();
return this.state;
}
public NoUser getNoUser() throws NoSessionConfirmedException, NoSessionExpiredException {
this.check();
return this.current;
}
public UUID getUUID() {
return this.uuid;
}
public String getUUIDAsString() {
return this.uuid.toString();
}
public byte[] getEncryptedUUID() {
return NoUtil.encrypt(this.uuid.toString().getBytes());
}
public String getEncryptedUUIDAsString() {
return new String(this.getEncryptedUUID());
}
public byte[] getOriginalHash() {
if (this.original != null) {
return this.original.createHash();
} else {
return null;
}
}
public static UUID decryptUUID(byte[] data) throws NoDashSessionBadUUID {
try {
return UUID.fromString(new String(NoUtil.decrypt(data)));
} catch (IllegalBlockSizeException e) {
throw new NoDashSessionBadUUID();
} catch (BadPaddingException e) {
throw new NoDashSessionBadUUID();
}
}
public void consume(NoByteSet byteSet) throws NoByteSetBadDecryptionException {
this.current.consume(byteSet);
}
public void close() {
this.state = NoState.CLOSED;
}
}

View File

@@ -0,0 +1,151 @@
package nodash.models;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.ArrayList;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import sun.security.rsa.RSAPublicKeyImpl;
import nodash.core.NoUtil;
import nodash.exceptions.NoByteSetBadDecryptionException;
public class NoUser implements Serializable {
private static final long serialVersionUID = 7132405837081692211L;
private PublicKey publicKey;
private PrivateKey privateKey;
public int influences;
public int actions;
private ArrayList<NoAction> outgoing = new ArrayList<NoAction>();
public NoUser() {
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;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
}
public final byte[] createFile(char[] password) {
ArrayList<NoAction> temp = this.outgoing;
try {
this.outgoing = new ArrayList<NoAction>();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
byte[] encrypted = NoUtil.encryptByteArray(baos.toByteArray(), password);
oos.close();
baos.close();
return encrypted;
} catch (IOException e) {
e.printStackTrace();
} finally {
this.outgoing = temp;
}
return null;
}
public final byte[] createHash() {
ArrayList<NoAction> temp = this.outgoing;
try {
this.outgoing = new ArrayList<NoAction>();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
byte[] userBytes = baos.toByteArray();
return NoUtil.getHashFromByteArray(userBytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
this.outgoing = temp;
}
return null;
}
public final String createHashString() {
return new String(this.createHash());
}
public final void consume(NoByteSet byteSet) throws NoByteSetBadDecryptionException {
try {
SecretKey secretKey = new SecretKeySpec(decryptRSA(byteSet.key), NoUtil.CIPHER_KEY_SPEC);
byte[] key = secretKey.getEncoded();
secretKey = null;
NoInfluence influence = NoInfluence.decrypt(byteSet.data, key);
NoUtil.wipeBytes(key);
influence.applyTo(this);
this.influences++;
} catch (BadPaddingException e) {
throw new NoByteSetBadDecryptionException(e);
} catch (IllegalBlockSizeException e) {
throw new NoByteSetBadDecryptionException(e);
} catch (ClassNotFoundException e) {
throw new NoByteSetBadDecryptionException(e);
} catch (InvalidKeyException e) {
throw new NoByteSetBadDecryptionException(e);
}
}
public final void addAction(NoAction action) {
this.outgoing.add(action);
this.actions++;
}
public final ArrayList<NoAction> getNoActions() {
return this.outgoing;
}
public final BigInteger getPublicExponent() {
return ((RSAPublicKeyImpl) publicKey).getPublicExponent();
}
public final BigInteger getModulus() {
return ((RSAPublicKeyImpl) publicKey).getModulus();
}
public final PublicKey getRSAPublicKey() {
return (RSAPublicKeyImpl) this.publicKey;
}
private final byte[] decryptRSA(byte[] data) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
return NoUtil.decryptRSA(data, this.privateKey);
}
public static NoUser createUserFromFile(byte[] data, char[] password) throws IllegalBlockSizeException, BadPaddingException, IOException, ClassNotFoundException {
byte[] decrypted = NoUtil.decryptByteArray(data, password);
ByteArrayInputStream bais = new ByteArrayInputStream(decrypted);
ObjectInputStream ois = new ObjectInputStream(bais);
NoUser noUser = (NoUser) ois.readObject();
ois.close();
bais.close();
return noUser;
}
}

View File

@@ -0,0 +1,33 @@
package nodash.models.noactiontypes;
import java.security.PublicKey;
import nodash.core.NoCore;
import nodash.exceptions.NoCannotGetInfluenceException;
import nodash.models.NoByteSet;
import nodash.models.NoInfluence;
public abstract class NoErrorableAction extends NoTargetedAction {
private static final long serialVersionUID = -6077150774349400823L;
public NoErrorableAction(PublicKey target) {
super(target);
}
public void execute() {
NoInfluence influence;
try {
influence = this.generateTargetInfluence();
if (influence != null) {
NoByteSet byteSet = influence.getByteSet(this.target);
NoCore.addByteSet(byteSet, this.target);
}
} catch (NoCannotGetInfluenceException e) {
NoInfluence errorInfluence = e.getResponseInfluence();
if (errorInfluence != null) {
NoByteSet byteSet = errorInfluence.getByteSet(this.target);
NoCore.addByteSet(byteSet, this.target);
}
}
}
}

View File

@@ -0,0 +1,44 @@
package nodash.models.noactiontypes;
import java.security.PublicKey;
import nodash.core.NoCore;
import nodash.exceptions.NoCannotGetInfluenceException;
import nodash.models.NoByteSet;
import nodash.models.NoInfluence;
public abstract class NoHandshakeAction extends NoSourcedAction {
private static final long serialVersionUID = 3195466136587475680L;
protected abstract NoInfluence generateReturnedInfluence();
public NoHandshakeAction(PublicKey target, PublicKey source) {
super(target, source);
}
public void execute() {
try {
NoInfluence influence = this.generateTargetInfluence();
if (influence != null) {
NoByteSet byteSet = influence.getByteSet(this.target);
NoCore.addByteSet(byteSet, this.target);
}
NoInfluence result = this.generateReturnedInfluence();
if (result != null) {
NoByteSet byteSet = result.getByteSet(this.source);
NoCore.addByteSet(byteSet, this.source);
}
} catch (NoCannotGetInfluenceException e) {
NoInfluence errorInfluence = e.getResponseInfluence();
if (errorInfluence != null) {
NoByteSet byteSet = errorInfluence.getByteSet(this.source);
NoCore.addByteSet(byteSet, this.source);
}
}
}
public void purge() {
super.purge();
}
}

View File

@@ -0,0 +1,41 @@
package nodash.models.noactiontypes;
import java.security.PublicKey;
import nodash.core.NoCore;
import nodash.exceptions.NoCannotGetInfluenceException;
import nodash.models.NoByteSet;
import nodash.models.NoInfluence;
public abstract class NoSourcedAction extends NoTargetedAction {
private static final long serialVersionUID = -2996690472537380062L;
protected PublicKey source;
protected abstract NoInfluence generateTargetInfluence() throws NoCannotGetInfluenceException;
public NoSourcedAction(PublicKey target, PublicKey source) {
super(target);
this.source = source;
}
public void execute() {
NoInfluence influence;
try {
influence = this.generateTargetInfluence();
if (influence != null) {
NoByteSet byteSet = influence.getByteSet(this.target);
NoCore.addByteSet(byteSet, this.target);
}
} catch (NoCannotGetInfluenceException e) {
NoInfluence errorInfluence = e.getResponseInfluence();
if (errorInfluence != null) {
NoByteSet byteSet = errorInfluence.getByteSet(this.source);
NoCore.addByteSet(byteSet, this.source);
}
}
}
public void purge() {
super.purge();
this.source = null;
}
}

View File

@@ -0,0 +1,40 @@
package nodash.models.noactiontypes;
import java.security.PublicKey;
import nodash.core.NoCore;
import nodash.exceptions.NoCannotGetInfluenceException;
import nodash.exceptions.NoDashFatalException;
import nodash.models.NoAction;
import nodash.models.NoByteSet;
import nodash.models.NoInfluence;
public abstract class NoTargetedAction extends NoAction {
private static final long serialVersionUID = -8893381130155149646L;
protected PublicKey target;
protected abstract NoInfluence generateTargetInfluence() throws NoCannotGetInfluenceException;
public NoTargetedAction(PublicKey target) {
this.target = target;
}
public void execute() {
NoInfluence influence;
try {
influence = this.generateTargetInfluence();
if (influence != null) {
NoByteSet byteSet = influence.getByteSet(this.target);
NoCore.addByteSet(byteSet, this.target);
}
} catch (NoCannotGetInfluenceException e) {
if (e.getResponseInfluence() != null) {
throw new NoDashFatalException("Unsourced action has generated an error with an undeliverable influence.");
}
}
}
public void purge() {
this.target = null;
}
}