// 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_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);

// 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);

// Create a license request message for the |key_id| and |session_type|
// specified. Currently ClearKey generates a message for each key individually,
// so no need to take a list of |key_id|'s. |license| is updated to contain the
// resulting JSON string.
MEDIA_EXPORT void CreateLicenseRequest(const uint8* key_id,
                                       int key_id_length,
                                       MediaKeys::SessionType session_type,
                                       std::vector<uint8>* license);

// 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_
