Coverage Report

Created: 2022-04-27 14:33

/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
26.0k
{
12
26.0k
        return calloc(1, sizeof(fido_blob_t));
13
26.0k
}
14
15
void
16
fido_blob_reset(fido_blob_t *b)
17
1.30M
{
18
1.30M
        freezero(b->ptr, b->len);
19
1.30M
        explicit_bzero(b, sizeof(*b));
20
1.30M
}
21
22
int
23
fido_blob_set(fido_blob_t *b, const u_char *ptr, size_t len)
24
215k
{
25
215k
        fido_blob_reset(b);
26
27
215k
        if (ptr == NULL || len == 0) {
28
15.4k
                fido_log_debug("%s: ptr=%p, len=%zu", __func__,
29
15.4k
                    (const void *)ptr, len);
30
15.4k
                return -1;
31
15.4k
        }
32
33
199k
        if ((b->ptr = malloc(len)) == NULL) {
34
636
                fido_log_debug("%s: malloc", __func__);
35
636
                return -1;
36
636
        }
37
38
199k
        memcpy(b->ptr, ptr, len);
39
199k
        b->len = len;
40
41
199k
        return 0;
42
199k
}
43
44
int
45
fido_blob_append(fido_blob_t *b, const u_char *ptr, size_t len)
46
365
{
47
365
        u_char *tmp;
48
49
365
        if (ptr == NULL || len == 0) {
50
11
                fido_log_debug("%s: ptr=%p, len=%zu", __func__,
51
11
                    (const void *)ptr, len);
52
11
                return -1;
53
11
        }
54
354
        if (SIZE_MAX - b->len < len) {
55
0
                fido_log_debug("%s: overflow", __func__);
56
0
                return -1;
57
0
        }
58
354
        if ((tmp = realloc(b->ptr, b->len + len)) == NULL) {
59
3
                fido_log_debug("%s: realloc", __func__);
60
3
                return -1;
61
3
        }
62
351
        b->ptr = tmp;
63
351
        memcpy(&b->ptr[b->len], ptr, len);
64
351
        b->len += len;
65
66
351
        return 0;
67
354
}
68
69
void
70
fido_blob_free(fido_blob_t **bp)
71
44.8k
{
72
44.8k
        fido_blob_t *b;
73
74
44.8k
        if (bp == NULL || (b = *bp) == NULL)
75
18.9k
                return;
76
77
25.9k
        fido_blob_reset(b);
78
25.9k
        free(b);
79
25.9k
        *bp = NULL;
80
25.9k
}
81
82
void
83
fido_free_blob_array(fido_blob_array_t *array)
84
58.6k
{
85
58.6k
        if (array->ptr == NULL)
86
55.7k
                return;
87
88
138k
        for (size_t i = 0; i < array->len; i++) {
89
135k
                fido_blob_t *b = &array->ptr[i];
90
135k
                freezero(b->ptr, b->len);
91
135k
                b->ptr = NULL;
92
135k
        }
93
94
2.94k
        free(array->ptr);
95
2.94k
        array->ptr = NULL;
96
2.94k
        array->len = 0;
97
2.94k
}
98
99
cbor_item_t *
100
fido_blob_encode(const fido_blob_t *b)
101
7.39k
{
102
7.39k
        if (b == NULL || b->ptr == NULL)
103
21
                return NULL;
104
105
7.37k
        return cbor_build_bytestring(b->ptr, b->len);
106
7.39k
}
107
108
int
109
fido_blob_decode(const cbor_item_t *item, fido_blob_t *b)
110
8.39k
{
111
8.39k
        return cbor_bytestring_copy(item, &b->ptr, &b->len);
112
8.39k
}
113
114
int
115
fido_blob_is_empty(const fido_blob_t *b)
116
46.2k
{
117
46.2k
        return b->ptr == NULL || b->len == 0;
118
46.2k
}
119
120
int
121
fido_blob_serialise(fido_blob_t *b, const cbor_item_t *item)
122
566
{
123
566
        size_t alloc;
124
125
566
        if (!fido_blob_is_empty(b))
126
0
                return -1;
127
566
        if ((b->len = cbor_serialize_alloc(item, &b->ptr, &alloc)) == 0) {
128
3
                b->ptr = NULL;
129
3
                return -1;
130
3
        }
131
132
563
        return 0;
133
566
}