Coverage Report

Created: 2021-07-20 18:14

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