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 #pragma once
17 
18 #include <aidl/android/hardware/drm/BnDrmPlugin.h>
19 #include <aidl/android/hardware/drm/IDrmPluginListener.h>
20 #include <aidl/android/hardware/drm/Status.h>
21 
22 #include <stdio.h>
23 #include <map>
24 
25 #include <utils/List.h>
26 
27 #include "DeviceFiles.h"
28 #include "SessionLibrary.h"
29 
30 namespace aidl {
31 namespace android {
32 namespace hardware {
33 namespace drm {
34 namespace clearkey {
35 
36 using namespace clearkeydrm;
37 using ::aidl::android::hardware::drm::KeyType;
38 using ::aidl::android::hardware::drm::Status;
39 
40 struct DrmPlugin : public BnDrmPlugin {
41   public:
42     explicit DrmPlugin(SessionLibrary* sessionLibrary);
~DrmPluginDrmPlugin43     virtual ~DrmPlugin() { mFileHandle.DeleteAllLicenses(); }
44 
45     ::ndk::ScopedAStatus closeSession(const std::vector<uint8_t>& in_sessionId) override;
46     ::ndk::ScopedAStatus decrypt(const std::vector<uint8_t>& in_sessionId,
47                                  const std::vector<uint8_t>& in_keyId,
48                                  const std::vector<uint8_t>& in_input,
49                                  const std::vector<uint8_t>& in_iv,
50                                  std::vector<uint8_t>* _aidl_return) override;
51     ::ndk::ScopedAStatus encrypt(const std::vector<uint8_t>& in_sessionId,
52                                  const std::vector<uint8_t>& in_keyId,
53                                  const std::vector<uint8_t>& in_input,
54                                  const std::vector<uint8_t>& in_iv,
55                                  std::vector<uint8_t>* _aidl_return) override;
56     ::ndk::ScopedAStatus getHdcpLevels(
57             ::aidl::android::hardware::drm::HdcpLevels* _aidl_return) override;
58     ::ndk::ScopedAStatus getKeyRequest(
59             const std::vector<uint8_t>& in_scope, const std::vector<uint8_t>& in_initData,
60             const std::string& in_mimeType, ::aidl::android::hardware::drm::KeyType in_keyType,
61             const std::vector<::aidl::android::hardware::drm::KeyValue>& in_optionalParameters,
62             ::aidl::android::hardware::drm::KeyRequest* _aidl_return) override;
63     ::ndk::ScopedAStatus getLogMessages(
64             std::vector<::aidl::android::hardware::drm::LogMessage>* _aidl_return) override;
65 
66     ::ndk::ScopedAStatus getMetrics(
67             std::vector<::aidl::android::hardware::drm::DrmMetricGroup>* _aidl_return) override;
68     ::ndk::ScopedAStatus getNumberOfSessions(
69             ::aidl::android::hardware::drm::NumberOfSessions* _aidl_return) override;
70     ::ndk::ScopedAStatus getOfflineLicenseKeySetIds(
71             std::vector<::aidl::android::hardware::drm::KeySetId>* _aidl_return) override;
72     ::ndk::ScopedAStatus getOfflineLicenseState(
73             const ::aidl::android::hardware::drm::KeySetId& in_keySetId,
74             ::aidl::android::hardware::drm::OfflineLicenseState* _aidl_return) override;
75     ::ndk::ScopedAStatus getPropertyByteArray(const std::string& in_propertyName,
76                                               std::vector<uint8_t>* _aidl_return) override;
77     ::ndk::ScopedAStatus getPropertyString(const std::string& in_propertyName,
78                                            std::string* _aidl_return) override;
79     ::ndk::ScopedAStatus getProvisionRequest(
80             const std::string& in_certificateType, const std::string& in_certificateAuthority,
81             ::aidl::android::hardware::drm::ProvisionRequest* _aidl_return) override;
82     ::ndk::ScopedAStatus getSecureStop(
83             const ::aidl::android::hardware::drm::SecureStopId& in_secureStopId,
84             ::aidl::android::hardware::drm::SecureStop* _aidl_return) override;
85     ::ndk::ScopedAStatus getSecureStopIds(
86             std::vector<::aidl::android::hardware::drm::SecureStopId>* _aidl_return) override;
87     ::ndk::ScopedAStatus getSecureStops(
88             std::vector<::aidl::android::hardware::drm::SecureStop>* _aidl_return) override;
89     ::ndk::ScopedAStatus getSecurityLevel(
90             const std::vector<uint8_t>& in_sessionId,
91             ::aidl::android::hardware::drm::SecurityLevel* _aidl_return) override;
92     ::ndk::ScopedAStatus openSession(::aidl::android::hardware::drm::SecurityLevel in_securityLevel,
93                                      std::vector<uint8_t>* _aidl_return) override;
94     ::ndk::ScopedAStatus provideKeyResponse(
95             const std::vector<uint8_t>& in_scope, const std::vector<uint8_t>& in_response,
96             ::aidl::android::hardware::drm::KeySetId* _aidl_return) override;
97     ::ndk::ScopedAStatus provideProvisionResponse(
98             const std::vector<uint8_t>& in_response,
99             ::aidl::android::hardware::drm::ProvideProvisionResponseResult* _aidl_return) override;
100     ::ndk::ScopedAStatus queryKeyStatus(
101             const std::vector<uint8_t>& in_sessionId,
102             std::vector<::aidl::android::hardware::drm::KeyValue>* _aidl_return) override;
103     ::ndk::ScopedAStatus releaseAllSecureStops() override;
104     ::ndk::ScopedAStatus releaseSecureStop(
105             const ::aidl::android::hardware::drm::SecureStopId& in_secureStopId) override;
106     ::ndk::ScopedAStatus releaseSecureStops(
107             const ::aidl::android::hardware::drm::OpaqueData& in_ssRelease) override;
108     ::ndk::ScopedAStatus removeAllSecureStops() override;
109     ::ndk::ScopedAStatus removeKeys(const std::vector<uint8_t>& in_sessionId) override;
110     ::ndk::ScopedAStatus removeOfflineLicense(
111             const ::aidl::android::hardware::drm::KeySetId& in_keySetId) override;
112     ::ndk::ScopedAStatus removeSecureStop(
113             const ::aidl::android::hardware::drm::SecureStopId& in_secureStopId) override;
114     ::ndk::ScopedAStatus requiresSecureDecoder(
115             const std::string& in_mime, ::aidl::android::hardware::drm::SecurityLevel in_level,
116             bool* _aidl_return) override;
117     ::ndk::ScopedAStatus restoreKeys(
118             const std::vector<uint8_t>& in_sessionId,
119             const ::aidl::android::hardware::drm::KeySetId& in_keySetId) override;
120     ::ndk::ScopedAStatus setCipherAlgorithm(const std::vector<uint8_t>& in_sessionId,
121                                             const std::string& in_algorithm) override;
122     ::ndk::ScopedAStatus setListener(
123             //            const ::android::sp<::aidl::android::hardware::drm::IDrmPluginListener>&
124             //            in_listener)
125             const std::shared_ptr<IDrmPluginListener>& in_listener) override;
126     ::ndk::ScopedAStatus setMacAlgorithm(const std::vector<uint8_t>& in_sessionId,
127                                          const std::string& in_algorithm) override;
128     ::ndk::ScopedAStatus setPlaybackId(const std::vector<uint8_t>& in_sessionId,
129                                        const std::string& in_playbackId) override;
130     ::ndk::ScopedAStatus setPropertyByteArray(const std::string& in_propertyName,
131                                               const std::vector<uint8_t>& in_value) override;
132     ::ndk::ScopedAStatus setPropertyString(const std::string& in_propertyName,
133                                            const std::string& in_value) override;
134     ::ndk::ScopedAStatus sign(const std::vector<uint8_t>& in_sessionId,
135                               const std::vector<uint8_t>& in_keyId,
136                               const std::vector<uint8_t>& in_message,
137                               std::vector<uint8_t>* _aidl_return) override;
138     ::ndk::ScopedAStatus signRSA(const std::vector<uint8_t>& in_sessionId,
139                                  const std::string& in_algorithm,
140                                  const std::vector<uint8_t>& in_message,
141                                  const std::vector<uint8_t>& in_wrappedkey,
142                                  std::vector<uint8_t>* _aidl_return) override;
143     ::ndk::ScopedAStatus verify(const std::vector<uint8_t>& in_sessionId,
144                                 const std::vector<uint8_t>& in_keyId,
145                                 const std::vector<uint8_t>& in_message,
146                                 const std::vector<uint8_t>& in_signature,
147                                 bool* _aidl_return) override;
148 
149   private:
150     void initProperties();
151     int32_t getIntProperty(const std::string& prop, int32_t defaultVal = 0) const;
152     int32_t getOemError() const;
153     int32_t getErrorContext() const;
154     void installSecureStop(const std::vector<uint8_t>& sessionId);
155     bool makeKeySetId(std::string* keySetId);
156     void setPlayPolicy();
157 
158     void sendEvent(::aidl::android::hardware::drm::EventType in_eventType,
159                    const std::vector<uint8_t>& in_sessionId,
160                    const std::vector<uint8_t>& in_data);
161     void sendExpirationUpdate(const std::vector<uint8_t>& in_sessionId,
162                               int64_t in_expiryTimeInMS);
163     void sendKeysChange(
164             const std::vector<uint8_t>& in_sessionId,
165             const std::vector<::aidl::android::hardware::drm::KeyStatus>& in_keyStatusList,
166             bool in_hasNewUsableKey);
167     void sendSessionLostState(const std::vector<uint8_t>& in_sessionId);
168 
169     Status setSecurityLevel(const std::vector<uint8_t>& sessionId, SecurityLevel level);
170 
171     Status getKeyRequestCommon(const std::vector<uint8_t>& scope,
172                                const std::vector<uint8_t>& initData, const std::string& mimeType,
173                                KeyType keyType, const std::vector<KeyValue>& optionalParameters,
174                                std::vector<uint8_t>* request, KeyRequestType* getKeyRequestType,
175                                std::string* defaultUrl);
176 
177     struct ClearkeySecureStop {
178         std::vector<uint8_t> id;
179         std::vector<uint8_t> data;
180     };
181 
182     std::map<std::vector<uint8_t>, ClearkeySecureStop> mSecureStops;
183     std::vector<KeyValue> mPlayPolicy;
184     std::map<std::string, std::string> mStringProperties;
185     std::map<std::string, std::vector<uint8_t>> mByteArrayProperties;
186     std::map<std::string, std::vector<uint8_t>> mReleaseKeysMap;
187     std::map<std::vector<uint8_t>, std::string> mPlaybackId;
188     std::map<std::vector<uint8_t>, SecurityLevel> mSecurityLevel
189         GUARDED_BY(mSecurityLevelLock);
190     ::std::shared_ptr<IDrmPluginListener> mListener;
191     SessionLibrary* mSessionLibrary;
192     int64_t mOpenSessionOkCount;
193     int64_t mCloseSessionOkCount;
194     int64_t mCloseSessionNotOpenedCount;
195     uint32_t mNextSecureStopId;
196     ::android::Mutex mPlayPolicyLock;
197 
198     // set by property to mock error scenarios
199     Status mMockError;
200 
processMockErrorDrmPlugin201     void processMockError(const ::android::sp<Session>& session) {
202         session->setMockError(static_cast<CdmResponseType>(mMockError));
203         mMockError = Status::OK;
204     }
205 
206     DeviceFiles mFileHandle;
207     ::android::Mutex mSecureStopLock;
208     ::android::Mutex mSecurityLevelLock;
209 
210     CLEARKEY_DISALLOW_COPY_AND_ASSIGN_AND_NEW(DrmPlugin);
211 };
212 
213 }  // namespace clearkey
214 }  // namespace drm
215 }  // namespace hardware
216 }  // namespace android
217 }  // namespace aidl
218