1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "hidl_InitDataParser"
19
20 #include <algorithm>
21 #include <utils/Log.h>
22
23 #include "InitDataParser.h"
24
25 #include "Base64.h"
26
27 #include "ClearKeyUUID.h"
28 #include "MimeType.h"
29 #include "Utils.h"
30
31 namespace android {
32 namespace hardware {
33 namespace drm {
34 namespace V1_4 {
35 namespace clearkey {
36
37 namespace {
38 const size_t kKeyIdSize = 16;
39 const size_t kSystemIdSize = 16;
40 }
41
StrToVector(const std::string & str)42 std::vector<uint8_t> StrToVector(const std::string& str) {
43 std::vector<uint8_t> vec(str.begin(), str.end());
44 return vec;
45 }
46
parse(const std::vector<uint8_t> & initData,const std::string & mimeType,V1_0::KeyType keyType,std::vector<uint8_t> * licenseRequest)47 Status InitDataParser::parse(const std::vector<uint8_t>& initData,
48 const std::string& mimeType,
49 V1_0::KeyType keyType,
50 std::vector<uint8_t>* licenseRequest) {
51 // Build a list of the key IDs
52 std::vector<const uint8_t*> keyIds;
53
54 if (mimeType == kIsoBmffVideoMimeType.c_str() ||
55 mimeType == kIsoBmffAudioMimeType.c_str() ||
56 mimeType == kCencInitDataFormat.c_str()) {
57 Status res = parsePssh(initData, &keyIds);
58 if (res != Status::OK) {
59 return res;
60 }
61 } else if (mimeType == kWebmVideoMimeType.c_str() ||
62 mimeType == kWebmAudioMimeType.c_str() ||
63 mimeType == kWebmInitDataFormat.c_str()) {
64 // WebM "init data" is just a single key ID
65 if (initData.size() != kKeyIdSize) {
66 return Status::ERROR_DRM_CANNOT_HANDLE;
67 }
68 keyIds.push_back(initData.data());
69 } else {
70 return Status::ERROR_DRM_CANNOT_HANDLE;
71 }
72
73 if (keyType == V1_0::KeyType::RELEASE) {
74 // restore key
75 }
76
77 // Build the request
78 std::string requestJson = generateRequest(keyType, keyIds);
79 std::vector<uint8_t> requestJsonVec = StrToVector(requestJson);
80
81 licenseRequest->clear();
82 licenseRequest->insert(licenseRequest->end(), requestJsonVec.begin(), requestJsonVec.end());
83 return Status::OK;
84 }
85
parsePssh(const std::vector<uint8_t> & initData,std::vector<const uint8_t * > * keyIds)86 Status InitDataParser::parsePssh(const std::vector<uint8_t>& initData,
87 std::vector<const uint8_t*>* keyIds) {
88 // Description of PSSH format:
89 // https://w3c.github.io/encrypted-media/format-registry/initdata/cenc.html
90 size_t readPosition = 0;
91
92 uint32_t expectedSize = initData.size();
93 const char psshIdentifier[4] = {'p', 's', 's', 'h'};
94 const uint8_t psshVersion1[4] = {1, 0, 0, 0};
95 uint32_t keyIdCount = 0;
96 size_t headerSize = sizeof(expectedSize) + sizeof(psshIdentifier) +
97 sizeof(psshVersion1) + kSystemIdSize + sizeof(keyIdCount);
98 if (initData.size() < headerSize) {
99 return Status::ERROR_DRM_CANNOT_HANDLE;
100 }
101
102 // Validate size field
103 expectedSize = htonl(expectedSize);
104 if (memcmp(&initData[readPosition], &expectedSize,
105 sizeof(expectedSize)) != 0) {
106 return Status::ERROR_DRM_CANNOT_HANDLE;
107 }
108 readPosition += sizeof(expectedSize);
109
110 // Validate PSSH box identifier
111 if (memcmp(&initData[readPosition], psshIdentifier,
112 sizeof(psshIdentifier)) != 0) {
113 return Status::ERROR_DRM_CANNOT_HANDLE;
114 }
115 readPosition += sizeof(psshIdentifier);
116
117 // Validate EME version number
118 if (memcmp(&initData[readPosition], psshVersion1,
119 sizeof(psshVersion1)) != 0) {
120 return Status::ERROR_DRM_CANNOT_HANDLE;
121 }
122 readPosition += sizeof(psshVersion1);
123
124 // Validate system ID
125 if (!clearkeydrm::isClearKeyUUID(&initData[readPosition])) {
126 return Status::ERROR_DRM_CANNOT_HANDLE;
127 }
128 readPosition += kSystemIdSize;
129
130 // Read key ID count
131 memcpy(&keyIdCount, &initData[readPosition], sizeof(keyIdCount));
132 keyIdCount = ntohl(keyIdCount);
133 readPosition += sizeof(keyIdCount);
134
135 uint64_t psshSize = 0;
136 if (__builtin_mul_overflow(keyIdCount, kKeyIdSize, &psshSize) ||
137 __builtin_add_overflow(readPosition, psshSize, &psshSize) ||
138 psshSize != initData.size() - sizeof(uint32_t) /* DataSize(0) */) {
139 return Status::ERROR_DRM_CANNOT_HANDLE;
140 }
141
142 // Calculate the key ID offsets
143 for (uint32_t i = 0; i < keyIdCount; ++i) {
144 size_t keyIdPosition = readPosition + (i * kKeyIdSize);
145 keyIds->push_back(&initData[keyIdPosition]);
146 }
147 return Status::OK;
148 }
149
generateRequest(V1_0::KeyType keyType,const std::vector<const uint8_t * > & keyIds)150 std::string InitDataParser::generateRequest(V1_0::KeyType keyType,
151 const std::vector<const uint8_t*>& keyIds) {
152 const std::string kRequestPrefix("{\"kids\":[");
153 const std::string kTemporarySession("],\"type\":\"temporary\"}");
154 const std::string kPersistentSession("],\"type\":\"persistent-license\"}");
155
156 std::string request(kRequestPrefix);
157 std::string encodedId;
158 for (size_t i = 0; i < keyIds.size(); ++i) {
159 encodedId.clear();
160 encodeBase64Url(keyIds[i], kKeyIdSize, &encodedId);
161 if (i != 0) {
162 request.append(",");
163 }
164 request.push_back('\"');
165 request.append(encodedId);
166 request.push_back('\"');
167 }
168 if (keyType == V1_0::KeyType::STREAMING) {
169 request.append(kTemporarySession);
170 } else if (keyType == V1_0::KeyType::OFFLINE ||
171 keyType == V1_0::KeyType::RELEASE) {
172 request.append(kPersistentSession);
173 }
174
175 // Android's Base64 encoder produces padding. EME forbids padding.
176 const char kBase64Padding = '=';
177 request.erase(std::remove(request.begin(), request.end(), kBase64Padding), request.end());
178
179 return request;
180 }
181
182 } // namespace clearkey
183 } // namespace V1_4
184 } // namespace drm
185 } // namespace hardware
186 } // namespace android
187