mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-12-05 00:02:27 -08:00
Better KDF Exception management
This commit is contained in:
1 parent
4192cf2403
commit
fe8c962f73
12 files changed
+103
-64
No files matched your search
@@ -33,8 +33,8 @@ import java.util.UUID;
|
||||
|
||||
public class AesKdf extends KdfEngine {
|
||||
|
||||
public static final int DEFAULT_ROUNDS = 6000;
|
||||
public static final String DEFAULT_NAME = "AES-KDF";
|
||||
private static final int DEFAULT_ROUNDS = 6000;
|
||||
private static final String DEFAULT_NAME = "AES-KDF";
|
||||
|
||||
public static final UUID CIPHER_UUID = Types.bytestoUUID(
|
||||
new byte[]{(byte) 0xC9, (byte) 0xD9, (byte) 0xF3, (byte) 0x9A, (byte) 0x62, (byte) 0x8A, (byte) 0x44, (byte) 0x60,
|
||||
@@ -44,8 +44,8 @@ public class AesKdf extends KdfEngine {
|
||||
public static final String ParamRounds = "R";
|
||||
public static final String ParamSeed = "S";
|
||||
|
||||
public AesKdf() {
|
||||
uuid = CIPHER_UUID;
|
||||
AesKdf() {
|
||||
setUUID(CIPHER_UUID);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -35,16 +35,16 @@ public class Argon2Kdf extends KdfEngine {
|
||||
(byte) 0x91, (byte) 0xF7, (byte) 0xA9, (byte) 0xA4, (byte)0x03, (byte) 0xE3, (byte) 0x0A, (byte) 0x0C
|
||||
});
|
||||
|
||||
public static final String ParamSalt = "S"; // byte[]
|
||||
public static final String ParamParallelism = "P"; // UInt32
|
||||
public static final String ParamMemory = "M"; // UInt64
|
||||
public static final String ParamIterations = "I"; // UInt64
|
||||
public static final String ParamVersion = "V"; // UInt32
|
||||
public static final String ParamSecretKey = "K"; // byte[]
|
||||
public static final String ParamAssocData = "A"; // byte[]
|
||||
private static final String ParamSalt = "S"; // byte[]
|
||||
private static final String ParamParallelism = "P"; // UInt32
|
||||
private static final String ParamMemory = "M"; // UInt64
|
||||
private static final String ParamIterations = "I"; // UInt64
|
||||
private static final String ParamVersion = "V"; // UInt32
|
||||
private static final String ParamSecretKey = "K"; // byte[]
|
||||
private static final String ParamAssocData = "A"; // byte[]
|
||||
|
||||
public static final long MinVersion = 0x10;
|
||||
public static final long MaxVersion = 0x13;
|
||||
private static final long MinVersion = 0x10;
|
||||
private static final long MaxVersion = 0x13;
|
||||
|
||||
private static final int MinSalt = 8;
|
||||
private static final int MaxSalt = Integer.MAX_VALUE;
|
||||
@@ -64,8 +64,8 @@ public class Argon2Kdf extends KdfEngine {
|
||||
|
||||
private static final String DEFAULT_NAME = "Argon2";
|
||||
|
||||
public Argon2Kdf() {
|
||||
uuid = CIPHER_UUID;
|
||||
Argon2Kdf() {
|
||||
setUUID(CIPHER_UUID);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -29,7 +29,15 @@ public abstract class KdfEngine implements ObjectNameResource{
|
||||
public static final int UNKNOW_VALUE = -1;
|
||||
public static final String UNKNOW_VALUE_STRING = String.valueOf(-1);
|
||||
|
||||
public UUID uuid;
|
||||
protected UUID uuid;
|
||||
|
||||
public UUID getUUID() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void setUUID(UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public abstract KdfParameters getDefaultParameters();
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
*/
|
||||
package com.kunzisoft.keepass.crypto.keyDerivation;
|
||||
|
||||
import com.kunzisoft.keepass.database.exception.UnknownKDF;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -28,22 +30,26 @@ public class KdfFactory {
|
||||
public static Argon2Kdf argon2Kdf = new Argon2Kdf();
|
||||
|
||||
public static List<KdfEngine> kdfListV3 = new ArrayList<>();
|
||||
public static List<KdfEngine> kdfList = new ArrayList<>();
|
||||
public static List<KdfEngine> kdfListV4 = new ArrayList<>();
|
||||
|
||||
static {
|
||||
kdfListV3.add(aesKdf);
|
||||
|
||||
kdfList.add(aesKdf);
|
||||
kdfList.add(argon2Kdf);
|
||||
kdfListV4.add(aesKdf);
|
||||
kdfListV4.add(argon2Kdf);
|
||||
}
|
||||
|
||||
public static KdfEngine get(KdfParameters kdfParameters) {
|
||||
for (KdfEngine engine: kdfList) {
|
||||
if (engine.uuid.equals(kdfParameters.kdfUUID)) {
|
||||
public static KdfEngine getEngineV4(KdfParameters kdfParameters) throws UnknownKDF {
|
||||
UnknownKDF unknownKDFException = new UnknownKDF();
|
||||
if (kdfParameters == null) {
|
||||
throw unknownKDFException;
|
||||
}
|
||||
for (KdfEngine engine: kdfListV4) {
|
||||
if (engine.getUUID().equals(kdfParameters.getUUID())) {
|
||||
return engine;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
throw unknownKDFException;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,7 +31,7 @@ import java.util.UUID;
|
||||
|
||||
public class KdfParameters extends VariantDictionary {
|
||||
|
||||
public UUID kdfUUID;
|
||||
private UUID kdfUUID;
|
||||
|
||||
private static final String ParamUUID = "$UUID";
|
||||
|
||||
@@ -39,6 +39,10 @@ public class KdfParameters extends VariantDictionary {
|
||||
kdfUUID = uuid;
|
||||
}
|
||||
|
||||
public UUID getUUID() {
|
||||
return kdfUUID;
|
||||
}
|
||||
|
||||
protected void setParamUUID() {
|
||||
setByteArray(ParamUUID, Types.UUIDtoBytes(kdfUUID));
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ public class Database {
|
||||
loadData(ctx, uri, password, keyfile, status, !Importer.DEBUG);
|
||||
}
|
||||
|
||||
public void loadData(Context ctx, Uri uri, String password, Uri keyfile, ProgressTaskUpdater status, boolean debug) throws IOException, FileNotFoundException, InvalidDBException {
|
||||
private void loadData(Context ctx, Uri uri, String password, Uri keyfile, ProgressTaskUpdater status, boolean debug) throws IOException, FileNotFoundException, InvalidDBException {
|
||||
mUri = uri;
|
||||
readOnly = false;
|
||||
if (uri.getScheme().equals("file")) {
|
||||
@@ -384,7 +384,7 @@ public class Database {
|
||||
public List<KdfEngine> getAvailableKdfEngines() {
|
||||
switch (getPwDatabase().getVersion()) {
|
||||
case V4:
|
||||
return KdfFactory.kdfList;
|
||||
return KdfFactory.kdfListV4;
|
||||
case V3:
|
||||
return KdfFactory.kdfListV3;
|
||||
}
|
||||
@@ -407,7 +407,7 @@ public class Database {
|
||||
case V4:
|
||||
PwDatabaseV4 db = ((PwDatabaseV4) getPwDatabase());
|
||||
if (db.getKdfParameters() == null
|
||||
|| !db.getKdfParameters().kdfUUID.equals(kdfEngine.getDefaultParameters().kdfUUID))
|
||||
|| !db.getKdfParameters().getUUID().equals(kdfEngine.getDefaultParameters().getUUID()))
|
||||
db.setKdfParameters(kdfEngine.getDefaultParameters());
|
||||
setNumberKeyEncryptionRounds(kdfEngine.getDefaultKeyRounds());
|
||||
setMemoryUsage(kdfEngine.getDefaultMemoryUsage());
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
*/
|
||||
package com.kunzisoft.keepass.database;
|
||||
|
||||
import android.util.Log;
|
||||
import android.webkit.URLUtil;
|
||||
|
||||
import com.kunzisoft.keepass.collections.VariantDictionary;
|
||||
@@ -30,6 +31,7 @@ import com.kunzisoft.keepass.crypto.keyDerivation.KdfEngine;
|
||||
import com.kunzisoft.keepass.crypto.keyDerivation.KdfFactory;
|
||||
import com.kunzisoft.keepass.crypto.keyDerivation.KdfParameters;
|
||||
import com.kunzisoft.keepass.database.exception.InvalidKeyFileException;
|
||||
import com.kunzisoft.keepass.database.exception.UnknownKDF;
|
||||
import com.kunzisoft.keepass.utils.EmptyUtils;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
@@ -50,6 +52,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
@@ -57,6 +60,7 @@ import biz.source_code.base64Coder.Base64Coder;
|
||||
|
||||
|
||||
public class PwDatabaseV4 extends PwDatabase<PwGroupV4, PwEntryV4> {
|
||||
private static final String TAG = PwDatabaseV4.class.getName();
|
||||
|
||||
private static final int DEFAULT_HISTORY_MAX_ITEMS = 10; // -1 unlimited
|
||||
private static final long DEFAULT_HISTORY_MAX_SIZE = 6 * 1024 * 1024; // -1 unlimited
|
||||
@@ -150,9 +154,14 @@ public class PwDatabaseV4 extends PwDatabase<PwGroupV4, PwEntryV4> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public KdfEngine getKdfEngine() {
|
||||
return KdfFactory.get(getKdfParameters());
|
||||
}
|
||||
public @Nullable KdfEngine getKdfEngine() {
|
||||
try {
|
||||
return KdfFactory.getEngineV4(kdfParameters);
|
||||
} catch (UnknownKDF unknownKDF) {
|
||||
Log.i(TAG, "Unable to retrieve KDF engine", unknownKDF);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public KdfParameters getKdfParameters() {
|
||||
return kdfParameters;
|
||||
@@ -398,16 +407,12 @@ public class PwDatabaseV4 extends PwDatabase<PwGroupV4, PwEntryV4> {
|
||||
makeFinalKey(masterSeed, 0);
|
||||
}
|
||||
|
||||
public void makeFinalKey(byte[] masterSeed, long roundsFix)
|
||||
throws IOException {
|
||||
public void makeFinalKey(byte[] masterSeed, long roundsFix) throws IOException {
|
||||
|
||||
KdfEngine kdfEngine = KdfFactory.get(kdfParameters);
|
||||
if (kdfEngine == null) {
|
||||
throw new IOException("Unknown key derivation function");
|
||||
}
|
||||
KdfEngine kdfEngine = KdfFactory.getEngineV4(kdfParameters);
|
||||
|
||||
// Set to 6000 rounds to open corrupted database
|
||||
if (roundsFix > 0 && kdfParameters.kdfUUID.equals(AesKdf.CIPHER_UUID)) {
|
||||
if (roundsFix > 0 && kdfParameters.getUUID().equals(AesKdf.CIPHER_UUID)) {
|
||||
kdfParameters.setUInt32(AesKdf.ParamRounds, roundsFix);
|
||||
numKeyEncRounds = roundsFix;
|
||||
}
|
||||
|
||||
@@ -146,7 +146,7 @@ public class PwDbHeaderV4 extends PwDbHeader {
|
||||
private int getMinKdbxVersion(PwDatabaseV4 databaseV4) {
|
||||
// Return v4 if AES is not use
|
||||
if (databaseV4.getKdfParameters() != null
|
||||
&& !databaseV4.getKdfParameters().kdfUUID.equals(AesKdf.CIPHER_UUID)) {
|
||||
&& !databaseV4.getKdfParameters().getUUID().equals(AesKdf.CIPHER_UUID)) {
|
||||
return PwDbHeaderV4.FILE_VERSION_32;
|
||||
}
|
||||
|
||||
@@ -287,7 +287,7 @@ public class PwDbHeaderV4 extends PwDbHeader {
|
||||
}
|
||||
|
||||
private void assignAesKdfEngineIfNotExists() {
|
||||
if (db.getKdfParameters() == null || !db.getKdfParameters().kdfUUID.equals(KdfFactory.aesKdf.uuid)) {
|
||||
if (db.getKdfParameters() == null || !db.getKdfParameters().getUUID().equals(KdfFactory.aesKdf.getUUID())) {
|
||||
db.setKdfParameters(KdfFactory.aesKdf.getDefaultParameters());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.kunzisoft.keepass.database.exception;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class UnknownKDF extends IOException {
|
||||
|
||||
private static String message = "Unknown key derivation function";
|
||||
|
||||
public UnknownKDF() {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public UnknownKDF(Exception e) {
|
||||
super(message, e);
|
||||
}
|
||||
}
|
||||
@@ -125,14 +125,8 @@ public class ImporterV4 extends Importer {
|
||||
db.setDataEngine(engine);
|
||||
db.setEncryptionAlgorithm(engine.getPwEncryptionAlgorithm());
|
||||
cipher = engine.getCipher(Cipher.DECRYPT_MODE, db.getFinalKey(), header.encryptionIV);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IOException("Invalid algorithm.");
|
||||
} catch (NoSuchPaddingException e) {
|
||||
throw new IOException("Invalid algorithm.");
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new IOException("Invalid algorithm.");
|
||||
} catch (InvalidAlgorithmParameterException e) {
|
||||
throw new IOException("Invalid algorithm.");
|
||||
} catch (NoSuchAlgorithmException|NoSuchPaddingException|InvalidKeyException|InvalidAlgorithmParameterException e) {
|
||||
throw new IOException("Invalid algorithm.", e);
|
||||
}
|
||||
|
||||
InputStream isPlain;
|
||||
|
||||
@@ -63,7 +63,7 @@ public class PwDbV3Output extends PwDbOutput<PwDbHeaderV3> {
|
||||
mPM.makeFinalKey(h3.masterSeed, h3.transformSeed, mPM.getNumberKeyEncryptionRounds());
|
||||
return mPM.getFinalKey();
|
||||
} catch (IOException e) {
|
||||
throw new PwDbOutputException("Key creation failed: " + e.getMessage());
|
||||
throw new PwDbOutputException("Key creation failed.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ public class PwDbV3Output extends PwDbOutput<PwDbHeaderV3> {
|
||||
throw new Exception();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new PwDbOutputException("Algorithm not supported.");
|
||||
throw new PwDbOutputException("Algorithm not supported.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -97,11 +97,11 @@ public class PwDbV3Output extends PwDbOutput<PwDbHeaderV3> {
|
||||
bos.close();
|
||||
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new PwDbOutputException("Invalid key");
|
||||
throw new PwDbOutputException("Invalid key", e);
|
||||
} catch (InvalidAlgorithmParameterException e) {
|
||||
throw new PwDbOutputException("Invalid algorithm parameter.");
|
||||
throw new PwDbOutputException("Invalid algorithm parameter.", e);
|
||||
} catch (IOException e) {
|
||||
throw new PwDbOutputException("Failed to output final encrypted part.");
|
||||
throw new PwDbOutputException("Failed to output final encrypted part.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ public class PwDbV3Output extends PwDbOutput<PwDbHeaderV3> {
|
||||
try {
|
||||
md = MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new PwDbOutputException("SHA-256 not implemented here.");
|
||||
throw new PwDbOutputException("SHA-256 not implemented here.", e);
|
||||
}
|
||||
|
||||
// Header checksum
|
||||
@@ -153,7 +153,7 @@ public class PwDbV3Output extends PwDbOutput<PwDbHeaderV3> {
|
||||
try {
|
||||
headerDigest = MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new PwDbOutputException("SHA-256 not implemented here.");
|
||||
throw new PwDbOutputException("SHA-256 not implemented here.", e);
|
||||
}
|
||||
NullOutputStream nos;
|
||||
nos = new NullOutputStream();
|
||||
@@ -180,7 +180,7 @@ public class PwDbV3Output extends PwDbOutput<PwDbHeaderV3> {
|
||||
bos.flush();
|
||||
bos.close();
|
||||
} catch (IOException e) {
|
||||
throw new PwDbOutputException("Failed to generate checksum.");
|
||||
throw new PwDbOutputException("Failed to generate checksum.", e);
|
||||
}
|
||||
|
||||
header.contentsHash = md.digest();
|
||||
@@ -210,7 +210,7 @@ public class PwDbV3Output extends PwDbOutput<PwDbHeaderV3> {
|
||||
los.writeInt(headerHashBlock.length);
|
||||
los.write(headerHashBlock);
|
||||
} catch (IOException e) {
|
||||
throw new PwDbOutputException("Failed to output header hash: " + e.getMessage());
|
||||
throw new PwDbOutputException("Failed to output header hash.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,7 +222,7 @@ public class PwDbV3Output extends PwDbOutput<PwDbHeaderV3> {
|
||||
try {
|
||||
pgo.output();
|
||||
} catch (IOException e) {
|
||||
throw new PwDbOutputException("Failed to output a tree: " + e.getMessage());
|
||||
throw new PwDbOutputException("Failed to output a tree", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,7 +233,7 @@ public class PwDbV3Output extends PwDbOutput<PwDbHeaderV3> {
|
||||
try {
|
||||
peo.output();
|
||||
} catch (IOException e) {
|
||||
throw new PwDbOutputException("Failed to output an entry.");
|
||||
throw new PwDbOutputException("Failed to output an entry.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
*/
|
||||
package com.kunzisoft.keepass.database.save;
|
||||
|
||||
import android.util.Log;
|
||||
import android.util.Xml;
|
||||
|
||||
import com.kunzisoft.keepass.crypto.CipherFactory;
|
||||
@@ -42,6 +43,7 @@ import com.kunzisoft.keepass.database.PwEntryV4;
|
||||
import com.kunzisoft.keepass.database.PwGroupV4;
|
||||
import com.kunzisoft.keepass.database.PwIconCustom;
|
||||
import com.kunzisoft.keepass.database.exception.PwDbOutputException;
|
||||
import com.kunzisoft.keepass.database.exception.UnknownKDF;
|
||||
import com.kunzisoft.keepass.database.security.ProtectedBinary;
|
||||
import com.kunzisoft.keepass.database.security.ProtectedString;
|
||||
import com.kunzisoft.keepass.stream.HashedBlockOutputStream;
|
||||
@@ -74,6 +76,7 @@ import javax.crypto.CipherOutputStream;
|
||||
import biz.source_code.base64Coder.Base64Coder;
|
||||
|
||||
public class PwDbV4Output extends PwDbOutput<PwDbHeaderV4> {
|
||||
private static final String TAG = PwDbV4Output.class.getName();
|
||||
|
||||
private PwDatabaseV4 mPM;
|
||||
private StreamCipher randomStream;
|
||||
@@ -276,10 +279,8 @@ public class PwDbV4Output extends PwDbOutput<PwDbHeaderV4> {
|
||||
} catch (Exception e) {
|
||||
throw new PwDbOutputException("Invalid algorithm.", e);
|
||||
}
|
||||
|
||||
CipherOutputStream cos = new CipherOutputStream(os, cipher);
|
||||
|
||||
return cos;
|
||||
|
||||
return new CipherOutputStream(os, cipher);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -296,8 +297,13 @@ public class PwDbV4Output extends PwDbOutput<PwDbHeaderV4> {
|
||||
if (mPM.getKdfParameters() == null) {
|
||||
mPM.setKdfParameters(KdfFactory.aesKdf.getDefaultParameters());
|
||||
}
|
||||
KdfEngine kdf = KdfFactory.get(mPM.getKdfParameters());
|
||||
kdf.randomize(mPM.getKdfParameters());
|
||||
|
||||
try {
|
||||
KdfEngine kdf = KdfFactory.getEngineV4(mPM.getKdfParameters());
|
||||
kdf.randomize(mPM.getKdfParameters());
|
||||
} catch (UnknownKDF unknownKDF) {
|
||||
Log.e(TAG, "Unable to retrieve header", unknownKDF);
|
||||
}
|
||||
|
||||
if (header.getVersion() < PwDbHeaderV4.FILE_VERSION_32_4) {
|
||||
header.innerRandomStream = CrsAlgorithm.Salsa20;
|
||||
|
||||
Reference in new issue
Block a user