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