RDK Documentation (Open Sourced RDK Components)
AampDRMutils.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 2018 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 AampDRMutils.cpp
23  * @brief DataStructures and methods for DRM license acquisition
24  */
25 
26 #include "AampDRMutils.h"
27 #include "AampConfig.h"
28 #include "_base64.h"
29 
30 #include <uuid/uuid.h>
31 #include <regex>
32 
33 #define KEYID_TAG_START "<KID>"
34 #define KEYID_TAG_END "</KID>"
35 
36 /* Regex to detect if a string starts with a protocol definition e.g. http:// */
37 static const std::string PROTOCOL_REGEX = "^[a-zA-Z0-9\\+\\.-]+://";
38 #define KEY_ID_SZE_INDICATOR 0x12
39 
40 /**
41  * @brief Default constructor for DrmData.
42  * NULL initialize data and dataLength.
43  */
44 DrmData::DrmData() : data("")
45 {
46 }
47 
48 /**
49  * @brief Constructor for DrmData
50  * allocate memory and initialize data and
51  * dataLength with given params.
52  *
53  */
54 DrmData::DrmData(unsigned char *data, int dataLength) : data("")
55 {
56  this->data.assign(reinterpret_cast<const char*>(data),dataLength);
57 }
58 
59 /**
60  * @brief Distructor for DrmData.
61  * Free memory (if any) allocated for data.
62  */
64 {
65  if(!data.empty())
66  {
67  data.clear();
68  }
69 }
70 
71 /**
72  * @brief Getter method for data.
73  *
74  */
75 const std::string &DrmData::getData()
76 {
77  return data;
78 }
79 
80 /**
81  * @brief Getter method for dataLength.
82  */
84 {
85  return (this->data.length());
86 }
87 
88 /**
89  * @brief Updates DrmData with given data.
90  */
91 void DrmData::setData(unsigned char *data, int dataLength)
92 {
93  if(!this->data.empty())
94  {
95  this->data.clear();
96  }
97  this->data.assign(reinterpret_cast<const char*>(data),dataLength);
98 }
99 
100 /**
101  * @brief Appends DrmData with given data.
102  */
103 void DrmData::addData(unsigned char *data, int dataLength)
104 {
105  if(this->data.empty())
106  {
107  this->setData(data,dataLength);
108  }
109  else
110  {
111  std::string key;
112  key.assign(reinterpret_cast<const char*>(data),dataLength);
113  this->data = this->data + key;
114  }
115 }
116 
117 /**
118  * @brief Swap the bytes at given positions.
119  *
120  * @param[out] bytes - Pointer to byte block where swapping is done.
121  * @param[in] pos1, pos2 - Swap positions.
122  * @return void.
123  */
124 static void aamp_SwapBytes(unsigned char *bytes, int pos1, int pos2)
125 {
126  unsigned char temp = bytes[pos1];
127  bytes[pos1] = bytes[pos2];
128  bytes[pos2] = temp;
129 }
130 
131 /**
132  * @brief Convert endianness of 16 byte block.
133  */
134 void aamp_ConvertEndianness(unsigned char *original, unsigned char *guidBytes)
135 {
136  memcpy(guidBytes, original, 16);
137  aamp_SwapBytes(guidBytes, 0, 3);
138  aamp_SwapBytes(guidBytes, 1, 2);
139  aamp_SwapBytes(guidBytes, 4, 5);
140  aamp_SwapBytes(guidBytes, 6, 7);
141 }
142 
143 /**
144  * @brief Extract WideVine content meta data from DRM
145  * Agnostic PSSH header. Might not work with WideVine PSSH header
146  *
147  */
148 std::string aamp_ExtractWVContentMetadataFromPssh(const char* psshData, int dataLength)
149 {
150  //WV PSSH format 4+4+4+16(system id)+4(data size)
151  uint32_t header = 28;
152  unsigned char* content_id = NULL;
153  std::string metadata;
154  uint32_t content_id_size =
155  (uint32_t)((psshData[header] & 0x000000FFu) << 24 |
156  (psshData[header+1] & 0x000000FFu) << 16 |
157  (psshData[header+2] & 0x000000FFu) << 8 |
158  (psshData[header+3] & 0x000000FFu));
159 
160  AAMPLOG_INFO("content meta data length : %d",content_id_size);
161  if ((header + 4 + content_id_size) <= dataLength)
162  {
163  metadata = std::string(psshData + header + 4, content_id_size);
164  }
165  else
166  {
167  AAMPLOG_WARN("psshData : %d bytes in length, metadata would read past end of buffer", dataLength);
168  }
169 
170  return metadata;
171 }
172 //End of special for Widevine
173 
174 /**
175  * @brief Get the base URI of a resource
176  *
177  * @param uri URI of a resource
178  * @param originOnly if true, only the domain (and port, if present) is returned.
179  * if false, the full parent path of the resource is returned
180  * @return base URI
181  */
182 static std::string aamp_getBaseUri(std::string uri, bool originOnly)
183 {
184  std::smatch results;
185  if (std::regex_match(uri, results, std::regex("(" + PROTOCOL_REGEX + "[^/]+)/.*")))
186  {
187  return originOnly ? results[1].str() : uri.substr(0, uri.rfind("/"));
188  }
189 
190  return uri;
191 }
192 
DrmData::addData
void addData(unsigned char *data, int dataLength)
Appends DrmData with given data.
Definition: AampDRMutils.cpp:103
DrmData::setData
void setData(unsigned char *data, int dataLength)
Updates DrmData with given data.
Definition: AampDRMutils.cpp:91
aamp_ConvertEndianness
void aamp_ConvertEndianness(unsigned char *original, unsigned char *guidBytes)
Convert endianness of 16 byte block.
Definition: AampDRMutils.cpp:134
DrmData::getDataLength
int getDataLength()
Getter method for dataLength.
Definition: AampDRMutils.cpp:83
_base64.h
base64 source Encoder/Decoder
aamp_SwapBytes
static void aamp_SwapBytes(unsigned char *bytes, int pos1, int pos2)
Swap the bytes at given positions.
Definition: AampDRMutils.cpp:124
AampDRMutils.h
Context-free common utility functions.
AampConfig.h
Configurations for AAMP.
aamp_getBaseUri
static std::string aamp_getBaseUri(std::string uri, bool originOnly)
Get the base URI of a resource.
Definition: AampDRMutils.cpp:182
DrmData::data
std::string data
Definition: AampDrmData.h:35
DrmData::~DrmData
~DrmData()
Distructor for DrmData. Free memory (if any) allocated for data.
Definition: AampDRMutils.cpp:63
DrmData::getData
const std::string & getData()
Getter method for data.
Definition: AampDRMutils.cpp:75
DrmData::DrmData
DrmData()
Default constructor for DrmData. NULL initialize data and dataLength.
Definition: AampDRMutils.cpp:44
aamp_ExtractWVContentMetadataFromPssh
std::string aamp_ExtractWVContentMetadataFromPssh(const char *psshData, int dataLength)
Extract WideVine content meta data from DRM Agnostic PSSH header. Might not work with WideVine PSSH h...
Definition: AampDRMutils.cpp:148