1 /*
2  * Copyright (C) 2017 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 
17 #ifndef DRM_HAL_H_
18 
19 #define DRM_HAL_H_
20 
21 #include <android/hardware/drm/1.0/IDrmFactory.h>
22 #include <android/hardware/drm/1.0/IDrmPlugin.h>
23 #include <android/hardware/drm/1.1/IDrmFactory.h>
24 #include <android/hardware/drm/1.1/IDrmPlugin.h>
25 #include <android/hardware/drm/1.2/IDrmFactory.h>
26 #include <android/hardware/drm/1.2/IDrmPlugin.h>
27 #include <android/hardware/drm/1.2/IDrmPluginListener.h>
28 
29 #include <media/IResourceManagerService.h>
30 #include <media/MediaAnalyticsItem.h>
31 #include <mediadrm/DrmMetrics.h>
32 #include <mediadrm/DrmSessionManager.h>
33 #include <mediadrm/IDrm.h>
34 #include <mediadrm/IDrmClient.h>
35 #include <utils/threads.h>
36 
37 namespace drm = ::android::hardware::drm;
38 using drm::V1_0::EventType;
39 using drm::V1_0::IDrmFactory;
40 using drm::V1_0::IDrmPlugin;
41 using drm::V1_0::IDrmPluginListener;
42 using drm::V1_1::SecurityLevel;
43 using drm::V1_2::KeyStatus;
44 using drm::V1_2::OfflineLicenseState;
45 using ::android::hardware::hidl_vec;
46 using ::android::hardware::Return;
47 using ::android::hardware::Void;
48 
49 typedef drm::V1_2::IDrmPluginListener IDrmPluginListener_V1_2;
50 typedef drm::V1_0::KeyStatus KeyStatus_V1_0;
51 
52 namespace android {
53 
54 struct DrmSessionClientInterface;
55 
56 inline bool operator==(const Vector<uint8_t> &l, const Vector<uint8_t> &r) {
57     if (l.size() != r.size()) return false;
58     return memcmp(l.array(), r.array(), l.size()) == 0;
59 }
60 
61 struct DrmHal : public BnDrm,
62                 public IBinder::DeathRecipient,
63                 public IDrmPluginListener_V1_2 {
64 
65     struct DrmSessionClient : public BnResourceManagerClient {
DrmSessionClientDrmHal::DrmSessionClient66         explicit DrmSessionClient(DrmHal* drm, const Vector<uint8_t>& sessionId)
67           : mSessionId(sessionId),
68             mDrm(drm) {}
69 
70         virtual bool reclaimResource();
71         virtual String8 getName();
72 
73         const Vector<uint8_t> mSessionId;
74 
75     protected:
76         virtual ~DrmSessionClient();
77 
78     private:
79         wp<DrmHal> mDrm;
80 
81         DISALLOW_EVIL_CONSTRUCTORS(DrmSessionClient);
82     };
83 
84     DrmHal();
85     virtual ~DrmHal();
86 
87     virtual status_t initCheck() const;
88 
89     virtual status_t isCryptoSchemeSupported(const uint8_t uuid[16],
90                                              const String8& mimeType,
91                                              DrmPlugin::SecurityLevel level,
92                                              bool *isSupported);
93 
94     virtual status_t createPlugin(const uint8_t uuid[16],
95                                   const String8 &appPackageName);
96 
97     virtual status_t destroyPlugin();
98 
99     virtual status_t openSession(DrmPlugin::SecurityLevel level,
100             Vector<uint8_t> &sessionId);
101 
102     virtual status_t closeSession(Vector<uint8_t> const &sessionId);
103 
104     virtual status_t
105         getKeyRequest(Vector<uint8_t> const &sessionId,
106                       Vector<uint8_t> const &initData,
107                       String8 const &mimeType, DrmPlugin::KeyType keyType,
108                       KeyedVector<String8, String8> const &optionalParameters,
109                       Vector<uint8_t> &request, String8 &defaultUrl,
110                       DrmPlugin::KeyRequestType *keyRequestType);
111 
112     virtual status_t provideKeyResponse(Vector<uint8_t> const &sessionId,
113                                         Vector<uint8_t> const &response,
114                                         Vector<uint8_t> &keySetId);
115 
116     virtual status_t removeKeys(Vector<uint8_t> const &keySetId);
117 
118     virtual status_t restoreKeys(Vector<uint8_t> const &sessionId,
119                                  Vector<uint8_t> const &keySetId);
120 
121     virtual status_t queryKeyStatus(Vector<uint8_t> const &sessionId,
122                                     KeyedVector<String8, String8> &infoMap) const;
123 
124     virtual status_t getProvisionRequest(String8 const &certType,
125                                          String8 const &certAuthority,
126                                          Vector<uint8_t> &request,
127                                          String8 &defaulUrl);
128 
129     virtual status_t provideProvisionResponse(Vector<uint8_t> const &response,
130                                               Vector<uint8_t> &certificate,
131                                               Vector<uint8_t> &wrappedKey);
132 
133     virtual status_t getSecureStops(List<Vector<uint8_t>> &secureStops);
134     virtual status_t getSecureStopIds(List<Vector<uint8_t>> &secureStopIds);
135     virtual status_t getSecureStop(Vector<uint8_t> const &ssid, Vector<uint8_t> &secureStop);
136 
137     virtual status_t releaseSecureStops(Vector<uint8_t> const &ssRelease);
138     virtual status_t removeSecureStop(Vector<uint8_t> const &ssid);
139     virtual status_t removeAllSecureStops();
140 
141     virtual status_t getHdcpLevels(DrmPlugin::HdcpLevel *connectedLevel,
142             DrmPlugin::HdcpLevel *maxLevel) const;
143     virtual status_t getNumberOfSessions(uint32_t *currentSessions,
144             uint32_t *maxSessions) const;
145     virtual status_t getSecurityLevel(Vector<uint8_t> const &sessionId,
146             DrmPlugin::SecurityLevel *level) const;
147 
148     virtual status_t getOfflineLicenseKeySetIds(List<Vector<uint8_t>> &keySetIds) const;
149     virtual status_t removeOfflineLicense(Vector<uint8_t> const &keySetId);
150     virtual status_t getOfflineLicenseState(Vector<uint8_t> const &keySetId,
151             DrmPlugin::OfflineLicenseState *licenseState) const;
152 
153     virtual status_t getPropertyString(String8 const &name, String8 &value ) const;
154     virtual status_t getPropertyByteArray(String8 const &name,
155                                           Vector<uint8_t> &value ) const;
156     virtual status_t setPropertyString(String8 const &name, String8 const &value ) const;
157     virtual status_t setPropertyByteArray(String8 const &name,
158                                           Vector<uint8_t> const &value ) const;
159     virtual status_t getMetrics(os::PersistableBundle *metrics);
160 
161     virtual status_t setCipherAlgorithm(Vector<uint8_t> const &sessionId,
162                                         String8 const &algorithm);
163 
164     virtual status_t setMacAlgorithm(Vector<uint8_t> const &sessionId,
165                                      String8 const &algorithm);
166 
167     virtual status_t encrypt(Vector<uint8_t> const &sessionId,
168                              Vector<uint8_t> const &keyId,
169                              Vector<uint8_t> const &input,
170                              Vector<uint8_t> const &iv,
171                              Vector<uint8_t> &output);
172 
173     virtual status_t decrypt(Vector<uint8_t> const &sessionId,
174                              Vector<uint8_t> const &keyId,
175                              Vector<uint8_t> const &input,
176                              Vector<uint8_t> const &iv,
177                              Vector<uint8_t> &output);
178 
179     virtual status_t sign(Vector<uint8_t> const &sessionId,
180                           Vector<uint8_t> const &keyId,
181                           Vector<uint8_t> const &message,
182                           Vector<uint8_t> &signature);
183 
184     virtual status_t verify(Vector<uint8_t> const &sessionId,
185                             Vector<uint8_t> const &keyId,
186                             Vector<uint8_t> const &message,
187                             Vector<uint8_t> const &signature,
188                             bool &match);
189 
190     virtual status_t signRSA(Vector<uint8_t> const &sessionId,
191                              String8 const &algorithm,
192                              Vector<uint8_t> const &message,
193                              Vector<uint8_t> const &wrappedKey,
194                              Vector<uint8_t> &signature);
195 
196     virtual status_t setListener(const sp<IDrmClient>& listener);
197 
198     // Methods of IDrmPluginListener
199     Return<void> sendEvent(EventType eventType,
200             const hidl_vec<uint8_t>& sessionId, const hidl_vec<uint8_t>& data);
201 
202     Return<void> sendExpirationUpdate(const hidl_vec<uint8_t>& sessionId,
203             int64_t expiryTimeInMS);
204 
205     Return<void> sendKeysChange(const hidl_vec<uint8_t>& sessionId,
206             const hidl_vec<KeyStatus_V1_0>& keyStatusList, bool hasNewUsableKey);
207 
208     Return<void> sendKeysChange_1_2(const hidl_vec<uint8_t>& sessionId,
209             const hidl_vec<KeyStatus>& keyStatusList, bool hasNewUsableKey);
210 
211     Return<void> sendSessionLostState(const hidl_vec<uint8_t>& sessionId);
212 
213     virtual void binderDied(const wp<IBinder> &the_late_who);
214 
215 private:
216     static Mutex mLock;
217 
218     sp<IDrmClient> mListener;
219     mutable Mutex mEventLock;
220     mutable Mutex mNotifyLock;
221 
222     const Vector<sp<IDrmFactory>> mFactories;
223     sp<IDrmPlugin> mPlugin;
224     sp<drm::V1_1::IDrmPlugin> mPluginV1_1;
225     sp<drm::V1_2::IDrmPlugin> mPluginV1_2;
226     String8 mAppPackageName;
227 
228     // Mutable to allow modification within GetPropertyByteArray.
229     mutable MediaDrmMetrics mMetrics;
230 
231     Vector<sp<DrmSessionClient>> mOpenSessions;
232     void closeOpenSessions();
233     void cleanup();
234 
235     /**
236      * mInitCheck is:
237      *   NO_INIT if a plugin hasn't been created yet
238      *   ERROR_UNSUPPORTED if a plugin can't be created for the uuid
239      *   OK after a plugin has been created and mPlugin is valid
240      */
241     status_t mInitCheck;
242 
243     Vector<sp<IDrmFactory>> makeDrmFactories();
244     sp<IDrmPlugin> makeDrmPlugin(const sp<IDrmFactory>& factory,
245             const uint8_t uuid[16], const String8& appPackageName);
246 
247     void writeByteArray(Parcel &obj, const hidl_vec<uint8_t>& array);
248 
249     void reportPluginMetrics() const;
250     void reportFrameworkMetrics() const;
251     status_t getPropertyStringInternal(String8 const &name, String8 &value) const;
252     status_t getPropertyByteArrayInternal(String8 const &name,
253                                           Vector<uint8_t> &value) const;
254     status_t matchMimeTypeAndSecurityLevel(const sp<IDrmFactory> &factory,
255                                            const uint8_t uuid[16],
256                                            const String8 &mimeType,
257                                            DrmPlugin::SecurityLevel level,
258                                            bool *isSupported);
259 
260     DISALLOW_EVIL_CONSTRUCTORS(DrmHal);
261 };
262 
263 }  // namespace android
264 
265 #endif  // DRM_HAL_H_
266