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 "key_session_impl.h"
17 #include "drm_log.h"
18 #include "drm_error_code.h"
19 #include "drm_trace.h"
20 
21 namespace OHOS {
22 namespace DrmStandard {
23 
MediaKeySessionImpl(sptr<IMediaKeySessionService> &keySession)24 MediaKeySessionImpl::MediaKeySessionImpl(sptr<IMediaKeySessionService> &keySession)
25     : keySessionServiceCallback_(nullptr), keySessionServiceProxy_(keySession)
26 {
27     DRM_DEBUG_LOG("0x%{public}06" PRIXPTR "MediaKeySessionImpl Instances create",
28         FAKE_POINTER(this));
29 
30     sptr<IRemoteObject> object = keySessionServiceProxy_->AsObject();
31     pid_t pid = 0;
32     deathRecipient_ = new(std::nothrow) DrmDeathRecipient(pid);
33     DRM_CHECK_AND_RETURN_LOG(deathRecipient_ != nullptr, "failed to new DrmDeathRecipient.");
34     deathRecipient_->SetNotifyCb([this] (pid_t pid) {
35         this->MediaKeySessionServerDied(pid);
36     });
37     bool result = object->AddDeathRecipient(deathRecipient_);
38     if (!result) {
39         DRM_ERR_LOG("failed to add deathRecipient");
40         return;
41     }
42 
43     CreateListenerObject();
44 }
45 
CreateListenerObject()46 int32_t MediaKeySessionImpl::CreateListenerObject()
47 {
48     DRM_INFO_LOG("CreateListenerObject");
49     std::lock_guard<std::recursive_mutex> lock(mutex_);
50     listenerStub_ = new(std::nothrow) DrmListenerStub();
51     DRM_CHECK_AND_RETURN_RET_LOG(listenerStub_ != nullptr, DRM_MEMORY_ERROR,
52         "failed to new DrmListenerStub object");
53     DRM_CHECK_AND_RETURN_RET_LOG(keySessionServiceProxy_ != nullptr, DRM_MEMORY_ERROR,
54         "Drm service does not exist.");
55 
56     sptr<IRemoteObject> object = listenerStub_->AsObject();
57     DRM_CHECK_AND_RETURN_RET_LOG(object != nullptr, DRM_MEMORY_ERROR, "listener object is nullptr.");
58 
59     return keySessionServiceProxy_->SetListenerObject(object);
60 }
61 
~MediaKeySessionImpl()62 MediaKeySessionImpl::~MediaKeySessionImpl()
63 {
64     DRM_INFO_LOG("~MediaKeySessionImpl enter.");
65     std::lock_guard<std::recursive_mutex> lock(mutex_);
66     keySessionServiceProxy_ = nullptr;
67     keySessionServiceCallback_ = nullptr;
68     DRM_DEBUG_LOG("0x%{public}06" PRIXPTR "MediaKeySessionImpl Instances release",
69         FAKE_POINTER(this));
70 }
71 
MediaKeySessionServerDied(pid_t pid)72 void MediaKeySessionImpl::MediaKeySessionServerDied(pid_t pid)
73 {
74     DRM_ERR_LOG("MediaKeySession server has died, pid:%{public}d!", pid);
75     std::lock_guard<std::recursive_mutex> lock(mutex_);
76     if (keySessionServiceProxy_ != nullptr && keySessionServiceProxy_->AsObject() != nullptr
77         && deathRecipient_ != nullptr) {
78         (void)keySessionServiceProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
79         keySessionServiceProxy_ = nullptr;
80     }
81     listenerStub_ = nullptr;
82     deathRecipient_ = nullptr;
83 }
84 
Release()85 int32_t MediaKeySessionImpl::Release()
86 {
87     DRM_INFO_LOG("MediaKeySessionImpl Release enter.");
88     std::lock_guard<std::recursive_mutex> lock(mutex_);
89     int32_t ret = DRM_UNKNOWN_ERROR;
90     if (keySessionServiceProxy_ != nullptr) {
91         sptr<IRemoteObject> object = keySessionServiceProxy_->AsObject();
92         if (object != nullptr && deathRecipient_ != nullptr) {
93             object->RemoveDeathRecipient(deathRecipient_);
94             deathRecipient_ = nullptr;
95         }
96         ret = keySessionServiceProxy_->Release();
97         if (ret != DRM_OK) {
98             DRM_ERR_LOG("Failed to Release key session!, %{public}d", ret);
99         }
100     } else {
101         DRM_ERR_LOG("MediaKeySessionServiceProxy_ == nullptr");
102     }
103     keySessionServiceProxy_ = nullptr;
104     keySessionServiceCallback_ = nullptr;
105     return ret;
106 }
107 
GenerateMediaKeyRequest(IMediaKeySessionService::MediaKeyRequestInfo &licenseRequestInfo, IMediaKeySessionService::MediaKeyRequest &licenseRequest)108 int32_t MediaKeySessionImpl::GenerateMediaKeyRequest(IMediaKeySessionService::MediaKeyRequestInfo &licenseRequestInfo,
109     IMediaKeySessionService::MediaKeyRequest &licenseRequest)
110 {
111     DrmTrace trace("GenerateMediaKeyRequest");
112     DRM_INFO_LOG("GenerateMediaKeyRequest enter.");
113     std::lock_guard<std::recursive_mutex> lock(mutex_);
114     int32_t retCode = DRM_OK;
115     if (keySessionServiceProxy_ == nullptr) {
116         DRM_ERR_LOG("GenerateMediaKeyRequest keySessionServiceProxy_ is null");
117         return DRM_SERVICE_ERROR;
118     }
119     retCode = keySessionServiceProxy_->GenerateMediaKeyRequest(licenseRequestInfo, licenseRequest);
120     if (retCode != DRM_OK) {
121         DRM_ERR_LOG("GenerateMediaKeyRequest failed, retCode: %{public}d", retCode);
122         return DRM_SERVICE_ERROR;
123     }
124     return DRM_OK;
125 }
126 
ProcessMediaKeyResponse(std::vector<uint8_t> &licenseId, std::vector<uint8_t> &licenseResponse)127 int32_t MediaKeySessionImpl::ProcessMediaKeyResponse(std::vector<uint8_t> &licenseId,
128     std::vector<uint8_t> &licenseResponse)
129 {
130     DrmTrace trace("MediaKeySessionImpl::ProcessMediaKeyResponse");
131     DRM_INFO_LOG("ProcessMediaKeyResponse enter.");
132     std::lock_guard<std::recursive_mutex> lock(mutex_);
133     int32_t retCode = DRM_OK;
134 
135     if (keySessionServiceProxy_ == nullptr) {
136         DRM_ERR_LOG("ProcessMediaKeyResponse keySessionServiceProxy_ is null");
137         return DRM_SERVICE_ERROR;
138     }
139     retCode = keySessionServiceProxy_->ProcessMediaKeyResponse(licenseId, licenseResponse);
140     if (retCode != DRM_OK) {
141         DRM_ERR_LOG("ProcessMediaKeyResponse failed, retCode: %{public}d", retCode);
142         return DRM_SERVICE_ERROR;
143     }
144     return DRM_OK;
145 }
146 
GenerateOfflineReleaseRequest(std::vector<uint8_t> &licenseId, std::vector<uint8_t> &releaseRequest)147 int32_t MediaKeySessionImpl::GenerateOfflineReleaseRequest(std::vector<uint8_t> &licenseId,
148     std::vector<uint8_t> &releaseRequest)
149 {
150     DRM_INFO_LOG("GenerateOfflineReleaseRequest enter.");
151     std::lock_guard<std::recursive_mutex> lock(mutex_);
152     int32_t retCode = DRM_OK;
153     if (keySessionServiceProxy_ == nullptr) {
154         DRM_ERR_LOG("GenerateOfflineReleaseRequest keySessionServiceProxy_ is null");
155         return DRM_SERVICE_ERROR;
156     }
157 
158     retCode = keySessionServiceProxy_->GenerateOfflineReleaseRequest(licenseId, releaseRequest);
159     if (retCode != DRM_OK) {
160         DRM_ERR_LOG("GenerateOfflineReleaseRequest failed, retCode: %{public}d", retCode);
161         return DRM_SERVICE_ERROR;
162     }
163     return DRM_OK;
164 }
165 
ProcessOfflineReleaseResponse(std::vector<uint8_t> &licenseId, std::vector<uint8_t> &releaseResponse)166 int32_t MediaKeySessionImpl::ProcessOfflineReleaseResponse(std::vector<uint8_t> &licenseId,
167     std::vector<uint8_t> &releaseResponse)
168 {
169     DRM_INFO_LOG("ProcessOfflineReleaseResponse enter.");
170     std::lock_guard<std::recursive_mutex> lock(mutex_);
171     int32_t retCode = DRM_OK;
172 
173     if (keySessionServiceProxy_ == nullptr) {
174         DRM_ERR_LOG("ProcessOfflineReleaseResponse keySessionServiceProxy_ is null");
175         return DRM_SERVICE_ERROR;
176     }
177     retCode = keySessionServiceProxy_->ProcessOfflineReleaseResponse(licenseId, releaseResponse);
178     if (retCode != DRM_OK) {
179         DRM_ERR_LOG("ProcessOfflineReleaseResponse failed, retCode: %{public}d", retCode);
180         return DRM_SERVICE_ERROR;
181     }
182     return DRM_OK;
183 }
184 
GetContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel *securityLevel)185 int32_t MediaKeySessionImpl::GetContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel *securityLevel)
186 {
187     DRM_INFO_LOG("GetContentProtectionLevel enter.");
188     std::lock_guard<std::recursive_mutex> lock(mutex_);
189     int32_t retCode = DRM_OK;
190 
191     if (keySessionServiceProxy_ == nullptr) {
192         DRM_ERR_LOG("GetContentProtectionLevel serviceProxy_ is null");
193         return DRM_SERVICE_ERROR;
194     }
195     retCode = keySessionServiceProxy_->GetContentProtectionLevel(
196         (IMediaKeySessionService::ContentProtectionLevel *)securityLevel);
197     if (retCode != DRM_OK) {
198         DRM_ERR_LOG("GetContentProtectionLevel failed, retCode: %{public}d", retCode);
199         return DRM_SERVICE_ERROR;
200     }
201     return DRM_OK;
202 }
203 
CheckMediaKeyStatus(std::map<std::string, std::string> &licenseStatus)204 int32_t MediaKeySessionImpl::CheckMediaKeyStatus(std::map<std::string, std::string> &licenseStatus)
205 {
206     DRM_INFO_LOG("CheckMediaKeyStatus enter.");
207     std::lock_guard<std::recursive_mutex> lock(mutex_);
208     int32_t retCode = DRM_OK;
209 
210     if (keySessionServiceProxy_ == nullptr) {
211         DRM_ERR_LOG("CheckMediaKeyStatus keySessionServiceProxy_ is null");
212         return DRM_SERVICE_ERROR;
213     }
214     retCode = keySessionServiceProxy_->CheckMediaKeyStatus(licenseStatus);
215     if (retCode != DRM_OK) {
216         DRM_ERR_LOG("CheckMediaKeyStatus failed, retCode: %{public}d", retCode);
217         return DRM_SERVICE_ERROR;
218     }
219     return DRM_OK;
220 }
221 
RestoreOfflineMediaKeys(std::vector<uint8_t> &licenseId)222 int32_t MediaKeySessionImpl::RestoreOfflineMediaKeys(std::vector<uint8_t> &licenseId)
223 {
224     DRM_INFO_LOG("RestoreOfflineMediaKeys enter.");
225     std::lock_guard<std::recursive_mutex> lock(mutex_);
226     int32_t retCode = DRM_OK;
227 
228     if (keySessionServiceProxy_ == nullptr) {
229         DRM_ERR_LOG("RestoreOfflineMediaKeys keySessionServiceProxy_ is null");
230         return DRM_SERVICE_ERROR;
231     }
232     retCode = keySessionServiceProxy_->RestoreOfflineMediaKeys(licenseId);
233     if (retCode != DRM_OK) {
234         DRM_ERR_LOG("RestoreOfflineMediaKeys failed, retCode: %{public}d", retCode);
235         return DRM_SERVICE_ERROR;
236     }
237     return DRM_OK;
238 }
239 
ClearMediaKeys()240 int32_t MediaKeySessionImpl::ClearMediaKeys()
241 {
242     DRM_INFO_LOG("ClearMediaKeys enter.");
243     std::lock_guard<std::recursive_mutex> lock(mutex_);
244     int32_t retCode = DRM_OK;
245 
246     if (keySessionServiceProxy_ == nullptr) {
247         DRM_ERR_LOG("ClearMediaKeys keySessionServiceProxy_ is null");
248         return DRM_SERVICE_ERROR;
249     }
250     retCode = keySessionServiceProxy_->ClearMediaKeys();
251     if (retCode != DRM_OK) {
252         DRM_ERR_LOG("ClearMediaKeys failed, retCode: %{public}d", retCode);
253         return DRM_SERVICE_ERROR;
254     }
255     return DRM_OK;
256 }
257 
RequireSecureDecoderModule(std::string &mimeType, bool *status)258 int32_t MediaKeySessionImpl::RequireSecureDecoderModule(std::string &mimeType, bool *status)
259 {
260     DRM_INFO_LOG("RequireSecureDecoderModule enter.");
261     std::lock_guard<std::recursive_mutex> lock(mutex_);
262     int32_t retCode = DRM_OK;
263 
264     if (keySessionServiceProxy_ == nullptr) {
265         DRM_ERR_LOG("RequireSecureDecoderModule keySessionServiceProxy_ is null");
266         return DRM_SERVICE_ERROR;
267     }
268     retCode = keySessionServiceProxy_->RequireSecureDecoderModule(mimeType, status);
269     if (retCode != DRM_OK) {
270         DRM_ERR_LOG("status: %{public}d", *status);
271         return retCode;
272     }
273     return DRM_OK;
274 }
275 
GetMediaKeySessionServiceProxy()276 sptr<IMediaKeySessionService> MediaKeySessionImpl::GetMediaKeySessionServiceProxy()
277 {
278     DRM_INFO_LOG("GetMediaKeySessionServiceProxy enter.");
279     std::lock_guard<std::recursive_mutex> lock(mutex_);
280     if (keySessionServiceProxy_ != nullptr) {
281         DRM_DEBUG_LOG("MediaKeySessionImpl MediaKeySessionServiceProxy is not nullptr");
282     }
283     return keySessionServiceProxy_;
284 }
285 
GetApplicationCallback()286 sptr<MediaKeySessionImplCallback> MediaKeySessionImpl::GetApplicationCallback()
287 {
288     DRM_INFO_LOG("GetApplicationCallback enter.");
289     std::lock_guard<std::recursive_mutex> lock(mutex_);
290     return keySessionApplicationCallback_;
291 }
292 
SetCallback(const sptr<MediaKeySessionImplCallback> &callback)293 int32_t MediaKeySessionImpl::SetCallback(const sptr<MediaKeySessionImplCallback> &callback)
294 {
295     DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " SetCallback in", FAKE_POINTER(this));
296     std::lock_guard<std::recursive_mutex> lock(mutex_);
297     DRM_CHECK_AND_RETURN_RET_LOG(callback != nullptr, DRM_INVALID_ARG, "callback is nullptr");
298     keySessionApplicationCallback_ = callback;
299 
300     int32_t ret = DRM_ERROR;
301     keySessionServiceCallback_ = new (std::nothrow) MediaKeySessionServiceCallback(this);
302     if (keySessionServiceCallback_ == nullptr) {
303         DRM_ERR_LOG("MediaKeySessionServiceCallback alloc failed.");
304         return ret;
305     }
306 
307     if (keySessionServiceProxy_ == nullptr) {
308         DRM_ERR_LOG("SetCallback keySessionServiceProxy_ is null");
309         return DRM_SERVICE_ERROR;
310     }
311     ret = keySessionServiceProxy_->SetCallback(keySessionServiceCallback_);
312     if (ret != DRM_OK) {
313         DRM_ERR_LOG("SetCallback failed, retCode: %{public}d", ret);
314         return DRM_SERVICE_ERROR;
315     }
316     return ret;
317 }
318 
InitEventMap()319 void MediaKeySessionServiceCallback::InitEventMap()
320 {
321     DRM_INFO_LOG("InitEventMap enter");
322     std::lock_guard<std::recursive_mutex> lock(mutex_);
323     eventMap_[static_cast<int32_t>(DRM_EVENT_KEY_NEEDED)] = MediaKeySessionEvent::EVENT_STR_KEY_NEEDED;
324     eventMap_[static_cast<int32_t>(DRM_EVENT_KEY_EXPIRED)] = MediaKeySessionEvent::EVENT_STR_KEY_EXPIRED;
325     eventMap_[static_cast<int32_t>(DRM_EVENT_EXPIRATION_UPDATED)] = MediaKeySessionEvent::EVENT_STR_EXPIRATION_UPDATED;
326     eventMap_[static_cast<int32_t>(DRM_EVENT_KEY_CHANGED)] = MediaKeySessionEvent::EVENT_STR_KEY_CHANGED;
327     eventMap_[static_cast<int32_t>(DRM_EVENT_VENDOR_DEFINED)] = MediaKeySessionEvent::EVENT_STR_VENDOR_DEFINED;
328 }
329 
GetEventName(DrmEventType event)330 std::string MediaKeySessionServiceCallback::GetEventName(DrmEventType event)
331 {
332     DRM_INFO_LOG("GetEventName enter");
333     std::string eventName = "";
334     std::lock_guard<std::recursive_mutex> lock(mutex_);
335     int32_t eventType = static_cast<int32_t>(event);
336     if (eventMap_.find(eventType) == eventMap_.end()) {
337         return eventName;
338     }
339     return eventMap_[eventType];
340 }
341 
SendEvent(DrmEventType event, int32_t extra, const std::vector<uint8_t> &data)342 int32_t MediaKeySessionServiceCallback::SendEvent(DrmEventType event, int32_t extra, const std::vector<uint8_t> &data)
343 {
344     DRM_INFO_LOG("SendEvent enter");
345     std::string eventName = GetEventName(event);
346     std::lock_guard<std::recursive_mutex> lock(mutex_);
347     if (keySessionImpl_ != nullptr && eventName.length() != 0) {
348         sptr<MediaKeySessionImplCallback> applicationCallback = keySessionImpl_->GetApplicationCallback();
349         if (applicationCallback != nullptr) {
350             applicationCallback->SendEvent(eventName, extra, data);
351             return DRM_OK;
352         }
353     }
354     DRM_DEBUG_LOG("SendEvent failed.");
355     return DRM_ERROR;
356 }
357 
SendEventKeyChanged( std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> statusTable, bool hasNewGoodLicense)358 int32_t MediaKeySessionServiceCallback::SendEventKeyChanged(
359     std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> statusTable, bool hasNewGoodLicense)
360 {
361     DRM_INFO_LOG("SendEventKeyChanged enter.");
362     std::lock_guard<std::recursive_mutex> lock(mutex_);
363     if (keySessionImpl_ != nullptr) {
364         sptr<MediaKeySessionImplCallback> callback = keySessionImpl_->GetApplicationCallback();
365         if (callback != nullptr) {
366             callback->SendEventKeyChanged(statusTable, hasNewGoodLicense);
367             return DRM_OK;
368         }
369     }
370     DRM_ERR_LOG("SendEventKeyChanged failed.");
371     return DRM_ERROR;
372 }
373 } // DrmStandard
374 } // OHOS