Skip to content

Commit

Permalink
feat: Rename aes128Key to aesKey to allow aes256 in the future (#5990)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad committed Dec 5, 2023
1 parent 6229284 commit 31c06ca
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 127 deletions.
15 changes: 9 additions & 6 deletions externs/shaka/manifest.js
Expand Up @@ -318,20 +318,23 @@ shaka.extern.CreateSegmentIndexFunction;

/**
* @typedef {{
* method: string,
* bitsKey: number,
* blockCipherMode: string,
* cryptoKey: (webCrypto.CryptoKey|undefined),
* fetchKey: (shaka.extern.CreateSegmentIndexFunction|undefined),
* iv: (!Uint8Array|undefined),
* firstMediaSequenceNumber: number
* }}
*
* @description
* AES-128 key and iv info from the manifest.
* AES key and iv info from the manifest.
*
* @property {string} method
* The key method defined in the manifest.
* @property {number} bitsKey
* The number of the bit key (eg: 128, 256).
* @property {string} blockCipherMode
* The block cipher mode of operation. Possible values: 'CTR' or 'CBC'.
* @property {webCrypto.CryptoKey|undefined} cryptoKey
* Web crypto key object of the AES-128 CBC key. If unset, the "fetchKey"
* Web crypto key object of the AES key. If unset, the "fetchKey"
* property should be provided.
* @property {shaka.extern.FetchCryptoKeysFunction|undefined} fetchKey
* A function that fetches the key.
Expand All @@ -346,7 +349,7 @@ shaka.extern.CreateSegmentIndexFunction;
*
* @exportDoc
*/
shaka.extern.aes128Key;
shaka.extern.aesKey;


/**
Expand Down
21 changes: 11 additions & 10 deletions lib/dash/dash_parser.js
Expand Up @@ -1417,8 +1417,8 @@ shaka.dash.DashParser = class {
const isImage = contentType == ContentType.IMAGE;

try {
/** @type {shaka.extern.aes128Key|undefined} */
let aes128Key = undefined;
/** @type {shaka.extern.aesKey|undefined} */
let aesKey = undefined;
if (contentProtection.aes128Info) {
const getBaseUris = context.representation.getBaseUris;
const uris = shaka.util.ManifestParserUtils.resolveUris(
Expand All @@ -1427,15 +1427,16 @@ shaka.dash.DashParser = class {
const request = shaka.net.NetworkingEngine.makeRequest(
uris, this.config_.retryParameters);

aes128Key = {
method: 'AES-128',
aesKey = {
bitsKey: 128,
blockCipherMode: 'CBC',
iv: contentProtection.aes128Info.iv,
firstMediaSequenceNumber: 0,
};

// Don't download the key object until the segment is parsed, to
// avoid a startup delay for long manifests with lots of keys.
aes128Key.fetchKey = async () => {
aesKey.fetchKey = async () => {
const keyResponse =
await this.makeNetworkRequest_(request, requestType);

Expand All @@ -1451,27 +1452,27 @@ shaka.dash.DashParser = class {
const algorithm = {
name: 'AES-CBC',
};
aes128Key.cryptoKey = await window.crypto.subtle.importKey(
aesKey.cryptoKey = await window.crypto.subtle.importKey(
'raw', keyResponse.data, algorithm, true, ['decrypt']);
aes128Key.fetchKey = undefined; // No longer needed.
aesKey.fetchKey = undefined; // No longer needed.
};
}
const requestSegment = (uris, startByte, endByte, isInit) => {
return this.requestSegment_(uris, startByte, endByte, isInit);
};
if (context.representation.segmentBase) {
streamInfo = shaka.dash.SegmentBase.createStreamInfo(
context, requestSegment, aes128Key);
context, requestSegment, aesKey);
} else if (context.representation.segmentList) {
streamInfo = shaka.dash.SegmentList.createStreamInfo(
context, this.streamMap_, aes128Key);
context, this.streamMap_, aesKey);
} else if (context.representation.segmentTemplate) {
const hasManifest = !!this.manifest_;

streamInfo = shaka.dash.SegmentTemplate.createStreamInfo(
context, requestSegment, this.streamMap_, hasManifest,
this.config_.dash.initialSegmentLimit, this.periodDurations_,
aes128Key);
aesKey);
} else {
goog.asserts.assert(isText,
'Must have Segment* with non-text streams.');
Expand Down
12 changes: 6 additions & 6 deletions lib/dash/segment_base.js
Expand Up @@ -31,10 +31,10 @@ shaka.dash.SegmentBase = class {
*
* @param {shaka.dash.DashParser.Context} context
* @param {function(?shaka.dash.DashParser.InheritanceFrame):Element} callback
* @param {shaka.extern.aes128Key|undefined} aes128Key
* @param {shaka.extern.aesKey|undefined} aesKey
* @return {shaka.media.InitSegmentReference}
*/
static createInitSegment(context, callback, aes128Key) {
static createInitSegment(context, callback, aesKey) {
const MpdUtils = shaka.dash.MpdUtils;
const XmlUtils = shaka.util.XmlUtils;
const ManifestParserUtils = shaka.util.ManifestParserUtils;
Expand Down Expand Up @@ -69,18 +69,18 @@ shaka.dash.SegmentBase = class {
qualityInfo,
/* timescale= */ null,
/* segmentData= */ null,
aes128Key);
aesKey);
}

/**
* Creates a new StreamInfo object.
*
* @param {shaka.dash.DashParser.Context} context
* @param {shaka.dash.DashParser.RequestSegmentCallback} requestSegment
* @param {shaka.extern.aes128Key|undefined} aes128Key
* @param {shaka.extern.aesKey|undefined} aesKey
* @return {shaka.dash.DashParser.StreamInfo}
*/
static createStreamInfo(context, requestSegment, aes128Key) {
static createStreamInfo(context, requestSegment, aesKey) {
goog.asserts.assert(context.representation.segmentBase,
'Should only be called with SegmentBase');
// Since SegmentBase does not need updates, simply treat any call as
Expand All @@ -103,7 +103,7 @@ shaka.dash.SegmentBase = class {
(unscaledPresentationTimeOffset / timescale) || 0;

const initSegmentReference = SegmentBase.createInitSegment(
context, SegmentBase.fromInheritance_, aes128Key);
context, SegmentBase.fromInheritance_, aesKey);

// Throws an immediate error if the format is unsupported.
SegmentBase.checkSegmentIndexRangeSupport_(context, initSegmentReference);
Expand Down
14 changes: 7 additions & 7 deletions lib/dash/segment_list.js
Expand Up @@ -31,16 +31,16 @@ shaka.dash.SegmentList = class {
*
* @param {shaka.dash.DashParser.Context} context
* @param {!Object.<string, !shaka.extern.Stream>} streamMap
* @param {shaka.extern.aes128Key|undefined} aes128Key
* @param {shaka.extern.aesKey|undefined} aesKey
* @return {shaka.dash.DashParser.StreamInfo}
*/
static createStreamInfo(context, streamMap, aes128Key) {
static createStreamInfo(context, streamMap, aesKey) {
goog.asserts.assert(context.representation.segmentList,
'Should only be called with SegmentList');
const SegmentList = shaka.dash.SegmentList;

const initSegmentReference = shaka.dash.SegmentBase.createInitSegment(
context, SegmentList.fromInheritance_, aes128Key);
context, SegmentList.fromInheritance_, aesKey);
const info = SegmentList.parseSegmentListInfo_(context);

SegmentList.checkSegmentListInfo_(context, info);
Expand All @@ -60,7 +60,7 @@ shaka.dash.SegmentList = class {
const references = SegmentList.createSegmentReferences_(
context.periodInfo.start, context.periodInfo.duration,
info.startNumber, context.representation.getBaseUris, info,
initSegmentReference, aes128Key);
initSegmentReference, aesKey);

const isNew = !segmentIndex;
if (segmentIndex) {
Expand Down Expand Up @@ -198,13 +198,13 @@ shaka.dash.SegmentList = class {
* @param {function():!Array.<string>} getBaseUris
* @param {shaka.dash.SegmentList.SegmentListInfo} info
* @param {shaka.media.InitSegmentReference} initSegmentReference
* @param {shaka.extern.aes128Key|undefined} aes128Key
* @param {shaka.extern.aesKey|undefined} aesKey
* @return {!Array.<!shaka.media.SegmentReference>}
* @private
*/
static createSegmentReferences_(
periodStart, periodDuration, startNumber, getBaseUris, info,
initSegmentReference, aes128Key) {
initSegmentReference, aesKey) {
const ManifestParserUtils = shaka.util.ManifestParserUtils;

let max = info.mediaSegments.length;
Expand Down Expand Up @@ -266,7 +266,7 @@ shaka.dash.SegmentList = class {
/* tileDuration= */ null,
/* syncTime= */ null,
shaka.media.SegmentReference.Status.AVAILABLE,
aes128Key));
aesKey));
prevEndTime = endTime;
}

Expand Down
34 changes: 17 additions & 17 deletions lib/dash/segment_template.js
Expand Up @@ -36,19 +36,19 @@ shaka.dash.SegmentTemplate = class {
* @param {number} segmentLimit The maximum number of segments to generate for
* a SegmentTemplate with fixed duration.
* @param {!Object.<string, number>} periodDurationMap
* @param {shaka.extern.aes128Key|undefined} aes128Key
* @param {shaka.extern.aesKey|undefined} aesKey
* @return {shaka.dash.DashParser.StreamInfo}
*/
static createStreamInfo(
context, requestSegment, streamMap, isUpdate, segmentLimit,
periodDurationMap, aes128Key) {
periodDurationMap, aesKey) {
goog.asserts.assert(context.representation.segmentTemplate,
'Should only be called with SegmentTemplate');
const SegmentTemplate = shaka.dash.SegmentTemplate;
const TimelineSegmentIndex = shaka.dash.TimelineSegmentIndex;

const initSegmentReference =
SegmentTemplate.createInitSegment_(context, aes128Key);
SegmentTemplate.createInitSegment_(context, aesKey);

/** @type {shaka.dash.SegmentTemplate.SegmentTemplateInfo} */
const info = SegmentTemplate.parseSegmentTemplateInfo_(context);
Expand Down Expand Up @@ -84,7 +84,7 @@ shaka.dash.SegmentTemplate = class {
generateSegmentIndex: () => {
return SegmentTemplate.generateSegmentIndexFromDuration_(
shallowCopyOfContext, info, segmentLimit, initSegmentReference,
periodDurationMap, aes128Key);
periodDurationMap, aesKey);
},
};
} else {
Expand Down Expand Up @@ -136,7 +136,7 @@ shaka.dash.SegmentTemplate = class {
periodEnd,
initSegmentReference,
shouldFit,
aes128Key,
aesKey,
context.representation.segmentSequenceCadence,
);
} else {
Expand Down Expand Up @@ -304,13 +304,13 @@ shaka.dash.SegmentTemplate = class {
* @param {number} segmentLimit The maximum number of segments to generate.
* @param {shaka.media.InitSegmentReference} initSegmentReference
* @param {!Object.<string, number>} periodDurationMap
* @param {shaka.extern.aes128Key|undefined} aes128Key
* @param {shaka.extern.aesKey|undefined} aesKey
* @return {!Promise.<shaka.media.SegmentIndex>}
* @private
*/
static generateSegmentIndexFromDuration_(
context, info, segmentLimit, initSegmentReference, periodDurationMap,
aes128Key) {
aesKey) {
goog.asserts.assert(info.mediaTemplate,
'There should be a media template with duration');

Expand Down Expand Up @@ -462,7 +462,7 @@ shaka.dash.SegmentTemplate = class {
/* tileDuration= */ null,
/* syncTime= */ null,
shaka.media.SegmentReference.Status.AVAILABLE,
aes128Key);
aesKey);
// This is necessary information for thumbnail streams:
ref.trueEndTime = trueSegmentEnd;
return ref;
Expand Down Expand Up @@ -525,11 +525,11 @@ shaka.dash.SegmentTemplate = class {
* Creates an init segment reference from a context object.
*
* @param {shaka.dash.DashParser.Context} context
* @param {shaka.extern.aes128Key|undefined} aes128Key
* @param {shaka.extern.aesKey|undefined} aesKey
* @return {shaka.media.InitSegmentReference}
* @private
*/
static createInitSegment_(context, aes128Key) {
static createInitSegment_(context, aesKey) {
const MpdUtils = shaka.dash.MpdUtils;
const ManifestParserUtils = shaka.util.ManifestParserUtils;
const SegmentTemplate = shaka.dash.SegmentTemplate;
Expand Down Expand Up @@ -559,7 +559,7 @@ shaka.dash.SegmentTemplate = class {
qualityInfo,
/* timescale= */ null,
/* segmentData= */ null,
aes128Key);
aesKey);
}
};

Expand All @@ -586,12 +586,12 @@ shaka.dash.TimelineSegmentIndex = class extends shaka.media.SegmentIndex {
* @param {number} periodEnd
* @param {shaka.media.InitSegmentReference} initSegmentReference
* @param {boolean} shouldFit
* @param {shaka.extern.aes128Key|undefined} aes128Key
* @param {shaka.extern.aesKey|undefined} aesKey
* @param {number} segmentSequenceCadence
*/
constructor(templateInfo, representationId, bandwidth, getBaseUris,
periodStart, periodEnd, initSegmentReference, shouldFit,
aes128Key, segmentSequenceCadence) {
aesKey, segmentSequenceCadence) {
super([]);

/** @private {?shaka.dash.SegmentTemplate.SegmentTemplateInfo} */
Expand All @@ -608,8 +608,8 @@ shaka.dash.TimelineSegmentIndex = class extends shaka.media.SegmentIndex {
this.periodEnd_ = periodEnd;
/** @private {shaka.media.InitSegmentReference} */
this.initSegmentReference_ = initSegmentReference;
/** @private {shaka.extern.aes128Key|undefined} */
this.aes128Key_ = aes128Key;
/** @private {shaka.extern.aesKey|undefined} */
this.aesKey_ = aesKey;
/** @private {number} */
this.segmentSequenceCadence_ = segmentSequenceCadence;

Expand Down Expand Up @@ -872,7 +872,7 @@ shaka.dash.TimelineSegmentIndex = class extends shaka.media.SegmentIndex {
/* tileDuration= */ null,
/* syncTime= */ null,
shaka.media.SegmentReference.Status.AVAILABLE,
this.aes128Key_);
this.aesKey_);
if (this.segmentSequenceCadence_ == 0) {
if (i > 0) {
partial.markAsNonIndependent();
Expand Down Expand Up @@ -914,7 +914,7 @@ shaka.dash.TimelineSegmentIndex = class extends shaka.media.SegmentIndex {
/* tileDuration= */ null,
/* syncTime= */ null,
shaka.media.SegmentReference.Status.AVAILABLE,
this.aes128Key_,
this.aesKey_,
/* allPartialSegments= */ range.partialSegments > 0);
this.references[correctedPosition] = ref;
}
Expand Down

0 comments on commit 31c06ca

Please sign in to comment.