blob: 81d1ec02d5a9a05b8614d913218bbb264b408e33 [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>
25#include <media/stagefright/foundation/ABase.h>
26
27// Loadable DrmEngine shared libraries should define the entry points
28// createDrmFactory and createCryptoFactory as shown below:
29//
30// extern "C" {
31// extern android::DrmFactory *createDrmFactory();
32// extern android::CryptoFactory *createCryptoFactory();
33// }
34
35namespace android {
36
37 struct DrmPlugin;
38
39 // DRMs are implemented in DrmEngine plugins, which are dynamically
40 // loadable shared libraries that implement the entry points
41 // createDrmFactory and createCryptoFactory. createDrmFactory
42 // constructs and returns an instance of a DrmFactory object. Similarly,
43 // createCryptoFactory creates an instance of a CryptoFactory object.
44 // When a MediaCrypto or MediaDrm object needs to be constructed, all
45 // available DrmEngines present in the plugins directory on the device
46 // are scanned for a matching DrmEngine that can support the crypto
47 // scheme. When a match is found, the DrmEngine's createCryptoPlugin and
48 // createDrmPlugin methods are used to create CryptoPlugin or
49 // DrmPlugin instances to support that DRM scheme.
50
51 class DrmFactory {
52 public:
53 DrmFactory() {}
54 virtual ~DrmFactory() {}
55
56 // DrmFactory::isCryptoSchemeSupported can be called to determine
57 // if the plugin factory is able to construct plugins that support a
58 // given crypto scheme, which is specified by a UUID.
59 virtual bool isCryptoSchemeSupported(const uint8_t uuid[16]) = 0;
60
61 // Construct a DrmPlugin for the crypto scheme specified by UUID.
62 virtual status_t createDrmPlugin(
63 const uint8_t uuid[16], DrmPlugin **plugin) = 0;
64
65 private:
66 DrmFactory(const DrmFactory &);
67 DrmFactory &operator=(const DrmFactory &);
68 };
69
70 class DrmPlugin {
71 public:
72 enum EventType {
73 kDrmPluginEventProvisionRequired,
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -070074 kDrmPluginEventKeyNeeded,
75 kDrmPluginEventKeyExpired,
Jeff Tinker56c78c42013-02-07 17:46:18 -080076 kDrmPluginEventVendorDefined
77 };
78
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -070079 // Drm keys can be for offline content or for online streaming.
80 // Offline keys are persisted on the device and may be used when the device
Jeff Tinker56c78c42013-02-07 17:46:18 -080081 // is disconnected from the network.
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -070082 enum KeyType {
83 kKeyType_Offline,
84 kKeyType_Streaming
Jeff Tinker56c78c42013-02-07 17:46:18 -080085 };
86
87 DrmPlugin() {}
88 virtual ~DrmPlugin() {}
89
90 // Open a new session with the DrmPlugin object. A session ID is returned
91 // in the sessionId parameter.
92 virtual status_t openSession(Vector<uint8_t> &sessionId) = 0;
93
94 // Close a session on the DrmPlugin object.
95 virtual status_t closeSession(Vector<uint8_t> const &sessionId) = 0;
96
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -070097 // A key request/response exchange occurs between the app and a License
98 // Server to obtain the keys required to decrypt the content. getKeyRequest()
99 // is used to obtain an opaque key request blob that is delivered to the
Jeff Tinker56c78c42013-02-07 17:46:18 -0800100 // license server.
101 //
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700102 // The init data passed to getKeyRequest is container-specific and its
Jeff Tinker56c78c42013-02-07 17:46:18 -0800103 // meaning is interpreted based on the mime type provided in the mimeType
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700104 // parameter to getKeyRequest. It could contain, for example, the content
Jeff Tinker56c78c42013-02-07 17:46:18 -0800105 // ID, key ID or other data obtained from the content metadata that is required
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700106 // in generating the key request.
Jeff Tinker56c78c42013-02-07 17:46:18 -0800107 //
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700108 // keyType specifes if the keys are to be used for streaming or offline content
Jeff Tinker56c78c42013-02-07 17:46:18 -0800109 //
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700110 // optionalParameters are included in the key request message to allow a
111 // client application to provide additional message parameters to the server.
Jeff Tinker56c78c42013-02-07 17:46:18 -0800112 //
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700113 // If successful, the opaque key request blob is returned to the caller.
Jeff Tinker56c78c42013-02-07 17:46:18 -0800114 virtual status_t
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700115 getKeyRequest(Vector<uint8_t> const &sessionId,
116 Vector<uint8_t> const &initData,
117 String8 const &mimeType, KeyType keyType,
118 KeyedVector<String8, String8> const &optionalParameters,
119 Vector<uint8_t> &request, String8 &defaultUrl) = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800120
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700121 // After a key response is received by the app, it is provided to the
122 // Drm plugin using provideKeyResponse. Returns the id of the key set
123 // in keySetId. The keySetId can be used by removeKeys or restoreKeys
124 // when the keys are used for offline content.
125 virtual status_t provideKeyResponse(Vector<uint8_t> const &sessionId,
126 Vector<uint8_t> const &response,
127 Vector<uint8_t> &keySetId) = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800128
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700129 // Remove the persisted keys associated with an offline license for a session.
130 virtual status_t removeKeys(Vector<uint8_t> const &keySetId) = 0;
131
132 // Restore persisted offline keys into a new session. keySetId identifies
133 // the keys to load, obtained from a prior call to provideKeyResponse().
134 virtual status_t restoreKeys(Vector<uint8_t> const &sessionId,
135 Vector<uint8_t> const &keySetId) = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800136
137 // Request an informative description of the license for the session. The status
138 // is in the form of {name, value} pairs. Since DRM license policies vary by
139 // vendor, the specific status field names are determined by each DRM vendor.
140 // Refer to your DRM provider documentation for definitions of the field names
141 // for a particular DrmEngine.
142 virtual status_t
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700143 queryKeyStatus(Vector<uint8_t> const &sessionId,
144 KeyedVector<String8, String8> &infoMap) const = 0;
Jeff Tinker56c78c42013-02-07 17:46:18 -0800145
146 // A provision request/response exchange occurs between the app and a
147 // provisioning server to retrieve a device certificate. getProvisionRequest
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700148 // is used to obtain an opaque key request blob that is delivered to the
Jeff Tinker56c78c42013-02-07 17:46:18 -0800149 // provisioning server.
150 //
151 // If successful, the opaque provision request blob is returned to the caller.
152 virtual status_t getProvisionRequest(Vector<uint8_t> &request,
153 String8 &defaultUrl) = 0;
154
155 // After a provision response is received by the app, it is provided to the
156 // Drm plugin using provideProvisionResponse.
157 virtual status_t provideProvisionResponse(Vector<uint8_t> const &response) = 0;
158
159 // A means of enforcing the contractual requirement for a concurrent stream
160 // limit per subscriber across devices is provided via SecureStop. SecureStop
161 // is a means of securely monitoring the lifetime of sessions. Since playback
162 // on a device can be interrupted due to reboot, power failure, etc. a means
163 // of persisting the lifetime information on the device is needed.
164 //
165 // A signed version of the sessionID is written to persistent storage on the
166 // device when each MediaCrypto object is created. The sessionID is signed by
167 // the device private key to prevent tampering.
168 //
169 // In the normal case, playback will be completed, the session destroyed and
170 // the Secure Stops will be queried. The App queries secure stops and forwards
171 // the secure stop message to the server which verifies the signature and
172 // notifies the server side database that the session destruction has been
173 // confirmed. The persisted record on the client is only removed after positive
174 // confirmation that the server received the message using releaseSecureStops().
175 virtual status_t getSecureStops(List<Vector<uint8_t> > &secureStops) = 0;
176 virtual status_t releaseSecureStops(Vector<uint8_t> const &ssRelease) = 0;
177
178 // Read a property value given the device property string. There are a few forms
179 // of property access methods, depending on the data type returned.
180 // Since DRM plugin properties may vary, additional field names may be defined
181 // by each DRM vendor. Refer to your DRM provider documentation for definitions
182 // of its additional field names.
183 //
184 // Standard values are:
185 // "vendor" [string] identifies the maker of the plugin
186 // "version" [string] identifies the version of the plugin
187 // "description" [string] describes the plugin
188 // 'deviceUniqueId' [byte array] The device unique identifier is established
189 // during device provisioning and provides a means of uniquely identifying
190 // each device.
191 virtual status_t getPropertyString(String8 const &name, String8 &value ) const = 0;
192 virtual status_t getPropertyByteArray(String8 const &name,
193 Vector<uint8_t> &value ) const = 0;
194
195 // Write a property value given the device property string. There are a few forms
196 // of property setting methods, depending on the data type.
197 // Since DRM plugin properties may vary, additional field names may be defined
198 // by each DRM vendor. Refer to your DRM provider documentation for definitions
199 // of its field names.
200 virtual status_t setPropertyString(String8 const &name,
201 String8 const &value ) = 0;
202 virtual status_t setPropertyByteArray(String8 const &name,
203 Vector<uint8_t> const &value ) = 0;
204
Jeff Tinkerbcbd78b2013-03-30 16:28:20 -0700205 // The following methods implement operations on a CryptoSession to support
206 // encrypt, decrypt, sign verify operations on operator-provided
207 // session keys.
208
209 //
210 // The algorithm string conforms to JCA Standard Names for Cipher
211 // Transforms and is case insensitive. For example "AES/CBC/PKCS5Padding".
212 //
213 // Return OK if the algorithm is supported, otherwise return BAD_VALUE
214 //
215 virtual status_t setCipherAlgorithm(Vector<uint8_t> const &sessionId,
216 String8 const &algorithm) = 0;
217
218 //
219 // The algorithm string conforms to JCA Standard Names for Mac
220 // Algorithms and is case insensitive. For example "HmacSHA256".
221 //
222 // Return OK if the algorithm is supported, otherwise return BAD_VALUE
223 //
224 virtual status_t setMacAlgorithm(Vector<uint8_t> const &sessionId,
225 String8 const &algorithm) = 0;
226
227 // Encrypt the provided input buffer with the cipher algorithm
228 // specified by setCipherAlgorithm and the key selected by keyId,
229 // and return the encrypted data.
230 virtual status_t encrypt(Vector<uint8_t> const &sessionId,
231 Vector<uint8_t> const &keyId,
232 Vector<uint8_t> const &input,
233 Vector<uint8_t> const &iv,
234 Vector<uint8_t> &output) = 0;
235
236 // Decrypt the provided input buffer with the cipher algorithm
237 // specified by setCipherAlgorithm and the key selected by keyId,
238 // and return the decrypted data.
239 virtual status_t decrypt(Vector<uint8_t> const &sessionId,
240 Vector<uint8_t> const &keyId,
241 Vector<uint8_t> const &input,
242 Vector<uint8_t> const &iv,
243 Vector<uint8_t> &output) = 0;
244
245 // Compute a signature on the provided message using the mac algorithm
246 // specified by setMacAlgorithm and the key selected by keyId,
247 // and return the signature.
248 virtual status_t sign(Vector<uint8_t> const &sessionId,
249 Vector<uint8_t> const &keyId,
250 Vector<uint8_t> const &message,
251 Vector<uint8_t> &signature) = 0;
252
253 // Compute a signature on the provided message using the mac algorithm
254 // specified by setMacAlgorithm and the key selected by keyId,
255 // and compare with the expected result. Set result to true or
256 // false depending on the outcome.
257 virtual status_t verify(Vector<uint8_t> const &sessionId,
258 Vector<uint8_t> const &keyId,
259 Vector<uint8_t> const &message,
260 Vector<uint8_t> const &signature,
261 bool &match) = 0;
262
263
264
Jeff Tinker56c78c42013-02-07 17:46:18 -0800265 // TODO: provide way to send an event
266 private:
267 DISALLOW_EVIL_CONSTRUCTORS(DrmPlugin);
268 };
269
270} // namespace android
271
272#endif // DRM_API_H_