• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "pinauth_controller.h"
17 #include <openssl/evp.h>
18 #include <openssl/rand.h>
19 #include "ipc_skeleton.h"
20 #include "iservice_registry.h"
21 #include "parameter.h"
22 #include "coauth_info_define.h"
23 #include "pinauth_defines.h"
24 #include "pinauth_log_wrapper.h"
25 
26 namespace OHOS {
27 namespace UserIAM {
28 namespace PinAuth {
PinAuthController()29 PinAuthController::PinAuthController()
30 {
31     attributes_ = nullptr;
32     pin_ = nullptr;
33 }
34 
~PinAuthController()35 PinAuthController::~PinAuthController()
36 {
37     attributes_ = nullptr;
38     pin_ = nullptr;
39 }
40 
OnStart(std::vector<uint8_t> & salt)41 bool PinAuthController::OnStart(std::vector<uint8_t> &salt)
42 {
43     PINAUTH_HILOGI(MODULE_SERVICE, "PinAuthController::OnStart start");
44     int32_t ret = attributes_->GetUint32Value(AUTH_SCHEDULE_MODE, command_);
45     if (ret != SUCCESS) {
46         PINAUTH_HILOGE(MODULE_SERVICE, "PinAuthController::OnStart GetUint32Value AUTH_SCHEDULE_MODE error");
47         return false;
48     }
49     if (command_ == COMMAND_ENROLL_PIN) {
50         NewSalt(salt);
51         PINAUTH_HILOGI(MODULE_COMMON, "PinAuthController::OnStart NewSalt finish");
52     } else if (command_ == COMMAND_AUTH_PIN) {
53         ret = attributes_->GetUint64Value(AUTH_TEMPLATE_ID, templateId_);
54         if (ret != SUCCESS) {
55             PINAUTH_HILOGE(MODULE_SERVICE, "PinAuthController::OnStart GetUint64Value AUTH_TEMPLATE_ID error");
56             return false;
57         }
58         ret = pin_->GetSalt(templateId_, salt);
59         if (ret != SUCCESS) {
60             PINAUTH_HILOGE(MODULE_SERVICE, "PinAuthController::OnStart GetSalt error");
61             return false;
62         }
63         PINAUTH_HILOGI(MODULE_COMMON, "PinAuthController::OnStart GetSalt finish");
64     } else {
65         PINAUTH_HILOGE(MODULE_COMMON, "PinAuthController::OnStart command unknown %{public}u", command_);
66         return false;
67     }
68     salt_ = salt;
69     return true;
70 }
71 
OnSetData(int32_t authSubType,std::vector<uint8_t> data)72 void PinAuthController::OnSetData(int32_t authSubType, std::vector<uint8_t> data)
73 {
74     PINAUTH_HILOGI(MODULE_SERVICE, "PinAuthController::OnSetData start");
75     std::lock_guard<std::mutex> guard(mutex_);
76     if (canceled) {
77         PINAUTH_HILOGI(MODULE_SERVICE, "PinAuthController::onSetData event has canceled");
78         return;
79     }
80 
81     PINAUTH_HILOGI(MODULE_SERVICE, "PinAuthController::onSetData data size is : [%{public}zu]", data.size());
82     int32_t ret = SUCCESS;
83     if (data.size() == 0) {
84         PINAUTH_HILOGE(MODULE_SERVICE, "PinAuthController::onSetData data is null");
85         ret = FAIL;
86     }
87 
88     auto finalResult = std::make_shared<AuthResPool::AuthAttributes>();
89     std::vector<uint8_t> result;
90     if (ret == SUCCESS) {
91         if (command_ == COMMAND_ENROLL_PIN) {
92             PINAUTH_HILOGI(MODULE_SERVICE, "PinAuthController::onSetData command == COMMAND_ENROLL_PIN");
93             ret = pin_->EnrollPin(scheduleId_, static_cast<uint64_t>(authSubType), salt_, data, result);
94             PINAUTH_HILOGI(MODULE_COMMON, "EnrollPin finish %{public}d", ret);
95         } else if (command_ == COMMAND_AUTH_PIN) {
96             PINAUTH_HILOGI(MODULE_SERVICE, "PinAuthController::onSetData command == COMMAND_AUTH_PIN");
97             ret = pin_->AuthPin(scheduleId_, templateId_, data, result);
98             PINAUTH_HILOGI(MODULE_COMMON, "AuthPin finish %{public}d", ret);
99         }
100     }
101 
102     PINAUTH_HILOGI(MODULE_COMMON, "PinAuthController::onSetData finalResult is unpack");
103     finalResult->SetUint8ArrayValue(AUTH_RESULT, result);
104     if (messenger_ != nullptr) {
105         int32_t sendRet = messenger_->Finish(scheduleId_, PIN, ret, finalResult);
106         if (sendRet != SUCCESS) {
107             PINAUTH_HILOGE(MODULE_SERVICE, "PinAuthController::onSetData call finish failed");
108         }
109     } else {
110         PINAUTH_HILOGE(MODULE_COMMON, "PinAuthController::onSetData messenger_ is null");
111     }
112 
113     PINAUTH_HILOGI(MODULE_SERVICE, "PinAuthController::OnSetData end");
114 }
115 
SaveParam(uint64_t scheduleId,std::shared_ptr<PinAuth> pin,std::shared_ptr<AuthResPool::AuthAttributes> attributes)116 void PinAuthController::SaveParam(uint64_t scheduleId, std::shared_ptr<PinAuth> pin,
117     std::shared_ptr<AuthResPool::AuthAttributes> attributes)
118 {
119     std::lock_guard<std::mutex> guard(mutex_);
120     PINAUTH_HILOGI(MODULE_SERVICE, "PinAuthController::SaveParam start");
121     scheduleId_ = scheduleId;
122     pin_ = pin;
123     attributes_ = attributes;
124 }
125 
SetMessenger(const sptr<AuthResPool::IExecutorMessenger> & messenger)126 void PinAuthController::SetMessenger(const sptr<AuthResPool::IExecutorMessenger> &messenger)
127 {
128     PINAUTH_HILOGI(MODULE_SERVICE, "PinAuthController::SetMessenger start");
129     std::lock_guard<std::mutex> guard(mutex_);
130     messenger_ = messenger;
131 }
132 
Cancel()133 void PinAuthController::Cancel()
134 {
135     PINAUTH_HILOGI(MODULE_SERVICE, "PinAuthController::Cancel start");
136     std::lock_guard<std::mutex> guard(mutex_);
137     canceled = true;
138 }
139 
NewSalt(std::vector<uint8_t> & saltV)140 void NewSalt(std::vector<uint8_t> &saltV)
141 {
142     char localDeviceId[DEVICE_UUID_LENGTH] = {0};
143     GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
144     unsigned char random[RANDOM_LENGTH] = {0};
145     RAND_bytes(random, (int)RANDOM_LENGTH);
146     std::vector<uint8_t> sum;
147     for (uint32_t i = 0; i < DEVICE_UUID_LENGTH; i++) {
148         sum.push_back(localDeviceId[i]);
149     }
150     for (uint32_t i = 0; i < RANDOM_LENGTH; i++) {
151         sum.push_back(random[i]);
152     }
153     const EVP_MD *alg = EVP_sha256();
154     PINAUTH_HILOGI(MODULE_SERVICE, "PinAuthController::NewSalt EVP_sha256 success");
155     uint32_t size;
156     uint8_t result[SHA256_LENGTH] = {0};
157     EVP_Digest(sum.data(), sum.size(), result, &size, alg, NULL);
158     for (uint32_t i = 0; i < size; i++) {
159         saltV.push_back(result[i]);
160     }
161     PINAUTH_HILOGI(MODULE_SERVICE, "PinAuthController::NewSalt result size is : [%{public}u]", size);
162 }
163 } // namespace PinAuth
164 } // namespace UserIAM
165 } // namespace OHOS
166