Firefox OS/Remote Control: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
(Re-authentication)
(→‎Control event processing: Update to TLS version, remove encryption)
Line 67: Line 67:


=== Control event processing ===
=== Control event processing ===
Control page provides: 1) a virtual touchpad, 2) a virtual vertical scrollbar, 3) 3 function keys. Every touch, swipe or click will be generated to an event in JSON format, encrypted and send to TV. Following describes how control event is sent and processed:
Control page provides: 1) a virtual touchpad, 2) a virtual vertical scrollbar, 3) 3 function keys. Every touch, swipe or click will be generated to an event in JSON format then send to TV. Following describes how control event is sent and processed:


[[File:RemoteControl ControlEventProcessing.png]]
[[File:RemoteControl ControlEventProcessing.png]]


# User opens the URL.
# Client requests page with UUID can access control page.
# Server returns client.html as requested page for remote control.
# User operates control page.
# User operates control page.
# Client generates an event and encrypts the event using symmetric key. [https://github.com/luke-chang/gaia/blob/1228262_tv_remote_control_secure/tv_apps/remote-control-client/js/client.js#L33 source]
# Client generates and sends the event in JSON format.
# Client sends encrypted event.
# Server parses the event to JSON object, dispatch to client.sjs.
# Client.sjs responses with latest event result.
# Client.sjs decrypts event with symmetric key received in establish secure connection. [https://github.com/MDTsai/gecko-dev/blob/Bug_1235013_new_httpserver/b2g/remotecontrol/client.sjs#L474 source]
# Client.sjs parses event and dispatch to Gecko or Gaia system app.
# Client.sjs parses event and dispatch to Gecko or Gaia system app.



Revision as of 06:26, 18 April 2016

Introduction

Remote control is a feature for Firefox OS on TV. After Send tab to TV, user can easily extend browsing experience on TV via Firefox on Android, using virtual touchpad to control TV.

Meta Bug: bug 1205939

Prototype Demo Video: http://bit.ly/1Fa2ST1

UX & Visual Spec

UX Spec: http://bit.ly/1jrWqgn

Visual Spec: http://bit.ly/1jrWQU3

Architecture Designs

RemoteControl BlockDiagram.png

  • Remote Control Service: Entry point of remote control feature. Control service related logic and data in runtime.
  • TLS server: Serves connections from user.

User interaction of remote control is divided into two parts:

  1. Peer authentication
  2. Control event processing

Peer authentication

Remote control is designed to protect data transmitted between client and server, for example: user input string may contains privacy data. The connection is based on self-signed TLS server. To prevent man in the middle(MITM) attack, we adopt J-PAKE to exchange symmetric key for peer authentication. Following describes how it works:

RemoteControl FirstTimeHandshake.png

  1. The user sends the tab to TV on fennec.
  2. Client sends request to handshake without ID.
  3. Server replies with the handshake type is 1st time handshake.
  4. Client computes J-PAKE round 1 and sends the result to server.
  5. Server generates PIN code and show on screen, computes J-PAKE round 1 and sends the result to client.
  6. Server computes J-PAKE round 2 with PIN code attached first 12 characters as weak secret.
  7. User enter PIN code, client computs J-PAKE round 2 with PIN code attached first 12 characters as weak secret.
  8. Client sends round 2 result to server.
  9. Server dismisses PIN code notification on screen, sends round 2 result to client.
  10. Both client and server compute J-PAKE final round, gets AES and HMAC key.
  11. Server use two keys to get a signature of H(H(AES key)), then send the signature to client.
  12. Client verifies the signature matches it has.
  13. Client use two keys to get a signature of H(AES key), then send the signature to server.
  14. Server verifies the signature matches it has.
  15. Server replies with the handshake finish with client's ID for connection in the future.

Re-authentication

User is only required to input PIN code when first time connect to TV. In the second time, client and server leverage previous AES key value and derive new AES and HMAC key. This can reduce number of user input and make the procedure from send tab to TV to remote control more smoothly.

