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 #ifndef __SE_TRANSPORT__
18 #define __SE_TRANSPORT__
19 
20 namespace se_transport {
21 
22 /**
23  * ITransport is an abstract interface with a set of virtual methods that allow communication
24  * between the keymaster HAL and the secure element.
25  */
26 class ITransport {
27   public:
~ITransport()28     virtual ~ITransport(){}
29 
30     /**
31      * Opens connection.
32      */
33     virtual bool openConnection() = 0;
34     /**
35      * Send data over communication channel and receives data back from the remote end.
36      */
37     virtual bool sendData(const uint8_t* inData, const size_t inLen,
38                           std::vector<uint8_t>& output) = 0;
39     /**
40      * Closes the connection.
41      */
42     virtual bool closeConnection() = 0;
43     /**
44      * Returns the state of the connection status. Returns true if the connection is active, false if
45      * connection is broken.
46      */
47     virtual bool isConnected() = 0;
48 
49 };
50 
51 /**
52  * OmapiTransport is derived from ITransport. This class gets the OMAPI service binder instance and
53  * uses IPC to communicate with OMAPI service. OMAPI inturn communicates with hardware via
54  * ISecureElement.
55  */
56 class OmapiTransport : public ITransport {
57 
58   public:
59     /**
60      * Gets the binder instance of ISEService, gets the reader corresponding to secure element,
61      * establishes a session and opens a basic channel.
62      */
63     bool openConnection() override;
64     /**
65      * Transmists the data over the opened basic channel and receives the data back.
66      */
67     bool sendData(const uint8_t* inData, const size_t inLen, std::vector<uint8_t>& output) override;
68     /**
69      * Closes the connection.
70      */
71     bool closeConnection() override;
72     /**
73      * Returns the state of the connection status. Returns true if the connection is active, false
74      * if connection is broken.
75      */
76     bool isConnected() override;
77 
78 };
79 
80 class SocketTransport : public ITransport {
81 
82   public:
SocketTransport()83     SocketTransport() : mSocket(-1), socketStatus(false) {}
84     /**
85      * Creates a socket instance and connects to the provided server IP and port.
86      */
87     bool openConnection() override;
88     /**
89      * Sends data over socket and receives data back.
90      */
91     bool sendData(const uint8_t* inData, const size_t inLen, std::vector<uint8_t>& output) override;
92     /**
93      * Closes the connection.
94      */
95     bool closeConnection() override;
96     /**
97      * Returns the state of the connection status. Returns true if the connection is active, false
98      * if connection is broken.
99      */
100     bool isConnected() override;
101   private:
102     /**
103      * Socket instance.
104      */
105     int mSocket;
106     bool socketStatus;
107 };
108 
109 }  // namespace se_transport
110 #endif /* __SE_TRANSPORT__ */
111