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 <hdf_log.h>
17 #include "session.h"
18 #include "openssl/aes.h"
19 #include "mime_type.h"
20 #include "data_parser.h"
21
22 namespace OHOS {
23 namespace HDI {
24 namespace Drm {
25 namespace V1_0 {
getKeyRequest(const std::vector<uint8_t> & indexInfo,const std::string & mimeType,MediaKeyType keyType,std::map<std::string,std::string> optionalData,std::vector<uint8_t> * keyRequest)26 int32_t Session::getKeyRequest(const std::vector<uint8_t> &indexInfo, const std::string &mimeType, MediaKeyType keyType,
27 std::map<std::string, std::string> optionalData, std::vector<uint8_t> *keyRequest)
28 {
29 std::vector<std::vector<uint8_t>> keyIds;
30 int32_t ret;
31 if (mimeType == ISO_VIDEO_MIME_TYPE || mimeType == ISO_AUDIO_MIME_TYPE || mimeType == CENC_INIT_DATA_FORMAT) {
32 ret = ParsePssh(indexInfo, keyIds);
33 if (ret != HDF_SUCCESS) {
34 return ret;
35 }
36 } else if (mimeType == WEBM_INIT_DATA_FORMAT || mimeType == WEBM_AUDIO_DATA_FORMAT ||
37 mimeType == WEBM_VIDEO_DATA_FORMAT) {
38 if (indexInfo.size() != KEY_ID_SIZE) {
39 return HDF_ERR_INVALID_PARAM;
40 }
41 keyIds.push_back(indexInfo);
42 } else {
43 return HDF_ERR_INVALID_PARAM;
44 }
45
46 std::string requestJson;
47 if (generateRequest(keyType, keyIds, &requestJson) != HDF_SUCCESS) {
48 return HDF_ERR_INVALID_PARAM;
49 }
50 for (auto optionalDataIt = optionalData.begin(); optionalDataIt != optionalData.end(); ++optionalDataIt) {
51 HDF_LOGI("optionalData name: %{public}s, optionalData value: %{public}s", optionalDataIt->first.c_str(),
52 optionalDataIt->second.c_str());
53 }
54 HDF_LOGI("requestJson: %{public}s", requestJson.c_str());
55 keyRequest->clear();
56 *keyRequest = std::vector<uint8_t>(requestJson.begin(), requestJson.end());
57 return HDF_SUCCESS;
58 }
59
setKeyIdAndKeyValue(const std::vector<uint8_t> & keyId,const std::vector<uint8_t> & keyValue)60 int32_t Session::setKeyIdAndKeyValue(const std::vector<uint8_t> &keyId, const std::vector<uint8_t> &keyValue)
61 {
62 HDF_LOGI("%{public}s: start", __func__);
63 keyIdAndKeyValue_.push_back(make_pair(keyId, keyValue));
64 keyIdStatusMap[keyId] = OFFLINE_MEDIA_KEY_STATUS_USABLE;
65 HDF_LOGI("%{public}s: end", __func__);
66 return HDF_SUCCESS;
67 }
68
getKeyValueByKeyId(const std::vector<uint8_t> & keyId,std::vector<uint8_t> & keyValue)69 int32_t Session::getKeyValueByKeyId(const std::vector<uint8_t> &keyId, std::vector<uint8_t> &keyValue)
70 {
71 for (auto &idValuePair : keyIdAndKeyValue_) {
72 if (idValuePair.first == keyId && keyIdStatusMap[keyId] == OFFLINE_MEDIA_KEY_STATUS_USABLE) {
73 keyValue = idValuePair.second;
74 return HDF_SUCCESS;
75 }
76 }
77 HDF_LOGE("%{public}s: The key status is incorrect and cannot be use!", __func__);
78 return HDF_FAILURE;
79 }
80 } // V1_0
81 } // Drm
82 } // HDI
83 } // OHOS