1 /*
2 * Copyright (C) 2021 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 //#define LOG_NDEBUG 0
17 #define LOG_TAG "clearkey-InitDataParser"
18
19 #include <algorithm>
20 #include <arpa/inet.h>
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
30 namespace {
31 const size_t kKeyIdSize = 16;
32 const size_t kSystemIdSize = 16;
33 } // namespace
34
35 namespace clearkeydrm {
36
StrToVector(const std::string & str)37 std::vector<uint8_t> StrToVector(const std::string& str) {
38 std::vector<uint8_t> vec(str.begin(), str.end());
39 return vec;
40 }
41
parse(const std::vector<uint8_t> & initData,const std::string & mimeType,CdmKeyType keyType,std::vector<uint8_t> * licenseRequest)42 CdmResponseType InitDataParser::parse(const std::vector<uint8_t>& initData,
43 const std::string& mimeType,
44 CdmKeyType keyType,
45 std::vector<uint8_t>* licenseRequest) {
46 // Build a list of the key IDs
47 std::vector<const uint8_t*> keyIds;
48
49 if (mimeType == kIsoBmffVideoMimeType.c_str() || mimeType == kIsoBmffAudioMimeType.c_str() ||
50 mimeType == kCencInitDataFormat.c_str()) {
51 auto res = parsePssh(initData, &keyIds);
52 if (res != clearkeydrm::OK) {
53 return res;
54 }
55 } else if (mimeType == kWebmVideoMimeType.c_str() || mimeType == kWebmAudioMimeType.c_str() ||
56 mimeType == kWebmInitDataFormat.c_str()) {
57 // WebM "init data" is just a single key ID
58 if (initData.size() != kKeyIdSize) {
59 return clearkeydrm::ERROR_CANNOT_HANDLE;
60 }
61 keyIds.push_back(initData.data());
62 } else {
63 return clearkeydrm::ERROR_CANNOT_HANDLE;
64 }
65
66 if (keyType == clearkeydrm::KEY_TYPE_RELEASE) {
67 // restore key
68 }
69
70 // Build the request
71 std::string requestJson = generateRequest(keyType, keyIds);
72 std::vector<uint8_t> requestJsonVec = StrToVector(requestJson);
73
74 licenseRequest->clear();
75 licenseRequest->insert(licenseRequest->end(), requestJsonVec.begin(), requestJsonVec.end());
76 return clearkeydrm::OK;
77 }
78
parsePssh(const std::vector<uint8_t> & initData,std::vector<const uint8_t * > * keyIds)79 CdmResponseType InitDataParser::parsePssh(const std::vector<uint8_t>& initData,
80 std::vector<const uint8_t*>* keyIds) {
81 // Description of PSSH format:
82 // https://w3c.github.io/encrypted-media/format-registry/initdata/cenc.html
83 size_t readPosition = 0;
84
85 uint32_t expectedSize = initData.size();
86 const char psshIdentifier[4] = {'p', 's', 's', 'h'};
87 const uint8_t psshVersion1[4] = {1, 0, 0, 0};
88 uint32_t keyIdCount = 0;
89 size_t headerSize = sizeof(expectedSize) + sizeof(psshIdentifier) + sizeof(psshVersion1) +
90 kSystemIdSize + sizeof(keyIdCount);
91 if (initData.size() < headerSize) {
92 return clearkeydrm::ERROR_CANNOT_HANDLE;
93 }
94
95 // Validate size field
96 expectedSize = htonl(expectedSize);
97 if (memcmp(&initData[readPosition], &expectedSize, sizeof(expectedSize)) != 0) {
98 return clearkeydrm::ERROR_CANNOT_HANDLE;
99 }
100 readPosition += sizeof(expectedSize);
101
102 // Validate PSSH box identifier
103 if (memcmp(&initData[readPosition], psshIdentifier, sizeof(psshIdentifier)) != 0) {
104 return clearkeydrm::ERROR_CANNOT_HANDLE;
105 }
106 readPosition += sizeof(psshIdentifier);
107
108 // Validate EME version number
109 if (memcmp(&initData[readPosition], psshVersion1, sizeof(psshVersion1)) != 0) {
110 return clearkeydrm::ERROR_CANNOT_HANDLE;
111 }
112 readPosition += sizeof(psshVersion1);
113
114 // Validate system ID
115 if (!isClearKeyUUID(&initData[readPosition])) {
116 return clearkeydrm::ERROR_CANNOT_HANDLE;
117 }
118 readPosition += kSystemIdSize;
119
120 // Read key ID count
121 memcpy(&keyIdCount, &initData[readPosition], sizeof(keyIdCount));
122 keyIdCount = ntohl(keyIdCount);
123 readPosition += sizeof(keyIdCount);
124
125 uint64_t psshSize = 0;
126 if (__builtin_mul_overflow(keyIdCount, kKeyIdSize, &psshSize) ||
127 __builtin_add_overflow(readPosition, psshSize, &psshSize) ||
128 psshSize != initData.size() - sizeof(uint32_t) /* DataSize(0) */) {
129 return clearkeydrm::ERROR_CANNOT_HANDLE;
130 }
131
132 // Calculate the key ID offsets
133 for (uint32_t i = 0; i < keyIdCount; ++i) {
134 size_t keyIdPosition = readPosition + (i * kKeyIdSize);
135 keyIds->push_back(&initData[keyIdPosition]);
136 }
137 return clearkeydrm::OK;
138 }
139
generateRequest(CdmKeyType keyType,const std::vector<const uint8_t * > & keyIds)140 std::string InitDataParser::generateRequest(CdmKeyType keyType,
141 const std::vector<const uint8_t*>& keyIds) {
142 const std::string kRequestPrefix("{\"kids\":[");
143 const std::string kTemporarySession("],\"type\":\"temporary\"}");
144 const std::string kPersistentSession("],\"type\":\"persistent-license\"}");
145
146 std::string request(kRequestPrefix);
147 std::string encodedId;
148 for (size_t i = 0; i < keyIds.size(); ++i) {
149 encodedId.clear();
150 encodeBase64Url(keyIds[i], kKeyIdSize, &encodedId);
151 if (i != 0) {
152 request.append(",");
153 }
154 request.push_back('\"');
155 request.append(encodedId);
156 request.push_back('\"');
157 }
158 if (keyType == clearkeydrm::KEY_TYPE_STREAMING) {
159 request.append(kTemporarySession);
160 } else if (keyType == clearkeydrm::KEY_TYPE_OFFLINE ||
161 keyType == clearkeydrm::KEY_TYPE_RELEASE) {
162 request.append(kPersistentSession);
163 }
164
165 // Android's Base64 encoder produces padding. EME forbids padding.
166 const char kBase64Padding = '=';
167 request.erase(std::remove(request.begin(), request.end(), kBase64Padding), request.end());
168
169 return request;
170 }
171
172 } // namespace clearkeydrm
173