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 "keymaster_ipc.h"
33 #include "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] = {
80             .iov_base = out + *out_size,
81             .iov_len = std::min<uint32_t>(KEYMASTER_MAX_BUFFER_LENGTH, 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