Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
Shawn Willden | 117a0cc | 2015-06-01 07:05:41 -0600 | [diff] [blame] | 17 | #include <keymaster/authorization_set.h> |
Janis Danisevskis | f54cc93 | 2017-05-10 15:29:10 -0700 | [diff] [blame] | 18 | #include <keymaster/key.h> |
| 19 | #include <keymaster/operation.h> |
Shawn Willden | 117a0cc | 2015-06-01 07:05:41 -0600 | [diff] [blame] | 20 | |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 21 | namespace keymaster { |
| 22 | |
Shawn Willden | d92591d | 2014-12-30 18:19:10 -0700 | [diff] [blame] | 23 | bool OperationFactory::supported(keymaster_padding_t padding) const { |
| 24 | size_t padding_count; |
| 25 | const keymaster_padding_t* supported_paddings = SupportedPaddingModes(&padding_count); |
| 26 | for (size_t i = 0; i < padding_count; ++i) |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 27 | if (padding == supported_paddings[i]) return true; |
Shawn Willden | d92591d | 2014-12-30 18:19:10 -0700 | [diff] [blame] | 28 | return false; |
| 29 | } |
| 30 | |
| 31 | bool OperationFactory::supported(keymaster_block_mode_t block_mode) const { |
| 32 | size_t block_mode_count; |
| 33 | const keymaster_block_mode_t* supported_block_modes = SupportedBlockModes(&block_mode_count); |
| 34 | for (size_t i = 0; i < block_mode_count; ++i) |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 35 | if (block_mode == supported_block_modes[i]) return true; |
Shawn Willden | d92591d | 2014-12-30 18:19:10 -0700 | [diff] [blame] | 36 | return false; |
| 37 | } |
| 38 | |
| 39 | bool OperationFactory::supported(keymaster_digest_t digest) const { |
| 40 | size_t digest_count; |
| 41 | const keymaster_digest_t* supported_digests = SupportedDigests(&digest_count); |
| 42 | for (size_t i = 0; i < digest_count; ++i) |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 43 | if (digest == supported_digests[i]) return true; |
Shawn Willden | d92591d | 2014-12-30 18:19:10 -0700 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | |
Shawn Willden | 294a2db | 2015-06-17 11:20:56 -0600 | [diff] [blame] | 47 | inline bool is_public_key_algorithm(keymaster_algorithm_t algorithm) { |
| 48 | switch (algorithm) { |
| 49 | case KM_ALGORITHM_HMAC: |
| 50 | case KM_ALGORITHM_AES: |
Shawn Willden | 7efc772 | 2018-01-08 22:00:12 -0700 | [diff] [blame] | 51 | case KM_ALGORITHM_TRIPLE_DES: |
Shawn Willden | 294a2db | 2015-06-17 11:20:56 -0600 | [diff] [blame] | 52 | return false; |
| 53 | case KM_ALGORITHM_RSA: |
| 54 | case KM_ALGORITHM_EC: |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | // Unreachable. |
| 59 | assert(false); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | bool OperationFactory::is_public_key_operation() const { |
| 64 | KeyType key_type = registry_key(); |
| 65 | |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 66 | if (!is_public_key_algorithm(key_type.algorithm)) return false; |
Shawn Willden | 294a2db | 2015-06-17 11:20:56 -0600 | [diff] [blame] | 67 | |
| 68 | switch (key_type.purpose) { |
| 69 | case KM_PURPOSE_VERIFY: |
| 70 | case KM_PURPOSE_ENCRYPT: |
Shawn Willden | dd7e8a0 | 2018-01-12 13:03:37 -0700 | [diff] [blame] | 71 | case KM_PURPOSE_WRAP: |
Shawn Willden | 294a2db | 2015-06-17 11:20:56 -0600 | [diff] [blame] | 72 | return true; |
| 73 | case KM_PURPOSE_SIGN: |
| 74 | case KM_PURPOSE_DECRYPT: |
Shawn Willden | d4c83db | 2015-12-22 14:40:15 -0700 | [diff] [blame] | 75 | case KM_PURPOSE_DERIVE_KEY: |
David Zeuthen | a843b3d | 2021-01-19 16:01:00 -0500 | [diff] [blame] | 76 | case KM_PURPOSE_AGREE_KEY: |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 77 | case KM_PURPOSE_ATTEST_KEY: |
Shawn Willden | 294a2db | 2015-06-17 11:20:56 -0600 | [diff] [blame] | 78 | return false; |
| 79 | }; |
| 80 | |
| 81 | // Unreachable. |
| 82 | assert(false); |
| 83 | return false; |
| 84 | } |
| 85 | |
Shawn Willden | 117a0cc | 2015-06-01 07:05:41 -0600 | [diff] [blame] | 86 | bool OperationFactory::GetAndValidatePadding(const AuthorizationSet& begin_params, const Key& key, |
| 87 | keymaster_padding_t* padding, |
| 88 | keymaster_error_t* error) const { |
| 89 | *error = KM_ERROR_UNSUPPORTED_PADDING_MODE; |
| 90 | if (!begin_params.GetTagValue(TAG_PADDING, padding)) { |
| 91 | LOG_E("%d padding modes specified in begin params", begin_params.GetTagCount(TAG_PADDING)); |
| 92 | return false; |
| 93 | } else if (!supported(*padding)) { |
| 94 | LOG_E("Padding mode %d not supported", *padding); |
| 95 | return false; |
Shawn Willden | bfd9ed7 | 2015-06-11 10:51:12 -0600 | [diff] [blame] | 96 | } else if ( |
Shawn Willden | 294a2db | 2015-06-17 11:20:56 -0600 | [diff] [blame] | 97 | // If it's a public key operation, all padding modes are authorized. |
| 98 | !is_public_key_operation() && |
Shawn Willden | bfd9ed7 | 2015-06-11 10:51:12 -0600 | [diff] [blame] | 99 | // Otherwise the key needs to authorize the specific mode. |
| 100 | !key.authorizations().Contains(TAG_PADDING, *padding) && |
| 101 | !key.authorizations().Contains(TAG_PADDING_OLD, *padding)) { |
Shawn Willden | 117a0cc | 2015-06-01 07:05:41 -0600 | [diff] [blame] | 102 | LOG_E("Padding mode %d was specified, but not authorized by key", *padding); |
| 103 | *error = KM_ERROR_INCOMPATIBLE_PADDING_MODE; |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | *error = KM_ERROR_OK; |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | bool OperationFactory::GetAndValidateDigest(const AuthorizationSet& begin_params, const Key& key, |
| 112 | keymaster_digest_t* digest, |
| 113 | keymaster_error_t* error) const { |
Matthew Maurer | 90174bd | 2019-05-10 16:31:00 -0700 | [diff] [blame] | 114 | return GetAndValidateDigest(begin_params, key, digest, error, false); |
| 115 | } |
| 116 | |
| 117 | bool OperationFactory::GetAndValidateDigest(const AuthorizationSet& begin_params, const Key& key, |
| 118 | keymaster_digest_t* digest, keymaster_error_t* error, |
| 119 | bool require_explicit_digest) const { |
Shawn Willden | 117a0cc | 2015-06-01 07:05:41 -0600 | [diff] [blame] | 120 | *error = KM_ERROR_UNSUPPORTED_DIGEST; |
| 121 | if (!begin_params.GetTagValue(TAG_DIGEST, digest)) { |
Matthew Maurer | 90174bd | 2019-05-10 16:31:00 -0700 | [diff] [blame] | 122 | if (require_explicit_digest) { |
| 123 | return false; |
| 124 | } |
Janis Danisevskis | 5a44a2f | 2018-08-03 13:46:37 -0700 | [diff] [blame] | 125 | if (key.authorizations().Contains(TAG_DIGEST, KM_DIGEST_NONE)) { |
| 126 | *digest = KM_DIGEST_NONE; |
| 127 | } else { |
| 128 | LOG_E("%d digests specified in begin params and NONE not authorized", |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 129 | begin_params.GetTagCount(TAG_DIGEST)); |
Janis Danisevskis | 5a44a2f | 2018-08-03 13:46:37 -0700 | [diff] [blame] | 130 | return false; |
| 131 | } |
Shawn Willden | 117a0cc | 2015-06-01 07:05:41 -0600 | [diff] [blame] | 132 | } else if (!supported(*digest)) { |
| 133 | LOG_E("Digest %d not supported", *digest); |
| 134 | return false; |
Shawn Willden | bfd9ed7 | 2015-06-11 10:51:12 -0600 | [diff] [blame] | 135 | } else if ( |
Shawn Willden | 294a2db | 2015-06-17 11:20:56 -0600 | [diff] [blame] | 136 | // If it's a public key operation, all digests are authorized. |
| 137 | !is_public_key_operation() && |
Shawn Willden | bfd9ed7 | 2015-06-11 10:51:12 -0600 | [diff] [blame] | 138 | // Otherwise the key needs to authorize the specific digest. |
| 139 | !key.authorizations().Contains(TAG_DIGEST, *digest) && |
| 140 | !key.authorizations().Contains(TAG_DIGEST_OLD, *digest)) { |
Shawn Willden | 117a0cc | 2015-06-01 07:05:41 -0600 | [diff] [blame] | 141 | LOG_E("Digest %d was specified, but not authorized by key", *digest); |
| 142 | *error = KM_ERROR_INCOMPATIBLE_DIGEST; |
| 143 | return false; |
| 144 | } |
| 145 | *error = KM_ERROR_OK; |
| 146 | return true; |
| 147 | } |
| 148 | |
Shawn Willden | cb647fe | 2016-01-27 12:59:13 -0700 | [diff] [blame] | 149 | keymaster_error_t Operation::UpdateForFinish(const AuthorizationSet& input_params, |
| 150 | const Buffer& input) { |
| 151 | if (!input_params.empty() || input.available_read()) { |
| 152 | size_t input_consumed; |
| 153 | Buffer output; |
| 154 | AuthorizationSet output_params; |
| 155 | keymaster_error_t error = |
| 156 | Update(input_params, input, &output_params, &output, &input_consumed); |
Shawn Willden | 3287352 | 2020-12-14 22:29:46 -0700 | [diff] [blame] | 157 | if (error != KM_ERROR_OK) return error; |
Shawn Willden | cb647fe | 2016-01-27 12:59:13 -0700 | [diff] [blame] | 158 | assert(input_consumed == input.available_read()); |
| 159 | assert(output_params.empty()); |
| 160 | assert(output.available_read() == 0); |
| 161 | } |
| 162 | |
| 163 | return KM_ERROR_OK; |
| 164 | } |
| 165 | |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 166 | } // namespace keymaster |