1 /*
2  * Copyright 2020 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 <keymint_support/authorization_set.h>
18 
19 #include <aidl/android/hardware/security/keymint/Algorithm.h>
20 #include <aidl/android/hardware/security/keymint/BlockMode.h>
21 #include <aidl/android/hardware/security/keymint/Digest.h>
22 #include <aidl/android/hardware/security/keymint/KeyParameter.h>
23 #include <aidl/android/hardware/security/keymint/KeyPurpose.h>
24 
25 namespace aidl::android::hardware::security::keymint {
26 
Sort()27 void AuthorizationSet::Sort() {
28     std::sort(data_.begin(), data_.end());
29 }
30 
Deduplicate()31 void AuthorizationSet::Deduplicate() {
32     if (data_.empty()) return;
33 
34     Sort();
35     std::vector<KeyParameter> result;
36 
37     auto curr = data_.begin();
38     auto prev = curr++;
39     for (; curr != data_.end(); ++prev, ++curr) {
40         if (prev->tag == Tag::INVALID) continue;
41 
42         if (*prev != *curr) {
43             result.push_back(std::move(*prev));
44         }
45     }
46     result.push_back(std::move(*prev));
47 
48     std::swap(data_, result);
49 }
50 
Union(const AuthorizationSet & other)51 void AuthorizationSet::Union(const AuthorizationSet& other) {
52     data_.insert(data_.end(), other.data_.begin(), other.data_.end());
53     Deduplicate();
54 }
55 
Subtract(const AuthorizationSet & other)56 void AuthorizationSet::Subtract(const AuthorizationSet& other) {
57     Deduplicate();
58 
59     auto i = other.begin();
60     while (i != other.end()) {
61         int pos = -1;
62         do {
63             pos = find(i->tag, pos);
64             if (pos != -1 && (*i == data_[pos])) {
65                 data_.erase(data_.begin() + pos);
66                 break;
67             }
68         } while (pos != -1);
69         ++i;
70     }
71 }
72 
operator [](int at)73 KeyParameter& AuthorizationSet::operator[](int at) {
74     return data_[at];
75 }
76 
operator [](int at) const77 const KeyParameter& AuthorizationSet::operator[](int at) const {
78     return data_[at];
79 }
80 
Clear()81 void AuthorizationSet::Clear() {
82     data_.clear();
83 }
84 
GetTagCount(Tag tag) const85 size_t AuthorizationSet::GetTagCount(Tag tag) const {
86     size_t count = 0;
87     for (int pos = -1; (pos = find(tag, pos)) != -1;) ++count;
88     return count;
89 }
90 
find(Tag tag,int begin) const91 int AuthorizationSet::find(Tag tag, int begin) const {
92     auto iter = data_.begin() + (1 + begin);
93 
94     while (iter != data_.end() && iter->tag != tag) ++iter;
95 
96     if (iter != data_.end()) return iter - data_.begin();
97     return -1;
98 }
99 
erase(int index)100 bool AuthorizationSet::erase(int index) {
101     auto pos = data_.begin() + index;
102     if (pos != data_.end()) {
103         data_.erase(pos);
104         return true;
105     }
106     return false;
107 }
108 
GetEntry(Tag tag) const109 std::optional<std::reference_wrapper<const KeyParameter>> AuthorizationSet::GetEntry(
110         Tag tag) const {
111     int pos = find(tag);
112     if (pos == -1) return {};
113     return std::reference_wrapper(data_[pos]);
114 }
115 
RsaKey(uint32_t key_size,uint64_t public_exponent)116 AuthorizationSetBuilder& AuthorizationSetBuilder::RsaKey(uint32_t key_size,
117                                                          uint64_t public_exponent) {
118     Authorization(TAG_ALGORITHM, Algorithm::RSA);
119     Authorization(TAG_KEY_SIZE, key_size);
120     Authorization(TAG_RSA_PUBLIC_EXPONENT, public_exponent);
121     return *this;
122 }
123 
EcdsaKey(uint32_t key_size)124 AuthorizationSetBuilder& AuthorizationSetBuilder::EcdsaKey(uint32_t key_size) {
125     Authorization(TAG_ALGORITHM, Algorithm::EC);
126     Authorization(TAG_KEY_SIZE, key_size);
127     return *this;
128 }
129 
EcdsaKey(EcCurve curve)130 AuthorizationSetBuilder& AuthorizationSetBuilder::EcdsaKey(EcCurve curve) {
131     Authorization(TAG_ALGORITHM, Algorithm::EC);
132     Authorization(TAG_EC_CURVE, curve);
133     return *this;
134 }
135 
AesKey(uint32_t key_size)136 AuthorizationSetBuilder& AuthorizationSetBuilder::AesKey(uint32_t key_size) {
137     Authorization(TAG_ALGORITHM, Algorithm::AES);
138     return Authorization(TAG_KEY_SIZE, key_size);
139 }
140 
TripleDesKey(uint32_t key_size)141 AuthorizationSetBuilder& AuthorizationSetBuilder::TripleDesKey(uint32_t key_size) {
142     Authorization(TAG_ALGORITHM, Algorithm::TRIPLE_DES);
143     return Authorization(TAG_KEY_SIZE, key_size);
144 }
145 
HmacKey(uint32_t key_size)146 AuthorizationSetBuilder& AuthorizationSetBuilder::HmacKey(uint32_t key_size) {
147     Authorization(TAG_ALGORITHM, Algorithm::HMAC);
148     Authorization(TAG_KEY_SIZE, key_size);
149     return SigningKey();
150 }
151 
RsaSigningKey(uint32_t key_size,uint64_t public_exponent)152 AuthorizationSetBuilder& AuthorizationSetBuilder::RsaSigningKey(uint32_t key_size,
153                                                                 uint64_t public_exponent) {
154     RsaKey(key_size, public_exponent);
155     return SigningKey();
156 }
157 
RsaEncryptionKey(uint32_t key_size,uint64_t public_exponent)158 AuthorizationSetBuilder& AuthorizationSetBuilder::RsaEncryptionKey(uint32_t key_size,
159                                                                    uint64_t public_exponent) {
160     RsaKey(key_size, public_exponent);
161     return EncryptionKey();
162 }
163 
EcdsaSigningKey(uint32_t key_size)164 AuthorizationSetBuilder& AuthorizationSetBuilder::EcdsaSigningKey(uint32_t key_size) {
165     EcdsaKey(key_size);
166     return SigningKey();
167 }
168 
EcdsaSigningKey(EcCurve curve)169 AuthorizationSetBuilder& AuthorizationSetBuilder::EcdsaSigningKey(EcCurve curve) {
170     EcdsaKey(curve);
171     return SigningKey();
172 }
173 
AesEncryptionKey(uint32_t key_size)174 AuthorizationSetBuilder& AuthorizationSetBuilder::AesEncryptionKey(uint32_t key_size) {
175     AesKey(key_size);
176     return EncryptionKey();
177 }
178 
TripleDesEncryptionKey(uint32_t key_size)179 AuthorizationSetBuilder& AuthorizationSetBuilder::TripleDesEncryptionKey(uint32_t key_size) {
180     TripleDesKey(key_size);
181     return EncryptionKey();
182 }
183 
SigningKey()184 AuthorizationSetBuilder& AuthorizationSetBuilder::SigningKey() {
185     Authorization(TAG_PURPOSE, KeyPurpose::SIGN);
186     return Authorization(TAG_PURPOSE, KeyPurpose::VERIFY);
187 }
188 
EncryptionKey()189 AuthorizationSetBuilder& AuthorizationSetBuilder::EncryptionKey() {
190     Authorization(TAG_PURPOSE, KeyPurpose::ENCRYPT);
191     return Authorization(TAG_PURPOSE, KeyPurpose::DECRYPT);
192 }
193 
AttestKey()194 AuthorizationSetBuilder& AuthorizationSetBuilder::AttestKey() {
195     return Authorization(TAG_PURPOSE, KeyPurpose::ATTEST_KEY);
196 }
197 
NoDigestOrPadding()198 AuthorizationSetBuilder& AuthorizationSetBuilder::NoDigestOrPadding() {
199     Authorization(TAG_DIGEST, Digest::NONE);
200     return Authorization(TAG_PADDING, PaddingMode::NONE);
201 }
202 
EcbMode()203 AuthorizationSetBuilder& AuthorizationSetBuilder::EcbMode() {
204     return Authorization(TAG_BLOCK_MODE, BlockMode::ECB);
205 }
206 
GcmModeMinMacLen(uint32_t minMacLength)207 AuthorizationSetBuilder& AuthorizationSetBuilder::GcmModeMinMacLen(uint32_t minMacLength) {
208     return BlockMode(BlockMode::GCM)
209             .Padding(PaddingMode::NONE)
210             .Authorization(TAG_MIN_MAC_LENGTH, minMacLength);
211 }
212 
GcmModeMacLen(uint32_t macLength)213 AuthorizationSetBuilder& AuthorizationSetBuilder::GcmModeMacLen(uint32_t macLength) {
214     return BlockMode(BlockMode::GCM)
215             .Padding(PaddingMode::NONE)
216             .Authorization(TAG_MAC_LENGTH, macLength);
217 }
218 
BlockMode(std::initializer_list<aidl::android::hardware::security::keymint::BlockMode> blockModes)219 AuthorizationSetBuilder& AuthorizationSetBuilder::BlockMode(
220         std::initializer_list<aidl::android::hardware::security::keymint::BlockMode> blockModes) {
221     for (auto mode : blockModes) {
222         push_back(TAG_BLOCK_MODE, mode);
223     }
224     return *this;
225 }
226 
Digest(std::vector<keymint::Digest> digests)227 AuthorizationSetBuilder& AuthorizationSetBuilder::Digest(std::vector<keymint::Digest> digests) {
228     for (auto digest : digests) {
229         push_back(TAG_DIGEST, digest);
230     }
231     return *this;
232 }
233 
OaepMGFDigest(const std::vector<android::hardware::security::keymint::Digest> & digests)234 AuthorizationSetBuilder& AuthorizationSetBuilder::OaepMGFDigest(
235         const std::vector<android::hardware::security::keymint::Digest>& digests) {
236     for (auto digest : digests) {
237         push_back(TAG_RSA_OAEP_MGF_DIGEST, digest);
238     }
239     return *this;
240 }
241 
Padding(std::initializer_list<PaddingMode> paddingModes)242 AuthorizationSetBuilder& AuthorizationSetBuilder::Padding(
243         std::initializer_list<PaddingMode> paddingModes) {
244     for (auto paddingMode : paddingModes) {
245         push_back(TAG_PADDING, paddingMode);
246     }
247     return *this;
248 }
249 
SetDefaultValidity()250 AuthorizationSetBuilder& AuthorizationSetBuilder::SetDefaultValidity() {
251     // Per RFC 5280 4.1.2.5, an undefined expiration (not-after) field should be set to
252     // GeneralizedTime 999912312359559, which is 253402300799000 ms from Jan 1, 1970.
253     constexpr uint64_t kUndefinedExpirationDateTime = 253402300799000;
254     Authorization(TAG_CERTIFICATE_NOT_BEFORE, 0);
255     return Authorization(TAG_CERTIFICATE_NOT_AFTER, kUndefinedExpirationDateTime);
256 }
257 
258 }  // namespace aidl::android::hardware::security::keymint
259