def upgrade_firmware( self, firmware_name, firmware_key, url=LEDGER_HSM_URL, key=LEDGER_HSM_KEY ): script = HsmScript( "distributeFirmware11_scan", {"persoKey": key, "scpv2": "dummy"} ) server = HsmServer(script, url) self.authenticate(server) response = b"" while True: app_data = server.query( data=None if len(response) < 2 else b"\xff\xff" + response[:-2] ) if len(app_data) == 0: break response = self.raw_exchange(app_data) application_data = server.query( params={"firmware": firmware_name, "firmwareKey": firmware_key} ) offset = 0 while offset < len(application_data): apdu_len = application_data[offset + 4] self.raw_exchange(application_data[offset : offset + 5 + apdu_len]) offset += 5 + apdu_len
def endorse(self, key_id: int, url=LEDGER_HSM_URL, key=LEDGER_HSM_KEY): script = HsmScript("signEndorsement", {"persoKey": key}) server = HsmServer(script, url) self.authenticate(server) server.query() # Commit agreement data = self.apdu_exchange(LedgerIns.ENDORSE_SET_START, p1=key_id) certificate = server.query(data, params={"endorsementKey": "attest_1"}) # Commit endorsement certificate self.apdu_exchange(LedgerIns.ENDORSE_SET_COMMIT, certificate) return True
def list_apps_remote(self, url=LEDGER_HSM_URL, key=LEDGER_HSM_KEY): script = HsmScript("listApps", {"persoKey": key, "scpv2": "dummy"}) server = HsmServer(script, url) self.authenticate(server) application_data = server.query(params={"scpv2": "dummy"}) while True: if len(application_data) == 0: break client_data = self.raw_exchange(application_data) application_data = server.query(client_data[:-2], params={"scpv2": "dummy"}) application_data = server.query(params={"scpv2": "dummy"}) apps = AppList() apps.ParseFromString(application_data) for app in apps.list: yield AppInfo(app.name, app.flags & 0xFFFF, app.hashCodeData, app.hash)
def genuine_check(self, url=LEDGER_HSM_URL, key=LEDGER_HSM_KEY): script = HsmScript("checkGenuine", {"persoKey": key, "scpv2": "dummy"}) server = HsmServer(script, url) self.authenticate(server) client_data = b"" while True: application_data = server.query(client_data[:-2]) # No not send status word if len(application_data) < 5: break client_data = self.raw_exchange(application_data) # custom_ui = client_data[0] # custom_ca = client_data[1] return True
def install_remote_app( self, app_path, key_path, url=LEDGER_HSM_URL, key=LEDGER_HSM_KEY ): script = HsmScript("distributeFirmware11", {"persoKey": key, "scpv2": "dummy"}) server = HsmServer(script, url) self.authenticate(server) application_data = server.query( params={"firmware": app_path, "firmwareKey": key_path, "scpv2": "dummy"} ) offset = 0 while offset < len(application_data): apdu_len = application_data[offset + 4] self.raw_exchange(application_data[offset : offset + 5 + apdu_len]) offset += 5 + apdu_len