blob: 6e84ab4534bc0f6e816854be801102ad613b1752 [file] [log] [blame]
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -07001/*
Jeff Tinker56c78c42013-02-07 17:46:18 -08002 * Copyright (C) 2013 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_API_H_
18#define DRM_API_H_
19
20#include <utils/List.h>
21#include <utils/String8.h>
22#include <utils/Vector.h>
23#include <utils/KeyedVector.h>
24#include <utils/RefBase.h>
Jeff Tinker7eafcae2013-04-02 13:16:21 -070025#include <utils/Mutex.h>
Jeff Tinker56c78c42013-02-07 17:46:18 -080026#include <media/stagefright/foundation/ABase.h>
27
28// Loadable DrmEngine shared libraries should define the entry points
29// createDrmFactory and createCryptoFactory as shown below:
30//
31// extern "C" {
32// extern android::DrmFactory *createDrmFactory();
33// extern android::CryptoFactory *createCryptoFactory();
34// }
35
36namespace android {
37
Jeff Tinker7eafcae2013-04-02 13:16:21 -070038 class DrmPlugin;
39 class DrmPluginListener;
Jeff Tinker56c78c42013-02-07 17:46:18 -080040
41 // DRMs are implemented in DrmEngine plugins, which are dynamically
42 // loadable shared libraries that implement the entry points
43 // createDrmFactory and createCryptoFactory. createDrmFactory
44 // constructs and returns an instance of a DrmFactory object. Similarly,
45 // createCryptoFactory creates an instance of a CryptoFactory object.
46 // When a MediaCrypto or MediaDrm object needs to be constructed, all
47 // available DrmEngines present in the plugins directory on the device
48 // are scanned for a matching DrmEngine that can support the crypto
49 // scheme. When a match is found, the DrmEngine's createCryptoPlugin and
50 // createDrmPlugin methods are used to create CryptoPlugin or
51 // DrmPlugin instances to support that DRM scheme.
52
53 class DrmFactory {
54 public:
55 DrmFactory() {}
56 virtual ~DrmFactory() {}
57
58 // DrmFactory::isCryptoSchemeSupported can be called to determine
59 // if the plugin factory is able to construct plugins that support a
60 // given crypto scheme, which is specified by a UUID.
61 virtual bool isCryptoSchemeSupported(const uint8_t uuid[16]) = 0;
62
Jeff Tinker611d3d42013-08-21 11:57:40 -070063 // DrmFactory::isContentTypeSupported can be called to determine
64 // if the plugin factory is able to construct plugins that support a
65 // given media container format specified by mimeType
66 virtual bool isContentTypeSupported(const String8 &mimeType) = 0;
67
Jeff Tinker56c78c42013-02-07 17:46:18 -080068 // Construct a DrmPlugin for the crypto scheme specified by UUID.
69 virtual status_t createDrmPlugin(
70 const uint8_t uuid[16], DrmPlugin **plugin) = 0;
71
72 private:
73 DrmFactory(const DrmFactory &);
74 DrmFactory &operator=(const DrmFactory &);
75 };
76
77 class DrmPlugin {
78 public:
79 enum EventType {
Jeff Tinker7eafcae2013-04-02 13:16:21 -070080 kDrmPluginEventProvisionRequired = 1,
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -070081 kDrmPluginEventKeyNeeded,
82 kDrmPluginEventKeyExpired,
Ronghua Wu46722ff2015-03-05 09:58:16 -080083 kDrmPluginEventVendorDefined,
84 kDrmPluginEventSessionReclaimed
Jeff Tinker56c78c42013-02-07 17:46:18 -080085 };
86
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -070087 // Drm keys can be for offline content or for online streaming.
88 // Offline keys are persisted on the device and may be used when the device
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -070089 // is disconnected from the network. The Release type is used to request
90 // that offline keys be no longer restricted to offline use.
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -070091 enum KeyType {
92 kKeyType_Offline,
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -070093 kKeyType_Streaming,
94 kKeyType_Release
Jeff Tinker56c78c42013-02-07 17:46:18 -080095 };
96
97 DrmPlugin() {}
98 virtual ~DrmPlugin() {}
99
100 // Open a new session with the DrmPlugin object. A session ID is returned
101 // in the sessionId parameter.
102 virtual status_t openSession(Vector<uint8_t> &sessionId) = 0;
103
104 // Close a session on the DrmPlugin object.
105 virtual status_t closeSession(Vector<uint8_t> const &sessionId) = 0;
106
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700107 // A key request/response exchange occurs between the app and a License
108 // Server to obtain the keys required to decrypt the content. getKeyRequest()
109 // is used to obtain an opaque key request blob that is delivered to the
Jeff Tinker56c78c42013-02-07 17:46:18 -0800110 // license server.
111 //
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -0700112 // The scope parameter may be a sessionId or a keySetId, depending on the
113 // specified keyType. When the keyType is kKeyType_Offline or
114 // kKeyType_Streaming, scope should be set to the sessionId the keys will be
115 // provided to. When the keyType is kKeyType_Release, scope should be set to
116 // the keySetId of the keys being released. Releasing keys from a device
117 // invalidates them for all sessions.
118 //
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700119 // The init data passed to getKeyRequest is container-specific and its
Jeff Tinker56c78c42013-02-07 17:46:18 -0800120 // meaning is interpreted based on the mime type provided in the mimeType
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700121 // parameter to getKeyRequest. It could contain, for example, the content
Jeff Tinker56c78c42013-02-07 17:46:18 -0800122 // ID, key ID or other data obtained from the content metadata that is required
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -0700123 // in generating the key request. Init may be null when keyType is
124 // kKeyType_Release.
Jeff Tinker56c78c42013-02-07 17:46:18 -0800125 //
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -0700126 // mimeType identifies the mime type of the content
127 //
128 // keyType specifies if the keys are to be used for streaming or offline content
Jeff Tinker56c78c42013-02-07 17:46:18 -0800129 //
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700130 // optionalParameters are included in the key request message to allow a
131 // client application to provide additional message parameters to the server.
Jeff Tinker56c78c42013-02-07 17:46:18 -0800132 //
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700133 // If successful, the opaque key request blob is returned to the caller.
Jeff Tinker56c78c42013-02-07 17:46:18 -0800134 virtual status_t
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -0700135 getKeyRequest(Vector<uint8_t> const &scope,
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700136 Vector<uint8_t> const &initData,
137 String8 const &mimeType, KeyType keyType,
138 KeyedVector<String8, String8> const &optionalParameters,
139 Vector<uint8_t> &request, String8 &defaultUrl) = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800140
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -0700141 //
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700142 // After a key response is received by the app, it is provided to the
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -0700143 // Drm plugin using provideKeyResponse.
144 //
145 // scope may be a sessionId or a keySetId depending on the type of the
146 // response. Scope should be set to the sessionId when the response is
147 // for either streaming or offline key requests. Scope should be set to the
148 // keySetId when the response is for a release request.
149 //
150 // When the response is for an offline key request, a keySetId is returned
151 // in the keySetId vector parameter that can be used to later restore the
152 // keys to a new session with the method restoreKeys. When the response is
153 // for a streaming or release request, no keySetId is returned.
154 //
155 virtual status_t provideKeyResponse(Vector<uint8_t> const &scope,
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700156 Vector<uint8_t> const &response,
157 Vector<uint8_t> &keySetId) = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800158
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -0700159 // Remove the current keys from a session
160 virtual status_t removeKeys(Vector<uint8_t> const &sessionId) = 0;
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700161
162 // Restore persisted offline keys into a new session. keySetId identifies
163 // the keys to load, obtained from a prior call to provideKeyResponse().
164 virtual status_t restoreKeys(Vector<uint8_t> const &sessionId,
165 Vector<uint8_t> const &keySetId) = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800166
167 // Request an informative description of the license for the session. The status
168 // is in the form of {name, value} pairs. Since DRM license policies vary by
169 // vendor, the specific status field names are determined by each DRM vendor.
170 // Refer to your DRM provider documentation for definitions of the field names
171 // for a particular DrmEngine.
172 virtual status_t
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700173 queryKeyStatus(Vector<uint8_t> const &sessionId,
174 KeyedVector<String8, String8> &infoMap) const = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800175
176 // A provision request/response exchange occurs between the app and a
177 // provisioning server to retrieve a device certificate. getProvisionRequest
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700178 // is used to obtain an opaque key request blob that is delivered to the
Jeff Tinker56c78c42013-02-07 17:46:18 -0800179 // provisioning server.
180 //
181 // If successful, the opaque provision request blob is returned to the caller.
Jeff Tinkerc2f10f22014-03-04 13:23:56 -0800182 virtual status_t getProvisionRequest(String8 const &cert_type,
183 String8 const &cert_authority,
184 Vector<uint8_t> &request,
Jeff Tinker56c78c42013-02-07 17:46:18 -0800185 String8 &defaultUrl) = 0;
186
187 // After a provision response is received by the app, it is provided to the
188 // Drm plugin using provideProvisionResponse.
Jeff Tinkerc2f10f22014-03-04 13:23:56 -0800189 virtual status_t provideProvisionResponse(Vector<uint8_t> const &response,
190 Vector<uint8_t> &certificate,
191 Vector<uint8_t> &wrapped_key) = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800192
Jeff Tinkerbc6b9e72014-04-30 10:21:53 -0700193 // Remove device provisioning.
194 virtual status_t unprovisionDevice() = 0;
195
Jeff Tinker56c78c42013-02-07 17:46:18 -0800196 // A means of enforcing the contractual requirement for a concurrent stream
197 // limit per subscriber across devices is provided via SecureStop. SecureStop
198 // is a means of securely monitoring the lifetime of sessions. Since playback
199 // on a device can be interrupted due to reboot, power failure, etc. a means
200 // of persisting the lifetime information on the device is needed.
201 //
202 // A signed version of the sessionID is written to persistent storage on the
203 // device when each MediaCrypto object is created. The sessionID is signed by
204 // the device private key to prevent tampering.
205 //
206 // In the normal case, playback will be completed, the session destroyed and
207 // the Secure Stops will be queried. The App queries secure stops and forwards
208 // the secure stop message to the server which verifies the signature and
209 // notifies the server side database that the session destruction has been
210 // confirmed. The persisted record on the client is only removed after positive
211 // confirmation that the server received the message using releaseSecureStops().
212 virtual status_t getSecureStops(List<Vector<uint8_t> > &secureStops) = 0;
Jeff Tinker57481592014-10-31 00:42:33 -0700213 virtual status_t getSecureStop(Vector<uint8_t> const &ssid, Vector<uint8_t> &secureStop) = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800214 virtual status_t releaseSecureStops(Vector<uint8_t> const &ssRelease) = 0;
Jeff Tinker57481592014-10-31 00:42:33 -0700215 virtual status_t releaseAllSecureStops() = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800216
217 // Read a property value given the device property string. There are a few forms
218 // of property access methods, depending on the data type returned.
219 // Since DRM plugin properties may vary, additional field names may be defined
220 // by each DRM vendor. Refer to your DRM provider documentation for definitions
221 // of its additional field names.
222 //
223 // Standard values are:
224 // "vendor" [string] identifies the maker of the plugin
225 // "version" [string] identifies the version of the plugin
226 // "description" [string] describes the plugin
227 // 'deviceUniqueId' [byte array] The device unique identifier is established
228 // during device provisioning and provides a means of uniquely identifying
229 // each device.
230 virtual status_t getPropertyString(String8 const &name, String8 &value ) const = 0;
231 virtual status_t getPropertyByteArray(String8 const &name,
232 Vector<uint8_t> &value ) const = 0;
233
234 // Write a property value given the device property string. There are a few forms
235 // of property setting methods, depending on the data type.
236 // Since DRM plugin properties may vary, additional field names may be defined
237 // by each DRM vendor. Refer to your DRM provider documentation for definitions
238 // of its field names.
239 virtual status_t setPropertyString(String8 const &name,
240 String8 const &value ) = 0;
241 virtual status_t setPropertyByteArray(String8 const &name,
242 Vector<uint8_t> const &value ) = 0;
243
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700244 // The following methods implement operations on a CryptoSession to support
245 // encrypt, decrypt, sign verify operations on operator-provided
246 // session keys.
247
248 //
249 // The algorithm string conforms to JCA Standard Names for Cipher
250 // Transforms and is case insensitive. For example "AES/CBC/PKCS5Padding".
251 //
252 // Return OK if the algorithm is supported, otherwise return BAD_VALUE
253 //
254 virtual status_t setCipherAlgorithm(Vector<uint8_t> const &sessionId,
255 String8 const &algorithm) = 0;
256
257 //
258 // The algorithm string conforms to JCA Standard Names for Mac
259 // Algorithms and is case insensitive. For example "HmacSHA256".
260 //
261 // Return OK if the algorithm is supported, otherwise return BAD_VALUE
262 //
263 virtual status_t setMacAlgorithm(Vector<uint8_t> const &sessionId,
264 String8 const &algorithm) = 0;
265
266 // Encrypt the provided input buffer with the cipher algorithm
267 // specified by setCipherAlgorithm and the key selected by keyId,
268 // and return the encrypted data.
269 virtual status_t encrypt(Vector<uint8_t> const &sessionId,
270 Vector<uint8_t> const &keyId,
271 Vector<uint8_t> const &input,
272 Vector<uint8_t> const &iv,
273 Vector<uint8_t> &output) = 0;
274
275 // Decrypt the provided input buffer with the cipher algorithm
276 // specified by setCipherAlgorithm and the key selected by keyId,
277 // and return the decrypted data.
278 virtual status_t decrypt(Vector<uint8_t> const &sessionId,
279 Vector<uint8_t> const &keyId,
280 Vector<uint8_t> const &input,
281 Vector<uint8_t> const &iv,
282 Vector<uint8_t> &output) = 0;
283
284 // Compute a signature on the provided message using the mac algorithm
285 // specified by setMacAlgorithm and the key selected by keyId,
286 // and return the signature.
287 virtual status_t sign(Vector<uint8_t> const &sessionId,
288 Vector<uint8_t> const &keyId,
289 Vector<uint8_t> const &message,
290 Vector<uint8_t> &signature) = 0;
291
292 // Compute a signature on the provided message using the mac algorithm
293 // specified by setMacAlgorithm and the key selected by keyId,
294 // and compare with the expected result. Set result to true or
295 // false depending on the outcome.
296 virtual status_t verify(Vector<uint8_t> const &sessionId,
297 Vector<uint8_t> const &keyId,
298 Vector<uint8_t> const &message,
299 Vector<uint8_t> const &signature,
300 bool &match) = 0;
301
302
Jeff Tinkerc2f10f22014-03-04 13:23:56 -0800303 // Compute an RSA signature on the provided message using the algorithm
304 // specified by algorithm.
305 virtual status_t signRSA(Vector<uint8_t> const &sessionId,
306 String8 const &algorithm,
307 Vector<uint8_t> const &message,
308 Vector<uint8_t> const &wrapped_key,
309 Vector<uint8_t> &signature) = 0;
310
311
Jeff Tinker7eafcae2013-04-02 13:16:21 -0700312 status_t setListener(const sp<DrmPluginListener>& listener) {
313 Mutex::Autolock lock(mEventLock);
314 mListener = listener;
315 return OK;
316 }
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700317
Jeff Tinker7eafcae2013-04-02 13:16:21 -0700318 protected:
319 // Plugins call sendEvent to deliver events to the java app
320 void sendEvent(EventType eventType, int extra,
321 Vector<uint8_t> const *sessionId,
322 Vector<uint8_t> const *data);
323
Jeff Tinker56c78c42013-02-07 17:46:18 -0800324 private:
Jeff Tinker7eafcae2013-04-02 13:16:21 -0700325 Mutex mEventLock;
326 sp<DrmPluginListener> mListener;
327
Jeff Tinker56c78c42013-02-07 17:46:18 -0800328 DISALLOW_EVIL_CONSTRUCTORS(DrmPlugin);
329 };
330
Jeff Tinker7eafcae2013-04-02 13:16:21 -0700331 class DrmPluginListener: virtual public RefBase
332 {
333 public:
334 virtual void sendEvent(DrmPlugin::EventType eventType, int extra,
335 Vector<uint8_t> const *sesionId,
336 Vector<uint8_t> const *data) = 0;
337 };
338
339 inline void DrmPlugin::sendEvent(EventType eventType, int extra,
340 Vector<uint8_t> const *sessionId,
341 Vector<uint8_t> const *data) {
342
343 mEventLock.lock();
344 sp<DrmPluginListener> listener = mListener;
345 mEventLock.unlock();
346
347 if (listener != NULL) {
348 listener->sendEvent(eventType, extra, sessionId, data);
349 }
350 }
351
Jeff Tinker56c78c42013-02-07 17:46:18 -0800352} // namespace android
353
354#endif // DRM_API_H_