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