Coverage Report

Created: 2022-01-17 10:46

/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
32.0k
{
12
32.0k
        return calloc(1, sizeof(fido_blob_t));
13
32.0k
}
14
15
void
16
fido_blob_reset(fido_blob_t *b)
17
1.31M
{
18
1.31M
        freezero(b->ptr, b->len);
19
1.31M
        explicit_bzero(b, sizeof(*b));
20
1.31M
}
21
22
int
23
fido_blob_set(fido_blob_t *b, const u_char *ptr, size_t len)
24
221k
{
25
221k
        fido_blob_reset(b);
26
27
221k
        if (ptr == NULL || len == 0) {
28
15.7k
                fido_log_debug("%s: ptr=%p, len=%zu", __func__,
29
15.7k
                    (const void *)ptr, len);
30
15.7k
                return -1;
31
15.7k
        }
32
33
206k
        if ((b->ptr = malloc(len)) == NULL) {
34
633
                fido_log_debug("%s: malloc", __func__);
35
633
                return -1;
36
633
        }
37
38
205k
        memcpy(b->ptr, ptr, len);
39
205k
        b->len = len;
40
41
205k
        return 0;
42
205k
}
43
44
int
45
fido_blob_append(fido_blob_t *b, const u_char *ptr, size_t len)
46
644
{
47
644
        u_char *tmp;
48
49
644
        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
625
        if (SIZE_MAX - b->len < len) {
55
0
                fido_log_debug("%s: overflow", __func__);
56
0
                return -1;
57
0
        }
58
625
        if ((tmp = realloc(b->ptr, b->len + len)) == NULL) {
59
4
                fido_log_debug("%s: realloc", __func__);
60
4
                return -1;
61
4
        }
62
621
        b->ptr = tmp;
63
621
        memcpy(&b->ptr[b->len], ptr, len);
64
621
        b->len += len;
65
66
621
        return 0;
67
621
}
68
69
void
70
fido_blob_free(fido_blob_t **bp)
71
53.5k
{
72
53.5k
        fido_blob_t *b;
73
74
53.5k
        if (bp == NULL || (b = *bp) == NULL)
75
53.5k
                return;
76
77
31.8k
        fido_blob_reset(b);
78
31.8k
        free(b);
79
31.8k
        *bp = NULL;
80
31.8k
}
81
82
void
83
fido_free_blob_array(fido_blob_array_t *array)
84
58.4k
{
85
58.4k
        if (array->ptr == NULL)
86
58.4k
                return;
87
88
142k
        for (size_t i = 0; i < array->len; i++) {
89
139k
                fido_blob_t *b = &array->ptr[i];
90
139k
                freezero(b->ptr, b->len);
91
139k
                b->ptr = NULL;
92
139k
        }
93
94
3.05k
        free(array->ptr);
95
3.05k
        array->ptr = NULL;
96
3.05k
        array->len = 0;
97
3.05k
}
98
99
cbor_item_t *
100
fido_blob_encode(const fido_blob_t *b)
101
8.65k
{
102
8.65k
        if (b == NULL || b->ptr == NULL)
103
8.65k
                return NULL;
104
105
8.63k
        return cbor_build_bytestring(b->ptr, b->len);
106
8.63k
}
107
108
int
109
fido_blob_decode(const cbor_item_t *item, fido_blob_t *b)
110
9.87k
{
111
9.87k
        return cbor_bytestring_copy(item, &b->ptr, &b->len);
112
9.87k
}
113
114
int
115
fido_blob_is_empty(const fido_blob_t *b)
116
46.4k
{
117
46.4k
        return b->ptr == NULL || b->len == 0;
118
46.4k
}
119
120
int
121
fido_blob_serialise(fido_blob_t *b, const cbor_item_t *item)
122
848
{
123
848
        size_t alloc;
124
125
848
        if (!fido_blob_is_empty(b))
126
0
                return -1;
127
848
        if ((b->len = cbor_serialize_alloc(item, &b->ptr, &alloc)) == 0) {
128
5
                b->ptr = NULL;
129
5
                return -1;
130
5
        }
131
132
843
        return 0;
133
843
}