1 /* 2 * Copyright (C) 2018 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 CLEARKEY_DRM_PLUGIN_H_ 18 #define CLEARKEY_DRM_PLUGIN_H_ 19 20 #include <android/hardware/drm/1.4/IDrmPlugin.h> 21 #include <android/hardware/drm/1.2/IDrmPluginListener.h> 22 23 #include <map> 24 #include <stdio.h> 25 26 #include <utils/List.h> 27 28 #include "DeviceFiles.h" 29 #include "SessionLibrary.h" 30 #include "Utils.h" 31 32 namespace android { 33 namespace hardware { 34 namespace drm { 35 namespace V1_4 { 36 namespace clearkey { 37 38 namespace drm = ::android::hardware::drm; 39 using drm::V1_0::EventType; 40 using drm::V1_0::IDrmPluginListener; 41 using drm::V1_0::KeyRequestType; 42 using drm::V1_0::KeyStatus; 43 using drm::V1_0::KeyType; 44 using drm::V1_0::KeyValue; 45 using drm::V1_0::SecureStop; 46 using drm::V1_0::SecureStopId; 47 using drm::V1_0::SessionId; 48 using drm::V1_0::Status; 49 using drm::V1_1::DrmMetricGroup; 50 using drm::V1_1::HdcpLevel; 51 using drm::V1_1::SecureStopRelease; 52 using drm::V1_1::SecurityLevel; 53 using drm::V1_2::KeySetId; 54 using drm::V1_2::OfflineLicenseState; 55 using drm::V1_4::clearkey::DeviceFiles; 56 using drm::V1_4::clearkey::Session; 57 using drm::V1_4::clearkey::SessionLibrary; 58 using drm::V1_4::IDrmPlugin; 59 60 using ::android::hardware::hidl_string; 61 using ::android::hardware::hidl_vec; 62 using ::android::hardware::Return; 63 using ::android::hardware::Void; 64 using ::android::sp; 65 66 typedef drm::V1_1::KeyRequestType KeyRequestType_V1_1; 67 typedef drm::V1_2::IDrmPluginListener IDrmPluginListener_V1_2; 68 typedef drm::V1_2::KeyStatus KeyStatus_V1_2; 69 typedef drm::V1_2::Status Status_V1_2; 70 typedef drm::V1_2::HdcpLevel HdcpLevel_V1_2; 71 72 struct DrmPlugin : public IDrmPlugin { 73 explicit DrmPlugin(SessionLibrary* sessionLibrary); 74 ~DrmPluginDrmPlugin75 virtual ~DrmPlugin() { mFileHandle.DeleteAllLicenses(); } 76 77 Return<void> openSession(openSession_cb _hidl_cb) override; 78 Return<void> openSession_1_1(SecurityLevel securityLevel, 79 openSession_cb _hidl_cb) override; 80 81 Return<Status> closeSession(const hidl_vec<uint8_t>& sessionId) override; 82 83 Return<void> getKeyRequest( 84 const hidl_vec<uint8_t>& scope, 85 const hidl_vec<uint8_t>& initData, 86 const hidl_string& mimeType, 87 KeyType keyType, 88 const hidl_vec<KeyValue>& optionalParameters, 89 getKeyRequest_cb _hidl_cb) override; 90 91 Return<void> getKeyRequest_1_1( 92 const hidl_vec<uint8_t>& scope, 93 const hidl_vec<uint8_t>& initData, 94 const hidl_string& mimeType, 95 KeyType keyType, 96 const hidl_vec<KeyValue>& optionalParameters, 97 getKeyRequest_1_1_cb _hidl_cb) override; 98 99 Return<void> getKeyRequest_1_2( 100 const hidl_vec<uint8_t>& scope, 101 const hidl_vec<uint8_t>& initData, 102 const hidl_string& mimeType, 103 KeyType keyType, 104 const hidl_vec<KeyValue>& optionalParameters, 105 getKeyRequest_1_2_cb _hidl_cb) override; 106 107 Return<void> provideKeyResponse( 108 const hidl_vec<uint8_t>& scope, 109 const hidl_vec<uint8_t>& response, 110 provideKeyResponse_cb _hidl_cb) override; 111 removeKeysDrmPlugin112 Return<Status> removeKeys(const hidl_vec<uint8_t>& sessionId) { 113 if (sessionId.size() == 0) { 114 return Status::BAD_VALUE; 115 } 116 return Status::ERROR_DRM_CANNOT_HANDLE; 117 } 118 119 Return<Status> restoreKeys( 120 const hidl_vec<uint8_t>& sessionId, 121 const hidl_vec<uint8_t>& keySetId) override; 122 123 Return<void> queryKeyStatus( 124 const hidl_vec<uint8_t>& sessionId, 125 queryKeyStatus_cb _hidl_cb) override; 126 getProvisionRequestDrmPlugin127 Return<void> getProvisionRequest( 128 const hidl_string& certificateType, 129 const hidl_string& certificateAuthority, 130 getProvisionRequest_cb _hidl_cb) { 131 UNUSED(certificateType); 132 UNUSED(certificateAuthority); 133 134 hidl_string defaultUrl; 135 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, hidl_vec<uint8_t>(), defaultUrl); 136 return Void(); 137 } 138 getProvisionRequest_1_2DrmPlugin139 Return<void> getProvisionRequest_1_2( 140 const hidl_string& certificateType, 141 const hidl_string& certificateAuthority, 142 getProvisionRequest_1_2_cb _hidl_cb) { 143 UNUSED(certificateType); 144 UNUSED(certificateAuthority); 145 146 hidl_string defaultUrl; 147 _hidl_cb(Status_V1_2::ERROR_DRM_CANNOT_HANDLE, hidl_vec<uint8_t>(), defaultUrl); 148 return Void(); 149 } 150 provideProvisionResponseDrmPlugin151 Return<void> provideProvisionResponse( 152 const hidl_vec<uint8_t>& response, 153 provideProvisionResponse_cb _hidl_cb) { 154 155 if (response.size() == 0) { 156 _hidl_cb(Status::BAD_VALUE, hidl_vec<uint8_t>(), hidl_vec<uint8_t>()); 157 return Void(); 158 } 159 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, hidl_vec<uint8_t>(), hidl_vec<uint8_t>()); 160 return Void(); 161 } 162 getHdcpLevelsDrmPlugin163 Return<void> getHdcpLevels(getHdcpLevels_cb _hidl_cb) { 164 HdcpLevel connectedLevel = HdcpLevel::HDCP_NONE; 165 HdcpLevel maxLevel = HdcpLevel::HDCP_NO_OUTPUT; 166 _hidl_cb(Status::OK, connectedLevel, maxLevel); 167 return Void(); 168 } 169 getHdcpLevels_1_2DrmPlugin170 Return<void> getHdcpLevels_1_2(getHdcpLevels_1_2_cb _hidl_cb) { 171 HdcpLevel_V1_2 connectedLevel = HdcpLevel_V1_2::HDCP_NONE; 172 HdcpLevel_V1_2 maxLevel = HdcpLevel_V1_2::HDCP_NO_OUTPUT; 173 _hidl_cb(Status_V1_2::OK, connectedLevel, maxLevel); 174 return Void(); 175 } 176 177 Return<void> getNumberOfSessions(getNumberOfSessions_cb _hidl_cb) override; 178 179 Return<void> getSecurityLevel(const hidl_vec<uint8_t>& sessionId, 180 getSecurityLevel_cb _hidl_cb) override; 181 182 Return<void> getMetrics(getMetrics_cb _hidl_cb) override; 183 184 Return<void> getOfflineLicenseKeySetIds(getOfflineLicenseKeySetIds_cb _hidl_cb) override; 185 186 Return<Status> removeOfflineLicense(const KeySetId &keySetId) override; 187 188 Return<void> getOfflineLicenseState(const KeySetId &keySetId, 189 getOfflineLicenseState_cb _hidl_cb) override; 190 191 Return<void> getPropertyString( 192 const hidl_string& name, 193 getPropertyString_cb _hidl_cb) override; 194 195 Return<void> getPropertyByteArray( 196 const hidl_string& name, 197 getPropertyByteArray_cb _hidl_cb) override; 198 199 Return<Status> setPropertyString( 200 const hidl_string& name, const hidl_string& value) override; 201 202 Return<Status> setPropertyByteArray( 203 const hidl_string& name, const hidl_vec<uint8_t>& value) override; 204 205 Return<void> getLogMessages( 206 getLogMessages_cb _hidl_cb) override; 207 208 Return<Status> setPlaybackId( 209 const hidl_vec<uint8_t>& sessionId, 210 const hidl_string& playbackId) override; 211 212 Return<bool> requiresSecureDecoder( 213 const hidl_string& mime, SecurityLevel level) override; 214 215 Return<bool> requiresSecureDecoderDefault(const hidl_string& mime) override; 216 setCipherAlgorithmDrmPlugin217 Return<Status> setCipherAlgorithm( 218 const hidl_vec<uint8_t>& sessionId, const hidl_string& algorithm) { 219 if (sessionId.size() == 0 || algorithm.size() == 0) { 220 return Status::BAD_VALUE; 221 } 222 return Status::ERROR_DRM_CANNOT_HANDLE; 223 } 224 setMacAlgorithmDrmPlugin225 Return<Status> setMacAlgorithm( 226 const hidl_vec<uint8_t>& sessionId, const hidl_string& algorithm) { 227 if (sessionId.size() == 0 || algorithm.size() == 0) { 228 return Status::BAD_VALUE; 229 } 230 return Status::ERROR_DRM_CANNOT_HANDLE; 231 } 232 encryptDrmPlugin233 Return<void> encrypt( 234 const hidl_vec<uint8_t>& sessionId, 235 const hidl_vec<uint8_t>& keyId, 236 const hidl_vec<uint8_t>& input, 237 const hidl_vec<uint8_t>& iv, 238 encrypt_cb _hidl_cb) { 239 if (sessionId.size() == 0 || keyId.size() == 0 || 240 input.size() == 0 || iv.size() == 0) { 241 _hidl_cb(Status::BAD_VALUE, hidl_vec<uint8_t>()); 242 return Void(); 243 } 244 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, hidl_vec<uint8_t>()); 245 return Void(); 246 } 247 decryptDrmPlugin248 Return<void> decrypt( 249 const hidl_vec<uint8_t>& sessionId, 250 const hidl_vec<uint8_t>& keyId, 251 const hidl_vec<uint8_t>& input, 252 const hidl_vec<uint8_t>& iv, 253 decrypt_cb _hidl_cb) { 254 if (sessionId.size() == 0 || keyId.size() == 0 || 255 input.size() == 0 || iv.size() == 0) { 256 _hidl_cb(Status::BAD_VALUE, hidl_vec<uint8_t>()); 257 return Void(); 258 } 259 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, hidl_vec<uint8_t>()); 260 return Void(); 261 } 262 signDrmPlugin263 Return<void> sign( 264 const hidl_vec<uint8_t>& sessionId, 265 const hidl_vec<uint8_t>& keyId, 266 const hidl_vec<uint8_t>& message, 267 sign_cb _hidl_cb) { 268 if (sessionId.size() == 0 || keyId.size() == 0 || 269 message.size() == 0) { 270 _hidl_cb(Status::BAD_VALUE, hidl_vec<uint8_t>()); 271 return Void(); 272 } 273 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, hidl_vec<uint8_t>()); 274 return Void(); 275 } 276 verifyDrmPlugin277 Return<void> verify( 278 const hidl_vec<uint8_t>& sessionId, 279 const hidl_vec<uint8_t>& keyId, 280 const hidl_vec<uint8_t>& message, 281 const hidl_vec<uint8_t>& signature, 282 verify_cb _hidl_cb) { 283 284 if (sessionId.size() == 0 || keyId.size() == 0 || 285 message.size() == 0 || signature.size() == 0) { 286 _hidl_cb(Status::BAD_VALUE, false); 287 return Void(); 288 } 289 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, false); 290 return Void(); 291 } 292 signRSADrmPlugin293 Return<void> signRSA( 294 const hidl_vec<uint8_t>& sessionId, 295 const hidl_string& algorithm, 296 const hidl_vec<uint8_t>& message, 297 const hidl_vec<uint8_t>& wrappedKey, 298 signRSA_cb _hidl_cb) { 299 if (sessionId.size() == 0 || algorithm.size() == 0 || 300 message.size() == 0 || wrappedKey.size() == 0) { 301 _hidl_cb(Status::BAD_VALUE, hidl_vec<uint8_t>()); 302 return Void(); 303 } 304 _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, hidl_vec<uint8_t>()); 305 return Void(); 306 } 307 setListenerDrmPlugin308 Return<void> setListener(const sp<IDrmPluginListener>& listener) { 309 mListener = listener; 310 mListenerV1_2 = IDrmPluginListener_V1_2::castFrom(listener); 311 return Void(); 312 }; 313 sendEventDrmPlugin314 Return<void> sendEvent( 315 EventType eventType, 316 const hidl_vec<uint8_t>& sessionId, 317 const hidl_vec<uint8_t>& data) { 318 if (mListenerV1_2 != NULL) { 319 mListenerV1_2->sendEvent(eventType, sessionId, data); 320 } else if (mListener != NULL) { 321 mListener->sendEvent(eventType, sessionId, data); 322 } else { 323 ALOGE("Null event listener, event not sent"); 324 } 325 return Void(); 326 } 327 sendExpirationUpdateDrmPlugin328 Return<void> sendExpirationUpdate( 329 const hidl_vec<uint8_t>& sessionId, 330 int64_t expiryTimeInMS) { 331 if (mListenerV1_2 != NULL) { 332 mListenerV1_2->sendExpirationUpdate(sessionId, expiryTimeInMS); 333 } else if (mListener != NULL) { 334 mListener->sendExpirationUpdate(sessionId, expiryTimeInMS); 335 } else { 336 ALOGE("Null event listener, event not sent"); 337 } 338 return Void(); 339 } 340 sendKeysChangeDrmPlugin341 Return<void> sendKeysChange( 342 const hidl_vec<uint8_t>& sessionId, 343 const hidl_vec<KeyStatus>& keyStatusList, bool hasNewUsableKey) { 344 if (mListenerV1_2 != NULL) { 345 mListenerV1_2->sendKeysChange(sessionId, keyStatusList, hasNewUsableKey); 346 } else if (mListener != NULL) { 347 mListener->sendKeysChange(sessionId, keyStatusList, hasNewUsableKey); 348 } else { 349 ALOGE("Null event listener, event not sent"); 350 } 351 return Void(); 352 } 353 sendKeysChange_1_2DrmPlugin354 Return<void> sendKeysChange_1_2( 355 const hidl_vec<uint8_t>& sessionId, 356 const hidl_vec<KeyStatus_V1_2>& keyStatusList, bool hasNewUsableKey) { 357 if (mListenerV1_2 != NULL) { 358 mListenerV1_2->sendKeysChange_1_2(sessionId, keyStatusList, hasNewUsableKey); 359 } 360 return Void(); 361 } 362 sendSessionLostStateDrmPlugin363 Return<void> sendSessionLostState( 364 const hidl_vec<uint8_t>& sessionId) { 365 if (mListenerV1_2 != NULL) { 366 mListenerV1_2->sendSessionLostState(sessionId); 367 } 368 return Void(); 369 } 370 371 Return<void> getSecureStops(getSecureStops_cb _hidl_cb); 372 373 Return<void> getSecureStop(const hidl_vec<uint8_t>& secureStopId, 374 getSecureStop_cb _hidl_cb); 375 376 Return<Status> releaseSecureStop(const hidl_vec<uint8_t>& ssRelease); 377 378 Return<Status> releaseAllSecureStops(); 379 380 Return<void> getSecureStopIds(getSecureStopIds_cb _hidl_cb); 381 382 Return<Status> releaseSecureStops(const SecureStopRelease& ssRelease); 383 384 Return<Status> removeSecureStop(const hidl_vec<uint8_t>& secureStopId); 385 386 Return<Status> removeAllSecureStops(); 387 388 private: 389 void initProperties(); 390 void installSecureStop(const hidl_vec<uint8_t>& sessionId); 391 bool makeKeySetId(std::string* keySetId); 392 void setPlayPolicy(); 393 394 Return<Status> setSecurityLevel(const hidl_vec<uint8_t>& sessionId, 395 SecurityLevel level); 396 397 Status_V1_2 getKeyRequestCommon(const hidl_vec<uint8_t>& scope, 398 const hidl_vec<uint8_t>& initData, 399 const hidl_string& mimeType, 400 KeyType keyType, 401 const hidl_vec<KeyValue>& optionalParameters, 402 std::vector<uint8_t> *request, 403 KeyRequestType_V1_1 *getKeyRequestType, 404 std::string *defaultUrl); 405 406 struct ClearkeySecureStop { 407 std::vector<uint8_t> id; 408 std::vector<uint8_t> data; 409 }; 410 411 std::map<std::vector<uint8_t>, ClearkeySecureStop> mSecureStops; 412 std::vector<KeyValue> mPlayPolicy; 413 std::map<std::string, std::string> mStringProperties; 414 std::map<std::string, std::vector<uint8_t> > mByteArrayProperties; 415 std::map<std::string, std::vector<uint8_t> > mReleaseKeysMap; 416 std::map<std::vector<uint8_t>, std::string> mPlaybackId; 417 std::map<std::vector<uint8_t>, SecurityLevel> mSecurityLevel; 418 sp<IDrmPluginListener> mListener; 419 sp<IDrmPluginListener_V1_2> mListenerV1_2; 420 SessionLibrary *mSessionLibrary; 421 int64_t mOpenSessionOkCount; 422 int64_t mCloseSessionOkCount; 423 int64_t mCloseSessionNotOpenedCount; 424 uint32_t mNextSecureStopId; 425 android::Mutex mPlayPolicyLock; 426 427 // set by property to mock error scenarios 428 Status_V1_2 mMockError; 429 processMockErrorDrmPlugin430 void processMockError(const sp<Session> &session) { 431 session->setMockError(mMockError); 432 mMockError = Status_V1_2::OK; 433 } 434 435 DeviceFiles mFileHandle GUARDED_BY(mFileHandleLock); 436 Mutex mFileHandleLock; 437 Mutex mSecureStopLock; 438 439 CLEARKEY_DISALLOW_COPY_AND_ASSIGN_AND_NEW(DrmPlugin); 440 }; 441 442 } // namespace clearkey 443 } // namespace V1_4 444 } // namespace drm 445 } // namespace hardware 446 } // namespace android 447 448 #endif // CLEARKEY_DRM_PLUGIN_H_ 449