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_TAG "secure_element_hidl_hal_test"
18 #include <android-base/logging.h>
19 
20 #include <android/hardware/secure_element/1.0/ISecureElement.h>
21 #include <android/hardware/secure_element/1.0/ISecureElementHalCallback.h>
22 #include <android/hardware/secure_element/1.0/types.h>
23 #include <gtest/gtest.h>
24 #include <hidl/GtestPrinter.h>
25 #include <hidl/ServiceManagement.h>
26 
27 #include <VtsHalHidlTargetCallbackBase.h>
28 
29 using ::android::hardware::secure_element::V1_0::ISecureElement;
30 using ::android::hardware::secure_element::V1_0::ISecureElementHalCallback;
31 using ::android::hardware::secure_element::V1_0::SecureElementStatus;
32 using ::android::hardware::secure_element::V1_0::LogicalChannelResponse;
33 using ::android::hardware::Return;
34 using ::android::hardware::Void;
35 using ::android::sp;
36 
37 #define DATA_APDU \
38     { 0x00, 0x08, 0x00, 0x00, 0x00 }
39 #define ANDROID_TEST_AID                                                                          \
40     {                                                                                             \
41         0xA0, 0x00, 0x00, 0x04, 0x76, 0x41, 0x6E, 0x64, 0x72, 0x6F, 0x69, 0x64, 0x43, 0x54, 0x53, \
42             0x31                                                                                  \
43     }
44 
45 constexpr char kCallbackNameOnStateChange[] = "onStateChange";
46 
47 class SecureElementCallbackArgs {
48    public:
49     bool state_;
50 };
51 
52 class SecureElementHalCallback
53     : public ::testing::VtsHalHidlTargetCallbackBase<SecureElementCallbackArgs>,
54       public ISecureElementHalCallback {
55    public:
56     virtual ~SecureElementHalCallback() = default;
57 
onStateChange(bool state)58     Return<void> onStateChange(bool state) override {
59         SecureElementCallbackArgs args;
60         args.state_ = state;
61         NotifyFromCallback(kCallbackNameOnStateChange, args);
62         return Void();
63     };
64 };
65 
66 class SecureElementHidlTest : public ::testing::TestWithParam<std::string> {
67   public:
SetUp()68     virtual void SetUp() override {
69         LOG(INFO) << "get service with name:" << GetParam();
70         se_ = ISecureElement::getService(GetParam());
71         ASSERT_NE(se_, nullptr);
72 
73         se_cb_ = new SecureElementHalCallback();
74         ASSERT_NE(se_cb_, nullptr);
75         se_->init(se_cb_);
76         auto res = se_cb_->WaitForCallback(kCallbackNameOnStateChange);
77         EXPECT_TRUE(res.no_timeout);
78         EXPECT_TRUE(res.args->state_);
79     }
80 
81     sp<ISecureElement> se_;
82     sp<SecureElementHalCallback> se_cb_;
83 };
84 
85 /*
86  * isCardPresent:
87  * Expects the card to be present
88  */
TEST_P(SecureElementHidlTest,isCardPresent)89 TEST_P(SecureElementHidlTest, isCardPresent) {
90     EXPECT_TRUE(se_->isCardPresent());
91 }
92 
93 /*
94  * transmit:
95  * Check status word in the response
96  */
TEST_P(SecureElementHidlTest,transmit)97 TEST_P(SecureElementHidlTest, transmit) {
98     std::vector<uint8_t> aid = ANDROID_TEST_AID;
99     SecureElementStatus statusReturned;
100     LogicalChannelResponse response;
101     se_->openLogicalChannel(
102         aid, 0x00,
103         [&statusReturned, &response](LogicalChannelResponse channelResponse,
104                                      SecureElementStatus status) {
105             statusReturned = status;
106             if (status == SecureElementStatus::SUCCESS) {
107                 response.channelNumber = channelResponse.channelNumber;
108                 response.selectResponse.resize(channelResponse.selectResponse.size());
109                 for (size_t i = 0; i < channelResponse.selectResponse.size(); i++) {
110                     response.selectResponse[i] = channelResponse.selectResponse[i];
111                 }
112             }
113         });
114     EXPECT_EQ(SecureElementStatus::SUCCESS, statusReturned);
115     EXPECT_LE((unsigned int)2, response.selectResponse.size());
116     EXPECT_LE(1, response.channelNumber);
117     std::vector<uint8_t> command = DATA_APDU;
118     command[0] |= response.channelNumber;
119     std::vector<uint8_t> transmitResponse;
120     se_->transmit(command, [&transmitResponse](std::vector<uint8_t> res) {
121         transmitResponse.resize(res.size());
122         for (size_t i = 0; i < res.size(); i++) {
123             transmitResponse[i] = res[i];
124         }
125     });
126     EXPECT_LE((unsigned int)3, transmitResponse.size());
127     EXPECT_EQ(0x90, transmitResponse[transmitResponse.size() - 2]);
128     EXPECT_EQ(0x00, transmitResponse[transmitResponse.size() - 1]);
129     EXPECT_EQ(SecureElementStatus::SUCCESS, se_->closeChannel(response.channelNumber));
130 }
131 
132 /*
133  * OpenCloseBasicChannel:
134  * If the secure element allows opening of basic channel:
135  *  open channel, check the length of selectResponse and close the channel
136  */
TEST_P(SecureElementHidlTest,openBasicChannel)137 TEST_P(SecureElementHidlTest, openBasicChannel) {
138     std::vector<uint8_t> aid = ANDROID_TEST_AID;
139     SecureElementStatus statusReturned;
140     std::vector<uint8_t> response;
141     se_->openBasicChannel(aid, 0x00,
142                           [&statusReturned, &response](std::vector<uint8_t> selectResponse,
143                                                        SecureElementStatus status) {
144                               statusReturned = status;
145                               if (status == SecureElementStatus::SUCCESS) {
146                                   response.resize(selectResponse.size());
147                                   for (size_t i = 0; i < selectResponse.size(); i++) {
148                                       response[i] = selectResponse[i];
149                                   }
150                               }
151                           });
152     if (statusReturned == SecureElementStatus::SUCCESS) {
153         EXPECT_LE((unsigned int)2, response.size());
154         se_->closeChannel(0);
155         return;
156     }
157     EXPECT_EQ(SecureElementStatus::CHANNEL_NOT_AVAILABLE, statusReturned);
158 }
159 
160 /*
161  * GetATR
162  */
TEST_P(SecureElementHidlTest,getAtr)163 TEST_P(SecureElementHidlTest, getAtr) {
164     std::vector<uint8_t> atr;
165     se_->getAtr([&atr](std::vector<uint8_t> atrReturned) {
166         atr.resize(atrReturned.size());
167         for (size_t i = 0; i < atrReturned.size(); i++) {
168             atr[i] = atrReturned[i];
169         }
170     });
171     if (atr.size() == 0) {
172         return;
173     }
174     EXPECT_GE((unsigned int)32, atr.size());
175     EXPECT_LE((unsigned int)1, atr.size());
176 }
177 
178 /*
179  * OpenCloseLogicalChannel:
180  * Open Channel
181  * Check status
182  * Close Channel
183  */
TEST_P(SecureElementHidlTest,openCloseLogicalChannel)184 TEST_P(SecureElementHidlTest, openCloseLogicalChannel) {
185     std::vector<uint8_t> aid = ANDROID_TEST_AID;
186     SecureElementStatus statusReturned;
187     LogicalChannelResponse response;
188     se_->openLogicalChannel(
189         aid, 0x00,
190         [&statusReturned, &response](LogicalChannelResponse channelResponse,
191                                      SecureElementStatus status) {
192             statusReturned = status;
193             if (status == SecureElementStatus::SUCCESS) {
194                 response.channelNumber = channelResponse.channelNumber;
195                 response.selectResponse.resize(channelResponse.selectResponse.size());
196                 for (size_t i = 0; i < channelResponse.selectResponse.size(); i++) {
197                     response.selectResponse[i] = channelResponse.selectResponse[i];
198                 }
199             }
200         });
201     EXPECT_EQ(SecureElementStatus::SUCCESS, statusReturned);
202     EXPECT_LE((unsigned int)2, response.selectResponse.size());
203     EXPECT_LE(1, response.channelNumber);
204     EXPECT_EQ(SecureElementStatus::SUCCESS, se_->closeChannel(response.channelNumber));
205 }
206 
207 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SecureElementHidlTest);
208 INSTANTIATE_TEST_SUITE_P(
209         PerInstance, SecureElementHidlTest,
210         testing::ValuesIn(android::hardware::getAllHalInstanceNames(ISecureElement::descriptor)),
211         android::hardware::PrintInstanceNameToString);