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         ASSERT_TRUE(res.args);
79         EXPECT_TRUE(res.args->state_);
80     }
81 
82     sp<ISecureElement> se_;
83     sp<SecureElementHalCallback> se_cb_;
84 };
85 
86 /*
87  * isCardPresent:
88  * Expects the card to be present
89  */
TEST_P(SecureElementHidlTest,isCardPresent)90 TEST_P(SecureElementHidlTest, isCardPresent) {
91     EXPECT_TRUE(se_->isCardPresent());
92 }
93 
94 /*
95  * transmit:
96  * Check status word in the response
97  */
TEST_P(SecureElementHidlTest,transmit)98 TEST_P(SecureElementHidlTest, transmit) {
99     std::vector<uint8_t> aid = ANDROID_TEST_AID;
100     SecureElementStatus statusReturned;
101     LogicalChannelResponse response;
102     se_->openLogicalChannel(
103         aid, 0x00,
104         [&statusReturned, &response](LogicalChannelResponse channelResponse,
105                                      SecureElementStatus status) {
106             statusReturned = status;
107             if (status == SecureElementStatus::SUCCESS) {
108                 response.channelNumber = channelResponse.channelNumber;
109                 response.selectResponse.resize(channelResponse.selectResponse.size());
110                 for (size_t i = 0; i < channelResponse.selectResponse.size(); i++) {
111                     response.selectResponse[i] = channelResponse.selectResponse[i];
112                 }
113             }
114         });
115     EXPECT_EQ(SecureElementStatus::SUCCESS, statusReturned);
116     EXPECT_LE((unsigned int)2, response.selectResponse.size());
117     EXPECT_LE(1, response.channelNumber);
118     std::vector<uint8_t> command = DATA_APDU;
119     command[0] |= response.channelNumber;
120     std::vector<uint8_t> transmitResponse;
121     se_->transmit(command, [&transmitResponse](std::vector<uint8_t> res) {
122         transmitResponse.resize(res.size());
123         for (size_t i = 0; i < res.size(); i++) {
124             transmitResponse[i] = res[i];
125         }
126     });
127     EXPECT_LE((unsigned int)3, transmitResponse.size());
128     EXPECT_EQ(0x90, transmitResponse[transmitResponse.size() - 2]);
129     EXPECT_EQ(0x00, transmitResponse[transmitResponse.size() - 1]);
130     EXPECT_EQ(SecureElementStatus::SUCCESS, se_->closeChannel(response.channelNumber));
131 }
132 
133 /*
134  * OpenCloseBasicChannel:
135  * If the secure element allows opening of basic channel:
136  *  open channel, check the length of selectResponse and close the channel
137  */
TEST_P(SecureElementHidlTest,openBasicChannel)138 TEST_P(SecureElementHidlTest, openBasicChannel) {
139     std::vector<uint8_t> aid = ANDROID_TEST_AID;
140     SecureElementStatus statusReturned;
141     std::vector<uint8_t> response;
142     se_->openBasicChannel(aid, 0x00,
143                           [&statusReturned, &response](std::vector<uint8_t> selectResponse,
144                                                        SecureElementStatus status) {
145                               statusReturned = status;
146                               if (status == SecureElementStatus::SUCCESS) {
147                                   response.resize(selectResponse.size());
148                                   for (size_t i = 0; i < selectResponse.size(); i++) {
149                                       response[i] = selectResponse[i];
150                                   }
151                               }
152                           });
153     if (statusReturned == SecureElementStatus::SUCCESS) {
154         EXPECT_LE((unsigned int)2, response.size());
155         se_->closeChannel(0);
156         return;
157     }
158     EXPECT_EQ(SecureElementStatus::CHANNEL_NOT_AVAILABLE, statusReturned);
159 }
160 
161 /*
162  * GetATR
163  */
TEST_P(SecureElementHidlTest,getAtr)164 TEST_P(SecureElementHidlTest, getAtr) {
165     std::vector<uint8_t> atr;
166     se_->getAtr([&atr](std::vector<uint8_t> atrReturned) {
167         atr.resize(atrReturned.size());
168         for (size_t i = 0; i < atrReturned.size(); i++) {
169             atr[i] = atrReturned[i];
170         }
171     });
172     if (atr.size() == 0) {
173         return;
174     }
175     EXPECT_GE((unsigned int)32, atr.size());
176     EXPECT_LE((unsigned int)1, atr.size());
177 }
178 
179 /*
180  * OpenCloseLogicalChannel:
181  * Open Channel
182  * Check status
183  * Close Channel
184  */
TEST_P(SecureElementHidlTest,openCloseLogicalChannel)185 TEST_P(SecureElementHidlTest, openCloseLogicalChannel) {
186     std::vector<uint8_t> aid = ANDROID_TEST_AID;
187     SecureElementStatus statusReturned;
188     LogicalChannelResponse response;
189     se_->openLogicalChannel(
190         aid, 0x00,
191         [&statusReturned, &response](LogicalChannelResponse channelResponse,
192                                      SecureElementStatus status) {
193             statusReturned = status;
194             if (status == SecureElementStatus::SUCCESS) {
195                 response.channelNumber = channelResponse.channelNumber;
196                 response.selectResponse.resize(channelResponse.selectResponse.size());
197                 for (size_t i = 0; i < channelResponse.selectResponse.size(); i++) {
198                     response.selectResponse[i] = channelResponse.selectResponse[i];
199                 }
200             }
201         });
202     EXPECT_EQ(SecureElementStatus::SUCCESS, statusReturned);
203     EXPECT_LE((unsigned int)2, response.selectResponse.size());
204     EXPECT_LE(1, response.channelNumber);
205     EXPECT_EQ(SecureElementStatus::SUCCESS, se_->closeChannel(response.channelNumber));
206 }
207 
208 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SecureElementHidlTest);
209 INSTANTIATE_TEST_SUITE_P(
210         PerInstance, SecureElementHidlTest,
211         testing::ValuesIn(android::hardware::getAllHalInstanceNames(ISecureElement::descriptor)),
212         android::hardware::PrintInstanceNameToString);