formatting

This commit is contained in:
Dave
2015-06-27 21:50:28 +02:00
parent 7bb6c6fdfa
commit 34b9a748f9
36 changed files with 2381 additions and 2360 deletions

View File

@@ -9,46 +9,46 @@ import javax.crypto.SecretKey;
import nodash.exceptions.NoDashFatalException;
public abstract class NoConfigBase implements NoConfigInterface {
protected boolean ready;
protected SecretKey secretKey;
protected boolean saveDatabase;
protected boolean saveByteSets;
protected final void generateSecretKey() {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(NoUtil.CIPHER_KEY_SPEC);
keyGenerator.init(NoUtil.AES_STRENGTH);
this.secretKey = keyGenerator.generateKey();
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for CIPHER_KEY_SPEC not valid.", e);
}
}
protected boolean ready;
protected SecretKey secretKey;
protected boolean saveDatabase;
protected boolean saveByteSets;
@Override
public void construct() {
this.generateSecretKey();
this.ready = true;
}
protected final void generateSecretKey() {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(NoUtil.CIPHER_KEY_SPEC);
keyGenerator.init(NoUtil.AES_STRENGTH);
this.secretKey = keyGenerator.generateKey();
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for CIPHER_KEY_SPEC not valid.", e);
}
}
@Override
public SecretKey getSecretKey() {
return this.secretKey;
}
@Override
public void construct() {
this.generateSecretKey();
this.ready = true;
}
@Override
public boolean saveDatabase() {
return this.saveDatabase;
}
@Override
public SecretKey getSecretKey() {
return this.secretKey;
}
@Override
public boolean saveByteSets() {
return this.saveByteSets;
}
@Override
public boolean saveDatabase() {
return this.saveDatabase;
}
@Override
public abstract void saveNoConfig();
@Override
public boolean saveByteSets() {
return this.saveByteSets;
}
@Override
public abstract NoConfigInterface loadNoConfig() throws IOException;
@Override
public abstract void saveNoConfig();
@Override
public abstract NoConfigInterface loadNoConfig() throws IOException;
}

View File

@@ -1,20 +1,17 @@
/*
* Copyright 2014 David Horscroft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* The NoConfig is a means to store the server secret key, database address
* and other configs.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* The NoConfig is a means to store the server secret key, database address and other configs.
*/
package nodash.core;
@@ -32,59 +29,60 @@ import java.nio.file.StandardOpenOption;
import nodash.exceptions.NoDashFatalException;
public final class NoConfigDefault extends NoConfigBase implements Serializable {
private static final long serialVersionUID = -8498303909736017075L;
private static final long serialVersionUID = -8498303909736017075L;
private static final String CONFIG_FILENAME = "noconfig.cfg";
private String databaseFileName = "nodatabase.hash";
private static final String CONFIG_FILENAME = "noconfig.cfg";
@Override
public boolean saveDatabase() {
return saveDatabase;
}
public String getDatabaseName() {
return databaseFileName;
}
private String databaseFileName = "nodatabase.hash";
@Override
public boolean saveByteSets() {
return saveByteSets;
}
@Override
public boolean saveDatabase() {
return saveDatabase;
}
@Override
public void saveNoConfig() {
try {
File file = new File(NoConfigDefault.CONFIG_FILENAME);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
byte[] data = baos.toByteArray();
Files.write(file.toPath(), data, StandardOpenOption.CREATE_NEW);
} catch (IOException e) {
throw new NoDashFatalException("Unable to save config, including generated secret key.", e);
}
}
public String getDatabaseName() {
return databaseFileName;
}
@Override
public NoConfigInterface loadNoConfig() throws IOException {
File file = new File(NoConfigDefault.CONFIG_FILENAME);
byte[] data = Files.readAllBytes(file.toPath());
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(bais);
NoConfigInterface noConfig;
try {
noConfig = (NoConfigDefault) ois.readObject();
} catch (ClassNotFoundException e) {
throw new NoDashFatalException("Given bytestream does not compile into a configuration object.", e);
}
this.ready = true;
return noConfig;
}
@Override
public boolean saveByteSets() {
return saveByteSets;
}
@Override
public boolean isReady() {
return this.ready;
}
@Override
public void saveNoConfig() {
try {
File file = new File(NoConfigDefault.CONFIG_FILENAME);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
byte[] data = baos.toByteArray();
Files.write(file.toPath(), data, StandardOpenOption.CREATE_NEW);
} catch (IOException e) {
throw new NoDashFatalException("Unable to save config, including generated secret key.", e);
}
}
@Override
public NoConfigInterface loadNoConfig() throws IOException {
File file = new File(NoConfigDefault.CONFIG_FILENAME);
byte[] data = Files.readAllBytes(file.toPath());
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(bais);
NoConfigInterface noConfig;
try {
noConfig = (NoConfigDefault) ois.readObject();
} catch (ClassNotFoundException e) {
throw new NoDashFatalException(
"Given bytestream does not compile into a configuration object.", e);
}
this.ready = true;
return noConfig;
}
@Override
public boolean isReady() {
return this.ready;
}
}

View File

