This commit is contained in:
Dave
2015-11-28 22:53:54 +02:00
parent 256ba00cf8
commit 7c0271f527
3 changed files with 29 additions and 37 deletions

View File

@@ -7,13 +7,8 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import com.google.gson.Gson;
import nodash.core.NoAdapter; import nodash.core.NoAdapter;
import nodash.core.NoUtil; import nodash.core.NoUtil;
import nodash.exceptions.NoByteSetBadDecryptionException; import nodash.exceptions.NoByteSetBadDecryptionException;
@@ -58,7 +53,6 @@ public final class NoSession implements Serializable {
public NoSession(byte[] data, char[] password) throws NoUserNotValidException { public NoSession(byte[] data, char[] password) throws NoUserNotValidException {
this(); this();
this.state = NoState.IDLE; this.state = NoState.IDLE;
try {
byte[] originalData = Arrays.copyOf(data, data.length); byte[] originalData = Arrays.copyOf(data, data.length);
char[] originalPassword = Arrays.copyOf(password, password.length); char[] originalPassword = Arrays.copyOf(password, password.length);
this.original = this.original =
@@ -67,11 +61,6 @@ public final class NoSession implements Serializable {
NoUtil.wipeBytes(data); NoUtil.wipeBytes(data);
NoUtil.wipeChars(password); NoUtil.wipeChars(password);
this.uuid = UUID.randomUUID().toString(); this.uuid = UUID.randomUUID().toString();
} catch (IllegalBlockSizeException e) {
throw new NoUserNotValidException();
} catch (BadPaddingException e) {
throw new NoUserNotValidException();
}
} }
public void check() throws NoSessionConfirmedException, NoSessionExpiredException { public void check() throws NoSessionConfirmedException, NoSessionExpiredException {
@@ -118,14 +107,7 @@ public final class NoSession implements Serializable {
throw new NoSessionNotAwaitingConfirmationException(); throw new NoSessionNotAwaitingConfirmationException();
} }
NoUser confirmed; NoUser confirmed = NoUser.createUserFromFile(confirmData, password, NoUtil.NO_USER_CLASS);
try {
confirmed = NoUser.createUserFromFile(confirmData, password, NoUtil.NO_USER_CLASS);
} catch (IllegalBlockSizeException e) {
throw new NoUserNotValidException();
} catch (BadPaddingException e) {
throw new NoUserNotValidException();
}
NoUtil.wipeBytes(confirmData); NoUtil.wipeBytes(confirmData);
NoUtil.wipeChars(password); NoUtil.wipeChars(password);

View File

@@ -23,15 +23,12 @@ import java.io.IOException;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.KeyPair; import java.security.KeyPair;
import java.security.KeyPairGenerator; import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException; import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey; import java.security.PublicKey;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.ArrayList; import java.util.ArrayList;
@@ -47,12 +44,14 @@ import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import sun.security.rsa.RSAPrivateCrtKeyImpl; import sun.security.rsa.RSAPrivateCrtKeyImpl;
import sun.security.rsa.RSAPublicKeyImpl; import sun.security.rsa.RSAPublicKeyImpl;
import nodash.core.NoUtil; import nodash.core.NoUtil;
import nodash.exceptions.NoByteSetBadDecryptionException; import nodash.exceptions.NoByteSetBadDecryptionException;
import nodash.exceptions.NoDashFatalException; import nodash.exceptions.NoDashFatalException;
import nodash.exceptions.NoUserNotValidException;
public class NoUser implements Serializable { public class NoUser implements Serializable {
private static final long serialVersionUID = 7132405837081692211L; private static final long serialVersionUID = 7132405837081692211L;
@@ -123,6 +122,7 @@ public class NoUser implements Serializable {
return encrypted; return encrypted;
} }
@SuppressWarnings("unchecked")
public final byte[] createHash() { public final byte[] createHash() {
try { try {
List<Object> items = new ArrayList<Object>(); List<Object> items = new ArrayList<Object>();
@@ -226,14 +226,22 @@ public class NoUser implements Serializable {
} }
public static NoUser createUserFromFile(byte[] data, char[] password, Class<? extends NoUser> clazz) public static NoUser createUserFromFile(byte[] data, char[] password, Class<? extends NoUser> clazz)
throws IllegalBlockSizeException, BadPaddingException { throws NoUserNotValidException {
byte[] decrypted = NoUtil.decrypt(data, password); byte[] decrypted;
try {
decrypted = NoUtil.decrypt(data, password);
} catch (IllegalBlockSizeException | BadPaddingException e) {
throw new NoUserNotValidException(e);
}
Gson gson = new Gson(); Gson gson = new Gson();
String json = NoUtil.fromBytes(decrypted); String json = NoUtil.fromBytes(decrypted);
try {
NoUser noUser = gson.fromJson(json, clazz); NoUser noUser = gson.fromJson(json, clazz);
return noUser; return noUser;
} catch (JsonSyntaxException e) {
throw new NoUserNotValidException(e);
}
} }
public String createHashString() { public String createHashString() {

View File

@@ -26,6 +26,7 @@ import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException; import javax.crypto.IllegalBlockSizeException;
import nodash.core.NoUtil; import nodash.core.NoUtil;
import nodash.exceptions.NoUserNotValidException;
import nodash.models.NoUser; import nodash.models.NoUser;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
@@ -82,7 +83,8 @@ public class NoUserTest {
@Test @Test
public void testCreateUserFromFile() public void testCreateUserFromFile()
throws IllegalBlockSizeException, BadPaddingException, ClassNotFoundException, IOException { throws IllegalBlockSizeException, BadPaddingException,
ClassNotFoundException, IOException, NoUserNotValidException {
NoUser user = new NoUser(); NoUser user = new NoUser();
final byte[] originalFile = user.createFile("password".toCharArray()); final byte[] originalFile = user.createFile("password".toCharArray());
byte[] file = Arrays.copyOf(originalFile, originalFile.length); byte[] file = Arrays.copyOf(originalFile, originalFile.length);
@@ -93,7 +95,7 @@ public class NoUserTest {
try { try {
user = NoUser.createUserFromFile(file, "wrongpassword".toCharArray(), NoUtil.NO_USER_CLASS); user = NoUser.createUserFromFile(file, "wrongpassword".toCharArray(), NoUtil.NO_USER_CLASS);
fail("Should have thrown an error when given wrong password."); fail("Should have thrown an error when given wrong password.");
} catch (BadPaddingException e) { } catch (NoUserNotValidException e) {
// Do nothing, correct // Do nothing, correct
} }