| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.store.fs.encrypt; |
| 7 |
|
| 8 |
import java.io.EOFException; |
| 9 |
import java.io.IOException; |
| 10 |
import java.nio.ByteBuffer; |
| 11 |
import java.nio.channels.FileChannel; |
| 12 |
import java.nio.channels.FileLock; |
| 13 |
import java.nio.charset.StandardCharsets; |
| 14 |
import java.util.Arrays; |
| 15 |
import org.h2.mvstore.DataUtils; |
| 16 |
import org.h2.security.AES; |
| 17 |
import org.h2.security.SHA256; |
| 18 |
import org.h2.store.fs.FileBaseDefault; |
| 19 |
import org.h2.util.MathUtils; |
| 20 |
|
| 21 |
/** |
| 22 |
* An encrypted file with a read cache. |
| 23 |
*/ |
| 24 |
public class FileEncrypt extends FileBaseDefault { |
| 25 |
|
| 26 |
/** |
| 27 |
* The block size. |
| 28 |
*/ |
| 29 |
public static final int BLOCK_SIZE = 4096; |
| 30 |
|
| 31 |
/** |
| 32 |
* The block size bit mask. |
| 33 |
*/ |
| 34 |
static final int BLOCK_SIZE_MASK = BLOCK_SIZE - 1; |
| 35 |
|
| 36 |
/** |
| 37 |
* The length of the file header. Using a smaller header is possible, |
| 38 |
* but would mean reads and writes are not aligned to the block size. |
| 39 |
*/ |
| 40 |
static final int HEADER_LENGTH = BLOCK_SIZE; |
| 41 |
|
| 42 |
private static final byte[] HEADER = "H2encrypt\n".getBytes(StandardCharsets.ISO_8859_1); |
| 43 |
private static final int SALT_POS = HEADER.length; |
| 44 |
|
| 45 |
/** |
| 46 |
* The length of the salt, in bytes. |
| 47 |
*/ |
| 48 |
private static final int SALT_LENGTH = 8; |
| 49 |
|
| 50 |
/** |
| 51 |
* The number of iterations. It is relatively low; a higher value would |
| 52 |
* slow down opening files on Android too much. |
| 53 |
*/ |
| 54 |
private static final int HASH_ITERATIONS = 10; |
| 55 |
|
| 56 |
private final FileChannel base; |
| 57 |
|
| 58 |
/** |
| 59 |
* The current file size, from a user perspective. |
| 60 |
*/ |
| 61 |
private volatile long size; |
| 62 |
|
| 63 |
private final String name; |
| 64 |
|
| 65 |
private volatile XTS xts; |
| 66 |
|
| 67 |
private byte[] encryptionKey; |
| 68 |
|
| 69 |
private FileEncrypt source; |
| 70 |
|
| 71 |
public FileEncrypt(String name, byte[] encryptionKey, FileChannel base) { |
| 72 |
// don't do any read or write operations here, because they could |
| 73 |
// fail if the file is locked, and we want to give the caller a |
| 74 |
// chance to lock the file first |
| 75 |
this.name = name; |
| 76 |
this.base = base; |
| 77 |
this.encryptionKey = encryptionKey; |
| 78 |
} |
| 79 |
|
| 80 |
public FileEncrypt(String name, FileEncrypt source, FileChannel base) { |
| 81 |
// don't do any read or write operations here, because they could |
| 82 |
// fail if the file is locked, and we want to give the caller a |
| 83 |
// chance to lock the file first |
| 84 |
this.name = name; |
| 85 |
this.base = base; |
| 86 |
this.source = source; |
| 87 |
try { |
| 88 |
source.init(); |
| 89 |
} catch (IOException e) { |
| 90 |
throw DataUtils.newMVStoreException(DataUtils.ERROR_INTERNAL, |
| 91 |
"Can not open {0} using encryption of {1}", name, source.name); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
private XTS init() throws IOException { |
| 96 |
// Keep this method small to allow inlining |
| 97 |
XTS xts = this.xts; |
| 98 |
if (xts == null) { |
| 99 |
xts = createXTS(); |
| 100 |
} |
| 101 |
return xts; |
| 102 |
} |
| 103 |
|
| 104 |
private synchronized XTS createXTS() throws IOException { |
| 105 |
XTS xts = this.xts; |
| 106 |
if (xts != null) { |
| 107 |
return xts; |
| 108 |
} |
| 109 |
assert size == 0; |
| 110 |
long sz = base.size() - HEADER_LENGTH; |
| 111 |
boolean existingFile = sz >= 0; |
| 112 |
if (encryptionKey != null) { |
| 113 |
byte[] salt; |
| 114 |
if (existingFile) { |
| 115 |
salt = new byte[SALT_LENGTH]; |
| 116 |
readFully(base, SALT_POS, ByteBuffer.wrap(salt)); |
| 117 |
} else { |
| 118 |
byte[] header = Arrays.copyOf(HEADER, BLOCK_SIZE); |
| 119 |
salt = MathUtils.secureRandomBytes(SALT_LENGTH); |
| 120 |
System.arraycopy(salt, 0, header, SALT_POS, salt.length); |
| 121 |
writeFully(base, 0, ByteBuffer.wrap(header)); |
| 122 |
} |
| 123 |
AES cipher = new AES(); |
| 124 |
cipher.setKey(SHA256.getPBKDF2(encryptionKey, salt, HASH_ITERATIONS, 16)); |
| 125 |
encryptionKey = null; |
| 126 |
xts = new XTS(cipher); |
| 127 |
} else { |
| 128 |
if (!existingFile) { |
| 129 |
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BLOCK_SIZE); |
| 130 |
readFully(source.base, 0, byteBuffer); |
| 131 |
byteBuffer.flip(); |
| 132 |
writeFully(base, 0, byteBuffer); |
| 133 |
} |
| 134 |
xts = source.xts; |
| 135 |
source = null; |
| 136 |
} |
| 137 |
if (existingFile) { |
| 138 |
if ((sz & BLOCK_SIZE_MASK) != 0) { |
| 139 |
sz -= BLOCK_SIZE; |
| 140 |
} |
| 141 |
size = sz; |
| 142 |
} |
| 143 |
return this.xts = xts; |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
protected void implCloseChannel() throws IOException { |
| 148 |
base.close(); |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
public int read(ByteBuffer dst, long position) throws IOException { |
| 153 |
int len = dst.remaining(); |
| 154 |
if (len == 0) { |
| 155 |
return 0; |
| 156 |
} |
| 157 |
XTS xts = init(); |
| 158 |
len = (int) Math.min(len, size - position); |
| 159 |
if (position >= size) { |
| 160 |
return -1; |
| 161 |
} else if (position < 0) { |
| 162 |
throw new IllegalArgumentException("pos: " + position); |
| 163 |
} |
| 164 |
if ((position & BLOCK_SIZE_MASK) != 0 || (len & BLOCK_SIZE_MASK) != 0) { |
| 165 |
// either the position or the len is unaligned: |
| 166 |
// read aligned, and then truncate |
| 167 |
long p = position / BLOCK_SIZE * BLOCK_SIZE; |
| 168 |
int offset = (int) (position - p); |
| 169 |
int l = (len + offset + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE; |
| 170 |
ByteBuffer temp = ByteBuffer.allocate(l); |
| 171 |
readInternal(temp, p, l, xts); |
| 172 |
temp.flip().limit(offset + len).position(offset); |
| 173 |
dst.put(temp); |
| 174 |
return len; |
| 175 |
} |
| 176 |
readInternal(dst, position, len, xts); |
| 177 |
return len; |
| 178 |
} |
| 179 |
|
| 180 |
private void readInternal(ByteBuffer dst, long position, int len, XTS xts) throws IOException { |
| 181 |
int x = dst.position(); |
| 182 |
readFully(base, position + HEADER_LENGTH, dst); |
| 183 |
long block = position / BLOCK_SIZE; |
| 184 |
while (len > 0) { |
| 185 |
xts.decrypt(block++, BLOCK_SIZE, dst.array(), dst.arrayOffset() + x); |
| 186 |
x += BLOCK_SIZE; |
| 187 |
len -= BLOCK_SIZE; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
private static void readFully(FileChannel file, long pos, ByteBuffer dst) throws IOException { |
| 192 |
do { |
| 193 |
int len = file.read(dst, pos); |
| 194 |
if (len < 0) { |
| 195 |
throw new EOFException(); |
| 196 |
} |
| 197 |
pos += len; |
| 198 |
} while (dst.remaining() > 0); |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public int write(ByteBuffer src, long position) throws IOException { |
| 203 |
XTS xts = init(); |
| 204 |
int len = src.remaining(); |
| 205 |
if ((position & BLOCK_SIZE_MASK) != 0 || (len & BLOCK_SIZE_MASK) != 0) { |
| 206 |
// either the position or the len is unaligned: |
| 207 |
// read aligned, and then truncate |
| 208 |
long p = position / BLOCK_SIZE * BLOCK_SIZE; |
| 209 |
int offset = (int) (position - p); |
| 210 |
int l = (len + offset + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE; |
| 211 |
ByteBuffer temp = ByteBuffer.allocate(l); |
| 212 |
int available = (int) (size - p + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE; |
| 213 |
int readLen = Math.min(l, available); |
| 214 |
if (readLen > 0) { |
| 215 |
readInternal(temp, p, readLen, xts); |
| 216 |
temp.rewind(); |
| 217 |
} |
| 218 |
temp.limit(offset + len).position(offset); |
| 219 |
temp.put(src).limit(l).rewind(); |
| 220 |
writeInternal(temp, p, l, xts); |
| 221 |
long p2 = position + len; |
| 222 |
size = Math.max(size, p2); |
| 223 |
int plus = (int) (size & BLOCK_SIZE_MASK); |
| 224 |
if (plus > 0) { |
| 225 |
temp = ByteBuffer.allocate(plus); |
| 226 |
writeFully(base, p + HEADER_LENGTH + l, temp); |
| 227 |
} |
| 228 |
return len; |
| 229 |
} |
| 230 |
writeInternal(src, position, len, xts); |
| 231 |
long p2 = position + len; |
| 232 |
size = Math.max(size, p2); |
| 233 |
return len; |
| 234 |
} |
| 235 |
|
| 236 |
private void writeInternal(ByteBuffer src, long position, int len, XTS xts) throws IOException { |
| 237 |
ByteBuffer crypt = ByteBuffer.allocate(len).put(src); |
| 238 |
crypt.flip(); |
| 239 |
long block = position / BLOCK_SIZE; |
| 240 |
int x = 0, l = len; |
| 241 |
while (l > 0) { |
| 242 |
xts.encrypt(block++, BLOCK_SIZE, crypt.array(), crypt.arrayOffset() + x); |
| 243 |
x += BLOCK_SIZE; |
| 244 |
l -= BLOCK_SIZE; |
| 245 |
} |
| 246 |
writeFully(base, position + HEADER_LENGTH, crypt); |
| 247 |
} |
| 248 |
|
| 249 |
private static void writeFully(FileChannel file, long pos, ByteBuffer src) throws IOException { |
| 250 |
do { |
| 251 |
pos += file.write(src, pos); |
| 252 |
} while (src.remaining() > 0); |
| 253 |
} |
| 254 |
|
| 255 |
@Override |
| 256 |
public long size() throws IOException { |
| 257 |
init(); |
| 258 |
return size; |
| 259 |
} |
| 260 |
|
| 261 |
@Override |
| 262 |
protected void implTruncate(long newSize) throws IOException { |
| 263 |
init(); |
| 264 |
if (newSize > size) { |
| 265 |
return; |
| 266 |
} |
| 267 |
if (newSize < 0) { |
| 268 |
throw new IllegalArgumentException("newSize: " + newSize); |
| 269 |
} |
| 270 |
int offset = (int) (newSize & BLOCK_SIZE_MASK); |
| 271 |
if (offset > 0) { |
| 272 |
base.truncate(newSize + HEADER_LENGTH + BLOCK_SIZE); |
| 273 |
} else { |
| 274 |
base.truncate(newSize + HEADER_LENGTH); |
| 275 |
} |
| 276 |
this.size = newSize; |
| 277 |
} |
| 278 |
|
| 279 |
@Override |
| 280 |
public void force(boolean metaData) throws IOException { |
| 281 |
base.force(metaData); |
| 282 |
} |
| 283 |
|
| 284 |
@Override |
| 285 |
public FileLock tryLock(long position, long size, boolean shared) throws IOException { |
| 286 |
return base.tryLock(position, size, shared); |
| 287 |
} |
| 288 |
|
| 289 |
@Override |
| 290 |
public String toString() { |
| 291 |
return name; |
| 292 |
} |
| 293 |
|
| 294 |
} |