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: DrmFactory()55 DrmFactory() {} ~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 // 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 68 // 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 { 80 kDrmPluginEventProvisionRequired = 1, 81 kDrmPluginEventKeyNeeded, 82 kDrmPluginEventKeyExpired, 83 kDrmPluginEventVendorDefined, 84 kDrmPluginEventSessionReclaimed, 85 kDrmPluginEventExpirationUpdate, 86 kDrmPluginEventKeysChange, 87 }; 88 89 // 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 91 // is disconnected from the network. The Release type is used to request 92 // that offline keys be no longer restricted to offline use. 93 enum KeyType { 94 kKeyType_Offline, 95 kKeyType_Streaming, 96 kKeyType_Release 97 }; 98 99 // 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 kKeyRequestType_None, 107 kKeyRequestType_Update, 108 }; 109 110 // Enumerate KeyStatusTypes which indicate the state of a key 111 enum KeyStatusType 112 { 113 kKeyStatusType_Usable, 114 kKeyStatusType_Expired, 115 kKeyStatusType_OutputNotAllowed, 116 kKeyStatusType_StatusPending, 117 kKeyStatusType_InternalError 118 }; 119 120 // Used by sendKeysChange to report the usability status of each 121 // key to the app. 122 struct KeyStatus 123 { 124 Vector<uint8_t> mKeyId; 125 KeyStatusType mType; 126 }; 127 128 // Enumerate HDCP output protection levels 129 enum HdcpLevel { 130 // Failure to access HDCP level, an error occurred 131 kHdcpLevelUnknown, 132 // HDCP is not supported on this device, content is unprotected 133 kHdcpNone, 134 // HDCP version 1.0 135 kHdcpV1, 136 // HDCP version 2.0 Type 1. 137 kHdcpV2, 138 // HDCP version 2.1 Type 1. 139 kHdcpV2_1, 140 // HDCP version 2.2 Type 1. 141 kHdcpV2_2, 142 // No digital output, implicitly secure 143 kHdcpNoOutput = 0x7fff 144 }; 145 146 // SecurityLevel indicates the level of robustness of the DRM 147 // implementation on the device 148 enum SecurityLevel { 149 // Failure to access security level, an error occurred 150 kSecurityLevelUnknown, 151 // The maximum security level of the device. This is the default when 152 // a session is opened if no security level is specified 153 kSecurityLevelMax, 154 // Software-based whitebox crypto 155 kSecurityLevelSwSecureCrypto, 156 // Software-based whitebox crypto and an obfuscated decoder 157 kSecurityLevelSwSecureDecode, 158 // DRM key management and crypto operations are performed within a 159 // hardware backed trusted execution environment 160 kSecurityLevelHwSecureCrypto, 161 // DRM key management, crypto operations and decoding of content 162 // are performed within a hardware backed trusted execution environment 163 kSecurityLevelHwSecureDecode, 164 // DRM key management, crypto operations, decoding of content and all 165 // handling of the media (compressed and uncompressed) is handled within 166 // a hardware backed trusted execution environment. 167 kSecurityLevelHwSecureAll 168 }; 169 DrmPlugin()170 DrmPlugin() {} ~DrmPlugin()171 virtual ~DrmPlugin() {} 172 173 // Open a new session with the DrmPlugin object. A session ID is returned 174 // in the sessionId parameter. 175 virtual status_t openSession(Vector<uint8_t> &sessionId) = 0; 176 177 // Close a session on the DrmPlugin object. 178 virtual status_t closeSession(Vector<uint8_t> const &sessionId) = 0; 179 180 // A key request/response exchange occurs between the app and a License 181 // Server to obtain the keys required to decrypt the content. getKeyRequest() 182 // is used to obtain an opaque key request blob that is delivered to the 183 // license server. 184 // 185 // The scope parameter may be a sessionId or a keySetId, depending on the 186 // specified keyType. When the keyType is kKeyType_Offline or 187 // kKeyType_Streaming, scope should be set to the sessionId the keys will be 188 // provided to. When the keyType is kKeyType_Release, scope should be set to 189 // the keySetId of the keys being released. Releasing keys from a device 190 // invalidates them for all sessions. 191 // 192 // The init data passed to getKeyRequest is container-specific and its 193 // meaning is interpreted based on the mime type provided in the mimeType 194 // parameter to getKeyRequest. It could contain, for example, the content 195 // ID, key ID or other data obtained from the content metadata that is required 196 // in generating the key request. Init may be null when keyType is 197 // kKeyType_Release. 198 // 199 // mimeType identifies the mime type of the content 200 // 201 // keyType specifies if the keys are to be used for streaming or offline content 202 // 203 // optionalParameters are included in the key request message to allow a 204 // client application to provide additional message parameters to the server. 205 // 206 // If successful, the opaque key request blob is returned to the caller. 207 virtual status_t 208 getKeyRequest(Vector<uint8_t> const &scope, 209 Vector<uint8_t> const &initData, 210 String8 const &mimeType, KeyType keyType, 211 KeyedVector<String8, String8> const &optionalParameters, 212 Vector<uint8_t> &request, String8 &defaultUrl, 213 KeyRequestType *keyRequestType) = 0; 214 215 // 216 // After a key response is received by the app, it is provided to the 217 // Drm plugin using provideKeyResponse. 218 // 219 // scope may be a sessionId or a keySetId depending on the type of the 220 // response. Scope should be set to the sessionId when the response is 221 // for either streaming or offline key requests. Scope should be set to the 222 // keySetId when the response is for a release request. 223 // 224 // When the response is for an offline key request, a keySetId is returned 225 // in the keySetId vector parameter that can be used to later restore the 226 // keys to a new session with the method restoreKeys. When the response is 227 // for a streaming or release request, no keySetId is returned. 228 // 229 virtual status_t provideKeyResponse(Vector<uint8_t> const &scope, 230 Vector<uint8_t> const &response, 231 Vector<uint8_t> &keySetId) = 0; 232 233 // Remove the current keys from a session 234 virtual status_t removeKeys(Vector<uint8_t> const &sessionId) = 0; 235 236 // Restore persisted offline keys into a new session. keySetId identifies 237 // the keys to load, obtained from a prior call to provideKeyResponse(). 238 virtual status_t restoreKeys(Vector<uint8_t> const &sessionId, 239 Vector<uint8_t> const &keySetId) = 0; 240 241 // Request an informative description of the license for the session. The status 242 // is in the form of {name, value} pairs. Since DRM license policies vary by 243 // vendor, the specific status field names are determined by each DRM vendor. 244 // Refer to your DRM provider documentation for definitions of the field names 245 // for a particular DrmEngine. 246 virtual status_t 247 queryKeyStatus(Vector<uint8_t> const &sessionId, 248 KeyedVector<String8, String8> &infoMap) const = 0; 249 250 // A provision request/response exchange occurs between the app and a 251 // provisioning server to retrieve a device certificate. getProvisionRequest 252 // is used to obtain an opaque key request blob that is delivered to the 253 // provisioning server. 254 // 255 // If successful, the opaque provision request blob is returned to the caller. 256 virtual status_t getProvisionRequest(String8 const &cert_type, 257 String8 const &cert_authority, 258 Vector<uint8_t> &request, 259 String8 &defaultUrl) = 0; 260 261 // After a provision response is received by the app, it is provided to the 262 // Drm plugin using provideProvisionResponse. 263 virtual status_t provideProvisionResponse(Vector<uint8_t> const &response, 264 Vector<uint8_t> &certificate, 265 Vector<uint8_t> &wrapped_key) = 0; 266 267 // A means of enforcing the contractual requirement for a concurrent stream 268 // limit per subscriber across devices is provided via SecureStop. SecureStop 269 // is a means of securely monitoring the lifetime of sessions. Since playback 270 // on a device can be interrupted due to reboot, power failure, etc. a means 271 // of persisting the lifetime information on the device is needed. 272 // 273 // A signed version of the sessionID is written to persistent storage on the 274 // device when each MediaCrypto object is created. The sessionID is signed by 275 // the device private key to prevent tampering. 276 // 277 // In the normal case, playback will be completed, the session destroyed and 278 // the Secure Stops will be queried. The App queries secure stops and forwards 279 // the secure stop message to the server which verifies the signature and 280 // notifies the server side database that the session destruction has been 281 // confirmed. The persisted record on the client is only removed after positive 282 // confirmation that the server received the message using releaseSecureStops(). 283 virtual status_t getSecureStops(List<Vector<uint8_t> > &secureStops) = 0; 284 virtual status_t getSecureStop(Vector<uint8_t> const &ssid, Vector<uint8_t> &secureStop) = 0; 285 virtual status_t releaseSecureStops(Vector<uint8_t> const &ssRelease) = 0; 286 virtual status_t releaseAllSecureStops() = 0; 287 288 // Read a property value given the device property string. There are a few forms 289 // of property access methods, depending on the data type returned. 290 // Since DRM plugin properties may vary, additional field names may be defined 291 // by each DRM vendor. Refer to your DRM provider documentation for definitions 292 // of its additional field names. 293 // 294 // Standard values are: 295 // "vendor" [string] identifies the maker of the plugin 296 // "version" [string] identifies the version of the plugin 297 // "description" [string] describes the plugin 298 // 'deviceUniqueId' [byte array] The device unique identifier is established 299 // during device provisioning and provides a means of uniquely identifying 300 // each device. 301 virtual status_t getPropertyString(String8 const &name, String8 &value ) const = 0; 302 virtual status_t getPropertyByteArray(String8 const &name, 303 Vector<uint8_t> &value ) const = 0; 304 305 // Write a property value given the device property string. There are a few forms 306 // of property setting methods, depending on the data type. 307 // Since DRM plugin properties may vary, additional field names may be defined 308 // by each DRM vendor. Refer to your DRM provider documentation for definitions 309 // of its field names. 310 virtual status_t setPropertyString(String8 const &name, 311 String8 const &value ) = 0; 312 virtual status_t setPropertyByteArray(String8 const &name, 313 Vector<uint8_t> const &value ) = 0; 314 315 // The following methods implement operations on a CryptoSession to support 316 // encrypt, decrypt, sign verify operations on operator-provided 317 // session keys. 318 319 // 320 // The algorithm string conforms to JCA Standard Names for Cipher 321 // Transforms and is case insensitive. For example "AES/CBC/PKCS5Padding". 322 // 323 // Return OK if the algorithm is supported, otherwise return BAD_VALUE 324 // 325 virtual status_t setCipherAlgorithm(Vector<uint8_t> const &sessionId, 326 String8 const &algorithm) = 0; 327 328 // 329 // The algorithm string conforms to JCA Standard Names for Mac 330 // Algorithms and is case insensitive. For example "HmacSHA256". 331 // 332 // Return OK if the algorithm is supported, otherwise return BAD_VALUE 333 // 334 virtual status_t setMacAlgorithm(Vector<uint8_t> const &sessionId, 335 String8 const &algorithm) = 0; 336 337 // Encrypt the provided input buffer with the cipher algorithm 338 // specified by setCipherAlgorithm and the key selected by keyId, 339 // and return the encrypted data. 340 virtual status_t encrypt(Vector<uint8_t> const &sessionId, 341 Vector<uint8_t> const &keyId, 342 Vector<uint8_t> const &input, 343 Vector<uint8_t> const &iv, 344 Vector<uint8_t> &output) = 0; 345 346 // Decrypt the provided input buffer with the cipher algorithm 347 // specified by setCipherAlgorithm and the key selected by keyId, 348 // and return the decrypted data. 349 virtual status_t decrypt(Vector<uint8_t> const &sessionId, 350 Vector<uint8_t> const &keyId, 351 Vector<uint8_t> const &input, 352 Vector<uint8_t> const &iv, 353 Vector<uint8_t> &output) = 0; 354 355 // Compute a signature on the provided message using the mac algorithm 356 // specified by setMacAlgorithm and the key selected by keyId, 357 // and return the signature. 358 virtual status_t sign(Vector<uint8_t> const &sessionId, 359 Vector<uint8_t> const &keyId, 360 Vector<uint8_t> const &message, 361 Vector<uint8_t> &signature) = 0; 362 363 // Compute a signature on the provided message using the mac algorithm 364 // specified by setMacAlgorithm and the key selected by keyId, 365 // and compare with the expected result. Set result to true or 366 // false depending on the outcome. 367 virtual status_t verify(Vector<uint8_t> const &sessionId, 368 Vector<uint8_t> const &keyId, 369 Vector<uint8_t> const &message, 370 Vector<uint8_t> const &signature, 371 bool &match) = 0; 372 373 374 // Compute an RSA signature on the provided message using the algorithm 375 // specified by algorithm. 376 virtual status_t signRSA(Vector<uint8_t> const &sessionId, 377 String8 const &algorithm, 378 Vector<uint8_t> const &message, 379 Vector<uint8_t> const &wrapped_key, 380 Vector<uint8_t> &signature) = 0; 381 382 setListener(const sp<DrmPluginListener> & listener)383 status_t setListener(const sp<DrmPluginListener>& listener) { 384 Mutex::Autolock lock(mEventLock); 385 mListener = listener; 386 return OK; 387 } 388 389 protected: 390 // Plugins call these methods to deliver events to the java app 391 void sendEvent(EventType eventType, int extra, 392 Vector<uint8_t> const *sessionId, 393 Vector<uint8_t> const *data); 394 395 void sendExpirationUpdate(Vector<uint8_t> const *sessionId, 396 int64_t expiryTimeInMS); 397 398 void sendKeysChange(Vector<uint8_t> const *sessionId, 399 Vector<DrmPlugin::KeyStatus> const *keyStatusList, 400 bool hasNewUsableKey); 401 402 private: 403 Mutex mEventLock; 404 sp<DrmPluginListener> mListener; 405 406 DISALLOW_EVIL_CONSTRUCTORS(DrmPlugin); 407 }; 408 409 class DrmPluginListener: virtual public RefBase 410 { 411 public: 412 virtual void sendEvent(DrmPlugin::EventType eventType, int extra, 413 Vector<uint8_t> const *sessionId, 414 Vector<uint8_t> const *data) = 0; 415 416 virtual void sendExpirationUpdate(Vector<uint8_t> const *sessionId, 417 int64_t expiryTimeInMS) = 0; 418 419 virtual void sendKeysChange(Vector<uint8_t> const *sessionId, 420 Vector<DrmPlugin::KeyStatus> const *keyStatusList, 421 bool hasNewUsableKey) = 0; 422 }; 423 sendEvent(EventType eventType,int extra,Vector<uint8_t> const * sessionId,Vector<uint8_t> const * data)424 inline void DrmPlugin::sendEvent(EventType eventType, int extra, 425 Vector<uint8_t> const *sessionId, 426 Vector<uint8_t> const *data) { 427 mEventLock.lock(); 428 sp<DrmPluginListener> listener = mListener; 429 mEventLock.unlock(); 430 431 if (listener != NULL) { 432 listener->sendEvent(eventType, extra, sessionId, data); 433 } 434 } 435 sendExpirationUpdate(Vector<uint8_t> const * sessionId,int64_t expiryTimeInMS)436 inline void DrmPlugin::sendExpirationUpdate(Vector<uint8_t> const *sessionId, 437 int64_t expiryTimeInMS) { 438 mEventLock.lock(); 439 sp<DrmPluginListener> listener = mListener; 440 mEventLock.unlock(); 441 442 if (listener != NULL) { 443 listener->sendExpirationUpdate(sessionId, expiryTimeInMS); 444 } 445 } 446 sendKeysChange(Vector<uint8_t> const * sessionId,Vector<DrmPlugin::KeyStatus> const * keyStatusList,bool hasNewUsableKey)447 inline void DrmPlugin::sendKeysChange(Vector<uint8_t> const *sessionId, 448 Vector<DrmPlugin::KeyStatus> const *keyStatusList, 449 bool hasNewUsableKey) { 450 mEventLock.lock(); 451 sp<DrmPluginListener> listener = mListener; 452 mEventLock.unlock(); 453 454 if (listener != NULL) { 455 listener->sendKeysChange(sessionId, keyStatusList, hasNewUsableKey); 456 } 457 } 458 } // namespace android 459 460 #endif // DRM_API_H_ 461