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 CLEARKEY_SESSION_LIBRARY_H_ 18 #define CLEARKEY_SESSION_LIBRARY_H_ 19 20 #include <media/cas/CasAPI.h> 21 #include <media/cas/DescramblerAPI.h> 22 #include <openssl/aes.h> 23 #include <utils/KeyedVector.h> 24 #include <utils/Mutex.h> 25 26 namespace android { 27 struct ABuffer; 28 29 namespace clearkeycas { 30 class KeyFetcher; 31 32 class ClearKeyCasSession { 33 public: 34 explicit ClearKeyCasSession(CasPlugin *plugin); 35 36 virtual ~ClearKeyCasSession(); 37 38 ssize_t decrypt( 39 bool secure, 40 DescramblerPlugin::ScramblingControl scramblingControl, 41 size_t numSubSamples, 42 const DescramblerPlugin::SubSample *subSamples, 43 const void *srcPtr, 44 void *dstPtr, 45 AString * /* errorDetailMsg */); 46 47 status_t updateECM(KeyFetcher *keyFetcher, void *ecm, size_t size); 48 49 private: 50 enum { 51 kNumKeys = 2, 52 }; 53 struct KeyInfo { 54 bool valid; 55 AES_KEY contentKey; 56 }; 57 sp<ABuffer> mEcmBuffer; 58 Mutex mKeyLock; 59 CasPlugin* mPlugin; 60 KeyInfo mKeyInfo[kNumKeys]; 61 62 friend class ClearKeySessionLibrary; 63 getPlugin()64 CasPlugin* getPlugin() const { return mPlugin; } 65 status_t decryptPayload( 66 const AES_KEY& key, size_t length, size_t offset, char* buffer) const; 67 68 DISALLOW_EVIL_CONSTRUCTORS(ClearKeyCasSession); 69 }; 70 71 class ClearKeySessionLibrary { 72 public: 73 static ClearKeySessionLibrary* get(); 74 75 status_t addSession(CasPlugin *plugin, CasSessionId *sessionId); 76 77 std::shared_ptr<ClearKeyCasSession> findSession(const CasSessionId& sessionId); 78 79 void destroySession(const CasSessionId& sessionId); 80 81 void destroyPlugin(CasPlugin *plugin); 82 83 private: 84 static Mutex sSingletonLock; 85 static ClearKeySessionLibrary* sSingleton; 86 87 Mutex mSessionsLock; 88 uint32_t mNextSessionId; 89 KeyedVector<CasSessionId, std::shared_ptr<ClearKeyCasSession>> mIDToSessionMap; 90 91 ClearKeySessionLibrary(); 92 DISALLOW_EVIL_CONSTRUCTORS(ClearKeySessionLibrary); 93 }; 94 95 } // namespace clearkeycas 96 } // namespace android 97 98 #endif // CLEARKEY_SESSION_LIBRARY_H_ 99