1 /*
2 * Copyright (C) 2018 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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "hidl_ClearKeySession"
19 #include <utils/Log.h>
20
21 #include "Session.h"
22 #include "Utils.h"
23
24 #include "AesCtrDecryptor.h"
25 #include "InitDataParser.h"
26 #include "JsonWebKey.h"
27
28 namespace android {
29 namespace hardware {
30 namespace drm {
31 namespace V1_2 {
32 namespace clearkey {
33
34 using ::android::hardware::drm::V1_0::KeyValue;
35 using ::android::hardware::drm::V1_0::Status;
36 using ::android::hardware::drm::V1_0::SubSample;
37 using ::android::hardware::Return;
38 using ::android::sp;
39
40 using android::Mutex;
41
getKeyRequest(const std::vector<uint8_t> & initData,const std::string & mimeType,V1_0::KeyType keyType,std::vector<uint8_t> * keyRequest) const42 Status Session::getKeyRequest(
43 const std::vector<uint8_t>& initData,
44 const std::string& mimeType,
45 V1_0::KeyType keyType,
46 std::vector<uint8_t>* keyRequest) const {
47 InitDataParser parser;
48 return parser.parse(initData, mimeType, keyType, keyRequest);
49 }
50
provideKeyResponse(const std::vector<uint8_t> & response)51 Status Session::provideKeyResponse(const std::vector<uint8_t>& response) {
52 std::string responseString(
53 reinterpret_cast<const char*>(response.data()), response.size());
54 KeyMap keys;
55
56 Mutex::Autolock lock(mMapLock);
57 JsonWebKey parser;
58 if (parser.extractKeysFromJsonWebKeySet(responseString, &keys)) {
59 for (auto &key : keys) {
60 std::string first(key.first.begin(), key.first.end());
61 std::string second(key.second.begin(), key.second.end());
62 mKeyMap.insert(std::pair<std::vector<uint8_t>,
63 std::vector<uint8_t> >(key.first, key.second));
64 }
65 return Status::OK;
66 } else {
67 return Status::ERROR_DRM_UNKNOWN;
68 }
69 }
70
decrypt(const KeyId keyId,const Iv iv,const uint8_t * srcPtr,uint8_t * destPtr,const std::vector<SubSample> subSamples,size_t * bytesDecryptedOut)71 Status_V1_2 Session::decrypt(
72 const KeyId keyId, const Iv iv, const uint8_t* srcPtr,
73 uint8_t* destPtr, const std::vector<SubSample> subSamples,
74 size_t* bytesDecryptedOut) {
75 Mutex::Autolock lock(mMapLock);
76
77 if (getMockError() != Status_V1_2::OK) {
78 return getMockError();
79 }
80
81 std::vector<uint8_t> keyIdVector;
82 keyIdVector.clear();
83 keyIdVector.insert(keyIdVector.end(), keyId, keyId + kBlockSize);
84 std::map<std::vector<uint8_t>, std::vector<uint8_t> >::iterator itr;
85 itr = mKeyMap.find(keyIdVector);
86 if (itr == mKeyMap.end()) {
87 return Status_V1_2::ERROR_DRM_NO_LICENSE;
88 }
89
90 AesCtrDecryptor decryptor;
91 Status status = decryptor.decrypt(
92 itr->second /*key*/, iv, srcPtr, destPtr, subSamples,
93 subSamples.size(), bytesDecryptedOut);
94 return static_cast<Status_V1_2>(status);
95 }
96
97 } // namespace clearkey
98 } // namespace V1_2
99 } // namespace drm
100 } // namespace hardware
101 } // namespace android
102