ClearKeySessionManager.h (5508B)
1 /* 2 * Copyright 2015, Mozilla Foundation and contributors 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 __ClearKeyDecryptor_h__ 18 #define __ClearKeyDecryptor_h__ 19 20 // This include is required in order for content_decryption_module to work 21 // on Unix systems. 22 23 #include <functional> 24 #include <map> 25 #include <optional> 26 #include <queue> 27 #include <set> 28 #include <string> 29 30 #include "ClearKeyDecryptionManager.h" 31 #include "ClearKeyPersistence.h" 32 #include "ClearKeySession.h" 33 #include "ClearKeyUtils.h" 34 #include "RefCounted.h" 35 #include "content_decryption_module.h" 36 #include "mozilla/TimeStamp.h" 37 38 class ClearKeySessionManager final : public RefCounted { 39 public: 40 explicit ClearKeySessionManager(cdm::Host_11* aHost); 41 42 void Init(bool aDistinctiveIdentifierAllowed, bool aPersistentStateAllowed); 43 44 void CreateSession(uint32_t aPromiseId, cdm::InitDataType aInitDataType, 45 const uint8_t* aInitData, uint32_t aInitDataSize, 46 cdm::SessionType aSessionType); 47 48 void LoadSession(uint32_t aPromiseId, const char* aSessionId, 49 uint32_t aSessionIdLength); 50 51 void UpdateSession(uint32_t aPromiseId, const char* aSessionId, 52 uint32_t aSessionIdLength, const uint8_t* aResponse, 53 uint32_t aResponseSize); 54 55 void CloseSession(uint32_t aPromiseId, const char* aSessionId, 56 uint32_t aSessionIdLength); 57 58 void RemoveSession(uint32_t aPromiseId, const char* aSessionId, 59 uint32_t aSessionIdLength); 60 61 void SetServerCertificate(uint32_t aPromiseId, const uint8_t* aServerCert, 62 uint32_t aServerCertSize); 63 64 cdm::Status Decrypt(const cdm::InputBuffer_2& aBuffer, 65 cdm::DecryptedBlock* aDecryptedBlock); 66 67 void DecryptingComplete(); 68 69 void PersistentSessionDataLoaded(uint32_t aPromiseId, 70 const std::string& aSessionId, 71 const uint8_t* aKeyData, 72 uint32_t aKeyDataSize); 73 74 // Receives the result of an output protection query from the user agent. 75 // This may trigger a key status change. 76 // @param aResult indicates if the query succeeded or not. If a query did 77 // not succeed then that other arguments are ignored. 78 // @param aLinkMask is used to indicate if output could be captured by the 79 // user agent. It should be set to `kLinkTypeNetwork` if capture is possible, 80 // otherwise it should be zero. 81 // @param aOutputProtectionMask this argument is unused. 82 void OnQueryOutputProtectionStatus(cdm::QueryResult aResult, 83 uint32_t aLinkMask, 84 uint32_t aOutputProtectionMask); 85 86 // Prompts the session manager to query the output protection status if we 87 // haven't yet, or if enough time has passed since the last check. Will also 88 // notify if a check has not been responded to on time. 89 void QueryOutputProtectionStatusIfNeeded(); 90 91 private: 92 ~ClearKeySessionManager(); 93 94 void ClearInMemorySessionData(ClearKeySession* aSession); 95 bool MaybeDeferTillInitialized(std::function<void()>&& aMaybeDefer); 96 void Serialize(const ClearKeySession* aSession, 97 std::vector<uint8_t>& aOutKeyData); 98 99 // Signals the host to perform an output protection check. 100 void QueryOutputProtectionStatusFromHost(); 101 102 // Called to notify the result of an output protection status call. The 103 // following arguments are expected, along with their intended use: 104 // - KeyStatus::kUsable indicates that the query was responded to and the 105 // response showed output is protected. 106 // - KeyStatus::kOutputRestricted indicates that the query was responded to 107 // and the response showed output is not protected. 108 // - KeyStatus::kInternalError indicates a query was not repsonded to on 109 // time, or that a query was responded to with a failed cdm::QueryResult. 110 // The status passed to this function will be used to update the status of 111 // the keyId "output-protection", which tests an observe. 112 void NotifyOutputProtectionStatus(cdm::KeyStatus aStatus); 113 114 RefPtr<ClearKeyDecryptionManager> mDecryptionManager; 115 RefPtr<ClearKeyPersistence> mPersistence; 116 117 cdm::Host_11* mHost = nullptr; 118 119 std::set<KeyId> mKeyIds; 120 std::map<std::string, ClearKeySession*> mSessions; 121 122 // The session id of the last session created or loaded from persistent 123 // storage. Used to fire test messages at that session. 124 std::optional<std::string> mLastSessionId; 125 126 std::queue<std::function<void()>> mDeferredInitialize; 127 128 // If there is an inflight query to the host to check the output protection 129 // status. Multiple in flight queries should not be allowed, avoid firing 130 // more if this is true. 131 bool mHasOutstandingOutputProtectionQuery = false; 132 // The last time the manager called QueryOutputProtectionStatus on the host. 133 mozilla::TimeStamp mLastOutputProtectionQueryTime; 134 }; 135 136 #endif // __ClearKeyDecryptor_h__