Unverified 提交 7db8f19e authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #1075 from grandinj/1003_decrypt

Issue #1003: Decrypting database with incorrect password renders the database corrupt 
...@@ -21,6 +21,8 @@ Change Log ...@@ -21,6 +21,8 @@ Change Log
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul> <ul>
<li>Issue #1003: Decrypting database with incorrect password renders the database corrupt
</li>
<li>Issue #873: No error when `=` in equal condition when column is not of array type <li>Issue #873: No error when `=` in equal condition when column is not of array type
</li> </li>
<li>Issue #1069: Failed to add DATETIME(3) column since 1.4.197 <li>Issue #1069: Failed to add DATETIME(3) column since 1.4.197
......
...@@ -12,9 +12,9 @@ import java.nio.channels.FileChannel; ...@@ -12,9 +12,9 @@ import java.nio.channels.FileChannel;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.MVStore;
import org.h2.security.SHA256; import org.h2.security.SHA256;
import org.h2.store.FileLister; import org.h2.store.FileLister;
import org.h2.store.FileStore; import org.h2.store.FileStore;
...@@ -63,8 +63,13 @@ public class ChangeFileEncryption extends Tool { ...@@ -63,8 +63,13 @@ public class ChangeFileEncryption extends Tool {
* *
* @param args the command line arguments * @param args the command line arguments
*/ */
public static void main(String... args) throws SQLException { public static void main(String... args) {
try {
new ChangeFileEncryption().runTool(args); new ChangeFileEncryption().runTool(args);
} catch (SQLException ex) {
ex.printStackTrace(System.err);
System.exit(1);
}
} }
@Override @Override
...@@ -109,8 +114,7 @@ public class ChangeFileEncryption extends Tool { ...@@ -109,8 +114,7 @@ public class ChangeFileEncryption extends Tool {
} }
/** /**
* Get the file encryption key for a given password. The password must be * Get the file encryption key for a given password.
* supplied as char arrays and is cleaned in this method.
* *
* @param password the password as a char array * @param password the password as a char array
* @return the encryption key * @return the encryption key
...@@ -119,7 +123,8 @@ public class ChangeFileEncryption extends Tool { ...@@ -119,7 +123,8 @@ public class ChangeFileEncryption extends Tool {
if (password == null) { if (password == null) {
return null; return null;
} }
return SHA256.getKeyPasswordHash("file", password); // the clone is to avoid the unhelpful array cleaning
return SHA256.getKeyPasswordHash("file", password.clone());
} }
/** /**
...@@ -187,22 +192,22 @@ public class ChangeFileEncryption extends Tool { ...@@ -187,22 +192,22 @@ public class ChangeFileEncryption extends Tool {
for (String fileName : files) { for (String fileName : files) {
// don't process a lob directory, just the files in the directory. // don't process a lob directory, just the files in the directory.
if (!FileUtils.isDirectory(fileName)) { if (!FileUtils.isDirectory(fileName)) {
change.process(fileName, quiet); change.process(fileName, quiet, decryptPassword);
} }
} }
} }
private void process(String fileName, boolean quiet) { private void process(String fileName, boolean quiet, char[] decryptPassword) throws SQLException {
if (fileName.endsWith(Constants.SUFFIX_MV_FILE)) { if (fileName.endsWith(Constants.SUFFIX_MV_FILE)) {
try { try {
copy(fileName, quiet); copyMvStore(fileName, quiet, decryptPassword);
} catch (IOException e) { } catch (IOException e) {
throw DbException.convertIOException(e, throw DbException.convertIOException(e,
"Error encrypting / decrypting file " + fileName); "Error encrypting / decrypting file " + fileName);
} }
return; return;
} }
FileStore in; final FileStore in;
if (decrypt == null) { if (decrypt == null) {
in = FileStore.open(null, fileName, "r"); in = FileStore.open(null, fileName, "r");
} else { } else {
...@@ -210,23 +215,35 @@ public class ChangeFileEncryption extends Tool { ...@@ -210,23 +215,35 @@ public class ChangeFileEncryption extends Tool {
} }
try { try {
in.init(); in.init();
copy(fileName, in, encrypt, quiet); copyPageStore(fileName, in, encrypt, quiet);
} finally { } finally {
in.closeSilently(); in.closeSilently();
} }
} }
private void copy(String fileName, boolean quiet) throws IOException { private void copyMvStore(String fileName, boolean quiet, char[] decryptPassword) throws IOException, SQLException {
if (FileUtils.isDirectory(fileName)) { if (FileUtils.isDirectory(fileName)) {
return; return;
} }
// check that we have the right encryption key
try {
final MVStore source = new MVStore.Builder().
fileName(fileName).
readOnly().
encryptionKey(decryptPassword).
open();
source.close();
} catch (IllegalStateException ex) {
throw new SQLException("error decrypting file " + fileName, ex);
}
String temp = directory + "/temp.db"; String temp = directory + "/temp.db";
try (FileChannel fileIn = getFileChannel(fileName, "r", decryptKey)){ try (FileChannel fileIn = getFileChannel(fileName, "r", decryptKey)){
try(InputStream inStream = new FileChannelInputStream(fileIn, true)) { try(InputStream inStream = new FileChannelInputStream(fileIn, true)) {
FileUtils.delete(temp); FileUtils.delete(temp);
try (OutputStream outStream = new FileChannelOutputStream(getFileChannel(temp, "rw", encryptKey), try (OutputStream outStream = new FileChannelOutputStream(getFileChannel(temp, "rw", encryptKey),
true)) { true)) {
byte[] buffer = new byte[4 * 1024]; final byte[] buffer = new byte[4 * 1024];
long remaining = fileIn.size(); long remaining = fileIn.size();
long total = remaining; long total = remaining;
long time = System.nanoTime(); long time = System.nanoTime();
...@@ -247,19 +264,21 @@ public class ChangeFileEncryption extends Tool { ...@@ -247,19 +264,21 @@ public class ChangeFileEncryption extends Tool {
FileUtils.move(temp, fileName); FileUtils.move(temp, fileName);
} }
private FileChannel getFileChannel(String fileName, String r, byte[] decryptKey) throws IOException { private static FileChannel getFileChannel(String fileName, String r,
byte[] decryptKey) throws IOException {
FileChannel fileIn = FilePath.get(fileName).open(r); FileChannel fileIn = FilePath.get(fileName).open(r);
if (decryptKey != null) { if (decryptKey != null) {
fileIn = new FilePathEncrypt.FileEncrypt(fileName, decryptKey, fileIn); fileIn = new FilePathEncrypt.FileEncrypt(fileName, decryptKey,
fileIn);
} }
return fileIn; return fileIn;
} }
private void copy(String fileName, FileStore in, byte[] key, boolean quiet) { private void copyPageStore(String fileName, FileStore in, byte[] key, boolean quiet) {
if (FileUtils.isDirectory(fileName)) { if (FileUtils.isDirectory(fileName)) {
return; return;
} }
String temp = directory + "/temp.db"; final String temp = directory + "/temp.db";
FileUtils.delete(temp); FileUtils.delete(temp);
FileStore fileOut; FileStore fileOut;
if (key == null) { if (key == null) {
...@@ -267,8 +286,8 @@ public class ChangeFileEncryption extends Tool { ...@@ -267,8 +286,8 @@ public class ChangeFileEncryption extends Tool {
} else { } else {
fileOut = FileStore.open(null, temp, "rw", cipherType, key); fileOut = FileStore.open(null, temp, "rw", cipherType, key);
} }
final byte[] buffer = new byte[4 * 1024];
fileOut.init(); fileOut.init();
byte[] buffer = new byte[4 * 1024];
long remaining = in.length() - FileStore.HEADER_LENGTH; long remaining = in.length() - FileStore.HEADER_LENGTH;
long total = remaining; long total = remaining;
in.seek(FileStore.HEADER_LENGTH); in.seek(FileStore.HEADER_LENGTH);
......
...@@ -13,7 +13,6 @@ import java.io.PrintStream; ...@@ -13,7 +13,6 @@ import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.h2.store.fs.FileUtils; import org.h2.store.fs.FileUtils;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.tools.DeleteDbFiles; import org.h2.tools.DeleteDbFiles;
...@@ -106,8 +105,6 @@ public class TestSampleApps extends TestBase { ...@@ -106,8 +105,6 @@ public class TestSampleApps extends TestBase {
// tools // tools
testApp("Allows changing the database file encryption password or algorithm*", testApp("Allows changing the database file encryption password or algorithm*",
org.h2.tools.ChangeFileEncryption.class, "-help"); org.h2.tools.ChangeFileEncryption.class, "-help");
testApp("Allows changing the database file encryption password or algorithm*",
org.h2.tools.ChangeFileEncryption.class);
testApp("Deletes all files belonging to a database.*", testApp("Deletes all files belonging to a database.*",
org.h2.tools.DeleteDbFiles.class, "-help"); org.h2.tools.DeleteDbFiles.class, "-help");
FileUtils.delete(getBaseDir() + "/optimizations.sql"); FileUtils.delete(getBaseDir() + "/optimizations.sql");
......
...@@ -38,7 +38,6 @@ import java.util.ArrayList; ...@@ -38,7 +38,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.UUID; import java.util.UUID;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.engine.SysProperties; import org.h2.engine.SysProperties;
import org.h2.store.FileLister; import org.h2.store.FileLister;
...@@ -1033,18 +1032,18 @@ public class TestTools extends TestBase { ...@@ -1033,18 +1032,18 @@ public class TestTools extends TestBase {
conn.close(); conn.close();
String[] args = { "-dir", dir, "-db", "testChangeFileEncryption", String[] args = { "-dir", dir, "-db", "testChangeFileEncryption",
"-cipher", "AES", "-decrypt", "abc", "-quiet" }; "-cipher", "AES", "-decrypt", "abc", "-quiet" };
ChangeFileEncryption.main(args); new ChangeFileEncryption().runTool(args);
args = new String[] { "-dir", dir, "-db", "testChangeFileEncryption", args = new String[] { "-dir", dir, "-db", "testChangeFileEncryption",
"-cipher", "AES", "-encrypt", "def", "-quiet" }; "-cipher", "AES", "-encrypt", "def", "-quiet" };
ChangeFileEncryption.main(args); new ChangeFileEncryption().runTool(args);
conn = getConnection(url, "sa", "def 123"); conn = getConnection(url, "sa", "def 123");
stat = conn.createStatement(); stat = conn.createStatement();
stat.execute("SELECT * FROM TEST"); stat.execute("SELECT * FROM TEST");
new AssertThrows(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1) { new AssertThrows(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1) {
@Override @Override
public void test() throws SQLException { public void test() throws SQLException {
ChangeFileEncryption.main(new String[] { "-dir", dir, "-db", new ChangeFileEncryption().runTool(new String[] { "-dir", dir,
"testChangeFileEncryption", "-cipher", "AES", "-db", "testChangeFileEncryption", "-cipher", "AES",
"-decrypt", "def", "-quiet" }); "-decrypt", "def", "-quiet" });
} }
}; };
...@@ -1055,14 +1054,8 @@ public class TestTools extends TestBase { ...@@ -1055,14 +1054,8 @@ public class TestTools extends TestBase {
} }
private void testChangeFileEncryptionWithWrongPassword() throws SQLException { private void testChangeFileEncryptionWithWrongPassword() throws SQLException {
if (config.mvStore) {
// the file system encryption abstraction used by the MVStore
// doesn't detect wrong passwords
return;
}
org.h2.Driver.load(); org.h2.Driver.load();
final String dir = getBaseDir(); final String dir = getBaseDir();
// TODO: this doesn't seem to work in MVSTORE mode yet
String url = "jdbc:h2:" + dir + "/testChangeFileEncryption;CIPHER=AES"; String url = "jdbc:h2:" + dir + "/testChangeFileEncryption;CIPHER=AES";
DeleteDbFiles.execute(dir, "testChangeFileEncryption", true); DeleteDbFiles.execute(dir, "testChangeFileEncryption", true);
Connection conn = getConnection(url, "sa", "abc 123"); Connection conn = getConnection(url, "sa", "abc 123");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论