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 | |
| 25 | #include <cutils/log.h> |
| 26 | #include <private/android_filesystem_config.h> |
| 27 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 28 | #include <keystore/authorization_set.h> |
| 29 | #include <keystore/keystore_client.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 30 | size_t readFully(int fd, uint8_t* data, size_t size) { |
| 31 | size_t remaining = size; |
| 32 | while (remaining > 0) { |
| 33 | ssize_t n = TEMP_FAILURE_RETRY(read(fd, data, remaining)); |
| 34 | if (n <= 0) { |
| 35 | return size - remaining; |
| 36 | } |
| 37 | data += n; |
| 38 | remaining -= n; |
| 39 | } |
| 40 | return size; |
| 41 | } |
| 42 | |
| 43 | size_t writeFully(int fd, uint8_t* data, size_t size) { |
| 44 | size_t remaining = size; |
| 45 | while (remaining > 0) { |
| 46 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, data, remaining)); |
| 47 | if (n < 0) { |
| 48 | ALOGW("write failed: %s", strerror(errno)); |
| 49 | return size - remaining; |
| 50 | } |
| 51 | data += n; |
| 52 | remaining -= n; |
| 53 | } |
Jacob Abrams | 3b06f80 | 2016-04-20 13:39:50 -0700 | [diff] [blame] | 54 | if (TEMP_FAILURE_RETRY(fsync(fd)) == -1) { |
| 55 | ALOGW("fsync failed: %s", strerror(errno)); |
| 56 | return -1; |
| 57 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 58 | return size; |
| 59 | } |
| 60 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 61 | void add_legacy_key_authorizations(int keyType, keystore::AuthorizationSet* params) { |
| 62 | using namespace keystore; |
| 63 | params->push_back(TAG_PURPOSE, KeyPurpose::SIGN); |
| 64 | params->push_back(TAG_PURPOSE, KeyPurpose::VERIFY); |
| 65 | params->push_back(TAG_PURPOSE, KeyPurpose::ENCRYPT); |
| 66 | params->push_back(TAG_PURPOSE, KeyPurpose::DECRYPT); |
| 67 | params->push_back(TAG_PADDING, PaddingMode::NONE); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 68 | if (keyType == EVP_PKEY_RSA) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 69 | params->push_back(TAG_PADDING, PaddingMode::RSA_PKCS1_1_5_SIGN); |
| 70 | params->push_back(TAG_PADDING, PaddingMode::RSA_PKCS1_1_5_ENCRYPT); |
| 71 | params->push_back(TAG_PADDING, PaddingMode::RSA_PSS); |
| 72 | params->push_back(TAG_PADDING, PaddingMode::RSA_OAEP); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 73 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 74 | params->push_back(TAG_DIGEST, Digest::NONE); |
| 75 | params->push_back(TAG_DIGEST, Digest::MD5); |
| 76 | params->push_back(TAG_DIGEST, Digest::SHA1); |
| 77 | params->push_back(TAG_DIGEST, Digest::SHA_2_224); |
| 78 | params->push_back(TAG_DIGEST, Digest::SHA_2_256); |
| 79 | params->push_back(TAG_DIGEST, Digest::SHA_2_384); |
| 80 | params->push_back(TAG_DIGEST, Digest::SHA_2_512); |
| 81 | params->push_back(TAG_ALL_USERS); |
| 82 | params->push_back(TAG_NO_AUTH_REQUIRED); |
| 83 | params->push_back(TAG_ORIGINATION_EXPIRE_DATETIME, LLONG_MAX); |
| 84 | params->push_back(TAG_USAGE_EXPIRE_DATETIME, LLONG_MAX); |
| 85 | params->push_back(TAG_ACTIVE_DATETIME, 0); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | uid_t get_app_id(uid_t uid) { |
| 89 | return uid % AID_USER; |
| 90 | } |
| 91 | |
| 92 | uid_t get_user_id(uid_t uid) { |
| 93 | return uid / AID_USER; |
| 94 | } |