|
| 1 | +from __future__ import annotations |
| 2 | +from e3.os.process import Run |
| 3 | +import base64 |
| 4 | +import json |
| 5 | +import tempfile |
| 6 | +import os |
| 7 | + |
| 8 | + |
| 9 | +class DSSEError(Exception): |
| 10 | + pass |
| 11 | + |
| 12 | + |
| 13 | +class DSSE: |
| 14 | + """DSSE: Dead Simple Signing Envelope. |
| 15 | +
|
| 16 | + The current implementation relies on openssl tool. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, body: str | bytes, payload_type: str) -> None: |
| 20 | + """Initiazse a DSSE envelope. |
| 21 | +
|
| 22 | + :param body: the content to sign |
| 23 | + :param payload_type: the type of the payload |
| 24 | + """ |
| 25 | + if isinstance(body, str): |
| 26 | + self.body = body.encode("utf-8") |
| 27 | + else: |
| 28 | + self.body = body |
| 29 | + self.payload_type = payload_type |
| 30 | + self.signatures: list[dict[str, str]] = [] |
| 31 | + |
| 32 | + def sign(self, key_id: str, private_key: str) -> str: |
| 33 | + """Sign the payload using openssl X509 certificate. |
| 34 | +
|
| 35 | + :param key_id: the key id (used by end-user to identify which key to use |
| 36 | + for verification). |
| 37 | + :param private_key: path to file containing the private key |
| 38 | + :return: return the signature as base64 string |
| 39 | + """ |
| 40 | + p = Run( |
| 41 | + ["openssl", "dgst", "-sha256", "-sign", private_key, "-out", "-", "-"], |
| 42 | + input=b"|" + self.pae, |
| 43 | + ) |
| 44 | + if p.status == 0 and p.raw_out is not None: |
| 45 | + base64_signature = base64.b64encode(p.raw_out).decode("utf-8") |
| 46 | + self.signatures.append({"keyid": key_id, "sig": base64_signature}) |
| 47 | + return base64_signature |
| 48 | + else: |
| 49 | + raise DSSEError(f"SSL error: {p.out}") |
| 50 | + |
| 51 | + def verify(self, certificate: str) -> bool: |
| 52 | + """Preliminary check on the signature. |
| 53 | +
|
| 54 | + The current algorithm is to check that at least one signature correspond |
| 55 | + to the certificate given as parameter. This part should be improved |
| 56 | +
|
| 57 | + :param certifciate: path to the certificate containing the public key |
| 58 | + :return: True if one of the signature can be checked with the certificate |
| 59 | + """ |
| 60 | + # First get the public key |
| 61 | + p = Run(["openssl", "x509", "-pubkey", "-noout", "-in", certificate]) |
| 62 | + if p.status != 0 or p.raw_out is None: |
| 63 | + raise DSSEError(f"Cannot fetch public key from {certificate}") |
| 64 | + public_key = p.raw_out |
| 65 | + |
| 66 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 67 | + with open(os.path.join(temp_dir, "pub.crt"), "wb") as fd: |
| 68 | + fd.write(public_key) |
| 69 | + |
| 70 | + with open(os.path.join(temp_dir, "pae"), "wb") as fd: |
| 71 | + fd.write(self.pae) |
| 72 | + |
| 73 | + for s in self.signatures: |
| 74 | + with open(os.path.join(temp_dir, "sig"), "wb") as fd: |
| 75 | + fd.write(base64.b64decode(s["sig"])) |
| 76 | + |
| 77 | + p = Run( |
| 78 | + [ |
| 79 | + "openssl", |
| 80 | + "dgst", |
| 81 | + "-verify", |
| 82 | + os.path.join(temp_dir, "pub.crt"), |
| 83 | + "-signature", |
| 84 | + os.path.join(temp_dir, "sig"), |
| 85 | + os.path.join(temp_dir, "pae"), |
| 86 | + ], |
| 87 | + ) |
| 88 | + if p.status == 0: |
| 89 | + return True |
| 90 | + return False |
| 91 | + |
| 92 | + @property |
| 93 | + def payload(self) -> str: |
| 94 | + """Return the content to sign as base64 string. |
| 95 | +
|
| 96 | + :return: a base64 string representing the content |
| 97 | + """ |
| 98 | + return base64.b64encode(self.body).decode("utf-8") |
| 99 | + |
| 100 | + @property |
| 101 | + def pae(self) -> bytes: |
| 102 | + """Return the Pre-Authentication Encoding. |
| 103 | +
|
| 104 | + This is the content that is really signed |
| 105 | + """ |
| 106 | + payload_type_bytes = self.payload_type.encode("utf-8") |
| 107 | + return b" ".join( |
| 108 | + ( |
| 109 | + b"DSSEv1", |
| 110 | + str(len(payload_type_bytes)).encode("utf-8"), |
| 111 | + payload_type_bytes, |
| 112 | + str(len(self.body)).encode("utf-8"), |
| 113 | + self.body, |
| 114 | + ) |
| 115 | + ) |
| 116 | + |
| 117 | + def as_dict(self) -> dict: |
| 118 | + """Return the dict representing the DSSE envelope.""" |
| 119 | + return { |
| 120 | + "payload": self.payload, |
| 121 | + "payloadType": self.payload_type, |
| 122 | + "signatures": self.signatures, |
| 123 | + } |
| 124 | + |
| 125 | + def as_json(self) -> str: |
| 126 | + """Return the DSSE envelope.""" |
| 127 | + return json.dumps(self.as_dict()) |
| 128 | + |
| 129 | + @classmethod |
| 130 | + def load_json(cls, envelope: str) -> DSSE: |
| 131 | + """Load a json DSSE string and return a Python DSSE object. |
| 132 | +
|
| 133 | + :param envelope: the json envelope |
| 134 | + """ |
| 135 | + return cls.load_dict(json.loads(envelope)) |
| 136 | + |
| 137 | + @classmethod |
| 138 | + def load_dict(cls, envelope: dict) -> DSSE: |
| 139 | + """Load a dict and return a Python DSSE object. |
| 140 | +
|
| 141 | + :param envelope: the json envelope |
| 142 | + """ |
| 143 | + result = cls( |
| 144 | + body=base64.b64decode(envelope["payload"]), |
| 145 | + payload_type=envelope["payloadType"], |
| 146 | + ) |
| 147 | + result.signatures = [ |
| 148 | + {"keyid": sig["keyid"], "sig": sig["sig"]} for sig in envelope["signatures"] |
| 149 | + ] |
| 150 | + return result |
0 commit comments