summaryrefslogtreecommitdiffstats
path: root/media/cdm/json_web_key.h
blob: 8502ad92936d18aad8e1cb187b101c19df81905a (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_CDM_JSON_WEB_KEY_H_
#define MEDIA_CDM_JSON_WEB_KEY_H_

#include <string>
#include <utility>
#include <vector>

#include "base/basictypes.h"
#include "media/base/media_export.h"
#include "media/base/media_keys.h"

namespace media {

// The ClearKey license request format (ref:
// https://dvcs.w3.org/hg/html-media/raw-file/default/encrypted-media/encrypted-media.html#clear-key)
// is a JSON object containing the following members:
//   "kids" : An array of key IDs. Each element of the array is the base64url
//            encoding of the octet sequence containing the key ID value.
//   "type" : The requested SessionType.
// An example:
//   { "kids":["67ef0gd8pvfd0","77ef0gd8pvfd0"], "type":"temporary" }

// The ClearKey license format is a JSON Web Key (JWK) Set containing
// representation of the symmetric key to be used for decryption.
// For each JWK in the set, the parameter values are as follows:
//   "kty" (key type)  : "oct" (octet sequence)
//   "alg" (algorithm) : "A128KW" (AES key wrap using a 128-bit key)
//   "k" (key value)   : The base64url encoding of the octet sequence
//                       containing the symmetric key value.
//   "kid" (key ID)    : The base64url encoding of the octet sequence
//                       containing the key ID value.
// The JSON object may have an optional "type" member value, which may be
// any of the SessionType values. If not specified, the default value of
// "temporary" is used.
// A JSON Web Key Set looks like the following in JSON:
//   { "keys": [ JWK1, JWK2, ... ], "type":"temporary" }
// A symmetric keys JWK looks like the following in JSON:
//   { "kty":"oct",
//     "alg":"A128KW",
//     "kid":"AQIDBAUGBwgJCgsMDQ4PEA",
//     "k":"FBUWFxgZGhscHR4fICEiIw" }

// There may be other properties specified, but they are ignored.
// Ref: http://tools.ietf.org/html/draft-ietf-jose-json-web-key and:
// http://tools.ietf.org/html/draft-jones-jose-json-private-and-symmetric-key

// Vector of key IDs.
typedef std::vector<std::vector<uint8>> KeyIdList;

// Vector of [key_id, key_value] pairs. Values are raw binary data, stored in
// strings for convenience.
typedef std::pair<std::string, std::string> KeyIdAndKeyPair;
typedef std::vector<KeyIdAndKeyPair> KeyIdAndKeyPairs;

// Converts a single |key|, |key_id| pair to a JSON Web Key Set.
MEDIA_EXPORT std::string GenerateJWKSet(const uint8* key, int key_length,
                                        const uint8* key_id, int key_id_length);

// Converts a set of |key|, |key_id| pairs to a JSON Web Key Set.
MEDIA_EXPORT std::string GenerateJWKSet(const KeyIdAndKeyPairs& keys,
                                        MediaKeys::SessionType session_type);

// Extracts the JSON Web Keys from a JSON Web Key Set. If |input| looks like
// a valid JWK Set, then true is returned and |keys| and |session_type| are
// updated to contain the values found. Otherwise return false.
MEDIA_EXPORT bool ExtractKeysFromJWKSet(const std::string& jwk_set,
                                        KeyIdAndKeyPairs* keys,
                                        MediaKeys::SessionType* session_type);

// Extracts the Key Ids from a Key IDs Initialization Data
// (https://w3c.github.io/encrypted-media/keyids-format.html). If |input| looks
// valid, then true is returned and |key_ids| is updated to contain the values
// found. Otherwise return false and |error_message| contains the reason.
MEDIA_EXPORT bool ExtractKeyIdsFromKeyIdsInitData(const std::string& input,
                                                  KeyIdList* key_ids,
                                                  std::string* error_message);

// Creates a license request message for the |key_ids| and |session_type|
// specified. |license| is updated to contain the resulting JSON string.
MEDIA_EXPORT void CreateLicenseRequest(const KeyIdList& key_ids,
                                       MediaKeys::SessionType session_type,
                                       std::vector<uint8>* license);

// Creates a keyIDs init_data message for the |key_ids| specified.
// |key_ids_init_data| is updated to contain the resulting JSON string.
MEDIA_EXPORT void CreateKeyIdsInitData(const KeyIdList& key_ids,
                                       std::vector<uint8>* key_ids_init_data);

// Extract the first key from the license request message. Returns true if
// |license| is a valid license request and contains at least one key,
// otherwise false and |first_key| is not touched.
MEDIA_EXPORT bool ExtractFirstKeyIdFromLicenseRequest(
    const std::vector<uint8>& license,
    std::vector<uint8>* first_key);

}  // namespace media

#endif  // MEDIA_CDM_JSON_WEB_KEY_H_