• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <mutex>
17 #include <shared_mutex>
18 #include <string>
19 #include <refbase.h>
20 #include <securec.h>
21 #include "drm_log.h"
22 #include "native_drm_base.h"
23 #include "native_drm_object.h"
24 #include "key_session_impl.h"
25 #include "native_mediakeysession.h"
26 #include "native_mediakeysystem.h"
27 
28 using namespace OHOS::DrmStandard;
29 
30 
DealMediaKeyRequest(IMediaKeySessionService::MediaKeyRequest & licenseRequest,DRM_MediaKeyRequest * mediaKeyRequest)31 static Drm_ErrCode DealMediaKeyRequest(IMediaKeySessionService::MediaKeyRequest &licenseRequest,
32     DRM_MediaKeyRequest *mediaKeyRequest)
33 {
34     memset_s(mediaKeyRequest, sizeof(DRM_MediaKeyRequest), 0, sizeof(DRM_MediaKeyRequest));
35     mediaKeyRequest->type = (DRM_MediaKeyRequestType)(licenseRequest.requestType);
36     mediaKeyRequest->dataLen = licenseRequest.mData.size();
37     int ret = memcpy_s(mediaKeyRequest->data, licenseRequest.mData.size(), licenseRequest.mData.data(),
38         licenseRequest.mData.size());
39     if (ret != 0) {
40         DRM_DEBUG_LOG("licenseRequest.mData.data() is nullptr!");
41         return DRM_ERR_NO_MEMORY;
42     }
43     if (licenseRequest.mDefaultURL.size() == 0) {
44         return DRM_ERR_OK;
45     }
46     ret = memcpy_s(mediaKeyRequest->defaultUrl, licenseRequest.mDefaultURL.size(), licenseRequest.mDefaultURL.data(),
47         licenseRequest.mDefaultURL.size());
48     if (ret != 0) {
49         DRM_DEBUG_LOG("licenseRequest.mDefaultURL.data() is nullptr!");
50         return DRM_ERR_NO_MEMORY;
51     }
52     return DRM_ERR_OK;
53 }
54 
OH_MediaKeySession_GenerateMediaKeyRequest(MediaKeySession * mediaKeySession,DRM_MediaKeyRequestInfo * info,DRM_MediaKeyRequest * mediaKeyRequest)55 Drm_ErrCode OH_MediaKeySession_GenerateMediaKeyRequest(MediaKeySession *mediaKeySession, DRM_MediaKeyRequestInfo *info,
56     DRM_MediaKeyRequest *mediaKeyRequest)
57 {
58     DRM_INFO_LOG("OH_MediaKeySession_GenerateMediaKeyRequest enter");
59     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySession != nullptr) && (info != nullptr) && (mediaKeyRequest != nullptr)),
60         DRM_ERR_INVALID_VAL, "parameter is error");
61     IMediaKeySessionService::MediaKeyRequestInfo licenseRequestInfo;
62     IMediaKeySessionService::MediaKeyRequest licenseRequest;
63     licenseRequest.requestType = OHOS::DrmStandard::IMediaKeySessionService::REQUEST_TYPE_RELEASE;
64     licenseRequestInfo.mediaKeyType = (IMediaKeySessionService::MediaKeyType)info->type;
65     licenseRequestInfo.mimeType = std::string(info->mimeType, info->mimeType + sizeof(info->mimeType));
66     std::vector<uint8_t> initDataStr(info->initData, info->initData + info->initDataLen);
67     licenseRequestInfo.initData = initDataStr;
68     for (uint32_t i = 0; i < info->optionsCount; i++) {
69         std::string optionsname(info->optionName[i], info->optionName[i] + strlen(info->optionName[i]));
70         std::string optionsvalue(info->optionData[i], info->optionData[i] + strlen(info->optionData[i]));
71         licenseRequestInfo.optionalData.insert(std::make_pair(optionsname, optionsvalue));
72     }
73     MediaKeySessionObject *sessionObject = reinterpret_cast<MediaKeySessionObject *>(mediaKeySession);
74     int ret = sessionObject->sessionImpl_->GenerateMediaKeyRequest(licenseRequestInfo, licenseRequest);
75     DRM_CHECK_AND_RETURN_RET_LOG((ret == DRM_ERR_OK), DRM_ERR_INVALID_VAL,
76         "OH_MediaKeySession_GenerateMediaKeyRequest call Failed!");
77     Drm_ErrCode result = DealMediaKeyRequest(licenseRequest, mediaKeyRequest);
78     DRM_INFO_LOG("OH_MediaKeySession_GenerateMediaKeyRequest exit");
79     return result;
80 }
81 
OH_MediaKeySession_ProcessMediaKeyResponse(MediaKeySession * mediaKeySession,uint8_t * response,int32_t responseLen,uint8_t * offlineMediaKeyId,int32_t * offlineMediaKeyIdLen)82 Drm_ErrCode OH_MediaKeySession_ProcessMediaKeyResponse(MediaKeySession *mediaKeySession, uint8_t *response,
83     int32_t responseLen, uint8_t *offlineMediaKeyId, int32_t *offlineMediaKeyIdLen)
84 {
85     DRM_CHECK_AND_RETURN_RET_LOG(
86         ((mediaKeySession != nullptr) && (response != nullptr) && (responseLen > 0) &&
87         (offlineMediaKeyId != nullptr) && (offlineMediaKeyIdLen != nullptr)), DRM_ERR_INVALID_VAL,
88         "parameter is error!");
89     MediaKeySessionObject *sessionObject = reinterpret_cast<MediaKeySessionObject *>(mediaKeySession);
90     std::vector<uint8_t> licenseResponseVec(response, response + responseLen);
91     std::vector<uint8_t> keyIdVec;
92     int32_t ret = sessionObject->sessionImpl_->ProcessMediaKeyResponse(keyIdVec, licenseResponseVec);
93     DRM_CHECK_AND_RETURN_RET_LOG(ret == DRM_ERR_OK, DRM_ERR_INVALID_VAL, "got licenseResponse null!");
94     if (keyIdVec.size() == 0) {
95         DRM_DEBUG_LOG("keyIdVec.data() is nullptr!");
96         return DRM_ERR_OK;
97     }
98     ret = memcpy_s(offlineMediaKeyId, *offlineMediaKeyIdLen, keyIdVec.data(), keyIdVec.size());
99     if (ret != 0) {
100         DRM_ERR_LOG("OH_MediaKeySession_ProcessMediaKeyResponse memcpy_s faild!");
101         return DRM_ERR_NO_MEMORY;
102     }
103     *offlineMediaKeyIdLen = keyIdVec.size();
104     return DRM_ERR_OK;
105 }
106 
MapToClist(std::map<std::string,std::string> licenseStatus,DRM_MediaKeyStatus * mediaKeyStatus)107 static Drm_ErrCode MapToClist(std::map<std::string, std::string> licenseStatus, DRM_MediaKeyStatus *mediaKeyStatus)
108 {
109     memset_s(mediaKeyStatus, sizeof(DRM_MediaKeyStatus), 0, sizeof(DRM_MediaKeyStatus));
110     mediaKeyStatus->statusCount = licenseStatus.size();
111     auto it = licenseStatus.begin();
112     for (size_t i = 0; i < licenseStatus.size(); i++) {
113         int32_t ret = memcpy_s(mediaKeyStatus->statusName[i], it->first.size(), it->first.c_str(), it->first.size());
114         if (ret != 0) {
115             DRM_DEBUG_LOG("ediaKeyStatus->statusName is nullptr!");
116             return DRM_ERR_NO_MEMORY;
117         }
118         ret = memcpy_s(mediaKeyStatus->statusValue[i], it->second.size(), it->second.c_str(), it->second.size());
119         if (ret != 0) {
120             DRM_DEBUG_LOG("mediaKeyStatus->statusValue is nullptr!");
121             return DRM_ERR_NO_MEMORY;
122         }
123         it++;
124     }
125     DRM_INFO_LOG("MapToClist exit.");
126     return DRM_ERR_OK;
127 }
128 
OH_MediaKeySession_CheckMediaKeyStatus(MediaKeySession * mediaKeySessoin,DRM_MediaKeyStatus * mediaKeyStatus)129 Drm_ErrCode OH_MediaKeySession_CheckMediaKeyStatus(MediaKeySession *mediaKeySessoin, DRM_MediaKeyStatus *mediaKeyStatus)
130 {
131     DRM_INFO_LOG("OH_MediaKeySession_CheckMediaKeyStatus enter");
132     std::map<std::string, std::string> licenseStatus;
133     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySessoin != nullptr) && (mediaKeyStatus != nullptr)), DRM_ERR_INVALID_VAL,
134         "OH_MediaKeySession_ClearMediaKeys parameter is error!");
135     MediaKeySessionObject *sessionObject = reinterpret_cast<MediaKeySessionObject *>(mediaKeySessoin);
136     int32_t result = sessionObject->sessionImpl_->CheckMediaKeyStatus(licenseStatus);
137     DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_INVALID_VAL,
138         "OH_SetConfigurationByteArray mediaKeySystemImpl::SetConfigurationByteArray faild!");
139     if (licenseStatus.size() == 0) {
140         DRM_ERR_LOG("Licence not exist.");
141         return DRM_ERR_INVALID_VAL;
142     }
143     Drm_ErrCode ret = MapToClist(licenseStatus, mediaKeyStatus);
144     DRM_INFO_LOG("OH_MediaKeySession_CheckMediaKeyStatus exit.");
145     return ret;
146 }
147 
OH_MediaKeySession_ClearMediaKeys(MediaKeySession * mediaKeySessoin)148 Drm_ErrCode OH_MediaKeySession_ClearMediaKeys(MediaKeySession *mediaKeySessoin)
149 {
150     DRM_INFO_LOG("OH_MediaKeySession_ClearMediaKeys enter.");
151     DRM_CHECK_AND_RETURN_RET_LOG(mediaKeySessoin != nullptr, DRM_ERR_INVALID_VAL,
152         "OH_MediaKeySession_ClearMediaKeys parameter is error!");
153     int32_t currentPid = OHOS::IPCSkeleton::GetCallingPid();
154     DRM_DEBUG_LOG("MediaKeySessionNapi GetCallingPID: %{public}d", currentPid);
155     int32_t result = DRM_ERR_OK;
156     MediaKeySessionObject *sessionObject = reinterpret_cast<MediaKeySessionObject *>(mediaKeySessoin);
157     result = sessionObject->sessionImpl_->ClearMediaKeys();
158     DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_INVALID_VAL,
159         "OH_SetConfigurationByteArray mediaKeySystemImpl::SetConfigurationByteArray faild!");
160     DRM_INFO_LOG("OH_MediaKeySession_ClearMediaKeys exit.");
161     return DRM_ERR_OK;
162 }
163 
OH_MediaKeySession_GenerateOfflineReleaseRequest(MediaKeySession * mediaKeySessoin,uint8_t * offlineMediaKeyId,int32_t offlineMediaKeyIdLen,uint8_t * releaseRequest,int32_t * releaseRequestLen)164 Drm_ErrCode OH_MediaKeySession_GenerateOfflineReleaseRequest(MediaKeySession *mediaKeySessoin,
165     uint8_t *offlineMediaKeyId, int32_t offlineMediaKeyIdLen, uint8_t *releaseRequest, int32_t *releaseRequestLen)
166 {
167     DRM_INFO_LOG("OH_MediaKeySession_GenerateOfflineReleaseRequest enter");
168     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySessoin != nullptr) && (offlineMediaKeyId != nullptr) &&
169         (offlineMediaKeyIdLen > 0) && (releaseRequest != nullptr) && (releaseRequestLen != nullptr)),
170         DRM_ERR_INVALID_VAL, "OH_MediaKeySession_GenerateOfflineReleaseRequest parameter is error!");
171     std::vector<uint8_t> ReleaseRequest;
172     std::vector<uint8_t> licenseIdVec(offlineMediaKeyId, offlineMediaKeyId + offlineMediaKeyIdLen);
173 
174     MediaKeySessionObject *sessionObject = reinterpret_cast<MediaKeySessionObject *>(mediaKeySessoin);
175 
176     uint32_t result = sessionObject->sessionImpl_->GenerateOfflineReleaseRequest(licenseIdVec, ReleaseRequest);
177     DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_INVALID_VAL,
178         "OH_MediaKeySession_GenerateOfflineReleaseRequest GenerateOfflineReleaseRequest faild!");
179     if (ReleaseRequest.size() == 0) {
180         DRM_DEBUG_LOG("ReleaseRequest.data() is nullptr!");
181         return DRM_ERR_OK;
182     }
183     int32_t ret = memcpy_s(releaseRequest, *releaseRequestLen, ReleaseRequest.data(), ReleaseRequest.size());
184     if (ret != 0) {
185         DRM_ERR_LOG("OH_MediaKeySession_GenerateOfflineReleaseRequest memcpy_s faild!");
186         return DRM_ERR_INVALID_VAL;
187     }
188     *releaseRequestLen = ReleaseRequest.size();
189     DRM_INFO_LOG("OH_MediaKeySession_GenerateOfflineReleaseRequest exit");
190     return DRM_ERR_OK;
191 }
192 
OH_MediaKeySession_ProcessOfflineReleaseResponse(MediaKeySession * mediaKeySessoin,uint8_t * offlineMediaKeyId,int32_t offlineMediaKeyIdLen,uint8_t * releaseReponse,int32_t releaseReponseLen)193 Drm_ErrCode OH_MediaKeySession_ProcessOfflineReleaseResponse(MediaKeySession *mediaKeySessoin,
194     uint8_t *offlineMediaKeyId, int32_t offlineMediaKeyIdLen, uint8_t *releaseReponse, int32_t releaseReponseLen)
195 {
196     DRM_INFO_LOG("OH_MediaKeySession_ProcessOfflineReleaseResponse enter.");
197     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySessoin != nullptr) && (offlineMediaKeyId != nullptr) &&
198         (offlineMediaKeyIdLen > 0) && (releaseReponse != nullptr) && (releaseReponseLen > 0)),
199         DRM_ERR_INVALID_VAL, "OH_MediaKeySession_ClearMediaKeys parameter is error!");
200     std::vector<uint8_t> licenseIdVec(offlineMediaKeyId, offlineMediaKeyId + offlineMediaKeyIdLen);
201     std::vector<uint8_t> responseVec(releaseReponse, releaseReponse + releaseReponseLen);
202     int32_t result = DRM_ERR_OK;
203     MediaKeySessionObject *sessionObject = reinterpret_cast<MediaKeySessionObject *>(mediaKeySessoin);
204     result = sessionObject->sessionImpl_->ProcessOfflineReleaseResponse(licenseIdVec, responseVec);
205     DRM_CHECK_AND_RETURN_RET_LOG((result == DRM_ERR_OK), DRM_ERR_INVALID_VAL,
206         "OH_MediaKeySession_ProcessOfflineReleaseResponse call Failed!");
207     DRM_INFO_LOG("OH_MediaKeySession_ProcessOfflineReleaseResponse exit.");
208     return DRM_ERR_OK;
209 }
210 
OH_MediaKeySession_RestoreOfflineMediaKeys(MediaKeySession * mediaKeySessoin,uint8_t * offlineMediaKeyId,int32_t offlineMediaKeyIdLen)211 Drm_ErrCode OH_MediaKeySession_RestoreOfflineMediaKeys(MediaKeySession *mediaKeySessoin,
212     uint8_t *offlineMediaKeyId, int32_t offlineMediaKeyIdLen)
213 {
214     DRM_INFO_LOG("OH_MediaKeySession_RestoreOfflineLMediaKey enter.");
215     DRM_CHECK_AND_RETURN_RET_LOG(
216         ((mediaKeySessoin != nullptr) && (offlineMediaKeyId != nullptr) && (offlineMediaKeyIdLen > 0)),
217         DRM_ERR_INVALID_VAL, "OH_MediaKeySession_RestoreOfflineLMediaKey parameter is error!");
218     std::vector<uint8_t> licenseIdVec(offlineMediaKeyId, offlineMediaKeyId + offlineMediaKeyIdLen);
219     int32_t result = DRM_ERR_OK;
220     MediaKeySessionObject *sessionObject = reinterpret_cast<MediaKeySessionObject *>(mediaKeySessoin);
221     result = sessionObject->sessionImpl_->RestoreOfflineMediaKeys(licenseIdVec);
222     DRM_CHECK_AND_RETURN_RET_LOG((result == DRM_ERR_OK), DRM_ERR_INVALID_VAL,
223         "OH_MediaKeySession_restoreOfflineMediaKey call Failed!");
224     DRM_INFO_LOG("OH_MediaKeySession_restoreOfflineMediaKey exit.");
225     return DRM_ERR_OK;
226 }
227 
OH_MediaKeySession_GetContentProtectionLevel(MediaKeySession * mediaKeySessoin,DRM_ContentProtectionLevel * contentProtectionLevel)228 Drm_ErrCode OH_MediaKeySession_GetContentProtectionLevel(MediaKeySession *mediaKeySessoin,
229     DRM_ContentProtectionLevel *contentProtectionLevel)
230 {
231     DRM_INFO_LOG("OH_MediaKeySession_GetContentProtectionLevel enter.");
232     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySessoin != nullptr) && (contentProtectionLevel != nullptr)),
233         DRM_ERR_INVALID_VAL, "OH_MediaKeySession_GetContentProtectionLevel parameter is error!");
234     int32_t result = DRM_ERR_OK;
235     IMediaKeySessionService::ContentProtectionLevel level =
236         IMediaKeySessionService::ContentProtectionLevel::CONTENT_PROTECTION_LEVEL_UNKNOWN;
237     MediaKeySessionObject *sessionObject = reinterpret_cast<MediaKeySessionObject *>(mediaKeySessoin);
238     result = sessionObject->sessionImpl_->GetContentProtectionLevel(&level);
239     DRM_CHECK_AND_RETURN_RET_LOG((result == DRM_ERR_OK), DRM_ERR_INVALID_VAL,
240         "OH_MediaKeySession_GetContentProtectionLevel get level fail!");
241     *contentProtectionLevel = static_cast<DRM_ContentProtectionLevel>(level);
242     if (*contentProtectionLevel < CONTENT_PROTECTION_LEVEL_UNKNOWN ||
243         *contentProtectionLevel > CONTENT_PROTECTION_LEVEL_MAX) {
244         DRM_ERR_LOG("OH_MediaKeySession_GetContentProtectionLevel faild!");
245         return DRM_ERR_INVALID_VAL;
246     }
247     DRM_INFO_LOG("OH_MediaKeySession_GetContentProtectionLevel exit");
248     return DRM_ERR_OK;
249 }
250 
OH_MediaKeySession_RequireSecureDecoderModule(MediaKeySession * mediaKeySessoin,const char * mimeType,bool * status)251 Drm_ErrCode OH_MediaKeySession_RequireSecureDecoderModule(MediaKeySession *mediaKeySessoin, const char *mimeType,
252     bool *status)
253 {
254     DRM_INFO_LOG("OH_MediaKeySession_RequireSecureDecoderModule enter.");
255     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySessoin != nullptr) && (mimeType != nullptr) && (status != nullptr)),
256         DRM_ERR_INVALID_VAL, "OH_MediaKeySession_RequireSecureDecoderModule parameter is error!");
257     std::string mimeTypeBuf = std::string(mimeType);
258     DRM_CHECK_AND_RETURN_RET_LOG((mimeTypeBuf.size() != 0), DRM_ERR_INVALID_VAL,
259         "OH_MediaKeySession_RequireSecureDecoderModule mimeTypesize is zero!");
260     bool statusValue = false;
261     int32_t result = DRM_ERR_OK;
262     MediaKeySessionObject *sessionObject = reinterpret_cast<MediaKeySessionObject *>(mediaKeySessoin);
263     result = sessionObject->sessionImpl_->RequireSecureDecoderModule(mimeTypeBuf, &statusValue);
264     if (result != DRM_ERR_OK) {
265         DRM_ERR_LOG("OH_MediaKeySession_RequireSecureDecoderModule keySessionImpl_->RequireSecureDecoderModule faild!");
266         return DRM_ERR_INVALID_VAL;
267     }
268     *status = statusValue;
269     DRM_INFO_LOG("OH_MediaKeySession_RequireSecureDecoderModule exit.");
270     return DRM_ERR_OK;
271 }
272 
OH_MediaKeySession_SetMediaKeySessionCallback(MediaKeySession * mediaKeySessoin,MediaKeySession_Callback * callback)273 Drm_ErrCode OH_MediaKeySession_SetMediaKeySessionCallback(MediaKeySession *mediaKeySessoin,
274     MediaKeySession_Callback *callback)
275 {
276     DRM_INFO_LOG("OH_MediaKeySession_SetMediaKeySessionCallback enter.");
277     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySessoin != nullptr) && (callback != nullptr)), DRM_ERR_INVALID_VAL,
278         "mediaKeySessoin is nullptr!");
279     MediaKeySessionObject *sessionObject = reinterpret_cast<MediaKeySessionObject *>(mediaKeySessoin);
280     sessionObject->sessionCallback_->SetCallbackReference(*callback);
281     return DRM_ERR_OK;
282 }
283 
OH_MediaKeySession_Destroy(MediaKeySession * keySession)284 Drm_ErrCode OH_MediaKeySession_Destroy(MediaKeySession *keySession)
285 {
286     DRM_INFO_LOG("OH_MediaKeySession_Destroy enter.");
287     DRM_CHECK_AND_RETURN_RET_LOG(keySession != nullptr, DRM_ERR_INVALID_VAL,
288         "OH_MediaKeySession_Destroy keySession is nullptr!");
289     int32_t result = DRM_ERR_OK;
290     MediaKeySessionObject *sessionObject = reinterpret_cast<MediaKeySessionObject *>(keySession);
291     result = sessionObject->sessionImpl_->Release();
292     if (result != DRM_ERR_OK) {
293         DRM_ERR_LOG("OH_MediaKeySession_Destroy keySessionImpl_->Release faild!");
294         return DRM_ERR_INVALID_STATE;
295     }
296     delete sessionObject;
297     DRM_INFO_LOG("OH_MediaKeySession_Destroy exit.");
298     return DRM_ERR_OK;
299 }