1 /*
2  * Copyright (C) 2014 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 <media/drm/DrmAPI.h>
21 #include <media/stagefright/foundation/ABase.h>
22 #include <media/stagefright/MediaErrors.h>
23 #include <utils/Errors.h>
24 #include <utils/KeyedVector.h>
25 #include <utils/List.h>
26 #include <utils/String8.h>
27 #include <utils/Vector.h>
28 
29 #include "SessionLibrary.h"
30 #include "Utils.h"
31 
32 namespace clearkeydrm {
33 
34 using android::KeyedVector;
35 using android::List;
36 using android::status_t;
37 using android::String8;
38 using android::Vector;
39 
40 class DrmPlugin : public android::DrmPlugin {
41 public:
42     explicit DrmPlugin(SessionLibrary* sessionLibrary);
43 
~DrmPlugin()44     virtual ~DrmPlugin() {}
45 
46     virtual status_t openSession(Vector<uint8_t>& sessionId);
47 
48     virtual status_t closeSession(const Vector<uint8_t>& sessionId);
49 
50     virtual status_t getKeyRequest(
51             const Vector<uint8_t>& scope,
52             const Vector<uint8_t>& mimeType,
53             const String8& initDataType,
54             KeyType keyType,
55             const KeyedVector<String8, String8>& optionalParameters,
56             Vector<uint8_t>& request,
57             String8& defaultUrl,
58             DrmPlugin::KeyRequestType *keyRequestType);
59 
60     virtual status_t provideKeyResponse(
61             const Vector<uint8_t>& scope,
62             const Vector<uint8_t>& response,
63             Vector<uint8_t>& keySetId);
64 
removeKeys(const Vector<uint8_t> & sessionId)65     virtual status_t removeKeys(const Vector<uint8_t>& sessionId) {
66         if (sessionId.size() == 0) {
67             return android::BAD_VALUE;
68         }
69 
70         return android::ERROR_DRM_CANNOT_HANDLE;
71     }
72 
restoreKeys(const Vector<uint8_t> & sessionId,const Vector<uint8_t> & keySetId)73     virtual status_t restoreKeys(
74             const Vector<uint8_t>& sessionId,
75             const Vector<uint8_t>& keySetId) {
76         if (sessionId.size() == 0 || keySetId.size() == 0) {
77             return android::BAD_VALUE;
78         }
79         return android::ERROR_DRM_CANNOT_HANDLE;
80     }
81 
82     virtual status_t queryKeyStatus(
83             const Vector<uint8_t>& sessionId,
84             KeyedVector<String8, String8>& infoMap) const;
85 
getProvisionRequest(const String8 & cert_type,const String8 & cert_authority,Vector<uint8_t> & request,String8 & defaultUrl)86     virtual status_t getProvisionRequest(
87             const String8& cert_type,
88             const String8& cert_authority,
89             Vector<uint8_t>& request,
90             String8& defaultUrl) {
91         UNUSED(cert_type);
92         UNUSED(cert_authority);
93         UNUSED(request);
94         UNUSED(defaultUrl);
95         return android::ERROR_DRM_CANNOT_HANDLE;
96     }
97 
provideProvisionResponse(const Vector<uint8_t> & response,Vector<uint8_t> & certificate,Vector<uint8_t> & wrappedKey)98     virtual status_t provideProvisionResponse(
99             const Vector<uint8_t>& response,
100             Vector<uint8_t>& certificate,
101             Vector<uint8_t>& wrappedKey) {
102         UNUSED(certificate);
103         UNUSED(wrappedKey);
104         if (response.size() == 0) {
105             // empty response
106             return android::BAD_VALUE;
107         }
108         return android::ERROR_DRM_CANNOT_HANDLE;
109     }
110 
getSecureStops(List<Vector<uint8_t>> & secureStops)111     virtual status_t getSecureStops(List<Vector<uint8_t> >& secureStops) {
112         UNUSED(secureStops);
113         return android::ERROR_DRM_CANNOT_HANDLE;
114     }
115 
getSecureStop(Vector<uint8_t> const & ssid,Vector<uint8_t> & secureStop)116     virtual status_t getSecureStop(Vector<uint8_t> const &ssid, Vector<uint8_t> &secureStop) {
117         if (ssid.size() == 0) {
118             return android::BAD_VALUE;
119         }
120 
121         UNUSED(secureStop);
122         return android::ERROR_DRM_CANNOT_HANDLE;
123     }
124 
releaseSecureStops(const Vector<uint8_t> & ssRelease)125     virtual status_t releaseSecureStops(const Vector<uint8_t>& ssRelease) {
126         if (ssRelease.size() == 0) {
127             return android::BAD_VALUE;
128         }
129         return android::ERROR_DRM_CANNOT_HANDLE;
130     }
131 
releaseAllSecureStops()132     virtual status_t releaseAllSecureStops() {
133         return android::ERROR_DRM_CANNOT_HANDLE;
134     }
135 
getHdcpLevels(HdcpLevel * connectedLevel,HdcpLevel * maxLevel)136     virtual status_t getHdcpLevels(HdcpLevel *connectedLevel,
137             HdcpLevel *maxLevel) const {
138         UNUSED(connectedLevel);
139         UNUSED(maxLevel);
140         return android::ERROR_DRM_CANNOT_HANDLE;
141     }
142 
143 
getNumberOfSessions(uint32_t * currentSessions,uint32_t * maxSessions)144     virtual status_t getNumberOfSessions(uint32_t *currentSessions,
145             uint32_t *maxSessions) const {
146         UNUSED(currentSessions);
147         UNUSED(maxSessions);
148         return android::ERROR_DRM_CANNOT_HANDLE;
149     }
150 
getSecurityLevel(Vector<uint8_t> const & sessionId,SecurityLevel * level)151     virtual status_t getSecurityLevel(Vector<uint8_t> const &sessionId,
152             SecurityLevel *level) const {
153         UNUSED(sessionId);
154         UNUSED(level);
155         return android::ERROR_DRM_CANNOT_HANDLE;
156     }
157 
setSecurityLevel(Vector<uint8_t> const & sessionId,const SecurityLevel & level)158     virtual status_t setSecurityLevel(Vector<uint8_t> const &sessionId,
159             const SecurityLevel& level) {
160         UNUSED(sessionId);
161         UNUSED(level);
162         return android::ERROR_DRM_CANNOT_HANDLE;
163     }
164 
165     virtual status_t getPropertyString(
166             const String8& name, String8& value) const;
167 
168     virtual status_t getPropertyByteArray(
169             const String8& name, Vector<uint8_t>& value) const;
170 
171     virtual status_t setPropertyString(
172             const String8& name, const String8& value);
173 
174     virtual status_t setPropertyByteArray(
175             const String8& name, const Vector<uint8_t>& value);
176 
setCipherAlgorithm(const Vector<uint8_t> & sessionId,const String8 & algorithm)177     virtual status_t setCipherAlgorithm(
178             const Vector<uint8_t>& sessionId, const String8& algorithm) {
179         if (sessionId.size() == 0 || algorithm.size() == 0) {
180             return android::BAD_VALUE;
181         }
182         return android::ERROR_DRM_CANNOT_HANDLE;
183     }
184 
setMacAlgorithm(const Vector<uint8_t> & sessionId,const String8 & algorithm)185     virtual status_t setMacAlgorithm(
186             const Vector<uint8_t>& sessionId, const String8& algorithm) {
187         if (sessionId.size() == 0 || algorithm.size() == 0) {
188             return android::BAD_VALUE;
189         }
190         return android::ERROR_DRM_CANNOT_HANDLE;
191     }
192 
encrypt(const Vector<uint8_t> & sessionId,const Vector<uint8_t> & keyId,const Vector<uint8_t> & input,const Vector<uint8_t> & iv,Vector<uint8_t> & output)193     virtual status_t encrypt(
194             const Vector<uint8_t>& sessionId,
195             const Vector<uint8_t>& keyId,
196             const Vector<uint8_t>& input,
197             const Vector<uint8_t>& iv,
198             Vector<uint8_t>& output) {
199         if (sessionId.size() == 0 || keyId.size() == 0 ||
200                 input.size() == 0 || iv.size() == 0) {
201             return android::BAD_VALUE;
202         }
203         UNUSED(output);
204         return android::ERROR_DRM_CANNOT_HANDLE;
205     }
206 
decrypt(const Vector<uint8_t> & sessionId,const Vector<uint8_t> & keyId,const Vector<uint8_t> & input,const Vector<uint8_t> & iv,Vector<uint8_t> & output)207     virtual status_t decrypt(
208             const Vector<uint8_t>& sessionId,
209             const Vector<uint8_t>& keyId,
210             const Vector<uint8_t>& input,
211             const Vector<uint8_t>& iv,
212             Vector<uint8_t>& output) {
213         if (sessionId.size() == 0 || keyId.size() == 0 ||
214                 input.size() == 0 || iv.size() == 0) {
215             return android::BAD_VALUE;
216         }
217         UNUSED(output);
218         return android::ERROR_DRM_CANNOT_HANDLE;
219     }
220 
sign(const Vector<uint8_t> & sessionId,const Vector<uint8_t> & keyId,const Vector<uint8_t> & message,Vector<uint8_t> & signature)221     virtual status_t sign(
222             const Vector<uint8_t>& sessionId,
223             const Vector<uint8_t>& keyId,
224             const Vector<uint8_t>& message,
225             Vector<uint8_t>& signature) {
226         if (sessionId.size() == 0 || keyId.size() == 0 ||
227                 message.size() == 0) {
228             return android::BAD_VALUE;
229         }
230         UNUSED(signature);
231         return android::ERROR_DRM_CANNOT_HANDLE;
232     }
233 
verify(const Vector<uint8_t> & sessionId,const Vector<uint8_t> & keyId,const Vector<uint8_t> & message,const Vector<uint8_t> & signature,bool & match)234     virtual status_t verify(
235             const Vector<uint8_t>& sessionId,
236             const Vector<uint8_t>& keyId,
237             const Vector<uint8_t>& message,
238             const Vector<uint8_t>& signature, bool& match) {
239         if (sessionId.size() == 0 || keyId.size() == 0 ||
240                 message.size() == 0 || signature.size() == 0) {
241             return android::BAD_VALUE;
242         }
243         UNUSED(match);
244         return android::ERROR_DRM_CANNOT_HANDLE;
245     }
246 
signRSA(const Vector<uint8_t> & sessionId,const String8 & algorithm,const Vector<uint8_t> & message,const Vector<uint8_t> & wrappedKey,Vector<uint8_t> & signature)247     virtual status_t signRSA(
248             const Vector<uint8_t>& sessionId,
249             const String8& algorithm,
250             const Vector<uint8_t>& message,
251             const Vector<uint8_t>& wrappedKey,
252             Vector<uint8_t>& signature) {
253         if (sessionId.size() == 0 || algorithm.size() == 0 ||
254                 message.size() == 0 || wrappedKey.size() == 0) {
255             return android::BAD_VALUE;
256         }
257         UNUSED(signature);
258         return android::ERROR_DRM_CANNOT_HANDLE;
259     }
260 
261 private:
262     void initProperties();
263     void setPlayPolicy();
264 
265     android::KeyedVector<String8, String8> mPlayPolicy;
266     android::KeyedVector<String8, String8> mStringProperties;
267     android::KeyedVector<String8, Vector<uint8_t>> mByteArrayProperties;
268 
269     SessionLibrary* mSessionLibrary;
270 
271     DISALLOW_EVIL_CONSTRUCTORS(DrmPlugin);
272 };
273 
274 } // namespace clearkeydrm
275 
276 #endif // CLEARKEY_DRM_PLUGIN_H_
277