Coverage Report

Created: 2022-04-27 14:33

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