1 /*
2 * Copyright (C) 2021 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 #define LOG_TAG "clearkey-Session"
17
18 #include <utils/Log.h>
19
20 #include "Session.h"
21
22 #include "AesCtrDecryptor.h"
23 #include "InitDataParser.h"
24 #include "JsonWebKey.h"
25
26 namespace clearkeydrm {
27
28 using ::android::Mutex;
29 using ::android::sp;
30
getKeyRequest(const std::vector<uint8_t> & initData,const std::string & mimeType,CdmKeyType keyType,std::vector<uint8_t> * keyRequest) const31 CdmResponseType Session::getKeyRequest(const std::vector<uint8_t>& initData,
32 const std::string& mimeType,
33 CdmKeyType keyType,
34 std::vector<uint8_t>* keyRequest) const {
35 InitDataParser parser;
36 return parser.parse(initData, mimeType, keyType, keyRequest);
37 }
38
provideKeyResponse(const std::vector<uint8_t> & response)39 CdmResponseType Session::provideKeyResponse(const std::vector<uint8_t>& response) {
40 std::string responseString(reinterpret_cast<const char*>(response.data()), response.size());
41 KeyMap keys;
42
43 Mutex::Autolock lock(mMapLock);
44 JsonWebKey parser;
45 if (parser.extractKeysFromJsonWebKeySet(responseString, &keys)) {
46 for (auto& key : keys) {
47 std::string first(key.first.begin(), key.first.end());
48 std::string second(key.second.begin(), key.second.end());
49 mKeyMap.insert(
50 std::pair<std::vector<uint8_t>, std::vector<uint8_t>>(key.first, key.second));
51 }
52 return clearkeydrm::OK;
53 } else {
54 return clearkeydrm::ERROR_UNKNOWN;
55 }
56 }
57
decrypt(const KeyId keyId,const Iv iv,const uint8_t * srcPtr,uint8_t * destPtr,const std::vector<int32_t> & clearDataLengths,const std::vector<int32_t> & encryptedDataLengths,size_t * bytesDecryptedOut)58 CdmResponseType Session::decrypt(const KeyId keyId, const Iv iv,
59 const uint8_t* srcPtr, uint8_t* destPtr,
60 const std::vector<int32_t>& clearDataLengths,
61 const std::vector<int32_t>& encryptedDataLengths,
62 size_t* bytesDecryptedOut) {
63 Mutex::Autolock lock(mMapLock);
64
65 if (getMockError() != clearkeydrm::OK) {
66 return getMockError();
67 }
68
69 std::vector<uint8_t> keyIdVector;
70 keyIdVector.clear();
71 keyIdVector.insert(keyIdVector.end(), keyId, keyId + kBlockSize);
72 std::map<std::vector<uint8_t>, std::vector<uint8_t>>::iterator itr;
73 itr = mKeyMap.find(keyIdVector);
74 if (itr == mKeyMap.end()) {
75 return clearkeydrm::ERROR_NO_LICENSE;
76 }
77
78 clearkeydrm::AesCtrDecryptor decryptor;
79 auto status = decryptor.decrypt(itr->second /*key*/, iv, srcPtr, destPtr,
80 clearDataLengths,
81 encryptedDataLengths,
82 bytesDecryptedOut);
83 return status;
84 }
85
86 } // namespace clearkeydrm
87