Initial commit
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Local software-key demo. The Pi keeps its actual private key in its TPM."""
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from urllib.error import HTTPError
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
from cryptography.hazmat.primitives import hashes, serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||
|
||||
from protocol import canonical_payload
|
||||
from server import signature_padding
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("action", choices=["init", "send", "emit"])
|
||||
parser.add_argument("--url", default="http://127.0.0.1:8000/api/evidence")
|
||||
parser.add_argument("--event-id", default=None)
|
||||
parser.add_argument("--sequence", type=int, default=None)
|
||||
args = parser.parse_args()
|
||||
if args.action == "init":
|
||||
if Path("keys.json").exists() or Path("demo-keys").exists():
|
||||
parser.error("keys.json or demo-keys already exists; refusing to overwrite")
|
||||
key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
|
||||
Path("demo-keys").mkdir(mode=0o700)
|
||||
private_bytes = key.private_bytes(serialization.Encoding.PEM,
|
||||
serialization.PrivateFormat.PKCS8,
|
||||
serialization.NoEncryption())
|
||||
with os.fdopen(os.open("demo-keys/private.pem", os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600), "wb") as output:
|
||||
output.write(private_bytes)
|
||||
Path("demo-keys/public.pem").write_bytes(key.public_key().public_bytes(
|
||||
serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo))
|
||||
Path("keys.json").write_text(json.dumps({"baltic-edge-01-key-01": {
|
||||
"node_id": "baltic-edge-01", "public_key": "demo-keys/public.pem",
|
||||
"algorithm": "rsassa-sha256"
|
||||
}}, indent=2) + "\n", encoding="utf-8")
|
||||
print("Demo keys and keys.json created.")
|
||||
return
|
||||
payload = {
|
||||
"event_id": args.event_id or f"EVT-{uuid.uuid4()}",
|
||||
"node_id": "baltic-edge-01", "risk_level": "HIGH",
|
||||
"timestamp": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||
"sequence": args.sequence if args.sequence is not None else time.time_ns() // 1_000_000,
|
||||
}
|
||||
private_key = serialization.load_pem_private_key(Path("demo-keys/private.pem").read_bytes(), password=None)
|
||||
signature = private_key.sign(canonical_payload(payload), signature_padding("rsassa-sha256"), hashes.SHA256())
|
||||
envelope = {"payload": payload, "signature": base64.b64encode(signature).decode("ascii"),
|
||||
"key_id": "baltic-edge-01-key-01"}
|
||||
body = json.dumps(envelope, indent=2).encode("utf-8")
|
||||
if args.action == "emit":
|
||||
print(body.decode("utf-8"))
|
||||
return
|
||||
request = Request(args.url, data=body, headers={"Content-Type": "application/json"}, method="POST")
|
||||
try:
|
||||
with urlopen(request, timeout=10) as response:
|
||||
print(response.status, response.read().decode("utf-8"))
|
||||
except HTTPError as error:
|
||||
print(error.code, error.read().decode("utf-8"))
|
||||
raise SystemExit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user