Coverage Report

Created: 2021-10-21 13:35

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