def deserialize_ascii_armored_timestamp(git_commit, gpg_sig): stamp_start = gpg_sig.find(ASCII_ARMOR_HEADER) if stamp_start == -1: return (None, None, None) stamp_end = gpg_sig.find(b'\n' + ASCII_ARMOR_FOOTER) if stamp_end == -1: return (None, None, None) base64_encoded_stamp = gpg_sig[stamp_start + len(ASCII_ARMOR_HEADER):stamp_end] initial_msg = hash_signed_commit(git_commit, gpg_sig[0:stamp_start]) try: serialized_stamp = base64.standard_b64decode(base64_encoded_stamp) major_version = serialized_stamp[0] minor_version = serialized_stamp[1] if major_version != 1: logging.error("Can't verify timestamp; major version %d not known" % major_version) sys.exit(1) logging.debug("Git timestamp is version %d.%d" % (major_version, minor_version)) ctx = BytesDeserializationContext(serialized_stamp[2:]) timestamp = Timestamp.deserialize(ctx, initial_msg) return (major_version, minor_version, timestamp) except Exception as err: logging.error("Bad timestamp: %r" % err) return (None, None, None)
def get_timestamp(self, commitment, timeout=None): """Get a timestamp for a given commitment Raises KeyError if the calendar doesn't have that commitment """ req = urllib.request.Request( self.url + '/timestamp/' + binascii.hexlify(commitment).decode('utf8'), headers=self.request_headers) try: with urllib.request.urlopen(req, timeout=timeout) as resp: if resp.status == 200: # FIXME: Not a particularly nice way of handling this, but it'll do # the job for now. resp_bytes = resp.read(10000) if len(resp_bytes) > 10000: raise Exception( "Calendar response exceeded size limit") ctx = BytesDeserializationContext(resp_bytes) return Timestamp.deserialize(ctx, commitment) else: raise Exception("Unknown response from calendar: %d" % resp.status) except urllib.error.HTTPError as exp: if exp.code == 404: raise CommitmentNotFoundError(get_sanitised_resp_msg(exp)) else: raise exp
def get_timestamp(self, commitment): """Get a timestamp for a given commitment Raises KeyError if the calendar doesn't have that commitment """ req = urllib.request.Request( self.url + "/timestamp/" + binascii.hexlify(commitment).decode("utf8"), headers=self.request_headers ) try: with urllib.request.urlopen(req) as resp: if resp.status == 200: # FIXME: Not a particularly nice way of handling this, but it'll do # the job for now. resp_bytes = resp.read(10000) if len(resp_bytes) > 10000: raise Exception("Calendar response exceeded size limit") ctx = BytesDeserializationContext(resp_bytes) return Timestamp.deserialize(ctx, commitment) else: raise Exception("Unknown response from calendar: %d" % resp.status) except urllib.error.HTTPError as exp: if exp.code == 404: raise CommitmentNotFoundError(get_sanitised_resp_msg(exp)) else: raise exp
def __getitem__(self, commitment): if self.path is None: raise KeyError elif len(commitment) > 64: # FIXME: hack to avoid filename-too-long errors raise KeyError try: with open(self.__commitment_to_filename(commitment), 'rb') as stamp_fd: ctx = StreamDeserializationContext(stamp_fd) stamp = Timestamp.deserialize(ctx, commitment) return stamp except FileNotFoundError: raise KeyError
def submit(self, digest): """Submit a digest to the calendar Returns a Timestamp committing to that digest """ req = urllib.request.Request(self.url + "/digest", data=digest, headers=self.request_headers) with urllib.request.urlopen(req) as resp: if resp.status != 200: raise Exception("Unknown response from calendar: %d" % resp.status) # FIXME: Not a particularly nice way of handling this, but it'll do # the job for now. resp_bytes = resp.read(10000) if len(resp_bytes) > 10000: raise Exception("Calendar response exceeded size limit") ctx = BytesDeserializationContext(resp_bytes) return Timestamp.deserialize(ctx, digest)
def submit(self, digest, timeout=None): """Submit a digest to the calendar Returns a Timestamp committing to that digest """ req = urllib.request.Request(self.url + '/digest', data=digest, headers=self.request_headers) with urllib.request.urlopen(req, timeout=timeout) as resp: if resp.status != 200: raise Exception("Unknown response from calendar: %d" % resp.status) # FIXME: Not a particularly nice way of handling this, but it'll do # the job for now. resp_bytes = resp.read(10000) if len(resp_bytes) > 10000: raise Exception("Calendar response exceeded size limit") ctx = BytesDeserializationContext(resp_bytes) return Timestamp.deserialize(ctx, digest)