class SignedCtrlPayload(Cerealizable): NAME = "SignedCtrlPayload" P_CLS = P.SignedCtrlPld def __init__(self, p): # pragma: no cover super().__init__(p) self.psign = ProtoSign(self.p.sign) @classmethod def from_raw(cls, raw): data = Raw(raw, "%s.from_raw" % cls.NAME) plen = struct.unpack("!I", data.pop(4))[0] if len(data) != plen: raise SCIONParseError( "Payload length mismatch. Expected: %s Actual: %s" % (plen, len(data))) try: p = cls.P_CLS.from_bytes_packed(data.pop()).as_builder() except capnp.lib.capnp.KjException as e: raise SCIONParseError("Unable to parse %s capnp message: %s" % (cls.NAME, e)) from None return cls.from_proto(p) @classmethod def from_values(cls, cpld_raw, sign=None): if not sign: sign = ProtoSign.from_values(ProtoSignType.NONE, b"") return cls(cls.P_CLS.new_message(blob=cpld_raw, sign=sign.p)) def sign(self, key): return self.psign.sign(key, self.p.blob) def verify(self, key): return self.psign.verify(key, self.p.blob) def pack(self): # pragma: no cover raw = self.proto().to_bytes_packed() return struct.pack("!I", len(raw)) + raw def pld(self): return CtrlPayload.from_raw(self.p.blob)
def sign(self, key): # pragma: no cover assert len(self.p.asEntries) > 0, "No ASMarkings to sign" s = ProtoSign(self.p.asEntries[-1].sign) s.sign(key, self._sig_input())