Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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_HAL_H_ |
| 18 | |
| 19 | #define DRM_HAL_H_ |
| 20 | |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 21 | #include <android/hardware/drm/1.0/IDrmFactory.h> |
Jeff Tinker | e307dc4 | 2018-02-11 19:53:54 +0000 | [diff] [blame] | 22 | #include <android/hardware/drm/1.0/IDrmPlugin.h> |
Jeff Tinker | e307dc4 | 2018-02-11 19:53:54 +0000 | [diff] [blame] | 23 | #include <android/hardware/drm/1.1/IDrmFactory.h> |
| 24 | #include <android/hardware/drm/1.1/IDrmPlugin.h> |
Jeff Tinker | c8baaba | 2018-10-23 11:32:36 -0700 | [diff] [blame] | 25 | #include <android/hardware/drm/1.2/IDrmFactory.h> |
| 26 | #include <android/hardware/drm/1.2/IDrmPlugin.h> |
Jeff Tinker | b8684f3 | 2018-12-12 08:41:31 -0800 | [diff] [blame] | 27 | #include <android/hardware/drm/1.2/IDrmPluginListener.h> |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 28 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame^] | 29 | #include <media/IResourceManagerService.h> |
Adam Stone | ab394d1 | 2017-12-22 12:34:20 -0800 | [diff] [blame] | 30 | #include <media/MediaAnalyticsItem.h> |
Jeff Tinker | 7d2c6e8 | 2018-02-16 16:14:59 -0800 | [diff] [blame] | 31 | #include <mediadrm/DrmMetrics.h> |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame^] | 32 | #include <mediadrm/DrmSessionManager.h> |
Jeff Tinker | 7d2c6e8 | 2018-02-16 16:14:59 -0800 | [diff] [blame] | 33 | #include <mediadrm/IDrm.h> |
| 34 | #include <mediadrm/IDrmClient.h> |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 35 | #include <utils/threads.h> |
| 36 | |
Jeff Tinker | 6d998b6 | 2017-12-18 14:37:43 -0800 | [diff] [blame] | 37 | namespace drm = ::android::hardware::drm; |
| 38 | using drm::V1_0::EventType; |
| 39 | using drm::V1_0::IDrmFactory; |
| 40 | using drm::V1_0::IDrmPlugin; |
| 41 | using drm::V1_0::IDrmPluginListener; |
Jeff Tinker | 99dbfa8 | 2019-01-17 17:27:06 -0800 | [diff] [blame] | 42 | using drm::V1_1::SecurityLevel; |
Robert Shih | a503326 | 2019-05-06 14:15:12 -0700 | [diff] [blame] | 43 | using drm::V1_2::KeyStatus; |
Jeff Tinker | c8baaba | 2018-10-23 11:32:36 -0700 | [diff] [blame] | 44 | using drm::V1_2::OfflineLicenseState; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 45 | using ::android::hardware::hidl_vec; |
| 46 | using ::android::hardware::Return; |
| 47 | using ::android::hardware::Void; |
| 48 | |
Jeff Tinker | b8684f3 | 2018-12-12 08:41:31 -0800 | [diff] [blame] | 49 | typedef drm::V1_2::IDrmPluginListener IDrmPluginListener_V1_2; |
Robert Shih | a503326 | 2019-05-06 14:15:12 -0700 | [diff] [blame] | 50 | typedef drm::V1_0::KeyStatus KeyStatus_V1_0; |
Jeff Tinker | b8684f3 | 2018-12-12 08:41:31 -0800 | [diff] [blame] | 51 | |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 52 | namespace android { |
| 53 | |
| 54 | struct DrmSessionClientInterface; |
| 55 | |
Jeff Tinker | 6133281 | 2017-05-15 16:53:10 -0700 | [diff] [blame] | 56 | inline bool operator==(const Vector<uint8_t> &l, const Vector<uint8_t> &r) { |
| 57 | if (l.size() != r.size()) return false; |
| 58 | return memcmp(l.array(), r.array(), l.size()) == 0; |
| 59 | } |
| 60 | |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 61 | struct DrmHal : public BnDrm, |
Robert Shih | a503326 | 2019-05-06 14:15:12 -0700 | [diff] [blame] | 62 | public IBinder::DeathRecipient, |
Jeff Tinker | b8684f3 | 2018-12-12 08:41:31 -0800 | [diff] [blame] | 63 | public IDrmPluginListener_V1_2 { |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame^] | 64 | |
| 65 | struct DrmSessionClient : public BnResourceManagerClient { |
| 66 | explicit DrmSessionClient(DrmHal* drm, const Vector<uint8_t>& sessionId) |
| 67 | : mSessionId(sessionId), |
| 68 | mDrm(drm) {} |
| 69 | |
| 70 | virtual bool reclaimResource(); |
| 71 | virtual String8 getName(); |
| 72 | |
| 73 | const Vector<uint8_t> mSessionId; |
| 74 | |
| 75 | protected: |
| 76 | virtual ~DrmSessionClient(); |
| 77 | |
| 78 | private: |
| 79 | wp<DrmHal> mDrm; |
| 80 | |
| 81 | DISALLOW_EVIL_CONSTRUCTORS(DrmSessionClient); |
| 82 | }; |
| 83 | |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 84 | DrmHal(); |
| 85 | virtual ~DrmHal(); |
| 86 | |
| 87 | virtual status_t initCheck() const; |
| 88 | |
Jeff Tinker | db3fa5f | 2019-01-25 22:56:56 -0800 | [diff] [blame] | 89 | virtual status_t isCryptoSchemeSupported(const uint8_t uuid[16], |
| 90 | const String8& mimeType, |
| 91 | DrmPlugin::SecurityLevel level, |
| 92 | bool *isSupported); |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 93 | |
Edwin Wong | 68b3d9f | 2017-01-06 19:07:54 -0800 | [diff] [blame] | 94 | virtual status_t createPlugin(const uint8_t uuid[16], |
| 95 | const String8 &appPackageName); |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 96 | |
| 97 | virtual status_t destroyPlugin(); |
| 98 | |
Jeff Tinker | 41d279a | 2018-02-11 19:52:08 +0000 | [diff] [blame] | 99 | virtual status_t openSession(DrmPlugin::SecurityLevel level, |
| 100 | Vector<uint8_t> &sessionId); |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 101 | |
| 102 | virtual status_t closeSession(Vector<uint8_t> const &sessionId); |
| 103 | |
| 104 | virtual status_t |
| 105 | getKeyRequest(Vector<uint8_t> const &sessionId, |
| 106 | Vector<uint8_t> const &initData, |
| 107 | String8 const &mimeType, DrmPlugin::KeyType keyType, |
| 108 | KeyedVector<String8, String8> const &optionalParameters, |
| 109 | Vector<uint8_t> &request, String8 &defaultUrl, |
| 110 | DrmPlugin::KeyRequestType *keyRequestType); |
| 111 | |
| 112 | virtual status_t provideKeyResponse(Vector<uint8_t> const &sessionId, |
| 113 | Vector<uint8_t> const &response, |
| 114 | Vector<uint8_t> &keySetId); |
| 115 | |
| 116 | virtual status_t removeKeys(Vector<uint8_t> const &keySetId); |
| 117 | |
| 118 | virtual status_t restoreKeys(Vector<uint8_t> const &sessionId, |
| 119 | Vector<uint8_t> const &keySetId); |
| 120 | |
| 121 | virtual status_t queryKeyStatus(Vector<uint8_t> const &sessionId, |
| 122 | KeyedVector<String8, String8> &infoMap) const; |
| 123 | |
| 124 | virtual status_t getProvisionRequest(String8 const &certType, |
| 125 | String8 const &certAuthority, |
| 126 | Vector<uint8_t> &request, |
| 127 | String8 &defaulUrl); |
| 128 | |
| 129 | virtual status_t provideProvisionResponse(Vector<uint8_t> const &response, |
| 130 | Vector<uint8_t> &certificate, |
| 131 | Vector<uint8_t> &wrappedKey); |
| 132 | |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 133 | virtual status_t getSecureStops(List<Vector<uint8_t>> &secureStops); |
Jeff Tinker | 15177d7 | 2018-01-25 12:57:55 -0800 | [diff] [blame] | 134 | virtual status_t getSecureStopIds(List<Vector<uint8_t>> &secureStopIds); |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 135 | virtual status_t getSecureStop(Vector<uint8_t> const &ssid, Vector<uint8_t> &secureStop); |
| 136 | |
| 137 | virtual status_t releaseSecureStops(Vector<uint8_t> const &ssRelease); |
Jeff Tinker | 15177d7 | 2018-01-25 12:57:55 -0800 | [diff] [blame] | 138 | virtual status_t removeSecureStop(Vector<uint8_t> const &ssid); |
| 139 | virtual status_t removeAllSecureStops(); |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 140 | |
Jeff Tinker | 6d998b6 | 2017-12-18 14:37:43 -0800 | [diff] [blame] | 141 | virtual status_t getHdcpLevels(DrmPlugin::HdcpLevel *connectedLevel, |
| 142 | DrmPlugin::HdcpLevel *maxLevel) const; |
| 143 | virtual status_t getNumberOfSessions(uint32_t *currentSessions, |
| 144 | uint32_t *maxSessions) const; |
| 145 | virtual status_t getSecurityLevel(Vector<uint8_t> const &sessionId, |
| 146 | DrmPlugin::SecurityLevel *level) const; |
Jeff Tinker | 6d998b6 | 2017-12-18 14:37:43 -0800 | [diff] [blame] | 147 | |
Jeff Tinker | c8baaba | 2018-10-23 11:32:36 -0700 | [diff] [blame] | 148 | virtual status_t getOfflineLicenseKeySetIds(List<Vector<uint8_t>> &keySetIds) const; |
| 149 | virtual status_t removeOfflineLicense(Vector<uint8_t> const &keySetId); |
| 150 | virtual status_t getOfflineLicenseState(Vector<uint8_t> const &keySetId, |
| 151 | DrmPlugin::OfflineLicenseState *licenseState) const; |
| 152 | |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 153 | virtual status_t getPropertyString(String8 const &name, String8 &value ) const; |
| 154 | virtual status_t getPropertyByteArray(String8 const &name, |
| 155 | Vector<uint8_t> &value ) const; |
| 156 | virtual status_t setPropertyString(String8 const &name, String8 const &value ) const; |
| 157 | virtual status_t setPropertyByteArray(String8 const &name, |
| 158 | Vector<uint8_t> const &value ) const; |
Adam Stone | 637b785 | 2018-01-30 12:09:36 -0800 | [diff] [blame] | 159 | virtual status_t getMetrics(os::PersistableBundle *metrics); |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 160 | |
| 161 | virtual status_t setCipherAlgorithm(Vector<uint8_t> const &sessionId, |
| 162 | String8 const &algorithm); |
| 163 | |
| 164 | virtual status_t setMacAlgorithm(Vector<uint8_t> const &sessionId, |
| 165 | String8 const &algorithm); |
| 166 | |
| 167 | virtual status_t encrypt(Vector<uint8_t> const &sessionId, |
| 168 | Vector<uint8_t> const &keyId, |
| 169 | Vector<uint8_t> const &input, |
| 170 | Vector<uint8_t> const &iv, |
| 171 | Vector<uint8_t> &output); |
| 172 | |
| 173 | virtual status_t decrypt(Vector<uint8_t> const &sessionId, |
| 174 | Vector<uint8_t> const &keyId, |
| 175 | Vector<uint8_t> const &input, |
| 176 | Vector<uint8_t> const &iv, |
| 177 | Vector<uint8_t> &output); |
| 178 | |
| 179 | virtual status_t sign(Vector<uint8_t> const &sessionId, |
| 180 | Vector<uint8_t> const &keyId, |
| 181 | Vector<uint8_t> const &message, |
| 182 | Vector<uint8_t> &signature); |
| 183 | |
| 184 | virtual status_t verify(Vector<uint8_t> const &sessionId, |
| 185 | Vector<uint8_t> const &keyId, |
| 186 | Vector<uint8_t> const &message, |
| 187 | Vector<uint8_t> const &signature, |
| 188 | bool &match); |
| 189 | |
| 190 | virtual status_t signRSA(Vector<uint8_t> const &sessionId, |
| 191 | String8 const &algorithm, |
| 192 | Vector<uint8_t> const &message, |
| 193 | Vector<uint8_t> const &wrappedKey, |
| 194 | Vector<uint8_t> &signature); |
| 195 | |
| 196 | virtual status_t setListener(const sp<IDrmClient>& listener); |
| 197 | |
| 198 | // Methods of IDrmPluginListener |
| 199 | Return<void> sendEvent(EventType eventType, |
| 200 | const hidl_vec<uint8_t>& sessionId, const hidl_vec<uint8_t>& data); |
| 201 | |
| 202 | Return<void> sendExpirationUpdate(const hidl_vec<uint8_t>& sessionId, |
| 203 | int64_t expiryTimeInMS); |
| 204 | |
| 205 | Return<void> sendKeysChange(const hidl_vec<uint8_t>& sessionId, |
Robert Shih | a503326 | 2019-05-06 14:15:12 -0700 | [diff] [blame] | 206 | const hidl_vec<KeyStatus_V1_0>& keyStatusList, bool hasNewUsableKey); |
| 207 | |
| 208 | Return<void> sendKeysChange_1_2(const hidl_vec<uint8_t>& sessionId, |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 209 | const hidl_vec<KeyStatus>& keyStatusList, bool hasNewUsableKey); |
| 210 | |
Jeff Tinker | b8684f3 | 2018-12-12 08:41:31 -0800 | [diff] [blame] | 211 | Return<void> sendSessionLostState(const hidl_vec<uint8_t>& sessionId); |
| 212 | |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 213 | virtual void binderDied(const wp<IBinder> &the_late_who); |
| 214 | |
| 215 | private: |
| 216 | static Mutex mLock; |
| 217 | |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 218 | sp<IDrmClient> mListener; |
| 219 | mutable Mutex mEventLock; |
| 220 | mutable Mutex mNotifyLock; |
| 221 | |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 222 | const Vector<sp<IDrmFactory>> mFactories; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 223 | sp<IDrmPlugin> mPlugin; |
Jeff Tinker | 6d998b6 | 2017-12-18 14:37:43 -0800 | [diff] [blame] | 224 | sp<drm::V1_1::IDrmPlugin> mPluginV1_1; |
Jeff Tinker | c8baaba | 2018-10-23 11:32:36 -0700 | [diff] [blame] | 225 | sp<drm::V1_2::IDrmPlugin> mPluginV1_2; |
Adam Stone | 32494f5 | 2018-02-26 22:53:27 -0800 | [diff] [blame] | 226 | String8 mAppPackageName; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 227 | |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 228 | // Mutable to allow modification within GetPropertyByteArray. |
| 229 | mutable MediaDrmMetrics mMetrics; |
Adam Stone | aaf87dd | 2018-01-08 11:55:06 -0800 | [diff] [blame] | 230 | |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame^] | 231 | Vector<sp<DrmSessionClient>> mOpenSessions; |
Jeff Tinker | 6133281 | 2017-05-15 16:53:10 -0700 | [diff] [blame] | 232 | void closeOpenSessions(); |
Jeff Tinker | 7dfe28f | 2018-02-15 12:17:40 -0800 | [diff] [blame] | 233 | void cleanup(); |
Jeff Tinker | 6133281 | 2017-05-15 16:53:10 -0700 | [diff] [blame] | 234 | |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 235 | /** |
| 236 | * mInitCheck is: |
| 237 | * NO_INIT if a plugin hasn't been created yet |
| 238 | * ERROR_UNSUPPORTED if a plugin can't be created for the uuid |
| 239 | * OK after a plugin has been created and mPlugin is valid |
| 240 | */ |
| 241 | status_t mInitCheck; |
| 242 | |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 243 | Vector<sp<IDrmFactory>> makeDrmFactories(); |
| 244 | sp<IDrmPlugin> makeDrmPlugin(const sp<IDrmFactory>& factory, |
| 245 | const uint8_t uuid[16], const String8& appPackageName); |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 246 | |
| 247 | void writeByteArray(Parcel &obj, const hidl_vec<uint8_t>& array); |
| 248 | |
Adam Stone | fb679e3 | 2018-02-07 10:25:48 -0800 | [diff] [blame] | 249 | void reportPluginMetrics() const; |
| 250 | void reportFrameworkMetrics() const; |
John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 251 | status_t getPropertyStringInternal(String8 const &name, String8 &value) const; |
| 252 | status_t getPropertyByteArrayInternal(String8 const &name, |
| 253 | Vector<uint8_t> &value) const; |
Jeff Tinker | db3fa5f | 2019-01-25 22:56:56 -0800 | [diff] [blame] | 254 | status_t matchMimeTypeAndSecurityLevel(const sp<IDrmFactory> &factory, |
| 255 | const uint8_t uuid[16], |
| 256 | const String8 &mimeType, |
| 257 | DrmPlugin::SecurityLevel level, |
| 258 | bool *isSupported); |
John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 259 | |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 260 | DISALLOW_EVIL_CONSTRUCTORS(DrmHal); |
| 261 | }; |
| 262 | |
| 263 | } // namespace android |
| 264 | |
| 265 | #endif // DRM_HAL_H_ |