1 /* 2 ** 3 ** Copyright 2020, 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 2022-2024 NXP 34 ** 35 *********************************************************************************/ 36 #if defined OMAPI_TRANSPORT 37 #pragma once 38 39 #include <aidl/android/se/omapi/BnSecureElementListener.h> 40 #include <aidl/android/se/omapi/ISecureElementChannel.h> 41 #include <aidl/android/se/omapi/ISecureElementListener.h> 42 #include <aidl/android/se/omapi/ISecureElementReader.h> 43 #include <aidl/android/se/omapi/ISecureElementService.h> 44 #include <aidl/android/se/omapi/ISecureElementSession.h> 45 // #include <aidl/android/se/omapi/SecureElementErrorCode.h> 46 #include <android/binder_manager.h> 47 48 #include <map> 49 50 #include <IntervalTimer.h> 51 #include <memory> 52 #include <vector> 53 #include "ITransport.h" 54 55 #include <SBAccessController.h> 56 57 #define APP_NOT_FOUND_SW1 0x6A 58 #define APP_NOT_FOUND_SW2 0x82 59 60 namespace keymint::javacard { 61 using std::shared_ptr; 62 using std::vector; 63 64 /** 65 * OmapiTransport is derived from ITransport. This class gets the OMAPI service binder instance and uses IPC to 66 * communicate with OMAPI service. OMAPI inturn communicates with hardware via ISecureElement. 67 */ 68 class OmapiTransport : public std::enable_shared_from_this<OmapiTransport>, 69 public ITransport { 70 71 public: make(const std::vector<uint8_t> & mAppletAID)72 static shared_ptr<OmapiTransport> make(const std::vector<uint8_t> &mAppletAID) { 73 return std::shared_ptr<OmapiTransport>(new OmapiTransport(mAppletAID)); 74 } 75 virtual ~OmapiTransport(); 76 77 #ifdef NXP_EXTNS 78 /** 79 * Sets Applet Aid 80 */ setAppletAid(const vector<uint8_t> & aid)81 bool setAppletAid(const vector<uint8_t> &aid) { 82 mSelectableAid = aid; 83 return true; 84 } 85 86 /** 87 * Sets state(start/finish) of crypto operation. 88 * This is required for channel session timeout mgmt. 89 */ 90 void setCryptoOperationState(uint8_t state) override; 91 #endif 92 /** 93 * Gets the binder instance of ISEService, gets te reader corresponding to secure element, 94 * establishes a session and opens a basic channel. 95 */ 96 bool openConnection() override; 97 /** 98 * Transmists the data over the opened basic channel and receives the data back. 99 */ 100 bool sendData(const vector<uint8_t>& inData, vector<uint8_t>& output) override; 101 /** 102 * Closes the connection. 103 */ 104 bool closeConnection() override; 105 /** 106 * Returns the state of the connection status. Returns true if the connection is active, false if connection is 107 * broken. 108 */ 109 bool isConnected() override; 110 #ifdef NXP_EXTNS 111 /** 112 * Closes the opened channel. 113 */ 114 void closeChannel(); 115 /** 116 * set default Interval timer timeout value. 117 */ 118 void setDefaultTimeout(int timeout); 119 #endif 120 121 private: 122 //AppletConnection mAppletConnection; 123 SBAccessController& mSBAccessController; 124 IntervalTimer mTimer; 125 int mTimeout; 126 std::vector<uint8_t> mSelectableAid; 127 std::shared_ptr<aidl::android::se::omapi::ISecureElementService> omapiSeService; 128 std::shared_ptr<aidl::android::se::omapi::ISecureElementReader> eSEReader; 129 std::shared_ptr<aidl::android::se::omapi::ISecureElementSession> session; 130 std::shared_ptr<aidl::android::se::omapi::ISecureElementChannel> channel; 131 std::map<std::string, std::shared_ptr<aidl::android::se::omapi::ISecureElementReader>> 132 mVSReaders; 133 #ifdef NXP_EXTNS 134 /* Applet ID Weaver */ 135 const std::vector<uint8_t> kWeaverAID = {0xA0, 0x00, 0x00, 0x03, 0x96, 0x10, 0x10}; 136 #endif OmapiTransport(const std::vector<uint8_t> & mAppletAID)137 OmapiTransport(const std::vector<uint8_t>& mAppletAID) 138 : ITransport(mAppletAID), 139 mSBAccessController(SBAccessController::getInstance()), 140 mTimeout(0), 141 mSelectableAid(mAppletAID), 142 omapiSeService(nullptr), 143 eSEReader(nullptr), 144 session(nullptr), 145 channel(nullptr), 146 mVSReaders({}) { 147 #ifdef NXP_EXTNS 148 mDeathRecipient = ::ndk::ScopedAIBinder_DeathRecipient( 149 AIBinder_DeathRecipient_new(BinderDiedCallback)); 150 #endif 151 } 152 bool initialize(); 153 bool internalTransmitApdu( 154 std::shared_ptr<aidl::android::se::omapi::ISecureElementReader> reader, 155 std::vector<uint8_t> apdu, std::vector<uint8_t>& transmitResponse); 156 157 #ifdef NXP_EXTNS 158 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient; 159 std::mutex mCookieKeysMutex; 160 std::vector<uintptr_t> mCookieKeys; 161 162 static void BinderDiedCallback(void *cookie); 163 bool internalProtectedTransmitApdu( 164 std::shared_ptr<aidl::android::se::omapi::ISecureElementReader> reader, 165 std::vector<uint8_t> apdu, std::vector<uint8_t>& transmitResponse); 166 void prepareErrorRepsponse(std::vector<uint8_t>& resp); 167 bool openChannelToApplet(); 168 #endif 169 #ifdef INTERVAL_TIMER getApduStatus(std::vector<uint8_t> & inputData)170 inline uint16_t getApduStatus(std::vector<uint8_t> &inputData) { 171 // Last two bytes are the status SW0SW1 172 uint8_t SW0 = inputData.at(inputData.size() - 2); 173 uint8_t SW1 = inputData.at(inputData.size() - 1); 174 return (SW0 << 8 | SW1); 175 } 176 #endif 177 }; 178 } // namespace keymint::javacard 179 #endif 180