• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * Hack-y server to forward communication with an eSE during development.
17  * See README.md for more information.
18  */
19 
20 #include <arpa/inet.h>
21 #include <linux/un.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/socket.h>
26 #include <unistd.h>
27 
28 #define LOG_TAG "ese-relay"
29 #include <ese/ese.h>
30 #include <ese/log.h>
31 
32 extern const uint8_t *kAtr;
33 extern size_t kAtrLength;
34 extern void *kEseOpenData;
35 void ese_relay_init(struct EseInterface *ese);
36 
37 /*
38  * Aligned with vpcd.h in
39  * https://frankmorgner.github.io/vsmartcard/virtualsmartcard
40  */
41 #define CMD_POWER_OFF 0
42 #define CMD_POWER_ON 1
43 #define CMD_RESET 2
44 #define CMD_ATR 4
45 
setup_socket(const char * name)46 int setup_socket(const char *name) {
47   int fd;
48   struct sockaddr_un addr;
49 
50   memset(&addr, 0, sizeof(struct sockaddr_un));
51   addr.sun_family = AF_UNIX;
52   if (strlen(name) > UNIX_PATH_MAX - 1) {
53     ALOGE("Abstract listener name too long.");
54     return -1;
55   }
56   strncpy(&addr.sun_path[1], name, strlen(name));
57   fd = socket(AF_UNIX, SOCK_STREAM, 0);
58   if (fd == -1) {
59     ALOGE("Could not open socket.");
60     return fd;
61   }
62   if (bind(fd, (struct sockaddr *)&addr,
63            sizeof(sa_family_t) + strlen(name) + 1) == -1) {
64     ALOGE("Failed to bind to abstract socket name");
65     close(fd);
66     return -1;
67   }
68   return fd;
69 }
70 
main()71 int main() {
72   int server_fd = setup_socket(LOG_TAG);
73   int client_fd = -1;
74   struct EseInterface ese;
75   ese_relay_init(&ese);
76 
77   if (listen(server_fd, 4)) {
78     ALOGE("Failed to listen on socket.");
79     close(server_fd);
80     return -1;
81   }
82 
83   while (server_fd) {
84     struct sockaddr client_info;
85     socklen_t client_info_len = (socklen_t)sizeof(&client_info);
86     int client_fd;
87     uint32_t tx_len, data_read;
88     uint32_t rx_len;
89     uint16_t network_tx_len;
90     uint16_t network_rx_len;
91     uint8_t tx_buf[4096];
92     uint8_t rx_buf[4096];
93     int connected = 0;
94 
95     if ((client_fd = accept(server_fd, &client_info, &client_info_len)) == -1) {
96       ALOGE("Fatal error accept()ing a client connection.");
97       return -1;
98     }
99     printf("Client connected.\n");
100     connected = 1;
101     if (ese_open(&ese, kEseOpenData)) {
102       ALOGE("Cannot open hw");
103       if (ese_error(&ese))
104         ALOGE("eSE error (%d): %s", ese_error_code(&ese),
105               ese_error_message(&ese));
106       return 1;
107     }
108     printf("eSE is open\n");
109 
110     while (connected) {
111       printf("Listening for data from client\n");
112       if (read(client_fd, &network_tx_len, sizeof(network_tx_len)) !=
113           sizeof(network_tx_len)) {
114         ALOGE("Client disconnected.");
115         break;
116       }
117       tx_len = (uint32_t)ntohs(network_tx_len);
118       printf("tx_len: %u\n", tx_len);
119       if (tx_len == 0) {
120         ALOGE("Client had nothing to say. Goodbye.");
121         break;
122       }
123       if (tx_len > sizeof(tx_buf)) {
124         ALOGE("Client payload too large: %u", tx_len);
125         break;
126       }
127       for (data_read = 0; data_read < tx_len;) {
128         printf("Reading payload: %u of %u remaining\n", data_read, tx_len);
129         ssize_t bytes = read(client_fd, tx_buf + data_read, tx_len - data_read);
130         if (bytes < 0) {
131           ALOGE("Client abandoned hope during transmission.");
132           connected = 0;
133           break;
134         }
135         data_read += bytes;
136       }
137       /* Finally, we can transcieve. */
138       if (tx_len) {
139         uint32_t i;
140         printf("Sending %u bytes to card\n", tx_len);
141         printf("TX: ");
142         for (i = 0; i < tx_len; ++i)
143           printf("%.2X ", tx_buf[i]);
144         printf("\n");
145       }
146 
147       if (tx_len == 1) { /* Control request */
148         printf("Received a control request: %x\n", tx_buf[0]);
149         rx_len = 0;
150         switch (tx_buf[0]) {
151         case CMD_POWER_OFF:
152           ese.ops->hw_reset(&ese);
153           break;
154         case CMD_POWER_ON:
155           break;
156         case CMD_RESET:
157           ese.ops->hw_reset(&ese);
158           break;
159         case CMD_ATR:
160           /* Send a dummy ATR for another JCOP card */
161           rx_len = kAtrLength;
162           printf("Filling card RX buf with fake ATR (%u)\n", rx_len);
163           memcpy(rx_buf, kAtr, rx_len);
164           printf("Sending back ATR of length %u\n", rx_len);
165           break;
166         default:
167           ALOGE("Unknown control byte seen: %x", tx_buf[0]);
168         }
169         if (!rx_len)
170           continue;
171       } else {
172         rx_len = ese_transceive(&ese, tx_buf, tx_len, rx_buf, sizeof(rx_buf));
173         if (ese_error(&ese)) {
174           ALOGE("An error (%d) occurred: %s", ese_error_code(&ese),
175                 ese_error_message(&ese));
176           return -1;
177         }
178       }
179       if (rx_len > 0) {
180         uint32_t i;
181         printf("Read %d bytes from card\n", rx_len);
182         printf("RX: ");
183         for (i = 0; i < rx_len; ++i)
184           printf("%.2X ", rx_buf[i]);
185         printf("\n");
186       }
187 
188       /* Send to client */
189       network_rx_len = htons((uint16_t)rx_len);
190       if (write(client_fd, &network_rx_len, sizeof(network_rx_len)) !=
191           sizeof(network_rx_len)) {
192         ALOGE("Client abandoned hope during response size.");
193         break;
194       }
195 
196       for (data_read = 0; data_read < rx_len;) {
197         ssize_t bytes =
198             write(client_fd, rx_buf + data_read, rx_len - data_read);
199         if (bytes < 0) {
200           ALOGE("Client abandoned hope during response.");
201           connected = 0;
202           break;
203         }
204         data_read += bytes;
205       }
206       usleep(1000);
207     }
208     close(client_fd);
209     printf("Session ended\n\n");
210     ese_close(&ese);
211   }
212   return 0;
213 }
214