1 /*
2  * Copyright (C) 2015 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 
17 #define LOG_TAG "TrustyKeymaster"
18 
19 // TODO: make this generic in libtrusty
20 
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/uio.h>
25 #include <unistd.h>
26 
27 #include <algorithm>
28 
29 #include <log/log.h>
30 #include <trusty/tipc.h>
31 
32 #include <trusty_keymaster/ipc/keymaster_ipc.h>
33 #include <trusty_keymaster/ipc/trusty_keymaster_ipc.h>
34 
35 #define TRUSTY_DEVICE_NAME "/dev/trusty-ipc-dev0"
36 
37 static int handle_ = -1;
38 
trusty_keymaster_connect()39 int trusty_keymaster_connect() {
40     int rc = tipc_connect(TRUSTY_DEVICE_NAME, KEYMASTER_PORT);
41     if (rc < 0) {
42         return rc;
43     }
44 
45     handle_ = rc;
46     return 0;
47 }
48 
trusty_keymaster_call(uint32_t cmd,void * in,uint32_t in_size,uint8_t * out,uint32_t * out_size)49 int trusty_keymaster_call(uint32_t cmd, void* in, uint32_t in_size, uint8_t* out,
50                           uint32_t* out_size) {
51     if (handle_ < 0) {
52         ALOGE("not connected\n");
53         return -EINVAL;
54     }
55 
56     size_t msg_size = in_size + sizeof(struct keymaster_message);
57     struct keymaster_message* msg = reinterpret_cast<struct keymaster_message*>(malloc(msg_size));
58     if (!msg) {
59         ALOGE("failed to allocate msg buffer\n");
60         return -EINVAL;
61     }
62 
63     msg->cmd = cmd;
64     memcpy(msg->payload, in, in_size);
65 
66     ssize_t rc = write(handle_, msg, msg_size);
67     free(msg);
68 
69     if (rc < 0) {
70         ALOGE("failed to send cmd (%d) to %s: %s\n", cmd, KEYMASTER_PORT, strerror(errno));
71         return -errno;
72     }
73     size_t out_max_size = *out_size;
74     *out_size = 0;
75     struct iovec iov[2];
76     struct keymaster_message header;
77     iov[0] = {.iov_base = &header, .iov_len = sizeof(struct keymaster_message)};
78     while (true) {
79         iov[1] = {.iov_base = out + *out_size,
80                   .iov_len = std::min<uint32_t>(KEYMASTER_MAX_BUFFER_LENGTH,
81                                                 out_max_size - *out_size)};
82         rc = readv(handle_, iov, 2);
83         if (rc < 0) {
84             ALOGE("failed to retrieve response for cmd (%d) to %s: %s\n", cmd, KEYMASTER_PORT,
85                   strerror(errno));
86             return -errno;
87         }
88 
89         if ((size_t)rc < sizeof(struct keymaster_message)) {
90             ALOGE("invalid response size (%d)\n", (int)rc);
91             return -EINVAL;
92         }
93 
94         if ((cmd | KEYMASTER_RESP_BIT) != (header.cmd & ~(KEYMASTER_STOP_BIT))) {
95             ALOGE("invalid command (%d)", header.cmd);
96             return -EINVAL;
97         }
98         *out_size += ((size_t)rc - sizeof(struct keymaster_message));
99         if (header.cmd & KEYMASTER_STOP_BIT) {
100             break;
101         }
102     }
103 
104     return rc;
105 }
106 
trusty_keymaster_disconnect()107 void trusty_keymaster_disconnect() {
108     if (handle_ >= 0) {
109         tipc_close(handle_);
110     }
111     handle_ = -1;
112 }
113 
translate_error(int err)114 keymaster_error_t translate_error(int err) {
115     switch (err) {
116         case 0:
117             return KM_ERROR_OK;
118         case -EPERM:
119         case -EACCES:
120             return KM_ERROR_SECURE_HW_ACCESS_DENIED;
121 
122         case -ECANCELED:
123             return KM_ERROR_OPERATION_CANCELLED;
124 
125         case -ENODEV:
126             return KM_ERROR_UNIMPLEMENTED;
127 
128         case -ENOMEM:
129             return KM_ERROR_MEMORY_ALLOCATION_FAILED;
130 
131         case -EBUSY:
132             return KM_ERROR_SECURE_HW_BUSY;
133 
134         case -EIO:
135             return KM_ERROR_SECURE_HW_COMMUNICATION_FAILED;
136 
137         case -EOVERFLOW:
138             return KM_ERROR_INVALID_INPUT_LENGTH;
139 
140         default:
141             return KM_ERROR_UNKNOWN_ERROR;
142     }
143 }
144 
trusty_keymaster_send(uint32_t command,const keymaster::Serializable & req,keymaster::KeymasterResponse * rsp)145 keymaster_error_t trusty_keymaster_send(uint32_t command, const keymaster::Serializable& req,
146                                         keymaster::KeymasterResponse* rsp) {
147     uint32_t req_size = req.SerializedSize();
148     if (req_size > TRUSTY_KEYMASTER_SEND_BUF_SIZE) {
149         ALOGE("Request too big: %u Max size: %u", req_size, TRUSTY_KEYMASTER_SEND_BUF_SIZE);
150         return KM_ERROR_INVALID_INPUT_LENGTH;
151     }
152 
153     uint8_t send_buf[TRUSTY_KEYMASTER_SEND_BUF_SIZE];
154     keymaster::Eraser send_buf_eraser(send_buf, TRUSTY_KEYMASTER_SEND_BUF_SIZE);
155     req.Serialize(send_buf, send_buf + req_size);
156 
157     // Send it
158     uint8_t recv_buf[TRUSTY_KEYMASTER_RECV_BUF_SIZE];
159     keymaster::Eraser recv_buf_eraser(recv_buf, TRUSTY_KEYMASTER_RECV_BUF_SIZE);
160     uint32_t rsp_size = TRUSTY_KEYMASTER_RECV_BUF_SIZE;
161     int rc = trusty_keymaster_call(command, send_buf, req_size, recv_buf, &rsp_size);
162     if (rc < 0) {
163         // Reset the connection on tipc error
164         trusty_keymaster_disconnect();
165         trusty_keymaster_connect();
166         ALOGE("tipc error: %d\n", rc);
167         // TODO(swillden): Distinguish permanent from transient errors and set error_ appropriately.
168         return translate_error(rc);
169     } else {
170         ALOGV("Received %d byte response\n", rsp_size);
171     }
172 
173     const uint8_t* p = recv_buf;
174     if (!rsp->Deserialize(&p, p + rsp_size)) {
175         ALOGE("Error deserializing response of size %d\n", (int)rsp_size);
176         return KM_ERROR_UNKNOWN_ERROR;
177     } else if (rsp->error != KM_ERROR_OK) {
178         ALOGE("Response of size %d contained error code %d\n", (int)rsp_size, (int)rsp->error);
179         return rsp->error;
180     }
181     return rsp->error;
182 }
183