Skip to content

fix: USB AOAP recovery, TLS bridge guard, SSL decrypt drain, diagnostics#84

Merged
matt2005 merged 2 commits intodevelopfrom
fix/ssl-read-decrypt-trace
Mar 13, 2026
Merged

fix: USB AOAP recovery, TLS bridge guard, SSL decrypt drain, diagnostics#84
matt2005 merged 2 commits intodevelopfrom
fix/ssl-read-decrypt-trace

Conversation

@matt2005
Copy link

Summary

This PR contains a set of fixes and diagnostic improvements to the USB AOAP transport stack, uncovered during runtime debugging on Raspberry Pi 5 with a USB-connected Android phone.


USB/AOAPDevice.cpp — stale interface claim recovery

When the transport is torn down abruptly (e.g. control version-request timeout triggering a full AASDK stack reset), the OS may keep the AOAP bulk interface claimed by the previous libusb handle. The next claimInterface call returns LIBUSB_ERROR_BUSY (-6).

Added a recovery path: on LIBUSB_ERROR_BUSY, call releaseInterface then retry claimInterface once before throwing.


Messenger/MessageInStream.cpp — TLS bridge guard

The code that synthesises an ENCAPSULATED_SSL prefix (MessageId 3) was unconditionally applied to all encrypted frames on the control channel before the cryptor became active. This misclassified plain control payloads (e.g. version responses) as TLS records.

Fixed by adding a TLS record heuristic guard: only inject the prefix when the payload's first byte is in 0x14–0x17 (TLS content-type) and the second byte is 0x03 (TLS version major). Also promotes frame/payload logs to info level for runtime visibility.


Messenger/Cryptor.cpp — SSL decrypt drain simplification

The previous drain loop used an available-bytes heuristic derived from SSL_pending combined with a payload-length estimate. Under certain timing conditions this caused the loop to exit before the SSL layer had yielded all plaintext bytes.

Replaced with a pure while(true) drain loop with fixed 2048-byte reads that exits only on SSL_ERROR_WANT_READ/SSL_ERROR_WANT_WRITE (all data drained) or a fatal error. Demotes normal drain-complete log from warning to debug.


Channel/Control/ControlServiceChannel.cpp — version/handshake logging

Add info-level log lines for sendVersionRequest (logs AASDK_MAJOR/AASDK_MINOR) and sendHandshake (logs payload size). Promote the incoming MessageId dispatch log from debug to info.


Transport/USBTransport.cpp — diagnostic logging

Add info/warning-level logs for the USB bulk transfer lifecycle:

  • doSend: endpoint address + byte count on submission
  • sendComplete/sendError: outcome with error code + native code
  • enqueueReceive: endpoint address on arm
  • receiveComplete/receiveError: outcome with byte count or error detail

Testing

Validated on Pi 5 (aarch64) against a USB-connected Android phone in AOAP mode. After a control version-request timeout and full AASDK stack reset:

  • LIBUSB_ERROR_BUSY no longer blocks re-attach
  • Control version handshake completes on the second connect cycle
  • Service discovery completes
  • Video channel opens, configures and starts

- handle SSL WANT_READ/WANT_WRITE as partial frame in Cryptor decrypt\n- add richer SSL diagnostics in SSLWrapper/Cryptor\n- move cert/key install path from /etc/openauto to /etc/aasdk\n- add debian postinst migration and permission/ownership fixups
- AOAPDevice: detect LIBUSB_ERROR_BUSY on claimInterface and attempt
  releaseInterface + retry before throwing to handle stale ownership
  after abrupt transport teardown

- MessageInStream: conditionally inject ENCAPSULATED_SSL prefix (MessageId 3)
  only when the incoming payload looks like a TLS record (content-type
  0x14-0x17, version byte 0x03); plain payloads such as version responses
  are no longer misclassified; promote frame/payload logs to info level

- Cryptor: simplify SSL decrypt drain loop to pure while(true) with
  fixed 2048-byte read chunks; remove overhead/expected-bytes heuristics
  that caused premature loop exit; demote WANT_READ/WANT_WRITE to debug

- ControlServiceChannel: add info-level logs for sendVersionRequest
  (logs major/minor) and sendHandshake (logs payload size); promote
  incoming MessageId log to info for runtime visibility

- USBTransport: add diagnostic logs for doSend submission, sendComplete,
  sendError, enqueueReceive, receiveComplete and receiveError including
  endpoint address, byte count and error code/native code
@matt2005 matt2005 merged commit ccfcb03 into develop Mar 13, 2026
3 checks passed
@matt2005 matt2005 deleted the fix/ssl-read-decrypt-trace branch March 13, 2026 23:58
matt2005 added a commit that referenced this pull request Mar 15, 2026
* fix: USB AOAP recovery, TLS bridge guard, SSL decrypt drain, diagnostics (#84)

* Fix SSL decrypt handling and migrate cert install to /etc/aasdk

- handle SSL WANT_READ/WANT_WRITE as partial frame in Cryptor decrypt\n- add richer SSL diagnostics in SSLWrapper/Cryptor\n- move cert/key install path from /etc/openauto to /etc/aasdk\n- add debian postinst migration and permission/ownership fixups

* fix: USB AOAP recovery, TLS bridge, SSL decrypt drain, diagnostics

- AOAPDevice: detect LIBUSB_ERROR_BUSY on claimInterface and attempt
  releaseInterface + retry before throwing to handle stale ownership
  after abrupt transport teardown

- MessageInStream: conditionally inject ENCAPSULATED_SSL prefix (MessageId 3)
  only when the incoming payload looks like a TLS record (content-type
  0x14-0x17, version byte 0x03); plain payloads such as version responses
  are no longer misclassified; promote frame/payload logs to info level

- Cryptor: simplify SSL decrypt drain loop to pure while(true) with
  fixed 2048-byte read chunks; remove overhead/expected-bytes heuristics
  that caused premature loop exit; demote WANT_READ/WANT_WRITE to debug

- ControlServiceChannel: add info-level logs for sendVersionRequest
  (logs major/minor) and sendHandshake (logs payload size); promote
  incoming MessageId log to info for runtime visibility

- USBTransport: add diagnostic logs for doSend submission, sendComplete,
  sendError, enqueueReceive, receiveComplete and receiveError including
  endpoint address, byte count and error code/native code

* Add runtime-toggleable cryptor/message tracing and docs (#85)

* Address Copilot PR review follow-ups (#87)

* Add MessageInStream regression tests and run CI unit tests on PR/main/develop (#88)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant