• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #define LOG_TAG "clearkey-DrmFactory"
17 
18 #include <utils/Log.h>
19 
20 #include "DrmFactory.h"
21 
22 #include "ClearKeyUUID.h"
23 #include "CryptoPlugin.h"
24 #include "DrmPlugin.h"
25 #include "MimeTypeStdStr.h"
26 #include "SessionLibrary.h"
27 #include "AidlUtils.h"
28 
29 namespace aidl {
30 namespace android {
31 namespace hardware {
32 namespace drm {
33 namespace clearkey {
34 
35 using std::string;
36 using std::vector;
37 
38 using ::aidl::android::hardware::drm::SecurityLevel;
39 using ::aidl::android::hardware::drm::Status;
40 using ::aidl::android::hardware::drm::Uuid;
41 
createDrmPlugin(const Uuid & in_uuid,const string & in_appPackageName,std::shared_ptr<::aidl::android::hardware::drm::IDrmPlugin> * _aidl_return)42 ::ndk::ScopedAStatus DrmFactory::createDrmPlugin(
43         const Uuid& in_uuid, const string& in_appPackageName,
44         std::shared_ptr<::aidl::android::hardware::drm::IDrmPlugin>* _aidl_return) {
45     UNUSED(in_appPackageName);
46 
47     if (!isClearKeyUUID(in_uuid.uuid.data())) {
48         ALOGE("Clearkey Drm HAL: failed to create drm plugin, "
49               "invalid crypto scheme");
50         *_aidl_return = nullptr;
51         return toNdkScopedAStatus(Status::BAD_VALUE);
52     }
53 
54     std::shared_ptr<DrmPlugin> plugin =
55             ::ndk::SharedRefBase::make<DrmPlugin>(SessionLibrary::get());
56     *_aidl_return = plugin;
57     return toNdkScopedAStatus(Status::OK);
58 }
59 
createCryptoPlugin(const Uuid & in_uuid,const std::vector<uint8_t> & in_initData,std::shared_ptr<::aidl::android::hardware::drm::ICryptoPlugin> * _aidl_return)60 ::ndk::ScopedAStatus DrmFactory::createCryptoPlugin(
61         const Uuid& in_uuid, const std::vector<uint8_t>& in_initData,
62         std::shared_ptr<::aidl::android::hardware::drm::ICryptoPlugin>* _aidl_return) {
63     if (!isClearKeyUUID(in_uuid.uuid.data())) {
64         ALOGE("Clearkey Drm HAL: failed to create crypto plugin, "
65               "invalid crypto scheme");
66         *_aidl_return = nullptr;
67         return toNdkScopedAStatus(Status::BAD_VALUE);
68     }
69 
70     std::shared_ptr<CryptoPlugin> plugin = ::ndk::SharedRefBase::make<CryptoPlugin>(in_initData);
71     Status status = plugin->getInitStatus();
72     if (status != Status::OK) {
73         plugin.reset();
74         plugin = nullptr;
75     }
76     *_aidl_return = plugin;
77     return toNdkScopedAStatus(status);
78 }
79 
getSupportedCryptoSchemes(CryptoSchemes * _aidl_return)80 ::ndk::ScopedAStatus DrmFactory::getSupportedCryptoSchemes(CryptoSchemes* _aidl_return) {
81     CryptoSchemes schemes{};
82     for (const auto& uuid : ::aidl::android::hardware::drm::clearkey::getSupportedCryptoSchemes()) {
83         schemes.uuids.push_back({uuid});
84     }
85     for (auto mime : {kIsoBmffVideoMimeType, kIsoBmffAudioMimeType, kCencInitDataFormat,
86                       kWebmVideoMimeType, kWebmAudioMimeType, kWebmInitDataFormat}) {
87         const auto minLevel = SecurityLevel::SW_SECURE_CRYPTO;
88         const auto maxLevel = SecurityLevel::SW_SECURE_CRYPTO;
89         schemes.mimeTypes.push_back({mime, minLevel, maxLevel});
90     }
91     *_aidl_return = schemes;
92     return ndk::ScopedAStatus::ok();
93 }
94 
dump(int fd,const char ** args,uint32_t numArgs)95 binder_status_t DrmFactory::dump(int fd, const char** args, uint32_t numArgs) {
96     UNUSED(args);
97     UNUSED(numArgs);
98 
99     if (fd < 0) {
100         ALOGE("%s: negative fd", __FUNCTION__);
101         return STATUS_BAD_VALUE;
102     }
103 
104     uint32_t currentSessions = SessionLibrary::get()->numOpenSessions();
105     dprintf(fd, "current open sessions: %u\n", currentSessions);
106 
107     return STATUS_OK;
108 }
109 
110 }  // namespace clearkey
111 }  // namespace drm
112 }  // namespace hardware
113 }  // namespace android
114 } // namespace aidl
115