1 /*
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 
17 #define LOG_TAG "credstore"
18 
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 
32 namespace android {
33 namespace security {
34 namespace identity {
35 
36 using ::android::base::StringPrintf;
37 
halStatusToError(const Status & halStatus,int credStoreError)38 Status 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 
halStatusToGenericError(const Status & halStatus)46 Status halStatusToGenericError(const Status& halStatus) {
47     return halStatusToError(halStatus, ICredentialStore::ERROR_GENERIC);
48 }
49 
fileGetContents(const string & path)50 optional<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) {
69         ssize_t numRead = TEMP_FAILURE_RETRY(read(fd, p, remaining));
70         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 
fileSetContents(const string & path,const vector<uint8_t> & data)83 bool 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) {
103         ssize_t numWritten = TEMP_FAILURE_RETRY(write(fd, p, remaining));
104         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 
113     if (TEMP_FAILURE_RETRY(fsync(fd))) {
114         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
132