Coverage Report

Created: 2024-06-03 09:43

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