Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
696536b6
提交
696536b6
authored
4月 18, 2018
作者:
Noel Grandin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Issue #1003: Decrypting database with incorrect password renders the database corrupt
上级
6fa531aa
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
43 行增加
和
23 行删除
+43
-23
changelog.html
h2/src/docsrc/html/changelog.html
+2
-0
ChangeFileEncryption.java
h2/src/main/org/h2/tools/ChangeFileEncryption.java
+37
-18
TestTools.java
h2/src/test/org/h2/test/unit/TestTools.java
+4
-5
没有找到文件。
h2/src/docsrc/html/changelog.html
浏览文件 @
696536b6
...
@@ -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
...
...
h2/src/main/org/h2/tools/ChangeFileEncryption.java
浏览文件 @
696536b6
...
@@ -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
)
{
new
ChangeFileEncryption
().
runTool
(
args
);
try
{
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
);
copy
MvStore
(
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
);
copy
PageStore
(
fileName
,
in
,
encrypt
,
quiet
);
}
finally
{
}
finally
{
in
.
closeSilently
();
in
.
closeSilently
();
}
}
}
}
private
void
copy
(
String
fileName
,
boolean
quiet
)
throws
IO
Exception
{
private
void
copy
MvStore
(
String
fileName
,
boolean
quiet
,
char
[]
decryptPassword
)
throws
IOException
,
SQL
Exception
{
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
copy
PageStore
(
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
);
...
...
h2/src/test/org/h2/test/unit/TestTools.java
浏览文件 @
696536b6
...
@@ -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"
});
}
}
};
};
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论