-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeepsync_encrypted_backup.py
More file actions
169 lines (137 loc) · 4.63 KB
/
Copy pathkeepsync_encrypted_backup.py
File metadata and controls
169 lines (137 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""AES-GCM encrypted backup for KeepSyncNotes SQLite database."""
import json
import os
import shutil
import sqlite3
import tempfile
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Optional
from keepsync_app_info import APP_NAME, APP_VERSION
BACKUP_MAGIC = b"KSENC1"
SALT_BYTES = 32
NONCE_BYTES = 12
TAG_BYTES = 16
KDF_ITERATIONS = 600_000
@dataclass
class EncryptedBackupResult:
output_path: Path
size_bytes: int
notes_count: int
def _derive_key(password: str, salt: bytes) -> bytes:
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=KDF_ITERATIONS,
)
return kdf.derive(password.encode("utf-8"))
def create_encrypted_backup(
db_path: str,
output_path: Path,
password: str,
) -> EncryptedBackupResult:
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
conn = sqlite3.connect(db_path)
try:
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as tmp:
tmp_path = tmp.name
backup_conn = sqlite3.connect(tmp_path)
conn.backup(backup_conn)
backup_conn.close()
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM notes WHERE trashed = 0")
notes_count = cursor.fetchone()[0]
finally:
conn.close()
try:
plaintext = Path(tmp_path).read_bytes()
finally:
os.unlink(tmp_path)
salt = os.urandom(SALT_BYTES)
key = _derive_key(password, salt)
nonce = os.urandom(NONCE_BYTES)
aesgcm = AESGCM(key)
ciphertext = aesgcm.encrypt(nonce, plaintext, None)
header = json.dumps({
"app": APP_NAME,
"version": APP_VERSION,
"created": datetime.now(timezone.utc).isoformat(),
"kdf": "pbkdf2-sha256",
"iterations": KDF_ITERATIONS,
"cipher": "aes-256-gcm",
}).encode("utf-8")
header_len = len(header).to_bytes(4, "big")
output_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_path, "wb") as f:
f.write(BACKUP_MAGIC)
f.write(header_len)
f.write(header)
f.write(salt)
f.write(nonce)
f.write(ciphertext)
return EncryptedBackupResult(
output_path=output_path,
size_bytes=output_path.stat().st_size,
notes_count=notes_count,
)
def restore_encrypted_backup(
backup_path: Path,
password: str,
restore_db_path: str,
) -> int:
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
data = backup_path.read_bytes()
if not data.startswith(BACKUP_MAGIC):
raise ValueError("Not a valid KeepSync encrypted backup")
offset = len(BACKUP_MAGIC)
header_len = int.from_bytes(data[offset:offset + 4], "big")
offset += 4
offset += header_len
salt = data[offset:offset + SALT_BYTES]
offset += SALT_BYTES
nonce = data[offset:offset + NONCE_BYTES]
offset += NONCE_BYTES
ciphertext = data[offset:]
key = _derive_key(password, salt)
aesgcm = AESGCM(key)
try:
plaintext = aesgcm.decrypt(nonce, ciphertext, None)
except Exception:
raise ValueError("Decryption failed — wrong password or corrupted backup")
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as tmp:
tmp.write(plaintext)
tmp_path = tmp.name
try:
source = sqlite3.connect(tmp_path)
try:
cursor = source.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
tables = {row[0] for row in cursor.fetchall()}
if "notes" not in tables:
raise ValueError("Backup does not contain a valid KeepSync database")
cursor.execute("SELECT COUNT(*) FROM notes WHERE trashed = 0")
notes_count = cursor.fetchone()[0]
finally:
source.close()
dest_path = Path(restore_db_path)
if dest_path.exists():
shutil.copy2(dest_path, dest_path.with_suffix(".db.pre-restore"))
shutil.copy2(tmp_path, dest_path)
finally:
os.unlink(tmp_path)
return notes_count
def read_backup_header(backup_path: Path) -> Optional[dict]:
try:
with open(backup_path, "rb") as f:
magic = f.read(len(BACKUP_MAGIC))
if magic != BACKUP_MAGIC:
return None
header_len = int.from_bytes(f.read(4), "big")
header_bytes = f.read(header_len)
return json.loads(header_bytes)
except Exception:
return None