Coverage Report

Created: 2022-07-22 12:05

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