1 /* 2 * 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 <utils/Mutex.h> 26 #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 36 namespace android { 37 38 class DrmPlugin; 39 class DrmPluginListener; 40 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 63 // Construct a DrmPlugin for the crypto scheme specified by UUID. 64 virtual status_t createDrmPlugin( 65 const uint8_t uuid[16], DrmPlugin **plugin) = 0; 66 67 private: 68 DrmFactory(const DrmFactory &); 69 DrmFactory &operator=(const DrmFactory &); 70 }; 71 72 class DrmPlugin { 73 public: 74 enum EventType { 75 kDrmPluginEventProvisionRequired = 1, 76 kDrmPluginEventKeyNeeded, 77 kDrmPluginEventKeyExpired, 78 kDrmPluginEventVendorDefined 79 }; 80 81 // Drm keys can be for offline content or for online streaming. 82 // Offline keys are persisted on the device and may be used when the device 83 // is disconnected from the network. The Release type is used to request 84 // that offline keys be no longer restricted to offline use. 85 enum KeyType { 86 kKeyType_Offline, 87 kKeyType_Streaming, 88 kKeyType_Release 89 }; 90 91 DrmPlugin() {} 92 virtual ~DrmPlugin() {} 93 94 // Open a new session with the DrmPlugin object. A session ID is returned 95 // in the sessionId parameter. 96 virtual status_t openSession(Vector<uint8_t> &sessionId) = 0; 97 98 // Close a session on the DrmPlugin object. 99 virtual status_t closeSession(Vector<uint8_t> const &sessionId) = 0; 100 101 // A key request/response exchange occurs between the app and a License 102 // Server to obtain the keys required to decrypt the content. getKeyRequest() 103 // is used to obtain an opaque key request blob that is delivered to the 104 // license server. 105 // 106 // The scope parameter may be a sessionId or a keySetId, depending on the 107 // specified keyType. When the keyType is kKeyType_Offline or 108 // kKeyType_Streaming, scope should be set to the sessionId the keys will be 109 // provided to. When the keyType is kKeyType_Release, scope should be set to 110 // the keySetId of the keys being released. Releasing keys from a device 111 // invalidates them for all sessions. 112 // 113 // The init data passed to getKeyRequest is container-specific and its 114 // meaning is interpreted based on the mime type provided in the mimeType 115 // parameter to getKeyRequest. It could contain, for example, the content 116 // ID, key ID or other data obtained from the content metadata that is required 117 // in generating the key request. Init may be null when keyType is 118 // kKeyType_Release. 119 // 120 // mimeType identifies the mime type of the content 121 // 122 // keyType specifies if the keys are to be used for streaming or offline content 123 // 124 // optionalParameters are included in the key request message to allow a 125 // client application to provide additional message parameters to the server. 126 // 127 // If successful, the opaque key request blob is returned to the caller. 128 virtual status_t 129 getKeyRequest(Vector<uint8_t> const &scope, 130 Vector<uint8_t> const &initData, 131 String8 const &mimeType, KeyType keyType, 132 KeyedVector<String8, String8> const &optionalParameters, 133 Vector<uint8_t> &request, String8 &defaultUrl) = 0; 134 135 // 136 // After a key response is received by the app, it is provided to the 137 // Drm plugin using provideKeyResponse. 138 // 139 // scope may be a sessionId or a keySetId depending on the type of the 140 // response. Scope should be set to the sessionId when the response is 141 // for either streaming or offline key requests. Scope should be set to the 142 // keySetId when the response is for a release request. 143 // 144 // When the response is for an offline key request, a keySetId is returned 145 // in the keySetId vector parameter that can be used to later restore the 146 // keys to a new session with the method restoreKeys. When the response is 147 // for a streaming or release request, no keySetId is returned. 148 // 149 virtual status_t provideKeyResponse(Vector<uint8_t> const &scope, 150 Vector<uint8_t> const &response, 151 Vector<uint8_t> &keySetId) = 0; 152 153 // Remove the current keys from a session 154 virtual status_t removeKeys(Vector<uint8_t> const &sessionId) = 0; 155 156 // Restore persisted offline keys into a new session. keySetId identifies 157 // the keys to load, obtained from a prior call to provideKeyResponse(). 158 virtual status_t restoreKeys(Vector<uint8_t> const &sessionId, 159 Vector<uint8_t> const &keySetId) = 0; 160 161 // Request an informative description of the license for the session. The status 162 // is in the form of {name, value} pairs. Since DRM license policies vary by 163 // vendor, the specific status field names are determined by each DRM vendor. 164 // Refer to your DRM provider documentation for definitions of the field names 165 // for a particular DrmEngine. 166 virtual status_t 167 queryKeyStatus(Vector<uint8_t> const &sessionId, 168 KeyedVector<String8, String8> &infoMap) const = 0; 169 170 // A provision request/response exchange occurs between the app and a 171 // provisioning server to retrieve a device certificate. getProvisionRequest 172 // is used to obtain an opaque key request blob that is delivered to the 173 // provisioning server. 174 // 175 // If successful, the opaque provision request blob is returned to the caller. 176 virtual status_t getProvisionRequest(Vector<uint8_t> &request, 177 String8 &defaultUrl) = 0; 178 179 // After a provision response is received by the app, it is provided to the 180 // Drm plugin using provideProvisionResponse. 181 virtual status_t provideProvisionResponse(Vector<uint8_t> const &response) = 0; 182 183 // A means of enforcing the contractual requirement for a concurrent stream 184 // limit per subscriber across devices is provided via SecureStop. SecureStop 185 // is a means of securely monitoring the lifetime of sessions. Since playback 186 // on a device can be interrupted due to reboot, power failure, etc. a means 187 // of persisting the lifetime information on the device is needed. 188 // 189 // A signed version of the sessionID is written to persistent storage on the 190 // device when each MediaCrypto object is created. The sessionID is signed by 191 // the device private key to prevent tampering. 192 // 193 // In the normal case, playback will be completed, the session destroyed and 194 // the Secure Stops will be queried. The App queries secure stops and forwards 195 // the secure stop message to the server which verifies the signature and 196 // notifies the server side database that the session destruction has been 197 // confirmed. The persisted record on the client is only removed after positive 198 // confirmation that the server received the message using releaseSecureStops(). 199 virtual status_t getSecureStops(List<Vector<uint8_t> > &secureStops) = 0; 200 virtual status_t releaseSecureStops(Vector<uint8_t> const &ssRelease) = 0; 201 202 // Read a property value given the device property string. There are a few forms 203 // of property access methods, depending on the data type returned. 204 // Since DRM plugin properties may vary, additional field names may be defined 205 // by each DRM vendor. Refer to your DRM provider documentation for definitions 206 // of its additional field names. 207 // 208 // Standard values are: 209 // "vendor" [string] identifies the maker of the plugin 210 // "version" [string] identifies the version of the plugin 211 // "description" [string] describes the plugin 212 // 'deviceUniqueId' [byte array] The device unique identifier is established 213 // during device provisioning and provides a means of uniquely identifying 214 // each device. 215 virtual status_t getPropertyString(String8 const &name, String8 &value ) const = 0; 216 virtual status_t getPropertyByteArray(String8 const &name, 217 Vector<uint8_t> &value ) const = 0; 218 219 // Write a property value given the device property string. There are a few forms 220 // of property setting methods, depending on the data type. 221 // Since DRM plugin properties may vary, additional field names may be defined 222 // by each DRM vendor. Refer to your DRM provider documentation for definitions 223 // of its field names. 224 virtual status_t setPropertyString(String8 const &name, 225 String8 const &value ) = 0; 226 virtual status_t setPropertyByteArray(String8 const &name, 227 Vector<uint8_t> const &value ) = 0; 228 229 // The following methods implement operations on a CryptoSession to support 230 // encrypt, decrypt, sign verify operations on operator-provided 231 // session keys. 232 233 // 234 // The algorithm string conforms to JCA Standard Names for Cipher 235 // Transforms and is case insensitive. For example "AES/CBC/PKCS5Padding". 236 // 237 // Return OK if the algorithm is supported, otherwise return BAD_VALUE 238 // 239 virtual status_t setCipherAlgorithm(Vector<uint8_t> const &sessionId, 240 String8 const &algorithm) = 0; 241 242 // 243 // The algorithm string conforms to JCA Standard Names for Mac 244 // Algorithms and is case insensitive. For example "HmacSHA256". 245 // 246 // Return OK if the algorithm is supported, otherwise return BAD_VALUE 247 // 248 virtual status_t setMacAlgorithm(Vector<uint8_t> const &sessionId, 249 String8 const &algorithm) = 0; 250 251 // Encrypt the provided input buffer with the cipher algorithm 252 // specified by setCipherAlgorithm and the key selected by keyId, 253 // and return the encrypted data. 254 virtual status_t encrypt(Vector<uint8_t> const &sessionId, 255 Vector<uint8_t> const &keyId, 256 Vector<uint8_t> const &input, 257 Vector<uint8_t> const &iv, 258 Vector<uint8_t> &output) = 0; 259 260 // Decrypt the provided input buffer with the cipher algorithm 261 // specified by setCipherAlgorithm and the key selected by keyId, 262 // and return the decrypted data. 263 virtual status_t decrypt(Vector<uint8_t> const &sessionId, 264 Vector<uint8_t> const &keyId, 265 Vector<uint8_t> const &input, 266 Vector<uint8_t> const &iv, 267 Vector<uint8_t> &output) = 0; 268 269 // Compute a signature on the provided message using the mac algorithm 270 // specified by setMacAlgorithm and the key selected by keyId, 271 // and return the signature. 272 virtual status_t sign(Vector<uint8_t> const &sessionId, 273 Vector<uint8_t> const &keyId, 274 Vector<uint8_t> const &message, 275 Vector<uint8_t> &signature) = 0; 276 277 // Compute a signature on the provided message using the mac algorithm 278 // specified by setMacAlgorithm and the key selected by keyId, 279 // and compare with the expected result. Set result to true or 280 // false depending on the outcome. 281 virtual status_t verify(Vector<uint8_t> const &sessionId, 282 Vector<uint8_t> const &keyId, 283 Vector<uint8_t> const &message, 284 Vector<uint8_t> const &signature, 285 bool &match) = 0; 286 287 288 status_t setListener(const sp<DrmPluginListener>& listener) { 289 Mutex::Autolock lock(mEventLock); 290 mListener = listener; 291 return OK; 292 } 293 294 protected: 295 // Plugins call sendEvent to deliver events to the java app 296 void sendEvent(EventType eventType, int extra, 297 Vector<uint8_t> const *sessionId, 298 Vector<uint8_t> const *data); 299 300 private: 301 Mutex mEventLock; 302 sp<DrmPluginListener> mListener; 303 304 DISALLOW_EVIL_CONSTRUCTORS(DrmPlugin); 305 }; 306 307 class DrmPluginListener: virtual public RefBase 308 { 309 public: 310 virtual void sendEvent(DrmPlugin::EventType eventType, int extra, 311 Vector<uint8_t> const *sesionId, 312 Vector<uint8_t> const *data) = 0; 313 }; 314 315 inline void DrmPlugin::sendEvent(EventType eventType, int extra, 316 Vector<uint8_t> const *sessionId, 317 Vector<uint8_t> const *data) { 318 319 mEventLock.lock(); 320 sp<DrmPluginListener> listener = mListener; 321 mEventLock.unlock(); 322 323 if (listener != NULL) { 324 listener->sendEvent(eventType, extra, sessionId, data); 325 } 326 } 327 328 } // namespace android 329 330 #endif // DRM_API_H_ 331