def _prepare_index(self): # Add space for file size self.index_offset += 8 # Space for num_signatures & num_additional_sections self.index_offset += 4 + 4 # Write the magic and placeholder for the index self.fileobj.write(b"MAR1" + packint(self.index_offset)) # Write placeholder for file size self.fileobj.write(struct.pack(">Q", 0)) # Write num_signatures self.fileobj.write(packint(len(self.signature_versions))) # Write placeholder signatures for algo_id, keyfile in self.signature_versions: sig = MarSignature(algo_id, keyfile) sig._offset = self.index_offset self.index_offset += sig.size self.signatures.append(sig) # algoid self.fileobj.write(packint(algo_id)) # numbytes self.fileobj.write(packint(sig.sigsize)) # space for signature self.fileobj.write("\0" * sig.sigsize) # Write placeholder for number of additional sections self.fileobj.write(packint(0))
def _read_signatures(self): fp = self.fileobj log.debug("reading signatures") fp.seek(8) file_size = unpacklongint(fp.read(8)) # Check that the file size matches fp.seek(0, 2) assert fp.tell() == file_size fp.seek(16) num_sigs = unpackint(fp.read(4)) log.debug("file_size: %i bytes", file_size) log.debug("%i signatures present", num_sigs) signatures = [] for i in range(num_sigs): sig = MarSignature.from_fileobj(fp) for algo_id, keyfile in self.signature_versions: if algo_id == sig.algo_id: sig.keyfile = keyfile break else: log.info("no key specified to validate %i" " signature", sig.algo_id) signatures.append(sig) # Read additional sections; this is also only present if we have a # signature block num_additional_sections = unpackint(fp.read(4)) log.debug("%i additional sections present", num_additional_sections) for i in range(num_additional_sections): info = AdditionalInfo.from_fileobj(fp) log.debug("%s", info) self.additional_info.append(info) return signatures