xref: /aosp_15_r20/system/security/identity/Util.cpp (revision e1997b9af69e3155ead6e072d106a0077849ffba)
1*e1997b9aSAndroid Build Coastguard Worker /*
2*e1997b9aSAndroid Build Coastguard Worker  * Copyright (c) 2019, The Android Open Source Project
3*e1997b9aSAndroid Build Coastguard Worker  *
4*e1997b9aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e1997b9aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e1997b9aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e1997b9aSAndroid Build Coastguard Worker  *
8*e1997b9aSAndroid Build Coastguard Worker  *     http://www.apache.org/licenses/LICENSE-2.0
9*e1997b9aSAndroid Build Coastguard Worker  *
10*e1997b9aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e1997b9aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e1997b9aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e1997b9aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e1997b9aSAndroid Build Coastguard Worker  * limitations under the License.
15*e1997b9aSAndroid Build Coastguard Worker  */
16*e1997b9aSAndroid Build Coastguard Worker 
17*e1997b9aSAndroid Build Coastguard Worker #define LOG_TAG "credstore"
18*e1997b9aSAndroid Build Coastguard Worker 
19*e1997b9aSAndroid Build Coastguard Worker #include <fcntl.h>
20*e1997b9aSAndroid Build Coastguard Worker #include <stdlib.h>
21*e1997b9aSAndroid Build Coastguard Worker #include <sys/stat.h>
22*e1997b9aSAndroid Build Coastguard Worker #include <sys/types.h>
23*e1997b9aSAndroid Build Coastguard Worker #include <unistd.h>
24*e1997b9aSAndroid Build Coastguard Worker 
25*e1997b9aSAndroid Build Coastguard Worker #include <android-base/logging.h>
26*e1997b9aSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
27*e1997b9aSAndroid Build Coastguard Worker 
28*e1997b9aSAndroid Build Coastguard Worker #include <android/security/identity/ICredentialStore.h>
29*e1997b9aSAndroid Build Coastguard Worker 
30*e1997b9aSAndroid Build Coastguard Worker #include "Util.h"
31*e1997b9aSAndroid Build Coastguard Worker 
32*e1997b9aSAndroid Build Coastguard Worker namespace android {
33*e1997b9aSAndroid Build Coastguard Worker namespace security {
34*e1997b9aSAndroid Build Coastguard Worker namespace identity {
35*e1997b9aSAndroid Build Coastguard Worker 
36*e1997b9aSAndroid Build Coastguard Worker using ::android::base::StringPrintf;
37*e1997b9aSAndroid Build Coastguard Worker 
halStatusToError(const Status & halStatus,int credStoreError)38*e1997b9aSAndroid Build Coastguard Worker Status halStatusToError(const Status& halStatus, int credStoreError) {
39*e1997b9aSAndroid Build Coastguard Worker     string message = StringPrintf(
40*e1997b9aSAndroid Build Coastguard Worker         "HAL failed with exception code %d (%s), service-specific error code %d, message '%s'",
41*e1997b9aSAndroid Build Coastguard Worker         halStatus.exceptionCode(), Status::exceptionToString(halStatus.exceptionCode()).c_str(),
42*e1997b9aSAndroid Build Coastguard Worker         halStatus.serviceSpecificErrorCode(), halStatus.exceptionMessage().c_str());
43*e1997b9aSAndroid Build Coastguard Worker     return Status::fromServiceSpecificError(credStoreError, message.c_str());
44*e1997b9aSAndroid Build Coastguard Worker }
45*e1997b9aSAndroid Build Coastguard Worker 
halStatusToGenericError(const Status & halStatus)46*e1997b9aSAndroid Build Coastguard Worker Status halStatusToGenericError(const Status& halStatus) {
47*e1997b9aSAndroid Build Coastguard Worker     return halStatusToError(halStatus, ICredentialStore::ERROR_GENERIC);
48*e1997b9aSAndroid Build Coastguard Worker }
49*e1997b9aSAndroid Build Coastguard Worker 
fileGetContents(const string & path)50*e1997b9aSAndroid Build Coastguard Worker optional<vector<uint8_t>> fileGetContents(const string& path) {
51*e1997b9aSAndroid Build Coastguard Worker     int fd = open(path.c_str(), O_RDONLY);
52*e1997b9aSAndroid Build Coastguard Worker     if (fd == -1) {
53*e1997b9aSAndroid Build Coastguard Worker         PLOG(ERROR) << "Error opening " << path;
54*e1997b9aSAndroid Build Coastguard Worker         return {};
55*e1997b9aSAndroid Build Coastguard Worker     }
56*e1997b9aSAndroid Build Coastguard Worker 
57*e1997b9aSAndroid Build Coastguard Worker     struct stat statbuf;
58*e1997b9aSAndroid Build Coastguard Worker     if (fstat(fd, &statbuf) != 0) {
59*e1997b9aSAndroid Build Coastguard Worker         PLOG(ERROR) << "Error statting " << path;
60*e1997b9aSAndroid Build Coastguard Worker         close(fd);
61*e1997b9aSAndroid Build Coastguard Worker         return {};
62*e1997b9aSAndroid Build Coastguard Worker     }
63*e1997b9aSAndroid Build Coastguard Worker     vector<uint8_t> data;
64*e1997b9aSAndroid Build Coastguard Worker     data.resize(statbuf.st_size);
65*e1997b9aSAndroid Build Coastguard Worker 
66*e1997b9aSAndroid Build Coastguard Worker     uint8_t* p = data.data();
67*e1997b9aSAndroid Build Coastguard Worker     size_t remaining = data.size();
68*e1997b9aSAndroid Build Coastguard Worker     while (remaining > 0) {
69*e1997b9aSAndroid Build Coastguard Worker         ssize_t numRead = TEMP_FAILURE_RETRY(read(fd, p, remaining));
70*e1997b9aSAndroid Build Coastguard Worker         if (numRead <= 0) {
71*e1997b9aSAndroid Build Coastguard Worker             PLOG(ERROR) << "Failed reading from '" << path << "'";
72*e1997b9aSAndroid Build Coastguard Worker             close(fd);
73*e1997b9aSAndroid Build Coastguard Worker             return {};
74*e1997b9aSAndroid Build Coastguard Worker         }
75*e1997b9aSAndroid Build Coastguard Worker         p += numRead;
76*e1997b9aSAndroid Build Coastguard Worker         remaining -= numRead;
77*e1997b9aSAndroid Build Coastguard Worker     }
78*e1997b9aSAndroid Build Coastguard Worker     close(fd);
79*e1997b9aSAndroid Build Coastguard Worker 
80*e1997b9aSAndroid Build Coastguard Worker     return data;
81*e1997b9aSAndroid Build Coastguard Worker }
82*e1997b9aSAndroid Build Coastguard Worker 
fileSetContents(const string & path,const vector<uint8_t> & data)83*e1997b9aSAndroid Build Coastguard Worker bool fileSetContents(const string& path, const vector<uint8_t>& data) {
84*e1997b9aSAndroid Build Coastguard Worker     char tempName[4096];
85*e1997b9aSAndroid Build Coastguard Worker     int fd;
86*e1997b9aSAndroid Build Coastguard Worker 
87*e1997b9aSAndroid Build Coastguard Worker     string tempNameStr = path + ".XXXXXX";
88*e1997b9aSAndroid Build Coastguard Worker     if (tempNameStr.size() >= sizeof tempName - 1) {
89*e1997b9aSAndroid Build Coastguard Worker         LOG(ERROR) << "Path name too long";
90*e1997b9aSAndroid Build Coastguard Worker         return false;
91*e1997b9aSAndroid Build Coastguard Worker     }
92*e1997b9aSAndroid Build Coastguard Worker     strncpy(tempName, tempNameStr.c_str(), sizeof tempName);
93*e1997b9aSAndroid Build Coastguard Worker 
94*e1997b9aSAndroid Build Coastguard Worker     fd = mkstemp(tempName);
95*e1997b9aSAndroid Build Coastguard Worker     if (fd == -1) {
96*e1997b9aSAndroid Build Coastguard Worker         PLOG(ERROR) << "Error creating temp file for '" << path << "'";
97*e1997b9aSAndroid Build Coastguard Worker         return false;
98*e1997b9aSAndroid Build Coastguard Worker     }
99*e1997b9aSAndroid Build Coastguard Worker 
100*e1997b9aSAndroid Build Coastguard Worker     const uint8_t* p = data.data();
101*e1997b9aSAndroid Build Coastguard Worker     size_t remaining = data.size();
102*e1997b9aSAndroid Build Coastguard Worker     while (remaining > 0) {
103*e1997b9aSAndroid Build Coastguard Worker         ssize_t numWritten = TEMP_FAILURE_RETRY(write(fd, p, remaining));
104*e1997b9aSAndroid Build Coastguard Worker         if (numWritten <= 0) {
105*e1997b9aSAndroid Build Coastguard Worker             PLOG(ERROR) << "Failed writing into temp file for '" << path << "'";
106*e1997b9aSAndroid Build Coastguard Worker             close(fd);
107*e1997b9aSAndroid Build Coastguard Worker             return false;
108*e1997b9aSAndroid Build Coastguard Worker         }
109*e1997b9aSAndroid Build Coastguard Worker         p += numWritten;
110*e1997b9aSAndroid Build Coastguard Worker         remaining -= numWritten;
111*e1997b9aSAndroid Build Coastguard Worker     }
112*e1997b9aSAndroid Build Coastguard Worker 
113*e1997b9aSAndroid Build Coastguard Worker     if (TEMP_FAILURE_RETRY(fsync(fd))) {
114*e1997b9aSAndroid Build Coastguard Worker         PLOG(ERROR) << "Failed fsyncing temp file for '" << path << "'";
115*e1997b9aSAndroid Build Coastguard Worker         close(fd);
116*e1997b9aSAndroid Build Coastguard Worker         return false;
117*e1997b9aSAndroid Build Coastguard Worker     }
118*e1997b9aSAndroid Build Coastguard Worker     close(fd);
119*e1997b9aSAndroid Build Coastguard Worker 
120*e1997b9aSAndroid Build Coastguard Worker     if (rename(tempName, path.c_str()) != 0) {
121*e1997b9aSAndroid Build Coastguard Worker         PLOG(ERROR) << "Error renaming temp file for '" << path << "'";
122*e1997b9aSAndroid Build Coastguard Worker         close(fd);
123*e1997b9aSAndroid Build Coastguard Worker         return false;
124*e1997b9aSAndroid Build Coastguard Worker     }
125*e1997b9aSAndroid Build Coastguard Worker 
126*e1997b9aSAndroid Build Coastguard Worker     return true;
127*e1997b9aSAndroid Build Coastguard Worker }
128*e1997b9aSAndroid Build Coastguard Worker 
129*e1997b9aSAndroid Build Coastguard Worker }  // namespace identity
130*e1997b9aSAndroid Build Coastguard Worker }  // namespace security
131*e1997b9aSAndroid Build Coastguard Worker }  // namespace android
132