1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <arpa/inet.h>
17 #include <hdf_base.h>
18 #include <hdf_log.h>
19 #include <string>
20 #include "clearplay_uuid.h"
21 #include "mime_type.h"
22 #include "base64_utils.h"
23 #include "data_parser.h"
24 #include "cJSON.h"
25 #include "securec.h"
26
27 #define HDF_LOG_TAG data_parse
28
29 namespace OHOS {
30 namespace HDI {
31 namespace Drm {
32 namespace V1_0 {
ParsePssh(const std::vector<uint8_t> & initData,std::vector<std::vector<uint8_t>> & keyIds)33 int32_t ParsePssh(const std::vector<uint8_t> &initData, std::vector<std::vector<uint8_t>> &keyIds)
34 {
35 HDF_LOGD("%{public}s: start", __func__);
36 size_t readPosition = 0;
37
38 // Validate size field
39 uint32_t expectedSize = initData.size();
40 expectedSize = htonl(expectedSize);
41 if (memcmp(&initData[readPosition], &expectedSize, sizeof(expectedSize)) != 0) {
42 HDF_LOGD("%{public}s: memcmp(&initData[readPosition], &expectedSize, sizeof(expectedSize)) != 0", __func__);
43 }
44 readPosition += sizeof(expectedSize);
45
46 // Validate PSSH box identifier
47 const char psshIdentifier[4] = {'p', 's', 's', 'h'};
48 if (memcmp(&initData[readPosition], psshIdentifier, sizeof(psshIdentifier)) != 0) {
49 HDF_LOGD("%{public}s: without \"pssh\"", __func__);
50 }
51 readPosition += sizeof(psshIdentifier);
52
53 // Validate EME version number
54 const uint8_t psshVersion1[4] = {1, 0, 0, 0};
55 const uint8_t psshVersion0[4] = {0, 0, 0, 0};
56 int psshVersionId = 0;
57 if (memcmp(&initData[readPosition], psshVersion0, sizeof(psshVersion0)) != 0) {
58 if (memcmp(&initData[readPosition], psshVersion1, sizeof(psshVersion1)) != 0) {
59 HDF_LOGD("%{public}s: psshVersion error", __func__);
60 }
61 psshVersionId = 1;
62 }
63 readPosition += sizeof(psshVersion1);
64
65 // Validate system ID
66 std::string uuid((reinterpret_cast<const char*>(initData.data())) + readPosition, CLEARPLAY_NAME.size());
67 if (IsClearPlayUuid(uuid)) {
68 HDF_LOGD("%{public}s: uuid error", __func__);
69 }
70 readPosition += SYSTEM_ID_SIZE;
71
72 if (psshVersionId == 0) {
73 std::vector<uint8_t> keyIdString = { 'k', 'i', 'd', 's' };
74 int32_t keyIdPos = findSubVector(initData, keyIdString);
75 if (keyIdPos == -1) {
76 HDF_LOGD("%{public}s: without \"kids\"", __func__);
77 }
78 while (keyIdPos < initData.size() && initData[keyIdPos] != '[') {
79 ++keyIdPos;
80 }
81 std::string keyIdBase64 = "";
82 bool isLeft = false;
83 while (keyIdPos < initData.size() && initData[keyIdPos] != ']') {
84 if (initData[keyIdPos] == '"') {
85 isLeft = !isLeft;
86 if (!isLeft) {
87 std::string keyIdString = Decode(keyIdBase64);
88 keyIdBase64 = "";
89 std::vector<uint8_t> keyId(keyIdString.begin(), keyIdString.end());
90 keyIds.push_back(keyId);
91 }
92 } else if (isLeft) {
93 keyIdBase64 += initData[keyIdPos];
94 }
95 ++keyIdPos;
96 }
97 if (keyIdPos == initData.size()) {
98 HDF_LOGD("%{public}s: kids parse error", __func__);
99 }
100 } else if (psshVersionId == 1) {
101 // Read key ID count
102 uint32_t keyIdCount;
103 int32_t ret = HDF_FAILURE;
104 ret = memcpy_s(&keyIdCount, sizeof(keyIdCount), &initData[readPosition], sizeof(keyIdCount));
105 if(ret != 0) {
106 HDF_LOGE("%{public}s: memcpy_s faild", __func__);
107 return HDF_ERR_INVALID_PARAM;
108 }
109 keyIdCount = ntohl(keyIdCount);
110 readPosition += sizeof(keyIdCount);
111
112 // Calculate the key ID offsets
113 for (uint32_t i = 0; i < keyIdCount; ++i) {
114 std::vector<uint8_t> keyId;
115 for (size_t j = 0; i < KEY_ID_SIZE; ++j) {
116 keyId.push_back(initData[readPosition + i * KEY_ID_SIZE + j]);
117 }
118 keyIds.push_back(keyId);
119 }
120 }
121 return HDF_SUCCESS;
122 }
123
generateRequest(const MediaKeyType keyType,const std::vector<std::vector<uint8_t>> & keyIds,std::string * request)124 int32_t generateRequest(const MediaKeyType keyType, const std::vector<std::vector<uint8_t>> &keyIds,
125 std::string *request)
126 {
127 // begin
128 *request = "{\"kids\":[";
129 std::string encodedKeyId;
130 for (size_t i = 0; i < keyIds.size(); ++i) {
131 encodedKeyId.clear();
132 std::string keyId(keyIds[i].begin(), keyIds[i].end());
133 encodedKeyId = Encode(keyId);
134 if (i != 0) {
135 request->append(",");
136 }
137 request->append("\"");
138 request->append(encodedKeyId);
139 request->append("\"");
140 }
141 if (keyType == MEDIA_KEY_TYPE_ONLINE) {
142 request->append("],\"type\":\"temporary\"}");
143 } else if (keyType == MEDIA_KEY_TYPE_OFFLINE) {
144 request->append("],\"type\":\"persistent-license\"}");
145 } else {
146 return HDF_ERR_INVALID_PARAM;
147 }
148 return HDF_SUCCESS;
149 }
150
findSubVector(const std::vector<uint8_t> & main,const std::vector<uint8_t> & sub)151 int32_t findSubVector(const std::vector<uint8_t> &main, const std::vector<uint8_t> &sub)
152 {
153 for (size_t i = 0; i < main.size(); ++i) {
154 size_t j = 0;
155 for (j = 0; j < sub.size() && main[i + j] == sub[j]; ++j) {
156 }
157 // for j end
158 if (j == sub.size()) {
159 return i;
160 }
161 }
162 return -1;
163 }
164 } // V1_0
165 } // Drm
166 } // HDI
167 } // OHOS