summaryrefslogtreecommitdiff
path: root/KM300/authsecret/AuthSecret.cpp
blob: f6f5d0a80f5355712e1dd4c0a5e3009c0ec991c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/******************************************************************************
 **
 ** The original Work has been changed by NXP.
 **
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
 ** You may obtain a copy of the License at
 **
 ** http://www.apache.org/licenses/LICENSE-2.0
 **
 ** Unless required by applicable law or agreed to in writing, software
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 **
 ** Copyright 2022-2023 NXP
 **
 *********************************************************************************/

#define LOG_TAG "AuthSecret-Hal"
#include "AuthSecret.h"
#include "AuthSecretHelper.h"

using keymint::javacard::OmapiTransport;

const std::vector<uint8_t> gAuthSecretAppletAID = {0xA0, 0x00, 0x00, 0x03, 0x96,
                                                   0x54, 0x53, 0x00, 0x00, 0x00,
                                                   0x01, 0x00, 0x52};

static OmapiTransport *gTransport = new OmapiTransport(gAuthSecretAppletAID);
static AuthSecretHelper *gAuthSecretImplInstance = AuthSecretHelper::getInstance();

namespace aidl {
namespace android {
namespace hardware {
namespace authsecret {

static void authSecretTimerExpiryFunc(union sigval arg) {
  LOG(INFO) << StringPrintf(
      "%s: Enter. Clearing AuthSecret Approved Status !!!", __func__);
  AuthSecret *obj = (AuthSecret *)arg.sival_ptr;
  if (obj != nullptr)
    obj->clearAuthApprovedStatus();
}

void AuthSecret::clearAuthApprovedStatus() {
  LOG(INFO) << StringPrintf("%s: Enter", __func__);
  std::vector<uint8_t> cmd;
  std::vector<uint8_t> timeout;
  std::vector<uint8_t> input;
  bool status = gAuthSecretImplInstance->constructApdu(
      Instruction::INS_CLEAR_APPROVED_STATUS, input, cmd, std::move(timeout));
  if (!status) {
    LOG(ERROR) << StringPrintf("%s: constructApdu failed", __func__);
    return;
  }

  std::vector<uint8_t> resp;
  uint8_t retry = 0;
  do {
    if (!gTransport->sendData(cmd, resp)) {
      LOG(ERROR) << StringPrintf("%s: Error in sending data in sendData.",
                                 __func__);
    } else {
      if ((resp.size() < 2) || (getApduStatus(resp) != APDU_RESP_STATUS_OK)) {
        LOG(ERROR) << StringPrintf("%s: failed", __func__);
      } else { break; }
    }
    usleep(1 * ONE_SEC);
  } while (++retry < MAX_RETRY_COUNT);


  LOG(INFO) << StringPrintf("%s: Exit", __func__);
}

// Methods from ::android::hardware::authsecret::IAuthSecret follow.
::ndk::ScopedAStatus
AuthSecret::setPrimaryUserCredential(const std::vector<uint8_t> &in_secret) {
  LOG(INFO) << StringPrintf("%s: Enter", __func__);
  std::vector<uint8_t> cmd;
  std::vector<uint8_t> timeout;
  bool status = gAuthSecretImplInstance->constructApdu(
      Instruction::INS_VERIFY_PIN, in_secret, cmd, std::move(timeout));
  if (!status) {
    LOG(ERROR) << StringPrintf("%s: constructApdu failed", __func__);
    return ::ndk::ScopedAStatus::ok();
  }

  mAuthClearTimer.kill();

  clearAuthApprovedStatus();

  std::vector<uint8_t> resp;
  uint8_t retry = 0;
  do {
    if (!gTransport->sendData(cmd, resp)) {
      LOG(ERROR) << StringPrintf("%s: Error in sending data in sendData.",
                                 __func__);
    } else {
      break;
    }
  } while (++retry < MAX_RETRY_COUNT);

  if ((resp.size() < 2) || (getApduStatus(resp) != APDU_RESP_STATUS_OK) ||
      !gAuthSecretImplInstance->checkVerifyStatus(resp)) {
    clearAuthApprovedStatus();
    return ::ndk::ScopedAStatus::ok();
  }

  uint64_t clearAuthTimeout =
      gAuthSecretImplInstance->extractTimeoutValue(std::move(resp));
  LOG(INFO) << StringPrintf("%s: AuthSecret Clear status Timeout = %ld secs",
                            __func__, clearAuthTimeout);
  if (clearAuthTimeout) {
    if (!mAuthClearTimer.set(clearAuthTimeout * 1000, this,
                             authSecretTimerExpiryFunc)) {
      LOG(ERROR) << StringPrintf("%s: Set Timer Failed !!!", __func__);
      clearAuthApprovedStatus();
    }
  }
  gTransport->closeConnection();
  LOG(INFO) << StringPrintf("%s: Exit", __func__);
  return ::ndk::ScopedAStatus::ok();
}

} // namespace authsecret
} // namespace hardware
} // namespace android
} // aidl