blob: e2bce1484adf5b073a247b350e79e524dc2014b5 [file] [log] [blame]
Shawn Willden5ada7b62014-07-29 09:44:17 -06001/*
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 <stdlib.h>
18#include <string.h>
19#include <stddef.h>
20
21#include <assert.h>
22
Shawn Willden98d9b922014-08-26 08:14:10 -060023#include <keymaster/authorization_set.h>
24#include <keymaster/google_keymaster_utils.h>
Shawn Willden5ada7b62014-07-29 09:44:17 -060025
26namespace keymaster {
27
28static inline bool is_blob_tag(keymaster_tag_t tag) {
29 return (keymaster_tag_get_type(tag) == KM_BYTES || keymaster_tag_get_type(tag) == KM_BIGNUM);
30}
31
32const size_t STARTING_ELEMS_CAPACITY = 8;
33
Shawn Willden58e1a542014-08-08 21:58:29 -060034AuthorizationSet::AuthorizationSet(const AuthorizationSet& set)
Shawn Willden62de2662014-08-20 14:14:49 -060035 : Serializable(), elems_(NULL), indirect_data_(NULL) {
Shawn Willden76364712014-08-11 17:48:04 -060036 Reinitialize(set.elems_, set.elems_size_);
Shawn Willden5ada7b62014-07-29 09:44:17 -060037}
38
Shawn Willden2c242002015-02-27 07:01:02 -070039AuthorizationSet::AuthorizationSet(AuthorizationSetBuilder& builder) {
40 elems_ = builder.set.elems_;
41 builder.set.elems_ = NULL;
42
43 elems_size_ = builder.set.elems_size_;
44 builder.set.elems_size_ = 0;
45
46 elems_capacity_ = builder.set.elems_capacity_;
47 builder.set.elems_capacity_ = 0;
48
49 indirect_data_ = builder.set.indirect_data_;
50 builder.set.indirect_data_ = NULL;
51
52 indirect_data_capacity_ = builder.set.indirect_data_capacity_;
53 builder.set.indirect_data_capacity_ = 0;
54
55 indirect_data_size_ = builder.set.indirect_data_size_;
56 builder.set.indirect_data_size_ = 0;
57
58 error_ = builder.set.error_;
59 builder.set.error_ = OK;
60}
61
Shawn Willden8d336ae2014-08-09 15:47:05 -060062AuthorizationSet::~AuthorizationSet() {
63 FreeData();
64}
Shawn Willden58e1a542014-08-08 21:58:29 -060065
Shawn Willden37012132014-08-19 08:15:57 -060066bool AuthorizationSet::reserve_elems(size_t count) {
Shawn Willden437fbd12014-08-20 11:59:49 -060067 if (is_valid() != OK)
68 return false;
69
Shawn Willden37012132014-08-19 08:15:57 -060070 if (count >= elems_capacity_) {
71 keymaster_key_param_t* new_elems = new keymaster_key_param_t[count];
72 if (new_elems == NULL) {
73 set_invalid(ALLOCATION_FAILURE);
74 return false;
75 }
76 memcpy(new_elems, elems_, sizeof(*elems_) * elems_size_);
77 delete[] elems_;
78 elems_ = new_elems;
79 elems_capacity_ = count;
80 }
81 return true;
82}
83
84bool AuthorizationSet::reserve_indirect(size_t length) {
Shawn Willden437fbd12014-08-20 11:59:49 -060085 if (is_valid() != OK)
86 return false;
87
Shawn Willden37012132014-08-19 08:15:57 -060088 if (length > indirect_data_capacity_) {
89 uint8_t* new_data = new uint8_t[length];
90 if (new_data == NULL) {
91 set_invalid(ALLOCATION_FAILURE);
92 return false;
93 }
94 memcpy(new_data, indirect_data_, indirect_data_size_);
95
96 // Fix up the data pointers to point into the new region.
97 for (size_t i = 0; i < elems_size_; ++i) {
98 if (is_blob_tag(elems_[i].tag))
99 elems_[i].blob.data = new_data + (elems_[i].blob.data - indirect_data_);
100 }
101 delete[] indirect_data_;
102 indirect_data_ = new_data;
103 indirect_data_capacity_ = length;
104 }
105 return true;
106}
107
Shawn Willden5ada7b62014-07-29 09:44:17 -0600108bool AuthorizationSet::Reinitialize(const keymaster_key_param_t* elems, const size_t count) {
109 FreeData();
110
Shawn Willden37012132014-08-19 08:15:57 -0600111 if (!reserve_elems(count))
Shawn Willden5ada7b62014-07-29 09:44:17 -0600112 return false;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600113
Shawn Willden37012132014-08-19 08:15:57 -0600114 if (!reserve_indirect(ComputeIndirectDataSize(elems, count)))
115 return false;
116
117 memcpy(elems_, elems, sizeof(keymaster_key_param_t) * count);
118 elems_size_ = count;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600119 CopyIndirectData();
Shawn Willden37012132014-08-19 08:15:57 -0600120 error_ = OK;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600121 return true;
122}
123
Shawn Willden5ada7b62014-07-29 09:44:17 -0600124void AuthorizationSet::set_invalid(Error error) {
Shawn Willden5ada7b62014-07-29 09:44:17 -0600125 FreeData();
Shawn Willden37012132014-08-19 08:15:57 -0600126 error_ = error;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600127}
128
Shawn Willden2c242002015-02-27 07:01:02 -0700129void AuthorizationSet::Deduplicate() {
130 qsort(elems_, elems_size_, sizeof(*elems_),
131 reinterpret_cast<int (*)(const void*, const void*)>(keymaster_param_compare));
132
133 size_t invalid_count = 0;
134 for (size_t i = 1; i < size(); ++i) {
135 if (elems_[i - 1].tag == KM_TAG_INVALID)
136 ++invalid_count;
137 else if (keymaster_param_compare(elems_ + i - 1, elems_ + i) == 0) {
138 // Mark dups as invalid. Note that this "leaks" the data referenced by KM_BYTES and
139 // KM_BIGNUM entries, but those are just pointers into indirect_data_, so it will all
140 // get cleaned up.
141 elems_[i - 1].tag = KM_TAG_INVALID;
142 ++invalid_count;
143 }
144 }
145 if (size() > 0 && elems_[size() - 1].tag == KM_TAG_INVALID)
146 ++invalid_count;
147
148 if (invalid_count == 0)
149 return;
150
151 // Since KM_TAG_INVALID == 0, all of the invalid entries are first.
152 elems_size_ -= invalid_count;
153 memmove(elems_, elems_ + invalid_count, size() * sizeof(*elems_));
154}
155
Shawn Willdencb0d64b2015-01-22 10:23:42 -0700156void AuthorizationSet::CopyToParamSet(keymaster_key_param_set_t* set) const {
157 assert(set);
158
159 set->length = size();
160 set->params =
161 reinterpret_cast<keymaster_key_param_t*>(malloc(sizeof(keymaster_key_param_t) * size()));
162
163 for (size_t i = 0; i < size(); ++i) {
164 const keymaster_key_param_t src = (*this)[i];
165 keymaster_key_param_t& dst(set->params[i]);
166
167 dst = src;
168 keymaster_tag_type_t type = keymaster_tag_get_type(src.tag);
169 if (type == KM_BIGNUM || type == KM_BYTES) {
170 void* tmp = malloc(src.blob.data_length);
171 memcpy(tmp, src.blob.data, src.blob.data_length);
172 dst.blob.data = reinterpret_cast<uint8_t*>(tmp);
173 }
174 }
175}
176
Shawn Willden5ada7b62014-07-29 09:44:17 -0600177int AuthorizationSet::find(keymaster_tag_t tag, int begin) const {
Shawn Willden437fbd12014-08-20 11:59:49 -0600178 if (is_valid() != OK)
179 return -1;
180
Shawn Willden5ada7b62014-07-29 09:44:17 -0600181 int i = ++begin;
Shawn Willden8d336ae2014-08-09 15:47:05 -0600182 while (i < (int)elems_size_ && elems_[i].tag != tag)
183 ++i;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600184 if (i == (int)elems_size_)
185 return -1;
186 else
187 return i;
188}
189
190keymaster_key_param_t empty;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600191keymaster_key_param_t AuthorizationSet::operator[](int at) const {
Shawn Willden437fbd12014-08-20 11:59:49 -0600192 if (is_valid() == OK && at < (int)elems_size_) {
Shawn Willden8d336ae2014-08-09 15:47:05 -0600193 return elems_[at];
Shawn Willden5ada7b62014-07-29 09:44:17 -0600194 }
195 memset(&empty, 0, sizeof(empty));
196 return empty;
197}
198
Shawn Willden37012132014-08-19 08:15:57 -0600199bool AuthorizationSet::push_back(const AuthorizationSet& set) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600200 if (is_valid() != OK)
201 return false;
202
Shawn Willden37012132014-08-19 08:15:57 -0600203 if (!reserve_elems(elems_size_ + set.elems_size_))
204 return false;
205
206 if (!reserve_indirect(indirect_data_size_ + set.indirect_data_size_))
207 return false;
208
209 for (size_t i = 0; i < set.size(); ++i)
210 if (!push_back(set[i]))
Shawn Willden5ada7b62014-07-29 09:44:17 -0600211 return false;
Shawn Willden37012132014-08-19 08:15:57 -0600212
213 return true;
214}
215
216bool AuthorizationSet::push_back(keymaster_key_param_t elem) {
Shawn Willden437fbd12014-08-20 11:59:49 -0600217 if (is_valid() != OK)
218 return false;
219
Shawn Willden37012132014-08-19 08:15:57 -0600220 if (elems_size_ >= elems_capacity_)
221 if (!reserve_elems(elems_capacity_ ? elems_capacity_ * 2 : STARTING_ELEMS_CAPACITY))
222 return false;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600223
224 if (is_blob_tag(elem.tag)) {
Shawn Willden37012132014-08-19 08:15:57 -0600225 if (indirect_data_capacity_ - indirect_data_size_ < elem.blob.data_length)
226 if (!reserve_indirect(2 * (indirect_data_capacity_ + elem.blob.data_length)))
Shawn Willden5ada7b62014-07-29 09:44:17 -0600227 return false;
Shawn Willden58e1a542014-08-08 21:58:29 -0600228
Shawn Willden5ada7b62014-07-29 09:44:17 -0600229 memcpy(indirect_data_ + indirect_data_size_, elem.blob.data, elem.blob.data_length);
Shawn Willden8d336ae2014-08-09 15:47:05 -0600230 elem.blob.data = indirect_data_ + indirect_data_size_;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600231 indirect_data_size_ += elem.blob.data_length;
232 }
233
234 elems_[elems_size_++] = elem;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600235 return true;
236}
237
Shawn Willden8d336ae2014-08-09 15:47:05 -0600238static size_t serialized_size(const keymaster_key_param_t& param) {
239 switch (keymaster_tag_get_type(param.tag)) {
240 case KM_INVALID:
241 default:
242 return sizeof(uint32_t);
243 case KM_ENUM:
244 case KM_ENUM_REP:
245 case KM_INT:
246 case KM_INT_REP:
247 return sizeof(uint32_t) * 2;
248 case KM_LONG:
249 case KM_DATE:
250 return sizeof(uint32_t) + sizeof(uint64_t);
251 case KM_BOOL:
252 return sizeof(uint32_t) + 1;
253 break;
254 case KM_BIGNUM:
255 case KM_BYTES:
256 return sizeof(uint32_t) * 3;
257 }
Shawn Willden58e1a542014-08-08 21:58:29 -0600258}
259
Shawn Willden8d336ae2014-08-09 15:47:05 -0600260static uint8_t* serialize(const keymaster_key_param_t& param, uint8_t* buf, const uint8_t* end,
261 const uint8_t* indirect_base) {
Shawn Willden172f8c92014-08-17 07:50:34 -0600262 buf = append_uint32_to_buf(buf, end, param.tag);
Shawn Willden8d336ae2014-08-09 15:47:05 -0600263 switch (keymaster_tag_get_type(param.tag)) {
264 case KM_INVALID:
265 break;
266 case KM_ENUM:
267 case KM_ENUM_REP:
Shawn Willden172f8c92014-08-17 07:50:34 -0600268 buf = append_uint32_to_buf(buf, end, param.enumerated);
Shawn Willden8d336ae2014-08-09 15:47:05 -0600269 break;
270 case KM_INT:
271 case KM_INT_REP:
Shawn Willden172f8c92014-08-17 07:50:34 -0600272 buf = append_uint32_to_buf(buf, end, param.integer);
Shawn Willden8d336ae2014-08-09 15:47:05 -0600273 break;
274 case KM_LONG:
Shawn Willden172f8c92014-08-17 07:50:34 -0600275 buf = append_uint64_to_buf(buf, end, param.long_integer);
Shawn Willden8d336ae2014-08-09 15:47:05 -0600276 break;
277 case KM_DATE:
Shawn Willden172f8c92014-08-17 07:50:34 -0600278 buf = append_uint64_to_buf(buf, end, param.date_time);
Shawn Willden8d336ae2014-08-09 15:47:05 -0600279 break;
280 case KM_BOOL:
281 if (buf < end)
282 *buf = static_cast<uint8_t>(param.boolean);
283 buf++;
284 break;
285 case KM_BIGNUM:
286 case KM_BYTES:
Shawn Willden172f8c92014-08-17 07:50:34 -0600287 buf = append_uint32_to_buf(buf, end, param.blob.data_length);
288 buf = append_uint32_to_buf(buf, end, param.blob.data - indirect_base);
Shawn Willden8d336ae2014-08-09 15:47:05 -0600289 break;
290 }
291 return buf;
292}
293
Shawn Willden172f8c92014-08-17 07:50:34 -0600294static bool deserialize(keymaster_key_param_t* param, const uint8_t** buf_ptr, const uint8_t* end,
Shawn Willden8d336ae2014-08-09 15:47:05 -0600295 const uint8_t* indirect_base, const uint8_t* indirect_end) {
Shawn Willden172f8c92014-08-17 07:50:34 -0600296 if (!copy_uint32_from_buf(buf_ptr, end, &param->tag))
Shawn Willden8d336ae2014-08-09 15:47:05 -0600297 return false;
Shawn Willden8d336ae2014-08-09 15:47:05 -0600298
299 switch (keymaster_tag_get_type(param->tag)) {
300 default:
301 case KM_INVALID:
302 return false;
303 case KM_ENUM:
304 case KM_ENUM_REP:
Shawn Willden172f8c92014-08-17 07:50:34 -0600305 return copy_uint32_from_buf(buf_ptr, end, &param->enumerated);
Shawn Willden8d336ae2014-08-09 15:47:05 -0600306 case KM_INT:
307 case KM_INT_REP:
Shawn Willden172f8c92014-08-17 07:50:34 -0600308 return copy_uint32_from_buf(buf_ptr, end, &param->integer);
Shawn Willden8d336ae2014-08-09 15:47:05 -0600309 case KM_LONG:
Shawn Willden172f8c92014-08-17 07:50:34 -0600310 return copy_uint64_from_buf(buf_ptr, end, &param->long_integer);
Shawn Willden8d336ae2014-08-09 15:47:05 -0600311 case KM_DATE:
Shawn Willden172f8c92014-08-17 07:50:34 -0600312 return copy_uint64_from_buf(buf_ptr, end, &param->date_time);
Shawn Willden8d336ae2014-08-09 15:47:05 -0600313 break;
314 case KM_BOOL:
Shawn Willden172f8c92014-08-17 07:50:34 -0600315 if (*buf_ptr < end) {
316 param->boolean = static_cast<bool>(**buf_ptr);
317 (*buf_ptr)++;
Shawn Willden8d336ae2014-08-09 15:47:05 -0600318 return true;
319 }
320 return false;
321
322 case KM_BIGNUM:
323 case KM_BYTES: {
Shawn Willden8d336ae2014-08-09 15:47:05 -0600324 uint32_t offset;
Shawn Willden172f8c92014-08-17 07:50:34 -0600325 if (!copy_uint32_from_buf(buf_ptr, end, &param->blob.data_length) ||
326 !copy_uint32_from_buf(buf_ptr, end, &offset))
Shawn Willden8d336ae2014-08-09 15:47:05 -0600327 return false;
328 if (static_cast<ptrdiff_t>(offset) > indirect_end - indirect_base ||
Shawn Willden172f8c92014-08-17 07:50:34 -0600329 static_cast<ptrdiff_t>(offset + param->blob.data_length) > indirect_end - indirect_base)
Shawn Willden8d336ae2014-08-09 15:47:05 -0600330 return false;
Shawn Willden8d336ae2014-08-09 15:47:05 -0600331 param->blob.data = indirect_base + offset;
332 return true;
333 }
334 }
335}
336
337size_t AuthorizationSet::SerializedSizeOfElements() const {
338 size_t size = 0;
339 for (size_t i = 0; i < elems_size_; ++i) {
340 size += serialized_size(elems_[i]);
341 }
342 return size;
343}
344
345size_t AuthorizationSet::SerializedSize() const {
346 return sizeof(uint32_t) + // Size of indirect_data_
347 indirect_data_size_ + // indirect_data_
348 sizeof(uint32_t) + // Number of elems_
349 sizeof(uint32_t) + // Size of elems_
350 SerializedSizeOfElements(); // elems_
351}
352
353uint8_t* AuthorizationSet::Serialize(uint8_t* buf, const uint8_t* end) const {
354 buf = append_size_and_data_to_buf(buf, end, indirect_data_, indirect_data_size_);
Shawn Willden172f8c92014-08-17 07:50:34 -0600355 buf = append_uint32_to_buf(buf, end, elems_size_);
356 buf = append_uint32_to_buf(buf, end, SerializedSizeOfElements());
Shawn Willden8d336ae2014-08-09 15:47:05 -0600357 for (size_t i = 0; i < elems_size_; ++i) {
358 buf = serialize(elems_[i], buf, end, indirect_data_);
359 }
360 return buf;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600361}
362
Shawn Willden37012132014-08-19 08:15:57 -0600363bool AuthorizationSet::DeserializeIndirectData(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600364 UniquePtr<uint8_t[]> indirect_buf;
365 if (!copy_size_and_data_from_buf(buf_ptr, end, &indirect_data_size_, &indirect_buf)) {
Shawn Willden37012132014-08-19 08:15:57 -0600366 set_invalid(MALFORMED_DATA);
367 return false;
368 }
Shawn Willdenf2282b32014-08-25 06:49:54 -0600369 indirect_data_ = indirect_buf.release();
Shawn Willden37012132014-08-19 08:15:57 -0600370 return true;
371}
Shawn Willden5ada7b62014-07-29 09:44:17 -0600372
Shawn Willden37012132014-08-19 08:15:57 -0600373bool AuthorizationSet::DeserializeElementsData(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willden8d336ae2014-08-09 15:47:05 -0600374 uint32_t elements_count;
375 uint32_t elements_size;
Shawn Willden37012132014-08-19 08:15:57 -0600376 if (!copy_uint32_from_buf(buf_ptr, end, &elements_count) ||
Shawn Willden172f8c92014-08-17 07:50:34 -0600377 !copy_uint32_from_buf(buf_ptr, end, &elements_size)) {
Shawn Willden58e1a542014-08-08 21:58:29 -0600378 set_invalid(MALFORMED_DATA);
Shawn Willden5ada7b62014-07-29 09:44:17 -0600379 return false;
380 }
381
Shawn Willden834e8072014-08-09 16:38:53 -0600382 // Note that the following validation of elements_count is weak, but it prevents allocation of
383 // elems_ arrays which are clearly too large to be reasonable.
Shawn Willden62de2662014-08-20 14:14:49 -0600384 if (static_cast<ptrdiff_t>(elements_size) > end - *buf_ptr ||
385 elements_count * sizeof(uint32_t) > elements_size) {
Shawn Willden834e8072014-08-09 16:38:53 -0600386 set_invalid(MALFORMED_DATA);
387 return false;
388 }
389
Shawn Willden37012132014-08-19 08:15:57 -0600390 if (!reserve_elems(elements_count))
Shawn Willden5ada7b62014-07-29 09:44:17 -0600391 return false;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600392
Shawn Willden8d336ae2014-08-09 15:47:05 -0600393 uint8_t* indirect_end = indirect_data_ + indirect_data_size_;
Shawn Willden172f8c92014-08-17 07:50:34 -0600394 const uint8_t* elements_end = *buf_ptr + elements_size;
Shawn Willden8d336ae2014-08-09 15:47:05 -0600395 for (size_t i = 0; i < elements_count; ++i) {
Shawn Willden172f8c92014-08-17 07:50:34 -0600396 if (!deserialize(elems_ + i, buf_ptr, elements_end, indirect_data_, indirect_end)) {
Shawn Willden8d336ae2014-08-09 15:47:05 -0600397 set_invalid(MALFORMED_DATA);
398 return false;
399 }
400 }
Shawn Willden37012132014-08-19 08:15:57 -0600401 elems_size_ = elements_count;
402 return true;
403}
Shawn Willden8d336ae2014-08-09 15:47:05 -0600404
Shawn Willden37012132014-08-19 08:15:57 -0600405bool AuthorizationSet::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
406 FreeData();
407
408 if (!DeserializeIndirectData(buf_ptr, end) || !DeserializeElementsData(buf_ptr, end))
409 return false;
410
411 if (indirect_data_size_ != ComputeIndirectDataSize(elems_, elems_size_)) {
Shawn Willden58e1a542014-08-08 21:58:29 -0600412 set_invalid(MALFORMED_DATA);
Shawn Willden5ada7b62014-07-29 09:44:17 -0600413 return false;
Shawn Willden58e1a542014-08-08 21:58:29 -0600414 }
Shawn Willden8d336ae2014-08-09 15:47:05 -0600415 return true;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600416}
417
Shawn Willden941d1c42014-12-11 13:57:02 -0700418void AuthorizationSet::Clear() {
Shawn Willden1834d5f2014-12-08 12:41:59 -0700419 memset_s(elems_, 0, elems_size_ * sizeof(keymaster_key_param_t));
420 memset_s(indirect_data_, 0, indirect_data_size_);
Shawn Willden941d1c42014-12-11 13:57:02 -0700421 elems_size_ = 0;
422 indirect_data_size_ = 0;
423}
424
425void AuthorizationSet::FreeData() {
426 Clear();
Shawn Willden58e1a542014-08-08 21:58:29 -0600427
428 delete[] elems_;
429 delete[] indirect_data_;
430
Shawn Willden5ada7b62014-07-29 09:44:17 -0600431 elems_ = NULL;
432 indirect_data_ = NULL;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600433 elems_capacity_ = 0;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600434 indirect_data_capacity_ = 0;
Shawn Willden37012132014-08-19 08:15:57 -0600435 error_ = OK;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600436}
437
438/* static */
439size_t AuthorizationSet::ComputeIndirectDataSize(const keymaster_key_param_t* elems, size_t count) {
440 size_t size = 0;
441 for (size_t i = 0; i < count; ++i) {
442 if (is_blob_tag(elems[i].tag)) {
443 size += elems[i].blob.data_length;
444 }
445 }
446 return size;
447}
448
449void AuthorizationSet::CopyIndirectData() {
Shawn Willden37012132014-08-19 08:15:57 -0600450 memset_s(indirect_data_, 0, indirect_data_capacity_);
Shawn Willden5ada7b62014-07-29 09:44:17 -0600451
452 uint8_t* indirect_data_pos = indirect_data_;
453 for (size_t i = 0; i < elems_size_; ++i) {
Shawn Willden37012132014-08-19 08:15:57 -0600454 assert(indirect_data_pos <= indirect_data_ + indirect_data_capacity_);
Shawn Willden5ada7b62014-07-29 09:44:17 -0600455 if (is_blob_tag(elems_[i].tag)) {
456 memcpy(indirect_data_pos, elems_[i].blob.data, elems_[i].blob.data_length);
457 elems_[i].blob.data = indirect_data_pos;
458 indirect_data_pos += elems_[i].blob.data_length;
459 }
460 }
Shawn Willden37012132014-08-19 08:15:57 -0600461 assert(indirect_data_pos == indirect_data_ + indirect_data_capacity_);
462 indirect_data_size_ = indirect_data_pos - indirect_data_;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600463}
464
Shawn Willden1fa5d592015-01-14 08:34:07 -0700465size_t AuthorizationSet::GetTagCount(keymaster_tag_t tag) const {
466 size_t count = 0;
467 for (int pos = -1; (pos = find(tag, pos)) != -1;)
468 ++count;
469 return count;
470}
471
Shawn Willden5ada7b62014-07-29 09:44:17 -0600472bool AuthorizationSet::GetTagValueEnum(keymaster_tag_t tag, uint32_t* val) const {
473 int pos = find(tag);
474 if (pos == -1) {
475 return false;
476 }
Shawn Willdenebf627f2014-08-12 11:15:29 -0600477 *val = elems_[pos].enumerated;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600478 return true;
479}
480
481bool AuthorizationSet::GetTagValueEnumRep(keymaster_tag_t tag, size_t instance,
482 uint32_t* val) const {
483 size_t count = 0;
484 int pos = -1;
485 while (count <= instance) {
486 pos = find(tag, pos);
487 if (pos == -1) {
488 return false;
489 }
490 ++count;
491 }
Shawn Willdenebf627f2014-08-12 11:15:29 -0600492 *val = elems_[pos].enumerated;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600493 return true;
494}
495
496bool AuthorizationSet::GetTagValueInt(keymaster_tag_t tag, uint32_t* val) const {
497 int pos = find(tag);
498 if (pos == -1) {
499 return false;
500 }
Shawn Willdenebf627f2014-08-12 11:15:29 -0600501 *val = elems_[pos].integer;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600502 return true;
503}
504
505bool AuthorizationSet::GetTagValueIntRep(keymaster_tag_t tag, size_t instance,
506 uint32_t* val) const {
507 size_t count = 0;
508 int pos = -1;
509 while (count <= instance) {
510 pos = find(tag, pos);
511 if (pos == -1) {
512 return false;
513 }
514 ++count;
515 }
Shawn Willdenebf627f2014-08-12 11:15:29 -0600516 *val = elems_[pos].integer;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600517 return true;
518}
519
520bool AuthorizationSet::GetTagValueLong(keymaster_tag_t tag, uint64_t* val) const {
521 int pos = find(tag);
522 if (pos == -1) {
523 return false;
524 }
Shawn Willdenebf627f2014-08-12 11:15:29 -0600525 *val = elems_[pos].long_integer;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600526 return true;
527}
528
529bool AuthorizationSet::GetTagValueDate(keymaster_tag_t tag, uint64_t* val) const {
530 int pos = find(tag);
531 if (pos == -1) {
532 return false;
533 }
Shawn Willdenebf627f2014-08-12 11:15:29 -0600534 *val = elems_[pos].date_time;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600535 return true;
536}
537
538bool AuthorizationSet::GetTagValueBlob(keymaster_tag_t tag, keymaster_blob_t* val) const {
539 int pos = find(tag);
540 if (pos == -1) {
541 return false;
542 }
Shawn Willdenebf627f2014-08-12 11:15:29 -0600543 *val = elems_[pos].blob;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600544 return true;
545}
546
Shawn Willdendfa1c032015-02-07 00:39:01 -0700547bool AuthorizationSet::GetTagValueBool(keymaster_tag_t tag) const {
548 int pos = find(tag);
549 if (pos == -1) {
550 return false;
551 }
552 assert(elems_[pos].boolean);
553 return elems_[pos].boolean;
554}
555
Shawn Willden5ada7b62014-07-29 09:44:17 -0600556} // namespace keymaster