1 /*
2 * Copyright (C) 2014 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 "ClearKeyCryptoPlugin"
19 #include <utils/Log.h>
20
21 #include <endian.h>
22 #include <media/stagefright/foundation/AString.h>
23 #include <media/stagefright/foundation/base64.h>
24 #include <media/stagefright/MediaErrors.h>
25 #include <string.h>
26
27 #include "InitDataParser.h"
28
29 #include "ClearKeyUUID.h"
30 #include "MimeType.h"
31 #include "Utils.h"
32
33 namespace clearkeydrm {
34
35 using android::AString;
36 using android::String8;
37 using android::Vector;
38
39 namespace {
40 const size_t kKeyIdSize = 16;
41 const size_t kSystemIdSize = 16;
42 }
43
parse(const Vector<uint8_t> & initData,const String8 & type,Vector<uint8_t> * licenseRequest)44 android::status_t InitDataParser::parse(const Vector<uint8_t>& initData,
45 const String8& type,
46 Vector<uint8_t>* licenseRequest) {
47 // Build a list of the key IDs
48 Vector<const uint8_t*> keyIds;
49 if (type == kIsoBmffVideoMimeType ||
50 type == kIsoBmffAudioMimeType ||
51 type == kCencInitDataFormat) {
52 android::status_t res = parsePssh(initData, &keyIds);
53 if (res != android::OK) {
54 return res;
55 }
56 } else if (type == kWebmVideoMimeType ||
57 type == kWebmAudioMimeType ||
58 type == kWebmInitDataFormat) {
59 // WebM "init data" is just a single key ID
60 if (initData.size() != kKeyIdSize) {
61 return android::ERROR_DRM_CANNOT_HANDLE;
62 }
63 keyIds.push(initData.array());
64 } else {
65 return android::ERROR_DRM_CANNOT_HANDLE;
66 }
67
68 // Build the request
69 String8 requestJson = generateRequest(keyIds);
70 licenseRequest->clear();
71 licenseRequest->appendArray(
72 reinterpret_cast<const uint8_t*>(requestJson.c_str()),
73 requestJson.size());
74 return android::OK;
75 }
76
parsePssh(const Vector<uint8_t> & initData,Vector<const uint8_t * > * keyIds)77 android::status_t InitDataParser::parsePssh(const Vector<uint8_t>& initData,
78 Vector<const uint8_t*>* keyIds) {
79 // Description of PSSH format:
80 // https://w3c.github.io/encrypted-media/format-registry/initdata/cenc.html
81 size_t readPosition = 0;
82
83 uint32_t expectedSize = initData.size();
84 const char psshIdentifier[4] = {'p', 's', 's', 'h'};
85 const uint8_t psshVersion1[4] = {1, 0, 0, 0};
86 uint32_t keyIdCount = 0;
87 size_t headerSize = sizeof(expectedSize) + sizeof(psshIdentifier) +
88 sizeof(psshVersion1) + kSystemIdSize + sizeof(keyIdCount);
89 if (initData.size() < headerSize) {
90 return android::ERROR_DRM_CANNOT_HANDLE;
91 }
92
93 // Validate size field
94 expectedSize = htonl(expectedSize);
95 if (memcmp(&initData[readPosition], &expectedSize,
96 sizeof(expectedSize)) != 0) {
97 return android::ERROR_DRM_CANNOT_HANDLE;
98 }
99 readPosition += sizeof(expectedSize);
100
101 // Validate PSSH box identifier
102 if (memcmp(&initData[readPosition], psshIdentifier,
103 sizeof(psshIdentifier)) != 0) {
104 return android::ERROR_DRM_CANNOT_HANDLE;
105 }
106 readPosition += sizeof(psshIdentifier);
107
108 // Validate EME version number
109 if (memcmp(&initData[readPosition], psshVersion1,
110 sizeof(psshVersion1)) != 0) {
111 return android::ERROR_DRM_CANNOT_HANDLE;
112 }
113 readPosition += sizeof(psshVersion1);
114
115 // Validate system ID
116 if (!isClearKeyUUID(&initData[readPosition])) {
117 return android::ERROR_DRM_CANNOT_HANDLE;
118 }
119 readPosition += kSystemIdSize;
120
121 // Read key ID count
122 memcpy(&keyIdCount, &initData[readPosition], sizeof(keyIdCount));
123 keyIdCount = ntohl(keyIdCount);
124 readPosition += sizeof(keyIdCount);
125
126 uint64_t psshSize = 0;
127 if (__builtin_mul_overflow(keyIdCount, kKeyIdSize, &psshSize) ||
128 __builtin_add_overflow(readPosition, psshSize, &psshSize) ||
129 psshSize != initData.size() - sizeof(uint32_t) /* DataSize(0) */) {
130 return android::ERROR_DRM_CANNOT_HANDLE;
131 }
132
133 // Calculate the key ID offsets
134 for (uint32_t i = 0; i < keyIdCount; ++i) {
135 size_t keyIdPosition = readPosition + (i * kKeyIdSize);
136 keyIds->push(&initData[keyIdPosition]);
137 }
138 return android::OK;
139 }
140
generateRequest(const Vector<const uint8_t * > & keyIds)141 String8 InitDataParser::generateRequest(const Vector<const uint8_t*>& keyIds) {
142 const String8 kRequestPrefix("{\"kids\":[");
143 const String8 kRequestSuffix("],\"type\":\"temporary\"}");
144 const String8 kBase64Padding("=");
145
146 String8 request(kRequestPrefix);
147 AString encodedId;
148 for (size_t i = 0; i < keyIds.size(); ++i) {
149 encodedId.clear();
150 android::encodeBase64Url(keyIds[i], kKeyIdSize, &encodedId);
151 if (i != 0) {
152 request.append(",");
153 }
154 request.appendFormat("\"%s\"", encodedId.c_str());
155 }
156 request.append(kRequestSuffix);
157
158 // Android's Base64 encoder produces padding. EME forbids padding.
159 request.removeAll(kBase64Padding);
160 return request;
161 }
162
163 } // namespace clearkeydrm
164