Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 1 | /* |
| 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 Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 22 | #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 | |
| 31 | using namespace std::chrono_literals; |
| 32 | |
| 33 | using aidl::android::hardware::secure_element::BnSecureElementCallback; |
| 34 | using aidl::android::hardware::secure_element::ISecureElement; |
| 35 | using aidl::android::hardware::secure_element::LogicalChannelResponse; |
| 36 | using ndk::ScopedAStatus; |
| 37 | using ndk::SharedRefBase; |
| 38 | using ndk::SpAIBinder; |
| 39 | using testing::ElementsAre; |
| 40 | using 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 Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 48 | #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. |
| 57 | static const std::vector<uint8_t> kDataApdu = { |
| 58 | 0x00, 0x08, 0x00, 0x00, 0x00, |
| 59 | }; |
| 60 | |
| 61 | // Selectable test AID defined in CTS tests. |
| 62 | static 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. |
| 67 | static 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 Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 71 | |
| 72 | class 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 | |
| 96 | class SecureElementAidl : public ::testing::TestWithParam<std::string> { |
| 97 | public: |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 98 | void SetUp() override { |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 99 | SpAIBinder binder = SpAIBinder(AServiceManager_waitForService(GetParam().c_str())); |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 100 | |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 101 | secure_element_ = ISecureElement::fromBinder(binder); |
| 102 | ASSERT_NE(secure_element_, nullptr); |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 103 | |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 104 | 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 Chataing | 9ba974d | 2023-01-31 18:22:57 +0000 | [diff] [blame^] | 109 | |
| 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 Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 120 | void TearDown() override { |
Henri Chataing | 4b780eb | 2023-01-31 18:22:57 +0000 | [diff] [blame] | 121 | EXPECT_OK(secure_element_->reset()); |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 122 | 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 | |
| 139 | EXPECT_OK(secure_element_->transmit(apdu, &response)); |
| 140 | EXPECT_GE(response.size(), 2u); |
| 141 | uint16_t status = |
| 142 | (response[response.size() - 2] << 8) | (response[response.size() - 1] << 0); |
| 143 | |
| 144 | // When the command is successful the response |
| 145 | // must contain 256 bytes of data. |
| 146 | if (status == 0x9000) { |
| 147 | EXPECT_EQ(response.size(), 258); |
| 148 | } |
| 149 | |
| 150 | return status; |
| 151 | } |
| 152 | |
| 153 | std::shared_ptr<ISecureElement> secure_element_; |
| 154 | std::shared_ptr<MySecureElementCallback> secure_element_callback_; |
Henri Chataing | 9ba974d | 2023-01-31 18:22:57 +0000 | [diff] [blame^] | 155 | bool basic_channel_supported_{false}; |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 158 | TEST_P(SecureElementAidl, init) { |
| 159 | // init(nullptr) shall fail. |
| 160 | EXPECT_ERR(secure_element_->init(nullptr)); |
| 161 | |
| 162 | // init with a valid callback pointer shall succeed. |
| 163 | EXPECT_OK(secure_element_->init(secure_element_callback_)); |
| 164 | secure_element_callback_->expectCallbackHistory({true, true}); |
| 165 | } |
| 166 | |
| 167 | TEST_P(SecureElementAidl, reset) { |
| 168 | std::vector<uint8_t> basic_channel_response; |
| 169 | LogicalChannelResponse logical_channel_response; |
| 170 | |
| 171 | // reset called after init shall succeed. |
Henri Chataing | 9ba974d | 2023-01-31 18:22:57 +0000 | [diff] [blame^] | 172 | if (basic_channel_supported_) { |
| 173 | EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &basic_channel_response)); |
| 174 | } |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 175 | EXPECT_OK(secure_element_->openLogicalChannel(kSelectableAid, 0x00, &logical_channel_response)); |
| 176 | |
| 177 | EXPECT_OK(secure_element_->reset()); |
| 178 | secure_element_callback_->expectCallbackHistory({true, false, true}); |
| 179 | |
| 180 | // All opened channels must be closed. |
Henri Chataing | 9ba974d | 2023-01-31 18:22:57 +0000 | [diff] [blame^] | 181 | if (basic_channel_supported_) { |
| 182 | EXPECT_NE(transmit(0), 0x9000); |
| 183 | } |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 184 | EXPECT_NE(transmit(logical_channel_response.channelNumber), 0x9000); |
| 185 | } |
| 186 | |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 187 | TEST_P(SecureElementAidl, isCardPresent) { |
| 188 | bool res = false; |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 189 | |
| 190 | // isCardPresent called after init shall succeed. |
| 191 | EXPECT_OK(secure_element_->isCardPresent(&res)); |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 192 | EXPECT_TRUE(res); |
| 193 | } |
| 194 | |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 195 | TEST_P(SecureElementAidl, getAtr) { |
| 196 | std::vector<uint8_t> atr; |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 197 | |
| 198 | // getAtr called after init shall succeed. |
| 199 | // The ATR has size between 0 and 32 bytes. |
| 200 | EXPECT_OK(secure_element_->getAtr(&atr)); |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 201 | EXPECT_LE(atr.size(), 32u); |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 204 | TEST_P(SecureElementAidl, openBasicChannel) { |
| 205 | std::vector<uint8_t> response; |
| 206 | |
Henri Chataing | 9ba974d | 2023-01-31 18:22:57 +0000 | [diff] [blame^] | 207 | if (!basic_channel_supported_) { |
| 208 | return; |
| 209 | } |
| 210 | |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 211 | // openBasicChannel called with an invalid AID shall fail. |
| 212 | EXPECT_ERR(secure_element_->openBasicChannel(kNonSelectableAid, 0x00, &response)); |
| 213 | |
| 214 | // openBasicChannel called after init shall succeed. |
| 215 | // The response size must be larger than 2 bytes as it includes the |
| 216 | // status code. |
| 217 | EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &response)); |
| 218 | EXPECT_GE(response.size(), 2u); |
| 219 | |
Henri Chataing | 9ba974d | 2023-01-31 18:22:57 +0000 | [diff] [blame^] | 220 | // transmit called on the basic channel should succeed. |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 221 | EXPECT_EQ(transmit(0), 0x9000); |
| 222 | |
| 223 | // openBasicChannel called a second time shall fail. |
| 224 | // The basic channel can only be opened once. |
| 225 | EXPECT_ERR(secure_element_->openBasicChannel(kSelectableAid, 0x00, &response)); |
| 226 | |
| 227 | // openBasicChannel called after closing the basic channel shall succeed. |
| 228 | EXPECT_OK(secure_element_->closeChannel(0)); |
| 229 | EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &response)); |
| 230 | } |
| 231 | |
| 232 | TEST_P(SecureElementAidl, openLogicalChannel) { |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 233 | LogicalChannelResponse response; |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 234 | |
| 235 | // openLogicalChannel called with an invalid AID shall fail. |
| 236 | EXPECT_ERR(secure_element_->openLogicalChannel(kNonSelectableAid, 0x00, &response)); |
| 237 | |
| 238 | // openLogicalChannel called after init shall succeed. |
| 239 | // The response size must be larger than 2 bytes as it includes the |
| 240 | // status code. The channel number must be in the range 1-19. |
| 241 | EXPECT_OK(secure_element_->openLogicalChannel(kSelectableAid, 0x00, &response)); |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 242 | EXPECT_GE(response.selectResponse.size(), 2u); |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 243 | EXPECT_GE(response.channelNumber, 1u); |
| 244 | EXPECT_LE(response.channelNumber, 19u); |
| 245 | |
Henri Chataing | 9ba974d | 2023-01-31 18:22:57 +0000 | [diff] [blame^] | 246 | // transmit called on the logical channel should succeed. |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 247 | EXPECT_EQ(transmit(response.channelNumber), 0x9000); |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 250 | TEST_P(SecureElementAidl, closeChannel) { |
| 251 | std::vector<uint8_t> basic_channel_response; |
| 252 | LogicalChannelResponse logical_channel_response; |
| 253 | |
Henri Chataing | 4b780eb | 2023-01-31 18:22:57 +0000 | [diff] [blame] | 254 | // closeChannel called on non-existing basic or logical channel |
| 255 | // shall fail. |
| 256 | EXPECT_ERR(secure_element_->closeChannel(0)); |
| 257 | EXPECT_ERR(secure_element_->closeChannel(1)); |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 258 | |
| 259 | // closeChannel called on basic channel closes the basic channel. |
Henri Chataing | 9ba974d | 2023-01-31 18:22:57 +0000 | [diff] [blame^] | 260 | if (basic_channel_supported_) { |
| 261 | EXPECT_OK(secure_element_->openBasicChannel(kSelectableAid, 0x00, &basic_channel_response)); |
| 262 | EXPECT_OK(secure_element_->closeChannel(0)); |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 263 | |
Henri Chataing | 9ba974d | 2023-01-31 18:22:57 +0000 | [diff] [blame^] | 264 | // transmit called on the basic channel should fail. |
| 265 | EXPECT_NE(transmit(0), 0x9000); |
| 266 | } |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 267 | |
| 268 | // closeChannel called on logical channel closes the logical channel. |
| 269 | EXPECT_OK(secure_element_->openLogicalChannel(kSelectableAid, 0x00, &logical_channel_response)); |
| 270 | EXPECT_OK(secure_element_->closeChannel(logical_channel_response.channelNumber)); |
| 271 | |
Henri Chataing | 9ba974d | 2023-01-31 18:22:57 +0000 | [diff] [blame^] | 272 | // transmit called on the logical channel should fail. |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 273 | EXPECT_NE(transmit(logical_channel_response.channelNumber), 0x9000); |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Henri Chataing | cf06db3 | 2023-01-06 09:20:05 +0000 | [diff] [blame] | 276 | TEST_P(SecureElementAidl, transmit) { |
| 277 | std::vector<uint8_t> response; |
| 278 | |
| 279 | // transmit called after init shall succeed. |
| 280 | // Note: no channel is opened for this test and the transmit |
| 281 | // response will have the status SW_LOGICAL_CHANNEL_NOT_SUPPORTED. |
| 282 | // The transmit response shall be larger than 2 bytes as it includes the |
| 283 | // status code. |
| 284 | EXPECT_OK(secure_element_->transmit(kDataApdu, &response)); |
| 285 | EXPECT_GE(response.size(), 2u); |
Steven Moreland | 6fe6954 | 2022-11-03 17:42:32 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SecureElementAidl); |
| 289 | INSTANTIATE_TEST_SUITE_P( |
| 290 | SecureElement, SecureElementAidl, |
| 291 | testing::ValuesIn(android::getAidlHalInstanceNames(ISecureElement::descriptor)), |
| 292 | android::PrintInstanceNameToString); |
| 293 | |
| 294 | int main(int argc, char** argv) { |
| 295 | ::testing::InitGoogleTest(&argc, argv); |
| 296 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 297 | ABinderProcess_startThreadPool(); |
| 298 | return RUN_ALL_TESTS(); |
| 299 | } |