ClearKeyUtils.h (3594B)
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 __ClearKeyUtils_h__ 18 #define __ClearKeyUtils_h__ 19 20 #include <assert.h> 21 // stdef.h is required for content_decryption_module to work on Unix systems. 22 #include <stddef.h> 23 #include <stdint.h> 24 25 #include <string> 26 #include <vector> 27 28 #include "content_decryption_module.h" 29 #include "mozilla/Span.h" 30 #include "pk11pub.h" 31 32 #if 0 33 void CK_Log(const char* aFmt, ...); 34 # define CK_LOGE(...) CK_Log(__VA_ARGS__) 35 # define CK_LOGD(...) CK_Log(__VA_ARGS__) 36 # define CK_LOGW(...) CK_Log(__VA_ARGS__) 37 # define CK_LOGARRAY(APREPEND, ADATA, ADATA_SIZE) \ 38 CK_LogArray(APREPEND, ADATA, ADATA_SIZE) 39 #else 40 // Note: Enabling logging slows things down a LOT, especially when logging to 41 // a file. 42 # define CK_LOGE(...) 43 # define CK_LOGD(...) 44 # define CK_LOGW(...) 45 # define CK_LOGARRAY(APREPEND, ADATA, ADATA_SIZE) 46 #endif 47 48 typedef std::vector<uint8_t> KeyId; 49 typedef std::vector<uint8_t> Key; 50 51 // The session response size should be within a reasonable limit. 52 // The size 64 KB is referenced from web-platform-test. 53 static const uint32_t kMaxSessionResponseLength = 65536; 54 55 // Provide limitation for KeyIds length and webm initData size. 56 static const uint32_t kMaxWebmInitDataSize = 65536; 57 static const uint32_t kMaxKeyIdsLength = 512; 58 59 void CK_LogArray(const char* aPrepend, const uint8_t* aData, 60 const uint32_t aDataSize); 61 62 struct KeyIdPair { 63 KeyId mKeyId; 64 Key mKey; 65 }; 66 67 class ClearKeyUtils { 68 public: 69 static bool DecryptCbcs(const std::vector<uint8_t>& aKey, 70 const std::vector<uint8_t>& aIV, 71 mozilla::Span<uint8_t> aSubsampleEncryptedRange, 72 uint32_t aCryptByteBlocks, uint32_t aSkipByteBlocks); 73 74 static bool DecryptAES(const std::vector<uint8_t>& aKey, 75 std::vector<uint8_t>& aData, 76 std::vector<uint8_t>& aIV); 77 78 static bool ParseKeyIdsInitData(const uint8_t* aInitData, 79 uint32_t aInitDataSize, 80 std::vector<KeyId>& aOutKeyIds); 81 82 static void MakeKeyRequest(const std::vector<KeyId>& aKeyIds, 83 std::string& aOutRequest, 84 cdm::SessionType aSessionType); 85 86 static bool ParseJWK(const uint8_t* aKeyData, uint32_t aKeyDataSize, 87 std::vector<KeyIdPair>& aOutKeys, 88 cdm::SessionType aSessionType); 89 static const char* SessionTypeToString(cdm::SessionType aSessionType); 90 91 static bool IsValidSessionId(const char* aBuff, uint32_t aLength); 92 93 static std::string ToHexString(const uint8_t* aBytes, uint32_t aLength); 94 }; 95 96 template <class Container, class Element> 97 inline bool Contains(const Container& aContainer, const Element& aElement) { 98 return aContainer.find(aElement) != aContainer.end(); 99 } 100 101 template <typename T> 102 inline void Assign(std::vector<T>& aVec, const T* aData, size_t aLength) { 103 aVec.assign(aData, aData + aLength); 104 } 105 106 #endif // __ClearKeyUtils_h__