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 21 #include <android/hardware/drm/1.0/IDrmFactory.h> 22 #include <android/hardware/drm/1.0/IDrmPlugin.h> 23 #include <android/hardware/drm/1.1/IDrmFactory.h> 24 #include <android/hardware/drm/1.1/IDrmPlugin.h> 25 #include <android/hardware/drm/1.2/IDrmFactory.h> 26 #include <android/hardware/drm/1.2/IDrmPlugin.h> 27 #include <android/hardware/drm/1.2/IDrmPluginListener.h> 28 #include <android/hardware/drm/1.4/IDrmPlugin.h> 29 #include <android/hardware/drm/1.4/types.h> 30 31 #include <media/drm/DrmAPI.h> 32 #include <mediadrm/DrmMetrics.h> 33 #include <mediadrm/DrmSessionManager.h> 34 #include <mediadrm/IDrm.h> 35 #include <mediadrm/IDrmClient.h> 36 #include <mediadrm/IDrmMetricsConsumer.h> 37 #include <utils/threads.h> 38 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; 44 using drm::V1_1::SecurityLevel; 45 using drm::V1_2::KeyStatus; 46 using drm::V1_2::OfflineLicenseState; 47 using ::android::hardware::hidl_vec; 48 using ::android::hardware::Return; 49 using ::android::hardware::Void; 50 51 typedef drm::V1_2::IDrmPluginListener IDrmPluginListener_V1_2; 52 typedef drm::V1_0::KeyStatus KeyStatus_V1_0; 53 54 namespace android { 55 56 struct DrmSessionClientInterface; 57 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 63 struct DrmHal : public IDrm, 64 public IDrmPluginListener_V1_2 { 65 66 struct DrmSessionClient; 67 68 DrmHal(); 69 virtual ~DrmHal(); 70 71 virtual status_t initCheck() const; 72 73 virtual status_t isCryptoSchemeSupported(const uint8_t uuid[16], 74 const String8& mimeType, 75 DrmPlugin::SecurityLevel level, 76 bool *isSupported); 77 78 virtual status_t createPlugin(const uint8_t uuid[16], 79 const String8 &appPackageName); 80 81 virtual status_t destroyPlugin(); 82 83 virtual status_t openSession(DrmPlugin::SecurityLevel level, 84 Vector<uint8_t> &sessionId); 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 117 virtual status_t getSecureStops(List<Vector<uint8_t>> &secureStops); 118 virtual status_t getSecureStopIds(List<Vector<uint8_t>> &secureStopIds); 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); 122 virtual status_t removeSecureStop(Vector<uint8_t> const &ssid); 123 virtual status_t removeAllSecureStops(); 124 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; 131 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 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; 143 virtual status_t getMetrics(const sp<IDrmMetricsConsumer> &consumer); 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 182 virtual status_t requiresSecureDecoder(const char *mime, bool *required) const; 183 184 virtual status_t requiresSecureDecoder(const char *mime, DrmPlugin::SecurityLevel securityLevel, 185 bool *required) const; 186 187 virtual status_t setPlaybackId( 188 Vector<uint8_t> const &sessionId, 189 const char *playbackId); 190 191 virtual status_t getLogMessages(Vector<drm::V1_4::LogMessage> &logs) const; 192 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, 201 const hidl_vec<KeyStatus_V1_0>& keyStatusList, bool hasNewUsableKey); 202 203 Return<void> sendKeysChange_1_2(const hidl_vec<uint8_t>& sessionId, 204 const hidl_vec<KeyStatus>& keyStatusList, bool hasNewUsableKey); 205 206 Return<void> sendSessionLostState(const hidl_vec<uint8_t>& sessionId); 207 208 private: 209 static Mutex mLock; 210 211 sp<IDrmClient> mListener; 212 mutable Mutex mEventLock; 213 mutable Mutex mNotifyLock; 214 215 const std::vector<sp<IDrmFactory>> mFactories; 216 sp<IDrmPlugin> mPlugin; 217 sp<drm::V1_1::IDrmPlugin> mPluginV1_1; 218 sp<drm::V1_2::IDrmPlugin> mPluginV1_2; 219 sp<drm::V1_4::IDrmPlugin> mPluginV1_4; 220 String8 mAppPackageName; 221 222 // Mutable to allow modification within GetPropertyByteArray. 223 mutable MediaDrmMetrics mMetrics; 224 225 std::vector<std::shared_ptr<DrmSessionClient>> mOpenSessions; 226 void closeOpenSessions(); 227 void cleanup(); 228 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 237 std::vector<sp<IDrmFactory>> makeDrmFactories(); 238 sp<IDrmPlugin> makeDrmPlugin(const sp<IDrmFactory>& factory, 239 const uint8_t uuid[16], const String8& appPackageName); 240 241 void writeByteArray(Parcel &obj, const hidl_vec<uint8_t>& array); 242 243 std::string reportPluginMetrics() const; 244 std::string reportFrameworkMetrics(const std::string& pluginMetrics) const; 245 status_t getPropertyStringInternal(String8 const &name, String8 &value) const; 246 status_t getPropertyByteArrayInternal(String8 const &name, 247 Vector<uint8_t> &value) const; 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); 253 254 DISALLOW_EVIL_CONSTRUCTORS(DrmHal); 255 }; 256 257 } // namespace android 258 259 #endif // DRM_HAL_H_ 260