Coverage Report

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