Repository navigation
Expand file tree
/
Copy pathtest-ios-encrypted-graph-setup.py
More file actions
132 lines (121 loc) · 5.78 KB
/
Copy pathtest-ios-encrypted-graph-setup.py
File metadata and controls
132 lines (121 loc) · 5.78 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
#!/usr/bin/env python3
"""Run encrypted graph creation through native crypto and an isolated sync server."""
import base64
import http.server
import json
from pathlib import Path
import subprocess
import tempfile
import threading
ROOT = Path(__file__).resolve().parents[1]
class EncryptionServer(http.server.BaseHTTPRequestHandler):
user_keys = None
graphs = []
graph_keys = []
key_uploads = 0
graph_creates = 0
def log_message(self, *_args):
pass
def respond(self, status, value):
body = json.dumps(value).encode()
self.send_response(status)
self.send_header("Content-Length", str(len(body)))
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(body)
def do_GET(self):
token = self.headers.get("Authorization")
if self.path == "/e2ee/user-keys":
if token == "Bearer fixture-denied":
self.respond(403, {})
return
if token == "Bearer fixture-malformed":
self.respond(200, {"public-key": "partial-key"})
return
self.respond(200, self.user_keys or {})
elif self.path == "/graphs":
self.respond(200, {"graphs": self.graphs})
else:
self.respond(404, {})
def do_POST(self):
token = self.headers.get("Authorization")
raw = self.rfile.read(int(self.headers["Content-Length"]))
if "/snapshot/upload?" in self.path:
self.respond(200, {})
return
body = json.loads(raw)
state = type(self)
if self.path == "/e2ee/user-keys":
if token == "Bearer fixture-upload-failure":
self.respond(503, {})
return
assert token == "Bearer fixture-token"
assert state.user_keys is None, "Existing account keys must never be overwritten"
state.user_keys = body
state.key_uploads += 1
self.respond(200, body)
elif self.path == "/graphs":
state.graph_creates += 1
if body["graph-e2ee?"] and state.user_keys is None:
self.respond(400, {"error": "missing user rsa key pair"})
return
graph = {"graph-id": f"graph-{len(state.graphs)}", "graph-name": body["graph-name"],
"graph-e2ee?": body["graph-e2ee?"], "graph-ready-for-use?": True}
state.graphs.append(graph)
self.respond(201, graph)
elif self.path.startswith("/e2ee/graphs/"):
state.graph_keys.append(body["encrypted-aes-key"])
self.respond(200, body)
else:
self.respond(404, {})
def main():
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), EncryptionServer)
threading.Thread(target=server.serve_forever, daemon=True).start()
try:
with tempfile.TemporaryDirectory(prefix="logseq-encrypted-graph-") as directory:
source = Path(directory) / "check.swift"
source.write_text((ROOT / "apple/Tests/UI/encrypted-graph-setup.swift").read_text().replace(
"__SELF_HOSTED_BASE_URL__", f"http://127.0.0.1:{server.server_port}"))
subprocess.run(["python3", str(ROOT / "apple/Tests/UI/run-simulator-runtime-check.py"),
str(source), "EncryptedGraphSetupCheck"], check=True, cwd=ROOT)
assert EncryptionServer.key_uploads == 1
assert EncryptionServer.graph_creates == 3, "Password setup must precede graph creation"
keys = EncryptionServer.user_keys
public = json.loads(keys["public-key"])
private = json.loads(keys["encrypted-private-key"])
assert private[0] == "20251210"
def binary(value):
if isinstance(value, list):
assert value[0] == "~#'" and len(value) == 2
value = value[1]
assert value.startswith("~b"), value
return base64.b64decode(value[2:]).hex()
payload = {"public": binary(public), "salt": binary(private[1]), "iv": binary(private[2]),
"private": binary(private[3]),
"graphs": [binary(json.loads(key)) for key in EncryptionServer.graph_keys]}
# Import and decrypt with WebCrypto, independently of the native codec.
check = """
import {webcrypto} from 'node:crypto';
let input = ''; for await (const chunk of process.stdin) input += chunk;
const p = JSON.parse(input), subtle = webcrypto.subtle;
const bytes = h => Buffer.from(h, 'hex');
const password = await subtle.importKey('raw', Buffer.from('correct horse battery staple'), 'PBKDF2', false, ['deriveKey']);
const derived = await subtle.deriveKey({name:'PBKDF2', salt:bytes(p.salt), iterations:600000, hash:'SHA-256'}, password, {name:'AES-GCM', length:256}, false, ['decrypt']);
const raw = await subtle.decrypt({name:'AES-GCM', iv:bytes(p.iv)}, derived, bytes(p.private));
const key = await subtle.importKey('pkcs8', raw, {name:'RSA-OAEP', hash:'SHA-256'}, false, ['decrypt']);
const pub = await subtle.importKey('spki', bytes(p.public), {name:'RSA-OAEP', hash:'SHA-256'}, false, ['encrypt']);
if (key.algorithm.modulusLength !== 4096) throw Error('Expected RSA-4096');
for (const encrypted of p.graphs) {
const aes = await subtle.decrypt({name:'RSA-OAEP'}, key, bytes(encrypted));
if (aes.byteLength !== 32) throw Error('Expected AES-256 graph key');
const wrapped = await subtle.encrypt({name:'RSA-OAEP'}, pub, aes);
await subtle.decrypt({name:'RSA-OAEP'}, key, wrapped);
}
console.log('PASS: WebCrypto imports the native account pair and decrypts both graph keys');
"""
subprocess.run(["node", "--input-type=module", "-e", check], input=json.dumps(payload), text=True, check=True)
finally:
server.shutdown()
server.server_close()
if __name__ == "__main__":
main()