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