def test_srk_table_cert_hashing(data_dir, srk_pem): """Recreate SRK_1_2_H3_H4 table from certificates""" srk_table = SrkTable(version=0x40) srk_table.append( SrkItem.from_certificate( x509.load_pem_x509_certificate(srk_pem[0], default_backend()))) srk_table.append( SrkItem.from_certificate( x509.load_pem_x509_certificate(srk_pem[1], default_backend()))) srk_table.append( SrkItem.from_certificate( x509.load_pem_x509_certificate(srk_pem[2], default_backend())).hashed_entry()) srk_table.append( SrkItem.from_certificate( x509.load_pem_x509_certificate(srk_pem[3], default_backend())).hashed_entry()) assert srk_table.export() assert len(srk_table.export_fuses()) == 32 assert srk_table.info() # test export returns any result with open(os.path.join(data_dir, 'SRK_1_2_H3_H4_table.bin'), 'rb') as f: preimaged_srk_table_data = f.read() assert srk_table.export() == preimaged_srk_table_data assert srk_table == SrkTable.parse(preimaged_srk_table_data) with open(os.path.join(data_dir, 'SRK_1_2_3_4_fuse.bin'), 'rb') as f: srk_fuses = f.read() assert srk_table.export_fuses() == srk_fuses
def test_srk_table_export(data_dir, srk_pem): srk_table = SrkTable(version=0x40) for pem_data in srk_pem: cert = x509.load_pem_x509_certificate(pem_data, default_backend()) srk_table.append(SrkItem.from_certificate(cert)) with open(os.path.join(data_dir, 'SRK_1_2_3_4_table.bin'), 'rb') as f: srk_table_data = f.read() assert srk_table.export() == srk_table_data assert srk_table == SrkTable.parse(srk_table_data)
def test_srk_table_single_cert(srk_pem): """Smoke test that SrkTable with single certificate works""" srk_table = SrkTable(version=0x40) cert = x509.load_pem_x509_certificate(srk_pem[0], default_backend()) srk_table.append(SrkItem.from_certificate(cert)) # test export() returns any result assert srk_table.export() # test export_fuses() returns valid length assert len(srk_table.export_fuses()) == 32 # test get_fuse() returns valid value for fuse_index in range(8): assert srk_table.get_fuse(fuse_index) >= 0 with pytest.raises(AssertionError): srk_table.get_fuse(8) # test info() returns non-empty text assert srk_table.info() # test export returns any result
def main() -> None: """Main function.""" cert_files = [f for f in os.listdir(DATA_DIR) if f.endswith(".pem")] if not cert_files: print(f'Add generated *.pem files into "{DATA_DIR}" directory') sys.exit() # Create SRK Table instance srk_table = SrkTable(version=0x40) for cert_file in cert_files: with open(f"{DATA_DIR}/{cert_file}", "rb") as f: certificate = x509.load_pem_x509_certificate( f.read(), default_backend()) srk_item = SrkItem.from_certificate(certificate) srk_table.append(srk_item) with open(f"{DATA_DIR}/srk_fuses.bin", "wb") as f: f.write(srk_table.export_fuses()) with open(f"{DATA_DIR}/srk_table.bin", "wb") as f: f.write(srk_table.export())