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