• 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 <keymaster/android_keymaster_utils.h>
25 #include <keymaster/logger.h>
26 
27 namespace keymaster {
28 
is_blob_tag(keymaster_tag_t tag)29 static inline bool is_blob_tag(keymaster_tag_t tag) {
30     return (keymaster_tag_get_type(tag) == KM_BYTES || keymaster_tag_get_type(tag) == KM_BIGNUM);
31 }
32 
33 const size_t STARTING_ELEMS_CAPACITY = 8;
34 
AuthorizationSet(AuthorizationSetBuilder & builder)35 AuthorizationSet::AuthorizationSet(AuthorizationSetBuilder& builder) {
36     elems_ = builder.set.elems_;
37     builder.set.elems_ = nullptr;
38 
39     elems_size_ = builder.set.elems_size_;
40     builder.set.elems_size_ = 0;
41 
42     elems_capacity_ = builder.set.elems_capacity_;
43     builder.set.elems_capacity_ = 0;
44 
45     indirect_data_ = builder.set.indirect_data_;
46     builder.set.indirect_data_ = nullptr;
47 
48     indirect_data_capacity_ = builder.set.indirect_data_capacity_;
49     builder.set.indirect_data_capacity_ = 0;
50 
51     indirect_data_size_ = builder.set.indirect_data_size_;
52     builder.set.indirect_data_size_ = 0;
53 
54     error_ = builder.set.error_;
55     builder.set.error_ = OK;
56 }
57 
~AuthorizationSet()58 AuthorizationSet::~AuthorizationSet() {
59     FreeData();
60 }
61 
reserve_elems(size_t count)62 bool AuthorizationSet::reserve_elems(size_t count) {
63     if (is_valid() != OK) return false;
64 
65     if (count > elems_capacity_) {
66         keymaster_key_param_t* new_elems = new (std::nothrow) keymaster_key_param_t[count];
67         if (new_elems == nullptr) {
68             set_invalid(ALLOCATION_FAILURE);
69             return false;
70         }
71         memcpy(new_elems, elems_, sizeof(*elems_) * elems_size_);
72         delete[] elems_;
73         elems_ = new_elems;
74         elems_capacity_ = count;
75     }
76     return true;
77 }
78 
reserve_indirect(size_t length)79 bool AuthorizationSet::reserve_indirect(size_t length) {
80     if (is_valid() != OK) return false;
81 
82     if (length > indirect_data_capacity_) {
83         uint8_t* new_data = new (std::nothrow) uint8_t[length];
84         if (new_data == nullptr) {
85             set_invalid(ALLOCATION_FAILURE);
86             return false;
87         }
88         memcpy(new_data, indirect_data_, indirect_data_size_);
89 
90         // Fix up the data pointers to point into the new region.
91         for (size_t i = 0; i < elems_size_; ++i) {
92             if (is_blob_tag(elems_[i].tag))
93                 elems_[i].blob.data = new_data + (elems_[i].blob.data - indirect_data_);
94         }
95         delete[] indirect_data_;
96         indirect_data_ = new_data;
97         indirect_data_capacity_ = length;
98     }
99     return true;
100 }
101 
MoveFrom(AuthorizationSet & set)102 void AuthorizationSet::MoveFrom(AuthorizationSet& set) {
103     elems_ = set.elems_;
104     elems_size_ = set.elems_size_;
105     elems_capacity_ = set.elems_capacity_;
106     indirect_data_ = set.indirect_data_;
107     indirect_data_size_ = set.indirect_data_size_;
108     indirect_data_capacity_ = set.indirect_data_capacity_;
109     error_ = set.error_;
110     set.elems_ = nullptr;
111     set.elems_size_ = 0;
112     set.elems_capacity_ = 0;
113     set.indirect_data_ = nullptr;
114     set.indirect_data_size_ = 0;
115     set.indirect_data_capacity_ = 0;
116     set.error_ = OK;
117 }
118 
Reinitialize(const keymaster_key_param_t * elems,const size_t count)119 bool AuthorizationSet::Reinitialize(const keymaster_key_param_t* elems, const size_t count) {
120     FreeData();
121 
122     if (elems == nullptr || count == 0) {
123         error_ = OK;
124         return true;
125     }
126 
127     if (!reserve_elems(count)) return false;
128 
129     if (!reserve_indirect(ComputeIndirectDataSize(elems, count))) return false;
130 
131     memcpy(elems_, elems, sizeof(keymaster_key_param_t) * count);
132     elems_size_ = count;
133     CopyIndirectData();
134     error_ = OK;
135     return true;
136 }
137 
set_invalid(Error error)138 void AuthorizationSet::set_invalid(Error error) {
139     FreeData();
140     error_ = error;
141 }
142 
Sort()143 void AuthorizationSet::Sort() {
144     qsort(elems_, elems_size_, sizeof(*elems_),
145           reinterpret_cast<int (*)(const void*, const void*)>(keymaster_param_compare));
146 }
147 
Deduplicate()148 void AuthorizationSet::Deduplicate() {
149     Sort();
150 
151     size_t invalid_count = 0;
152     for (size_t i = 1; i < size(); ++i) {
153         if (elems_[i - 1].tag == KM_TAG_INVALID)
154             ++invalid_count;
155         else if (keymaster_param_compare(elems_ + i - 1, elems_ + i) == 0) {
156             // Mark dups as invalid.  Note that this "leaks" the data referenced by KM_BYTES and
157             // KM_BIGNUM entries, but those are just pointers into indirect_data_, so it will all
158             // get cleaned up.
159             elems_[i - 1].tag = KM_TAG_INVALID;
160             ++invalid_count;
161         }
162     }
163     if (size() > 0 && elems_[size() - 1].tag == KM_TAG_INVALID) ++invalid_count;
164 
165     if (invalid_count == 0) return;
166 
167     Sort();
168 
169     // Since KM_TAG_INVALID == 0, all of the invalid entries are first.
170     elems_size_ -= invalid_count;
171     memmove(elems_, elems_ + invalid_count, size() * sizeof(*elems_));
172 }
173 
Difference(const keymaster_key_param_set_t & set)174 void AuthorizationSet::Difference(const keymaster_key_param_set_t& set) {
175     if (set.length == 0) return;
176 
177     Deduplicate();
178 
179     for (size_t i = 0; i < set.length; i++) {
180         int index = -1;
181         do {
182             index = find(set.params[i].tag, index);
183             if (index != -1 && keymaster_param_compare(&elems_[index], &set.params[i]) == 0) {
184                 erase(index);
185                 break;
186             }
187         } while (index != -1);
188     }
189 }
190 
CopyToParamSet(keymaster_key_param_set_t * set) const191 void AuthorizationSet::CopyToParamSet(keymaster_key_param_set_t* set) const {
192     assert(set);
193 
194     set->length = size();
195     set->params =
196         reinterpret_cast<keymaster_key_param_t*>(malloc(sizeof(keymaster_key_param_t) * size()));
197 
198     for (size_t i = 0; i < size(); ++i) {
199         const keymaster_key_param_t src = (*this)[i];
200         keymaster_key_param_t& dst(set->params[i]);
201 
202         dst = src;
203         keymaster_tag_type_t type = keymaster_tag_get_type(src.tag);
204         if (type == KM_BIGNUM || type == KM_BYTES) {
205             void* tmp = malloc(src.blob.data_length);
206             memcpy(tmp, src.blob.data, src.blob.data_length);
207             dst.blob.data = reinterpret_cast<uint8_t*>(tmp);
208         }
209     }
210 }
211 
find(keymaster_tag_t tag,int begin) const212 int AuthorizationSet::find(keymaster_tag_t tag, int begin) const {
213     if (is_valid() != OK) return -1;
214 
215     int i = ++begin;
216     while (i < (int)elems_size_ && elems_[i].tag != tag)
217         ++i;
218     if (i == (int)elems_size_)
219         return -1;
220     else
221         return i;
222 }
223 
erase(int index)224 bool AuthorizationSet::erase(int index) {
225     if (index < 0 || index >= static_cast<int>(size())) return false;
226 
227     --elems_size_;
228     for (size_t i = index; i < elems_size_; ++i)
229         elems_[i] = elems_[i + 1];
230     return true;
231 }
232 
233 keymaster_key_param_t empty_param = {KM_TAG_INVALID, {}};
operator [](int at)234 keymaster_key_param_t& AuthorizationSet::operator[](int at) {
235     if (is_valid() == OK && at < (int)elems_size_) {
236         return elems_[at];
237     }
238     empty_param = {KM_TAG_INVALID, {}};
239     return empty_param;
240 }
241 
operator [](int at) const242 const keymaster_key_param_t& AuthorizationSet::operator[](int at) const {
243     if (is_valid() == OK && at < (int)elems_size_) {
244         return elems_[at];
245     }
246     empty_param = {KM_TAG_INVALID, {}};
247     return empty_param;
248 }
249 
push_back(const keymaster_key_param_set_t & set)250 bool AuthorizationSet::push_back(const keymaster_key_param_set_t& set) {
251     if (is_valid() != OK) return false;
252 
253     if (!reserve_elems(elems_size_ + set.length)) return false;
254 
255     if (!reserve_indirect(indirect_data_size_ + ComputeIndirectDataSize(set.params, set.length)))
256         return false;
257 
258     for (size_t i = 0; i < set.length; ++i)
259         if (!push_back(set.params[i])) return false;
260 
261     return true;
262 }
263 
push_back(keymaster_key_param_t elem)264 bool AuthorizationSet::push_back(keymaster_key_param_t elem) {
265     if (is_valid() != OK) return false;
266 
267     if (elems_size_ >= elems_capacity_)
268         if (!reserve_elems(elems_capacity_ ? elems_capacity_ * 2 : STARTING_ELEMS_CAPACITY))
269             return false;
270 
271     if (is_blob_tag(elem.tag)) {
272         if (indirect_data_capacity_ - indirect_data_size_ < elem.blob.data_length)
273             if (!reserve_indirect(2 * (indirect_data_capacity_ + elem.blob.data_length)))
274                 return false;
275 
276         memcpy(indirect_data_ + indirect_data_size_, elem.blob.data, elem.blob.data_length);
277         elem.blob.data = indirect_data_ + indirect_data_size_;
278         indirect_data_size_ += elem.blob.data_length;
279     }
280 
281     elems_[elems_size_++] = elem;
282     return true;
283 }
284 
serialized_size(const keymaster_key_param_t & param)285 static size_t serialized_size(const keymaster_key_param_t& param) {
286     switch (keymaster_tag_get_type(param.tag)) {
287     case KM_INVALID:
288         return sizeof(uint32_t);
289     case KM_ENUM:
290     case KM_ENUM_REP:
291     case KM_UINT:
292     case KM_UINT_REP:
293         return sizeof(uint32_t) * 2;
294     case KM_ULONG:
295     case KM_ULONG_REP:
296     case KM_DATE:
297         return sizeof(uint32_t) + sizeof(uint64_t);
298     case KM_BOOL:
299         return sizeof(uint32_t) + 1;
300     case KM_BIGNUM:
301     case KM_BYTES:
302         return sizeof(uint32_t) * 3;
303     }
304 
305     return sizeof(uint32_t);
306 }
307 
serialize(const keymaster_key_param_t & param,uint8_t * buf,const uint8_t * end,const uint8_t * indirect_base)308 static uint8_t* serialize(const keymaster_key_param_t& param, uint8_t* buf, const uint8_t* end,
309                           const uint8_t* indirect_base) {
310     buf = append_uint32_to_buf(buf, end, param.tag);
311     switch (keymaster_tag_get_type(param.tag)) {
312     case KM_INVALID:
313         break;
314     case KM_ENUM:
315     case KM_ENUM_REP:
316         buf = append_uint32_to_buf(buf, end, param.enumerated);
317         break;
318     case KM_UINT:
319     case KM_UINT_REP:
320         buf = append_uint32_to_buf(buf, end, param.integer);
321         break;
322     case KM_ULONG:
323     case KM_ULONG_REP:
324         buf = append_uint64_to_buf(buf, end, param.long_integer);
325         break;
326     case KM_DATE:
327         buf = append_uint64_to_buf(buf, end, param.date_time);
328         break;
329     case KM_BOOL:
330         if (buf < end) *buf = static_cast<uint8_t>(param.boolean);
331         buf++;
332         break;
333     case KM_BIGNUM:
334     case KM_BYTES:
335         buf = append_uint32_to_buf(buf, end, param.blob.data_length);
336         buf = append_uint32_to_buf(buf, end, param.blob.data - indirect_base);
337         break;
338     }
339     return buf;
340 }
341 
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)342 static bool deserialize(keymaster_key_param_t* param, const uint8_t** buf_ptr, const uint8_t* end,
343                         const uint8_t* indirect_base, const uint8_t* indirect_end) {
344     if (!copy_uint32_from_buf(buf_ptr, end, &param->tag)) return false;
345 
346     switch (keymaster_tag_get_type(param->tag)) {
347     case KM_INVALID:
348         return false;
349     case KM_ENUM:
350     case KM_ENUM_REP:
351         return copy_uint32_from_buf(buf_ptr, end, &param->enumerated);
352     case KM_UINT:
353     case KM_UINT_REP:
354         return copy_uint32_from_buf(buf_ptr, end, &param->integer);
355     case KM_ULONG:
356     case KM_ULONG_REP:
357         return copy_uint64_from_buf(buf_ptr, end, &param->long_integer);
358     case KM_DATE:
359         return copy_uint64_from_buf(buf_ptr, end, &param->date_time);
360         break;
361     case KM_BOOL:
362         if (*buf_ptr < end) {
363             uint8_t temp = **buf_ptr;
364             // Bools are converted to 0 or 1 when serialized so only accept
365             // one of these values when deserializing.
366             if (temp <= 1) {
367                 param->boolean = static_cast<bool>(temp);
368                 (*buf_ptr)++;
369                 return true;
370             }
371         }
372         return false;
373 
374     case KM_BIGNUM:
375     case KM_BYTES: {
376         uint32_t offset;
377         if (!copy_uint32_from_buf(buf_ptr, end, &param->blob.data_length) ||
378             !copy_uint32_from_buf(buf_ptr, end, &offset))
379             return false;
380         if (param->blob.data_length + offset < param->blob.data_length ||  // Overflow check
381             static_cast<ptrdiff_t>(offset) > indirect_end - indirect_base ||
382             static_cast<ptrdiff_t>(offset + param->blob.data_length) > indirect_end - indirect_base)
383             return false;
384         param->blob.data = indirect_base + offset;
385         return true;
386     }
387     }
388 
389     return false;
390 }
391 
SerializedSizeOfElements() const392 size_t AuthorizationSet::SerializedSizeOfElements() const {
393     size_t size = 0;
394     for (size_t i = 0; i < elems_size_; ++i) {
395         size += serialized_size(elems_[i]);
396     }
397     return size;
398 }
399 
SerializedSize() const400 size_t AuthorizationSet::SerializedSize() const {
401     return sizeof(uint32_t) +           // Size of indirect_data_
402            indirect_data_size_ +        // indirect_data_
403            sizeof(uint32_t) +           // Number of elems_
404            sizeof(uint32_t) +           // Size of elems_
405            SerializedSizeOfElements();  // elems_
406 }
407 
Serialize(uint8_t * buf,const uint8_t * end) const408 uint8_t* AuthorizationSet::Serialize(uint8_t* buf, const uint8_t* end) const {
409     buf = append_size_and_data_to_buf(buf, end, indirect_data_, indirect_data_size_);
410     buf = append_uint32_to_buf(buf, end, elems_size_);
411     buf = append_uint32_to_buf(buf, end, SerializedSizeOfElements());
412     for (size_t i = 0; i < elems_size_; ++i) {
413         buf = serialize(elems_[i], buf, end, indirect_data_);
414     }
415     return buf;
416 }
417 
DeserializeIndirectData(const uint8_t ** buf_ptr,const uint8_t * end)418 bool AuthorizationSet::DeserializeIndirectData(const uint8_t** buf_ptr, const uint8_t* end) {
419     UniquePtr<uint8_t[]> indirect_buf;
420     if (!copy_size_and_data_from_buf(buf_ptr, end, &indirect_data_size_, &indirect_buf)) {
421         LOG_E("Malformed data found in AuthorizationSet deserialization", 0);
422         set_invalid(MALFORMED_DATA);
423         return false;
424     }
425     indirect_data_ = indirect_buf.release();
426     return true;
427 }
428 
DeserializeElementsData(const uint8_t ** buf_ptr,const uint8_t * end)429 bool AuthorizationSet::DeserializeElementsData(const uint8_t** buf_ptr, const uint8_t* end) {
430     uint32_t elements_count;
431     uint32_t elements_size;
432     if (!copy_uint32_from_buf(buf_ptr, end, &elements_count) ||
433         !copy_uint32_from_buf(buf_ptr, end, &elements_size)) {
434         LOG_E("Malformed data found in AuthorizationSet deserialization", 0);
435         set_invalid(MALFORMED_DATA);
436         return false;
437     }
438 
439     // Note that the following validation of elements_count is weak, but it prevents allocation of
440     // elems_ arrays which are clearly too large to be reasonable.
441     size_t elems_refs_size;
442     size_t elems_alloc_size;
443     bool refs_size_overflow =
444         __builtin_mul_overflow(elements_count, sizeof(uint32_t), &elems_refs_size);
445     bool alloc_size_overflow =
446         __builtin_mul_overflow(elements_count, sizeof(*elems_), &elems_alloc_size);
447     /* elements_size must fit in the buffer */
448     if (static_cast<ptrdiff_t>(elements_size) > end - *buf_ptr ||
449         /* The element refs must all fit within elements_size */
450         elems_refs_size > elements_size ||
451         /* If our pointer math would overflow, bail */
452         refs_size_overflow ||
453         /* If the resulting allocation would overflow, bail */
454         alloc_size_overflow) {
455         LOG_E("Malformed data found in AuthorizationSet deserialization", 0);
456         set_invalid(MALFORMED_DATA);
457         return false;
458     }
459 
460     if (!reserve_elems(elements_count)) return false;
461 
462     uint8_t* indirect_end = indirect_data_ + indirect_data_size_;
463     const uint8_t* elements_end = *buf_ptr + elements_size;
464     for (size_t i = 0; i < elements_count; ++i) {
465         if (!deserialize(elems_ + i, buf_ptr, elements_end, indirect_data_, indirect_end)) {
466             LOG_E("Malformed data found in AuthorizationSet deserialization", 0);
467             set_invalid(MALFORMED_DATA);
468             return false;
469         }
470     }
471 
472     // Check if all the elements were consumed. If not, something was malformed as the
473     // retrieved elements_count and elements_size are not consistent with each other.
474     if (*buf_ptr != elements_end) {
475         LOG_E("Malformed data found in AuthorizationSet deserialization", 0);
476         set_invalid(MALFORMED_DATA);
477         return false;
478     }
479 
480     elems_size_ = elements_count;
481     return true;
482 }
483 
Deserialize(const uint8_t ** buf_ptr,const uint8_t * end)484 bool AuthorizationSet::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
485     FreeData();
486 
487     if (!DeserializeIndirectData(buf_ptr, end) || !DeserializeElementsData(buf_ptr, end))
488         return false;
489 
490     if (indirect_data_size_ != ComputeIndirectDataSize(elems_, elems_size_)) {
491         LOG_E("Malformed data found in AuthorizationSet deserialization", 0);
492         set_invalid(MALFORMED_DATA);
493         return false;
494     }
495     return true;
496 }
497 
Clear()498 void AuthorizationSet::Clear() {
499     memset_s(elems_, 0, elems_capacity_ * sizeof(keymaster_key_param_t));
500     memset_s(indirect_data_, 0, indirect_data_capacity_);
501     elems_size_ = 0;
502     indirect_data_size_ = 0;
503     error_ = OK;
504 }
505 
FreeData()506 void AuthorizationSet::FreeData() {
507     Clear();
508 
509     delete[] elems_;
510     delete[] indirect_data_;
511 
512     elems_ = nullptr;
513     indirect_data_ = nullptr;
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) return true;
655     return false;
656 }
657 
ContainsIntValue(keymaster_tag_t tag,uint32_t value) const658 bool AuthorizationSet::ContainsIntValue(keymaster_tag_t tag, uint32_t value) const {
659     for (auto& entry : *this)
660         if (entry.tag == tag && entry.integer == value) return true;
661     return false;
662 }
663 
664 }  // namespace keymaster
665