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