Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1 | /* |
| 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 Chien | cdc813f | 2018-04-23 13:52:28 +0800 | [diff] [blame] | 25 | #include <log/log.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> |
Pavel Grafov | cef3947 | 2018-02-12 18:45:02 +0000 | [diff] [blame] | 27 | #include <private/android_logger.h> |
| 28 | |
| 29 | #include <log/log_event_list.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 30 | |
Shawn Willden | bb22a6c | 2017-12-06 19:35:28 -0700 | [diff] [blame] | 31 | #include <keystore/keymaster_types.h> |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 32 | #include <keystore/keystore_client.h> |
Shawn Willden | bb22a6c | 2017-12-06 19:35:28 -0700 | [diff] [blame] | 33 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 34 | #include "blob.h" |
| 35 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 36 | size_t readFully(int fd, uint8_t* data, size_t size) { |
| 37 | size_t remaining = size; |
| 38 | while (remaining > 0) { |
| 39 | ssize_t n = TEMP_FAILURE_RETRY(read(fd, data, remaining)); |
| 40 | if (n <= 0) { |
| 41 | return size - remaining; |
| 42 | } |
| 43 | data += n; |
| 44 | remaining -= n; |
| 45 | } |
| 46 | return size; |
| 47 | } |
| 48 | |
| 49 | size_t writeFully(int fd, uint8_t* data, size_t size) { |
| 50 | size_t remaining = size; |
| 51 | while (remaining > 0) { |
| 52 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, data, remaining)); |
| 53 | if (n < 0) { |
| 54 | ALOGW("write failed: %s", strerror(errno)); |
| 55 | return size - remaining; |
| 56 | } |
| 57 | data += n; |
| 58 | remaining -= n; |
| 59 | } |
Jacob Abrams | 3b06f80 | 2016-04-20 13:39:50 -0700 | [diff] [blame] | 60 | if (TEMP_FAILURE_RETRY(fsync(fd)) == -1) { |
| 61 | ALOGW("fsync failed: %s", strerror(errno)); |
| 62 | return -1; |
| 63 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 64 | return size; |
| 65 | } |
| 66 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 67 | void add_legacy_key_authorizations(int keyType, keystore::AuthorizationSet* params) { |
| 68 | using namespace keystore; |
| 69 | params->push_back(TAG_PURPOSE, KeyPurpose::SIGN); |
| 70 | params->push_back(TAG_PURPOSE, KeyPurpose::VERIFY); |
| 71 | params->push_back(TAG_PURPOSE, KeyPurpose::ENCRYPT); |
| 72 | params->push_back(TAG_PURPOSE, KeyPurpose::DECRYPT); |
| 73 | params->push_back(TAG_PADDING, PaddingMode::NONE); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 74 | if (keyType == EVP_PKEY_RSA) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 75 | params->push_back(TAG_PADDING, PaddingMode::RSA_PKCS1_1_5_SIGN); |
| 76 | params->push_back(TAG_PADDING, PaddingMode::RSA_PKCS1_1_5_ENCRYPT); |
| 77 | params->push_back(TAG_PADDING, PaddingMode::RSA_PSS); |
| 78 | params->push_back(TAG_PADDING, PaddingMode::RSA_OAEP); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 79 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 80 | params->push_back(TAG_DIGEST, Digest::NONE); |
| 81 | params->push_back(TAG_DIGEST, Digest::MD5); |
| 82 | params->push_back(TAG_DIGEST, Digest::SHA1); |
| 83 | params->push_back(TAG_DIGEST, Digest::SHA_2_224); |
| 84 | params->push_back(TAG_DIGEST, Digest::SHA_2_256); |
| 85 | params->push_back(TAG_DIGEST, Digest::SHA_2_384); |
| 86 | params->push_back(TAG_DIGEST, Digest::SHA_2_512); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 87 | params->push_back(TAG_NO_AUTH_REQUIRED); |
| 88 | params->push_back(TAG_ORIGINATION_EXPIRE_DATETIME, LLONG_MAX); |
| 89 | params->push_back(TAG_USAGE_EXPIRE_DATETIME, LLONG_MAX); |
| 90 | params->push_back(TAG_ACTIVE_DATETIME, 0); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | uid_t get_app_id(uid_t uid) { |
| 94 | return uid % AID_USER; |
| 95 | } |
| 96 | |
| 97 | uid_t get_user_id(uid_t uid) { |
| 98 | return uid / AID_USER; |
| 99 | } |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 100 | |
Pavel Grafov | cef3947 | 2018-02-12 18:45:02 +0000 | [diff] [blame] | 101 | void log_key_integrity_violation(const char* name, uid_t uid) { |
| 102 | if (!__android_log_security()) return; |
| 103 | android_log_event_list(SEC_TAG_KEY_INTEGRITY_VIOLATION) |
| 104 | << name << int32_t(uid) << LOG_ID_SECURITY; |
| 105 | } |
| 106 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 107 | namespace keystore { |
| 108 | |
| 109 | hidl_vec<uint8_t> blob2hidlVec(const Blob& blob) { |
| 110 | hidl_vec<uint8_t> result; |
| 111 | result.setToExternal(const_cast<uint8_t*>(blob.getValue()), blob.getLength()); |
| 112 | return result; |
| 113 | } |
| 114 | |
| 115 | SecurityLevel flagsToSecurityLevel(int32_t flags) { |
| 116 | switch (flags & (KEYSTORE_FLAG_FALLBACK | KEYSTORE_FLAG_STRONGBOX)) { |
| 117 | case KEYSTORE_FLAG_FALLBACK: |
| 118 | // treating Strongbox flag as "don't care" if Fallback is set |
| 119 | case (KEYSTORE_FLAG_FALLBACK | KEYSTORE_FLAG_STRONGBOX): |
| 120 | return SecurityLevel::SOFTWARE; |
| 121 | case KEYSTORE_FLAG_STRONGBOX: |
| 122 | return SecurityLevel::STRONGBOX; |
| 123 | default: |
| 124 | return SecurityLevel::TRUSTED_ENVIRONMENT; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | uint32_t securityLevelToFlags(SecurityLevel secLevel) { |
| 129 | switch (secLevel) { |
| 130 | case SecurityLevel::SOFTWARE: |
| 131 | return KEYSTORE_FLAG_FALLBACK; |
| 132 | case SecurityLevel::STRONGBOX: |
| 133 | return KEYSTORE_FLAG_STRONGBOX; |
| 134 | default: |
| 135 | return 0; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | } // namespace keystore |