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 <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 || mimeType == WEBM_VIDEO_DATA_FORMAT) {
37         if (indexInfo.size() != KEY_ID_SIZE) {
38             return HDF_ERR_INVALID_PARAM;
39         }
40         keyIds.push_back(indexInfo);
41     } else {
42         return HDF_ERR_INVALID_PARAM;
43     }
44 
45     std::string requestJson;
46     if (generateRequest(keyType, keyIds, &requestJson) != HDF_SUCCESS) {
47         return HDF_ERR_INVALID_PARAM;
48     }
49     for (auto optionalDataIt = optionalData.begin(); optionalDataIt != optionalData.end(); ++optionalDataIt) {
50         HDF_LOGI("optionalData name: %{public}s, optionalData value: %{public}s", optionalDataIt->first.c_str(),
51             optionalDataIt->second.c_str());
52     }
53     HDF_LOGI("requestJson: %{public}s", requestJson.c_str());
54     keyRequest->clear();
55     *keyRequest = std::vector<uint8_t>(requestJson.begin(), requestJson.end());
56     return HDF_SUCCESS;
57 }
58 
setKeyIdAndKeyValue(const std::vector<uint8_t> & keyId,const std::vector<uint8_t> & keyValue)59 int32_t Session::setKeyIdAndKeyValue(const std::vector<uint8_t> &keyId, const std::vector<uint8_t> &keyValue)
60 {
61     HDF_LOGI("%{public}s: start", __func__);
62     keyIdAndKeyValue_.push_back(make_pair(keyId, keyValue));
63     keyIdStatusMap[keyId] = OFFLINE_MEDIA_KEY_STATUS_USABLE;
64     HDF_LOGI("%{public}s: end", __func__);
65     return HDF_SUCCESS;
66 }
67 
getKeyValueByKeyId(const std::vector<uint8_t> & keyId,std::vector<uint8_t> & keyValue)68 int32_t Session::getKeyValueByKeyId(const std::vector<uint8_t> &keyId, std::vector<uint8_t> &keyValue)
69 {
70     for (auto &idValuePair : keyIdAndKeyValue_) {
71         if (idValuePair.first == keyId && keyIdStatusMap[keyId] == OFFLINE_MEDIA_KEY_STATUS_USABLE) {
72             keyValue = idValuePair.second;
73             return HDF_SUCCESS;
74         }
75     }
76     HDF_LOGE("%{public}s: The key status is incorrect and cannot be use!", __func__);
77     return HDF_FAILURE;
78 }
79 } // V1_0
80 } // Drm
81 } // HDI
82 } // OHOS