Skip to content

Commit

Permalink
fix(HLS): Fix decryption of AES-128 (#5871)
Browse files Browse the repository at this point in the history
Related to
#5785 (comment)
  • Loading branch information
avelad authored Nov 8, 2023
1 parent a5adf31 commit da38b36
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/media/streaming_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -1523,11 +1523,11 @@ shaka.media.StreamingEngine = class {
/**
* @param {!BufferSource} rawResult
* @param {shaka.extern.aes128Key} aes128Key
* @param {!shaka.media.SegmentIterator} iter
* @param {number} position
* @return {!Promise.<!BufferSource>} finalResult
* @private
*/
async aes128Decrypt_(rawResult, aes128Key, iter) {
async aes128Decrypt_(rawResult, aes128Key, position) {
const key = aes128Key;
if (!key.cryptoKey) {
goog.asserts.assert(key.fetchKey, 'If AES-128 cryptoKey was not ' +
Expand All @@ -1538,7 +1538,7 @@ shaka.media.StreamingEngine = class {
let iv = key.iv;
if (!iv) {
iv = shaka.util.BufferUtils.toUint8(new ArrayBuffer(16));
let sequence = key.firstMediaSequenceNumber + iter.currentPosition();
let sequence = key.firstMediaSequenceNumber + position;
for (let i = iv.byteLength - 1; i >= 0; i--) {
iv[i] = sequence & 0xff;
sequence >>= 8;
Expand Down Expand Up @@ -2214,18 +2214,17 @@ shaka.media.StreamingEngine = class {
reference, mediaState.stream, streamDataCallback);
}

let position = 0;
if (mediaState.segmentIterator) {
position = mediaState.segmentIterator.currentPosition();
}

mediaState.operation = op;
const response = await op.promise;
mediaState.operation = null;
let result = response.data;
if (reference.aes128Key) {
let iter = mediaState.segmentIterator;
const time = reference.startTime;
if (!iter && time != null) {
iter = mediaState.stream.segmentIndex.getIteratorForTime(time);
}
goog.asserts.assert(iter, 'mediaState.segmentIterator should exist');
result = await this.aes128Decrypt_(result, reference.aes128Key, iter);
result = await this.aes128Decrypt_(result, reference.aes128Key, position);
}
return result;
}
Expand Down

0 comments on commit da38b36

Please sign in to comment.