summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/encrypted-media/util/clearkey-messagehandler.js
blob: c91a57f6d46cd616cfce09cab6596640fe3883d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Expect utf8decoder and utf8decoder to be TextEncoder('utf-8') and TextDecoder('utf-8') respectively

function MessageHandler( keysystem, content ) {
    this._keysystem = keysystem;
    this._content = content;
    this.messagehandler = MessageHandler.prototype.messagehandler.bind( this );
    this.servercertificate = undefined;
}

MessageHandler.prototype.messagehandler = function messagehandler( messageType, message )
{
    if ( messageType === 'license-request' )
    {
        var request = fromUtf8( message );

        var keys = request.kids.map( function( kid ) {

            var key;
            for( var i=0; i < this._content.keys.length; ++i )
            {
                if ( base64urlEncode( this._content.keys[ i ].kid ) === kid )
                {
                    key = base64urlEncode( this._content.keys[ i ].key );
                    break;
                }
            }

            return { kty: 'oct', kid: kid, k: key };

        }.bind( this ) );

        return Promise.resolve( toUtf8( { keys: keys } ) );
    }
    else if ( messageType === 'license-release' )
    {
        var release = fromUtf8( message );

        // TODO: Check the license release message here

        return Promise.resolve( toUtf8( { kids: release.kids } ) );
    }

    throw new TypeError( 'Unsupported message type for ClearKey' );
};

MessageHandler.prototype.createJWKSet = function createJWKSet(keyId, key) {
    var jwkSet = '{"keys":[';
    for (var i = 0; i < arguments.length; i++) {
        if (i != 0)
            jwkSet += ',';
        jwkSet += arguments[i];
    }
    jwkSet += ']}';
    return jwkSet;
};

MessageHandler.prototype.createJWK = function createJWK(keyId, key) {
    var jwk = '{"kty":"oct","alg":"A128KW","kid":"';
    jwk += base64urlEncode(keyId);
    jwk += '","k":"';
    jwk += base64urlEncode(key);
    jwk += '"}';
    return jwk;
};