|
|
@@ -44,10 +44,22 @@ set, then re-encrypt existing records old -> new:
|
|
|
|
for record in caller_iter():
|
|
|
|
for record in caller_iter():
|
|
|
|
caller_update(new_crypto.reencrypt(crypto, record))
|
|
|
|
caller_update(new_crypto.reencrypt(crypto, record))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reencrypt/is_encrypted_record/decrypt_record all detect a bare {secure, iv, data}
|
|
|
|
|
|
|
|
blob used AS the whole record (the file-storage pattern), not just blobs nested
|
|
|
|
|
|
|
|
under a key. reencrypt fails loud (raises) rather than silently leaving a field
|
|
|
|
|
|
|
|
under the old key — including when a blob sits deeper than `traversal_level`;
|
|
|
|
|
|
|
|
is_encrypted_record falls back to an unbounded-depth scan past traversal_level so
|
|
|
|
|
|
|
|
it reliably catches leftovers as a post-rotation audit.
|
|
|
|
|
|
|
|
|
|
|
|
config-free: the host supplies the DEK and RSA key paths; this lib never imports
|
|
|
|
config-free: the host supplies the DEK and RSA key paths; this lib never imports
|
|
|
|
config, configures logging, or touches a database. storage-agnostic — the
|
|
|
|
config, configures logging, or touches a database. storage-agnostic — the
|
|
|
|
encrypted blob is a plain dict; store it in mongo, a sql json column, or a file.
|
|
|
|
encrypted blob is a plain dict; store it in mongo, a sql json column, or a file.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
encrypt_data/decrypt_data round-trip a dict only when every key is a str —
|
|
|
|
|
|
|
|
json (the wire format under the hood) has no other key type, so encrypt_data
|
|
|
|
|
|
|
|
raises TypeError on a non-str key (e.g. an int-keyed dict of discord snowflakes)
|
|
|
|
|
|
|
|
rather than silently stringifying it and losing the original key on decrypt.
|
|
|
|
|
|
|
|
|
|
|
|
naming: EnvelopeCrypto is canonical. PCICrypto / DocumentCrypto / RecordCrypto are
|
|
|
|
naming: EnvelopeCrypto is canonical. PCICrypto / DocumentCrypto / RecordCrypto are
|
|
|
|
aliases (PCICrypto is a deprecated legacy alias). the document/record/dict
|
|
|
|
aliases (PCICrypto is a deprecated legacy alias). the document/record/dict
|
|
|
|
function variants are the same functions — use whichever fits your storage.
|
|
|
|
function variants are the same functions — use whichever fits your storage.
|
|
|
@@ -113,6 +125,24 @@ def _fingerprint_of(public_key) -> str:
|
|
|
|
return base64.b64encode(digest.finalize()).decode()
|
|
|
|
return base64.b64encode(digest.finalize()).decode()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _is_blob(value: Any) -> bool:
|
|
|
|
|
|
|
|
"""return whether value has the {secure, iv, data} encrypted-blob shape"""
|
|
|
|
|
|
|
|
return isinstance(value, dict) and value.get("secure") is True and "iv" in value and "data" in value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _has_encrypted_field(record: Any) -> bool:
|
|
|
|
|
|
|
|
"""unbounded-depth scan: does record (or anything nested under it) contain a blob
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
used to detect a blob left behind by a depth-limited traversal — no traversal_level
|
|
|
|
|
|
|
|
cutoff here, since the whole point is to catch what a bounded pass would miss.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not isinstance(record, dict):
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
if _is_blob(record):
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
return any(_has_encrypted_field(value) for value in record.values())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _load_public_key(key_data: bytes):
|
|
|
|
def _load_public_key(key_data: bytes):
|
|
|
|
"""load a PEM or OpenSSH public key, normalizing non-key input to ValueError
|
|
|
|
"""load a PEM or OpenSSH public key, normalizing non-key input to ValueError
|
|
|
|
|
|
|
|
|
|
|
@@ -163,7 +193,11 @@ class EnvelopeCrypto:
|
|
|
|
round-trips sample data through this instance's DEK, then wraps the DEK
|
|
|
|
round-trips sample data through this instance's DEK, then wraps the DEK
|
|
|
|
with the public key and unwraps with the private key, confirming they
|
|
|
|
with the public key and unwraps with the private key, confirming they
|
|
|
|
match. run after bootstrap (or anytime as a health check) to catch a bad
|
|
|
|
match. run after bootstrap (or anytime as a health check) to catch a bad
|
|
|
|
keypair or wrong key path before relying on it. returns True on success.
|
|
|
|
keypair or wrong key path before relying on it. is_file (default True) is
|
|
|
|
|
|
|
|
threaded through to both the public and private key loads, so
|
|
|
|
|
|
|
|
is_file=False correctly treats both rsa_public_key and rsa_private_key as
|
|
|
|
|
|
|
|
in-memory PEM/OpenSSH data rather than opening either as a file path.
|
|
|
|
|
|
|
|
returns True on success.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
if not self.master_key:
|
|
|
|
if not self.master_key:
|
|
|
|
raise ValueError("self_test: not initialized with a key")
|
|
|
|
raise ValueError("self_test: not initialized with a key")
|
|
|
@@ -174,7 +208,7 @@ class EnvelopeCrypto:
|
|
|
|
|
|
|
|
|
|
|
|
_, wrapped = self.encrypt_aes_key_with_rsa(self.master_key, rsa_public_key, is_file=is_file)
|
|
|
|
_, wrapped = self.encrypt_aes_key_with_rsa(self.master_key, rsa_public_key, is_file=is_file)
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
recovered = self.decrypt_aes_key_with_rsa(wrapped, rsa_private_key, password=password)
|
|
|
|
recovered = self.decrypt_aes_key_with_rsa(wrapped, rsa_private_key, is_file=is_file, password=password)
|
|
|
|
except Exception as error:
|
|
|
|
except Exception as error:
|
|
|
|
raise RuntimeError(
|
|
|
|
raise RuntimeError(
|
|
|
|
"self_test: key unwrap failed (public/private keys do not pair, "
|
|
|
|
"self_test: key unwrap failed (public/private keys do not pair, "
|
|
|
@@ -268,12 +302,21 @@ class EnvelopeCrypto:
|
|
|
|
return fingerprint, wrapped_b64
|
|
|
|
return fingerprint, wrapped_b64
|
|
|
|
|
|
|
|
|
|
|
|
def decrypt_aes_key_with_rsa(
|
|
|
|
def decrypt_aes_key_with_rsa(
|
|
|
|
self, encrypted_key_base64: str, rsa_private_key_path: str,
|
|
|
|
self, encrypted_key_base64: str, rsa_private_key: str,
|
|
|
|
password: Optional[str] = None,
|
|
|
|
is_file: bool = True, password: Optional[str] = None,
|
|
|
|
) -> bytes:
|
|
|
|
) -> bytes:
|
|
|
|
"""unwrap an AES key with an RSA private key"""
|
|
|
|
"""unwrap an AES key with an RSA private key
|
|
|
|
with open(rsa_private_key_path, "rb") as key_file:
|
|
|
|
|
|
|
|
key_data = key_file.read()
|
|
|
|
is_file defaults to True (rsa_private_key is a path), matching
|
|
|
|
|
|
|
|
encrypt_aes_key_with_rsa / get_rsa_key_fingerprint; pass is_file=False to
|
|
|
|
|
|
|
|
supply the PEM/OpenSSH key data directly (e.g. an in-memory or vault-sourced
|
|
|
|
|
|
|
|
key) instead of a file path.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
if is_file:
|
|
|
|
|
|
|
|
with open(rsa_private_key, "rb") as key_file:
|
|
|
|
|
|
|
|
key_data = key_file.read()
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
key_data = rsa_private_key.encode() if isinstance(rsa_private_key, str) else rsa_private_key
|
|
|
|
pw = password.encode() if password else None
|
|
|
|
pw = password.encode() if password else None
|
|
|
|
private_key = _load_private_key(key_data, pw)
|
|
|
|
private_key = _load_private_key(key_data, pw)
|
|
|
|
|
|
|
|
|
|
|
@@ -318,13 +361,30 @@ class EnvelopeCrypto:
|
|
|
|
return new_key, wrapped
|
|
|
|
return new_key, wrapped
|
|
|
|
|
|
|
|
|
|
|
|
def encrypt_data(self, data: Union[Dict[str, Any], str]) -> Dict[str, str]:
|
|
|
|
def encrypt_data(self, data: Union[Dict[str, Any], str]) -> Dict[str, str]:
|
|
|
|
"""encrypt a dict or string under the data key with a unique IV"""
|
|
|
|
"""encrypt a dict or string under the data key with a unique IV
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dict keys must be str: json.dumps silently stringifies int/float/bool/None
|
|
|
|
|
|
|
|
keys (e.g. a snowflake-int-keyed dict), which would make decrypt_data return
|
|
|
|
|
|
|
|
a dict that no longer matches the original by key type or identity — a
|
|
|
|
|
|
|
|
non-str key is rejected here instead, so the failure is loud at encrypt
|
|
|
|
|
|
|
|
time rather than a silent lookup miss after decrypt.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if not self.master_key:
|
|
|
|
if not self.master_key:
|
|
|
|
raise ValueError("not initialized with data key")
|
|
|
|
raise ValueError("not initialized with data key")
|
|
|
|
if not isinstance(data, (dict, str)):
|
|
|
|
if not isinstance(data, (dict, str)):
|
|
|
|
# a non-dict/non-str would .encode()-fail with an opaque AttributeError below;
|
|
|
|
# a non-dict/non-str would .encode()-fail with an opaque AttributeError below;
|
|
|
|
# reject it clearly (decrypt_data only round-trips dict or str anyway)
|
|
|
|
# reject it clearly (decrypt_data only round-trips dict or str anyway)
|
|
|
|
raise TypeError(f"encrypt_data expects a dict or str, got {type(data).__name__}")
|
|
|
|
raise TypeError(f"encrypt_data expects a dict or str, got {type(data).__name__}")
|
|
|
|
|
|
|
|
if isinstance(data, dict):
|
|
|
|
|
|
|
|
non_str_keys = [key for key in data if not isinstance(key, str)]
|
|
|
|
|
|
|
|
if non_str_keys:
|
|
|
|
|
|
|
|
# json.dumps would silently stringify these (int/float/bool/None keys),
|
|
|
|
|
|
|
|
# corrupting the round-trip (decrypt_data would return a dict keyed by
|
|
|
|
|
|
|
|
# the stringified value) — fail loud instead of coercing
|
|
|
|
|
|
|
|
raise TypeError(
|
|
|
|
|
|
|
|
"encrypt_data requires str dict keys, got non-str key(s): "
|
|
|
|
|
|
|
|
f"{[type(key).__name__ for key in non_str_keys]}"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
data_str = json.dumps(data) if isinstance(data, dict) else data
|
|
|
|
data_str = json.dumps(data) if isinstance(data, dict) else data
|
|
|
|
iv = os.urandom(12)
|
|
|
|
iv = os.urandom(12)
|
|
|
@@ -348,6 +408,8 @@ class EnvelopeCrypto:
|
|
|
|
without a type marker it is indistinguishable from a stored dict — don't store a
|
|
|
|
without a type marker it is indistinguishable from a stored dict — don't store a
|
|
|
|
bare string that is a json object if you need it back as a string. existing stored
|
|
|
|
bare string that is a json object if you need it back as a string. existing stored
|
|
|
|
blobs are unaffected — a dict was stored as a json object and still parses to a dict.
|
|
|
|
blobs are unaffected — a dict was stored as a json object and still parses to a dict.
|
|
|
|
|
|
|
|
dict KEYS round-trip faithfully only because encrypt_data now requires str keys —
|
|
|
|
|
|
|
|
json has no other key type, so this is the only shape decrypt_data ever sees.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
if not self.master_key:
|
|
|
|
if not self.master_key:
|
|
|
|
raise ValueError("not initialized with data key")
|
|
|
|
raise ValueError("not initialized with data key")
|
|
|
@@ -373,25 +435,42 @@ class EnvelopeCrypto:
|
|
|
|
key. only {secure, iv, data} fields are touched; plaintext fields are left
|
|
|
|
key. only {secure, iv, data} fields are touched; plaintext fields are left
|
|
|
|
as-is. returns a new dict; the input is not mutated. used during rotation.
|
|
|
|
as-is. returns a new dict; the input is not mutated. used during rotation.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if `record` itself is a {secure, iv, data} blob (the file-storage pattern, where
|
|
|
|
|
|
|
|
the blob IS the whole document) it is re-encrypted directly and the result is
|
|
|
|
|
|
|
|
returned in place of `record` — not nested under a key.
|
|
|
|
|
|
|
|
|
|
|
|
unlike decrypt_record (which logs a failed field and leaves it encrypted), a
|
|
|
|
unlike decrypt_record (which logs a failed field and leaves it encrypted), a
|
|
|
|
per-field decrypt failure here RAISES — rotation must fail loud, since silently
|
|
|
|
per-field decrypt failure here RAISES — rotation must fail loud, since silently
|
|
|
|
keeping a field under the old key would lose it once the old key is retired.
|
|
|
|
keeping a field under the old key would lose it once the old key is retired. for
|
|
|
|
|
|
|
|
the same reason, a blob nested DEEPER than `traversal_level` also RAISES instead
|
|
|
|
|
|
|
|
of being silently left under the old key: the caller either needs a higher
|
|
|
|
|
|
|
|
`traversal_level` or must flatten the record before rotation.
|
|
|
|
|
|
|
|
|
|
|
|
traversal recurses into nested DICTS only (up to `traversal_level`); a blob nested
|
|
|
|
traversal recurses into nested DICTS only; a blob nested inside a LIST is NOT
|
|
|
|
inside a LIST is NOT re-encrypted. records in this scheme key blobs by field name,
|
|
|
|
re-encrypted and is NOT covered by the depth-limit raise above. records in this
|
|
|
|
not inside arrays, so this doesn't arise in practice — but if you store
|
|
|
|
scheme key blobs by field name, not inside arrays, so this doesn't arise in
|
|
|
|
list-nested blobs, flatten them to dict fields before rotation or they'll be left
|
|
|
|
practice — but if you store list-nested blobs, flatten them to dict fields before
|
|
|
|
under the old key.
|
|
|
|
rotation or they'll be silently left under the old key.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
if not self.master_key:
|
|
|
|
if not self.master_key:
|
|
|
|
raise ValueError("destination not initialized with data key")
|
|
|
|
raise ValueError("destination not initialized with data key")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if _is_blob(record):
|
|
|
|
|
|
|
|
return self.encrypt_data(source_crypto.decrypt_data(record))
|
|
|
|
|
|
|
|
|
|
|
|
result = copy.deepcopy(record)
|
|
|
|
result = copy.deepcopy(record)
|
|
|
|
for key, value in record.items():
|
|
|
|
for key, value in record.items():
|
|
|
|
if isinstance(value, dict) and value.get("secure") is True and "iv" in value and "data" in value:
|
|
|
|
if _is_blob(value):
|
|
|
|
result[key] = self.encrypt_data(source_crypto.decrypt_data(value))
|
|
|
|
result[key] = self.encrypt_data(source_crypto.decrypt_data(value))
|
|
|
|
elif traversal_level > 0 and isinstance(value, dict):
|
|
|
|
elif isinstance(value, dict):
|
|
|
|
result[key] = self.reencrypt(source_crypto, value, traversal_level - 1)
|
|
|
|
if traversal_level > 0:
|
|
|
|
|
|
|
|
result[key] = self.reencrypt(source_crypto, value, traversal_level - 1)
|
|
|
|
|
|
|
|
elif _has_encrypted_field(value):
|
|
|
|
|
|
|
|
raise ValueError(
|
|
|
|
|
|
|
|
f"reencrypt: field {key!r} contains an encrypted blob nested deeper "
|
|
|
|
|
|
|
|
"than traversal_level; increase traversal_level or flatten the record "
|
|
|
|
|
|
|
|
"before rotation — leaving it would strand the field under the old key"
|
|
|
|
|
|
|
|
)
|
|
|
|
return result
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -404,28 +483,46 @@ PCICrypto = EnvelopeCrypto # deprecated legacy alias; remove after all systems
|
|
|
|
def is_encrypted_record(record, traversal_level: int = 2) -> bool:
|
|
|
|
def is_encrypted_record(record, traversal_level: int = 2) -> bool:
|
|
|
|
"""return whether a record has any encrypted ({secure, iv, data}) fields
|
|
|
|
"""return whether a record has any encrypted ({secure, iv, data}) fields
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
checks `record` itself (the file-storage pattern stores the blob AS the whole
|
|
|
|
|
|
|
|
document, not nested under a key) as well as fields up to `traversal_level` deep.
|
|
|
|
|
|
|
|
beyond that bounded pass, this ALSO does an unbounded-depth scan before giving up —
|
|
|
|
|
|
|
|
so a blob left behind by a shallower decrypt_record/reencrypt call (nested deeper
|
|
|
|
|
|
|
|
than their traversal_level) is still reported as encrypted. this makes the function
|
|
|
|
|
|
|
|
safe to use as a leftover-detecting post-rotation audit: it never returns False for
|
|
|
|
|
|
|
|
a record that still contains a blob, at any depth.
|
|
|
|
|
|
|
|
|
|
|
|
aliases: is_encrypted_document, is_encrypted_dict — same function
|
|
|
|
aliases: is_encrypted_document, is_encrypted_dict — same function
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
if not isinstance(record, dict):
|
|
|
|
if not isinstance(record, dict):
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if _is_blob(record):
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
for value in record.values():
|
|
|
|
for value in record.values():
|
|
|
|
if isinstance(value, dict) and value.get("secure") is True:
|
|
|
|
if _is_blob(value):
|
|
|
|
if "iv" in value and "data" in value:
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if traversal_level > 0:
|
|
|
|
if traversal_level > 0:
|
|
|
|
for value in record.values():
|
|
|
|
for value in record.values():
|
|
|
|
if isinstance(value, dict) and is_encrypted_record(value, traversal_level - 1):
|
|
|
|
if isinstance(value, dict) and is_encrypted_record(value, traversal_level - 1):
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return any(_has_encrypted_field(value) for value in record.values())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def decrypt_record(crypto: EnvelopeCrypto, record, traversal_level: int = 2) -> dict:
|
|
|
|
def decrypt_record(crypto: EnvelopeCrypto, record, traversal_level: int = 2) -> Union[dict, Any]:
|
|
|
|
"""decrypt a record's encrypted fields into a new dict (up to traversal_level deep)
|
|
|
|
"""decrypt a record's encrypted fields into a new dict (up to traversal_level deep)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if `record` itself is a {secure, iv, data} blob (the file-storage pattern, where the
|
|
|
|
|
|
|
|
blob IS the whole document) it is decrypted directly and the decrypted value
|
|
|
|
|
|
|
|
(dict or string — see decrypt_data) is returned in place of `record`.
|
|
|
|
|
|
|
|
|
|
|
|
failures on a single field are logged and that field is left encrypted, so a
|
|
|
|
failures on a single field are logged and that field is left encrypted, so a
|
|
|
|
partial failure is visible (the {secure,...} blob remains) rather than silent.
|
|
|
|
partial failure is visible (the {secure,...} blob remains) rather than silent. a
|
|
|
|
|
|
|
|
failure decrypting `record` itself (the self-blob case above) is likewise logged
|
|
|
|
|
|
|
|
and the still-encrypted blob is returned unchanged.
|
|
|
|
|
|
|
|
|
|
|
|
aliases: decrypt_document, decrypt_dict — same function
|
|
|
|
aliases: decrypt_document, decrypt_dict — same function
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@@ -434,9 +531,16 @@ def decrypt_record(crypto: EnvelopeCrypto, record, traversal_level: int = 2) ->
|
|
|
|
if not isinstance(record, dict):
|
|
|
|
if not isinstance(record, dict):
|
|
|
|
return record
|
|
|
|
return record
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if _is_blob(record):
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
return crypto.decrypt_data(record)
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
_log.exception("failed to decrypt record")
|
|
|
|
|
|
|
|
return copy.deepcopy(record)
|
|
|
|
|
|
|
|
|
|
|
|
result = copy.deepcopy(record)
|
|
|
|
result = copy.deepcopy(record)
|
|
|
|
for key, value in record.items():
|
|
|
|
for key, value in record.items():
|
|
|
|
if isinstance(value, dict) and value.get("secure") is True and "iv" in value and "data" in value:
|
|
|
|
if _is_blob(value):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
result[key] = crypto.decrypt_data(value)
|
|
|
|
result[key] = crypto.decrypt_data(value)
|
|
|
|
except Exception:
|
|
|
|
except Exception:
|
|
|
|