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 2020-2023 NXP 34 ** 35 *********************************************************************************/ 36 37 #ifndef __SE_TRANSPORT_FACTORY__ 38 #define __SE_TRANSPORT_FACTORY__ 39 40 #include "HalToHalTransport.h" 41 #include "OmapiTransport.h" 42 #include "SocketTransport.h" 43 44 namespace se_transport { 45 46 using keymint::javacard::HalToHalTransport; 47 using keymint::javacard::ITransport; 48 using keymint::javacard::SocketTransport; 49 #ifdef OMAPI_TRANSPORT 50 using keymint::javacard::OmapiTransport; 51 #endif 52 53 /** 54 * TransportFactory class decides which transport mechanism to be used to send data to secure element. In case of 55 * emulator the communication channel is socket and in case of device the communication channel is via OMAPI. 56 */ 57 class TransportFactory { 58 public: TransportFactory(bool isEmulator,const std::vector<uint8_t> & mAppletAID)59 TransportFactory(bool isEmulator, const std::vector<uint8_t>& mAppletAID) { 60 if (!isEmulator) { 61 #ifdef OMAPI_TRANSPORT 62 mTransport = OmapiTransport::make(mAppletAID); 63 #else 64 mTransport = std::shared_ptr<HalToHalTransport>(new HalToHalTransport(mAppletAID)); 65 #endif 66 } 67 #ifndef NXP_EXTNS 68 else 69 mTransport = std::shared_ptr<SocketTransport>(new SocketTransport(mAppletAID)); 70 #endif 71 } 72 ~TransportFactory()73 ~TransportFactory() {} 74 75 /** 76 * Sets Applet AID. 77 */ setAppletAid(const std::vector<uint8_t> & aid)78 inline bool setAppletAid(const std::vector<uint8_t> &aid) { 79 return mTransport->setAppletAid(aid); 80 } 81 82 /** 83 * Establishes a communication channel with the secure element. 84 */ openConnection()85 inline bool openConnection() { 86 return mTransport->openConnection(); 87 } 88 89 /** 90 * Sends the data to the secure element and also receives back the data. 91 * This is a blocking call. 92 */ sendData(const uint8_t * inData,const size_t inLen,std::vector<uint8_t> & output)93 inline bool sendData(const uint8_t* inData, const size_t inLen, std::vector<uint8_t>& output) { 94 std::vector input(inData, inData + inLen); 95 return mTransport->sendData(input, output); 96 } 97 98 /** 99 * Close the connection. 100 */ closeConnection()101 inline bool closeConnection() { 102 return mTransport->closeConnection(); 103 } 104 105 /** 106 * Returns the connection status of the communication channel. 107 */ isConnected()108 inline bool isConnected() { 109 return mTransport->isConnected(); 110 } 111 112 private: 113 /** 114 * Holds the instance of either OmapiTransport class or SocketTransport class. 115 */ 116 std::shared_ptr<ITransport> mTransport; 117 118 }; 119 } // namespace se_transport 120 #endif /* __SE_TRANSPORT_FACTORY__ */ 121