@@ -5,11 +5,17 @@ import java.io.IOException;
import javax.crypto.SecretKey;
public interface NoConfigInterface {
public void construct();
public SecretKey getSecretKey();
public boolean saveDatabase();
public boolean saveByteSets();
public void saveNoConfig();
public NoConfigInterface loadNoConfig() throws IOException;
public boolean isReady();
public void construct();
public SecretKey getSecretKey();
public boolean saveDatabase();
public boolean saveByteSets();
public void saveNoConfig();
public NoConfigInterface loadNoConfig() throws IOException;
public boolean isReady();
}

View File

@@ -1,20 +1,18 @@
/*
* Copyright 2014 David Horscroft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* The NoCore class is the interface between which the wrapper application
* (wrapplication?) accesses no- functionality.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* The NoCore class is the interface between which the wrapper application (wrapplication?) accesses
* no- functionality.
*/
package nodash.core;
@@ -39,83 +37,94 @@ import nodash.models.NoUser;
import nodash.models.NoSession.NoState;
public final class NoCore {
public static NoConfigInterface config;
public static NoHashSphereInterface hashSphere;
public static boolean isReady() {
return (config != null && config.isReady()) &&
(hashSphere != null && hashSphere.isReady());
}
public static void setup(NoConfigInterface config, NoHashSphereInterface hashSphere) {
NoCore.setup(config);
NoCore.setup(hashSphere);
}
public static void setup(NoConfigInterface config) {
NoCore.config = config;
}
public static void setup(NoHashSphereInterface hashSphere) {
NoCore.hashSphere = hashSphere;
hashSphere.setup();
}
public static void setup() {
NoConfigInterface newConfig = new NoConfigDefault();
try {
newConfig = newConfig.loadNoConfig();
} catch (IOException e) {
newConfig.construct();
}
NoCore.setup(newConfig);
NoCore.setup(new NoHashSphereDefault());
}
public static byte[] login(byte[] data, char[] password) throws NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionExpiredException {
/* steps 1 through to pre-3 */
return NoSessionSphere.login(data, password);
}
public static NoRegister register(NoUser user, char[] password) {
/* Straight to step 4 */
return NoSessionSphere.registerUser(user, password);
}
public static NoUser getUser(byte[] cookie) throws NoSessionExpiredException, NoSessionConfirmedException, NoDashSessionBadUUIDException {
/* Facilitates step 3
* allow website-side modifications to the NoUser or NoUser inheritant */
return NoSessionSphere.getUser(cookie);
}
public static NoState getSessionState(byte[] cookie) throws NoSessionExpiredException, NoSessionConfirmedException, NoDashSessionBadUUIDException {
/* Facilitates step 3
* allow front-side to keep track of session state */
return NoSessionSphere.getState(cookie);
}
public static byte[] requestSave(byte[] cookie, char[] password) throws NoSessionExpiredException, NoSessionConfirmedException, NoSessionNotChangedException, NoSessionAlreadyAwaitingConfirmationException, NoDashSessionBadUUIDException {
/* Step 4. Provides a user with the new binary file */
return NoSessionSphere.save(cookie, password);
}
public static void confirm(byte[] cookie, char[] password, byte[] data) throws NoSessionExpiredException, NoSessionConfirmedException, NoSessionNotAwaitingConfirmationException, NoUserNotValidException, NoDashSessionBadUUIDException {
/* Step 5. Assumes the user has re-uploaded the file along with providing the same password.
* Further attempts of getUser or getSessionState will fail with a NoSessionExpiredException*/
NoSessionSphere.confirm(cookie, password, data);
}
public static NoConfigInterface config;
public static NoHashSphereInterface hashSphere;
public static void addByteSet(NoByteSet byteSet, PublicKey publicKey) {
NoByteSetSphere.add(byteSet, publicKey);
}
public static void shred(byte[] cookie) {
/* 3.2 Hot pull */
NoSessionSphere.shred(cookie);
}
public static void triggerPrune() {
NoSessionSphere.prune();
}
}
public static boolean isReady() {
return (config != null && config.isReady()) && (hashSphere != null && hashSphere.isReady());
}
public static void setup(NoConfigInterface config, NoHashSphereInterface hashSphere) {
NoCore.setup(config);
NoCore.setup(hashSphere);
}
public static void setup(NoConfigInterface config) {
NoCore.config = config;
}
public static void setup(NoHashSphereInterface hashSphere) {
NoCore.hashSphere = hashSphere;
hashSphere.setup();
}
public static void setup() {
NoConfigInterface newConfig = new NoConfigDefault();
try {
newConfig = newConfig.loadNoConfig();
} catch (IOException e) {
newConfig.construct();
}
NoCore.setup(newConfig);
NoCore.setup(new NoHashSphereDefault());
}
public static byte[] login(byte[] data, char[] password) throws NoUserNotValidException,
NoUserAlreadyOnlineException, NoSessionExpiredException {
/* steps 1 through to pre-3 */
return NoSessionSphere.login(data, password);
}
public static NoRegister register(NoUser user, char[] password) {
/* Straight to step 4 */
return NoSessionSphere.registerUser(user, password);
}
public static NoUser getUser(byte[] cookie) throws NoSessionExpiredException,
NoSessionConfirmedException, NoDashSessionBadUUIDException {
/*
* Facilitates step 3 allow website-side modifications to the NoUser or NoUser inheritant
*/
return NoSessionSphere.getUser(cookie);
}
public static NoState getSessionState(byte[] cookie) throws NoSessionExpiredException,
NoSessionConfirmedException, NoDashSessionBadUUIDException {
/*
* Facilitates step 3 allow front-side to keep track of session state
*/
return NoSessionSphere.getState(cookie);
}
public static byte[] requestSave(byte[] cookie, char[] password)
throws NoSessionExpiredException, NoSessionConfirmedException, NoSessionNotChangedException,
NoSessionAlreadyAwaitingConfirmationException, NoDashSessionBadUUIDException {
/* Step 4. Provides a user with the new binary file */
return NoSessionSphere.save(cookie, password);
}
public static void confirm(byte[] cookie, char[] password, byte[] data)
throws NoSessionExpiredException, NoSessionConfirmedException,
NoSessionNotAwaitingConfirmationException, NoUserNotValidException,
NoDashSessionBadUUIDException {
/*
* Step 5. Assumes the user has re-uploaded the file along with providing the same password.
* Further attempts of getUser or getSessionState will fail with a NoSessionExpiredException
*/
NoSessionSphere.confirm(cookie, password, data);
}
public static void addByteSet(NoByteSet byteSet, PublicKey publicKey) {
NoByteSetSphere.add(byteSet, publicKey);
}
public static void shred(byte[] cookie) {
/* 3.2 Hot pull */
NoSessionSphere.shred(cookie);
}
public static void triggerPrune() {
NoSessionSphere.prune();
}
}

