/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef DRM_HAL_H_ #define DRM_HAL_H_ #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace drm = ::android::hardware::drm; using drm::V1_0::EventType; using drm::V1_0::IDrmFactory; using drm::V1_0::IDrmPlugin; using drm::V1_0::IDrmPluginListener; using drm::V1_1::SecurityLevel; using drm::V1_2::KeyStatus; using drm::V1_2::OfflineLicenseState; using ::android::hardware::hidl_vec; using ::android::hardware::Return; using ::android::hardware::Void; typedef drm::V1_2::IDrmPluginListener IDrmPluginListener_V1_2; typedef drm::V1_0::KeyStatus KeyStatus_V1_0; namespace android { struct DrmSessionClientInterface; inline bool operator==(const Vector &l, const Vector &r) { if (l.size() != r.size()) return false; return memcmp(l.array(), r.array(), l.size()) == 0; } struct DrmHal : public BnDrm, public IBinder::DeathRecipient, public IDrmPluginListener_V1_2 { struct DrmSessionClient : public BnResourceManagerClient { explicit DrmSessionClient(DrmHal* drm, const Vector& sessionId) : mSessionId(sessionId), mDrm(drm) {} virtual bool reclaimResource(); virtual String8 getName(); const Vector mSessionId; protected: virtual ~DrmSessionClient(); private: wp mDrm; DISALLOW_EVIL_CONSTRUCTORS(DrmSessionClient); }; DrmHal(); virtual ~DrmHal(); virtual status_t initCheck() const; virtual status_t isCryptoSchemeSupported(const uint8_t uuid[16], const String8& mimeType, DrmPlugin::SecurityLevel level, bool *isSupported); virtual status_t createPlugin(const uint8_t uuid[16], const String8 &appPackageName); virtual status_t destroyPlugin(); virtual status_t openSession(DrmPlugin::SecurityLevel level, Vector &sessionId); virtual status_t closeSession(Vector const &sessionId); virtual status_t getKeyRequest(Vector const &sessionId, Vector const &initData, String8 const &mimeType, DrmPlugin::KeyType keyType, KeyedVector const &optionalParameters, Vector &request, String8 &defaultUrl, DrmPlugin::KeyRequestType *keyRequestType); virtual status_t provideKeyResponse(Vector const &sessionId, Vector const &response, Vector &keySetId); virtual status_t removeKeys(Vector const &keySetId); virtual status_t restoreKeys(Vector const &sessionId, Vector const &keySetId); virtual status_t queryKeyStatus(Vector const &sessionId, KeyedVector &infoMap) const; virtual status_t getProvisionRequest(String8 const &certType, String8 const &certAuthority, Vector &request, String8 &defaulUrl); virtual status_t provideProvisionResponse(Vector const &response, Vector &certificate, Vector &wrappedKey); virtual status_t getSecureStops(List> &secureStops); virtual status_t getSecureStopIds(List> &secureStopIds); virtual status_t getSecureStop(Vector const &ssid, Vector &secureStop); virtual status_t releaseSecureStops(Vector const &ssRelease); virtual status_t removeSecureStop(Vector const &ssid); virtual status_t removeAllSecureStops(); virtual status_t getHdcpLevels(DrmPlugin::HdcpLevel *connectedLevel, DrmPlugin::HdcpLevel *maxLevel) const; virtual status_t getNumberOfSessions(uint32_t *currentSessions, uint32_t *maxSessions) const; virtual status_t getSecurityLevel(Vector const &sessionId, DrmPlugin::SecurityLevel *level) const; virtual status_t getOfflineLicenseKeySetIds(List> &keySetIds) const; virtual status_t removeOfflineLicense(Vector const &keySetId); virtual status_t getOfflineLicenseState(Vector const &keySetId, DrmPlugin::OfflineLicenseState *licenseState) const; virtual status_t getPropertyString(String8 const &name, String8 &value ) const; virtual status_t getPropertyByteArray(String8 const &name, Vector &value ) const; virtual status_t setPropertyString(String8 const &name, String8 const &value ) const; virtual status_t setPropertyByteArray(String8 const &name, Vector const &value ) const; virtual status_t getMetrics(os::PersistableBundle *metrics); virtual status_t setCipherAlgorithm(Vector const &sessionId, String8 const &algorithm); virtual status_t setMacAlgorithm(Vector const &sessionId, String8 const &algorithm); virtual status_t encrypt(Vector const &sessionId, Vector const &keyId, Vector const &input, Vector const &iv, Vector &output); virtual status_t decrypt(Vector const &sessionId, Vector const &keyId, Vector const &input, Vector const &iv, Vector &output); virtual status_t sign(Vector const &sessionId, Vector const &keyId, Vector const &message, Vector &signature); virtual status_t verify(Vector const &sessionId, Vector const &keyId, Vector const &message, Vector const &signature, bool &match); virtual status_t signRSA(Vector const &sessionId, String8 const &algorithm, Vector const &message, Vector const &wrappedKey, Vector &signature); virtual status_t setListener(const sp& listener); // Methods of IDrmPluginListener Return sendEvent(EventType eventType, const hidl_vec& sessionId, const hidl_vec& data); Return sendExpirationUpdate(const hidl_vec& sessionId, int64_t expiryTimeInMS); Return sendKeysChange(const hidl_vec& sessionId, const hidl_vec& keyStatusList, bool hasNewUsableKey); Return sendKeysChange_1_2(const hidl_vec& sessionId, const hidl_vec& keyStatusList, bool hasNewUsableKey); Return sendSessionLostState(const hidl_vec& sessionId); virtual void binderDied(const wp &the_late_who); private: static Mutex mLock; sp mListener; mutable Mutex mEventLock; mutable Mutex mNotifyLock; const Vector> mFactories; sp mPlugin; sp mPluginV1_1; sp mPluginV1_2; String8 mAppPackageName; // Mutable to allow modification within GetPropertyByteArray. mutable MediaDrmMetrics mMetrics; Vector> mOpenSessions; void closeOpenSessions(); void cleanup(); /** * mInitCheck is: * NO_INIT if a plugin hasn't been created yet * ERROR_UNSUPPORTED if a plugin can't be created for the uuid * OK after a plugin has been created and mPlugin is valid */ status_t mInitCheck; Vector> makeDrmFactories(); sp makeDrmPlugin(const sp& factory, const uint8_t uuid[16], const String8& appPackageName); void writeByteArray(Parcel &obj, const hidl_vec& array); void reportPluginMetrics() const; void reportFrameworkMetrics() const; status_t getPropertyStringInternal(String8 const &name, String8 &value) const; status_t getPropertyByteArrayInternal(String8 const &name, Vector &value) const; status_t matchMimeTypeAndSecurityLevel(const sp &factory, const uint8_t uuid[16], const String8 &mimeType, DrmPlugin::SecurityLevel level, bool *isSupported); DISALLOW_EVIL_CONSTRUCTORS(DrmHal); }; } // namespace android #endif // DRM_HAL_H_