You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Context.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* eslint-disable no-bitwise */
  2. /* global BigInt */
  3. import { deriveKeys, importKey, ratchet } from './crypto-utils';
  4. import { isArrayEqual } from './utils';
  5. // We use a ringbuffer of keys so we can change them and still decode packets that were
  6. // encrypted with an old key.
  7. const keyRingSize = 3;
  8. // We copy the first bytes of the VP8 payload unencrypted.
  9. // For keyframes this is 10 bytes, for non-keyframes (delta) 3. See
  10. // https://tools.ietf.org/html/rfc6386#section-9.1
  11. // This allows the bridge to continue detecting keyframes (only one byte needed in the JVB)
  12. // and is also a bit easier for the VP8 decoder (i.e. it generates funny garbage pictures
  13. // instead of being unable to decode).
  14. // This is a bit for show and we might want to reduce to 1 unconditionally in the final version.
  15. //
  16. // For audio (where frame.type is not set) we do not encrypt the opus TOC byte:
  17. // https://tools.ietf.org/html/rfc6716#section-3.1
  18. const unencryptedBytes = {
  19. key: 10,
  20. delta: 3,
  21. undefined: 1 // frame.type is not set on audio
  22. };
  23. // Use truncated SHA-256 hashes, 80 bіts for video, 32 bits for audio.
  24. // This follows the same principles as DTLS-SRTP.
  25. const authenticationTagOptions = {
  26. name: 'HMAC',
  27. hash: 'SHA-256'
  28. };
  29. const digestLength = {
  30. key: 10,
  31. delta: 10,
  32. undefined: 4 // frame.type is not set on audio
  33. };
  34. // Maximum number of forward ratchets to attempt when the authentication
  35. // tag on a remote packet does not match the current key.
  36. const ratchetWindow = 8;
  37. /**
  38. * Per-participant context holding the cryptographic keys and
  39. * encode/decode functions
  40. */
  41. export class Context {
  42. /**
  43. * @param {string} id - local muc resourcepart
  44. */
  45. constructor(id) {
  46. // An array (ring) of keys that we use for sending and receiving.
  47. this._cryptoKeyRing = new Array(keyRingSize);
  48. // A pointer to the currently used key.
  49. this._currentKeyIndex = -1;
  50. // A per-sender counter that is used create the AES CTR.
  51. // Must be incremented on every frame that is sent, can be reset on
  52. // key changes.
  53. this._sendCount = BigInt(0); // eslint-disable-line new-cap
  54. this._id = id;
  55. }
  56. /**
  57. * Derives the different subkeys and starts using them for encryption or
  58. * decryption.
  59. * @param {Uint8Array|false} key bytes. Pass false to disable.
  60. * @param {Number} keyIndex
  61. */
  62. async setKey(keyBytes, keyIndex) {
  63. let newKey;
  64. if (keyBytes) {
  65. const material = await importKey(keyBytes);
  66. newKey = await deriveKeys(material);
  67. } else {
  68. newKey = false;
  69. }
  70. this._currentKeyIndex = keyIndex % this._cryptoKeyRing.length;
  71. this._setKeys(newKey);
  72. }
  73. /**
  74. * Sets a set of keys and resets the sendCount.
  75. * decryption.
  76. * @param {Object} keys set of keys.
  77. * @private
  78. */
  79. _setKeys(keys) {
  80. this._cryptoKeyRing[this._currentKeyIndex] = keys;
  81. this._sendCount = BigInt(0); // eslint-disable-line new-cap
  82. }
  83. /**
  84. * Function that will be injected in a stream and will encrypt the given encoded frames.
  85. *
  86. * @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
  87. * @param {TransformStreamDefaultController} controller - TransportStreamController.
  88. *
  89. * The packet format is a variant of
  90. * https://tools.ietf.org/html/draft-omara-sframe-00
  91. * using a trailer instead of a header. One of the design goals was to not require
  92. * changes to the SFU which for video requires not encrypting the keyframe bit of VP8
  93. * as SFUs need to detect a keyframe (framemarking or the generic frame descriptor will
  94. * solve this eventually). This also "hides" that a client is using E2EE a bit.
  95. *
  96. * Note that this operates on the full frame, i.e. for VP8 the data described in
  97. * https://tools.ietf.org/html/rfc6386#section-9.1
  98. *
  99. * The VP8 payload descriptor described in
  100. * https://tools.ietf.org/html/rfc7741#section-4.2
  101. * is part of the RTP packet and not part of the encoded frame and is therefore not
  102. * controllable by us. This is fine as the SFU keeps having access to it for routing.
  103. */
  104. encodeFunction(encodedFrame, controller) {
  105. const keyIndex = this._currentKeyIndex;
  106. if (this._cryptoKeyRing[keyIndex]) {
  107. this._sendCount++;
  108. // Thіs is not encrypted and contains the VP8 payload descriptor or the Opus TOC byte.
  109. const frameHeader = new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrame.type]);
  110. // Construct frame trailer. Similar to the frame header described in
  111. // https://tools.ietf.org/html/draft-omara-sframe-00#section-4.2
  112. // but we put it at the end.
  113. // 0 1 2 3 4 5 6 7
  114. // ---------+---------------------------------+-+-+-+-+-+-+-+-+
  115. // payload | CTR... (length=LEN) |S|LEN |0| KID |
  116. // ---------+---------------------------------+-+-+-+-+-+-+-+-+
  117. const counter = new Uint8Array(16);
  118. const counterView = new DataView(counter.buffer);
  119. // The counter is encoded as a variable-length field.
  120. counterView.setBigUint64(8, this._sendCount);
  121. let counterLength = 8;
  122. for (let i = 8; i < counter.byteLength; i++ && counterLength--) {
  123. if (counterView.getUint8(i) !== 0) {
  124. break;
  125. }
  126. }
  127. const frameTrailer = new Uint8Array(counterLength + 1);
  128. frameTrailer.set(new Uint8Array(counter.buffer, counter.byteLength - counterLength));
  129. // Since we never send a counter of 0 we send counterLength - 1 on the wire.
  130. // This is different from the sframe draft, increases the key space and lets us
  131. // ignore the case of a zero-length counter at the receiver.
  132. frameTrailer[frameTrailer.byteLength - 1] = keyIndex | ((counterLength - 1) << 4);
  133. // XOR the counter with the saltKey to construct the AES CTR.
  134. const saltKey = new DataView(this._cryptoKeyRing[keyIndex].saltKey);
  135. for (let i = 0; i < counter.byteLength; i++) {
  136. counterView.setUint8(i, counterView.getUint8(i) ^ saltKey.getUint8(i));
  137. }
  138. return crypto.subtle.encrypt({
  139. name: 'AES-CTR',
  140. counter,
  141. length: 64
  142. }, this._cryptoKeyRing[keyIndex].encryptionKey, new Uint8Array(encodedFrame.data,
  143. unencryptedBytes[encodedFrame.type]))
  144. .then(cipherText => {
  145. const newData = new ArrayBuffer(frameHeader.byteLength + cipherText.byteLength
  146. + digestLength[encodedFrame.type] + frameTrailer.byteLength);
  147. const newUint8 = new Uint8Array(newData);
  148. newUint8.set(frameHeader); // copy first bytes.
  149. newUint8.set(new Uint8Array(cipherText), unencryptedBytes[encodedFrame.type]); // add ciphertext.
  150. // Leave some space for the authentication tag. This is filled with 0s initially, similar to
  151. // STUN message-integrity described in https://tools.ietf.org/html/rfc5389#section-15.4
  152. newUint8.set(frameTrailer, frameHeader.byteLength + cipherText.byteLength
  153. + digestLength[encodedFrame.type]); // append trailer.
  154. return crypto.subtle.sign(authenticationTagOptions, this._cryptoKeyRing[keyIndex].authenticationKey,
  155. new Uint8Array(newData)).then(authTag => {
  156. // Set the truncated authentication tag.
  157. newUint8.set(new Uint8Array(authTag, 0, digestLength[encodedFrame.type]),
  158. unencryptedBytes[encodedFrame.type] + cipherText.byteLength);
  159. encodedFrame.data = newData;
  160. return controller.enqueue(encodedFrame);
  161. });
  162. }, e => {
  163. // TODO: surface this to the app.
  164. console.error(e);
  165. // We are not enqueuing the frame here on purpose.
  166. });
  167. }
  168. /* NOTE WELL:
  169. * This will send unencrypted data (only protected by DTLS transport encryption) when no key is configured.
  170. * This is ok for demo purposes but should not be done once this becomes more relied upon.
  171. */
  172. controller.enqueue(encodedFrame);
  173. }
  174. /**
  175. * Function that will be injected in a stream and will decrypt the given encoded frames.
  176. *
  177. * @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
  178. * @param {TransformStreamDefaultController} controller - TransportStreamController.
  179. */
  180. async decodeFunction(encodedFrame, controller) {
  181. const data = new Uint8Array(encodedFrame.data);
  182. const keyIndex = data[encodedFrame.data.byteLength - 1] & 0x7;
  183. if (this._cryptoKeyRing[keyIndex]) {
  184. const counterLength = 1 + ((data[encodedFrame.data.byteLength - 1] >> 4) & 0x7);
  185. const frameHeader = new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrame.type]);
  186. // Extract the truncated authentication tag.
  187. const authTagOffset = encodedFrame.data.byteLength - (digestLength[encodedFrame.type]
  188. + counterLength + 1);
  189. const authTag = encodedFrame.data.slice(authTagOffset, authTagOffset
  190. + digestLength[encodedFrame.type]);
  191. // Set authentication tag bytes to 0.
  192. const zeros = new Uint8Array(digestLength[encodedFrame.type]);
  193. data.set(zeros, encodedFrame.data.byteLength - (digestLength[encodedFrame.type] + counterLength + 1));
  194. // Do truncated hash comparison. If the hash does not match we might have to advance the
  195. // ratchet a limited number of times. See (even though the description there is odd)
  196. // https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.5.1
  197. let { authenticationKey, material } = this._cryptoKeyRing[keyIndex];
  198. let valid = false;
  199. let newKeys = null;
  200. for (let distance = 0; distance < ratchetWindow; distance++) {
  201. const calculatedTag = await crypto.subtle.sign(authenticationTagOptions,
  202. authenticationKey, encodedFrame.data);
  203. if (isArrayEqual(new Uint8Array(authTag),
  204. new Uint8Array(calculatedTag.slice(0, digestLength[encodedFrame.type])))) {
  205. valid = true;
  206. if (distance > 0) {
  207. this._setKeys(newKeys);
  208. }
  209. break;
  210. }
  211. // Attempt to ratchet and generate the next set of keys.
  212. material = await importKey(await ratchet(material));
  213. newKeys = await deriveKeys(material);
  214. authenticationKey = newKeys.authenticationKey;
  215. }
  216. // Check whether we found a valid signature.
  217. if (!valid) {
  218. // TODO: return an error to the app.
  219. console.error('Authentication tag mismatch');
  220. return;
  221. }
  222. // Extract the counter.
  223. const counter = new Uint8Array(16);
  224. counter.set(data.slice(encodedFrame.data.byteLength - (counterLength + 1),
  225. encodedFrame.data.byteLength - 1), 16 - counterLength);
  226. const counterView = new DataView(counter.buffer);
  227. // XOR the counter with the saltKey to construct the AES CTR.
  228. const saltKey = new DataView(this._cryptoKeyRing[keyIndex].saltKey);
  229. for (let i = 0; i < counter.byteLength; i++) {
  230. counterView.setUint8(i,
  231. counterView.getUint8(i) ^ saltKey.getUint8(i));
  232. }
  233. return crypto.subtle.decrypt({
  234. name: 'AES-CTR',
  235. counter,
  236. length: 64
  237. }, this._cryptoKeyRing[keyIndex].encryptionKey, new Uint8Array(encodedFrame.data,
  238. unencryptedBytes[encodedFrame.type],
  239. encodedFrame.data.byteLength - (unencryptedBytes[encodedFrame.type]
  240. + digestLength[encodedFrame.type] + counterLength + 1))
  241. ).then(plainText => {
  242. const newData = new ArrayBuffer(unencryptedBytes[encodedFrame.type] + plainText.byteLength);
  243. const newUint8 = new Uint8Array(newData);
  244. newUint8.set(frameHeader);
  245. newUint8.set(new Uint8Array(plainText), unencryptedBytes[encodedFrame.type]);
  246. encodedFrame.data = newData;
  247. return controller.enqueue(encodedFrame);
  248. }, e => {
  249. console.error(e);
  250. // TODO: notify the application about error status.
  251. // TODO: For video we need a better strategy since we do not want to based any
  252. // non-error frames on a garbage keyframe.
  253. if (encodedFrame.type === undefined) { // audio, replace with silence.
  254. const newData = new ArrayBuffer(3);
  255. const newUint8 = new Uint8Array(newData);
  256. newUint8.set([ 0xd8, 0xff, 0xfe ]); // opus silence frame.
  257. encodedFrame.data = newData;
  258. controller.enqueue(encodedFrame);
  259. }
  260. });
  261. } else if (keyIndex >= this._cryptoKeyRing.length && this._cryptoKeyRing[this._currentKeyIndex]) {
  262. // If we are encrypting but don't have a key for the remote drop the frame.
  263. // This is a heuristic since we don't know whether a packet is encrypted,
  264. // do not have a checksum and do not have signaling for whether a remote participant does
  265. // encrypt or not.
  266. return;
  267. }
  268. // TODO: this just passes through to the decoder. Is that ok? If we don't know the key yet
  269. // we might want to buffer a bit but it is still unclear how to do that (and for how long etc).
  270. controller.enqueue(encodedFrame);
  271. }
  272. }