blob: 35a12b266b6e87ce45f32022075428b74939f758 [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
23#include "authorization_set.h"
Shawn Willden74aff352014-08-11 14:08:31 -060024#include "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)
35 : 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 Willden8d336ae2014-08-09 15:47:05 -060039AuthorizationSet::~AuthorizationSet() {
40 FreeData();
41}
Shawn Willden58e1a542014-08-08 21:58:29 -060042
Shawn Willden5ada7b62014-07-29 09:44:17 -060043bool AuthorizationSet::Reinitialize(const keymaster_key_param_t* elems, const size_t count) {
44 FreeData();
45
46 elems_size_ = count;
47 elems_capacity_ = count;
48 indirect_data_size_ = ComputeIndirectDataSize(elems, count);
49 indirect_data_capacity_ = indirect_data_size_;
Shawn Willden58e1a542014-08-08 21:58:29 -060050 error_ = OK;
Shawn Willden5ada7b62014-07-29 09:44:17 -060051
52 indirect_data_ = new uint8_t[indirect_data_size_];
53 elems_ = new keymaster_key_param_t[elems_size_];
54 if (indirect_data_ == NULL || elems_ == NULL) {
55 set_invalid(ALLOCATION_FAILURE);
56 return false;
57 }
Shawn Willden5ada7b62014-07-29 09:44:17 -060058
59 memcpy(elems_, elems, sizeof(keymaster_key_param_t) * elems_size_);
60 CopyIndirectData();
Shawn Willden5ada7b62014-07-29 09:44:17 -060061 return true;
62}
63
Shawn Willden5ada7b62014-07-29 09:44:17 -060064void AuthorizationSet::set_invalid(Error error) {
65 error_ = error;
66 FreeData();
67}
68
69int AuthorizationSet::find(keymaster_tag_t tag, int begin) const {
70 int i = ++begin;
Shawn Willden8d336ae2014-08-09 15:47:05 -060071 while (i < (int)elems_size_ && elems_[i].tag != tag)
72 ++i;
Shawn Willden5ada7b62014-07-29 09:44:17 -060073 if (i == (int)elems_size_)
74 return -1;
75 else
76 return i;
77}
78
79keymaster_key_param_t empty;
Shawn Willden5ada7b62014-07-29 09:44:17 -060080keymaster_key_param_t AuthorizationSet::operator[](int at) const {
81 if (at < (int)elems_size_) {
Shawn Willden8d336ae2014-08-09 15:47:05 -060082 return elems_[at];
Shawn Willden5ada7b62014-07-29 09:44:17 -060083 }
84 memset(&empty, 0, sizeof(empty));
85 return empty;
86}
87
Shawn Willden76364712014-08-11 17:48:04 -060088template <typename T> int comparator(const T& a, const T& b) {
89 if (a < b)
90 return -1;
91 else if (a > b)
92 return 1;
93 else
94 return 0;
95}
96
97static int param_comparator(const void* a, const void* b) {
98 const keymaster_key_param_t* lhs = static_cast<const keymaster_key_param_t*>(a);
99 const keymaster_key_param_t* rhs = static_cast<const keymaster_key_param_t*>(b);
100
101 if (lhs->tag < rhs->tag)
102 return -1;
103 else if (lhs->tag > rhs->tag)
104 return 1;
105 else
106 switch (keymaster_tag_get_type(lhs->tag)) {
107 default:
108 case KM_INVALID:
109 return 0;
110 case KM_ENUM:
111 case KM_ENUM_REP:
112 return comparator(lhs->enumerated, rhs->enumerated);
113 case KM_INT:
114 case KM_INT_REP:
115 return comparator(lhs->integer, rhs->integer);
116 case KM_LONG:
117 return comparator(lhs->long_integer, rhs->long_integer);
118 case KM_DATE:
119 return comparator(lhs->date_time, rhs->date_time);
120 case KM_BOOL:
121 return comparator(lhs->boolean, rhs->boolean);
122 case KM_BIGNUM:
123 case KM_BYTES: {
124 size_t min_len = lhs->blob.data_length;
125 if (rhs->blob.data_length < min_len)
126 min_len = rhs->blob.data_length;
127
128 if (lhs->blob.data_length == rhs->blob.data_length && min_len > 0)
129 return memcmp(lhs->blob.data, rhs->blob.data, min_len);
130 int cmp_result = memcmp(lhs->blob.data, rhs->blob.data, min_len);
131 if (cmp_result == 0) {
132 // The blobs are equal up to the length of the shortest (which may have length 0),
133 // so the shorter is less, the longer is greater and if they have the same length
134 // they're identical.
135 return comparator(lhs->blob.data_length, rhs->blob.data_length);
136 }
137 return cmp_result;
138 } break;
139 }
140}
141
Shawn Willden5ada7b62014-07-29 09:44:17 -0600142bool AuthorizationSet::push_back(keymaster_key_param_t elem) {
143 if (elems_size_ >= elems_capacity_) {
Shawn Willden58e1a542014-08-08 21:58:29 -0600144 size_t new_capacity = elems_capacity_ ? elems_capacity_ * 2 : STARTING_ELEMS_CAPACITY;
145 keymaster_key_param_t* new_elems = new keymaster_key_param_t[new_capacity];
146 if (new_elems == NULL) {
147 set_invalid(ALLOCATION_FAILURE);
Shawn Willden5ada7b62014-07-29 09:44:17 -0600148 return false;
149 }
Shawn Willden58e1a542014-08-08 21:58:29 -0600150 memcpy(new_elems, elems_, sizeof(*elems_) * elems_size_);
151 delete[] elems_;
152 elems_ = new_elems;
153 elems_capacity_ = new_capacity;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600154 }
155
156 if (is_blob_tag(elem.tag)) {
157 if (indirect_data_capacity_ - indirect_data_size_ < elem.blob.data_length) {
Shawn Willden58e1a542014-08-08 21:58:29 -0600158 size_t new_capacity = 2 * (indirect_data_capacity_ + elem.blob.data_length);
159 uint8_t* new_data = new uint8_t[new_capacity];
160 if (new_data == false) {
161 set_invalid(ALLOCATION_FAILURE);
Shawn Willden5ada7b62014-07-29 09:44:17 -0600162 return false;
163 }
Shawn Willden58e1a542014-08-08 21:58:29 -0600164 memcpy(new_data, indirect_data_, indirect_data_size_);
Shawn Willden8d336ae2014-08-09 15:47:05 -0600165 // Fix up the data pointers to point into the new region.
166 for (size_t i = 0; i < elems_size_; ++i) {
167 if (is_blob_tag(elems_[i].tag))
168 elems_[i].blob.data = new_data + (elems_[i].blob.data - indirect_data_);
169 }
Shawn Willden58e1a542014-08-08 21:58:29 -0600170 delete[] indirect_data_;
171 indirect_data_ = new_data;
172 indirect_data_capacity_ = new_capacity;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600173 }
Shawn Willden58e1a542014-08-08 21:58:29 -0600174
Shawn Willden5ada7b62014-07-29 09:44:17 -0600175 memcpy(indirect_data_ + indirect_data_size_, elem.blob.data, elem.blob.data_length);
Shawn Willden8d336ae2014-08-09 15:47:05 -0600176 elem.blob.data = indirect_data_ + indirect_data_size_;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600177 indirect_data_size_ += elem.blob.data_length;
178 }
179
180 elems_[elems_size_++] = elem;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600181 return true;
182}
183
Shawn Willden8d336ae2014-08-09 15:47:05 -0600184static size_t serialized_size(const keymaster_key_param_t& param) {
185 switch (keymaster_tag_get_type(param.tag)) {
186 case KM_INVALID:
187 default:
188 return sizeof(uint32_t);
189 case KM_ENUM:
190 case KM_ENUM_REP:
191 case KM_INT:
192 case KM_INT_REP:
193 return sizeof(uint32_t) * 2;
194 case KM_LONG:
195 case KM_DATE:
196 return sizeof(uint32_t) + sizeof(uint64_t);
197 case KM_BOOL:
198 return sizeof(uint32_t) + 1;
199 break;
200 case KM_BIGNUM:
201 case KM_BYTES:
202 return sizeof(uint32_t) * 3;
203 }
Shawn Willden58e1a542014-08-08 21:58:29 -0600204}
205
Shawn Willden8d336ae2014-08-09 15:47:05 -0600206static uint8_t* serialize(const keymaster_key_param_t& param, uint8_t* buf, const uint8_t* end,
207 const uint8_t* indirect_base) {
208 buf = append_to_buf(buf, end, static_cast<uint32_t>(param.tag));
209 switch (keymaster_tag_get_type(param.tag)) {
210 case KM_INVALID:
211 break;
212 case KM_ENUM:
213 case KM_ENUM_REP:
214 buf = append_to_buf(buf, end, param.enumerated);
215 break;
216 case KM_INT:
217 case KM_INT_REP:
218 buf = append_to_buf(buf, end, param.integer);
219 break;
220 case KM_LONG:
221 buf = append_to_buf(buf, end, param.long_integer);
222 break;
223 case KM_DATE:
224 buf = append_to_buf(buf, end, param.date_time);
225 break;
226 case KM_BOOL:
227 if (buf < end)
228 *buf = static_cast<uint8_t>(param.boolean);
229 buf++;
230 break;
231 case KM_BIGNUM:
232 case KM_BYTES:
233 buf = append_to_buf(buf, end, static_cast<uint32_t>(param.blob.data_length));
234 buf = append_to_buf(buf, end, static_cast<uint32_t>(param.blob.data - indirect_base));
235 break;
236 }
237 return buf;
238}
239
240static bool deserialize(keymaster_key_param_t* param, const uint8_t** buf, const uint8_t* end,
241 const uint8_t* indirect_base, const uint8_t* indirect_end) {
242 uint32_t tag_val;
243 if (!copy_from_buf(buf, end, &tag_val))
244 return false;
245 param->tag = static_cast<keymaster_tag_t>(tag_val);
246
247 switch (keymaster_tag_get_type(param->tag)) {
248 default:
249 case KM_INVALID:
250 return false;
251 case KM_ENUM:
252 case KM_ENUM_REP:
253 return copy_from_buf(buf, end, &param->enumerated);
254 case KM_INT:
255 case KM_INT_REP:
256 return copy_from_buf(buf, end, &param->integer);
257 case KM_LONG:
258 return copy_from_buf(buf, end, &param->long_integer);
259 case KM_DATE:
260 return copy_from_buf(buf, end, &param->date_time);
261 break;
262 case KM_BOOL:
263 if (*buf < end) {
264 param->boolean = static_cast<bool>(**buf);
265 (*buf)++;
266 return true;
267 }
268 return false;
269
270 case KM_BIGNUM:
271 case KM_BYTES: {
272 uint32_t length;
273 uint32_t offset;
274 if (!copy_from_buf(buf, end, &length) || !copy_from_buf(buf, end, &offset))
275 return false;
276 if (static_cast<ptrdiff_t>(offset) > indirect_end - indirect_base ||
277 static_cast<ptrdiff_t>(offset + length) > indirect_end - indirect_base)
278 return false;
279 param->blob.data_length = length;
280 param->blob.data = indirect_base + offset;
281 return true;
282 }
283 }
284}
285
286size_t AuthorizationSet::SerializedSizeOfElements() const {
287 size_t size = 0;
288 for (size_t i = 0; i < elems_size_; ++i) {
289 size += serialized_size(elems_[i]);
290 }
291 return size;
292}
293
294size_t AuthorizationSet::SerializedSize() const {
295 return sizeof(uint32_t) + // Size of indirect_data_
296 indirect_data_size_ + // indirect_data_
297 sizeof(uint32_t) + // Number of elems_
298 sizeof(uint32_t) + // Size of elems_
299 SerializedSizeOfElements(); // elems_
300}
301
302uint8_t* AuthorizationSet::Serialize(uint8_t* buf, const uint8_t* end) const {
303 buf = append_size_and_data_to_buf(buf, end, indirect_data_, indirect_data_size_);
304 buf = append_to_buf(buf, end, static_cast<uint32_t>(elems_size_));
305 buf = append_to_buf(buf, end, static_cast<uint32_t>(SerializedSizeOfElements()));
306 for (size_t i = 0; i < elems_size_; ++i) {
307 buf = serialize(elems_[i], buf, end, indirect_data_);
308 }
309 return buf;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600310}
311
Shawn Willden58e1a542014-08-08 21:58:29 -0600312bool AuthorizationSet::Deserialize(const uint8_t** buf, const uint8_t* end) {
Shawn Willden5ada7b62014-07-29 09:44:17 -0600313 FreeData();
314
Shawn Willden8d336ae2014-08-09 15:47:05 -0600315 uint32_t elements_count;
316 uint32_t elements_size;
317 if (!copy_size_and_data_from_buf(buf, end, &indirect_data_size_, &indirect_data_) ||
318 !copy_from_buf(buf, end, &elements_count) || !copy_from_buf(buf, end, &elements_size)) {
Shawn Willden58e1a542014-08-08 21:58:29 -0600319 set_invalid(MALFORMED_DATA);
Shawn Willden5ada7b62014-07-29 09:44:17 -0600320 return false;
321 }
322
Shawn Willden834e8072014-08-09 16:38:53 -0600323 // Note that the following validation of elements_count is weak, but it prevents allocation of
324 // elems_ arrays which are clearly too large to be reasonable.
325 if (elements_size > end - *buf || elements_count * sizeof(uint32_t) > elements_size) {
326 set_invalid(MALFORMED_DATA);
327 return false;
328 }
329
Shawn Willden8d336ae2014-08-09 15:47:05 -0600330 elems_ = new keymaster_key_param_t[elements_count];
Shawn Willden5ada7b62014-07-29 09:44:17 -0600331 if (elems_ == NULL) {
332 set_invalid(ALLOCATION_FAILURE);
333 return false;
334 }
Shawn Willden5ada7b62014-07-29 09:44:17 -0600335
Shawn Willden8d336ae2014-08-09 15:47:05 -0600336 uint8_t* indirect_end = indirect_data_ + indirect_data_size_;
337 const uint8_t* elements_end = *buf + elements_size;
338 for (size_t i = 0; i < elements_count; ++i) {
339 if (!deserialize(elems_ + i, buf, elements_end, indirect_data_, indirect_end)) {
340 set_invalid(MALFORMED_DATA);
341 return false;
342 }
343 }
344
345 if (indirect_data_size_ != ComputeIndirectDataSize(elems_, elements_count)) {
Shawn Willden58e1a542014-08-08 21:58:29 -0600346 set_invalid(MALFORMED_DATA);
Shawn Willden5ada7b62014-07-29 09:44:17 -0600347 return false;
Shawn Willden58e1a542014-08-08 21:58:29 -0600348 }
Shawn Willden5ada7b62014-07-29 09:44:17 -0600349
Shawn Willden8d336ae2014-08-09 15:47:05 -0600350 elems_size_ = elements_count;
351 elems_capacity_ = elements_count;
352 indirect_data_capacity_ = indirect_data_size_;
Shawn Willden58e1a542014-08-08 21:58:29 -0600353 error_ = OK;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600354
Shawn Willden8d336ae2014-08-09 15:47:05 -0600355 return true;
Shawn Willden5ada7b62014-07-29 09:44:17 -0600356}
357
358void AuthorizationSet::FreeData() {
Shawn Willden58e1a542014-08-08 21:58:29 -0600359 if (elems_ != NULL)
Shawn Willden74aff352014-08-11 14:08:31 -0600360 memset_s(elems_, 0, elems_size_ * sizeof(keymaster_key_param_t));
Shawn Willden58e1a542014-08-08 21:58:29 -0600361 if (indirect_data_ != NULL)
Shawn Willden74aff352014-08-11 14:08:31 -0600362 memset_s(indirect_data_, 0, indirect_data_size_);
Shawn Willden58e1a542014-08-08 21:58:29 -0600363
364 delete[] elems_;
365 delete[] indirect_data_;
366
Shawn Willden5ada7b62014-07-29 09:44:17 -0600367 elems_ = NULL;
368 indirect_data_ = NULL;
369 elems_size_ = 0;
370 elems_capacity_ = 0;
371 indirect_data_size_ = 0;
372 indirect_data_capacity_ = 0;
373}
374
375/* static */
376size_t AuthorizationSet::ComputeIndirectDataSize(const keymaster_key_param_t* elems, size_t count) {
377 size_t size = 0;
378 for (size_t i = 0; i < count; ++i) {
379 if (is_blob_tag(elems[i].tag)) {
380 size += elems[i].blob.data_length;
381 }
382 }
383 return size;
384}
385
386void AuthorizationSet::CopyIndirectData() {
387 memset(indirect_data_, 0, indirect_data_size_);
388
389 uint8_t* indirect_data_pos = indirect_data_;
390 for (size_t i = 0; i < elems_size_; ++i) {
391 assert(indirect_data_pos <= indirect_data_ + indirect_data_size_);
392 if (is_blob_tag(elems_[i].tag)) {
393 memcpy(indirect_data_pos, elems_[i].blob.data, elems_[i].blob.data_length);
394 elems_[i].blob.data = indirect_data_pos;
395 indirect_data_pos += elems_[i].blob.data_length;
396 }
397 }
398 assert(indirect_data_pos == indirect_data_ + indirect_data_size_);
399}
400
Shawn Willden5ada7b62014-07-29 09:44:17 -0600401bool AuthorizationSet::GetTagValueEnum(keymaster_tag_t tag, uint32_t* val) const {
402 int pos = find(tag);
403 if (pos == -1) {
404 return false;
405 }
406 *val = (*this)[pos].enumerated;
407 return true;
408}
409
410bool AuthorizationSet::GetTagValueEnumRep(keymaster_tag_t tag, size_t instance,
411 uint32_t* val) const {
412 size_t count = 0;
413 int pos = -1;
414 while (count <= instance) {
415 pos = find(tag, pos);
416 if (pos == -1) {
417 return false;
418 }
419 ++count;
420 }
421 *val = (*this)[pos].enumerated;
422 return true;
423}
424
425bool AuthorizationSet::GetTagValueInt(keymaster_tag_t tag, uint32_t* val) const {
426 int pos = find(tag);
427 if (pos == -1) {
428 return false;
429 }
430 *val = (*this)[pos].integer;
431 return true;
432}
433
434bool AuthorizationSet::GetTagValueIntRep(keymaster_tag_t tag, size_t instance,
435 uint32_t* val) const {
436 size_t count = 0;
437 int pos = -1;
438 while (count <= instance) {
439 pos = find(tag, pos);
440 if (pos == -1) {
441 return false;
442 }
443 ++count;
444 }
445 *val = (*this)[pos].integer;
446 return true;
447}
448
449bool AuthorizationSet::GetTagValueLong(keymaster_tag_t tag, uint64_t* val) const {
450 int pos = find(tag);
451 if (pos == -1) {
452 return false;
453 }
454 *val = (*this)[pos].long_integer;
455 return true;
456}
457
458bool AuthorizationSet::GetTagValueDate(keymaster_tag_t tag, uint64_t* val) const {
459 int pos = find(tag);
460 if (pos == -1) {
461 return false;
462 }
463 *val = (*this)[pos].date_time;
464 return true;
465}
466
467bool AuthorizationSet::GetTagValueBlob(keymaster_tag_t tag, keymaster_blob_t* val) const {
468 int pos = find(tag);
469 if (pos == -1) {
470 return false;
471 }
472 *val = (*this)[pos].blob;
473 return true;
474}
475
476} // namespace keymaster