RemoteControl SecondTimeHandshake.png

  1. The user sends the tab to TV on fennec.
  2. Client sends request to handshake with ID.
  3. Server looks up ID/AES key mapping and replies with the handshake type is 2nd time handshake.
  4. Client computes J-PAKE round 1 and sends the result to server.
  5. Server computes J-PAKE round 1 and sends the result to client.
  6. Client and server computes J-PAKE round 2 with previous AES key value as weak secret.
  7. Client sends round 2 result to server.
  8. Server sends round 2 result to client.
  9. Both client and server compute J-PAKE final round, gets new AES and HMAC key.
  10. Server use two keys to get a signature of H(H(AES key)), then send the signature to client.
  11. Client verifies the signature matches it has.
  12. Client use two keys to get a signature of H(AES key), then send the signature to server.
  13. Server verifies the signature matches it has.
  14. Server replies with the handshake finish.

After authentication, client can sends control event to server.

Control event processing

Control page provides: 1) a virtual touchpad, 2) a virtual vertical scrollbar, 3) 3 function keys. Every touch, swipe or click will be generated to an event in JSON format then send to TV. Following describes how control event is sent and processed:

RemoteControl ControlEventProcessing.png

  1. User operates control page.
  2. Client generates and sends the event in JSON format.
  3. Server parses the event to JSON object, dispatch to client.sjs.
  4. Client.sjs parses event and dispatch to Gecko or Gaia system app.

Data encryption/decryption

There are three kinds of data are encrypted while tranmission:

  1. Client polls UUID in establish secure connection stage
  2. Client sends PIN code in pin code pairing
  3. Client sends event in control event processing

As data are encrypted by symmetric key, AES-GCM, there are two things need noticed:

  1. AES-GCM need initialization vector(IV) to encrypt/decrypt. Currently, IV is random value for each data, length is 12 bytes. Encrypted data is appended after IV. Receiver needs to slice first 12 bytes as IV to decrypt.
  2. Each message type is string, use TextEncoder.encode() to encode as an UInt8Array, then encrypt to ArrayBuffer. Vice versa.
  3. Data encryption/decryption is asynchronous but HTTP request need response immediately. For UUID and PIN code case, remote control uses a ticket number to get status after decryption. But for event, we reduce polling result but use latest event's result instead.

Ajax Protocol

RSA public key exchange

Request

 {
   action: 'require-public-key'
 }

Response

 {
   publicKey: <RSA-OEAP SPKI in base64>
 }
 {
   error: <reason>
 }

Send symmetric key

Request

 {
   action: 'send-symmetric-key',
   wrappedSymmetricKey: <AES-GCM wrapped by RSA-OEAP in base64>
 }

Response

 {
   ticket: <ticket>
 }
 {
   error: <reason>
 }

Poll UUID

Request

 {
   action: 'poll-uuid'
   ticket: <ticket>
 }

Response

 {
   done: true,
   encryptedUUID: <IV + UUID encrypted by AES-GCM, in base64>,
 }
 {
   done: true,
   error: <reason>
 }
 {
   done: false // Symmetic key unwrapping or encrypting UUID
 }

Pair PIN code

Request

 {
   action: 'pair-pincode'
   encryptedPIN: <IV + PIN encrypted by AES-GCM in base64>
 }

Response

 {
   ticket: <ticket>
 }

Poll pair result

Request

 {
   action: 'poll-pair-result'
   ticket: <ticket>
 }

Response

 {
   done: true
   verified: <boolean>
   reason: <reason>
 }
 {
   done: false
 }

Encrypted control events

Client sends encrypted event in query string as:

 <IP_Address>:<Port>?message=<IV + encrypted event by AES-GCM in base64>

Response

 {
   verified: <boolean>
 }

After decrypted, following are control events:

Touch Events

 {
   type: 'touchstart',
   detail: {
     width: <touch panel width, integer, in pixels>,
     height: <touch panel height, integer, in pixels>,
     identifier: <A simple unique identifier for server to distinguish touch events between different clients, string>
   }
 }
 {
   type: 'touchmove',
   detail: {
     dx: <dx between current point and starting point, integer, in pixels>,
     dy: <dy between current point and starting point, integer, in pixels>,
     identifier: <should be identical with "touchstart">,
     duration: <duration from "touchstart", integer, in milliseconds>
   }
 }
 {
   type: 'touchend',
   detail: {
     dx: <same as "touchmove">,
     dy: <same as "touchmove">,
     identifier: <should be identical with "touchstart">,
     duration: <same as "touchmove">,
     swipe: <the gesture within this touch, string, should be "left", "right", "up" and "down">
   }
 }

