1 /*
2 * Copyright (c) 2023-2024 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_LOGE("%{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_LOGE("%{public}s: memcmp(&initData[readPosition], &expectedSize, sizeof(expectedSize)) != 0", __func__);
43 return HDF_ERR_INVALID_PARAM;
44 }
45 readPosition += sizeof(expectedSize);
46
47 // Validate PSSH box identifier
48 const char psshIdentifier[4] = {'p', 's', 's', 'h'};
49 if (memcmp(&initData[readPosition], psshIdentifier, sizeof(psshIdentifier)) != 0) {
50 HDF_LOGE("%{public}s: without \"pssh\"", __func__);
51 return HDF_ERR_INVALID_PARAM;
52 }
53 readPosition += sizeof(psshIdentifier);
54
55 // Validate EME version number
56 const uint8_t psshVersion1[4] = {1, 0, 0, 0};
57 const uint8_t psshVersion0[4] = {0, 0, 0, 0};
58 int psshVersionId = 0;
59 if (memcmp(&initData[readPosition], psshVersion0, sizeof(psshVersion0)) != 0) {
60 if (memcmp(&initData[readPosition], psshVersion1, sizeof(psshVersion1)) != 0) {
61 HDF_LOGE("%{public}s: psshVersion error", __func__);
62 return HDF_ERR_INVALID_PARAM;
63 }
64 psshVersionId = 1;
65 }
66 readPosition += sizeof(psshVersion1);
67
68 // Validate system ID
69 std::string uuid((reinterpret_cast<const char *>(initData.data())) + readPosition, CLEARPLAY_UUID.size());
70 if (IsClearPlayUuid(uuid)) {
71 HDF_LOGE("%{public}s: uuid error", __func__);
72 return HDF_ERR_INVALID_PARAM;
73 }
74 readPosition += SYSTEM_ID_SIZE;
75
76 if (psshVersionId == 0) {
77 std::vector<uint8_t> keyIdString = { 'k', 'i', 'd', 's' };
78 int32_t keyIdPos = findSubVector(initData, keyIdString);
79 if (keyIdPos == -1) {
80 HDF_LOGE("%{public}s: without \"kids\"", __func__);
81 return HDF_ERR_INVALID_PARAM;
82 }
83 while (keyIdPos < initData.size() && initData[keyIdPos] != '[') {
84 ++keyIdPos;
85 }
86 std::string keyIdBase64 = "";
87 bool isLeft = false;
88 while (keyIdPos < initData.size() && initData[keyIdPos] != ']') {
89 if (initData[keyIdPos] == '"') {
90 isLeft = !isLeft;
91 if (!isLeft) {
92 std::string keyIdString = Decode(keyIdBase64);
93 keyIdBase64 = "";
94 std::vector<uint8_t> keyId(keyIdString.begin(), keyIdString.end());
95 keyIds.push_back(keyId);
96 }
97 } else if (isLeft) {
98 keyIdBase64 += initData[keyIdPos];
99 }
100 ++keyIdPos;
101 }
102 if (keyIdPos == initData.size()) {
103 HDF_LOGE("%{public}s: kids parse error", __func__);
104 return HDF_ERR_INVALID_PARAM;
105 }
106 } else if (psshVersionId == 1) {
107 // Read key ID count
108 uint32_t keyIdCount;
109 int32_t ret = HDF_FAILURE;
110 ret = memcpy_s(&keyIdCount, sizeof(keyIdCount), &initData[readPosition], sizeof(keyIdCount));
111 if (ret != 0) {
112 HDF_LOGE("%{public}s: memcpy_s faild", __func__);
113 return ret;
114 }
115 keyIdCount = ntohl(keyIdCount);
116 readPosition += sizeof(keyIdCount);
117 if (readPosition + ((uint64_t)keyIdCount * KEY_ID_SIZE) != initData.size() - sizeof(uint32_t)) {
118 return HDF_ERR_INVALID_PARAM;
119 }
120
121 // Calculate the key ID offsets
122 for (uint32_t i = 0; i < keyIdCount; ++i) {
123 std::vector<uint8_t> keyId;
124 for (size_t j = 0; i < KEY_ID_SIZE; ++j) {
125 keyId.push_back(initData[readPosition + i * KEY_ID_SIZE + j]);
126 }
127 keyIds.push_back(keyId);
128 }
129 } else {
130 HDF_LOGE("%{public}s: psshVersionId is invalid", __func__);
131 return HDF_ERR_INVALID_PARAM;
132 }
133 return HDF_SUCCESS;
134 }
135
generateRequest(const MediaKeyType keyType,const std::vector<std::vector<uint8_t>> & keyIds,std::string * request)136 int32_t generateRequest(const MediaKeyType keyType, const std::vector<std::vector<uint8_t>> &keyIds,
137 std::string *request)
138 {
139 // begin
140 *request = "{\"kids\":[";
141 std::string encodedKeyId;
142 for (size_t i = 0; i < keyIds.size(); ++i) {
143 encodedKeyId.clear();
144 std::string keyId(keyIds[i].begin(), keyIds[i].end());
145 encodedKeyId = Encode(keyId);
146 if (i != 0) {
147 request->append(",");
148 }
149 request->append("\"");
150 request->append(encodedKeyId);
151 request->append("\"");
152 }
153 if (keyType == MEDIA_KEY_TYPE_ONLINE) {
154 request->append("],\"type\":\"temporary\"}");
155 } else if (keyType == MEDIA_KEY_TYPE_OFFLINE) {
156 request->append("],\"type\":\"persistent-license\"}");
157 } else {
158 return HDF_ERR_INVALID_PARAM;
159 }
160 return HDF_SUCCESS;
161 }
162
findSubVector(const std::vector<uint8_t> & main,const std::vector<uint8_t> & sub)163 int32_t findSubVector(const std::vector<uint8_t> &main, const std::vector<uint8_t> &sub)
164 {
165 for (size_t i = 0; i < main.size(); ++i) {
166 size_t j = 0;
167 for (j = 0; j < sub.size() && main[i + j] == sub[j]; ++j) {
168 }
169 // for j end
170 if (j == sub.size()) {
171 return i;
172 }
173 }
174 return -1;
175 }
176 } // V1_0
177 } // Drm
178 } // HDI
179 } // OHOS