1 /* 2 ** 3 ** Copyright 2018, The Android Open Source Project 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 ** 19 ** The original Work has been changed by NXP. 20 ** 21 ** Licensed under the Apache License, Version 2.0 (the "License"); 22 ** you may not use this file except in compliance with the License. 23 ** You may obtain a copy of the License at 24 ** 25 ** http://www.apache.org/licenses/LICENSE-2.0 26 ** 27 ** Unless required by applicable law or agreed to in writing, software 28 ** distributed under the License is distributed on an "AS IS" BASIS, 29 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 30 ** See the License for the specific language governing permissions and 31 ** limitations under the License. 32 ** 33 ** Copyright 2020-2021 NXP 34 ** 35 *********************************************************************************/ 36 #ifndef __APPLETCONNECTION_H__ 37 #define __APPLETCONNECTION_H__ 38 39 #include <android/hardware/secure_element/1.0/types.h> 40 #include <android/hardware/secure_element/1.1/ISecureElementHalCallback.h> 41 #include <android/hardware/secure_element/1.2/ISecureElement.h> 42 #include <hidl/MQDescriptor.h> 43 #include <hidl/Status.h> 44 #include <vector> 45 46 #include <SBAccessController.h> 47 48 namespace keymint::javacard { 49 50 using ::android::hardware::hidl_array; 51 using ::android::hardware::hidl_memory; 52 using ::android::hardware::hidl_string; 53 using ::android::hardware::hidl_vec; 54 using ::android::hardware::Return; 55 using ::android::hardware::Void; 56 using ::android::sp; 57 using ::android::hardware::secure_element::V1_2::ISecureElement; 58 using ::android::hardware::secure_element::V1_1::ISecureElementHalCallback; 59 60 struct AppletConnection { 61 public: 62 AppletConnection(const std::vector<uint8_t>& aid); 63 64 /** 65 * Connects to the secure element HAL service. Returns true if successful, false otherwise. 66 */ 67 bool connectToSEService(); 68 69 /** 70 * Select the applet on the secure element. SELECT command response is returned in resp vector 71 */ 72 bool openChannelToApplet(std::vector<uint8_t>& resp); 73 74 /** 75 * If open, closes the open channel to the applet. Returns an error if channel was not 76 * open or the SE HAL service returned an error. 77 */ 78 bool close(); 79 80 /** 81 * Sends the data to the secure element and also receives back the data. 82 * This is a blocking call. 83 */ 84 bool transmit(std::vector<uint8_t>& CommandApdu, std::vector<uint8_t>& output); 85 86 /** 87 * Checks if a channel to the applet is open. 88 */ 89 bool isChannelOpen(); 90 /** 91 * Get session timeout value based on select response normal/update session 92 */ 93 int getSessionTimeout(); 94 95 private: 96 /** 97 * Select applet with given P2 parameter 98 */ 99 bool selectApplet(std::vector<uint8_t>& resp, uint8_t p2); 100 101 std::mutex channel_mutex_; // exclusive access to isChannelopen()/close() 102 sp<ISecureElement> mSEClient; 103 std::vector<uint8_t> kAppletAID; 104 int8_t mOpenChannel = -1; 105 SBAccessController mSBAccessController; 106 }; 107 108 } // namespace keymint::javacard 109 #endif // __APPLETCONNECTION_H__ 110