Scroll Events

There are "scrollstart", "scrollmove" and "scrollend" events which details are all identical with corresponding touch events above.

Key Events

 {
   type: 'keypress',
   detail: <KeyEvent constant, string, sush as "DOM_VK_RETURN">
 }

Input Events

 {
   type: 'textinput',
   detail: {
     clear: <whether to clear the entire string in the current focused input field, boolean>,
     string: <new string to append, string>,
     keycode: <a specified key to be pressed after the string inputted, integer>
   }
 }

Custom Events

 {
   type: 'custom',
   detail: {
     action: <custom action name, string>,
     ...
   }
 }

Pairing (Full version, without secure connection)

Meta Bug: bug 1207996

Flowchart

First Time Connection (deprecated in secure connection)

RemoteControl FirstConnect.png

Enter PIN Code (deprecated in secure connection)

RemoteControl EnterPIN.png

Resume Connection (deprecated in secure connection)

RemoteControl ResumeConnection.png

Dismiss Pairing

RemoteControl DismissPairing.png

Protocol

from client to server via AJAX (deprecated in secure connection)

 {
   pincode: <pincode>
 }

response when success (deprecated in secure connection)

 {
   verified: true,
   uuid: <uuid>
 }

reponse when error

 {
   verified: false,
   reason: 'expired' / 'invalid'
 }

internal events at server side

 {
   type: 'mozChromeRemoteControlEvent',
   detail: {
     action: 'pin-created',
     pincode: <pincode>
   }
 }
 {
   type: 'mozChromeRemoteControlEvent',
   detail: {
     action: 'pin-destroyed'
   }
 }
 {
   type: 'mozContentEvent',
   detail: {
     type: 'remote-control-pin-dismissed',
     detail: {
       reason: 'timeout' / 'manually'
     }
   }
 }

Secure Connection (deprecated)

To protect private data between user and TV, we provide a secure connection which refers to SSL and jCryption. Here is the concept:

  1. Client requests RSA public key from TV.
  2. Client sends symmetric key and PIN code (if any) to TV, encrypted with public key.
  3. TV decrypts symmetric key with private key
  4. TV generates an UUID, encrypts with symmetric key then sends 2 UUIDs to client, one is encrypted the other is not.
  5. Client decrypts UUID with symmetric key, confirm UUID
  6. Begin remote control with symmetric key.

Goal of Remote Control is to create an easy to use way for every device, we hide public key exchange at the background. User doesn't need to input a long URL with public key nor scan QRCode. Everything is done automatically without interrupting user experience.

Remote Security.png

Bug Status

Top blockers of bug 1205939 - [TV][2.5][meta][TV side] Remote Control

Bugzilla query error

Array ( [type] => error [message] => http-bad-status [params] => Array ( [0] => 429 [1] => Unknown Error ) ) 1


Top blockers of bug 1207971 - [TV 2.5][meta] Provide a TV remote control app

Bugzilla query error

Array ( [type] => error [message] => http-bad-status [params] => Array ( [0] => 429 [1] => Unknown Error ) ) 1


Top blockers of bug 1208006 - [TV 2.5][meta] Implement a client-side page for TV remote control

Bugzilla query error

Array ( [type] => error [message] => http-bad-status [params] => Array ( [0] => 429 [1] => Unknown Error ) ) 1


Top blockers of bug 1212385 - [TV 2.5][meta] Remote Control features in Smart System

Bugzilla query error

Array ( [type] => error [message] => http-bad-status [params] => Array ( [0] => 429 [1] => Unknown Error ) ) 1


Top blockers of bug 1215457 - [TV 2.5] Secure connection between remote control server and client

Bugzilla query error

Array ( [type] => error [message] => http-bad-status [params] => Array ( [0] => 429 [1] => Unknown Error ) ) 1