2 * Copyright (c) 2018 Yubico AB. All rights reserved.
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file.
5 * SPDX-License-Identifier: BSD-2-Clause
13 return calloc(1, sizeof(fido_blob_t));
17 fido_blob_reset(fido_blob_t *b)
19 freezero(b->ptr, b->len);
20 explicit_bzero(b, sizeof(*b));
24 fido_blob_set(fido_blob_t *b, const u_char *ptr, size_t len)
28 if (ptr == NULL || len == 0) {
29 fido_log_debug("%s: ptr=%p, len=%zu", __func__,
30 (const void *)ptr, len);
34 if ((b->ptr = malloc(len)) == NULL) {
35 fido_log_debug("%s: malloc", __func__);
39 memcpy(b->ptr, ptr, len);
46 fido_blob_append(fido_blob_t *b, const u_char *ptr, size_t len)
50 if (ptr == NULL || len == 0) {
51 fido_log_debug("%s: ptr=%p, len=%zu", __func__,
52 (const void *)ptr, len);
55 if (SIZE_MAX - b->len < len) {
56 fido_log_debug("%s: overflow", __func__);
59 if ((tmp = realloc(b->ptr, b->len + len)) == NULL) {
60 fido_log_debug("%s: realloc", __func__);
64 memcpy(&b->ptr[b->len], ptr, len);
71 fido_blob_free(fido_blob_t **bp)
75 if (bp == NULL || (b = *bp) == NULL)
84 fido_free_blob_array(fido_blob_array_t *array)
86 if (array->ptr == NULL)
89 for (size_t i = 0; i < array->len; i++) {
90 fido_blob_t *b = &array->ptr[i];
91 freezero(b->ptr, b->len);
101 fido_blob_encode(const fido_blob_t *b)
103 if (b == NULL || b->ptr == NULL)
106 return cbor_build_bytestring(b->ptr, b->len);
110 fido_blob_decode(const cbor_item_t *item, fido_blob_t *b)
112 return cbor_bytestring_copy(item, &b->ptr, &b->len);
116 fido_blob_is_empty(const fido_blob_t *b)
118 return b->ptr == NULL || b->len == 0;
122 fido_blob_serialise(fido_blob_t *b, const cbor_item_t *item)
126 if (!fido_blob_is_empty(b))
128 if ((b->len = cbor_serialize_alloc(item, &b->ptr, &alloc)) == 0) {