json_web_key.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_CDM_JSON_WEB_KEY_H_
  5. #define MEDIA_CDM_JSON_WEB_KEY_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "media/base/media_export.h"
  11. namespace media {
  12. // The ClearKey license request format is a JSON object containing the following
  13. // members (http://w3c.github.io/encrypted-media/#clear-key-request-format):
  14. // "kids" : An array of key IDs. Each element of the array is the base64url
  15. // encoding of the octet sequence containing the key ID value.
  16. // "type" : The requested MediaKeySessionType.
  17. // An example:
  18. // { "kids":["67ef0gd8pvfd0","77ef0gd8pvfd0"], "type":"temporary" }
  19. // The ClearKey license format is a JSON Web Key (JWK) Set containing
  20. // representation of the symmetric key to be used for decryption.
  21. // (http://w3c.github.io/encrypted-media/#clear-key-license-format)
  22. // For each JWK in the set, the parameter values are as follows:
  23. // "kty" (key type) : "oct" (octet sequence)
  24. // "alg" (algorithm) : "A128KW" (AES key wrap using a 128-bit key)
  25. // "k" (key value) : The base64url encoding of the octet sequence
  26. // containing the symmetric key value.
  27. // "kid" (key ID) : The base64url encoding of the octet sequence
  28. // containing the key ID value.
  29. // The JSON object may have an optional "type" member value, which may be
  30. // any of the SessionType values. If not specified, the default value of
  31. // "temporary" is used.
  32. // A JSON Web Key Set looks like the following in JSON:
  33. // { "keys": [ JWK1, JWK2, ... ], "type":"temporary" }
  34. // A symmetric keys JWK looks like the following in JSON:
  35. // { "kty":"oct",
  36. // "alg":"A128KW",
  37. // "kid":"AQIDBAUGBwgJCgsMDQ4PEA",
  38. // "k":"FBUWFxgZGhscHR4fICEiIw" }
  39. // There may be other properties specified, but they are ignored.
  40. // Ref: http://tools.ietf.org/html/draft-ietf-jose-json-web-key and:
  41. // http://tools.ietf.org/html/draft-jones-jose-json-private-and-symmetric-key
  42. enum class CdmSessionType;
  43. // Vector of key IDs.
  44. typedef std::vector<std::vector<uint8_t>> KeyIdList;
  45. // Vector of [key_id, key_value] pairs. Values are raw binary data, stored in
  46. // strings for convenience.
  47. typedef std::pair<std::string, std::string> KeyIdAndKeyPair;
  48. typedef std::vector<KeyIdAndKeyPair> KeyIdAndKeyPairs;
  49. // Converts a single |key|, |key_id| pair to a JSON Web Key Set.
  50. MEDIA_EXPORT std::string GenerateJWKSet(const uint8_t* key,
  51. int key_length,
  52. const uint8_t* key_id,
  53. int key_id_length);
  54. // Converts a set of |key|, |key_id| pairs to a JSON Web Key Set.
  55. MEDIA_EXPORT std::string GenerateJWKSet(const KeyIdAndKeyPairs& keys,
  56. CdmSessionType session_type);
  57. // Extracts the JSON Web Keys from a JSON Web Key Set. If |input| looks like
  58. // a valid JWK Set, then true is returned and |keys| and |session_type| are
  59. // updated to contain the values found. Otherwise return false.
  60. MEDIA_EXPORT bool ExtractKeysFromJWKSet(const std::string& jwk_set,
  61. KeyIdAndKeyPairs* keys,
  62. CdmSessionType* session_type);
  63. // Extracts the Key Ids from a Key IDs Initialization Data
  64. // (https://w3c.github.io/encrypted-media/keyids-format.html). If |input| looks
  65. // valid, then true is returned and |key_ids| is updated to contain the values
  66. // found. Otherwise return false and |error_message| contains the reason.
  67. MEDIA_EXPORT bool ExtractKeyIdsFromKeyIdsInitData(const std::string& input,
  68. KeyIdList* key_ids,
  69. std::string* error_message);
  70. // Creates a license request message for the |key_ids| and |session_type|
  71. // specified. |license| is updated to contain the resulting JSON string.
  72. MEDIA_EXPORT void CreateLicenseRequest(const KeyIdList& key_ids,
  73. CdmSessionType session_type,
  74. std::vector<uint8_t>* license);
  75. // Creates a keyIDs init_data message for the |key_ids| specified.
  76. // |key_ids_init_data| is updated to contain the resulting JSON string.
  77. MEDIA_EXPORT void CreateKeyIdsInitData(const KeyIdList& key_ids,
  78. std::vector<uint8_t>* key_ids_init_data);
  79. MEDIA_EXPORT std::vector<uint8_t> CreateLicenseReleaseMessage(
  80. const KeyIdList& key_ids);
  81. // Extract the first key from the license request message. Returns true if
  82. // |license| is a valid license request and contains at least one key,
  83. // otherwise false and |first_key| is not touched.
  84. MEDIA_EXPORT bool ExtractFirstKeyIdFromLicenseRequest(
  85. const std::vector<uint8_t>& license,
  86. std::vector<uint8_t>* first_key);
  87. } // namespace media
  88. #endif // MEDIA_CDM_JSON_WEB_KEY_H_