• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_1 {
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,std::vector<uint8_t> * keyRequest) const42 Status Session::getKeyRequest(
43         const std::vector<uint8_t>& initData,
44         const std::string& mimeType,
45         std::vector<uint8_t>* keyRequest) const {
46     InitDataParser parser;
47     return parser.parse(initData, mimeType, keyRequest);
48 }
49 
provideKeyResponse(const std::vector<uint8_t> & response)50 Status Session::provideKeyResponse(const std::vector<uint8_t>& response) {
51     std::string responseString(
52             reinterpret_cast<const char*>(response.data()), response.size());
53     KeyMap keys;
54 
55     Mutex::Autolock lock(mMapLock);
56     JsonWebKey parser;
57     if (parser.extractKeysFromJsonWebKeySet(responseString, &keys)) {
58         for (auto &key : keys) {
59             std::string first(key.first.begin(), key.first.end());
60             std::string second(key.second.begin(), key.second.end());
61             mKeyMap.insert(std::pair<std::vector<uint8_t>,
62                     std::vector<uint8_t> >(key.first, key.second));
63         }
64         return Status::OK;
65     } else {
66         return Status::ERROR_DRM_UNKNOWN;
67     }
68 }
69 
decrypt(const KeyId keyId,const Iv iv,const uint8_t * srcPtr,uint8_t * destPtr,const std::vector<SubSample> subSamples,size_t * bytesDecryptedOut)70 Status Session::decrypt(
71         const KeyId keyId, const Iv iv, const uint8_t* srcPtr,
72         uint8_t* destPtr, const std::vector<SubSample> subSamples,
73         size_t* bytesDecryptedOut) {
74     Mutex::Autolock lock(mMapLock);
75 
76     std::vector<uint8_t> keyIdVector;
77     keyIdVector.clear();
78     keyIdVector.insert(keyIdVector.end(), keyId, keyId + kBlockSize);
79     std::map<std::vector<uint8_t>, std::vector<uint8_t> >::iterator itr;
80     itr = mKeyMap.find(keyIdVector);
81     if (itr == mKeyMap.end()) {
82         return Status::ERROR_DRM_NO_LICENSE;
83     }
84 
85     AesCtrDecryptor decryptor;
86     return decryptor.decrypt(
87             itr->second /*key*/, iv, srcPtr, destPtr, subSamples,
88             subSamples.size(), bytesDecryptedOut);
89 }
90 
91 } // namespace clearkey
92 } // namespace V1_1
93 } // namespace drm
94 } // namespace hardware
95 } // namespace android
96