blob: 985d919220cc6e1b437c17fe7ed996c33acb6ee4 [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,
Jeff Tinker9a498ef2015-03-31 15:47:24 -070084 kDrmPluginEventSessionReclaimed,
85 kDrmPluginEventExpirationUpdate,
86 kDrmPluginEventKeysChange,
Jeff Tinker56c78c42013-02-07 17:46:18 -080087 };
88
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -070089 // Drm keys can be for offline content or for online streaming.
90 // Offline keys are persisted on the device and may be used when the device
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -070091 // is disconnected from the network. The Release type is used to request
92 // that offline keys be no longer restricted to offline use.
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -070093 enum KeyType {
94 kKeyType_Offline,
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -070095 kKeyType_Streaming,
96 kKeyType_Release
Jeff Tinker56c78c42013-02-07 17:46:18 -080097 };
98
Jeff Tinker06a8cd62015-03-16 13:04:40 -070099 // Enumerate KeyRequestTypes to allow an app to determine the
100 // type of a key request returned from getKeyRequest.
101 enum KeyRequestType {
102 kKeyRequestType_Unknown,
103 kKeyRequestType_Initial,
104 kKeyRequestType_Renewal,
105 kKeyRequestType_Release
106 };
107
Jeff Tinker9a498ef2015-03-31 15:47:24 -0700108 // Enumerate KeyStatusTypes which indicate the state of a key
109 enum KeyStatusType
110 {
111 kKeyStatusType_Usable,
112 kKeyStatusType_Expired,
113 kKeyStatusType_OutputNotAllowed,
114 kKeyStatusType_StatusPending,
115 kKeyStatusType_InternalError
116 };
117
118 // Used by sendKeysChange to report the usability status of each
119 // key to the app.
120 struct KeyStatus
121 {
122 Vector<uint8_t> mKeyId;
123 KeyStatusType mType;
124 };
125
Jeff Tinker56c78c42013-02-07 17:46:18 -0800126 DrmPlugin() {}
127 virtual ~DrmPlugin() {}
128
129 // Open a new session with the DrmPlugin object. A session ID is returned
130 // in the sessionId parameter.
131 virtual status_t openSession(Vector<uint8_t> &sessionId) = 0;
132
133 // Close a session on the DrmPlugin object.
134 virtual status_t closeSession(Vector<uint8_t> const &sessionId) = 0;
135
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700136 // A key request/response exchange occurs between the app and a License
137 // Server to obtain the keys required to decrypt the content. getKeyRequest()
138 // is used to obtain an opaque key request blob that is delivered to the
Jeff Tinker56c78c42013-02-07 17:46:18 -0800139 // license server.
140 //
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -0700141 // The scope parameter may be a sessionId or a keySetId, depending on the
142 // specified keyType. When the keyType is kKeyType_Offline or
143 // kKeyType_Streaming, scope should be set to the sessionId the keys will be
144 // provided to. When the keyType is kKeyType_Release, scope should be set to
145 // the keySetId of the keys being released. Releasing keys from a device
146 // invalidates them for all sessions.
147 //
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700148 // The init data passed to getKeyRequest is container-specific and its
Jeff Tinker56c78c42013-02-07 17:46:18 -0800149 // meaning is interpreted based on the mime type provided in the mimeType
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700150 // parameter to getKeyRequest. It could contain, for example, the content
Jeff Tinker56c78c42013-02-07 17:46:18 -0800151 // ID, key ID or other data obtained from the content metadata that is required
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -0700152 // in generating the key request. Init may be null when keyType is
153 // kKeyType_Release.
Jeff Tinker56c78c42013-02-07 17:46:18 -0800154 //
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -0700155 // mimeType identifies the mime type of the content
156 //
157 // keyType specifies if the keys are to be used for streaming or offline content
Jeff Tinker56c78c42013-02-07 17:46:18 -0800158 //
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700159 // optionalParameters are included in the key request message to allow a
160 // client application to provide additional message parameters to the server.
Jeff Tinker56c78c42013-02-07 17:46:18 -0800161 //
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700162 // If successful, the opaque key request blob is returned to the caller.
Jeff Tinker56c78c42013-02-07 17:46:18 -0800163 virtual status_t
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -0700164 getKeyRequest(Vector<uint8_t> const &scope,
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700165 Vector<uint8_t> const &initData,
166 String8 const &mimeType, KeyType keyType,
167 KeyedVector<String8, String8> const &optionalParameters,
Jeff Tinker06a8cd62015-03-16 13:04:40 -0700168 Vector<uint8_t> &request, String8 &defaultUrl,
169 KeyRequestType *keyRequestType) = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800170
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -0700171 //
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700172 // After a key response is received by the app, it is provided to the
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -0700173 // Drm plugin using provideKeyResponse.
174 //
175 // scope may be a sessionId or a keySetId depending on the type of the
176 // response. Scope should be set to the sessionId when the response is
177 // for either streaming or offline key requests. Scope should be set to the
178 // keySetId when the response is for a release request.
179 //
180 // When the response is for an offline key request, a keySetId is returned
181 // in the keySetId vector parameter that can be used to later restore the
182 // keys to a new session with the method restoreKeys. When the response is
183 // for a streaming or release request, no keySetId is returned.
184 //
185 virtual status_t provideKeyResponse(Vector<uint8_t> const &scope,
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700186 Vector<uint8_t> const &response,
187 Vector<uint8_t> &keySetId) = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800188
Jeff Tinkerb84d1ca2013-05-07 14:07:10 -0700189 // Remove the current keys from a session
190 virtual status_t removeKeys(Vector<uint8_t> const &sessionId) = 0;
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700191
192 // Restore persisted offline keys into a new session. keySetId identifies
193 // the keys to load, obtained from a prior call to provideKeyResponse().
194 virtual status_t restoreKeys(Vector<uint8_t> const &sessionId,
195 Vector<uint8_t> const &keySetId) = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800196
197 // Request an informative description of the license for the session. The status
198 // is in the form of {name, value} pairs. Since DRM license policies vary by
199 // vendor, the specific status field names are determined by each DRM vendor.
200 // Refer to your DRM provider documentation for definitions of the field names
201 // for a particular DrmEngine.
202 virtual status_t
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700203 queryKeyStatus(Vector<uint8_t> const &sessionId,
204 KeyedVector<String8, String8> &infoMap) const = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800205
206 // A provision request/response exchange occurs between the app and a
207 // provisioning server to retrieve a device certificate. getProvisionRequest
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700208 // is used to obtain an opaque key request blob that is delivered to the
Jeff Tinker56c78c42013-02-07 17:46:18 -0800209 // provisioning server.
210 //
211 // If successful, the opaque provision request blob is returned to the caller.
Jeff Tinkerc2f10f22014-03-04 13:23:56 -0800212 virtual status_t getProvisionRequest(String8 const &cert_type,
213 String8 const &cert_authority,
214 Vector<uint8_t> &request,
Jeff Tinker56c78c42013-02-07 17:46:18 -0800215 String8 &defaultUrl) = 0;
216
217 // After a provision response is received by the app, it is provided to the
218 // Drm plugin using provideProvisionResponse.
Jeff Tinkerc2f10f22014-03-04 13:23:56 -0800219 virtual status_t provideProvisionResponse(Vector<uint8_t> const &response,
220 Vector<uint8_t> &certificate,
221 Vector<uint8_t> &wrapped_key) = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800222
223 // A means of enforcing the contractual requirement for a concurrent stream
224 // limit per subscriber across devices is provided via SecureStop. SecureStop
225 // is a means of securely monitoring the lifetime of sessions. Since playback
226 // on a device can be interrupted due to reboot, power failure, etc. a means
227 // of persisting the lifetime information on the device is needed.
228 //
229 // A signed version of the sessionID is written to persistent storage on the
230 // device when each MediaCrypto object is created. The sessionID is signed by
231 // the device private key to prevent tampering.
232 //
233 // In the normal case, playback will be completed, the session destroyed and
234 // the Secure Stops will be queried. The App queries secure stops and forwards
235 // the secure stop message to the server which verifies the signature and
236 // notifies the server side database that the session destruction has been
237 // confirmed. The persisted record on the client is only removed after positive
238 // confirmation that the server received the message using releaseSecureStops().
239 virtual status_t getSecureStops(List<Vector<uint8_t> > &secureStops) = 0;
Jeff Tinker57481592014-10-31 00:42:33 -0700240 virtual status_t getSecureStop(Vector<uint8_t> const &ssid, Vector<uint8_t> &secureStop) = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800241 virtual status_t releaseSecureStops(Vector<uint8_t> const &ssRelease) = 0;
Jeff Tinker57481592014-10-31 00:42:33 -0700242 virtual status_t releaseAllSecureStops() = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800243
244 // Read a property value given the device property string. There are a few forms
245 // of property access methods, depending on the data type returned.
246 // Since DRM plugin properties may vary, additional field names may be defined
247 // by each DRM vendor. Refer to your DRM provider documentation for definitions
248 // of its additional field names.
249 //
250 // Standard values are:
251 // "vendor" [string] identifies the maker of the plugin
252 // "version" [string] identifies the version of the plugin
253 // "description" [string] describes the plugin
254 // 'deviceUniqueId' [byte array] The device unique identifier is established
255 // during device provisioning and provides a means of uniquely identifying
256 // each device.
257 virtual status_t getPropertyString(String8 const &name, String8 &value ) const = 0;
258 virtual status_t getPropertyByteArray(String8 const &name,
259 Vector<uint8_t> &value ) const = 0;
260
261 // Write a property value given the device property string. There are a few forms
262 // of property setting methods, depending on the data type.
263 // Since DRM plugin properties may vary, additional field names may be defined
264 // by each DRM vendor. Refer to your DRM provider documentation for definitions
265 // of its field names.
266 virtual status_t setPropertyString(String8 const &name,
267 String8 const &value ) = 0;
268 virtual status_t setPropertyByteArray(String8 const &name,
269 Vector<uint8_t> const &value ) = 0;
270
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700271 // The following methods implement operations on a CryptoSession to support
272 // encrypt, decrypt, sign verify operations on operator-provided
273 // session keys.
274
275 //
276 // The algorithm string conforms to JCA Standard Names for Cipher
277 // Transforms and is case insensitive. For example "AES/CBC/PKCS5Padding".
278 //
279 // Return OK if the algorithm is supported, otherwise return BAD_VALUE
280 //
281 virtual status_t setCipherAlgorithm(Vector<uint8_t> const &sessionId,
282 String8 const &algorithm) = 0;
283
284 //
285 // The algorithm string conforms to JCA Standard Names for Mac
286 // Algorithms and is case insensitive. For example "HmacSHA256".
287 //
288 // Return OK if the algorithm is supported, otherwise return BAD_VALUE
289 //
290 virtual status_t setMacAlgorithm(Vector<uint8_t> const &sessionId,
291 String8 const &algorithm) = 0;
292
293 // Encrypt the provided input buffer with the cipher algorithm
294 // specified by setCipherAlgorithm and the key selected by keyId,
295 // and return the encrypted data.
296 virtual status_t encrypt(Vector<uint8_t> const &sessionId,
297 Vector<uint8_t> const &keyId,
298 Vector<uint8_t> const &input,
299 Vector<uint8_t> const &iv,
300 Vector<uint8_t> &output) = 0;
301
302 // Decrypt the provided input buffer with the cipher algorithm
303 // specified by setCipherAlgorithm and the key selected by keyId,
304 // and return the decrypted data.
305 virtual status_t decrypt(Vector<uint8_t> const &sessionId,
306 Vector<uint8_t> const &keyId,
307 Vector<uint8_t> const &input,
308 Vector<uint8_t> const &iv,
309 Vector<uint8_t> &output) = 0;
310
311 // Compute a signature on the provided message using the mac algorithm
312 // specified by setMacAlgorithm and the key selected by keyId,
313 // and return the signature.
314 virtual status_t sign(Vector<uint8_t> const &sessionId,
315 Vector<uint8_t> const &keyId,
316 Vector<uint8_t> const &message,
317 Vector<uint8_t> &signature) = 0;
318
319 // Compute a signature on the provided message using the mac algorithm
320 // specified by setMacAlgorithm and the key selected by keyId,
321 // and compare with the expected result. Set result to true or
322 // false depending on the outcome.
323 virtual status_t verify(Vector<uint8_t> const &sessionId,
324 Vector<uint8_t> const &keyId,
325 Vector<uint8_t> const &message,
326 Vector<uint8_t> const &signature,
327 bool &match) = 0;
328
329
Jeff Tinkerc2f10f22014-03-04 13:23:56 -0800330 // Compute an RSA signature on the provided message using the algorithm
331 // specified by algorithm.
332 virtual status_t signRSA(Vector<uint8_t> const &sessionId,
333 String8 const &algorithm,
334 Vector<uint8_t> const &message,
335 Vector<uint8_t> const &wrapped_key,
336 Vector<uint8_t> &signature) = 0;
337
338
Jeff Tinker7eafcae2013-04-02 13:16:21 -0700339 status_t setListener(const sp<DrmPluginListener>& listener) {
340 Mutex::Autolock lock(mEventLock);
341 mListener = listener;
342 return OK;
343 }
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700344
Jeff Tinker7eafcae2013-04-02 13:16:21 -0700345 protected:
Jeff Tinker9a498ef2015-03-31 15:47:24 -0700346 // Plugins call these methods to deliver events to the java app
Jeff Tinker7eafcae2013-04-02 13:16:21 -0700347 void sendEvent(EventType eventType, int extra,
348 Vector<uint8_t> const *sessionId,
349 Vector<uint8_t> const *data);
350
Jeff Tinker9a498ef2015-03-31 15:47:24 -0700351 void sendExpirationUpdate(Vector<uint8_t> const *sessionId,
352 int64_t expiryTimeInMS);
353
354 void sendKeysChange(Vector<uint8_t> const *sessionId,
355 Vector<DrmPlugin::KeyStatus> const *keyStatusList,
356 bool hasNewUsableKey);
357
Jeff Tinker56c78c42013-02-07 17:46:18 -0800358 private:
Jeff Tinker7eafcae2013-04-02 13:16:21 -0700359 Mutex mEventLock;
360 sp<DrmPluginListener> mListener;
361
Jeff Tinker56c78c42013-02-07 17:46:18 -0800362 DISALLOW_EVIL_CONSTRUCTORS(DrmPlugin);
363 };
364
Jeff Tinker7eafcae2013-04-02 13:16:21 -0700365 class DrmPluginListener: virtual public RefBase
366 {
367 public:
368 virtual void sendEvent(DrmPlugin::EventType eventType, int extra,
Jeff Tinker9a498ef2015-03-31 15:47:24 -0700369 Vector<uint8_t> const *sessionId,
Jeff Tinker7eafcae2013-04-02 13:16:21 -0700370 Vector<uint8_t> const *data) = 0;
Jeff Tinker9a498ef2015-03-31 15:47:24 -0700371
372 virtual void sendExpirationUpdate(Vector<uint8_t> const *sessionId,
373 int64_t expiryTimeInMS) = 0;
374
375 virtual void sendKeysChange(Vector<uint8_t> const *sessionId,
376 Vector<DrmPlugin::KeyStatus> const *keyStatusList,
377 bool hasNewUsableKey) = 0;
Jeff Tinker7eafcae2013-04-02 13:16:21 -0700378 };
379
380 inline void DrmPlugin::sendEvent(EventType eventType, int extra,
381 Vector<uint8_t> const *sessionId,
382 Vector<uint8_t> const *data) {
Jeff Tinker7eafcae2013-04-02 13:16:21 -0700383 mEventLock.lock();
384 sp<DrmPluginListener> listener = mListener;
385 mEventLock.unlock();
386
387 if (listener != NULL) {
388 listener->sendEvent(eventType, extra, sessionId, data);
389 }
390 }
391
Jeff Tinker9a498ef2015-03-31 15:47:24 -0700392 inline void DrmPlugin::sendExpirationUpdate(Vector<uint8_t> const *sessionId,
393 int64_t expiryTimeInMS) {
394 mEventLock.lock();
395 sp<DrmPluginListener> listener = mListener;
396 mEventLock.unlock();
397
398 if (listener != NULL) {
399 listener->sendExpirationUpdate(sessionId, expiryTimeInMS);
400 }
401 }
402
403 inline void DrmPlugin::sendKeysChange(Vector<uint8_t> const *sessionId,
404 Vector<DrmPlugin::KeyStatus> const *keyStatusList,
405 bool hasNewUsableKey) {
406 mEventLock.lock();
407 sp<DrmPluginListener> listener = mListener;
408 mEventLock.unlock();
409
410 if (listener != NULL) {
411 listener->sendKeysChange(sessionId, keyStatusList, hasNewUsableKey);
412 }
413 }
Jeff Tinker56c78c42013-02-07 17:46:18 -0800414} // namespace android
415
416#endif // DRM_API_H_