1 /*
2  * Copyright (C) 2018 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 CLEARKEY_DRM_PLUGIN_H_
18 #define CLEARKEY_DRM_PLUGIN_H_
19 
20 #include <android/hardware/drm/1.1/IDrmPlugin.h>
21 
22 #include <stdio.h>
23 #include <map>
24 
25 #include "SessionLibrary.h"
26 #include "Utils.h"
27 
28 namespace android {
29 namespace hardware {
30 namespace drm {
31 namespace V1_1 {
32 namespace clearkey {
33 
34 using ::android::hardware::drm::V1_0::EventType;
35 using ::android::hardware::drm::V1_0::IDrmPluginListener;
36 using ::android::hardware::drm::V1_0::KeyStatus;
37 using ::android::hardware::drm::V1_0::KeyType;
38 using ::android::hardware::drm::V1_0::KeyValue;
39 using ::android::hardware::drm::V1_0::SecureStop;
40 using ::android::hardware::drm::V1_0::SecureStopId;
41 using ::android::hardware::drm::V1_0::SessionId;
42 using ::android::hardware::drm::V1_0::Status;
43 using ::android::hardware::drm::V1_1::DrmMetricGroup;
44 using ::android::hardware::drm::V1_1::IDrmPlugin;
45 using ::android::hardware::drm::V1_1::KeyRequestType;
46 
47 using ::android::hardware::hidl_string;
48 using ::android::hardware::hidl_vec;
49 using ::android::hardware::Return;
50 using ::android::hardware::Void;
51 using ::android::sp;
52 
53 struct DrmPlugin : public IDrmPlugin {
54     explicit DrmPlugin(SessionLibrary* sessionLibrary);
55 
~DrmPluginDrmPlugin56     virtual ~DrmPlugin() {}
57 
58     Return<void> openSession(openSession_cb _hidl_cb) override;
59     Return<void> openSession_1_1(SecurityLevel securityLevel,
60             openSession_cb _hidl_cb) override;
61 
62     Return<Status> closeSession(const hidl_vec<uint8_t>& sessionId) override;
63 
64     Return<void> getKeyRequest(
65         const hidl_vec<uint8_t>& scope,
66         const hidl_vec<uint8_t>& initData,
67         const hidl_string& mimeType,
68         KeyType keyType,
69         const hidl_vec<KeyValue>& optionalParameters,
70         getKeyRequest_cb _hidl_cb) override;
71 
72     Return<void> getKeyRequest_1_1(
73         const hidl_vec<uint8_t>& scope,
74         const hidl_vec<uint8_t>& initData,
75         const hidl_string& mimeType,
76         KeyType keyType,
77         const hidl_vec<KeyValue>& optionalParameters,
78         getKeyRequest_1_1_cb _hidl_cb) override;
79 
80     Return<void> provideKeyResponse(
81         const hidl_vec<uint8_t>& scope,
82         const hidl_vec<uint8_t>& response,
83         provideKeyResponse_cb _hidl_cb) override;
84 
removeKeysDrmPlugin85     Return<Status> removeKeys(const hidl_vec<uint8_t>& sessionId) {
86         if (sessionId.size() == 0) {
87             return Status::BAD_VALUE;
88         }
89         return Status::ERROR_DRM_CANNOT_HANDLE;
90     }
91 
restoreKeysDrmPlugin92     Return<Status> restoreKeys(
93         const hidl_vec<uint8_t>& sessionId,
94         const hidl_vec<uint8_t>& keySetId) {
95 
96         if (sessionId.size() == 0 || keySetId.size() == 0) {
97             return Status::BAD_VALUE;
98         }
99         return Status::ERROR_DRM_CANNOT_HANDLE;
100     }
101 
102     Return<void> queryKeyStatus(
103         const hidl_vec<uint8_t>& sessionId,
104         queryKeyStatus_cb _hidl_cb) override;
105 
getProvisionRequestDrmPlugin106     Return<void> getProvisionRequest(
107         const hidl_string& certificateType,
108         const hidl_string& certificateAuthority,
109         getProvisionRequest_cb _hidl_cb) {
110         UNUSED(certificateType);
111         UNUSED(certificateAuthority);
112 
113         hidl_string defaultUrl;
114         _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, hidl_vec<uint8_t>(), defaultUrl);
115         return Void();
116     }
117 
provideProvisionResponseDrmPlugin118     Return<void> provideProvisionResponse(
119         const hidl_vec<uint8_t>& response,
120         provideProvisionResponse_cb _hidl_cb) {
121 
122         if (response.size() == 0) {
123             _hidl_cb(Status::BAD_VALUE, hidl_vec<uint8_t>(), hidl_vec<uint8_t>());
124             return Void();
125         }
126         _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, hidl_vec<uint8_t>(), hidl_vec<uint8_t>());
127         return Void();
128     }
129 
getHdcpLevelsDrmPlugin130     Return<void> getHdcpLevels(getHdcpLevels_cb _hidl_cb) {
131         HdcpLevel connectedLevel = HdcpLevel::HDCP_NONE;
132         HdcpLevel maxLevel = HdcpLevel::HDCP_NO_OUTPUT;
133         _hidl_cb(Status::OK, connectedLevel, maxLevel);
134         return Void();
135     }
136 
137     Return<void> getNumberOfSessions(getNumberOfSessions_cb _hidl_cb) override;
138 
139     Return<void> getSecurityLevel(const hidl_vec<uint8_t>& sessionId,
140             getSecurityLevel_cb _hidl_cb) override;
141 
142     Return<void> getMetrics(getMetrics_cb _hidl_cb) override;
143 
144     Return<void> getPropertyString(
145         const hidl_string& name,
146         getPropertyString_cb _hidl_cb) override;
147 
148     Return<void> getPropertyByteArray(
149         const hidl_string& name,
150         getPropertyByteArray_cb _hidl_cb) override;
151 
152     Return<Status> setPropertyString(
153             const hidl_string& name, const hidl_string& value) override;
154 
155     Return<Status> setPropertyByteArray(
156             const hidl_string& name, const hidl_vec<uint8_t>& value) override;
157 
setCipherAlgorithmDrmPlugin158     Return<Status> setCipherAlgorithm(
159             const hidl_vec<uint8_t>& sessionId, const hidl_string& algorithm) {
160         if (sessionId.size() == 0 || algorithm.size() == 0) {
161             return Status::BAD_VALUE;
162         }
163         return Status::ERROR_DRM_CANNOT_HANDLE;
164     }
165 
setMacAlgorithmDrmPlugin166     Return<Status> setMacAlgorithm(
167             const hidl_vec<uint8_t>& sessionId, const hidl_string& algorithm) {
168         if (sessionId.size() == 0 || algorithm.size() == 0) {
169             return Status::BAD_VALUE;
170         }
171         return Status::ERROR_DRM_CANNOT_HANDLE;
172     }
173 
encryptDrmPlugin174     Return<void> encrypt(
175             const hidl_vec<uint8_t>& sessionId,
176             const hidl_vec<uint8_t>& keyId,
177             const hidl_vec<uint8_t>& input,
178             const hidl_vec<uint8_t>& iv,
179             encrypt_cb _hidl_cb) {
180         if (sessionId.size() == 0 || keyId.size() == 0 ||
181                 input.size() == 0 || iv.size() == 0) {
182             _hidl_cb(Status::BAD_VALUE, hidl_vec<uint8_t>());
183             return Void();
184         }
185         _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, hidl_vec<uint8_t>());
186         return Void();
187     }
188 
decryptDrmPlugin189     Return<void> decrypt(
190             const hidl_vec<uint8_t>& sessionId,
191             const hidl_vec<uint8_t>& keyId,
192             const hidl_vec<uint8_t>& input,
193             const hidl_vec<uint8_t>& iv,
194             decrypt_cb _hidl_cb) {
195         if (sessionId.size() == 0 || keyId.size() == 0 ||
196                 input.size() == 0 || iv.size() == 0) {
197             _hidl_cb(Status::BAD_VALUE, hidl_vec<uint8_t>());
198             return Void();
199         }
200         _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, hidl_vec<uint8_t>());
201         return Void();
202     }
203 
signDrmPlugin204     Return<void> sign(
205             const hidl_vec<uint8_t>& sessionId,
206             const hidl_vec<uint8_t>& keyId,
207             const hidl_vec<uint8_t>& message,
208             sign_cb _hidl_cb) {
209         if (sessionId.size() == 0 || keyId.size() == 0 ||
210                 message.size() == 0) {
211             _hidl_cb(Status::BAD_VALUE, hidl_vec<uint8_t>());
212             return Void();
213         }
214         _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, hidl_vec<uint8_t>());
215         return Void();
216     }
217 
verifyDrmPlugin218     Return<void> verify(
219             const hidl_vec<uint8_t>& sessionId,
220             const hidl_vec<uint8_t>& keyId,
221             const hidl_vec<uint8_t>& message,
222             const hidl_vec<uint8_t>& signature,
223             verify_cb _hidl_cb) {
224 
225         if (sessionId.size() == 0 || keyId.size() == 0 ||
226                 message.size() == 0 || signature.size() == 0) {
227             _hidl_cb(Status::BAD_VALUE, false);
228             return Void();
229         }
230         _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, false);
231         return Void();
232     }
233 
signRSADrmPlugin234     Return<void> signRSA(
235             const hidl_vec<uint8_t>& sessionId,
236             const hidl_string& algorithm,
237             const hidl_vec<uint8_t>& message,
238             const hidl_vec<uint8_t>& wrappedKey,
239             signRSA_cb _hidl_cb) {
240         if (sessionId.size() == 0 || algorithm.size() == 0 ||
241                 message.size() == 0 || wrappedKey.size() == 0) {
242              _hidl_cb(Status::BAD_VALUE, hidl_vec<uint8_t>());
243              return Void();
244          }
245          _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, hidl_vec<uint8_t>());
246          return Void();
247     }
248 
setListenerDrmPlugin249     Return<void> setListener(const sp<IDrmPluginListener>& listener) {
250         mListener = listener;
251         return Void();
252     };
253 
sendEventDrmPlugin254     Return<void> sendEvent(EventType eventType, const hidl_vec<uint8_t>& sessionId,
255             const hidl_vec<uint8_t>& data) {
256         if (mListener != NULL) {
257             mListener->sendEvent(eventType, sessionId, data);
258         } else {
259             ALOGE("Null event listener, event not sent");
260         }
261         return Void();
262     }
263 
sendExpirationUpdateDrmPlugin264     Return<void> sendExpirationUpdate(const hidl_vec<uint8_t>& sessionId, int64_t expiryTimeInMS) {
265         if (mListener != NULL) {
266             mListener->sendExpirationUpdate(sessionId, expiryTimeInMS);
267         } else {
268             ALOGE("Null event listener, event not sent");
269         }
270         return Void();
271     }
272 
sendKeysChangeDrmPlugin273     Return<void> sendKeysChange(const hidl_vec<uint8_t>& sessionId,
274             const hidl_vec<KeyStatus>& keyStatusList, bool hasNewUsableKey) {
275         if (mListener != NULL) {
276             mListener->sendKeysChange(sessionId, keyStatusList, hasNewUsableKey);
277         } else {
278             ALOGE("Null event listener, event not sent");
279         }
280         return Void();
281     }
282 
283     Return<void> getSecureStops(getSecureStops_cb _hidl_cb);
284 
285     Return<void> getSecureStop(const hidl_vec<uint8_t>& secureStopId,
286             getSecureStop_cb _hidl_cb);
287 
288     Return<Status> releaseSecureStop(const hidl_vec<uint8_t>& ssRelease);
289 
290     Return<Status> releaseAllSecureStops();
291 
292     Return<void> getSecureStopIds(getSecureStopIds_cb _hidl_cb);
293 
294     Return<Status> releaseSecureStops(const SecureStopRelease& ssRelease);
295 
296     Return<Status> removeSecureStop(const hidl_vec<uint8_t>& secureStopId);
297 
298     Return<Status> removeAllSecureStops();
299 
300 private:
301     void initProperties();
302     void installSecureStop(const hidl_vec<uint8_t>& sessionId);
303     void setPlayPolicy();
304 
305     Return<Status> setSecurityLevel(const hidl_vec<uint8_t>& sessionId,
306             SecurityLevel level);
307 
308     Status getKeyRequestCommon(const hidl_vec<uint8_t>& scope,
309             const hidl_vec<uint8_t>& initData,
310             const hidl_string& mimeType,
311             KeyType keyType,
312             const hidl_vec<KeyValue>& optionalParameters,
313             std::vector<uint8_t> *request,
314             KeyRequestType *getKeyRequestType,
315             std::string *defaultUrl);
316 
317     struct ClearkeySecureStop {
318         std::vector<uint8_t> id;
319         std::vector<uint8_t> data;
320     };
321 
322     std::map<std::vector<uint8_t>, ClearkeySecureStop> mSecureStops;
323     std::vector<KeyValue> mPlayPolicy;
324     std::map<std::string, std::string> mStringProperties;
325     std::map<std::string, std::vector<uint8_t> > mByteArrayProperties;
326     std::map<std::vector<uint8_t>, SecurityLevel> mSecurityLevel;
327     sp<IDrmPluginListener> mListener;
328     SessionLibrary *mSessionLibrary;
329     int64_t mOpenSessionOkCount;
330     int64_t mCloseSessionOkCount;
331     int64_t mCloseSessionNotOpenedCount;
332     uint32_t mNextSecureStopId;
333 
334     CLEARKEY_DISALLOW_COPY_AND_ASSIGN_AND_NEW(DrmPlugin);
335 };
336 
337 } // namespace clearkey
338 } // namespace V1_1
339 } // namespace drm
340 } // namespace hardware
341 } // namespace android
342 
343 #endif // CLEARKEY_DRM_PLUGIN_H_
344