Coverage Report

Created: 2023-09-23 17:42

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