Repository navigation
feat: verify endorsed receipt rotations #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c79c02e
feat: verify endorsed receipt rotations
luckyPipewrench 15ccb80
chore: refresh patched dependency floors
luckyPipewrench 0c593bf
fix: fail closed on rotation trust boundaries
luckyPipewrench 91a5a46
fix: reject noncanonical rotation endorsements
luckyPipewrench 590d5b1
fix: reject malformed rotation chain records
luckyPipewrench File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| """Old-key-signed receipt rotation endorsement verification.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import hashlib | ||
| import json | ||
| import re | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| from cryptography.exceptions import InvalidSignature, UnsupportedAlgorithm | ||
| from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey | ||
|
|
||
| from ._common import InvalidReceiptError, _is_valid_rfc3339, loads_no_duplicate_keys | ||
|
|
||
| _VERSION = 1 | ||
| _DOMAIN = b"pipelock-rotation-endorsement-v1\x00" | ||
| _SIGNATURE_PREFIX = "ed25519:" | ||
| _MAX_BYTES = 64 << 10 | ||
| _FIELDS = frozenset( | ||
| { | ||
| "version", | ||
| "session_id", | ||
| "prior_signer_key", | ||
| "prior_final_seq", | ||
| "prior_tail_hash", | ||
| "new_signer_key", | ||
| "rotated_at", | ||
| "endorsement", | ||
| } | ||
| ) | ||
| _LOWER_HEX_32 = re.compile(r"^[0-9a-f]{64}$") | ||
| _ED25519_SIGNATURE = re.compile(r"^ed25519:[0-9a-fA-F]{128}$") | ||
| _CANONICAL_UTC = re.compile( | ||
| r"^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}" | ||
| r"(?:\.[0-9]{0,8}[1-9])?Z$" | ||
| ) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class RotationEndorsement: | ||
| """A retiring receipt key's authorization of its successor.""" | ||
|
|
||
| version: int | ||
| session_id: str | ||
| prior_signer_key: str | ||
| prior_final_seq: int | ||
| prior_tail_hash: str | ||
| new_signer_key: str | ||
| rotated_at: str | ||
| endorsement: str | ||
|
|
||
|
|
||
| def _normalize_go_string(value: str) -> str: | ||
| """Replace lone UTF-16 surrogates as Go encoding/json does.""" | ||
| out: list[str] = [] | ||
| index = 0 | ||
| while index < len(value): | ||
| code = ord(value[index]) | ||
| if 0xD800 <= code <= 0xDBFF: | ||
| if index + 1 < len(value) and 0xDC00 <= ord(value[index + 1]) <= 0xDFFF: | ||
| high = code - 0xD800 | ||
| low = ord(value[index + 1]) - 0xDC00 | ||
| out.append(chr(0x10000 + high * 0x400 + low)) | ||
| index += 2 | ||
| continue | ||
| out.append("\ufffd") | ||
| elif 0xDC00 <= code <= 0xDFFF: | ||
| out.append("\ufffd") | ||
| else: | ||
| out.append(value[index]) | ||
| index += 1 | ||
| return "".join(out) | ||
|
|
||
|
|
||
| def _require_string(value: Any, field: str) -> str: | ||
| if not isinstance(value, str) or not value: | ||
| raise InvalidReceiptError(f"rotation endorsement {field} is invalid") | ||
| return _normalize_go_string(value) | ||
|
|
||
|
|
||
| def _require_hex(value: Any, field: str) -> str: | ||
| text = _require_string(value, field) | ||
| if _LOWER_HEX_32.fullmatch(text) is None: | ||
| raise InvalidReceiptError(f"rotation endorsement {field} is invalid") | ||
| return text | ||
|
|
||
|
|
||
| def _decode(value: dict[str, Any]) -> RotationEndorsement: | ||
| if any(not isinstance(name, str) for name in value): | ||
| raise InvalidReceiptError("rotation endorsement field names must be strings") | ||
| unknown = set(value) - _FIELDS | ||
| if unknown: | ||
| raise InvalidReceiptError( | ||
| f"rotation endorsement contains unknown field {sorted(unknown)[0]}" | ||
| ) | ||
| missing = _FIELDS - set(value) | ||
| if missing: | ||
| raise InvalidReceiptError( | ||
| f"rotation endorsement is missing required field {sorted(missing)[0]}" | ||
| ) | ||
| version = value["version"] | ||
| if isinstance(version, bool) or not isinstance(version, int) or version != _VERSION: | ||
| raise InvalidReceiptError(f"unsupported rotation endorsement version {version!r}") | ||
| prior_final_seq = value["prior_final_seq"] | ||
| if ( | ||
| isinstance(prior_final_seq, bool) | ||
| or not isinstance(prior_final_seq, int) | ||
| or not 0 <= prior_final_seq <= 2**64 - 1 | ||
| ): | ||
| raise InvalidReceiptError("rotation endorsement prior_final_seq is invalid") | ||
| session_id = _require_string(value["session_id"], "session_id") | ||
| if not session_id.strip(): | ||
| raise InvalidReceiptError("rotation endorsement session_id is empty") | ||
| prior_key = _require_hex(value["prior_signer_key"], "prior_signer_key") | ||
| new_key = _require_hex(value["new_signer_key"], "new_signer_key") | ||
| if prior_key == new_key: | ||
| raise InvalidReceiptError("rotation endorsement successor key must differ from prior key") | ||
| rotated_at = _require_string(value["rotated_at"], "rotated_at") | ||
| if _CANONICAL_UTC.fullmatch(rotated_at) is None or not _is_valid_rfc3339(rotated_at): | ||
| raise InvalidReceiptError( | ||
| "rotation endorsement rotated_at must be canonical UTC RFC3339Nano" | ||
| ) | ||
| return RotationEndorsement( | ||
| version=_VERSION, | ||
| session_id=session_id, | ||
| prior_signer_key=prior_key, | ||
| prior_final_seq=prior_final_seq, | ||
| prior_tail_hash=_require_hex(value["prior_tail_hash"], "prior_tail_hash"), | ||
| new_signer_key=new_key, | ||
| rotated_at=rotated_at, | ||
| endorsement=_require_string(value["endorsement"], "signature"), | ||
| ) | ||
|
|
||
|
|
||
| def _canonical_bytes(endorsement: RotationEndorsement) -> bytes: | ||
| ordered = { | ||
| "version": endorsement.version, | ||
| "session_id": endorsement.session_id, | ||
| "prior_signer_key": endorsement.prior_signer_key, | ||
| "prior_final_seq": endorsement.prior_final_seq, | ||
| "prior_tail_hash": endorsement.prior_tail_hash, | ||
| "new_signer_key": endorsement.new_signer_key, | ||
| "rotated_at": endorsement.rotated_at, | ||
| } | ||
| encoded = json.dumps( | ||
| ordered, | ||
| ensure_ascii=False, | ||
| separators=(",", ":"), | ||
| ) | ||
| encoded = ( | ||
| encoded.replace("&", "\\u0026") | ||
| .replace("<", "\\u003c") | ||
| .replace(">", "\\u003e") | ||
| .replace("\u2028", "\\u2028") | ||
| .replace("\u2029", "\\u2029") | ||
| ) | ||
| return encoded.encode() | ||
|
|
||
|
|
||
| def verify_rotation_endorsement( | ||
| source: RotationEndorsement | dict[str, Any] | str | bytes, | ||
| ) -> RotationEndorsement: | ||
| """Strictly parse and verify one rotation endorsement.""" | ||
| if isinstance(source, RotationEndorsement): | ||
| endorsement = _decode(vars(source)) | ||
| else: | ||
| if isinstance(source, bytes): | ||
| if len(source) > _MAX_BYTES: | ||
| raise InvalidReceiptError(f"rotation endorsement exceeds {_MAX_BYTES} bytes") | ||
| try: | ||
| text = source.decode("utf-8") | ||
| parsed = loads_no_duplicate_keys(text) | ||
| except (UnicodeDecodeError, json.JSONDecodeError) as exc: | ||
| raise InvalidReceiptError(f"unmarshal rotation endorsement: {exc}") from exc | ||
| elif isinstance(source, str): | ||
| try: | ||
| source_size = len(source.encode()) | ||
| except UnicodeEncodeError as exc: | ||
| raise InvalidReceiptError(f"unmarshal rotation endorsement: {exc}") from exc | ||
| if source_size > _MAX_BYTES: | ||
| raise InvalidReceiptError(f"rotation endorsement exceeds {_MAX_BYTES} bytes") | ||
| try: | ||
| parsed = loads_no_duplicate_keys(source) | ||
| except json.JSONDecodeError as exc: | ||
| raise InvalidReceiptError(f"unmarshal rotation endorsement: {exc}") from exc | ||
| elif isinstance(source, dict): | ||
| parsed = source | ||
| else: | ||
| raise InvalidReceiptError("rotation endorsement must be a JSON object") | ||
| if not isinstance(parsed, dict): | ||
| raise InvalidReceiptError("rotation endorsement must be a JSON object") | ||
| endorsement = _decode(parsed) | ||
| if _ED25519_SIGNATURE.fullmatch(endorsement.endorsement) is None: | ||
| raise InvalidReceiptError("invalid endorsement signature") | ||
| try: | ||
| signature = bytes.fromhex(endorsement.endorsement[len(_SIGNATURE_PREFIX) :]) | ||
| except ValueError as exc: | ||
| raise InvalidReceiptError("invalid endorsement signature") from exc | ||
| if len(signature) != 64: | ||
| raise InvalidReceiptError("invalid endorsement signature") | ||
| digest = hashlib.sha256(_DOMAIN + _canonical_bytes(endorsement)).digest() | ||
| try: | ||
| public_key = Ed25519PublicKey.from_public_bytes(bytes.fromhex(endorsement.prior_signer_key)) | ||
| except (ValueError, UnsupportedAlgorithm) as exc: | ||
| raise InvalidReceiptError("rotation endorsement signer key is invalid") from exc | ||
| try: | ||
| public_key.verify(signature, digest) | ||
| except InvalidSignature as exc: | ||
| raise InvalidReceiptError("rotation endorsement signature verification failed") from exc | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| return endorsement | ||
|
|
||
|
|
||
| def load_rotation_endorsement(path: str | Path) -> RotationEndorsement: | ||
| """Read and verify one bounded endorsement file.""" | ||
| file = Path(path) | ||
| try: | ||
| with file.open("rb") as handle: | ||
| data = handle.read(_MAX_BYTES + 1) | ||
| except OSError as exc: | ||
| raise InvalidReceiptError(f"read rotation endorsement: {exc}") from exc | ||
| return verify_rotation_endorsement(data) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.