| 1 | // Copyright (C) 2016 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 | // Qt-Security score:critical reason:uses-kernel-cryptography |
| 4 | #include "lecmaccalculator_p.h" |
| 5 | |
| 6 | #include "bluez/bluez_data_p.h" |
| 7 | |
| 8 | #include <QtCore/qbytearray.h> |
| 9 | #include <QtCore/qloggingcategory.h> |
| 10 | #include <QtCore/private/qcore_unix_p.h> |
| 11 | |
| 12 | #include <cstring> |
| 13 | #include <sys/socket.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <unistd.h> |
| 16 | |
| 17 | #ifdef CONFIG_LINUX_CRYPTO_API |
| 18 | #include <linux/if_alg.h> |
| 19 | #endif |
| 20 | |
| 21 | QT_BEGIN_NAMESPACE |
| 22 | |
| 23 | Q_DECLARE_LOGGING_CATEGORY(QT_BT_BLUEZ) |
| 24 | |
| 25 | LeCmacCalculator::LeCmacCalculator() |
| 26 | { |
| 27 | #ifdef CONFIG_LINUX_CRYPTO_API |
| 28 | m_baseSocket = socket(AF_ALG, SOCK_SEQPACKET, protocol: 0); |
| 29 | if (m_baseSocket == -1) { |
| 30 | qCWarning(QT_BT_BLUEZ) << "failed to create first level crypto socket:" |
| 31 | << strerror(errno); |
| 32 | return; |
| 33 | } |
| 34 | sockaddr_alg sa; |
| 35 | using namespace std; |
| 36 | memset(s: &sa, c: 0, n: sizeof sa); |
| 37 | sa.salg_family = AF_ALG; |
| 38 | strcpy(dest: reinterpret_cast<char *>(sa.salg_type), src: "hash" ); |
| 39 | strcpy(dest: reinterpret_cast<char *>(sa.salg_name), src: "cmac(aes)" ); |
| 40 | if (::bind(fd: m_baseSocket, addr: reinterpret_cast<sockaddr *>(&sa), len: sizeof sa) == -1) { |
| 41 | qCWarning(QT_BT_BLUEZ) << "bind() failed for crypto socket:" << strerror(errno); |
| 42 | return; |
| 43 | } |
| 44 | #else // CONFIG_LINUX_CRYPTO_API |
| 45 | qCWarning(QT_BT_BLUEZ) << "Linux crypto API not present, CMAC verification will fail." ; |
| 46 | #endif |
| 47 | } |
| 48 | |
| 49 | LeCmacCalculator::~LeCmacCalculator() |
| 50 | { |
| 51 | if (m_baseSocket != -1) |
| 52 | close(fd: m_baseSocket); |
| 53 | } |
| 54 | |
| 55 | QByteArray LeCmacCalculator::createFullMessage(const QByteArray &message, quint32 signCounter) |
| 56 | { |
| 57 | // Spec v4.2, Vol 3, Part H, 2.4.5 |
| 58 | QByteArray fullMessage = message; |
| 59 | fullMessage.resize(size: fullMessage.size() + sizeof signCounter); |
| 60 | putBtData(src: signCounter, dst: fullMessage.data() + message.size()); |
| 61 | return fullMessage; |
| 62 | } |
| 63 | |
| 64 | quint64 LeCmacCalculator::calculateMac(const QByteArray &message, QUuid::Id128Bytes csrk) const |
| 65 | { |
| 66 | #ifdef CONFIG_LINUX_CRYPTO_API |
| 67 | if (m_baseSocket == -1) |
| 68 | return false; |
| 69 | QUuid::Id128Bytes csrkMsb; |
| 70 | std::reverse_copy(first: std::begin(arr&: csrk.data), last: std::end(arr&: csrk.data), result: std::begin(arr&: csrkMsb.data)); |
| 71 | qCDebug(QT_BT_BLUEZ) << "CSRK (MSB):" << QByteArray(reinterpret_cast<char *>(csrkMsb.data), |
| 72 | sizeof csrkMsb).toHex(); |
| 73 | if (setsockopt(fd: m_baseSocket, level: 279 /* SOL_ALG */, ALG_SET_KEY, optval: csrkMsb.data, optlen: sizeof csrkMsb) == -1) { |
| 74 | qCWarning(QT_BT_BLUEZ) << "setsockopt() failed for crypto socket:" << strerror(errno); |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | class SocketWrapper |
| 79 | { |
| 80 | public: |
| 81 | SocketWrapper(int socket) : m_socket(socket) {} |
| 82 | ~SocketWrapper() { |
| 83 | if (m_socket != -1) |
| 84 | close(fd: m_socket); |
| 85 | } |
| 86 | |
| 87 | int value() const { return m_socket; } |
| 88 | private: |
| 89 | int m_socket; |
| 90 | }; |
| 91 | SocketWrapper cryptoSocket(accept(fd: m_baseSocket, addr: nullptr, addr_len: nullptr)); |
| 92 | if (cryptoSocket.value() == -1) { |
| 93 | qCWarning(QT_BT_BLUEZ) << "accept() failed for crypto socket:" << strerror(errno); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | QByteArray messageSwapped(message.size(), Qt::Uninitialized); |
| 98 | std::reverse_copy(first: message.begin(), last: message.end(), result: messageSwapped.begin()); |
| 99 | qint64 totalBytesWritten = 0; |
| 100 | do { |
| 101 | const qint64 bytesWritten = qt_safe_write(fd: cryptoSocket.value(), |
| 102 | data: messageSwapped.constData() + totalBytesWritten, |
| 103 | len: messageSwapped.size() - totalBytesWritten); |
| 104 | if (bytesWritten == -1) { |
| 105 | qCWarning(QT_BT_BLUEZ) << "writing to crypto socket failed:" << strerror(errno); |
| 106 | return 0; |
| 107 | } |
| 108 | totalBytesWritten += bytesWritten; |
| 109 | } while (totalBytesWritten < messageSwapped.size()); |
| 110 | quint64 mac; |
| 111 | quint8 * const macPtr = reinterpret_cast<quint8 *>(&mac); |
| 112 | qint64 totalBytesRead = 0; |
| 113 | do { |
| 114 | const qint64 bytesRead = qt_safe_read(fd: cryptoSocket.value(), data: macPtr + totalBytesRead, |
| 115 | maxlen: sizeof mac - totalBytesRead); |
| 116 | if (bytesRead == -1) { |
| 117 | qCWarning(QT_BT_BLUEZ) << "reading from crypto socket failed:" << strerror(errno); |
| 118 | return 0; |
| 119 | } |
| 120 | totalBytesRead += bytesRead; |
| 121 | } while (totalBytesRead < qint64(sizeof mac)); |
| 122 | return qFromBigEndian(source: mac); |
| 123 | #else // CONFIG_LINUX_CRYPTO_API |
| 124 | Q_UNUSED(message); |
| 125 | Q_UNUSED(csrk); |
| 126 | qCWarning(QT_BT_BLUEZ) << "CMAC calculation failed due to missing Linux crypto API." ; |
| 127 | return 0; |
| 128 | #endif |
| 129 | } |
| 130 | |
| 131 | bool LeCmacCalculator::verify(const QByteArray &message, QUuid::Id128Bytes csrk, |
| 132 | quint64 expectedMac) const |
| 133 | { |
| 134 | #ifdef CONFIG_LINUX_CRYPTO_API |
| 135 | const quint64 actualMac = calculateMac(message, csrk); |
| 136 | if (actualMac != expectedMac) { |
| 137 | qCWarning(QT_BT_BLUEZ) << Qt::hex << "signature verification failed: calculated mac:" |
| 138 | << actualMac << "expected mac:" << expectedMac; |
| 139 | return false; |
| 140 | } |
| 141 | return true; |
| 142 | #else // CONFIG_LINUX_CRYPTO_API |
| 143 | Q_UNUSED(message); |
| 144 | Q_UNUSED(csrk); |
| 145 | Q_UNUSED(expectedMac); |
| 146 | qCWarning(QT_BT_BLUEZ) << "CMAC verification failed due to missing Linux crypto API." ; |
| 147 | return false; |
| 148 | #endif |
| 149 | } |
| 150 | |
| 151 | QT_END_NAMESPACE |
| 152 | |