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.

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