summaryrefslogtreecommitdiff
path: root/identity/Util.cpp
blob: 3a46bca4f5fa44d8f9e64902a0791b08b7b39ece (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright (c) 2019, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "credstore"

#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <android-base/logging.h>
#include <android-base/stringprintf.h>

#include <android/security/identity/ICredentialStore.h>

#include "Util.h"

namespace android {
namespace security {
namespace identity {

using ::android::base::StringPrintf;

Status halStatusToError(const Status& halStatus, int credStoreError) {
    string message = StringPrintf(
        "HAL failed with exception code %d (%s), service-specific error code %d, message '%s'",
        halStatus.exceptionCode(), Status::exceptionToString(halStatus.exceptionCode()).c_str(),
        halStatus.serviceSpecificErrorCode(), halStatus.exceptionMessage().c_str());
    return Status::fromServiceSpecificError(credStoreError, message.c_str());
}

Status halStatusToGenericError(const Status& halStatus) {
    return halStatusToError(halStatus, ICredentialStore::ERROR_GENERIC);
}

optional<vector<uint8_t>> fileGetContents(const string& path) {
    int fd = open(path.c_str(), O_RDONLY);
    if (fd == -1) {
        PLOG(ERROR) << "Error opening " << path;
        return {};
    }

    struct stat statbuf;
    if (fstat(fd, &statbuf) != 0) {
        PLOG(ERROR) << "Error statting " << path;
        close(fd);
        return {};
    }
    vector<uint8_t> data;
    data.resize(statbuf.st_size);

    uint8_t* p = data.data();
    size_t remaining = data.size();
    while (remaining > 0) {
        ssize_t numRead = TEMP_FAILURE_RETRY(read(fd, p, remaining));
        if (numRead <= 0) {
            PLOG(ERROR) << "Failed reading from '" << path << "'";
            close(fd);
            return {};
        }
        p += numRead;
        remaining -= numRead;
    }
    close(fd);

    return data;
}

bool fileSetContents(const string& path, const vector<uint8_t>& data) {
    char tempName[4096];
    int fd;

    string tempNameStr = path + ".XXXXXX";
    if (tempNameStr.size() >= sizeof tempName - 1) {
        LOG(ERROR) << "Path name too long";
        return false;
    }
    strncpy(tempName, tempNameStr.c_str(), sizeof tempName);

    fd = mkstemp(tempName);
    if (fd == -1) {
        PLOG(ERROR) << "Error creating temp file for '" << path << "'";
        return false;
    }

    const uint8_t* p = data.data();
    size_t remaining = data.size();
    while (remaining > 0) {
        ssize_t numWritten = TEMP_FAILURE_RETRY(write(fd, p, remaining));
        if (numWritten <= 0) {
            PLOG(ERROR) << "Failed writing into temp file for '" << path << "'";
            close(fd);
            return false;
        }
        p += numWritten;
        remaining -= numWritten;
    }

    if (TEMP_FAILURE_RETRY(fsync(fd))) {
        PLOG(ERROR) << "Failed fsyncing temp file for '" << path << "'";
        close(fd);
        return false;
    }
    close(fd);

    if (rename(tempName, path.c_str()) != 0) {
        PLOG(ERROR) << "Error renaming temp file for '" << path << "'";
        close(fd);
        return false;
    }

    return true;
}

}  // namespace identity
}  // namespace security
}  // namespace android