blob: 3a46bca4f5fa44d8f9e64902a0791b08b7b39ece [file] [log] [blame]
David Zeuthenab3e5652019-10-28 13:32:48 -04001/*
2 * Copyright (c) 2019, 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
David Zeuthen62d43bf2021-03-31 10:41:27 -040017#define LOG_TAG "credstore"
David Zeuthenab3e5652019-10-28 13:32:48 -040018
19#include <fcntl.h>
20#include <stdlib.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <unistd.h>
24
25#include <android-base/logging.h>
26#include <android-base/stringprintf.h>
27
28#include <android/security/identity/ICredentialStore.h>
29
30#include "Util.h"
31
32namespace android {
33namespace security {
34namespace identity {
35
36using ::android::base::StringPrintf;
37
David Zeuthena6f9fba2020-02-11 22:08:27 -050038Status halStatusToError(const Status& halStatus, int credStoreError) {
39 string message = StringPrintf(
40 "HAL failed with exception code %d (%s), service-specific error code %d, message '%s'",
41 halStatus.exceptionCode(), Status::exceptionToString(halStatus.exceptionCode()).c_str(),
42 halStatus.serviceSpecificErrorCode(), halStatus.exceptionMessage().c_str());
43 return Status::fromServiceSpecificError(credStoreError, message.c_str());
44}
45
46Status halStatusToGenericError(const Status& halStatus) {
47 return halStatusToError(halStatus, ICredentialStore::ERROR_GENERIC);
David Zeuthenab3e5652019-10-28 13:32:48 -040048}
49
50optional<vector<uint8_t>> fileGetContents(const string& path) {
51 int fd = open(path.c_str(), O_RDONLY);
52 if (fd == -1) {
53 PLOG(ERROR) << "Error opening " << path;
54 return {};
55 }
56
57 struct stat statbuf;
58 if (fstat(fd, &statbuf) != 0) {
59 PLOG(ERROR) << "Error statting " << path;
60 close(fd);
61 return {};
62 }
63 vector<uint8_t> data;
64 data.resize(statbuf.st_size);
65
66 uint8_t* p = data.data();
67 size_t remaining = data.size();
68 while (remaining > 0) {
Greg Kaiserc8966aa2020-01-21 07:05:08 -080069 ssize_t numRead = TEMP_FAILURE_RETRY(read(fd, p, remaining));
David Zeuthenab3e5652019-10-28 13:32:48 -040070 if (numRead <= 0) {
71 PLOG(ERROR) << "Failed reading from '" << path << "'";
72 close(fd);
73 return {};
74 }
75 p += numRead;
76 remaining -= numRead;
77 }
78 close(fd);
79
80 return data;
81}
82
83bool fileSetContents(const string& path, const vector<uint8_t>& data) {
84 char tempName[4096];
85 int fd;
86
87 string tempNameStr = path + ".XXXXXX";
88 if (tempNameStr.size() >= sizeof tempName - 1) {
89 LOG(ERROR) << "Path name too long";
90 return false;
91 }
92 strncpy(tempName, tempNameStr.c_str(), sizeof tempName);
93
94 fd = mkstemp(tempName);
95 if (fd == -1) {
96 PLOG(ERROR) << "Error creating temp file for '" << path << "'";
97 return false;
98 }
99
100 const uint8_t* p = data.data();
101 size_t remaining = data.size();
102 while (remaining > 0) {
Greg Kaiserc8966aa2020-01-21 07:05:08 -0800103 ssize_t numWritten = TEMP_FAILURE_RETRY(write(fd, p, remaining));
David Zeuthenab3e5652019-10-28 13:32:48 -0400104 if (numWritten <= 0) {
105 PLOG(ERROR) << "Failed writing into temp file for '" << path << "'";
106 close(fd);
107 return false;
108 }
109 p += numWritten;
110 remaining -= numWritten;
111 }
112
Yi Kong1bdb61e2020-06-19 14:00:40 +0800113 if (TEMP_FAILURE_RETRY(fsync(fd))) {
David Zeuthenab3e5652019-10-28 13:32:48 -0400114 PLOG(ERROR) << "Failed fsyncing temp file for '" << path << "'";
115 close(fd);
116 return false;
117 }
118 close(fd);
119
120 if (rename(tempName, path.c_str()) != 0) {
121 PLOG(ERROR) << "Error renaming temp file for '" << path << "'";
122 close(fd);
123 return false;
124 }
125
126 return true;
127}
128
129} // namespace identity
130} // namespace security
131} // namespace android