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_2 {
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 size_t readPosition = 0;
89
90 // Validate size field
91 uint32_t expectedSize = initData.size();
92 expectedSize = htonl(expectedSize);
93 if (memcmp(&initData[readPosition], &expectedSize,
94 sizeof(expectedSize)) != 0) {
95 return Status::ERROR_DRM_CANNOT_HANDLE;
96 }
97 readPosition += sizeof(expectedSize);
98
99 // Validate PSSH box identifier
100 const char psshIdentifier[4] = {'p', 's', 's', 'h'};
101 if (memcmp(&initData[readPosition], psshIdentifier,
102 sizeof(psshIdentifier)) != 0) {
103 return Status::ERROR_DRM_CANNOT_HANDLE;
104 }
105 readPosition += sizeof(psshIdentifier);
106
107 // Validate EME version number
108 const uint8_t psshVersion1[4] = {1, 0, 0, 0};
109 if (memcmp(&initData[readPosition], psshVersion1,
110 sizeof(psshVersion1)) != 0) {
111 return Status::ERROR_DRM_CANNOT_HANDLE;
112 }
113 readPosition += sizeof(psshVersion1);
114
115 // Validate system ID
116 if (!clearkeydrm::isClearKeyUUID(&initData[readPosition])) {
117 return Status::ERROR_DRM_CANNOT_HANDLE;
118 }
119 readPosition += kSystemIdSize;
120
121 // Read key ID count
122 uint32_t keyIdCount;
123 memcpy(&keyIdCount, &initData[readPosition], sizeof(keyIdCount));
124 keyIdCount = ntohl(keyIdCount);
125 readPosition += sizeof(keyIdCount);
126 if (readPosition + ((uint64_t)keyIdCount * kKeyIdSize) !=
127 initData.size() - sizeof(uint32_t)) {
128 return Status::ERROR_DRM_CANNOT_HANDLE;
129 }
130
131 // Calculate the key ID offsets
132 for (uint32_t i = 0; i < keyIdCount; ++i) {
133 size_t keyIdPosition = readPosition + (i * kKeyIdSize);
134 keyIds->push_back(&initData[keyIdPosition]);
135 }
136 return Status::OK;
137 }
138
generateRequest(V1_0::KeyType keyType,const std::vector<const uint8_t * > & keyIds)139 std::string InitDataParser::generateRequest(V1_0::KeyType keyType,
140 const std::vector<const uint8_t*>& keyIds) {
141 const std::string kRequestPrefix("{\"kids\":[");
142 const std::string kTemporarySession("],\"type\":\"temporary\"}");
143 const std::string kPersistentSession("],\"type\":\"persistent-license\"}");
144
145 std::string request(kRequestPrefix);
146 std::string encodedId;
147 for (size_t i = 0; i < keyIds.size(); ++i) {
148 encodedId.clear();
149 encodeBase64Url(keyIds[i], kKeyIdSize, &encodedId);
150 if (i != 0) {
151 request.append(",");
152 }
153 request.push_back('\"');
154 request.append(encodedId);
155 request.push_back('\"');
156 }
157 if (keyType == V1_0::KeyType::STREAMING) {
158 request.append(kTemporarySession);
159 } else if (keyType == V1_0::KeyType::OFFLINE ||
160 keyType == V1_0::KeyType::RELEASE) {
161 request.append(kPersistentSession);
162 }
163
164 // Android's Base64 encoder produces padding. EME forbids padding.
165 const char kBase64Padding = '=';
166 request.erase(std::remove(request.begin(), request.end(), kBase64Padding), request.end());
167
168 return request;
169 }
170
171 } // namespace clearkey
172 } // namespace V1_2
173 } // namespace drm
174 } // namespace hardware
175 } // namespace android
176