File size: 3,234 Bytes
8133df9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f7b5d2
8133df9
 
 
 
 
 
 
 
 
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
"""Whole-file tenant envelopes for mirror blobs and opt-in seeds (C8, R26, R36)."""
import hashlib
import json

from core import tenant_keys

VERSION = 1
MAGIC = b"AIOS-BLOB\x00"
HEADER = MAGIC + bytes([VERSION])


class BlobEnvelopeError(tenant_keys.TenantKeyLocked):
    """A sealed blob is malformed, locked or fails authentication."""


def _aad(tenant, path):
    if (not isinstance(path, str) or not path or path.startswith("/")
            or "\\" in path or "\x00" in path or any(p in ("", ".", "..") for p in path.split("/"))):
        raise ValueError("A blob needs its full relative storage path.")
    return json.dumps(["aios/blob", VERSION, tenant_keys.context(tenant)["tenant"], path],
                      separators=(",", ":")).encode("utf-8")


def is_sealed(data):
    """Recognise the envelope namespace, including an unsupported version or truncated header."""
    return bytes(data).startswith(MAGIC.rstrip(b"\x00"))


def content_metadata(plaintext):
    """Hash plaintext before sealing so random nonces cannot change content identity."""
    data = bytes(plaintext)
    return {"bytes": len(data), "sha256": hashlib.sha256(data).hexdigest()}


def _legacy_plaintext(data):
    if len(data) >= 8 and data.startswith(b"PAR1") and data.endswith(b"PAR1"):
        return True
    if len(data) >= 16 and data[8:12] == b"DUCK":
        return True
    if data.lstrip().startswith((b"{", b"[")):
        try:
            json.loads(data)
            return True
        except (ValueError, UnicodeError):
            pass
    return False


def seal(tenant, path, plaintext, *, required=False):
    """Seal under the tenant's blob subkey, or keep legacy bytes when no data key exists."""
    aad = _aad(tenant, path)
    data = bytes(plaintext)
    key = tenant_keys.subkey(tenant, "blob")
    if key is None:
        if required:
            raise BlobEnvelopeError("Sealing this blob requires a tenant data key.")
        return data
    return HEADER + tenant_keys.aead_seal(key, data, aad)


def open(tenant, path, data):
    """Open an envelope or legacy Parquet, JSON or DuckDB without writing storage or creating keys."""
    aad = _aad(tenant, path)
    data = bytes(data)
    if not is_sealed(data):
        if _legacy_plaintext(data):
            return data
        raise BlobEnvelopeError("The blob is neither an envelope nor a supported legacy file.")
    if not data.startswith(HEADER) or len(data) < len(HEADER) + 28:
        raise BlobEnvelopeError("The blob envelope is truncated or has an unsupported version.")
    key = tenant_keys.subkey(tenant, "blob")
    if key is None:
        raise BlobEnvelopeError("Opening this blob requires a tenant data key.")
    try:
        return tenant_keys.aead_open(key, memoryview(data)[len(HEADER):], aad)
    except Exception:
        raise BlobEnvelopeError("The blob envelope did not authenticate.") from None


def seal_seed(tenant, path, plaintext, *, enabled=False):
    """Seal a seed only when the caller explicitly enables it and a tenant key is available."""
    if type(enabled) is not bool:
        raise ValueError("Seed sealing needs a boolean switch.")
    return seal(tenant, path, plaintext, required=True) if enabled else bytes(plaintext)