3#include <QtCore/QCryptographicHash>
4#include <QtCore/QDateTime>
17 return status ? status->signing :
nullptr;
22 const mavlink_signing_t*
const signing = _channelSigningPtr(channel);
23 return signing ? signing->last_status : MAVLINK_SIGNING_STATUS_NONE;
43 return (message_id == MAVLINK_MSG_ID_RADIO_STATUS);
55bool pendingAcceptUnsignedCallback(
const mavlink_status_t* status, uint32_t message_id)
60 case MAVLINK_MSG_ID_RADIO_STATUS:
61 case MAVLINK_MSG_ID_HEARTBEAT:
62 case MAVLINK_MSG_ID_STATUSTEXT:
77 return pendingAcceptUnsignedCallback;
86 setup_signing.target_system = target_system.sysid > UINT8_MAX ? UINT8_MAX : target_system.sysid;
87 setup_signing.target_component = target_system.compid;
89 if (!keyBytes.isEmpty() && keyBytes.size() >=
static_cast<qsizetype
>(
sizeof(setup_signing.secret_key))) {
91 const mavlink_signing_t*
const signing = _channelSigningPtr(channel);
92 const uint64_t channelTs = signing ? signing->timestamp : 0;
94 memcpy(setup_signing.secret_key, keyBytes.constData(),
sizeof(setup_signing.secret_key));
106 (void) mavlink_msg_setup_signing_pack_chan(srcSysId, srcCompId, channel, &message, target_system.sysid,
107 payload.target_component, payload.secret_key, payload.initial_timestamp);
113 return (message.incompat_flags & MAVLINK_IFLAG_SIGNED) != 0;
119 message.incompat_flags |= MAVLINK_IFLAG_SIGNED;
121 message.incompat_flags &=
static_cast<uint8_t
>(~MAVLINK_IFLAG_SIGNED);
129 if (copy.magic == MAVLINK_STX) {
130 copy.incompat_flags &=
static_cast<uint8_t
>(~MAVLINK_IFLAG_SIGNED);
132 uint8_t header[MAVLINK_MAX_HEADER_LEN];
133 const uint8_t headerLength = mavlink_header_to_send_buffer(header, ©);
134 uint16_t checksum = crc_calculate(header + 1, headerLength - 1);
135 crc_accumulate_buffer(&checksum, _MAV_PAYLOAD(©), copy.len);
136 crc_accumulate(mavlink_get_crc_extra(©), &checksum);
138 copy.checksum = checksum;
139 mavlink_ck_a(©) =
static_cast<uint8_t
>(checksum & 0xFF);
140 mavlink_ck_b(©) =
static_cast<uint8_t
>(checksum >> 8);
143 QByteArray buf(MAVLINK_MAX_PACKET_LEN, Qt::Uninitialized);
144 const uint16_t len = mavlink_msg_to_send_buffer(
reinterpret_cast<uint8_t*
>(buf.data()), ©);
156 uint8_t header[MAVLINK_MAX_HEADER_LEN];
157 const uint8_t headerLength = mavlink_header_to_send_buffer(header, &message);
158 const char* payload = _MAV_PAYLOAD(&message);
159 const uint8_t* sig = message.signature;
160 const uint8_t crc[2] = {
static_cast<uint8_t
>(message.checksum & 0xFF),
static_cast<uint8_t
>(message.checksum >> 8)};
162 const QByteArrayView parts[] = {
164 QByteArrayView(
reinterpret_cast<const char*
>(header), headerLength),
165 QByteArrayView(payload, message.len),
166 QByteArrayView(
reinterpret_cast<const char*
>(crc),
sizeof(crc)),
169 (void) QCryptographicHash::hashInto(QSpan<uchar>(hashBuf), QSpan<const QByteArrayView>(parts),
170 QCryptographicHash::Sha256);
182 _computeSignatureHash(key, message, hashBuf);
195 message.signature[0] = linkId;
196 for (
int i = 0; i < kTimestampBytes; ++i) {
197 message.signature[1 + i] =
static_cast<uint8_t
>((timestamp >> (8 * i)) & 0xFF);
201 _computeSignatureHash(key, message, hashBuf);
209 return verifySignature(QByteArrayView(
reinterpret_cast<const char*
>(key.data()), key.size()), message);
214 const mavlink_signing_t*
const signing = _channelSigningPtr(channel);
216 qCWarning(MAVLinkSigningLog) <<
"checkSigningLinkId: no signing struct on channel" << channel;
219 return (signing->link_id ==
static_cast<mavlink_channel_t>(message.signature[0]));
224 switch (_lastSigningStatus(channel)) {
225 case MAVLINK_SIGNING_STATUS_OK:
226 return QStringLiteral(
"OK");
227 case MAVLINK_SIGNING_STATUS_BAD_SIGNATURE:
228 return QStringLiteral(
"Bad Signature");
229 case MAVLINK_SIGNING_STATUS_NO_STREAMS:
230 return QStringLiteral(
"No Streams");
231 case MAVLINK_SIGNING_STATUS_TOO_MANY_STREAMS:
232 return QStringLiteral(
"Too Many Streams");
233 case MAVLINK_SIGNING_STATUS_OLD_TIMESTAMP:
234 return QStringLiteral(
"Stale Timestamp");
235 case MAVLINK_SIGNING_STATUS_REPLAY:
236 return QStringLiteral(
"Replay Detected");
237 case MAVLINK_SIGNING_STATUS_NONE:
246 if (!status || !status->signing_streams) {
249 return status->signing_streams->num_signing_streams;
254 switch (_lastSigningStatus(channel)) {
255 case MAVLINK_SIGNING_STATUS_BAD_SIGNATURE:
256 qCWarning(MAVLinkSigningLog) <<
"Channel" << channel <<
"signing failure: bad signature (key mismatch)";
258 case MAVLINK_SIGNING_STATUS_NO_STREAMS:
259 qCWarning(MAVLinkSigningLog) <<
"Channel" << channel <<
"signing failure: no signing streams table";
261 case MAVLINK_SIGNING_STATUS_TOO_MANY_STREAMS:
262 qCWarning(MAVLinkSigningLog) <<
"Channel" << channel <<
"signing failure: stream table full (>"
265 case MAVLINK_SIGNING_STATUS_OLD_TIMESTAMP:
266 qCWarning(MAVLinkSigningLog) <<
"Channel" << channel
267 <<
"signing failure: new stream with stale timestamp (>"
268 << MAVLINK_SIGNING_TIMESTAMP_LIMIT <<
"s old)";
270 case MAVLINK_SIGNING_STATUS_REPLAY:
271 qCWarning(MAVLinkSigningLog) <<
"Channel" << channel
272 <<
"signing failure: replay detected (repeated/old timestamp)";
274 case MAVLINK_SIGNING_STATUS_OK:
275 case MAVLINK_SIGNING_STATUS_NONE:
#define MAVLINK_MAX_SIGNING_STREAMS
mavlink_status_t * mavlink_get_channel_status(uint8_t chan)
struct __mavlink_setup_signing_t mavlink_setup_signing_t
struct __mavlink_message mavlink_message_t
#define QGC_LOGGING_CATEGORY(name, categoryStr)
QByteArray serializeUnsignedCopy(const mavlink_message_t &message)
static constexpr int kSignatureHashBytes
bool checkSigningLinkId(mavlink_channel_t channel, const mavlink_message_t &message)
QString signingStatusString(mavlink_channel_t channel)
void signMessage(QByteArrayView key, uint8_t linkId, uint64_t timestamp, mavlink_message_t &message)
bool verifySignature(QByteArrayView key, const mavlink_message_t &message)
Verify a key against a signed message's signature.
uint64_t currentSigningTimestampTicks()
Current signing timestamp in 10µs ticks since 2015-01-01.
void createSetupSigning(mavlink_channel_t channel, mavlink_system_t target_system, QByteArrayView keyBytes, mavlink_setup_signing_t &setup_signing)
Build a SETUP_SIGNING payload. Empty keyBytes produces a disable payload (zero key,...
bool secureConnectionAcceptUnsignedCallback(const mavlink_status_t *status, uint32_t message_id)
std::optional< SigningKey > makeSigningKey(QByteArrayView bytes)
Build a SigningKey from arbitrary bytes. Returns nullopt if input is the wrong size.
bool isMessageSigned(const mavlink_message_t &message)
Returns true if the message has a MAVLink2 signature.
mavlink_accept_unsigned_t callbackForPolicy(UnsignedAcceptancePolicy policy)
Maps a high-level policy to the underlying libmavlink callback.
void logSigningFailure(mavlink_channel_t channel)
static constexpr int kSigningKeySize
void setMessageSigned(mavlink_message_t &message, bool isSigned)
Set or clear the MAVLink2 signature incompatibility flag on a message.
static constexpr int kSignaturePrefixBytes
bool insecureConnectionAcceptUnsignedCallback(const mavlink_status_t *status, uint32_t message_id)
int signingStreamCount(mavlink_channel_t channel)
std::array< uint8_t, kSigningKeySize > SigningKey
std::array avoids QByteArray COW detach so secureZero() actually wipes the bytes.
bool encodeSetupSigning(mavlink_channel_t channel, quint32 srcSysId, uint8_t srcCompId, mavlink_system_t target_system, QByteArrayView keyBytes, mavlink_message_t &message)