-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharchive_envelope.py
More file actions
306 lines (278 loc) · 11.3 KB
/
Copy patharchive_envelope.py
File metadata and controls
306 lines (278 loc) · 11.3 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python3
"""Create one age archive and provider-neutral wrapped-identity envelope.
This trusted preparation tool never unwraps. It sends the fresh age identity
only to the configured adapter process over stdin, ignores adapter stderr, and
writes only ciphertext plus the strict public envelope.
"""
from __future__ import annotations
import argparse
import base64
import hashlib
import json
import os
import pathlib
import stat
import subprocess
import sys
import tempfile
from typing import Any
try:
from .key_capability_contract import (
ADAPTER,
AGE_RECIPIENT,
BASE64,
ContractError,
archive_key_id,
canonical_archive_path,
envelope_binding_context,
validate_age_identity_bytes,
validate_envelope,
)
except ImportError:
from key_capability_contract import ( # type: ignore[no-redef]
ADAPTER,
AGE_RECIPIENT,
BASE64,
ContractError,
archive_key_id,
canonical_archive_path,
envelope_binding_context,
validate_age_identity_bytes,
validate_envelope,
)
MAX_ADAPTER_STDOUT_BYTES = 32768
ADAPTER_RESPONSE_FIELDS = {"schema_version", "adapter", "wrapped_identity"}
class EnvelopeError(ValueError):
"""Envelope preparation failed before any safe output was committed."""
def _sha256(path: pathlib.Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as source:
for chunk in iter(lambda: source.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def _tool_environment() -> dict[str, str]:
"""Keep cloud/provider credentials out of age and age-keygen."""
environment = {"PATH": os.environ.get("PATH", "")}
for name in ("LANG", "LC_ALL", "TZ"):
if name in os.environ:
environment[name] = os.environ[name]
return environment
def _run_age(command: list[str], *, label: str) -> subprocess.CompletedProcess[str]:
try:
result = subprocess.run(
command,
check=False,
capture_output=True,
text=True,
timeout=120,
env=_tool_environment(),
)
except (OSError, subprocess.TimeoutExpired, UnicodeError) as error:
raise EnvelopeError(f"{label} could not run") from error
if result.returncode != 0:
raise EnvelopeError(f"{label} failed with exit code {result.returncode}")
return result
def _read_identity(identity_path: pathlib.Path) -> bytes:
try:
mode = stat.S_IMODE(identity_path.stat().st_mode)
identity = identity_path.read_bytes()
except OSError as error:
raise EnvelopeError("age-keygen did not create a readable identity") from error
if mode & 0o077:
raise EnvelopeError("age identity permissions expose group or other access")
try:
return validate_age_identity_bytes(identity)
except ContractError as error:
raise EnvelopeError(str(error)) from error
def _adapter_wrap(
*,
adapter_executable: pathlib.Path,
adapter_name: str,
identity: bytes,
context: dict[str, str],
) -> str:
if ADAPTER.fullmatch(adapter_name) is None:
raise EnvelopeError("adapter name is not canonical")
request = {
"schema_version": 1,
"operation": "wrap",
"adapter": adapter_name,
"context": context,
"plaintext_identity_base64": base64.b64encode(identity).decode("ascii"),
}
try:
result = subprocess.run(
[str(adapter_executable), "wrap"],
input=json.dumps(request, separators=(",", ":"), sort_keys=True),
check=False,
capture_output=True,
text=True,
timeout=30,
)
except (OSError, subprocess.TimeoutExpired, UnicodeError) as error:
raise EnvelopeError("root-key adapter could not run") from error
if result.returncode != 0:
# Adapter stderr is intentionally not repeated: a faulty adapter might
# include the plaintext identity in its diagnostics.
raise EnvelopeError(f"root-key adapter failed with exit code {result.returncode}")
if len(result.stdout.encode("utf-8")) > MAX_ADAPTER_STDOUT_BYTES:
raise EnvelopeError("root-key adapter response exceeds the size limit")
try:
response: Any = json.loads(result.stdout)
except (json.JSONDecodeError, UnicodeError) as error:
raise EnvelopeError("root-key adapter did not return one JSON object") from error
if not isinstance(response, dict) or set(response) != ADAPTER_RESPONSE_FIELDS:
raise EnvelopeError("root-key adapter response fields are not canonical")
if response["schema_version"] != 1 or isinstance(response["schema_version"], bool):
raise EnvelopeError("root-key adapter response schema_version must be integer 1")
if response["adapter"] != adapter_name:
raise EnvelopeError("root-key adapter response names a different adapter")
wrapped = response["wrapped_identity"]
if not isinstance(wrapped, str) or BASE64.fullmatch(wrapped) is None:
raise EnvelopeError("root-key adapter response is not canonical base64")
return wrapped
def create_archive_envelope(
*,
source_tar: pathlib.Path,
submission_id: str,
output_dir: pathlib.Path,
adapter_executable: pathlib.Path,
adapter_name: str,
age_executable: str = "age",
age_keygen_executable: str = "age-keygen",
post_quantum: bool = True,
) -> tuple[pathlib.Path, pathlib.Path]:
if not source_tar.is_file() or source_tar.is_symlink():
raise EnvelopeError("source tar must be one regular file")
# Validate every caller-controlled identifier before creating secret
# material or invoking either external tool.
canonical_archive_path(submission_id)
if ADAPTER.fullmatch(adapter_name) is None:
raise EnvelopeError("adapter name is not canonical")
if not adapter_executable.is_file() or adapter_executable.is_symlink():
raise EnvelopeError("adapter executable must be one regular file")
if output_dir.exists() or output_dir.is_symlink():
raise EnvelopeError("output directory must not already exist")
output_dir.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
ciphertext_output = output_dir / "source.tar.gz.age"
envelope_output = output_dir / "archive-key-envelope.json"
with tempfile.TemporaryDirectory(prefix="lean-eval-secret-") as raw_secret:
secret_dir = pathlib.Path(raw_secret)
identity_path = secret_dir / "identity.age"
keygen = [age_keygen_executable]
if post_quantum:
keygen.append("-pq")
keygen.extend(["--output", str(identity_path)])
_run_age(keygen, label="age-keygen")
identity = _read_identity(identity_path)
recipient_result = _run_age(
[age_keygen_executable, "-y", str(identity_path)],
label="age recipient derivation",
)
recipient_lines = [
line.strip()
for line in recipient_result.stdout.splitlines()
if line.strip()
]
if len(recipient_lines) != 1 or AGE_RECIPIENT.fullmatch(recipient_lines[0]) is None:
raise EnvelopeError("age-keygen returned a noncanonical recipient")
recipient = recipient_lines[0]
staging_raw = tempfile.mkdtemp(
prefix=f".{output_dir.name}-",
dir=output_dir.parent,
)
staging = pathlib.Path(staging_raw)
try:
ciphertext_temp = staging / ciphertext_output.name
_run_age(
[
age_executable,
"--encrypt",
"--recipient",
recipient,
"--output",
str(ciphertext_temp),
str(source_tar),
],
label="age encryption",
)
try:
with ciphertext_temp.open("rb") as ciphertext:
header = ciphertext.read(32)
except OSError as error:
raise EnvelopeError("age did not create a readable ciphertext") from error
if not header.startswith(b"age-encryption.org/v1\n"):
raise EnvelopeError(
"age output does not have the format-version-1 ciphertext header"
)
archive_digest = _sha256(ciphertext_temp)
data_key_identity = archive_key_id(submission_id, recipient)
context = envelope_binding_context(
submission_id,
archive_digest,
data_key_identity,
recipient,
)
wrapped = _adapter_wrap(
adapter_executable=adapter_executable,
adapter_name=adapter_name,
identity=identity,
context=context,
)
if wrapped == base64.b64encode(identity).decode("ascii"):
raise EnvelopeError("root-key adapter returned the plaintext identity")
envelope = validate_envelope({
"schema_version": 1,
"submission_id": submission_id,
"archive_ciphertext_sha256": archive_digest,
"data_key_id": data_key_identity,
"age_recipient": recipient,
"adapter": adapter_name,
"wrapped_identity": wrapped,
})
envelope_temp = staging / envelope_output.name
envelope_temp.write_text(
json.dumps(envelope, indent=2, sort_keys=True) + "\n",
encoding="utf-8",
)
# Renaming the complete directory makes publication all-or-nothing:
# callers never observe only one of the two required artifacts.
os.replace(staging, output_dir)
finally:
if staging.exists():
for child in staging.iterdir():
child.unlink()
staging.rmdir()
return ciphertext_output, envelope_output
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--source-tar", required=True, type=pathlib.Path)
parser.add_argument("--submission-id", required=True)
parser.add_argument("--output-dir", required=True, type=pathlib.Path)
parser.add_argument("--adapter-executable", required=True, type=pathlib.Path)
parser.add_argument("--adapter-name", required=True)
parser.add_argument("--age-executable", default="age")
parser.add_argument("--age-keygen-executable", default="age-keygen")
parser.add_argument("--classic-age-key", action="store_true")
args = parser.parse_args(argv)
try:
ciphertext, envelope = create_archive_envelope(
source_tar=args.source_tar,
submission_id=args.submission_id,
output_dir=args.output_dir,
adapter_executable=args.adapter_executable,
adapter_name=args.adapter_name,
age_executable=args.age_executable,
age_keygen_executable=args.age_keygen_executable,
post_quantum=not args.classic_age_key,
)
except (ContractError, EnvelopeError, OSError) as error:
print(f"error: {error}", file=sys.stderr)
return 2
print(json.dumps({
"ciphertext": str(ciphertext),
"envelope": str(envelope),
}, sort_keys=True))
return 0
if __name__ == "__main__":
raise SystemExit(main())