async def finish_pairing(self, pin: int) -> HapCredentials: """Finish pairing process.""" self.srp.step1(pin) pub_key, proof = self.srp.step2(self._atv_pub_key, self._atv_salt) resp = await self.protocol.exchange_opack( FrameType.PS_Next, { PAIRING_DATA_KEY: hap_tlv8.write_tlv({ TlvValue.SeqNo: b"\x03", TlvValue.PublicKey: pub_key, TlvValue.Proof: proof, }), "_pwTy": 1, }, ) pairing_data = _get_pairing_data(resp) atv_proof = pairing_data[TlvValue.Proof] log_binary(_LOGGER, "Device", Proof=atv_proof) # TODO: Dummy data: what to set? needed at all? additional_data = { "altIRK": b"-\x54\xe0\x7a\x88*en\x11\xab\x82v-'%\xc5", "accountID": "DC6A7CB6-CA1A-4BF4-880D-A61B717814DB", "model": "AppleTV6,2", "wifiMAC": b"@\xff\xa1\x8f\xa1\xb9", "name": "Living Room", "mac": b"@\xc4\xff\x8f\xb1\x99", } encrypted_data = self.srp.step3( additional_data={17: opack.pack(additional_data)}) resp = await self.protocol.exchange_opack( FrameType.PS_Next, { PAIRING_DATA_KEY: hap_tlv8.write_tlv({ TlvValue.SeqNo: b"\x05", TlvValue.EncryptedData: encrypted_data, }), "_pwTy": 1, }, ) pairing_data = _get_pairing_data(resp) encrypted_data = pairing_data[TlvValue.EncryptedData] return self.srp.step4(encrypted_data)
def step3(self, additional_data=None): """Third pairing step.""" ios_device_x = hkdf_expand( "Pair-Setup-Controller-Sign-Salt", "Pair-Setup-Controller-Sign-Info", binascii.unhexlify(self._session.key), ) self._session_key = hkdf_expand( "Pair-Setup-Encrypt-Salt", "Pair-Setup-Encrypt-Info", binascii.unhexlify(self._session.key), ) device_info = ios_device_x + self.pairing_id + self._auth_public device_signature = self._signing_key.sign(device_info) tlv = { TlvValue.Identifier: self.pairing_id, TlvValue.PublicKey: self._auth_public, TlvValue.Signature: device_signature, } if additional_data: tlv.update(additional_data) chacha = chacha20.Chacha20Cipher(self._session_key, self._session_key) encrypted_data = chacha.encrypt( hap_tlv8.write_tlv(tlv), nounce="PS-Msg05".encode() ) log_binary(_LOGGER, "Data", Encrypted=encrypted_data) return encrypted_data
def _seqno5_pairing(self, _): session_key = hkdf_expand( "Pair-Setup-Encrypt-Salt", "Pair-Setup-Encrypt-Info", binascii.unhexlify(self.session.key), ) acc_device_x = hkdf_expand( "Pair-Setup-Accessory-Sign-Salt", "Pair-Setup-Accessory-Sign-Info", binascii.unhexlify(self.session.key), ) device_info = acc_device_x + self.unique_id + self.keys.auth_pub signature = self.keys.sign.sign(device_info) tlv = write_tlv({ TlvValue.Identifier: self.unique_id, TlvValue.PublicKey: self.keys.auth_pub, TlvValue.Signature: signature, }) chacha = chacha20.Chacha20Cipher(session_key, session_key) encrypted = chacha.encrypt(tlv, nounce="PS-Msg06".encode()) msg = messages.crypto_pairing({ TlvValue.SeqNo: b"\x06", TlvValue.EncryptedData: encrypted }) self.has_paired = True self.delegate.send(msg)
async def start_pairing(self) -> None: """Start pairing procedure.""" self.srp.initialize() await self.protocol.start() resp = await self.protocol.exchange_opack( FrameType.PS_Start, { PAIRING_DATA_KEY: hap_tlv8.write_tlv({ TlvValue.Method: b"\x00", TlvValue.SeqNo: b"\x01" }), "_pwTy": 1, }, ) pairing_data = _get_pairing_data(resp) self._atv_salt = pairing_data[TlvValue.Salt] self._atv_pub_key = pairing_data[TlvValue.PublicKey] log_binary( _LOGGER, "Got pub key and salt", Salt=self._atv_salt, PubKey=self._atv_pub_key, )
async def verify_credentials(self) -> None: """Verify credentials with device.""" _, public_key = self.srp.initialize() resp = await self.protocol.exchange_opack( FrameType.PV_Start, { PAIRING_DATA_KEY: hap_tlv8.write_tlv({ TlvValue.SeqNo: b"\x01", TlvValue.PublicKey: public_key }), "_auTy": 4, }, ) pairing_data = _get_pairing_data(resp) server_pub_key = pairing_data[TlvValue.PublicKey] encrypted = pairing_data[TlvValue.EncryptedData] log_binary(_LOGGER, "Device", Public=self.credentials.ltpk, Encrypted=encrypted) encrypted_data = self.srp.verify1(self.credentials, server_pub_key, encrypted) await self.protocol.exchange_opack( FrameType.PV_Next, { PAIRING_DATA_KEY: hap_tlv8.write_tlv({ TlvValue.SeqNo: b"\x03", TlvValue.EncryptedData: encrypted_data }), }, ) # TODO: check status code self._output_key, self._input_key = self.srp.verify2( SRP_SALT, SRP_OUTPUT_INFO, SRP_INPUT_INFO)
def crypto_pairing(pairing_data, is_pairing=False): """Create a new CRYPTO_PAIRING_MESSAGE.""" message = create(protobuf.CRYPTO_PAIRING_MESSAGE) crypto = message.inner() crypto.status = 0 crypto.pairingData = hap_tlv8.write_tlv(pairing_data) # Hardcoded values for now, might have to be changed crypto.isRetrying = False crypto.isUsingSystemPairing = False crypto.state = 2 if is_pairing else 0 return message
def verify1(self, credentials, session_pub_key, encrypted): """First verification step.""" self._shared = self._verify_private.exchange( X25519PublicKey.from_public_bytes(session_pub_key) ) session_key = hkdf_expand( "Pair-Verify-Encrypt-Salt", "Pair-Verify-Encrypt-Info", self._shared ) chacha = chacha20.Chacha20Cipher(session_key, session_key) decrypted_tlv = hap_tlv8.read_tlv( chacha.decrypt(encrypted, nounce="PV-Msg02".encode()) ) identifier = decrypted_tlv[TlvValue.Identifier] signature = decrypted_tlv[TlvValue.Signature] if identifier != credentials.atv_id: raise exceptions.AuthenticationError("incorrect device response") info = session_pub_key + bytes(identifier) + self._public_bytes ltpk = Ed25519PublicKey.from_public_bytes(bytes(credentials.ltpk)) try: ltpk.verify(bytes(signature), bytes(info)) except InvalidSignature as ex: raise exceptions.AuthenticationError("signature error") from ex device_info = self._public_bytes + credentials.client_id + session_pub_key device_signature = Ed25519PrivateKey.from_private_bytes(credentials.ltsk).sign( device_info ) tlv = hap_tlv8.write_tlv( { TlvValue.Identifier: credentials.client_id, TlvValue.Signature: device_signature, } ) return chacha.encrypt(tlv, nounce="PV-Msg03".encode())
def _seqno1_paired(self, pairing_data): server_pub_key = self.keys.verify_pub.public_bytes( encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw) client_pub_key = pairing_data[TlvValue.PublicKey] shared_key = self.keys.verify.exchange( X25519PublicKey.from_public_bytes(client_pub_key)) session_key = hkdf_expand("Pair-Verify-Encrypt-Salt", "Pair-Verify-Encrypt-Info", shared_key) info = server_pub_key + self.unique_id + client_pub_key signature = self.keys.sign.sign(info) tlv = write_tlv({ TlvValue.Identifier: self.unique_id, TlvValue.Signature: signature }) chacha = chacha20.Chacha20Cipher(session_key, session_key) encrypted = chacha.encrypt(tlv, nounce="PV-Msg02".encode()) msg = messages.crypto_pairing({ TlvValue.SeqNo: b"\x02", TlvValue.PublicKey: server_pub_key, TlvValue.EncryptedData: encrypted, }) self.output_key = hkdf_expand("MediaRemote-Salt", "MediaRemote-Write-Encryption-Key", shared_key) self.input_key = hkdf_expand("MediaRemote-Salt", "MediaRemote-Read-Encryption-Key", shared_key) log_binary(_LOGGER, "Keys", Output=self.output_key, Input=self.input_key) self.delegate.send(msg)
def test_write_key_larger_than_255_bytes(): # This will actually result in two serialized TLVs, one being 255 bytes # and the next one will contain the remaining one byte assert write_tlv(LARGE_KEY_IN) == LARGE_KEY_OUT
def test_write_two_keys(): assert write_tlv(DOUBLE_KEY_IN) == DOUBLE_KEY_OUT
def test_write_single_key(): assert write_tlv(SINGLE_KEY_IN) == SINGLE_KEY_OUT