1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/*
* Copyright (c) 2018 Yubico AB. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "fido.h"
fido_blob_t *
fido_blob_new(void)
{
return calloc(1, sizeof(fido_blob_t));
}
void
fido_blob_reset(fido_blob_t *b)
{
freezero(b->ptr, b->len);
explicit_bzero(b, sizeof(*b));
}
int
fido_blob_set(fido_blob_t *b, const u_char *ptr, size_t len)
{
fido_blob_reset(b);
if (ptr == NULL || len == 0) {
fido_log_debug("%s: ptr=%p, len=%zu", __func__,
(const void *)ptr, len);
return -1;
}
if ((b->ptr = malloc(len)) == NULL) {
fido_log_debug("%s: malloc", __func__);
return -1;
}
memcpy(b->ptr, ptr, len);
b->len = len;
return 0;
}
int
fido_blob_append(fido_blob_t *b, const u_char *ptr, size_t len)
{
u_char *tmp;
if (ptr == NULL || len == 0) {
fido_log_debug("%s: ptr=%p, len=%zu", __func__,
(const void *)ptr, len);
return -1;
}
if (SIZE_MAX - b->len < len) {
fido_log_debug("%s: overflow", __func__);
return -1;
}
if ((tmp = realloc(b->ptr, b->len + len)) == NULL) {
fido_log_debug("%s: realloc", __func__);
return -1;
}
b->ptr = tmp;
memcpy(&b->ptr[b->len], ptr, len);
b->len += len;
return 0;
}
void
fido_blob_free(fido_blob_t **bp)
{
fido_blob_t *b;
if (bp == NULL || (b = *bp) == NULL)
return;
fido_blob_reset(b);
free(b);
*bp = NULL;
}
void
fido_free_blob_array(fido_blob_array_t *array)
{
if (array->ptr == NULL)
return;
for (size_t i = 0; i < array->len; i++) {
fido_blob_t *b = &array->ptr[i];
freezero(b->ptr, b->len);
b->ptr = NULL;
}
free(array->ptr);
array->ptr = NULL;
array->len = 0;
}
cbor_item_t *
fido_blob_encode(const fido_blob_t *b)
{
if (b == NULL || b->ptr == NULL)
return NULL;
return cbor_build_bytestring(b->ptr, b->len);
}
int
fido_blob_decode(const cbor_item_t *item, fido_blob_t *b)
{
return cbor_bytestring_copy(item, &b->ptr, &b->len);
}
int
fido_blob_is_empty(const fido_blob_t *b)
{
return b->ptr == NULL || b->len == 0;
}
int
fido_blob_serialise(fido_blob_t *b, const cbor_item_t *item)
{
size_t alloc;
if (!fido_blob_is_empty(b))
return -1;
if ((b->len = cbor_serialize_alloc(item, &b->ptr, &alloc)) == 0) {
b->ptr = NULL;
return -1;
}
return 0;
}
|