blob: f0f60982081117b95f938a44447380f01a61f529 [file] [log] [blame]
Shawn Willdenc1d1fee2016-01-26 22:44:56 -07001/*
2 * Copyright (C) 2016 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 "keystore"
18
19#include "keystore_utils.h"
20
21#include <errno.h>
22#include <string.h>
23#include <unistd.h>
24
Logan Chiencdc813f2018-04-23 13:52:28 +080025#include <log/log.h>
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070026#include <private/android_filesystem_config.h>
Pavel Grafovcef39472018-02-12 18:45:02 +000027#include <private/android_logger.h>
28
29#include <log/log_event_list.h>
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070030
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070031#include <keystore/keymaster_types.h>
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010032#include <keystore/keystore_client.h>
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070033
Janis Danisevskis6a0d9982019-04-30 15:43:59 -070034#include <android-base/logging.h>
35#include <android-base/unique_fd.h>
36
Janis Danisevskisc1460142017-12-18 16:48:46 -080037#include "blob.h"
38
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070039size_t readFully(int fd, uint8_t* data, size_t size) {
40 size_t remaining = size;
41 while (remaining > 0) {
42 ssize_t n = TEMP_FAILURE_RETRY(read(fd, data, remaining));
43 if (n <= 0) {
44 return size - remaining;
45 }
46 data += n;
47 remaining -= n;
48 }
49 return size;
50}
51
52size_t writeFully(int fd, uint8_t* data, size_t size) {
53 size_t remaining = size;
54 while (remaining > 0) {
55 ssize_t n = TEMP_FAILURE_RETRY(write(fd, data, remaining));
56 if (n < 0) {
57 ALOGW("write failed: %s", strerror(errno));
58 return size - remaining;
59 }
60 data += n;
61 remaining -= n;
62 }
Jacob Abrams3b06f802016-04-20 13:39:50 -070063 if (TEMP_FAILURE_RETRY(fsync(fd)) == -1) {
64 ALOGW("fsync failed: %s", strerror(errno));
65 return -1;
66 }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070067 return size;
68}
69
Janis Danisevskis6a0d9982019-04-30 15:43:59 -070070std::string getContainingDirectory(const std::string& filename) {
71 std::string containing_dir;
72 size_t last_pos;
73 size_t pos = std::string::npos;
74
75 __builtin_add_overflow(filename.size(), -1, &last_pos);
76
77 // strip all trailing '/'
78 while ((pos = filename.find_last_of('/', last_pos)) == last_pos && pos != 0) {
79 --last_pos;
80 }
81
82 if (pos == 0) {
83 containing_dir = "/";
84 } else if (pos == std::string::npos) {
85 containing_dir = ".";
86 } else {
87 containing_dir = filename.substr(0, pos);
88 }
89
90 return containing_dir;
91}
92
93void fsyncDirectory(const std::string& path) {
94 android::base::unique_fd dir_fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_DIRECTORY | O_RDONLY)));
95
96 if (dir_fd < 0) {
97 LOG(WARNING) << "Could not open dir: " << path << " error: " << strerror(errno);
98 return;
99 }
100
101 if (TEMP_FAILURE_RETRY(fsync(dir_fd)) == -1) {
102 LOG(WARNING) << "Failed to fsync the directory " << path << " error: " << strerror(errno);
103 }
104
105 return;
106}
107
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100108void add_legacy_key_authorizations(int keyType, keystore::AuthorizationSet* params) {
109 using namespace keystore;
110 params->push_back(TAG_PURPOSE, KeyPurpose::SIGN);
111 params->push_back(TAG_PURPOSE, KeyPurpose::VERIFY);
112 params->push_back(TAG_PURPOSE, KeyPurpose::ENCRYPT);
113 params->push_back(TAG_PURPOSE, KeyPurpose::DECRYPT);
114 params->push_back(TAG_PADDING, PaddingMode::NONE);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700115 if (keyType == EVP_PKEY_RSA) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100116 params->push_back(TAG_PADDING, PaddingMode::RSA_PKCS1_1_5_SIGN);
117 params->push_back(TAG_PADDING, PaddingMode::RSA_PKCS1_1_5_ENCRYPT);
118 params->push_back(TAG_PADDING, PaddingMode::RSA_PSS);
119 params->push_back(TAG_PADDING, PaddingMode::RSA_OAEP);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700120 }
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100121 params->push_back(TAG_DIGEST, Digest::NONE);
122 params->push_back(TAG_DIGEST, Digest::MD5);
123 params->push_back(TAG_DIGEST, Digest::SHA1);
124 params->push_back(TAG_DIGEST, Digest::SHA_2_224);
125 params->push_back(TAG_DIGEST, Digest::SHA_2_256);
126 params->push_back(TAG_DIGEST, Digest::SHA_2_384);
127 params->push_back(TAG_DIGEST, Digest::SHA_2_512);
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100128 params->push_back(TAG_NO_AUTH_REQUIRED);
129 params->push_back(TAG_ORIGINATION_EXPIRE_DATETIME, LLONG_MAX);
130 params->push_back(TAG_USAGE_EXPIRE_DATETIME, LLONG_MAX);
131 params->push_back(TAG_ACTIVE_DATETIME, 0);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700132}
133
134uid_t get_app_id(uid_t uid) {
135 return uid % AID_USER;
136}
137
138uid_t get_user_id(uid_t uid) {
139 return uid / AID_USER;
140}
Janis Danisevskisc1460142017-12-18 16:48:46 -0800141
Pavel Grafovcef39472018-02-12 18:45:02 +0000142void log_key_integrity_violation(const char* name, uid_t uid) {
143 if (!__android_log_security()) return;
144 android_log_event_list(SEC_TAG_KEY_INTEGRITY_VIOLATION)
145 << name << int32_t(uid) << LOG_ID_SECURITY;
146}
147
Janis Danisevskisc1460142017-12-18 16:48:46 -0800148namespace keystore {
149
150hidl_vec<uint8_t> blob2hidlVec(const Blob& blob) {
Rob Barnesbb6cabd2018-10-04 17:10:37 -0600151 hidl_vec<uint8_t> result(blob.getValue(), blob.getValue() + blob.getLength());
Janis Danisevskisc1460142017-12-18 16:48:46 -0800152 return result;
153}
154
155SecurityLevel flagsToSecurityLevel(int32_t flags) {
156 switch (flags & (KEYSTORE_FLAG_FALLBACK | KEYSTORE_FLAG_STRONGBOX)) {
157 case KEYSTORE_FLAG_FALLBACK:
158 // treating Strongbox flag as "don't care" if Fallback is set
159 case (KEYSTORE_FLAG_FALLBACK | KEYSTORE_FLAG_STRONGBOX):
160 return SecurityLevel::SOFTWARE;
161 case KEYSTORE_FLAG_STRONGBOX:
162 return SecurityLevel::STRONGBOX;
163 default:
164 return SecurityLevel::TRUSTED_ENVIRONMENT;
165 }
166}
167
168uint32_t securityLevelToFlags(SecurityLevel secLevel) {
169 switch (secLevel) {
170 case SecurityLevel::SOFTWARE:
171 return KEYSTORE_FLAG_FALLBACK;
172 case SecurityLevel::STRONGBOX:
173 return KEYSTORE_FLAG_STRONGBOX;
174 default:
175 return 0;
176 }
177}
178
179} // namespace keystore