blob: 8d94b3cc2ab936c3cc1fb0516239437a4bb8b43a [file] [log] [blame]
Shawn Willden63ac0432014-12-29 14:07:08 -07001/*
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 Willden117a0cc2015-06-01 07:05:41 -060017#include <keymaster/authorization_set.h>
Janis Danisevskisf54cc932017-05-10 15:29:10 -070018#include <keymaster/key.h>
19#include <keymaster/operation.h>
Shawn Willden117a0cc2015-06-01 07:05:41 -060020
Shawn Willden63ac0432014-12-29 14:07:08 -070021namespace keymaster {
22
Shawn Willdend92591d2014-12-30 18:19:10 -070023bool 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 Willden32873522020-12-14 22:29:46 -070027 if (padding == supported_paddings[i]) return true;
Shawn Willdend92591d2014-12-30 18:19:10 -070028 return false;
29}
30
31bool 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 Willden32873522020-12-14 22:29:46 -070035 if (block_mode == supported_block_modes[i]) return true;
Shawn Willdend92591d2014-12-30 18:19:10 -070036 return false;
37}
38
39bool 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 Willden32873522020-12-14 22:29:46 -070043 if (digest == supported_digests[i]) return true;
Shawn Willdend92591d2014-12-30 18:19:10 -070044 return false;
45}
46
Shawn Willden294a2db2015-06-17 11:20:56 -060047inline bool is_public_key_algorithm(keymaster_algorithm_t algorithm) {
48 switch (algorithm) {
49 case KM_ALGORITHM_HMAC:
50 case KM_ALGORITHM_AES:
Shawn Willden7efc7722018-01-08 22:00:12 -070051 case KM_ALGORITHM_TRIPLE_DES:
Shawn Willden294a2db2015-06-17 11:20:56 -060052 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
63bool OperationFactory::is_public_key_operation() const {
64 KeyType key_type = registry_key();
65
Shawn Willden32873522020-12-14 22:29:46 -070066 if (!is_public_key_algorithm(key_type.algorithm)) return false;
Shawn Willden294a2db2015-06-17 11:20:56 -060067
68 switch (key_type.purpose) {
69 case KM_PURPOSE_VERIFY:
70 case KM_PURPOSE_ENCRYPT:
Shawn Willdendd7e8a02018-01-12 13:03:37 -070071 case KM_PURPOSE_WRAP:
Shawn Willden294a2db2015-06-17 11:20:56 -060072 return true;
73 case KM_PURPOSE_SIGN:
74 case KM_PURPOSE_DECRYPT:
Shawn Willdend4c83db2015-12-22 14:40:15 -070075 case KM_PURPOSE_DERIVE_KEY:
David Zeuthena843b3d2021-01-19 16:01:00 -050076 case KM_PURPOSE_AGREE_KEY:
Shawn Willden44c38882020-12-21 18:35:13 -070077 case KM_PURPOSE_ATTEST_KEY:
Shawn Willden294a2db2015-06-17 11:20:56 -060078 return false;
79 };
80
81 // Unreachable.
82 assert(false);
83 return false;
84}
85
Shawn Willden117a0cc2015-06-01 07:05:41 -060086bool 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 Willdenbfd9ed72015-06-11 10:51:12 -060096 } else if (
Shawn Willden294a2db2015-06-17 11:20:56 -060097 // If it's a public key operation, all padding modes are authorized.
98 !is_public_key_operation() &&
Shawn Willdenbfd9ed72015-06-11 10:51:12 -060099 // 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 Willden117a0cc2015-06-01 07:05:41 -0600102 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
111bool OperationFactory::GetAndValidateDigest(const AuthorizationSet& begin_params, const Key& key,
112 keymaster_digest_t* digest,
113 keymaster_error_t* error) const {
Matthew Maurer90174bd2019-05-10 16:31:00 -0700114 return GetAndValidateDigest(begin_params, key, digest, error, false);
115}
116
117bool 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 Willden117a0cc2015-06-01 07:05:41 -0600120 *error = KM_ERROR_UNSUPPORTED_DIGEST;
121 if (!begin_params.GetTagValue(TAG_DIGEST, digest)) {
Matthew Maurer90174bd2019-05-10 16:31:00 -0700122 if (require_explicit_digest) {
123 return false;
124 }
Janis Danisevskis5a44a2f2018-08-03 13:46:37 -0700125 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 Willden32873522020-12-14 22:29:46 -0700129 begin_params.GetTagCount(TAG_DIGEST));
Janis Danisevskis5a44a2f2018-08-03 13:46:37 -0700130 return false;
131 }
Shawn Willden117a0cc2015-06-01 07:05:41 -0600132 } else if (!supported(*digest)) {
133 LOG_E("Digest %d not supported", *digest);
134 return false;
Shawn Willdenbfd9ed72015-06-11 10:51:12 -0600135 } else if (
Shawn Willden294a2db2015-06-17 11:20:56 -0600136 // If it's a public key operation, all digests are authorized.
137 !is_public_key_operation() &&
Shawn Willdenbfd9ed72015-06-11 10:51:12 -0600138 // 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 Willden117a0cc2015-06-01 07:05:41 -0600141 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 Willdencb647fe2016-01-27 12:59:13 -0700149keymaster_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 Willden32873522020-12-14 22:29:46 -0700157 if (error != KM_ERROR_OK) return error;
Shawn Willdencb647fe2016-01-27 12:59:13 -0700158 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 Willden63ac0432014-12-29 14:07:08 -0700166} // namespace keymaster