RDK Documentation (Open Sourced RDK Components)
AampClearKeyHelper.cpp
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's license file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2020 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 
21 /**
22  * @file AampClearKeyHelper.cpp
23  * @brief Helper functions for Clear key
24  */
25 
26 #include "AampClearKeyHelper.h"
27 #include "AampJsonObject.h"
28 
29 #include "AampConfig.h"
30 #include "AampConstants.h"
31 
32 static AampClearKeyHelperFactory clearkey_helper_factory;
33 
34 const std::string AampClearKeyHelper::CLEARKEY_OCDM_ID = "org.w3.clearkey";
35 
36 const std::string& AampClearKeyHelper::ocdmSystemId() const
37 {
38  return CLEARKEY_OCDM_ID;
39 }
40 
41 void AampClearKeyHelper::createInitData(std::vector<uint8_t>& initData) const
42 {
43  // For DASH the init data should have been extracted from the PSSH
44  // For HLS, we need to construct it
45  if (mDrmInfo.mediaFormat == eMEDIAFORMAT_DASH)
46  {
47  initData = this->mInitData;
48  }
49  else
50  {
51  AampJsonObject jsonInitDataObj;
52  std::vector<std::string> keyIds = {CLEARKEY_KEY_ID};
53 
54  if (jsonInitDataObj.add("kids", keyIds))
55  {
56  jsonInitDataObj.print(initData);
57  }
58  }
59 }
60 
61 bool AampClearKeyHelper::parsePssh(const uint8_t* initData, uint32_t initDataLen)
62 {
63  this->mInitData.assign(initData, initData + initDataLen);
64 
65  mKeyID.assign(initData + CLEARKEY_DASH_KEY_ID_OFFSET, initData + CLEARKEY_DASH_KEY_ID_OFFSET + CLEARKEY_DASH_KEY_ID_LEN);
66 
67  return true;
68 }
69 
70 void AampClearKeyHelper::getKey(std::vector<uint8_t>& keyID) const
71 {
72  // For DASH the key should have been extracted from the PSSH
73  // For HLS, we return a fixed key, which we also place in the init data
74  if (mDrmInfo.mediaFormat == eMEDIAFORMAT_DASH)
75  {
76  keyID = this->mKeyID;
77  }
78  else
79  {
80  keyID.clear();
81  (void)keyID.insert(keyID.begin(), CLEARKEY_KEY_ID.begin(), CLEARKEY_KEY_ID.end());
82  }
83 }
84 
85 void AampClearKeyHelper::generateLicenseRequest(const AampChallengeInfo& challengeInfo, AampLicenseRequest& licenseRequest) const
86 {
87  licenseRequest.method = AampLicenseRequest::POST;
88 
89  if(licenseRequest.url.empty())
90  {
91  if (!mDrmInfo.keyURI.empty())
92  {
93  std::string keyURI;
94  aamp_ResolveURL(keyURI, mDrmInfo.manifestURL, mDrmInfo.keyURI.c_str(), mDrmInfo.bPropagateUriParams);
95  licenseRequest.url = keyURI;
96  }
97  else
98  {
99  licenseRequest.url = challengeInfo.url;
100  }
101  }
102 
103  if (NULL != challengeInfo.data)
104  {
105  licenseRequest.payload = challengeInfo.data->getData();
106  }
107 }
108 
109 void AampClearKeyHelper::transformLicenseResponse(std::shared_ptr<DrmData> licenseResponse) const
110 {
111  // HLS requires the returned key to be transformed into a JWK.
112  // For DASH it will already be in JWK format
113  if ((mDrmInfo.mediaFormat == eMEDIAFORMAT_HLS) ||
114  (mDrmInfo.mediaFormat == eMEDIAFORMAT_HLS_MP4))
115  {
116  std::vector<uint8_t> licenseResponseData(reinterpret_cast<const char*>(licenseResponse->getData().c_str()),
117  reinterpret_cast<const char*>(licenseResponse->getData().c_str()) + licenseResponse->getDataLength());
118 
119  std::vector<uint8_t> keyId(CLEARKEY_KEY_ID.begin(), CLEARKEY_KEY_ID.end());
120 
121  // Construct JSON Web Key (JWK)
122  AampJsonObject keyInstance;
123  keyInstance.add("alg", "cbc"); // Hard coded to cbc for now
124  keyInstance.add("k", licenseResponseData, AampJsonObject::ENCODING_BASE64_URL);
125  keyInstance.add("kid", keyId, AampJsonObject::ENCODING_BASE64_URL);
126 
127  std::vector<AampJsonObject*> values = {&keyInstance};
128 
129  AampJsonObject keyObj;
130  keyObj.add("keys", values);
131  std::string printedJson = keyObj.print();
132 
133  licenseResponse->setData((unsigned char*)printedJson.c_str(), printedJson.length());
134  }
135 }
136 
137 bool AampClearKeyHelperFactory::isDRM(const struct DrmInfo& drmInfo) const
138 {
139  return ((drmInfo.method == eMETHOD_AES_128) &&
140  ((drmInfo.mediaFormat == eMEDIAFORMAT_DASH) ||
141  (drmInfo.mediaFormat == eMEDIAFORMAT_HLS_MP4))
142  );
143 }
144 
145 std::shared_ptr<AampDrmHelper> AampClearKeyHelperFactory::createHelper(const struct DrmInfo& drmInfo, AampLogManager *logObj) const
146 {
147  if (isDRM(drmInfo))
148  {
149  return std::make_shared<AampClearKeyHelper>(drmInfo,logObj);
150  }
151  return NULL;
152 }
153 
154 void AampClearKeyHelperFactory::appendSystemId(std::vector<std::string>& systemIds) const
155 {
156  systemIds.push_back(CLEARKEY_UUID);
157 }
DrmInfo::keyURI
std::string keyURI
Definition: AampDrmInfo.h:87
AampClearKeyHelperFactory::createHelper
std::shared_ptr< AampDrmHelper > createHelper(const struct DrmInfo &drmInfo, AampLogManager *logObj=NULL) const
Build a helper class to support the identified DRM.
Definition: AampClearKeyHelper.cpp:145
DrmInfo::manifestURL
std::string manifestURL
Definition: AampDrmInfo.h:86
AampChallengeInfo::data
std::shared_ptr< DrmData > data
Definition: AampDrmHelper.h:46
eMEDIAFORMAT_DASH
@ eMEDIAFORMAT_DASH
Definition: AampDrmMediaFormat.h:35
AampLogManager
AampLogManager Class.
Definition: AampLogManager.h:150
AampClearKeyHelperFactory::appendSystemId
void appendSystemId(std::vector< std::string > &systemIds) const
Adds the system IDs supported by the DRM to a vector Used by the GStreamer plugins to advertise the D...
Definition: AampClearKeyHelper.cpp:154
AampLicenseRequest
Holds the data to get the License.
Definition: AampDrmHelper.h:57
DrmInfo
DRM information required to decrypt.
Definition: AampDrmInfo.h:47
AampChallengeInfo::url
std::string url
Definition: AampDrmHelper.h:48
AampJsonObject.h
File to handle Json format.
AampClearKeyHelper::transformLicenseResponse
void transformLicenseResponse(std::shared_ptr< DrmData > licenseResponse) const
Transform the license response from the server into the necessary format for OCDM.
Definition: AampClearKeyHelper.cpp:109
AampJsonObject::print
std::string print()
Print the constructed JSON to a string.
Definition: AampJsonObject.cpp:365
AampConfig.h
Configurations for AAMP.
eMETHOD_AES_128
@ eMETHOD_AES_128
Definition: AampDrmInfo.h:39
AampClearKeyHelperFactory
Helper Factory class to maintain Aamp DRM data.
Definition: AampClearKeyHelper.h:84
eMEDIAFORMAT_HLS
@ eMEDIAFORMAT_HLS
Definition: AampDrmMediaFormat.h:34
AampJsonObject::add
bool add(const std::string &name, const std::string &value, const ENCODING encoding=ENCODING_STRING)
Add a string value.
Definition: AampJsonObject.cpp:69
AampLicenseRequest::POST
@ POST
Definition: AampDrmHelper.h:65
DrmInfo::bPropagateUriParams
bool bPropagateUriParams
Definition: AampDrmInfo.h:82
AampConstants.h
Constants in AAMP.
AampClearKeyHelper::ocdmSystemId
const std::string & ocdmSystemId() const
Returns the OCDM system ID of the helper.
Definition: AampClearKeyHelper.cpp:36
AampClearKeyHelperFactory::isDRM
bool isDRM(const struct DrmInfo &drmInfo) const
Determines if a helper class provides the identified DRM.
Definition: AampClearKeyHelper.cpp:137
aamp_ResolveURL
void aamp_ResolveURL(std::string &dst, std::string base, const char *uri, bool bPropagateUriParams)
Resolve file URL from the base and file path.
Definition: AampUtils.cpp:157
AampJsonObject::ENCODING_BASE64_URL
@ ENCODING_BASE64_URL
Definition: AampJsonObject.h:50
eMEDIAFORMAT_HLS_MP4
@ eMEDIAFORMAT_HLS_MP4
Definition: AampDrmMediaFormat.h:37
DrmInfo::method
DrmMethod method
Definition: AampDrmInfo.h:79
AampClearKeyHelper::getKey
void getKey(std::vector< uint8_t > &keyID) const
Get the key ID.
Definition: AampClearKeyHelper.cpp:70
AampClearKeyHelper::generateLicenseRequest
void generateLicenseRequest(const AampChallengeInfo &challengeInfo, AampLicenseRequest &licenseRequest) const
Generate the request details for the DRM license.
Definition: AampClearKeyHelper.cpp:85
AampChallengeInfo
Aamp challenge info to get the License.
Definition: AampDrmHelper.h:44
DrmInfo::mediaFormat
MediaFormat mediaFormat
Definition: AampDrmInfo.h:80
AampClearKeyHelper::parsePssh
bool parsePssh(const uint8_t *initData, uint32_t initDataLen)
Parse the optional PSSH data.
Definition: AampClearKeyHelper.cpp:61
AampClearKeyHelper::createInitData
void createInitData(std::vector< uint8_t > &initData) const
Definition: AampClearKeyHelper.cpp:41
AampJsonObject
Utility class to construct a JSON string.
Definition: AampJsonObject.h:37