1 /*
2 * Copyright (C) 2022 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 #if defined(TRUSTY_USERSPACE)
18 #include <openssl/rand.h>
19 #include <trusty_ipc.h>
20 #else
21 #include <lib/rand/rand.h>
22 #endif
23
24 #include <binder/RpcTransportTipcTrusty.h>
25
26 #include "../OS.h"
27 #include "TrustyStatus.h"
28
29 using android::base::Result;
30
31 namespace android {
32
setNonBlocking(android::base::borrowed_fd)33 Result<void> setNonBlocking(android::base::borrowed_fd /*fd*/) {
34 // Trusty IPC syscalls are all non-blocking by default.
35 return {};
36 }
37
getRandomBytes(uint8_t * data,size_t size)38 status_t getRandomBytes(uint8_t* data, size_t size) {
39 #if defined(TRUSTY_USERSPACE)
40 int res = RAND_bytes(data, size);
41 return res == 1 ? OK : UNKNOWN_ERROR;
42 #else
43 int res = rand_get_bytes(data, size);
44 return res == 0 ? OK : UNKNOWN_ERROR;
45 #endif // TRUSTY_USERSPACE
46 }
47
dupFileDescriptor(int oldFd,int * newFd)48 status_t dupFileDescriptor(int oldFd, int* newFd) {
49 int res = dup(oldFd);
50 if (res < 0) {
51 return statusFromTrusty(res);
52 }
53
54 *newFd = res;
55 return OK;
56 }
57
makeDefaultRpcTransportCtxFactory()58 std::unique_ptr<RpcTransportCtxFactory> makeDefaultRpcTransportCtxFactory() {
59 return RpcTransportCtxFactoryTipcTrusty::make();
60 }
61
sendMessageOnSocket(const RpcTransportFd &,iovec *,int,const std::vector<std::variant<base::unique_fd,base::borrowed_fd>> *)62 ssize_t sendMessageOnSocket(
63 const RpcTransportFd& /* socket */, iovec* /* iovs */, int /* niovs */,
64 const std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* /* ancillaryFds */) {
65 errno = ENOTSUP;
66 return -1;
67 }
68
receiveMessageFromSocket(const RpcTransportFd &,iovec *,int,std::vector<std::variant<base::unique_fd,base::borrowed_fd>> *)69 ssize_t receiveMessageFromSocket(
70 const RpcTransportFd& /* socket */, iovec* /* iovs */, int /* niovs */,
71 std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* /* ancillaryFds */) {
72 errno = ENOTSUP;
73 return -1;
74 }
75
76 } // namespace android
77