Coverage Report

Created: 2023-09-23 17:42

/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
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#include "fido.h"
9
10
fido_blob_t *
11
fido_blob_new(void)
12
28.2k
{
13
28.2k
        return calloc(1, sizeof(fido_blob_t));
14
28.2k
}
15
16
void
17
fido_blob_reset(fido_blob_t *b)
18
1.38M
{
19
1.38M
        freezero(b->ptr, b->len);
20
1.38M
        explicit_bzero(b, sizeof(*b));
21
1.38M
}
22
23
int
24
fido_blob_set(fido_blob_t *b, const u_char *ptr, size_t len)
25
212k
{
26
212k
        fido_blob_reset(b);
27
28
212k
        if (ptr == NULL || len == 0) {
29
14.1k
                fido_log_debug("%s: ptr=%p, len=%zu", __func__,
30
14.1k
                    (const void *)ptr, len);
31
14.1k
                return -1;
32
14.1k
        }
33
34
198k
        if ((b->ptr = malloc(len)) == NULL) {
35
570
                fido_log_debug("%s: malloc", __func__);
36
570
                return -1;
37
570
        }
38
39
198k
        memcpy(b->ptr, ptr, len);
40
198k
        b->len = len;
41
42
198k
        return 0;
43
198k
}
44
45
int
46
fido_blob_append(fido_blob_t *b, const u_char *ptr, size_t len)
47
669
{
48
669
        u_char *tmp;
49
50
669
        if (ptr == NULL || len == 0) {
51
19
                fido_log_debug("%s: ptr=%p, len=%zu", __func__,
52
19
                    (const void *)ptr, len);
53
19
                return -1;
54
19
        }
55
650
        if (SIZE_MAX - b->len < len) {
56
0
                fido_log_debug("%s: overflow", __func__);
57
0
                return -1;
58
0
        }
59
650
        if ((tmp = realloc(b->ptr, b->len + len)) == NULL) {
60
3
                fido_log_debug("%s: realloc", __func__);
61
3
                return -1;
62
3
        }
63
647
        b->ptr = tmp;
64
647
        memcpy(&b->ptr[b->len], ptr, len);
65
647
        b->len += len;
66
67
647
        return 0;
68
650
}
69
70
void
71
fido_blob_free(fido_blob_t **bp)
72
47.4k
{
73
47.4k
        fido_blob_t *b;
74
75
47.4k
        if (bp == NULL || (b = *bp) == NULL)
76
19.3k
                return;
77
78
28.1k
        fido_blob_reset(b);
79
28.1k
        free(b);
80
28.1k
        *bp = NULL;
81
28.1k
}
82
83
void
84
fido_free_blob_array(fido_blob_array_t *array)
85
57.5k
{
86
57.5k
        if (array->ptr == NULL)
87
54.8k
                return;
88
89
132k
        for (size_t i = 0; i < array->len; i++) {
90
129k
                fido_blob_t *b = &array->ptr[i];
91
129k
                freezero(b->ptr, b->len);
92
129k
                b->ptr = NULL;
93
129k
        }
94
95
2.73k
        free(array->ptr);
96
2.73k
        array->ptr = NULL;
97
2.73k
        array->len = 0;
98
2.73k
}
99
100
cbor_item_t *
101
fido_blob_encode(const fido_blob_t *b)
102
7.21k
{
103
7.21k
        if (b == NULL || b->ptr == NULL)
104
30
                return NULL;
105
106
7.18k
        return cbor_build_bytestring(b->ptr, b->len);
107
7.21k
}
108
109
int
110
fido_blob_decode(const cbor_item_t *item, fido_blob_t *b)
111
10.6k
{
112
10.6k
        return cbor_bytestring_copy(item, &b->ptr, &b->len);
113
10.6k
}
114
115
int
116
fido_blob_is_empty(const fido_blob_t *b)
117
50.0k
{
118
50.0k
        return b->ptr == NULL || b->len == 0;
119
50.0k
}
120
121
int
122
fido_blob_serialise(fido_blob_t *b, const cbor_item_t *item)
123
796
{
124
796
        size_t alloc;
125
126
796
        if (!fido_blob_is_empty(b))
127
0
                return -1;
128
796
        if ((b->len = cbor_serialize_alloc(item, &b->ptr, &alloc)) == 0) {
129
6
                b->ptr = NULL;
130
6
                return -1;
131
6
        }
132
133
790
        return 0;
134
796
}