• 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<map>
18 #include <shared_mutex>
19 #include <string>
20 #include <refbase.h>
21 #include <securec.h>
22 #include "drm_log.h"
23 #include "native_drm_common.h"
24 #include "native_drm_base.h"
25 #include "native_drm_object.h"
26 #include "native_mediakeysystem.h"
27 
28 using namespace OHOS::DrmStandard;
29 
OH_MediaKeySystem_IsSupported(const char * uuid)30 bool OH_MediaKeySystem_IsSupported(const char *uuid)
31 {
32     DRM_INFO_LOG("OH_MediaKeySystemIsSupportedByUuid enter.");
33     DRM_CHECK_AND_RETURN_RET_LOG(uuid != nullptr, false, "MediaKeySystem uuid is nullptr!");
34     std::string uuidPtr(uuid);
35     bool isSurooprtted = false;
36     DRM_CHECK_AND_RETURN_RET_LOG(uuidPtr.size() != 0, false, "MediaKeySystem uuidPtr.size is nullptr!");
37     OHOS::sptr<MediaKeySystemFactoryImpl> fatory = MediaKeySystemFactoryImpl::GetInstance();
38     isSurooprtted = fatory->IsMediaKeySystemSupported(uuidPtr);
39     DRM_INFO_LOG("OH_MediaKeySystemIsSupportedByUuid exit.");
40     return isSurooprtted;
41 }
42 
OH_MediaKeySystem_IsSupported2(const char * uuid,const char * mimeType)43 bool OH_MediaKeySystem_IsSupported2(const char *uuid, const char *mimeType)
44 {
45     DRM_INFO_LOG("OH_MediaKeySystem_IsSupported2 enter.");
46     DRM_CHECK_AND_RETURN_RET_LOG(((uuid != nullptr) && (mimeType != nullptr)), false,
47         "OH_MediaKeySystem uuid is nullptr!");
48     bool isSurooprtted = false;
49     std::string uuidPtr(uuid);
50     DRM_CHECK_AND_RETURN_RET_LOG(uuidPtr.size() != 0, false, "MediaKeySystem uuidPtr.size is nullptr!");
51     std::string mimeTypePtr = std::string(mimeType);
52     DRM_CHECK_AND_RETURN_RET_LOG(mimeTypePtr.size() != 0, false, "MediaKeySystem mimeTypePtr.size is nullptr!");
53 
54     OHOS::sptr<MediaKeySystemFactoryImpl> fatory = MediaKeySystemFactoryImpl::GetInstance();
55     isSurooprtted = fatory->IsMediaKeySystemSupported(uuidPtr, mimeTypePtr);
56     DRM_INFO_LOG("OH_MediaKeySystem_IsSupported2 exit.");
57     return isSurooprtted;
58 }
59 
OH_MediaKeySystem_IsSupported3(const char * uuid,const char * mimeType,DRM_ContentProtectionLevel ContentProtectionLevel)60 bool OH_MediaKeySystem_IsSupported3(const char *uuid, const char *mimeType,
61     DRM_ContentProtectionLevel ContentProtectionLevel)
62 {
63     DRM_INFO_LOG("OH_MediaKeySystem_IsSupported3 enter.");
64     DRM_CHECK_AND_RETURN_RET_LOG(((uuid != nullptr) && (mimeType != nullptr)), false,
65         "OH_MediaKeySystem uuid is nullptr!");
66     bool isSurooprtted = false;
67     std::string uuidPtr(uuid);
68     DRM_CHECK_AND_RETURN_RET_LOG(uuidPtr.size() != 0, false, "MediaKeySystem Uuid.size is nullptr!");
69     std::string mimeTypePtr = std::string(mimeType);
70     DRM_CHECK_AND_RETURN_RET_LOG(mimeTypePtr.size() != 0, false, "MediaKeySystem mimeTypePtr.size is nullptr!");
71 
72     OHOS::sptr<MediaKeySystemFactoryImpl> fatory = MediaKeySystemFactoryImpl::GetInstance();
73 
74     IMediaKeySessionService::ContentProtectionLevel securityLevel =
75         (IMediaKeySessionService::ContentProtectionLevel)ContentProtectionLevel;
76     if ((securityLevel <= IMediaKeySessionService::CONTENT_PROTECTION_LEVEL_UNKNOWN) ||
77         (securityLevel >= IMediaKeySessionService::CONTENT_PROTECTION_LEVEL_MAX)) {
78         DRM_ERR_LOG("ContentProtectionLevel is invalid");
79         return false;
80     }
81     isSurooprtted = fatory->IsMediaKeySystemSupported(uuidPtr, mimeTypePtr, securityLevel);
82     DRM_INFO_LOG("OH_MediaKeySystem_IsSupported3 exit.");
83     return isSurooprtted;
84 }
85 
OH_MediaKeySystem_Create(const char * name,MediaKeySystem ** mediaKeySystem)86 Drm_ErrCode OH_MediaKeySystem_Create(const char *name, MediaKeySystem **mediaKeySystem)
87 {
88     std::map<int32_t, Drm_ErrCode> maps = {
89         {401, DRM_ERR_INVALID_VAL},
90         {24700201, DRM_ERR_SERVICE_DIED},
91         {24700103, DRM_ERR_MAX_SYSTEM_NUM_REACHED},
92         {24700101, DRM_ERR_UNKNOWN},
93         {0, DRM_ERR_OK}
94     };
95     DRM_CHECK_AND_RETURN_RET_LOG(((name != nullptr) && (mediaKeySystem != nullptr)), DRM_ERR_INVALID_VAL,
96         "parameter is error!");
97     OHOS::sptr<MediaKeySystemFactoryImpl> factory = MediaKeySystemFactoryImpl::GetInstance();
98     DRM_CHECK_AND_RETURN_RET_LOG(factory != nullptr, DRM_ERR_INVALID_VAL, "factory is nullptr!");
99 
100     std::string nameStr = name;
101     DRM_CHECK_AND_RETURN_RET_LOG(nameStr.size() != 0, DRM_ERR_INVALID_VAL, "nameStr.size() is zero");
102     OHOS::sptr<OHOS::DrmStandard::MediaKeySystemImpl> system = nullptr;
103     int32_t result = factory->CreateMediaKeySystem(nameStr, &system);
104     DRM_CHECK_AND_RETURN_RET_LOG(system != nullptr, maps[result], "system create by name failed!");
105 
106     struct MediaKeySystemObject *object = new (std::nothrow) MediaKeySystemObject(system);
107     DRM_CHECK_AND_RETURN_RET_LOG(object != nullptr, DRM_ERR_INVALID_VAL, "MediaKeySystemObject create failed!");
108 
109     object->systemCallback_ = new (std::nothrow) MediaKeySystemCallbackCapi();
110     if (object->systemCallback_ == nullptr) {
111         delete object;
112         DRM_ERR_LOG("MediaKeySystemObject create systemCallback failed!");
113     }
114     int32_t ret = object->systemImpl_->SetCallback(object->systemCallback_);
115     DRM_CHECK_AND_RETURN_RET_LOG(ret == DRM_ERR_OK, DRM_ERR_UNKNOWN, "system set callback failed!");
116 
117     *mediaKeySystem = object;
118     return DRM_ERR_OK;
119 }
120 
OH_MediaKeySystem_SetConfigurationString(MediaKeySystem * mediaKeySystem,const char * configName,const char * value)121 Drm_ErrCode OH_MediaKeySystem_SetConfigurationString(MediaKeySystem *mediaKeySystem, const char *configName,
122     const char *value)
123 {
124     DRM_INFO_LOG("OH_SetConfigurationString enter.");
125     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (configName != nullptr) && (value != nullptr)),
126         DRM_ERR_INVALID_VAL, "OH_SetConfigurationString parameter is error!");
127 
128     int32_t result = DRM_ERR_OK;
129     std::string name(configName);
130     DRM_CHECK_AND_RETURN_RET_LOG(name.size() != 0, DRM_ERR_INVALID_VAL,
131         "OH_SetConfigurationString configName.size is not zero!");
132     std::string valuePtr(value);
133     DRM_CHECK_AND_RETURN_RET_LOG(valuePtr.size() != 0, DRM_ERR_INVALID_VAL,
134         "OH_SetConfigurationString value.size is not zero!");
135 
136     MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
137     result = systemObject->systemImpl_->SetConfigurationString(name, valuePtr);
138     DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_INVALID_VAL,
139         "OH_SetConfigurationString mediaKeySystemImpl::SetConfigurationString faild!");
140 
141     DRM_INFO_LOG("OH_SetConfigurationString exit.");
142     return DRM_ERR_OK;
143 }
144 
OH_MediaKeySystem_GetConfigurationString(MediaKeySystem * mediaKeySystem,const char * configName,char * value,int32_t valueLen)145 Drm_ErrCode OH_MediaKeySystem_GetConfigurationString(MediaKeySystem *mediaKeySystem, const char *configName,
146     char *value, int32_t valueLen)
147 {
148     DRM_INFO_LOG("OH_GetConfigurationString enter");
149     DRM_CHECK_AND_RETURN_RET_LOG(
150         ((mediaKeySystem != nullptr) && (configName != nullptr) && (value != nullptr) && (valueLen > 0)),
151         DRM_ERR_INVALID_VAL, "OH_GetConfigurationString params is error!");
152 
153     std::string valuePtr;
154     int32_t result = DRM_ERR_UNKNOWN;
155     std::string name = std::string(configName);
156     DRM_CHECK_AND_RETURN_RET_LOG(name.size() != 0, DRM_ERR_INVALID_VAL,
157         "OH_SetConfigurationString configName.size is not zero!");
158     MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
159     result = systemObject->systemImpl_->GetConfigurationString(name, valuePtr);
160     DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_INVALID_VAL,
161         "OH_SetConfigurationString mediaKeySystemImpl::GetConfigurationString faild!");
162     DRM_CHECK_AND_RETURN_RET_LOG(valueLen >= (int32_t)valuePtr.size(), DRM_ERR_INVALID_VAL,
163         "OH_SetConfigurationString The space for value is too small");
164     memset_s(value, valueLen, 0, valueLen);
165     int32_t ret = memcpy_s(value, valuePtr.size(), valuePtr.c_str(), valuePtr.size());
166     if (ret != 0) {
167         DRM_ERR_LOG("OH_GetConfigurationString The length of the obtained value is zero !");
168         return DRM_ERR_UNKNOWN;
169     }
170     DRM_INFO_LOG("OH_GetConfigurationString exit");
171     return DRM_ERR_OK;
172 }
173 
OH_MediaKeySystem_SetConfigurationByteArray(MediaKeySystem * mediaKeySystem,const char * configName,uint8_t * value,int32_t valueLen)174 Drm_ErrCode OH_MediaKeySystem_SetConfigurationByteArray(MediaKeySystem *mediaKeySystem,
175     const char *configName, uint8_t *value, int32_t valueLen)
176 
177 {
178     DRM_INFO_LOG("OH_SetConfigurationByteArray enter.");
179     DRM_CHECK_AND_RETURN_RET_LOG(
180         ((mediaKeySystem != nullptr) && (configName != nullptr) && (value != nullptr) && (valueLen > 0)),
181         DRM_ERR_INVALID_VAL, "OH_SetConfigurationByteArray params is error!");
182 
183     int32_t result = DRM_ERR_OK;
184     std::string name(configName);
185     DRM_CHECK_AND_RETURN_RET_LOG(name.size() != 0, DRM_ERR_INVALID_VAL,
186         "OH_SetConfigurationByteArray configName.size is not zero!");
187     uint8_t *valueDataPtr = reinterpret_cast<uint8_t *>(value);
188     DRM_CHECK_AND_RETURN_RET_LOG(valueDataPtr != nullptr, DRM_ERR_INVALID_VAL,
189         "OH_SetConfigurationByteArray value is nullptr!");
190     std::vector<uint8_t> valueptr(valueDataPtr, valueDataPtr + valueLen);
191     DRM_CHECK_AND_RETURN_RET_LOG(valueptr.size() != 0, DRM_ERR_INVALID_VAL,
192         "OH_SetConfigurationByteArray value.size is not zero!");
193 
194     MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
195     result = systemObject->systemImpl_->SetConfigurationByteArray(name, valueptr);
196     DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_INVALID_VAL,
197         "OH_SetConfigurationByteArray mediaKeySystemImpl::SetConfigurationByteArray faild!");
198     DRM_INFO_LOG("OH_SetConfigurationByteArray exit.");
199     return DRM_ERR_OK;
200 }
201 
OH_MediaKeySystem_GetConfigurationByteArray(MediaKeySystem * mediaKeySystem,const char * configName,uint8_t * value,int32_t * valueLen)202 Drm_ErrCode OH_MediaKeySystem_GetConfigurationByteArray(MediaKeySystem *mediaKeySystem,
203     const char *configName, uint8_t *value, int32_t *valueLen)
204 {
205     DRM_INFO_LOG("OH_GetConfigurationByteArray enter");
206     DRM_CHECK_AND_RETURN_RET_LOG(
207         ((mediaKeySystem != nullptr) && (configName != nullptr) && (value != nullptr) && (valueLen != nullptr)),
208         DRM_ERR_INVALID_VAL, "OH_GetConfigurationByteArray parameter is error!");
209 
210     std::vector<uint8_t> valuePtr;
211     int32_t result = DRM_ERR_OK;
212     std::string name = std::string(configName);
213     DRM_CHECK_AND_RETURN_RET_LOG(name.size() != 0, DRM_ERR_INVALID_VAL,
214         "OH_GetConfigurationByteArray configName.size is not zero!");
215 
216     MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
217     result = systemObject->systemImpl_->GetConfigurationByteArray(name, valuePtr);
218     DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_INVALID_VAL,
219         "OH_GetConfigurationByteArray mediaKeySystemImpl::GetConfigurationByteArray faild!");
220     DRM_CHECK_AND_RETURN_RET_LOG(*valueLen >= (int32_t)valuePtr.size(), DRM_ERR_INVALID_VAL,
221         "OH_GetConfigurationByteArray The space for value is too small!");
222     *valueLen = valuePtr.size();
223     int32_t ret = memcpy_s(value, valuePtr.size(), valuePtr.data(), valuePtr.size());
224     if (ret != 0) {
225         DRM_ERR_LOG("OH_GetConfigurationByteArray memcpy_s faild!");
226         return DRM_ERR_NO_MEMORY;
227     }
228     DRM_INFO_LOG("OH_GetConfigurationByteArray exit");
229     return DRM_ERR_OK;
230 }
231 
vectorToClist(std::vector<IMediaKeySystemService::MetircKeyValue> & metrics,DRM_Statistics * statistics)232 static Drm_ErrCode vectorToClist(std::vector<IMediaKeySystemService::MetircKeyValue> &metrics,
233     DRM_Statistics *statistics)
234 {
235     DRM_INFO_LOG("vectorToCArray start.");
236     memset_s(statistics, sizeof(DRM_Statistics), 0, sizeof(DRM_Statistics));
237     statistics->statisticsCount = metrics.size();
238     for (size_t i = 0; i < metrics.size(); i++) {
239         int32_t ret = memcpy_s(statistics->statisticsName[i],
240             metrics[i].name.size(), metrics[i].name.c_str(), metrics[i].name.size());
241         if (ret != 0) {
242             DRM_ERR_LOG(" memcpy_s faild!");
243             return DRM_ERR_INVALID_VAL;
244         }
245         ret = memcpy_s(statistics->statisticsDescription[i],
246             metrics[i].value.size(), metrics[i].value.c_str(), metrics[i].value.size());
247         if (ret != 0) {
248             DRM_ERR_LOG(" memcpy_s faild!");
249             return DRM_ERR_INVALID_VAL;
250         }
251     }
252     DRM_INFO_LOG("vectorToCArray exit.");
253     return DRM_ERR_OK;
254 }
255 
OH_MediaKeySystem_GetStatistics(MediaKeySystem * mediaKeySystem,DRM_Statistics * statistics)256 Drm_ErrCode OH_MediaKeySystem_GetStatistics(MediaKeySystem *mediaKeySystem, DRM_Statistics *statistics)
257 {
258     DRM_INFO_LOG("OH_MediaKeySystem_GetStatistics enter.");
259     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (statistics != nullptr)), DRM_ERR_INVALID_VAL,
260         "OH_MediaKeySystem_GetStatistics params is error!");
261     MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
262     std::vector<IMediaKeySystemService::MetircKeyValue> metrics;
263     int32_t result = systemObject->systemImpl_->GetStatistics(metrics);
264     DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_INVALID_VAL,
265         "OH_MediaKeySystem_GetStatistics systemObject is nullptr!");
266     Drm_ErrCode ret = vectorToClist(metrics, statistics);
267     DRM_CHECK_AND_RETURN_RET_LOG(statistics != nullptr, DRM_ERR_INVALID_VAL,
268         "OH_MediaKeySystem_GetStatistics *DRM_Statistics is nullptr!");
269     DRM_INFO_LOG("OH_MediaKeySystem_GetStatistics exit.");
270     return ret;
271 }
272 
OH_MediaKeySystem_GetMaxContentProtectionLevel(MediaKeySystem * mediaKeySystem,DRM_ContentProtectionLevel * contentProtectionLevel)273 Drm_ErrCode OH_MediaKeySystem_GetMaxContentProtectionLevel(MediaKeySystem *mediaKeySystem,
274     DRM_ContentProtectionLevel *contentProtectionLevel)
275 {
276     DRM_INFO_LOG("GetMaxContentProtectionLevel enter.");
277     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (contentProtectionLevel != nullptr)),
278         DRM_ERR_INVALID_VAL, "OH_GetMaxContentProtectionLevel parameter is error!");
279     MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
280     int32_t result = DRM_ERR_OK;
281     IMediaKeySessionService::ContentProtectionLevel level = IMediaKeySessionService::CONTENT_PROTECTION_LEVEL_UNKNOWN;
282     result = systemObject->systemImpl_->GetMaxContentProtectionLevel(&level);
283     DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_INVALID_VAL, "OH_GetMaxContentProtectionLevel  fail!");
284     if (level < IMediaKeySessionService::CONTENT_PROTECTION_LEVEL_UNKNOWN ||
285         level > IMediaKeySessionService::CONTENT_PROTECTION_LEVEL_MAX) {
286         DRM_ERR_LOG("mediaKeySystemImpl::GetMaxContentProtectionLevel faild!");
287         return DRM_ERR_INVALID_VAL;
288     }
289     *contentProtectionLevel = static_cast<DRM_ContentProtectionLevel>(level);
290     DRM_INFO_LOG("OH_GetMaxContentProtectionLevel exit");
291     return DRM_ERR_OK;
292 }
293 
OH_MediaKeySystem_GenerateKeySystemRequest(MediaKeySystem * mediaKeySystem,uint8_t * request,int32_t * requestLen,char * defaultUrl,int32_t defaultUrlLen)294 Drm_ErrCode OH_MediaKeySystem_GenerateKeySystemRequest(MediaKeySystem *mediaKeySystem, uint8_t *request,
295     int32_t *requestLen, char *defaultUrl, int32_t defaultUrlLen)
296 {
297     DRM_INFO_LOG("OH_MediaKeySystem_GenerateKeySystemRequest enter");
298     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (request != nullptr) && (requestLen != nullptr) &&
299         (defaultUrl != nullptr) && (defaultUrlLen > 0)),
300         DRM_ERR_INVALID_VAL, "OH_MediaKeySystem_GenerateKeySystemRequest mediaKeySystem is nullptr!");
301     std::vector<uint8_t> requestData;
302     std::string defaultUrlData;
303 
304     MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
305     int32_t result = systemObject->systemImpl_->GenerateKeySystemRequest(requestData, defaultUrlData);
306     DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_INVALID_VAL,
307         "OH_MediaKeySystem_GenerateKeySystemRequest mediaKeySystem is nullptr!");
308     if (requestData.size() != 0) {
309         int32_t ret = memcpy_s(request, *requestLen, requestData.data(), requestData.size());
310         if (ret != 0) {
311             DRM_DEBUG_LOG(" requestData.data() is nullptr!");
312             return DRM_ERR_INVALID_VAL;
313         }
314     }
315     *requestLen = requestData.size();
316     int32_t ret = memset_s(defaultUrl, defaultUrlLen, 0, defaultUrlLen);
317     if (ret != 0) {
318         DRM_ERR_LOG(" memset defaultUrl err !");
319         return DRM_ERR_INVALID_VAL;
320     }
321     if (defaultUrlData.size() != 0) {
322         ret = memcpy_s(defaultUrl, defaultUrlLen, defaultUrlData.data(), defaultUrlData.size());
323         if (ret != 0) {
324             DRM_DEBUG_LOG(" defaultUrlData.data() is nullptr!");
325             return DRM_ERR_INVALID_VAL;
326         }
327     }
328     return DRM_ERR_OK;
329 }
330 
OH_MediaKeySystem_ProcessKeySystemResponse(MediaKeySystem * mediaKeySystem,uint8_t * response,int32_t responseLen)331 Drm_ErrCode OH_MediaKeySystem_ProcessKeySystemResponse(MediaKeySystem *mediaKeySystem,
332     uint8_t *response, int32_t responseLen)
333 {
334     DRM_INFO_LOG("OH_ProcessKeySystemResponse enter.");
335     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (response != nullptr) && (responseLen > 0)),
336         DRM_ERR_INVALID_VAL, "OH_ProcessKeySystemResponse parameter is error!");
337     int32_t result = DRM_ERR_OK;
338     std::vector<uint8_t> keySystemResponse(response, response + responseLen);
339     MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
340     result = systemObject->systemImpl_->ProcessKeySystemResponse(keySystemResponse);
341     DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_INVALID_VAL,
342         "OH_ProcessKeySystemResponse systemObject is nullptr!");
343     DRM_INFO_LOG("OH_ProcessKeySystemResponse exit.");
344     return DRM_ERR_OK;
345 }
346 
OH_MediaKeySystem_GetCertificateStatus(MediaKeySystem * mediaKeySystem,DRM_CertificateStatus * certStatus)347 Drm_ErrCode OH_MediaKeySystem_GetCertificateStatus(MediaKeySystem *mediaKeySystem, DRM_CertificateStatus *certStatus)
348 {
349     DRM_INFO_LOG("OH_MediaKeySystem_GetCertificateStatus enter.");
350     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (certStatus != nullptr)), DRM_ERR_INVALID_VAL,
351         "OH_ProcessKeySystemResponse parameter is error!");
352     IMediaKeySystemService::CertificateStatus CertStatus;
353     MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
354     int32_t result = systemObject->systemImpl_->GetCertificateStatus(&CertStatus);
355     if (result != DRM_ERR_OK) {
356         *certStatus = CERT_STATUS_UNAVAILABLE;
357         DRM_ERR_LOG("OH_MediaKeySystem_GetCertificateStatus faild!");
358         return DRM_ERR_INVALID_VAL;
359     }
360     *certStatus = (DRM_CertificateStatus)((int32_t)(CertStatus));
361     if (*certStatus < CERT_STATUS_PROVISIONED || *certStatus > CERT_STATUS_UNAVAILABLE) {
362         DRM_ERR_LOG("OH_MediaKeySystem_GetCertificateStatus faild!");
363         *certStatus = CERT_STATUS_UNAVAILABLE;
364         return DRM_ERR_INVALID_VAL;
365     }
366     DRM_INFO_LOG("OH_MediaKeySystem_GetCertificateStatus exit.");
367     return DRM_ERR_OK;
368 }
369 
OH_MediaKeySystem_SetMediaKeySystemCallback(MediaKeySystem * mediaKeySystem,MediaKeySystem_Callback callback)370 Drm_ErrCode OH_MediaKeySystem_SetMediaKeySystemCallback(MediaKeySystem *mediaKeySystem,
371     MediaKeySystem_Callback callback)
372 {
373     DRM_INFO_LOG("OH_MediaKeySystem_SetMediaKeySystemCallback enter.");
374     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (callback != nullptr)), DRM_ERR_INVALID_VAL,
375         "parameter is error!");
376     MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
377     systemObject->systemCallback_->SetCallbackReference(callback);
378     DRM_INFO_LOG("OH_MediaKeySystem_SetMediaKeySystemCallback exit.");
379     return DRM_ERR_OK;
380 }
381 
OH_MediaKeySystem_CreateMediaKeySession(MediaKeySystem * mediaKeySystem,DRM_ContentProtectionLevel * level,MediaKeySession ** mediaKeySession)382 Drm_ErrCode OH_MediaKeySystem_CreateMediaKeySession(MediaKeySystem *mediaKeySystem, DRM_ContentProtectionLevel *level,
383     MediaKeySession **mediaKeySession)
384 {
385     std::map<int32_t, Drm_ErrCode> maps = {
386         {24700201, DRM_ERR_SERVICE_DIED},
387         {24700104, DRM_ERR_MAX_SESSION_NUM_REACHED},
388         {24700101, DRM_ERR_UNKNOWN},
389         {0, DRM_ERR_OK}
390     };
391     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (level != nullptr) && (mediaKeySession != nullptr) &&
392         (*level > CONTENT_PROTECTION_LEVEL_UNKNOWN) && (*level < CONTENT_PROTECTION_LEVEL_MAX)),
393         DRM_ERR_INVALID_VAL, "mediaKeySystem is nullptr!");
394     struct MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
395 
396     int32_t secure = static_cast<int32_t>(*level);
397     IMediaKeySessionService::ContentProtectionLevel secureLevel =
398         static_cast<IMediaKeySessionService::ContentProtectionLevel>(secure);
399     OHOS::sptr<MediaKeySessionImpl> keySessionImpl = nullptr;
400     int32_t ret = systemObject->systemImpl_->CreateMediaKeySession(secureLevel, &keySessionImpl);
401 
402     DRM_CHECK_AND_RETURN_RET_LOG(ret == DRM_ERR_OK, maps[ret], "session create return failed!");
403     DRM_CHECK_AND_RETURN_RET_LOG(keySessionImpl != nullptr, DRM_ERR_INVALID_VAL, "session create failed!");
404 
405     struct MediaKeySessionObject *sessionObject = new (std::nothrow) MediaKeySessionObject(keySessionImpl);
406     DRM_CHECK_AND_RETURN_RET_LOG(sessionObject != nullptr, DRM_ERR_NO_MEMORY, "MediaKeySessionObject create failed!");
407 
408     sessionObject->sessionCallback_ = new (std::nothrow) MediaKeySessionCallbackCapi();
409     if (sessionObject->sessionCallback_ == nullptr) {
410         delete sessionObject;
411         DRM_ERR_LOG("MediaKeySessionObject create sessionCallback failed!");
412         return DRM_ERR_INVALID_VAL;
413     }
414     ret = sessionObject->sessionImpl_->SetCallback(sessionObject->sessionCallback_);
415     DRM_CHECK_AND_RETURN_RET_LOG(ret == DRM_ERR_OK, DRM_ERR_INVALID_VAL, "session set callback failed!");
416 
417     *mediaKeySession = static_cast<MediaKeySession *>(sessionObject);
418     return DRM_ERR_OK;
419 }
420 
vectorToC2DArray(std::vector<std::vector<uint8_t>> licenseIds,DRM_OfflineMediakeyIdArray * offlineMediaKeyIds)421 static Drm_ErrCode vectorToC2DArray(std::vector<std::vector<uint8_t>> licenseIds,
422     DRM_OfflineMediakeyIdArray *offlineMediaKeyIds)
423 {
424     if (licenseIds.size() >= MAX_OFFLINE_MEDIA_KEY_ID_COUNT) {
425         DRM_ERR_LOG("licenseIds size too large!");
426         return DRM_ERR_NO_MEMORY;
427     }
428 
429     offlineMediaKeyIds->idsCount = (uint32_t)(licenseIds.size());
430     for (size_t i = 0; i < licenseIds.size(); i++) {
431         offlineMediaKeyIds->idsLen[i] = (int32_t)(licenseIds[i].size());
432         int32_t ret = memcpy_s(offlineMediaKeyIds->ids[i], MAX_OFFLINE_MEDIA_KEY_ID_LEN, licenseIds[i].data(),
433             licenseIds[i].size());
434         if (ret != 0) {
435             DRM_ERR_LOG(" memcpy_s faild!");
436             return DRM_ERR_NO_MEMORY;
437         }
438     }
439     DRM_INFO_LOG("vectorToC2DArray exit.");
440     return DRM_ERR_OK;
441 }
442 
OH_MediaKeySystem_GetOfflineMediaKeyIds(MediaKeySystem * mediaKeySystem,DRM_OfflineMediakeyIdArray * offlineMediaKeyIds)443 Drm_ErrCode OH_MediaKeySystem_GetOfflineMediaKeyIds(MediaKeySystem *mediaKeySystem,
444     DRM_OfflineMediakeyIdArray *offlineMediaKeyIds)
445 {
446     DRM_INFO_LOG("OH_MediaKeySystem_GetOfflineMediaKeyIds enter.");
447     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (offlineMediaKeyIds != nullptr)), DRM_ERR_INVALID_VAL,
448         "OH_MediaKeySystem_GetOfflineMediaKeyIds parameter is error!");
449     std::vector<std::vector<uint8_t>> licenseIds;
450 
451     MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
452     int32_t result = systemObject->systemImpl_->GetOfflineMediaKeyIds(licenseIds);
453     DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_INVALID_VAL,
454         "OH_MediaKeySystem_GetOfflineMediaKeyIds faild!");
455     if (licenseIds.size() == 0) {
456         DRM_DEBUG_LOG("licenseIds.data() is nullptr!");
457         return DRM_ERR_OK;
458     }
459     result = vectorToC2DArray(licenseIds, offlineMediaKeyIds);
460     DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_INVALID_VAL,
461         "vectorToC2DArray faild!");
462     DRM_INFO_LOG("OH_MediaKeySystem_GetOfflineMediaKeyIds exit.");
463     return DRM_ERR_OK;
464 }
465 
OH_MediaKeySystem_GetOfflineMediaKeyStatus(MediaKeySystem * mediaKeySystem,uint8_t * offlineMediaKeyId,int32_t offlineMediaKeyIdLen,DRM_OfflineMediaKeyStatus * status)466 Drm_ErrCode OH_MediaKeySystem_GetOfflineMediaKeyStatus(MediaKeySystem *mediaKeySystem,
467     uint8_t *offlineMediaKeyId, int32_t offlineMediaKeyIdLen, DRM_OfflineMediaKeyStatus *status)
468 {
469     DRM_INFO_LOG("OH_MediaKeySystem_GetOfflineMediaKeyStatus enter");
470     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (offlineMediaKeyId != nullptr) &&
471         (offlineMediaKeyIdLen > 0) && (status != nullptr)),
472         DRM_ERR_INVALID_VAL, "OH_MediaKeySystem_GetOfflineMediaKeyStatus parameter is error!");
473     int32_t result = OFFLINE_MEDIA_KEY_STATUS_UNKNOWN;
474 
475     std::vector<uint8_t> licenseIdVec(offlineMediaKeyId, offlineMediaKeyId + offlineMediaKeyIdLen);
476     IMediaKeySessionService::OfflineMediaKeyStatus offlineMediaKeyStatus =
477         IMediaKeySessionService::OFFLINELICENSESTATUS_UNKNOWN;
478 
479     MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
480     result = systemObject->systemImpl_->GetOfflineMediaKeyStatus(licenseIdVec, offlineMediaKeyStatus);
481     if (result != DRM_ERR_OK) {
482         DRM_ERR_LOG("OH_MediaKeySystem_GetOfflineMediaKeyStatus faild!");
483         return DRM_ERR_INVALID_VAL;
484     }
485     DRM_OfflineMediaKeyStatus CofflineMediaKeyStatus = (DRM_OfflineMediaKeyStatus)((int32_t)(offlineMediaKeyStatus));
486     if (CofflineMediaKeyStatus < OFFLINE_MEDIA_KEY_STATUS_UNKNOWN ||
487         CofflineMediaKeyStatus > OFFLINE_MEDIA_KEY_STATUS_INACTIVE) {
488         DRM_ERR_LOG("OH_MediaKeySystem_GetOfflineMediaKeyStatus faild!");
489         return DRM_ERR_INVALID_VAL;
490     }
491     *status = CofflineMediaKeyStatus;
492     DRM_INFO_LOG("OH_MediaKeySystem_GetOfflineMediaKeyStatus exit");
493     return DRM_ERR_OK;
494 }
495 
OH_MediaKeySystem_ClearOfflineMediaKeys(MediaKeySystem * mediaKeySystem,uint8_t * offlineMediaKeyId,int32_t offlineMediaKeyIdLen)496 Drm_ErrCode OH_MediaKeySystem_ClearOfflineMediaKeys(MediaKeySystem *mediaKeySystem,
497     uint8_t *offlineMediaKeyId, int32_t offlineMediaKeyIdLen)
498 {
499     DRM_INFO_LOG("OH_RemoveOfflineLMediaKey enter.");
500     DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (offlineMediaKeyId != nullptr) &&
501         (offlineMediaKeyIdLen > 0)),
502         DRM_ERR_INVALID_VAL, "OH_GetOfflineMediaKeyStatus parameter is error!");
503     int32_t result = DRM_ERR_OK;
504     std::vector<uint8_t> licenseIdVec(offlineMediaKeyId, offlineMediaKeyId + offlineMediaKeyIdLen);
505     DRM_CHECK_AND_RETURN_RET_LOG(licenseIdVec.size() != 0, DRM_ERR_INVALID_VAL,
506         "OH_RemoveOfflineLMediaKey configName.size is not zero!");
507     MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
508     result = systemObject->systemImpl_->ClearOfflineMediaKeys(licenseIdVec);
509     DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_INVALID_VAL,
510         "OH_RemoveOfflineLMediaKey mediaKeySystemImpl::ClearOfflineMediaKeys faild!");
511     DRM_INFO_LOG("OH_RemoveOfflineLMediaKey exit.");
512     return DRM_ERR_OK;
513 }
514 
OH_MediaKeySystem_Destroy(MediaKeySystem * mediaKeySystem)515 Drm_ErrCode OH_MediaKeySystem_Destroy(MediaKeySystem *mediaKeySystem)
516 {
517     DRM_CHECK_AND_RETURN_RET_LOG(mediaKeySystem != nullptr, DRM_ERR_INVALID_VAL, "mediaKeySystem is nullptr!");
518 
519     struct MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
520 
521     int32_t ret = systemObject->systemImpl_->Release();
522     DRM_CHECK_AND_RETURN_RET_LOG(ret == DRM_ERR_OK, DRM_ERR_INVALID_VAL, "call media key system release failed!");
523     MediaKeySystemFactoryImpl::GetInstance()->keySystemNumber--;
524     delete mediaKeySystem;
525     return DRM_ERR_OK;
526 }