View File

@@ -1,25 +1,23 @@
/*
* Copyright 2014 David Horscroft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* The NoRegister class is a simple model used to return both cookies and
* download data upon user registration.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* The NoRegister class is a simple model used to return both cookies and download data upon user
* registration.
*/
package nodash.core;
public final class NoRegister {
public byte[] cookie;
public byte[] data;
}
public byte[] cookie;
public byte[] data;
}

View File

@@ -1,20 +1,18 @@
/*
* Copyright 2014 David Horscroft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* The NoUtil class encapsulates no- standard functions such as encryption/decryption
* and hashing algorithms.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* The NoUtil class encapsulates no- standard functions such as encryption/decryption and hashing
* algorithms.
*/
package nodash.core;
@@ -38,172 +36,177 @@ import javax.crypto.spec.SecretKeySpec;
import nodash.exceptions.NoDashFatalException;
public final class NoUtil {
public static final String CIPHER_TYPE = "AES/ECB/PKCS5PADDING";
public static final String CIPHER_KEY_SPEC = "AES";
public static final String DIGEST_TYPE = "SHA-512";
public static final String PBE_TYPE = "PBKDF2WithHmacSHA1";
public static final String CIPHER_RSA_TYPE = "RSA/ECB/PKCS1PADDING";
public static final String KEYPAIR_ALGORITHM = "RSA";
public static final String SECURERANDOM_ALGORITHM = "SHA1PRNG";
public static final String SECURERANDOM_PROVIDER = "SUN";
public static final int RSA_STRENGTH = 4096;
public static final int AES_STRENGTH = 256;
public static final byte BLANK_BYTE = 'A';
public static char[] bytesToChars(byte[] array) {
char[] result = new char[array.length];
for (int x=0; x<array.length; x++) {
result[x] = (char) array[x];
}
return result;
}
public static byte[] charsToBytes(char[] array) {
byte[] result = new byte[array.length];
for (int x=0; x<array.length; x++) {
result[x] = (byte) array[x];
}
return result;
}
public final class NoUtil {
public static final String CIPHER_TYPE = "AES/ECB/PKCS5PADDING";
public static final String CIPHER_KEY_SPEC = "AES";
public static final String DIGEST_TYPE = "SHA-512";
public static final String PBE_TYPE = "PBKDF2WithHmacSHA1";
public static final String CIPHER_RSA_TYPE = "RSA/ECB/PKCS1PADDING";
public static final String KEYPAIR_ALGORITHM = "RSA";
public static final String SECURERANDOM_ALGORITHM = "SHA1PRNG";
public static final String SECURERANDOM_PROVIDER = "SUN";
public static final int RSA_STRENGTH = 4096;
public static final int AES_STRENGTH = 256;
public static final byte BLANK_BYTE = 'A';
public static void wipeBytes(byte[] array) {
for (int x=0; x<array.length; x++) {
array[x] = NoUtil.BLANK_BYTE;
}
}
public static char[] bytesToChars(byte[] array) {
char[] result = new char[array.length];
for (int x = 0; x < array.length; x++) {
result[x] = (char) array[x];
}
return result;
}
public static void wipeChars(char[] array) {
for (int x=0; x<array.length; x++) {
array[x] = NoUtil.BLANK_BYTE;
}
}
public static byte[] charsToBytes(char[] array) {
byte[] result = new byte[array.length];
for (int x = 0; x < array.length; x++) {
result[x] = (byte) array[x];
}
return result;
}
private static byte[] getPBEKeyFromPassword(char[] password) {
SecretKeyFactory skf;
try {
skf = SecretKeyFactory.getInstance(NoUtil.PBE_TYPE);
} catch (NoSuchAlgorithmException e) {
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.", e);
}
NoUtil.wipeChars(password);
return key.getEncoded();
}
public static byte[] getHashFromByteArray(byte[] bytes) {
try {
MessageDigest messageDigest = MessageDigest.getInstance(NoUtil.DIGEST_TYPE);
return messageDigest.digest(bytes);
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for DIGEST_TYPE not valid.", e);
}
}
public static byte[] decrypt(byte[] data, char[] password) throws IllegalBlockSizeException, BadPaddingException {
byte[] passwordByte = NoUtil.getPBEKeyFromPassword(password);
byte[] response = NoUtil.decrypt(NoUtil.decrypt(data), passwordByte);
NoUtil.wipeBytes(passwordByte);
return response;
}
public static byte[] encrypt(byte[] data, char[] password) {
byte[] passwordByte = NoUtil.getPBEKeyFromPassword(password);
byte[] response = NoUtil.encrypt(NoUtil.encrypt(data, passwordByte));
NoUtil.wipeBytes(passwordByte);
return response;
}
public static byte[] encrypt(byte[] data, byte[] key) {
Cipher cipher;
try {
cipher = Cipher.getInstance(NoUtil.CIPHER_TYPE);
} catch (NoSuchAlgorithmException e) {
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).", e);
}
SecretKeySpec secretKey = new SecretKeySpec(key, NoUtil.CIPHER_KEY_SPEC);
try {
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
} catch (InvalidKeyException e) {
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.", e);
} catch (BadPaddingException e) {
throw new NoDashFatalException("Bad padding exception encountered during encryption.", e);
}
}
public static byte[] encrypt(byte[] data) {
return NoUtil.encrypt(data, NoCore.config.getSecretKey().getEncoded());
}
public static byte[] decrypt(byte[] data, byte[] key) throws IllegalBlockSizeException, BadPaddingException {
Cipher cipher;
try {
cipher = Cipher.getInstance(NoUtil.CIPHER_TYPE);
} catch (NoSuchAlgorithmException e) {
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).", e);
}
SecretKeySpec secretKey = new SecretKeySpec(key, NoUtil.CIPHER_KEY_SPEC);
try {
cipher.init(Cipher.DECRYPT_MODE, secretKey);
} catch (InvalidKeyException e) {
throw new NoDashFatalException("Secret key is invalid.", e);
}
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data) throws IllegalBlockSizeException, BadPaddingException {
return NoUtil.decrypt(data, NoCore.config.getSecretKey().getEncoded());
}
public static byte[] encryptRSA(byte[] data, PublicKey publicKey) {
Cipher cipher;
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).", e);
} catch (NoSuchPaddingException e) {
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.", e);
} catch (IllegalBlockSizeException e) {
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.", e);
}
}
public static byte[] decryptRSA(byte[] data, PrivateKey privateKey) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher;
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).", e);
} catch (NoSuchPaddingException e) {
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);
}
public static void wipeBytes(byte[] array) {
for (int x = 0; x < array.length; x++) {
array[x] = NoUtil.BLANK_BYTE;
}
}
}
public static void wipeChars(char[] array) {
for (int x = 0; x < array.length; x++) {
array[x] = NoUtil.BLANK_BYTE;
}
}
private static byte[] getPBEKeyFromPassword(char[] password) {
SecretKeyFactory skf;
try {
skf = SecretKeyFactory.getInstance(NoUtil.PBE_TYPE);
} catch (NoSuchAlgorithmException e) {
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.", e);
}
NoUtil.wipeChars(password);
return key.getEncoded();
}
public static byte[] getHashFromByteArray(byte[] bytes) {
try {
MessageDigest messageDigest = MessageDigest.getInstance(NoUtil.DIGEST_TYPE);
return messageDigest.digest(bytes);
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for DIGEST_TYPE not valid.", e);
}
}
public static byte[] decrypt(byte[] data, char[] password) throws IllegalBlockSizeException,
BadPaddingException {
byte[] passwordByte = NoUtil.getPBEKeyFromPassword(password);
byte[] response = NoUtil.decrypt(NoUtil.decrypt(data), passwordByte);
NoUtil.wipeBytes(passwordByte);
return response;
}
public static byte[] encrypt(byte[] data, char[] password) {
byte[] passwordByte = NoUtil.getPBEKeyFromPassword(password);
byte[] response = NoUtil.encrypt(NoUtil.encrypt(data, passwordByte));
NoUtil.wipeBytes(passwordByte);
return response;
}
public static byte[] encrypt(byte[] data, byte[] key) {
Cipher cipher;
try {
cipher = Cipher.getInstance(NoUtil.CIPHER_TYPE);
} catch (NoSuchAlgorithmException e) {
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).", e);
}
SecretKeySpec secretKey = new SecretKeySpec(key, NoUtil.CIPHER_KEY_SPEC);
try {
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
} catch (InvalidKeyException e) {
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.", e);
} catch (BadPaddingException e) {
throw new NoDashFatalException("Bad padding exception encountered during encryption.", e);
}
}
public static byte[] encrypt(byte[] data) {
return NoUtil.encrypt(data, NoCore.config.getSecretKey().getEncoded());
}
public static byte[] decrypt(byte[] data, byte[] key) throws IllegalBlockSizeException,
BadPaddingException {
Cipher cipher;
try {
cipher = Cipher.getInstance(NoUtil.CIPHER_TYPE);
} catch (NoSuchAlgorithmException e) {
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).", e);
}
SecretKeySpec secretKey = new SecretKeySpec(key, NoUtil.CIPHER_KEY_SPEC);
try {
cipher.init(Cipher.DECRYPT_MODE, secretKey);
} catch (InvalidKeyException e) {
throw new NoDashFatalException("Secret key is invalid.", e);
}
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data) throws IllegalBlockSizeException, BadPaddingException {
return NoUtil.decrypt(data, NoCore.config.getSecretKey().getEncoded());
}
public static byte[] encryptRSA(byte[] data, PublicKey publicKey) {
Cipher cipher;
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).",
e);
} catch (NoSuchPaddingException e) {
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.", e);
} catch (IllegalBlockSizeException e) {
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.", e);
}
}
public static byte[] decryptRSA(byte[] data, PrivateKey privateKey) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
Cipher cipher;
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).",
e);
} catch (NoSuchPaddingException e) {
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

@@ -1,20 +1,18 @@
/*
* Copyright 2014 David Horscroft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* The NoByteSetSphere stores user-to-user influences and their encryption
* keys in an accessible manner.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* The NoByteSetSphere stores user-to-user influences and their encryption keys in an accessible
* manner.
*/
package nodash.core.spheres;
@@ -27,31 +25,32 @@ import nodash.models.NoByteSet;
import nodash.models.NoUser;
public final class NoByteSetSphere {
private static final ArrayList<NoByteSet> EMPTY_BYTESET_LIST = new ArrayList<NoByteSet>(0);
private static ConcurrentHashMap<PublicKey, ArrayList<NoByteSet>> byteSets = new ConcurrentHashMap<PublicKey, ArrayList<NoByteSet>>();
public static void add(NoByteSet byteSet, PublicKey publicKey) {
if (!NoByteSetSphere.byteSets.containsKey(publicKey)) {
NoByteSetSphere.byteSets.put(publicKey, new ArrayList<NoByteSet>());
}
NoByteSetSphere.byteSets.get(publicKey).add(byteSet);
}
public static void addList(ArrayList<NoByteSet> byteSetList, PublicKey publicKey) {
if (!NoByteSetSphere.byteSets.containsKey(publicKey)) {
NoByteSetSphere.byteSets.put(publicKey, new ArrayList<NoByteSet>());
}
NoByteSetSphere.byteSets.get(publicKey).addAll(byteSetList);
}
public static ArrayList<NoByteSet> consume(NoUser user) {
if (NoByteSetSphere.byteSets.containsKey(user.getRSAPublicKey())) {
ArrayList<NoByteSet> result = NoByteSetSphere.byteSets.get(user.getRSAPublicKey());
NoByteSetSphere.byteSets.remove(user.getRSAPublicKey());
return result;
} else {
return NoByteSetSphere.EMPTY_BYTESET_LIST;
}
}
private static final ArrayList<NoByteSet> EMPTY_BYTESET_LIST = new ArrayList<NoByteSet>(0);
private static ConcurrentHashMap<PublicKey, ArrayList<NoByteSet>> byteSets =
new ConcurrentHashMap<PublicKey, ArrayList<NoByteSet>>();
public static void add(NoByteSet byteSet, PublicKey publicKey) {
if (!NoByteSetSphere.byteSets.containsKey(publicKey)) {
NoByteSetSphere.byteSets.put(publicKey, new ArrayList<NoByteSet>());
}
NoByteSetSphere.byteSets.get(publicKey).add(byteSet);
}
public static void addList(ArrayList<NoByteSet> byteSetList, PublicKey publicKey) {
if (!NoByteSetSphere.byteSets.containsKey(publicKey)) {
NoByteSetSphere.byteSets.put(publicKey, new ArrayList<NoByteSet>());
}
NoByteSetSphere.byteSets.get(publicKey).addAll(byteSetList);
}
public static ArrayList<NoByteSet> consume(NoUser user) {
if (NoByteSetSphere.byteSets.containsKey(user.getRSAPublicKey())) {
ArrayList<NoByteSet> result = NoByteSetSphere.byteSets.get(user.getRSAPublicKey());
NoByteSetSphere.byteSets.remove(user.getRSAPublicKey());
return result;
} else {
return NoByteSetSphere.EMPTY_BYTESET_LIST;
}
}
}

View File

@@ -1,17 +1,15 @@
/*
* Copyright 2014 David Horscroft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* The NoHashSpehre stores database hashes for user verification.
*
@@ -38,77 +36,78 @@ import nodash.exceptions.NoDashFatalException;
import nodash.models.NoUser;
public final class NoHashSphereDefault implements NoHashSphereInterface {
private Set<String> database = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
private String fileName;
private boolean ready = false;
public NoHashSphereDefault(String fileName) {
this.fileName = fileName;
}
public NoHashSphereDefault() {
this.fileName = ( (NoConfigDefault) NoCore.config).getDatabaseName();
}
@SuppressWarnings("unchecked")
public void setup() {
if (NoCore.config.saveDatabase()) {
File file = new File(this.fileName);
if (file.exists()) {
try {
byte[] data = Files.readAllBytes(file.toPath());
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(bais);
this.database = (Set<String>) ois.readObject();
ois.close();
bais.close();
} catch (IOException e){
throw new NoDashFatalException("Unable to load up given database file.", e);
} catch (ClassNotFoundException e) {
throw new NoDashFatalException("Database file not in a verifiable format.", e);
}
}
}
this.ready = true;
}
public synchronized void saveToFile() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this.database);
byte[] data = baos.toByteArray();
oos.close();
baos.close();
File file = new File( ( (NoConfigDefault) NoCore.config).getDatabaseName());
Files.write(file.toPath(), data, StandardOpenOption.CREATE);
}
public synchronized void addNewNoUser(NoUser user) throws IOException {
String hash = user.createHashString();
this.database.add(hash);
this.saveToFile();
}
public synchronized void insertHash(String hash) throws IOException {
this.database.add(hash);
this.saveToFile();
}
public synchronized void removeHash(String hash) throws IOException {
this.database.remove(hash);
this.saveToFile();
}
public synchronized boolean checkHash(String hash) {
return this.database.contains(hash);
}
private Set<String> database = Collections
.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
private String fileName;
private boolean ready = false;
public synchronized long size() {
return this.database.size();
}
public NoHashSphereDefault(String fileName) {
this.fileName = fileName;
}
@Override
public boolean isReady() {
return this.ready;
}
public NoHashSphereDefault() {
this.fileName = ((NoConfigDefault) NoCore.config).getDatabaseName();
}
@SuppressWarnings("unchecked")
public void setup() {
if (NoCore.config.saveDatabase()) {
File file = new File(this.fileName);
if (file.exists()) {
try {
byte[] data = Files.readAllBytes(file.toPath());
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(bais);
this.database = (Set<String>) ois.readObject();
ois.close();
bais.close();
} catch (IOException e) {
throw new NoDashFatalException("Unable to load up given database file.", e);
} catch (ClassNotFoundException e) {
throw new NoDashFatalException("Database file not in a verifiable format.", e);
}
}
}
this.ready = true;
}
public synchronized void saveToFile() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this.database);
byte[] data = baos.toByteArray();
oos.close();
baos.close();
File file = new File(((NoConfigDefault) NoCore.config).getDatabaseName());
Files.write(file.toPath(), data, StandardOpenOption.CREATE);
}
public synchronized void addNewNoUser(NoUser user) throws IOException {
String hash = user.createHashString();
this.database.add(hash);
this.saveToFile();
}
public synchronized void insertHash(String hash) throws IOException {
this.database.add(hash);
this.saveToFile();
}
public synchronized void removeHash(String hash) throws IOException {
this.database.remove(hash);
this.saveToFile();
}
public synchronized boolean checkHash(String hash) {
return this.database.contains(hash);
}
public synchronized long size() {
return this.database.size();
}
@Override
public boolean isReady() {
return this.ready;
}
}

View File

@@ -5,12 +5,19 @@ import java.io.IOException;
import nodash.models.NoUser;
public interface NoHashSphereInterface {
public void setup();
public void saveToFile() throws IOException;
public void addNewNoUser(NoUser user) throws IOException;
public void insertHash(String hash) throws IOException;
public void removeHash(String hash) throws IOException;
public boolean checkHash(String hash);
public long size();
public boolean isReady();
public void setup();
public void saveToFile() throws IOException;
public void addNewNoUser(NoUser user) throws IOException;
public void insertHash(String hash) throws IOException;
public void removeHash(String hash) throws IOException;
public boolean checkHash(String hash);
public long size();
public boolean isReady();
}

View File

@@ -1,20 +1,18 @@
/*
* Copyright 2014 David Horscroft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* The NoSessionSphere stores user sessions and allows their access and
* manipulation with the use of their UUID.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* The NoSessionSphere stores user sessions and allows their access and manipulation with the use of
* their UUID.
*/
package nodash.core.spheres;
@@ -43,148 +41,161 @@ import nodash.models.NoUser;
import nodash.models.NoSession.NoState;
public final class NoSessionSphere {
private static ConcurrentHashMap<UUID, NoSession> sessions = new ConcurrentHashMap<UUID, NoSession>();
private static Set<String> originalHashesOnline = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
public static synchronized void prune() {
for (UUID uuid : NoSessionSphere.sessions.keySet()) {
pruneSingle(uuid);
}
}
private static ConcurrentHashMap<UUID, NoSession> sessions =
new ConcurrentHashMap<UUID, NoSession>();
private static Set<String> originalHashesOnline = Collections
.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
public static void shred(byte[] encryptedUUID) {
try {
UUID uuid = NoSession.decryptUUID(encryptedUUID);
if (NoSessionSphere.sessions.containsKey(uuid)) {
NoSession session = NoSessionSphere.sessions.get(uuid);
NoByteSetSphere.addList(session.incoming, session.current.getRSAPublicKey());
NoSessionSphere.originalHashesOnline.remove(Base64.encodeBase64String(session.getOriginalHash()));
NoSessionSphere.sessions.remove(uuid);
session = null;
}
} catch (NoDashSessionBadUUIDException e) {
// Suppress, doesn't matter
}
}
public static synchronized void pruneSingle(UUID uuid) {
NoSession session = NoSessionSphere.sessions.get(uuid);
try {
session.check();
} catch (NoSessionExpiredException e) {
/* Resultant from 3.1 and 3.2 */
NoByteSetSphere.addList(session.incoming, session.current.getRSAPublicKey());
NoSessionSphere.originalHashesOnline.remove(session.getOriginalHash());
NoSessionSphere.sessions.remove(uuid);
session = null;
} catch (NoSessionConfirmedException e) {
/* Should be cleaned up at 5.2 */
}
}
public static synchronized byte[] login(byte[] data, char[] password) throws NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionExpiredException {
/* 1. Login with byte[] data and byte[] password */
NoSession session = new NoSession(data, password);
/* 1.1. User currently has an online session, must wait for it to expire. */
if (originalHashesOnline.contains(Base64.encodeBase64String(session.getOriginalHash()))) {
throw new NoUserAlreadyOnlineException();
}
/* 1.2. User successfully logged in: set up session records. */
NoSessionSphere.originalHashesOnline.add(Base64.encodeBase64String(session.getOriginalHash()));
NoSessionSphere.sessions.put(session.uuid, session);
/* 2. Check NoByteSetSphere for incoming Influences */
session.incoming = NoByteSetSphere.consume(session.current);
for (NoByteSet nbs : session.incoming) {
/* 2.1 Decrypt NoInfluence from NoByteSet, let the current user consume them */
try {
session.consume(nbs);
} catch (NoByteSetBadDecryptionException e) {
throw new NoDashFatalException("Bad byte sets on consumption.", e);
}
} /* 2.2 Alternatively, no NoByteSets to consume */
try {
session.check();
} catch (NoSessionConfirmedException e) {
/* Should be impossible to reach */
throw new NoDashFatalException(e);
}
/* Will set to 2.1[MODIFIED] or 2.2[IDLE] */
/* Precursor to 3.; allow website to associate user session with a cookie. */
return session.getEncryptedUUID();
}
public static NoUser getUser(byte[] encryptedUUID) throws NoDashSessionBadUUIDException, NoSessionExpiredException, NoSessionConfirmedException {
UUID uuid = NoSession.decryptUUID(encryptedUUID);
if (NoSessionSphere.sessions.containsKey(uuid)) {
NoSessionSphere.pruneSingle(uuid);
try {
return NoSessionSphere.sessions.get(uuid).getNoUser();
} catch (NullPointerException e) {
throw new NoSessionExpiredException();
}
}
throw new NoSessionExpiredException();
}
public static NoState getState(byte[] encryptedUUID) throws NoDashSessionBadUUIDException, NoSessionExpiredException, NoSessionConfirmedException {
UUID uuid = NoSession.decryptUUID(encryptedUUID);
if (NoSessionSphere.sessions.containsKey(uuid)) {
NoSessionSphere.pruneSingle(uuid);
NoSession session = NoSessionSphere.sessions.get(uuid);
return session.getNoState();
}
throw new NoSessionExpiredException();
}
public static synchronized byte[] save(byte[] encryptedUUID, char[] password) throws NoDashSessionBadUUIDException, NoSessionExpiredException, NoSessionConfirmedException, NoSessionNotChangedException, NoSessionAlreadyAwaitingConfirmationException {
UUID uuid = NoSession.decryptUUID(encryptedUUID);
if (NoSessionSphere.sessions.containsKey(uuid)) {
NoSessionSphere.pruneSingle(uuid);
NoSession session = NoSessionSphere.sessions.get(uuid);
if (session.getNoState().equals(NoState.IDLE)) {
throw new NoSessionNotChangedException();
} else if (session.getNoState().equals(NoState.AWAITING_CONFIRMATION)) {
throw new NoSessionAlreadyAwaitingConfirmationException();
}
return session.initiateSaveAttempt(password);
}
throw new NoSessionExpiredException();
}
public static synchronized void confirm(byte[] encryptedUUID, char[] password, byte[] data) throws NoDashSessionBadUUIDException, NoSessionExpiredException, NoSessionConfirmedException, NoSessionNotAwaitingConfirmationException, NoUserNotValidException {
UUID uuid = NoSession.decryptUUID(encryptedUUID);
if (NoSessionSphere.sessions.containsKey(uuid)) {
NoSessionSphere.pruneSingle(uuid);
NoSession session = NoSessionSphere.sessions.get(uuid);
session.confirmSave(data, password);
return;
}
throw new NoSessionExpiredException();
}
public static synchronized void prune() {
for (UUID uuid : NoSessionSphere.sessions.keySet()) {
pruneSingle(uuid);
}
}
public static synchronized NoRegister registerUser(NoUser user, char[] password) {
NoRegister result = new NoRegister();
NoSession session = new NoSession(user);
NoSessionSphere.sessions.put(session.uuid, session);
result.cookie = session.getEncryptedUUID();
try {
result.data = NoSessionSphere.save(result.cookie, password);
} catch (NoDashSessionBadUUIDException e) {
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.", e);
} catch (NoSessionConfirmedException e) {
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.", e);
} catch (NoSessionAlreadyAwaitingConfirmationException e) {
throw new NoDashFatalException("Session claims to be awaiting confirmation before returning data to the user.", e);
}
return result;
}
}
public static void shred(byte[] encryptedUUID) {
try {
UUID uuid = NoSession.decryptUUID(encryptedUUID);
if (NoSessionSphere.sessions.containsKey(uuid)) {
NoSession session = NoSessionSphere.sessions.get(uuid);
NoByteSetSphere.addList(session.incoming, session.current.getRSAPublicKey());
NoSessionSphere.originalHashesOnline.remove(Base64.encodeBase64String(session
.getOriginalHash()));
NoSessionSphere.sessions.remove(uuid);
session = null;
}
} catch (NoDashSessionBadUUIDException e) {
// Suppress, doesn't matter
}
}
public static synchronized void pruneSingle(UUID uuid) {
NoSession session = NoSessionSphere.sessions.get(uuid);
try {
session.check();
} catch (NoSessionExpiredException e) {
/* Resultant from 3.1 and 3.2 */
NoByteSetSphere.addList(session.incoming, session.current.getRSAPublicKey());
NoSessionSphere.originalHashesOnline.remove(session.getOriginalHash());
NoSessionSphere.sessions.remove(uuid);
session = null;
} catch (NoSessionConfirmedException e) {
/* Should be cleaned up at 5.2 */
}
}
public static synchronized byte[] login(byte[] data, char[] password)
throws NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionExpiredException {
/* 1. Login with byte[] data and byte[] password */
NoSession session = new NoSession(data, password);
/* 1.1. User currently has an online session, must wait for it to expire. */
if (originalHashesOnline.contains(Base64.encodeBase64String(session.getOriginalHash()))) {
throw new NoUserAlreadyOnlineException();
}
/* 1.2. User successfully logged in: set up session records. */
NoSessionSphere.originalHashesOnline.add(Base64.encodeBase64String(session.getOriginalHash()));
NoSessionSphere.sessions.put(session.uuid, session);
/* 2. Check NoByteSetSphere for incoming Influences */
session.incoming = NoByteSetSphere.consume(session.current);
for (NoByteSet nbs : session.incoming) {
/* 2.1 Decrypt NoInfluence from NoByteSet, let the current user consume them */
try {
session.consume(nbs);
} catch (NoByteSetBadDecryptionException e) {
throw new NoDashFatalException("Bad byte sets on consumption.", e);
}
} /* 2.2 Alternatively, no NoByteSets to consume */
try {
session.check();
} catch (NoSessionConfirmedException e) {
/* Should be impossible to reach */
throw new NoDashFatalException(e);
}
/* Will set to 2.1[MODIFIED] or 2.2[IDLE] */
/* Precursor to 3.; allow website to associate user session with a cookie. */
return session.getEncryptedUUID();
}
public static NoUser getUser(byte[] encryptedUUID) throws NoDashSessionBadUUIDException,
NoSessionExpiredException, NoSessionConfirmedException {
UUID uuid = NoSession.decryptUUID(encryptedUUID);
if (NoSessionSphere.sessions.containsKey(uuid)) {
NoSessionSphere.pruneSingle(uuid);
try {
return NoSessionSphere.sessions.get(uuid).getNoUser();
} catch (NullPointerException e) {
throw new NoSessionExpiredException();
}
}
throw new NoSessionExpiredException();
}
public static NoState getState(byte[] encryptedUUID) throws NoDashSessionBadUUIDException,
NoSessionExpiredException, NoSessionConfirmedException {
UUID uuid = NoSession.decryptUUID(encryptedUUID);
if (NoSessionSphere.sessions.containsKey(uuid)) {
NoSessionSphere.pruneSingle(uuid);
NoSession session = NoSessionSphere.sessions.get(uuid);
return session.getNoState();
}
throw new NoSessionExpiredException();
}
public static synchronized byte[] save(byte[] encryptedUUID, char[] password)
throws NoDashSessionBadUUIDException, NoSessionExpiredException, NoSessionConfirmedException,
NoSessionNotChangedException, NoSessionAlreadyAwaitingConfirmationException {
UUID uuid = NoSession.decryptUUID(encryptedUUID);
if (NoSessionSphere.sessions.containsKey(uuid)) {
NoSessionSphere.pruneSingle(uuid);
NoSession session = NoSessionSphere.sessions.get(uuid);
if (session.getNoState().equals(NoState.IDLE)) {
throw new NoSessionNotChangedException();
} else if (session.getNoState().equals(NoState.AWAITING_CONFIRMATION)) {
throw new NoSessionAlreadyAwaitingConfirmationException();
}
return session.initiateSaveAttempt(password);
}
throw new NoSessionExpiredException();
}
public static synchronized void confirm(byte[] encryptedUUID, char[] password, byte[] data)
throws NoDashSessionBadUUIDException, NoSessionExpiredException, NoSessionConfirmedException,
NoSessionNotAwaitingConfirmationException, NoUserNotValidException {
UUID uuid = NoSession.decryptUUID(encryptedUUID);
if (NoSessionSphere.sessions.containsKey(uuid)) {
NoSessionSphere.pruneSingle(uuid);
NoSession session = NoSessionSphere.sessions.get(uuid);
session.confirmSave(data, password);
return;
}
throw new NoSessionExpiredException();
}
public static synchronized NoRegister registerUser(NoUser user, char[] password) {
NoRegister result = new NoRegister();
NoSession session = new NoSession(user);
NoSessionSphere.sessions.put(session.uuid, session);
result.cookie = session.getEncryptedUUID();
try {
result.data = NoSessionSphere.save(result.cookie, password);
} catch (NoDashSessionBadUUIDException e) {
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.", e);
} catch (NoSessionConfirmedException e) {
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.", e);
} catch (NoSessionAlreadyAwaitingConfirmationException e) {
throw new NoDashFatalException(
"Session claims to be awaiting confirmation before returning data to the user.", e);
}
return result;
}
}