Coverage Report

Created: 2021-07-20 18:14

/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
28.8k
{
12
28.8k
        return calloc(1, sizeof(fido_blob_t));
13
28.8k
}
14
15
void
16
fido_blob_reset(fido_blob_t *b)
17
1.17M
{
18
1.17M
        freezero(b->ptr, b->len);
19
1.17M
        explicit_bzero(b, sizeof(*b));
20
1.17M
}
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
215k
27
215k
        if (ptr == NULL || len == 0) {
28
8.16k
                fido_log_debug("%s: ptr=%p, len=%zu", __func__,
29
8.16k
                    (const void *)ptr, len);
30
8.16k
                return -1;
31
8.16k
        }
32
206k
33
206k
        if ((b->ptr = malloc(len)) == NULL) {
34
705
                fido_log_debug("%s: malloc", __func__);
35
705
                return -1;
36
705
        }
37
206k
38
206k
        memcpy(b->ptr, ptr, len);
39
206k
        b->len = len;
40
206k
41
206k
        return 0;
42
206k
}
43
44
int
45
fido_blob_append(fido_blob_t *b, const u_char *ptr, size_t len)
46
691
{
47
691
        u_char *tmp;
48
691
49
691
        if (ptr == NULL || len == 0) {
50
20
                fido_log_debug("%s: ptr=%p, len=%zu", __func__,
51
20
                    (const void *)ptr, len);
52
20
                return -1;
53
20
        }
54
671
        if (SIZE_MAX - b->len < len) {
55
0
                fido_log_debug("%s: overflow", __func__);
56
0
                return -1;
57
0
        }
58
671
        if ((tmp = realloc(b->ptr, b->len + len)) == NULL) {
59
0
                fido_log_debug("%s: realloc", __func__);
60
0
                return -1;
61
0
        }
62
671
        b->ptr = tmp;
63
671
        memcpy(&b->ptr[b->len], ptr, len);
64
671
        b->len += len;
65
671
66
671
        return 0;
67
671
}
68
69
void
70
fido_blob_free(fido_blob_t **bp)
71
45.0k
{
72
45.0k
        fido_blob_t *b;
73
45.0k
74
45.0k
        if (bp == NULL || (b = *bp) == NULL)
75
45.0k
                return;
76
28.7k
77
28.7k
        fido_blob_reset(b);
78
28.7k
        free(b);
79
28.7k
        *bp = NULL;
80
28.7k
}
81
82
void
83
fido_free_blob_array(fido_blob_array_t *array)
84
56.8k
{
85
56.8k
        if (array->ptr == NULL)
86
56.8k
                return;
87
2.47k
88
147k
        for (size_t i = 0; i < array->len; i++) {
89
144k
                fido_blob_t *b = &array->ptr[i];
90
144k
                freezero(b->ptr, b->len);
91
144k
                b->ptr = NULL;
92
144k
        }
93
2.47k
94
2.47k
        free(array->ptr);
95
2.47k
        array->ptr = NULL;
96
2.47k
        array->len = 0;
97
2.47k
}
98
99
cbor_item_t *
100
fido_blob_encode(const fido_blob_t *b)
101
7.46k
{
102
7.46k
        if (b == NULL || b->ptr == NULL)
103
7.46k
                return NULL;
104
7.42k
105
7.42k
        return cbor_build_bytestring(b->ptr, b->len);
106
7.42k
}
107
108
int
109
fido_blob_decode(const cbor_item_t *item, fido_blob_t *b)
110
8.01k
{
111
8.01k
        return cbor_bytestring_copy(item, &b->ptr, &b->len);
112
8.01k
}
113
114
int
115
fido_blob_is_empty(const fido_blob_t *b)
116
44.7k
{
117
44.7k
        return b->ptr == NULL || b->len == 0;
118
44.7k
}
119
120
int
121
fido_blob_serialise(fido_blob_t *b, const cbor_item_t *item)
122
838
{
123
838
        size_t alloc;
124
838
125
838
        if (!fido_blob_is_empty(b))
126
0
                return -1;
127
838
        if ((b->len = cbor_serialize_alloc(item, &b->ptr, &alloc)) == 0) {
128
5
                b->ptr = NULL;
129
5
                return -1;
130
5
        }
131
833
132
833
        return 0;
133
833
}