Coverage Report

Created: 2021-03-26 11:35

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