Coverage Report

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