Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qapduutils.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qapduutils_p.h"
5#include <QtCore/QtEndian>
6
8
9/*
10 Utilities for handling smart card application protocol data units (APDU).
11
12 The structure of APDUs is defined by ISO/IEC 7816-4 Organization, security
13 and commands for interchange. The summary can be found on Wikipedia:
14
15 https://en.wikipedia.org/wiki/Smart_card_application_protocol_data_unit
16*/
17
18/*
19 Parses a response APDU from the raw data.
20
21 If the data is too short to contain SW bytes, the returned responses SW
22 is set to QResponseApdu::Empty.
23*/
25{
26 if (response.size() < 2) {
27 m_status = Empty;
28 m_data = response;
29 } else {
30 const auto dataSize = response.size() - 2;
31 m_status = qFromBigEndian(qFromUnaligned<uint16_t>(response.constData() + dataSize));
32 m_data = response.left(dataSize);
33 }
34}
35
36/*
37 Builds a command APDU from components according to ISO/IEC 7816.
38*/
39QByteArray QCommandApdu::build(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2,
40 QByteArrayView data, uint16_t ne)
41{
42 Q_ASSERT(data.size() <= 0xFFFF);
43
44 QByteArray apdu;
45 apdu.append(static_cast<char>(cla));
46 apdu.append(static_cast<char>(ins));
47 apdu.append(static_cast<char>(p1));
48 apdu.append(static_cast<char>(p2));
49
50 bool extendedLc = false;
51 uint16_t nc = data.size();
52
53 if (nc > 0) {
54 if (nc < 256) {
55 apdu.append(static_cast<char>(nc));
56 } else {
57 extendedLc = true;
58 apdu.append('\0');
59 apdu.append(static_cast<char>(nc >> 8));
60 apdu.append(static_cast<char>(nc & 0xFF));
61 }
62 apdu.append(data);
63 }
64
65 if (ne) {
66 if (ne < 256) {
67 apdu.append(static_cast<char>(ne));
68 } else if (ne == 256) {
69 apdu.append(static_cast<char>('\0'));
70 } else {
71 if (!extendedLc)
72 apdu.append('\0');
73 apdu.append(static_cast<char>(ne >> 8));
74 apdu.append(static_cast<char>(ne & 0xFF));
75 }
76 }
77
78 return apdu;
79}
80
\inmodule QtCore
Definition qbytearray.h:57
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:494
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
QByteArray left(qsizetype n) const &
Definition qbytearray.h:169
QByteArray & append(char c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QResponseApdu(const QByteArray &response={})
static constexpr uint16_t Empty
QPixmap p2
QPixmap p1
[0]
QByteArray build(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, QByteArrayView data, uint16_t ne=0)
Combined button and popup list for selecting options.
constexpr T qFromBigEndian(T source)
Definition qendian.h:174
GLenum GLsizei dataSize
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
#define Q_ASSERT(cond)
Definition qrandom.cpp:47