1 /******************************************************************************
2  *
3  *  Copyright 2018-2021 NXP
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 #define LOG_TAG "nxpese@1.1-service"
19 #include <android/hardware/secure_element/1.1/ISecureElement.h>
20 #include <hidl/LegacySupport.h>
21 #include <log/log.h>
22 #include <string.h>
23 #include <vendor/nxp/nxpese/1.0/INxpEse.h>
24 
25 #include <regex>
26 
27 #include "NxpEse.h"
28 #include "SecureElement.h"
29 #include "VirtualISO.h"
30 #ifdef NXP_BOOTTIME_UPDATE
31 #include "eSEClient.h"
32 #endif
33 
34 // Generated HIDL files
35 using android::OK;
36 using android::base::StringPrintf;
37 using android::hardware::configureRpcThreadpool;
38 using android::hardware::defaultPassthroughServiceImplementation;
39 using android::hardware::joinRpcThreadpool;
40 using android::hardware::registerPassthroughServiceImplementation;
41 using android::hardware::secure_element::V1_1::ISecureElement;
42 using android::hardware::secure_element::V1_1::implementation::SecureElement;
43 using vendor::nxp::nxpese::V1_0::INxpEse;
44 using vendor::nxp::nxpese::V1_0::implementation::NxpEse;
45 using vendor::nxp::virtual_iso::V1_0::implementation::VirtualISO;
46 
47 using android::OK;
48 using android::sp;
49 using android::status_t;
50 
main()51 int main() {
52   status_t status;
53 
54   char terminalID[5];
55   const char* SEterminal = "eSEx";
56   bool ret = false;
57 
58   android::sp<ISecureElement> se_service = nullptr;
59   android::sp<INxpEse> nxp_se_service = nullptr;
60   android::sp<ISecureElement> virtual_iso_service = nullptr;
61 
62   ALOGI("Secure Element HAL Service 1.1 is starting.");
63   try {
64     se_service = new SecureElement();
65     if (se_service == nullptr) {
66       ALOGE("Can not create an instance of Secure Element HAL Iface, exiting.");
67       goto shutdown;
68     }
69     configureRpcThreadpool(1, true /*callerWillJoin*/);
70 #ifdef NXP_BOOTTIME_UPDATE
71     checkEseClientUpdate();
72     ret = geteSETerminalId(terminalID);
73 #else
74     ret = true;
75 #endif
76     ALOGI("Terminal val = %s", terminalID);
77     if ((ret) && (strncmp(SEterminal, terminalID, 3) == 0)) {
78       ALOGI("Terminal ID found");
79       status = se_service->registerAsService(terminalID);
80 
81       if (status != OK) {
82         ALOGE("Could not register service for Secure Element HAL Iface (%d).",
83               status);
84         goto shutdown;
85       }
86       ALOGI("Secure Element Service is ready");
87 
88       ALOGI("NXP Secure Element Extn Service 1.0 is starting.");
89       nxp_se_service = new NxpEse();
90       if (nxp_se_service == nullptr) {
91         ALOGE(
92             "Can not create an instance of NXP Secure Element Extn "
93             "Iface,exiting.");
94         goto shutdown;
95       }
96       status = nxp_se_service->registerAsService();
97       if (status != OK) {
98         ALOGE(
99             "Could not register service for Power Secure Element Extn Iface "
100             "(%d).",
101             status);
102         goto shutdown;
103       }
104       ALOGI("Secure Element Service is ready");
105     }
106 #ifdef NXP_VISO_ENABLE
107     ALOGI("Virtual ISO HAL Service 1.0 is starting.");
108     virtual_iso_service = new VirtualISO();
109     if (virtual_iso_service == nullptr) {
110       ALOGE("Can not create an instance of Virtual ISO HAL Iface, exiting.");
111       goto shutdown;
112     }
113 #ifdef NXP_BOOTTIME_UPDATE
114     ret = geteUICCTerminalId(terminalID);
115 #else
116     strncpy(terminalID, "eSE2", 4);
117     ret = true;
118 #endif
119     if ((ret) && (strncmp(SEterminal, terminalID, 3) == 0)) {
120       status = virtual_iso_service->registerAsService(terminalID);
121       if (status != OK) {
122         ALOGE("Could not register service for Virtual ISO HAL Iface (%d).",
123               status);
124         goto shutdown;
125       }
126     }
127 
128     ALOGI("Virtual ISO: Secure Element Service is ready");
129 #endif
130 #ifdef NXP_BOOTTIME_UPDATE
131     perform_eSEClientUpdate();
132 #endif
133     joinRpcThreadpool();
134   } catch (const std::length_error& e) {
135     ALOGE("Length Exception occurred = %s ", e.what());
136   } catch (const std::__1::ios_base::failure& e) {
137     ALOGE("ios failure Exception occurred = %s ", e.what());
138   } catch (std::__1::regex_error& e) {
139     ALOGE("Regex Exception occurred = %s ", e.what());
140   }
141 // Should not pass this line
142 shutdown:
143   // In normal operation, we don't expect the thread pool to exit
144   ALOGE("Secure Element Service is shutting down");
145   return 1;
146 }
147