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