blob: 0925a2188f879bdc87a9d641f5d4a29297493614 [file] [log] [blame]
Steven Moreland6fe69542022-11-03 17:42:32 +00001/*
2 * Copyright (C) 2022 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#include <aidl/Gtest.h>
18#include <aidl/Vintf.h>
19#include <aidl/android/hardware/secure_element/BnSecureElementCallback.h>
20#include <aidl/android/hardware/secure_element/ISecureElement.h>
Henri Chataingcf06db32023-01-06 09:20:05 +000021#include <android-base/logging.h>
Steven Moreland6fe69542022-11-03 17:42:32 +000022#include <android/binder_manager.h>
23#include <android/binder_process.h>
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
26
27#include <chrono>
28#include <condition_variable>
29#include <mutex>
30
31using namespace std::chrono_literals;
32
33using aidl::android::hardware::secure_element::BnSecureElementCallback;
34using aidl::android::hardware::secure_element::ISecureElement;
35using aidl::android::hardware::secure_element::LogicalChannelResponse;
36using ndk::ScopedAStatus;
37using ndk::SharedRefBase;
38using ndk::SpAIBinder;
39using testing::ElementsAre;
40using testing::ElementsAreArray;
41
42#define EXPECT_OK(status) \
43 do { \
44 auto status_impl = (status); \
45 EXPECT_TRUE(status_impl.isOk()) << status_impl.getDescription(); \
46 } while (false)
47
Henri Chataingcf06db32023-01-06 09:20:05 +000048#define EXPECT_ERR(status) \
49 do { \
50 auto status_impl = (status); \
51 EXPECT_FALSE(status_impl.isOk()) << status_impl.getDescription(); \
52 } while (false)
53
54// APDU defined in CTS tests.
55// The applet selected with kSelectableAid will return 256 bytes of data
56// in response.
57static const std::vector<uint8_t> kDataApdu = {
58 0x00, 0x08, 0x00, 0x00, 0x00,
59};
60
61// Selectable test AID defined in CTS tests.
62static const std::vector<uint8_t> kSelectableAid = {
63 0xA0, 0x00, 0x00, 0x04, 0x76, 0x41, 0x6E, 0x64,
64 0x72, 0x6F, 0x69, 0x64, 0x43, 0x54, 0x53, 0x31,
65};
66// Non-selectable test AID defined in CTS tests.
67static const std::vector<uint8_t> kNonSelectableAid = {
68 0xA0, 0x00, 0x00, 0x04, 0x76, 0x41, 0x6E, 0x64,
69 0x72, 0x6F, 0x69, 0x64, 0x43, 0x54, 0x53, 0xFF,
70};
Steven Moreland6fe69542022-11-03 17:42:32 +000071
72class MySecureElementCallback : public BnSecureElementCallback {
73 public:
74 ScopedAStatus onStateChange(bool state, const std::string& debugReason) override {
75 {
76 std::unique_lock<std::mutex> l(m);
77 (void)debugReason;
78 history.push_back(state);
79 }
80 cv.notify_one();
81 return ScopedAStatus::ok();
82 };
83
84 void expectCallbackHistory(std::vector<bool>&& want) {
85 std::unique_lock<std::mutex> l(m);
86 cv.wait_for(l, 2s, [&]() { return history.size() >= want.size(); });
87 EXPECT_THAT(history, ElementsAreArray(want));
88 }
89
90 private:
91 std::mutex m; // guards history
92 std::condition_variable cv;
93 std::vector<bool> history;
94};
95
96class SecureElementAidl : public ::testing::TestWithParam<std::string> {
97 public:
Henri Chataingcf06db32023-01-06 09:20:05 +000098 void SetUp() override {
Steven Moreland6fe69542022-11-03 17:42:32 +000099 SpAIBinder binder = SpAIBinder(AServiceManager_waitForService(GetParam().c_str()));
Steven Moreland6fe69542022-11-03 17:42:32 +0000100
Henri Chataingcf06db32023-01-06 09:20:05 +0000101 secure_element_ = ISecureElement::fromBinder(binder);
102 ASSERT_NE(secure_element_, nullptr);
Steven Moreland6fe69542022-11-03 17:42:32 +0000103
Henri Chataingcf06db32023-01-06 09:20:05 +0000104 secure_element_callback_ = SharedRefBase::make<MySecureElementCallback>();
105 ASSERT_NE(secure_element_callback_, nullptr);
106
107 EXPECT_OK(secure_element_->init(secure_element_callback_));
108 secure_element_callback_->expectCallbackHistory({true});
Henri Chataing9ba974d2023-01-31 18:22:57 +0000109
110 // Check if the basic channel is supported by the bound SE.
111 std::vector<uint8_t> basic_channel_response;
112 auto status =
113 secure_element_->openBasicChannel(kSelectableAid, 0x00, &basic_channel_response);
114 if (status.isOk()) {
115 basic_channel_supported_ = true;
116 secure_element_->closeChannel(0);
117 }
Steven Moreland6fe69542022-11-03 17:42:32 +0000118 }
119
Henri Chataingcf06db32023-01-06 09:20:05 +0000120 void TearDown() override {
Henri Chataing4b780eb2023-01-31 18:22:57 +0000121 EXPECT_OK(secure_element_->reset());
Henri Chataingcf06db32023-01-06 09:20:05 +0000122 secure_element_ = nullptr;
123 secure_element_callback_ = nullptr;
124 }
125
126 // Call transmit with kDataApdu and the selected channel number.
127 // Return the response sstatus code.
128 uint16_t transmit(uint8_t channel_number) {
129 std::vector<uint8_t> apdu = kDataApdu;
130 std::vector<uint8_t> response;
131
132 // Edit the channel number into the CLA header byte.
133 if (channel_number < 4) {
134 apdu[0] |= channel_number;
135 } else {
136 apdu[0] |= (channel_number - 4) | 0x40;
137 }
138
Henri Chataing27b30c62023-02-27 17:28:12 +0000139 // transmit() will return an empty response with the error
140 // code CHANNEL_NOT_AVAILABLE when the SE cannot be
141 // communicated with.
142 auto status = secure_element_->transmit(apdu, &response);
143 if (!status.isOk()) {
144 return 0x6881;
145 }
146
147 // transmit() will return a response containing at least
148 // the APDU response status otherwise.
Henri Chataingcf06db32023-01-06 09:20:05 +0000149 EXPECT_GE(response.size(), 2u);
Henri Chataing27b30c62023-02-27 17:28:12 +0000150 uint16_t apdu_status =
Henri Chataingcf06db32023-01-06 09:20:05 +0000151 (response[response.size() - 2] << 8) | (response[response.size() - 1] << 0);
152
153 // When the command is successful the response
154 // must contain 256 bytes of data.
Henri Chataing27b30c62023-02-27 17:28:12 +0000155 if (apdu_status == 0x9000) {
Henri Chataingcf06db32023-01-06 09:20:05 +0000156 EXPECT_EQ(response.size(), 258);
157 }
158
Henri Chataing27b30c62023-02-27 17:28:12 +0000159 return apdu_status;
Henri Chataingcf06db32023-01-06 09:20:05 +0000160 }
161
162 std::shared_ptr<ISecureElement> secure_element_;
163 std::shared_ptr<MySecureElementCallback> secure_element_callback_;
Henri Chataing9ba974d2023-01-31 18:22:57 +0000164 bool basic_channel_supported_{false};
Steven Moreland6fe69542022-11-03 17:42:32 +0000165};
166
Henri Chataingcf06db32023-01-06 09:20:05 +0000167TEST_P(SecureElementAidl, init) {
168 // init(nullptr) shall fail.
169 EXPECT_ERR(secure_element_->init(nullptr));
170
171 // init with a valid callback pointer shall succeed.
172 EXPECT_OK(secure_element_->init(secure_element_callback_));
173 secure_element_callback_->expectCallbackHistory({true, true});
174}
175
176TEST_P(SecureElementAidl, reset) {
177 std::vector<uint8_t> basic_channel_response;
178 LogicalChannelResponse logical_channel_response;
179
180 // reset called after init shall succeed.
Henri Chataing9ba974d2023-01-31 18:22:57 +0000181 if (basic_channel_supported_) {
182 EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &basic_channel_response));
183 }
Henri Chataingcf06db32023-01-06 09:20:05 +0000184 EXPECT_OK(secure_element_->openLogicalChannel(kSelectableAid, 0x00, &logical_channel_response));
185
186 EXPECT_OK(secure_element_->reset());
187 secure_element_callback_->expectCallbackHistory({true, false, true});
188
189 // All opened channels must be closed.
Henri Chataing9ba974d2023-01-31 18:22:57 +0000190 if (basic_channel_supported_) {
191 EXPECT_NE(transmit(0), 0x9000);
192 }
Henri Chataingcf06db32023-01-06 09:20:05 +0000193 EXPECT_NE(transmit(logical_channel_response.channelNumber), 0x9000);
194}
195
Steven Moreland6fe69542022-11-03 17:42:32 +0000196TEST_P(SecureElementAidl, isCardPresent) {
197 bool res = false;
Henri Chataingcf06db32023-01-06 09:20:05 +0000198
199 // isCardPresent called after init shall succeed.
200 EXPECT_OK(secure_element_->isCardPresent(&res));
Steven Moreland6fe69542022-11-03 17:42:32 +0000201 EXPECT_TRUE(res);
202}
203
Steven Moreland6fe69542022-11-03 17:42:32 +0000204TEST_P(SecureElementAidl, getAtr) {
205 std::vector<uint8_t> atr;
Henri Chataingcf06db32023-01-06 09:20:05 +0000206
207 // getAtr called after init shall succeed.
208 // The ATR has size between 0 and 32 bytes.
209 EXPECT_OK(secure_element_->getAtr(&atr));
Steven Moreland6fe69542022-11-03 17:42:32 +0000210 EXPECT_LE(atr.size(), 32u);
Steven Moreland6fe69542022-11-03 17:42:32 +0000211}
212
Henri Chataingcf06db32023-01-06 09:20:05 +0000213TEST_P(SecureElementAidl, openBasicChannel) {
214 std::vector<uint8_t> response;
215
Henri Chataing9ba974d2023-01-31 18:22:57 +0000216 if (!basic_channel_supported_) {
217 return;
218 }
219
Henri Chataingcf06db32023-01-06 09:20:05 +0000220 // openBasicChannel called with an invalid AID shall fail.
221 EXPECT_ERR(secure_element_->openBasicChannel(kNonSelectableAid, 0x00, &response));
222
223 // openBasicChannel called after init shall succeed.
224 // The response size must be larger than 2 bytes as it includes the
225 // status code.
226 EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &response));
227 EXPECT_GE(response.size(), 2u);
228
Henri Chataing9ba974d2023-01-31 18:22:57 +0000229 // transmit called on the basic channel should succeed.
Henri Chataingcf06db32023-01-06 09:20:05 +0000230 EXPECT_EQ(transmit(0), 0x9000);
231
232 // openBasicChannel called a second time shall fail.
233 // The basic channel can only be opened once.
234 EXPECT_ERR(secure_element_->openBasicChannel(kSelectableAid, 0x00, &response));
235
236 // openBasicChannel called after closing the basic channel shall succeed.
237 EXPECT_OK(secure_element_->closeChannel(0));
238 EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &response));
239}
240
241TEST_P(SecureElementAidl, openLogicalChannel) {
Steven Moreland6fe69542022-11-03 17:42:32 +0000242 LogicalChannelResponse response;
Henri Chataingcf06db32023-01-06 09:20:05 +0000243
244 // openLogicalChannel called with an invalid AID shall fail.
245 EXPECT_ERR(secure_element_->openLogicalChannel(kNonSelectableAid, 0x00, &response));
246
247 // openLogicalChannel called after init shall succeed.
248 // The response size must be larger than 2 bytes as it includes the
249 // status code. The channel number must be in the range 1-19.
250 EXPECT_OK(secure_element_->openLogicalChannel(kSelectableAid, 0x00, &response));
Steven Moreland6fe69542022-11-03 17:42:32 +0000251 EXPECT_GE(response.selectResponse.size(), 2u);
Henri Chataingcf06db32023-01-06 09:20:05 +0000252 EXPECT_GE(response.channelNumber, 1u);
253 EXPECT_LE(response.channelNumber, 19u);
254
Henri Chataing9ba974d2023-01-31 18:22:57 +0000255 // transmit called on the logical channel should succeed.
Henri Chataingcf06db32023-01-06 09:20:05 +0000256 EXPECT_EQ(transmit(response.channelNumber), 0x9000);
Steven Moreland6fe69542022-11-03 17:42:32 +0000257}
258
Henri Chataingcf06db32023-01-06 09:20:05 +0000259TEST_P(SecureElementAidl, closeChannel) {
260 std::vector<uint8_t> basic_channel_response;
261 LogicalChannelResponse logical_channel_response;
262
Henri Chataing4b780eb2023-01-31 18:22:57 +0000263 // closeChannel called on non-existing basic or logical channel
264 // shall fail.
265 EXPECT_ERR(secure_element_->closeChannel(0));
266 EXPECT_ERR(secure_element_->closeChannel(1));
Henri Chataingcf06db32023-01-06 09:20:05 +0000267
268 // closeChannel called on basic channel closes the basic channel.
Henri Chataing9ba974d2023-01-31 18:22:57 +0000269 if (basic_channel_supported_) {
270 EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &basic_channel_response));
271 EXPECT_OK(secure_element_->closeChannel(0));
Henri Chataingcf06db32023-01-06 09:20:05 +0000272
Henri Chataing9ba974d2023-01-31 18:22:57 +0000273 // transmit called on the basic channel should fail.
274 EXPECT_NE(transmit(0), 0x9000);
275 }
Henri Chataingcf06db32023-01-06 09:20:05 +0000276
277 // closeChannel called on logical channel closes the logical channel.
278 EXPECT_OK(secure_element_->openLogicalChannel(kSelectableAid, 0x00, &logical_channel_response));
279 EXPECT_OK(secure_element_->closeChannel(logical_channel_response.channelNumber));
280
Henri Chataing9ba974d2023-01-31 18:22:57 +0000281 // transmit called on the logical channel should fail.
Henri Chataingcf06db32023-01-06 09:20:05 +0000282 EXPECT_NE(transmit(logical_channel_response.channelNumber), 0x9000);
Steven Moreland6fe69542022-11-03 17:42:32 +0000283}
284
Henri Chataingcf06db32023-01-06 09:20:05 +0000285TEST_P(SecureElementAidl, transmit) {
286 std::vector<uint8_t> response;
287
288 // transmit called after init shall succeed.
289 // Note: no channel is opened for this test and the transmit
290 // response will have the status SW_LOGICAL_CHANNEL_NOT_SUPPORTED.
291 // The transmit response shall be larger than 2 bytes as it includes the
292 // status code.
293 EXPECT_OK(secure_element_->transmit(kDataApdu, &response));
294 EXPECT_GE(response.size(), 2u);
Steven Moreland6fe69542022-11-03 17:42:32 +0000295}
296
297GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SecureElementAidl);
298INSTANTIATE_TEST_SUITE_P(
299 SecureElement, SecureElementAidl,
300 testing::ValuesIn(android::getAidlHalInstanceNames(ISecureElement::descriptor)),
301 android::PrintInstanceNameToString);
302
303int main(int argc, char** argv) {
304 ::testing::InitGoogleTest(&argc, argv);
305 ABinderProcess_setThreadPoolMaxThreadCount(1);
306 ABinderProcess_startThreadPool();
307 return RUN_ALL_TESTS();
308}