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