Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PlayReady DRM in Edge browser #5699

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/controller/eme-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,52 @@ class EMEController implements ComponentAPI {
);
}

private unpackPlayReadyKeyMessage(
xhr: XMLHttpRequest,
licenseChallenge: Uint8Array
): Uint8Array {
// On Edge, the raw license message is UTF-16-encoded XML. We need
// to unpack the Challenge element (base64-encoded string containing the
// actual license request) and any HttpHeader elements (sent as request
// headers).
// For PlayReady CDMs, we need to dig the Challenge out of the XML.
const xmlString = String.fromCharCode.apply(
null,
new Uint16Array(licenseChallenge.buffer)
);
if (!xmlString.includes('PlayReadyKeyMessage')) {
// This does not appear to be a wrapped message as on Edge. Some
// clients do not need this unwrapping, so we will assume this is one of
// them. Note that "xml" at this point probably looks like random
// garbage, since we interpreted UTF-8 as UTF-16.
xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
return licenseChallenge;
}
const keyMessageXml = new DOMParser().parseFromString(
xmlString,
'application/xml'
);
// Set request headers.
const headers = keyMessageXml.querySelectorAll('HttpHeader');
if (headers.length > 0) {
let header: Element;
for (let i = 0, len = headers.length; i < len; i++) {
header = headers[i];
const name = header.querySelector('name')?.textContent;
const value = header.querySelector('value')?.textContent;
if (name && value) {
xhr.setRequestHeader(name, value);
}
}
}
const challengeElement = keyMessageXml.querySelector('Challenge');
const challengeText = challengeElement?.textContent;
if (!challengeText) {
throw new Error(`Cannot find <Challenge> in key message`);
}
return strToUtf8array(atob(challengeText));
}

private setupLicenseXHR(
xhr: XMLHttpRequest,
url: string,
Expand Down Expand Up @@ -1117,6 +1163,12 @@ class EMEController implements ComponentAPI {

this.setupLicenseXHR(xhr, url, keySessionContext, licenseChallenge).then(
({ xhr, licenseChallenge }) => {
if (keySessionContext.keySystem == KeySystems.PLAYREADY) {
licenseChallenge = this.unpackPlayReadyKeyMessage(
xhr,
licenseChallenge
);
}
xhr.send(licenseChallenge);
}
);
Expand Down