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