Coverage Report

Created: 2022-07-22 12:05

/libfido2/src/blob.c
Line
Count
Source (jump to first uncovered line)
1
/*
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
 */
6
7
#include "fido.h"
8
9
fido_blob_t *
10
fido_blob_new(void)
11
12.5k
{
12
12.5k
        return calloc(1, sizeof(fido_blob_t));
13
12.5k
}
14
15
void
16
fido_blob_reset(fido_blob_t *b)
17
1.03M
{
18
1.03M
        freezero(b->ptr, b->len);
19
1.03M
        explicit_bzero(b, sizeof(*b));
20
1.03M
}
21
22
int
23
fido_blob_set(fido_blob_t *b, const u_char *ptr, size_t len)
24
179k
{
25
179k
        fido_blob_reset(b);
26
27
179k
        if (ptr == NULL || len == 0) {
28
10.8k
                fido_log_debug("%s: ptr=%p, len=%zu", __func__,
29
10.8k
                    (const void *)ptr, len);
30
10.8k
                return -1;
31
10.8k
        }
32
33
168k
        if ((b->ptr = malloc(len)) == NULL) {
34
499
                fido_log_debug("%s: malloc", __func__);
35
499
                return -1;
36
499
        }
37
38
168k
        memcpy(b->ptr, ptr, len);
39
168k
        b->len = len;
40
41
168k
        return 0;
42
168k
}
43
44
int
45
fido_blob_append(fido_blob_t *b, const u_char *ptr, size_t len)
46
370
{
47
370
        u_char *tmp;
48
49
370
        if (ptr == NULL || len == 0) {
50
8
                fido_log_debug("%s: ptr=%p, len=%zu", __func__,
51
8
                    (const void *)ptr, len);
52
8
                return -1;
53
8
        }
54
362
        if (SIZE_MAX - b->len < len) {
55
0
                fido_log_debug("%s: overflow", __func__);
56
0
                return -1;
57
0
        }
58
362
        if ((tmp = realloc(b->ptr, b->len + len)) == NULL) {
59
1
                fido_log_debug("%s: realloc", __func__);
60
1
                return -1;
61
1
        }
62
361
        b->ptr = tmp;
63
361
        memcpy(&b->ptr[b->len], ptr, len);
64
361
        b->len += len;
65
66
361
        return 0;
67
362
}
68
69
void
70
fido_blob_free(fido_blob_t **bp)
71
25.4k
{
72
25.4k
        fido_blob_t *b;
73
74
25.4k
        if (bp == NULL || (b = *bp) == NULL)
75
12.9k
                return;
76
77
12.4k
        fido_blob_reset(b);
78
12.4k
        free(b);
79
12.4k
        *bp = NULL;
80
12.4k
}
81
82
void
83
fido_free_blob_array(fido_blob_array_t *array)
84
43.4k
{
85
43.4k
        if (array->ptr == NULL)
86
41.4k
                return;
87
88
113k
        for (size_t i = 0; i < array->len; i++) {
89
111k
                fido_blob_t *b = &array->ptr[i];
90
111k
                freezero(b->ptr, b->len);
91
111k
                b->ptr = NULL;
92
111k
        }
93
94
2.04k
        free(array->ptr);
95
2.04k
        array->ptr = NULL;
96
2.04k
        array->len = 0;
97
2.04k
}
98
99
cbor_item_t *
100
fido_blob_encode(const fido_blob_t *b)
101
3.93k
{
102
3.93k
        if (b == NULL || b->ptr == NULL)
103
9
                return NULL;
104
105
3.92k
        return cbor_build_bytestring(b->ptr, b->len);
106
3.93k
}
107
108
int
109
fido_blob_decode(const cbor_item_t *item, fido_blob_t *b)
110
5.40k
{
111
5.40k
        return cbor_bytestring_copy(item, &b->ptr, &b->len);
112
5.40k
}
113
114
int
115
fido_blob_is_empty(const fido_blob_t *b)
116
44.1k
{
117
44.1k
        return b->ptr == NULL || b->len == 0;
118
44.1k
}
119
120
int
121
fido_blob_serialise(fido_blob_t *b, const cbor_item_t *item)
122
359
{
123
359
        size_t alloc;
124
125
359
        if (!fido_blob_is_empty(b))
126
0
                return -1;
127
359
        if ((b->len = cbor_serialize_alloc(item, &b->ptr, &alloc)) == 0) {
128
3
                b->ptr = NULL;
129
3
                return -1;
130
3
        }
131
132
356
        return 0;
133
359
}