tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ClearKeyDecryptionManager.h (3285B)


      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 __ClearKeyDecryptionManager_h__
     18 #define __ClearKeyDecryptionManager_h__
     19 
     20 // This include is required in order for content_decryption_module to work
     21 // on Unix systems.
     22 #include <stddef.h>
     23 
     24 #include <map>
     25 
     26 #include "ClearKeyUtils.h"
     27 #include "RefCounted.h"
     28 #include "content_decryption_module.h"
     29 
     30 class ClearKeyDecryptor;
     31 
     32 class CryptoMetaData {
     33 public:
     34  CryptoMetaData() = default;
     35 
     36  explicit CryptoMetaData(const cdm::InputBuffer_2* aInputBuffer) {
     37    Init(aInputBuffer);
     38  }
     39 
     40  void Init(const cdm::InputBuffer_2* aInputBuffer) {
     41    if (!aInputBuffer) {
     42      assert(!IsValid());
     43      return;
     44    }
     45 
     46    mEncryptionScheme = aInputBuffer->encryption_scheme;
     47    Assign(mKeyId, aInputBuffer->key_id, aInputBuffer->key_id_size);
     48    Assign(mIV, aInputBuffer->iv, aInputBuffer->iv_size);
     49    mCryptByteBlock = aInputBuffer->pattern.crypt_byte_block;
     50    mSkipByteBlock = aInputBuffer->pattern.skip_byte_block;
     51 
     52    for (uint32_t i = 0; i < aInputBuffer->num_subsamples; ++i) {
     53      const cdm::SubsampleEntry& subsample = aInputBuffer->subsamples[i];
     54      mClearBytes.push_back(subsample.clear_bytes);
     55      mCipherBytes.push_back(subsample.cipher_bytes);
     56    }
     57  }
     58 
     59  bool IsValid() const {
     60    return !mKeyId.empty() && !mIV.empty() && !mCipherBytes.empty() &&
     61           !mClearBytes.empty();
     62  }
     63 
     64  size_t NumSubsamples() const {
     65    assert(mClearBytes.size() == mCipherBytes.size());
     66    return mClearBytes.size();
     67  }
     68 
     69  cdm::EncryptionScheme mEncryptionScheme;
     70  std::vector<uint8_t> mKeyId;
     71  std::vector<uint8_t> mIV;
     72  uint32_t mCryptByteBlock;
     73  uint32_t mSkipByteBlock;
     74  std::vector<uint32_t> mClearBytes;
     75  std::vector<uint32_t> mCipherBytes;
     76 };
     77 
     78 class ClearKeyDecryptionManager : public RefCounted {
     79 private:
     80  ClearKeyDecryptionManager();
     81  ~ClearKeyDecryptionManager();
     82 
     83  static ClearKeyDecryptionManager* sInstance;
     84 
     85 public:
     86  static ClearKeyDecryptionManager* Get();
     87 
     88  bool HasSeenKeyId(const KeyId& aKeyId) const;
     89  bool HasKeyForKeyId(const KeyId& aKeyId) const;
     90 
     91  const Key& GetDecryptionKey(const KeyId& aKeyId);
     92 
     93  // Create a decryptor for the given KeyId if one does not already exist.
     94  void InitKey(KeyId aKeyId, Key aKey);
     95  void ExpectKeyId(KeyId aKeyId);
     96  void ReleaseKeyId(KeyId aKeyId);
     97 
     98  // Decrypts buffer *in place*.
     99  cdm::Status Decrypt(uint8_t* aBuffer, uint32_t aBufferSize,
    100                      const CryptoMetaData& aMetadata);
    101  cdm::Status Decrypt(std::vector<uint8_t>& aBuffer,
    102                      const CryptoMetaData& aMetadata);
    103 
    104 private:
    105  bool IsExpectingKeyForKeyId(const KeyId& aKeyId) const;
    106 
    107  std::map<KeyId, ClearKeyDecryptor*> mDecryptors;
    108 };
    109 
    110 #endif  // __ClearKeyDecryptionManager_h__