summaryrefslogtreecommitdiffstats
path: root/src/nfc/qapduutils.cpp
blob: 13243ea0ed5317ca82fed35a4365f111759bd776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
// Qt-Security score:critical reason:data-parser

#include "qapduutils_p.h"
#include <QtCore/QtEndian>

QT_BEGIN_NAMESPACE

/*
    Utilities for handling smart card application protocol data units (APDU).

    The structure of APDUs is defined by ISO/IEC 7816-4 Organization, security
    and commands for interchange. The summary can be found on Wikipedia:

        https://en.wikipedia.org/wiki/Smart_card_application_protocol_data_unit
*/

/*
    Parses a response APDU from the raw data.

    If the data is too short to contain SW bytes, the returned responses SW
    is set to QResponseApdu::Empty.
*/
QResponseApdu::QResponseApdu(const QByteArray &response)
{
    const auto view = qToByteArrayViewIgnoringNull(response);
    if (view.size() < 2) {
        m_status = Empty;
        m_data = response;
    } else {
        const auto dataSize = view.size() - 2;
        m_status = qFromBigEndian(qFromUnaligned<uint16_t>(view.data() + dataSize));
        m_data = response.left(dataSize);
    }
}

/*
    Builds a command APDU from components according to ISO/IEC 7816.
*/
QByteArray QCommandApdu::build(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2,
                               QByteArrayView data, uint16_t ne)
{
    Q_ASSERT(data.size() <= 0xFFFF);

    QByteArray apdu;
    apdu.append(static_cast<char>(cla));
    apdu.append(static_cast<char>(ins));
    apdu.append(static_cast<char>(p1));
    apdu.append(static_cast<char>(p2));

    bool extendedLc = false;
    uint16_t nc = data.size();

    if (nc > 0) {
        if (nc < 256) {
            apdu.append(static_cast<char>(nc));
        } else {
            extendedLc = true;
            apdu.append('\0');
            apdu.append(static_cast<char>(nc >> 8));
            apdu.append(static_cast<char>(nc & 0xFF));
        }
        apdu.append(data);
    }

    if (ne) {
        if (ne < 256) {
            apdu.append(static_cast<char>(ne));
        } else if (ne == 256) {
            apdu.append(static_cast<char>('\0'));
        } else {
            if (!extendedLc)
                apdu.append('\0');
            apdu.append(static_cast<char>(ne >> 8));
            apdu.append(static_cast<char>(ne & 0xFF));
        }
    }

    return apdu;
}

QT_END_NAMESPACE