Coverage Report

Created: 2025-03-01 02:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/libfido2/src/iso7816.c
Line
Count
Source
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
iso7816_apdu_t *
11
iso7816_new(uint8_t cla, uint8_t ins, uint8_t p1, uint16_t payload_len)
12
44.6k
{
13
44.6k
        iso7816_apdu_t *apdu;
14
44.6k
        size_t alloc_len;
15
16
44.6k
        alloc_len = sizeof(iso7816_apdu_t) + payload_len + 2; /* le1 le2 */
17
44.6k
        if ((apdu = calloc(1, alloc_len)) == NULL)
18
626
                return NULL;
19
43.9k
        apdu->alloc_len = alloc_len;
20
43.9k
        apdu->payload_len = payload_len;
21
43.9k
        apdu->payload_ptr = apdu->payload;
22
43.9k
        apdu->header.cla = cla;
23
43.9k
        apdu->header.ins = ins;
24
43.9k
        apdu->header.p1 = p1;
25
43.9k
        apdu->header.lc2 = (uint8_t)((payload_len >> 8) & 0xff);
26
43.9k
        apdu->header.lc3 = (uint8_t)(payload_len & 0xff);
27
28
43.9k
        return apdu;
29
44.6k
}
30
31
void
32
iso7816_free(iso7816_apdu_t **apdu_p)
33
48.1k
{
34
48.1k
        iso7816_apdu_t *apdu;
35
36
48.1k
        if (apdu_p == NULL || (apdu = *apdu_p) == NULL)
37
4.11k
                return;
38
43.9k
        freezero(apdu, apdu->alloc_len);
39
43.9k
        *apdu_p = NULL;
40
43.9k
}
41
42
int
43
iso7816_add(iso7816_apdu_t *apdu, const void *buf, size_t cnt)
44
80.4k
{
45
80.4k
        if (cnt > apdu->payload_len || cnt > UINT16_MAX)
46
0
                return -1;
47
80.4k
        memcpy(apdu->payload_ptr, buf, cnt);
48
80.4k
        apdu->payload_ptr += cnt;
49
80.4k
        apdu->payload_len = (uint16_t)(apdu->payload_len - cnt);
50
51
80.4k
        return 0;
52
80.4k
}
53
54
const unsigned char *
55
iso7816_ptr(const iso7816_apdu_t *apdu)
56
47.2k
{
57
47.2k
        return (const unsigned char *)&apdu->header;
58
47.2k
}
59
60
size_t
61
iso7816_len(const iso7816_apdu_t *apdu)
62
47.2k
{
63
47.2k
        return apdu->alloc_len - offsetof(iso7816_apdu_t, header) -
64
            (sizeof(iso7816_apdu_t) - offsetof(iso7816_apdu_t, payload));
65
47.2k
}