• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 <keymaster/authorization_set.h>
18 
19 #include <assert.h>
20 #include <stddef.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include <new>
25 
26 #include <keymaster/android_keymaster_utils.h>
27 #include <keymaster/logger.h>
28 
29 namespace keymaster {
30 
is_blob_tag(keymaster_tag_t tag)31 static inline bool is_blob_tag(keymaster_tag_t tag) {
32     return (keymaster_tag_get_type(tag) == KM_BYTES || keymaster_tag_get_type(tag) == KM_BIGNUM);
33 }
34 
35 const size_t STARTING_ELEMS_CAPACITY = 8;
36 
AuthorizationSet(AuthorizationSetBuilder & builder)37 AuthorizationSet::AuthorizationSet(AuthorizationSetBuilder& builder) {
38     elems_ = builder.set.elems_;
39     builder.set.elems_ = NULL;
40 
41     elems_size_ = builder.set.elems_size_;
42     builder.set.elems_size_ = 0;
43 
44     elems_capacity_ = builder.set.elems_capacity_;
45     builder.set.elems_capacity_ = 0;
46 
47     indirect_data_ = builder.set.indirect_data_;
48     builder.set.indirect_data_ = NULL;
49 
50     indirect_data_capacity_ = builder.set.indirect_data_capacity_;
51     builder.set.indirect_data_capacity_ = 0;
52 
53     indirect_data_size_ = builder.set.indirect_data_size_;
54     builder.set.indirect_data_size_ = 0;
55 
56     error_ = builder.set.error_;
57     builder.set.error_ = OK;
58 }
59 
~AuthorizationSet()60 AuthorizationSet::~AuthorizationSet() {
61     FreeData();
62 }
63 
reserve_elems(size_t count)64 bool AuthorizationSet::reserve_elems(size_t count) {
65     if (is_valid() != OK)
66         return false;
67 
68     if (count >= elems_capacity_) {
69         keymaster_key_param_t* new_elems = new (std::nothrow) keymaster_key_param_t[count];
70         if (new_elems == NULL) {
71             set_invalid(ALLOCATION_FAILURE);
72             return false;
73         }
74         memcpy(new_elems, elems_, sizeof(*elems_) * elems_size_);
75         delete[] elems_;
76         elems_ = new_elems;
77         elems_capacity_ = count;
78     }
79     return true;
80 }
81 
reserve_indirect(size_t length)82 bool AuthorizationSet::reserve_indirect(size_t length) {
83     if (is_valid() != OK)
84         return false;
85 
86     if (length > indirect_data_capacity_) {
87         uint8_t* new_data = new (std::nothrow) uint8_t[length];
88         if (new_data == NULL) {
89             set_invalid(ALLOCATION_FAILURE);
90             return false;
91         }
92         memcpy(new_data, indirect_data_, indirect_data_size_);
93 
94         // Fix up the data pointers to point into the new region.
95         for (size_t i = 0; i < elems_size_; ++i) {
96             if (is_blob_tag(elems_[i].tag))
97                 elems_[i].blob.data = new_data + (elems_[i].blob.data - indirect_data_);
98         }
99         delete[] indirect_data_;
100         indirect_data_ = new_data;
101         indirect_data_capacity_ = length;
102     }
103     return true;
104 }
105 
MoveFrom(AuthorizationSet & set)106 void AuthorizationSet::MoveFrom(AuthorizationSet& set) {
107     elems_ = set.elems_;
108     elems_size_ = set.elems_size_;
109     elems_capacity_ = set.elems_capacity_;
110     indirect_data_ = set.indirect_data_;
111     indirect_data_size_ = set.indirect_data_size_;
112     indirect_data_capacity_ = set.indirect_data_capacity_;
113     error_ = set.error_;
114     set.elems_ = nullptr;
115     set.elems_size_ = 0;
116     set.elems_capacity_ = 0;
117     set.indirect_data_ = nullptr;
118     set.indirect_data_size_ = 0;
119     set.indirect_data_capacity_ = 0;
120     set.error_ = OK;
121 }
122 
Reinitialize(const keymaster_key_param_t * elems,const size_t count)123 bool AuthorizationSet::Reinitialize(const keymaster_key_param_t* elems, const size_t count) {
124     FreeData();
125 
126     if (elems == NULL || count == 0) {
127         error_ = OK;
128         return true;
129     }
130 
131     if (!reserve_elems(count))
132         return false;
133 
134     if (!reserve_indirect(ComputeIndirectDataSize(elems, count)))
135         return false;
136 
137     memcpy(elems_, elems, sizeof(keymaster_key_param_t) * count);
138     elems_size_ = count;
139     CopyIndirectData();
140     error_ = OK;
141     return true;
142 }
143 
set_invalid(Error error)144 void AuthorizationSet::set_invalid(Error error) {
145     FreeData();
146     error_ = error;
147 }
148 
Sort()149 void AuthorizationSet::Sort() {
150     qsort(elems_, elems_size_, sizeof(*elems_),
151           reinterpret_cast<int (*)(const void*, const void*)>(keymaster_param_compare));
152 }
153 
Deduplicate()154 void AuthorizationSet::Deduplicate() {
155     Sort();
156 
157     size_t invalid_count = 0;
158     for (size_t i = 1; i < size(); ++i) {
159         if (elems_[i - 1].tag == KM_TAG_INVALID)
160             ++invalid_count;
161         else if (keymaster_param_compare(elems_ + i - 1, elems_ + i) == 0) {
162             // Mark dups as invalid.  Note that this "leaks" the data referenced by KM_BYTES and
163             // KM_BIGNUM entries, but those are just pointers into indirect_data_, so it will all
164             // get cleaned up.
165             elems_[i - 1].tag = KM_TAG_INVALID;
166             ++invalid_count;
167         }
168     }
169     if (size() > 0 && elems_[size() - 1].tag == KM_TAG_INVALID)
170         ++invalid_count;
171 
172     if (invalid_count == 0)
173         return;
174 
175     Sort();
176 
177     // Since KM_TAG_INVALID == 0, all of the invalid entries are first.
178     elems_size_ -= invalid_count;
179     memmove(elems_, elems_ + invalid_count, size() * sizeof(*elems_));
180 }
181 
Union(const keymaster_key_param_set_t & set)182 void AuthorizationSet::Union(const keymaster_key_param_set_t& set) {
183     if (set.length == 0)
184         return;
185 
186     push_back(set);
187     Deduplicate();
188 }
189 
Difference(const keymaster_key_param_set_t & set)190 void AuthorizationSet::Difference(const keymaster_key_param_set_t& set) {
191     if (set.length == 0)
192         return;
193 
194     Deduplicate();
195 
196     for (size_t i = 0; i < set.length; i++) {
197         int index = -1;
198         do {
199             index = find(set.params[i].tag, index);
200             if (index != -1 && keymaster_param_compare(&elems_[index], &set.params[i]) == 0) {
201                 erase(index);
202                 break;
203             }
204         } while (index != -1);
205     }
206 }
207 
CopyToParamSet(keymaster_key_param_set_t * set) const208 void AuthorizationSet::CopyToParamSet(keymaster_key_param_set_t* set) const {
209     assert(set);
210 
211     set->length = size();
212     set->params =
213         reinterpret_cast<keymaster_key_param_t*>(malloc(sizeof(keymaster_key_param_t) * size()));
214 
215     for (size_t i = 0; i < size(); ++i) {
216         const keymaster_key_param_t src = (*this)[i];
217         keymaster_key_param_t& dst(set->params[i]);
218 
219         dst = src;
220         keymaster_tag_type_t type = keymaster_tag_get_type(src.tag);
221         if (type == KM_BIGNUM || type == KM_BYTES) {
222             void* tmp = malloc(src.blob.data_length);
223             memcpy(tmp, src.blob.data, src.blob.data_length);
224             dst.blob.data = reinterpret_cast<uint8_t*>(tmp);
225         }
226     }
227 }
228 
find(keymaster_tag_t tag,int begin) const229 int AuthorizationSet::find(keymaster_tag_t tag, int begin) const {
230     if (is_valid() != OK)
231         return -1;
232 
233     int i = ++begin;
234     while (i < (int)elems_size_ && elems_[i].tag != tag)
235         ++i;
236     if (i == (int)elems_size_)
237         return -1;
238     else
239         return i;
240 }
241 
erase(int index)242 bool AuthorizationSet::erase(int index) {
243     if (index < 0 || index >= static_cast<int>(size()))
244         return false;
245 
246     --elems_size_;
247     for (size_t i = index; i < elems_size_; ++i)
248         elems_[i] = elems_[i + 1];
249     return true;
250 }
251 
252 keymaster_key_param_t empty_param = {KM_TAG_INVALID, {}};
operator [](int at)253 keymaster_key_param_t& AuthorizationSet::operator[](int at) {
254     if (is_valid() == OK && at < (int)elems_size_) {
255         return elems_[at];
256     }
257     empty_param = {KM_TAG_INVALID, {}};
258     return empty_param;
259 }
260 
operator [](int at) const261 keymaster_key_param_t AuthorizationSet::operator[](int at) const {
262     if (is_valid() == OK && at < (int)elems_size_) {
263         return elems_[at];
264     }
265     empty_param = {KM_TAG_INVALID, {}};
266     return empty_param;
267 }
268 
push_back(const keymaster_key_param_set_t & set)269 bool AuthorizationSet::push_back(const keymaster_key_param_set_t& set) {
270     if (is_valid() != OK)
271         return false;
272 
273     if (!reserve_elems(elems_size_ + set.length))
274         return false;
275 
276     if (!reserve_indirect(indirect_data_size_ + ComputeIndirectDataSize(set.params, set.length)))
277         return false;
278 
279     for (size_t i = 0; i < set.length; ++i)
280         if (!push_back(set.params[i]))
281             return false;
282 
283     return true;
284 }
285 
push_back(keymaster_key_param_t elem)286 bool AuthorizationSet::push_back(keymaster_key_param_t elem) {
287     if (is_valid() != OK)
288         return false;
289 
290     if (elems_size_ >= elems_capacity_)
291         if (!reserve_elems(elems_capacity_ ? elems_capacity_ * 2 : STARTING_ELEMS_CAPACITY))
292             return false;
293 
294     if (is_blob_tag(elem.tag)) {
295         if (indirect_data_capacity_ - indirect_data_size_ < elem.blob.data_length)
296             if (!reserve_indirect(2 * (indirect_data_capacity_ + elem.blob.data_length)))
297                 return false;
298 
299         memcpy(indirect_data_ + indirect_data_size_, elem.blob.data, elem.blob.data_length);
300         elem.blob.data = indirect_data_ + indirect_data_size_;
301         indirect_data_size_ += elem.blob.data_length;
302     }
303 
304     elems_[elems_size_++] = elem;
305     return true;
306 }
307 
serialized_size(const keymaster_key_param_t & param)308 static size_t serialized_size(const keymaster_key_param_t& param) {
309     switch (keymaster_tag_get_type(param.tag)) {
310     case KM_INVALID:
311         return sizeof(uint32_t);
312     case KM_ENUM:
313     case KM_ENUM_REP:
314     case KM_UINT:
315     case KM_UINT_REP:
316         return sizeof(uint32_t) * 2;
317     case KM_ULONG:
318     case KM_ULONG_REP:
319     case KM_DATE:
320         return sizeof(uint32_t) + sizeof(uint64_t);
321     case KM_BOOL:
322         return sizeof(uint32_t) + 1;
323     case KM_BIGNUM:
324     case KM_BYTES:
325         return sizeof(uint32_t) * 3;
326     }
327 
328     return sizeof(uint32_t);
329 }
330 
serialize(const keymaster_key_param_t & param,uint8_t * buf,const uint8_t * end,const uint8_t * indirect_base)331 static uint8_t* serialize(const keymaster_key_param_t& param, uint8_t* buf, const uint8_t* end,
332                           const uint8_t* indirect_base) {
333     buf = append_uint32_to_buf(buf, end, param.tag);
334     switch (keymaster_tag_get_type(param.tag)) {
335     case KM_INVALID:
336         break;
337     case KM_ENUM:
338     case KM_ENUM_REP:
339         buf = append_uint32_to_buf(buf, end, param.enumerated);
340         break;
341     case KM_UINT:
342     case KM_UINT_REP:
343         buf = append_uint32_to_buf(buf, end, param.integer);
344         break;
345     case KM_ULONG:
346     case KM_ULONG_REP:
347         buf = append_uint64_to_buf(buf, end, param.long_integer);
348         break;
349     case KM_DATE:
350         buf = append_uint64_to_buf(buf, end, param.date_time);
351         break;
352     case KM_BOOL:
353         if (buf < end)
354             *buf = static_cast<uint8_t>(param.boolean);
355         buf++;
356         break;
357     case KM_BIGNUM:
358     case KM_BYTES:
359         buf = append_uint32_to_buf(buf, end, param.blob.data_length);
360         buf = append_uint32_to_buf(buf, end, param.blob.data - indirect_base);
361         break;
362     }
363     return buf;
364 }
365 
deserialize(keymaster_key_param_t * param,const uint8_t ** buf_ptr,const uint8_t * end,const uint8_t * indirect_base,const uint8_t * indirect_end)366 static bool deserialize(keymaster_key_param_t* param, const uint8_t** buf_ptr, const uint8_t* end,
367                         const uint8_t* indirect_base, const uint8_t* indirect_end) {
368     if (!copy_uint32_from_buf(buf_ptr, end, &param->tag))
369         return false;
370 
371     switch (keymaster_tag_get_type(param->tag)) {
372     case KM_INVALID:
373         return false;
374     case KM_ENUM:
375     case KM_ENUM_REP:
376         return copy_uint32_from_buf(buf_ptr, end, &param->enumerated);
377     case KM_UINT:
378     case KM_UINT_REP:
379         return copy_uint32_from_buf(buf_ptr, end, &param->integer);
380     case KM_ULONG:
381     case KM_ULONG_REP:
382         return copy_uint64_from_buf(buf_ptr, end, &param->long_integer);
383     case KM_DATE:
384         return copy_uint64_from_buf(buf_ptr, end, &param->date_time);
385         break;
386     case KM_BOOL:
387         if (*buf_ptr < end) {
388             param->boolean = static_cast<bool>(**buf_ptr);
389             (*buf_ptr)++;
390             return true;
391         }
392         return false;
393 
394     case KM_BIGNUM:
395     case KM_BYTES: {
396         uint32_t offset;
397         if (!copy_uint32_from_buf(buf_ptr, end, &param->blob.data_length) ||
398             !copy_uint32_from_buf(buf_ptr, end, &offset))
399             return false;
400         if (param->blob.data_length + offset < param->blob.data_length ||  // Overflow check
401             static_cast<ptrdiff_t>(offset) > indirect_end - indirect_base ||
402             static_cast<ptrdiff_t>(offset + param->blob.data_length) > indirect_end - indirect_base)
403             return false;
404         param->blob.data = indirect_base + offset;
405         return true;
406     }
407     }
408 
409     return false;
410 }
411 
SerializedSizeOfElements() const412 size_t AuthorizationSet::SerializedSizeOfElements() const {
413     size_t size = 0;
414     for (size_t i = 0; i < elems_size_; ++i) {
415         size += serialized_size(elems_[i]);
416     }
417     return size;
418 }
419 
SerializedSize() const420 size_t AuthorizationSet::SerializedSize() const {
421     return sizeof(uint32_t) +           // Size of indirect_data_
422            indirect_data_size_ +        // indirect_data_
423            sizeof(uint32_t) +           // Number of elems_
424            sizeof(uint32_t) +           // Size of elems_
425            SerializedSizeOfElements();  // elems_
426 }
427 
Serialize(uint8_t * buf,const uint8_t * end) const428 uint8_t* AuthorizationSet::Serialize(uint8_t* buf, const uint8_t* end) const {
429     buf = append_size_and_data_to_buf(buf, end, indirect_data_, indirect_data_size_);
430     buf = append_uint32_to_buf(buf, end, elems_size_);
431     buf = append_uint32_to_buf(buf, end, SerializedSizeOfElements());
432     for (size_t i = 0; i < elems_size_; ++i) {
433         buf = serialize(elems_[i], buf, end, indirect_data_);
434     }
435     return buf;
436 }
437 
DeserializeIndirectData(const uint8_t ** buf_ptr,const uint8_t * end)438 bool AuthorizationSet::DeserializeIndirectData(const uint8_t** buf_ptr, const uint8_t* end) {
439     UniquePtr<uint8_t[]> indirect_buf;
440     if (!copy_size_and_data_from_buf(buf_ptr, end, &indirect_data_size_, &indirect_buf)) {
441         LOG_E("Malformed data found in AuthorizationSet deserialization", 0);
442         set_invalid(MALFORMED_DATA);
443         return false;
444     }
445     indirect_data_ = indirect_buf.release();
446     return true;
447 }
448 
DeserializeElementsData(const uint8_t ** buf_ptr,const uint8_t * end)449 bool AuthorizationSet::DeserializeElementsData(const uint8_t** buf_ptr, const uint8_t* end) {
450     uint32_t elements_count;
451     uint32_t elements_size;
452     if (!copy_uint32_from_buf(buf_ptr, end, &elements_count) ||
453         !copy_uint32_from_buf(buf_ptr, end, &elements_size)) {
454         LOG_E("Malformed data found in AuthorizationSet deserialization", 0);
455         set_invalid(MALFORMED_DATA);
456         return false;
457     }
458 
459     // Note that the following validation of elements_count is weak, but it prevents allocation of
460     // elems_ arrays which are clearly too large to be reasonable.
461     if (static_cast<ptrdiff_t>(elements_size) > end - *buf_ptr ||
462         elements_count * sizeof(uint32_t) > elements_size ||
463         *buf_ptr + (elements_count * sizeof(*elems_)) < *buf_ptr) {
464         LOG_E("Malformed data found in AuthorizationSet deserialization", 0);
465         set_invalid(MALFORMED_DATA);
466         return false;
467     }
468 
469     if (!reserve_elems(elements_count))
470         return false;
471 
472     uint8_t* indirect_end = indirect_data_ + indirect_data_size_;
473     const uint8_t* elements_end = *buf_ptr + elements_size;
474     for (size_t i = 0; i < elements_count; ++i) {
475         if (!deserialize(elems_ + i, buf_ptr, elements_end, indirect_data_, indirect_end)) {
476             LOG_E("Malformed data found in AuthorizationSet deserialization", 0);
477             set_invalid(MALFORMED_DATA);
478             return false;
479         }
480     }
481     elems_size_ = elements_count;
482     return true;
483 }
484 
Deserialize(const uint8_t ** buf_ptr,const uint8_t * end)485 bool AuthorizationSet::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
486     FreeData();
487 
488     if (!DeserializeIndirectData(buf_ptr, end) || !DeserializeElementsData(buf_ptr, end))
489         return false;
490 
491     if (indirect_data_size_ != ComputeIndirectDataSize(elems_, elems_size_)) {
492         LOG_E("Malformed data found in AuthorizationSet deserialization", 0);
493         set_invalid(MALFORMED_DATA);
494         return false;
495     }
496     return true;
497 }
498 
Clear()499 void AuthorizationSet::Clear() {
500     memset_s(elems_, 0, elems_size_ * sizeof(keymaster_key_param_t));
501     memset_s(indirect_data_, 0, indirect_data_size_);
502     elems_size_ = 0;
503     indirect_data_size_ = 0;
504 }
505 
FreeData()506 void AuthorizationSet::FreeData() {
507     Clear();
508 
509     delete[] elems_;
510     delete[] indirect_data_;
511 
512     elems_ = NULL;
513     indirect_data_ = NULL;
514     elems_capacity_ = 0;
515     indirect_data_capacity_ = 0;
516     error_ = OK;
517 }
518 
519 /* static */
ComputeIndirectDataSize(const keymaster_key_param_t * elems,size_t count)520 size_t AuthorizationSet::ComputeIndirectDataSize(const keymaster_key_param_t* elems, size_t count) {
521     size_t size = 0;
522     for (size_t i = 0; i < count; ++i) {
523         if (is_blob_tag(elems[i].tag)) {
524             size += elems[i].blob.data_length;
525         }
526     }
527     return size;
528 }
529 
CopyIndirectData()530 void AuthorizationSet::CopyIndirectData() {
531     memset_s(indirect_data_, 0, indirect_data_capacity_);
532 
533     uint8_t* indirect_data_pos = indirect_data_;
534     for (size_t i = 0; i < elems_size_; ++i) {
535         assert(indirect_data_pos <= indirect_data_ + indirect_data_capacity_);
536         if (is_blob_tag(elems_[i].tag)) {
537             memcpy(indirect_data_pos, elems_[i].blob.data, elems_[i].blob.data_length);
538             elems_[i].blob.data = indirect_data_pos;
539             indirect_data_pos += elems_[i].blob.data_length;
540         }
541     }
542     assert(indirect_data_pos == indirect_data_ + indirect_data_capacity_);
543     indirect_data_size_ = indirect_data_pos - indirect_data_;
544 }
545 
GetTagCount(keymaster_tag_t tag) const546 size_t AuthorizationSet::GetTagCount(keymaster_tag_t tag) const {
547     size_t count = 0;
548     for (int pos = -1; (pos = find(tag, pos)) != -1;)
549         ++count;
550     return count;
551 }
552 
GetTagValueEnum(keymaster_tag_t tag,uint32_t * val) const553 bool AuthorizationSet::GetTagValueEnum(keymaster_tag_t tag, uint32_t* val) const {
554     int pos = find(tag);
555     if (pos == -1) {
556         return false;
557     }
558     *val = elems_[pos].enumerated;
559     return true;
560 }
561 
GetTagValueEnumRep(keymaster_tag_t tag,size_t instance,uint32_t * val) const562 bool AuthorizationSet::GetTagValueEnumRep(keymaster_tag_t tag, size_t instance,
563                                           uint32_t* val) const {
564     size_t count = 0;
565     int pos = -1;
566     while (count <= instance) {
567         pos = find(tag, pos);
568         if (pos == -1) {
569             return false;
570         }
571         ++count;
572     }
573     *val = elems_[pos].enumerated;
574     return true;
575 }
576 
GetTagValueInt(keymaster_tag_t tag,uint32_t * val) const577 bool AuthorizationSet::GetTagValueInt(keymaster_tag_t tag, uint32_t* val) const {
578     int pos = find(tag);
579     if (pos == -1) {
580         return false;
581     }
582     *val = elems_[pos].integer;
583     return true;
584 }
585 
GetTagValueIntRep(keymaster_tag_t tag,size_t instance,uint32_t * val) const586 bool AuthorizationSet::GetTagValueIntRep(keymaster_tag_t tag, size_t instance,
587                                          uint32_t* val) const {
588     size_t count = 0;
589     int pos = -1;
590     while (count <= instance) {
591         pos = find(tag, pos);
592         if (pos == -1) {
593             return false;
594         }
595         ++count;
596     }
597     *val = elems_[pos].integer;
598     return true;
599 }
600 
GetTagValueLong(keymaster_tag_t tag,uint64_t * val) const601 bool AuthorizationSet::GetTagValueLong(keymaster_tag_t tag, uint64_t* val) const {
602     int pos = find(tag);
603     if (pos == -1) {
604         return false;
605     }
606     *val = elems_[pos].long_integer;
607     return true;
608 }
609 
GetTagValueLongRep(keymaster_tag_t tag,size_t instance,uint64_t * val) const610 bool AuthorizationSet::GetTagValueLongRep(keymaster_tag_t tag, size_t instance,
611                                           uint64_t* val) const {
612     size_t count = 0;
613     int pos = -1;
614     while (count <= instance) {
615         pos = find(tag, pos);
616         if (pos == -1) {
617             return false;
618         }
619         ++count;
620     }
621     *val = elems_[pos].long_integer;
622     return true;
623 }
624 
GetTagValueDate(keymaster_tag_t tag,uint64_t * val) const625 bool AuthorizationSet::GetTagValueDate(keymaster_tag_t tag, uint64_t* val) const {
626     int pos = find(tag);
627     if (pos == -1) {
628         return false;
629     }
630     *val = elems_[pos].date_time;
631     return true;
632 }
633 
GetTagValueBlob(keymaster_tag_t tag,keymaster_blob_t * val) const634 bool AuthorizationSet::GetTagValueBlob(keymaster_tag_t tag, keymaster_blob_t* val) const {
635     int pos = find(tag);
636     if (pos == -1) {
637         return false;
638     }
639     *val = elems_[pos].blob;
640     return true;
641 }
642 
GetTagValueBool(keymaster_tag_t tag) const643 bool AuthorizationSet::GetTagValueBool(keymaster_tag_t tag) const {
644     int pos = find(tag);
645     if (pos == -1) {
646         return false;
647     }
648     assert(elems_[pos].boolean);
649     return elems_[pos].boolean;
650 }
651 
ContainsEnumValue(keymaster_tag_t tag,uint32_t value) const652 bool AuthorizationSet::ContainsEnumValue(keymaster_tag_t tag, uint32_t value) const {
653     for (auto& entry : *this)
654         if (entry.tag == tag && entry.enumerated == value)
655             return true;
656     return false;
657 }
658 
ContainsIntValue(keymaster_tag_t tag,uint32_t value) const659 bool AuthorizationSet::ContainsIntValue(keymaster_tag_t tag, uint32_t value) const {
660     for (auto& entry : *this)
661         if (entry.tag == tag && entry.integer == value)
662             return true;
663     return false;
664 }
665 
666 }  // namespace keymaster
667