blob: de030a0386c84db8082fed39e1e5e850baa9ebde [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
17#include "operation.h"
18
Shawn Willden117a0cc2015-06-01 07:05:41 -060019#include <keymaster/authorization_set.h>
20
21#include "key.h"
22
Shawn Willden63ac0432014-12-29 14:07:08 -070023namespace keymaster {
24
Shawn Willdend92591d2014-12-30 18:19:10 -070025bool OperationFactory::supported(keymaster_padding_t padding) const {
26 size_t padding_count;
27 const keymaster_padding_t* supported_paddings = SupportedPaddingModes(&padding_count);
28 for (size_t i = 0; i < padding_count; ++i)
29 if (padding == supported_paddings[i])
30 return true;
31 return false;
32}
33
34bool OperationFactory::supported(keymaster_block_mode_t block_mode) const {
35 size_t block_mode_count;
36 const keymaster_block_mode_t* supported_block_modes = SupportedBlockModes(&block_mode_count);
37 for (size_t i = 0; i < block_mode_count; ++i)
38 if (block_mode == supported_block_modes[i])
39 return true;
40 return false;
41}
42
43bool OperationFactory::supported(keymaster_digest_t digest) const {
44 size_t digest_count;
45 const keymaster_digest_t* supported_digests = SupportedDigests(&digest_count);
46 for (size_t i = 0; i < digest_count; ++i)
47 if (digest == supported_digests[i])
48 return true;
49 return false;
50}
51
Shawn Willden117a0cc2015-06-01 07:05:41 -060052bool OperationFactory::GetAndValidatePadding(const AuthorizationSet& begin_params, const Key& key,
53 keymaster_padding_t* padding,
54 keymaster_error_t* error) const {
55 *error = KM_ERROR_UNSUPPORTED_PADDING_MODE;
56 if (!begin_params.GetTagValue(TAG_PADDING, padding)) {
57 LOG_E("%d padding modes specified in begin params", begin_params.GetTagCount(TAG_PADDING));
58 return false;
59 } else if (!supported(*padding)) {
60 LOG_E("Padding mode %d not supported", *padding);
61 return false;
Shawn Willdenbfd9ed72015-06-11 10:51:12 -060062 } else if (
63 // If key contains KM_PAD_NONE, all padding modes are authorized.
64 !key.authorizations().Contains(TAG_PADDING, KM_PAD_NONE) &&
65 !key.authorizations().Contains(TAG_PADDING_OLD, KM_PAD_NONE) &&
66 // Otherwise the key needs to authorize the specific mode.
67 !key.authorizations().Contains(TAG_PADDING, *padding) &&
68 !key.authorizations().Contains(TAG_PADDING_OLD, *padding)) {
Shawn Willden117a0cc2015-06-01 07:05:41 -060069 LOG_E("Padding mode %d was specified, but not authorized by key", *padding);
70 *error = KM_ERROR_INCOMPATIBLE_PADDING_MODE;
71 return false;
72 }
73
74 *error = KM_ERROR_OK;
75 return true;
76}
77
78bool OperationFactory::GetAndValidateDigest(const AuthorizationSet& begin_params, const Key& key,
79 keymaster_digest_t* digest,
80 keymaster_error_t* error) const {
81 *error = KM_ERROR_UNSUPPORTED_DIGEST;
82 if (!begin_params.GetTagValue(TAG_DIGEST, digest)) {
83 LOG_E("%d digests specified in begin params", begin_params.GetTagCount(TAG_DIGEST));
84 return false;
85 } else if (!supported(*digest)) {
86 LOG_E("Digest %d not supported", *digest);
87 return false;
Shawn Willdenbfd9ed72015-06-11 10:51:12 -060088 } else if (
89 // If key contains KM_DIGEST_NONE, all digests are authorized.
90 !key.authorizations().Contains(TAG_DIGEST, KM_DIGEST_NONE) &&
91 !key.authorizations().Contains(TAG_DIGEST_OLD, KM_DIGEST_NONE) &&
92 // Otherwise the key needs to authorize the specific digest.
93 !key.authorizations().Contains(TAG_DIGEST, *digest) &&
94 !key.authorizations().Contains(TAG_DIGEST_OLD, *digest)) {
Shawn Willden117a0cc2015-06-01 07:05:41 -060095 LOG_E("Digest %d was specified, but not authorized by key", *digest);
96 *error = KM_ERROR_INCOMPATIBLE_DIGEST;
97 return false;
98 }
99 *error = KM_ERROR_OK;
100 return true;
101}
102
Shawn Willden63ac0432014-12-29 14:07:08 -0700103} // namespace keymaster