KItinerary

iso9796_2decoder.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "iso9796_2decoder_p.h"
8
9#include "openssl/bignum_p.h"
10
11#include <QDebug>
12
13#include <openssl/bn.h>
14#include <openssl/err.h>
15
16using namespace KItinerary;
17
18Iso9796_2Decoder::Iso9796_2Decoder()
19 : m_rsa(RSA_new())
20{
21}
22
23Iso9796_2Decoder::~Iso9796_2Decoder() = default;
24
25void Iso9796_2Decoder::setRsaParameters(const uint8_t *modulus, uint16_t modulusSize, const uint8_t *exponent, uint16_t exponentSize)
26{
27 auto n = Bignum::fromByteArray(modulus, modulusSize);
28 auto e = Bignum::fromByteArray(exponent, exponentSize);
29 RSA_set0_key(m_rsa.get(), n.release(), e.release(), nullptr);
30}
31
32void Iso9796_2Decoder::addWithRecoveredMessage(const uint8_t *data, int size)
33{
34 QByteArray out;
35 out.resize(RSA_size(m_rsa.get()));
36 const auto outSize = RSA_public_decrypt(size, data, (uint8_t*)out.data(), m_rsa.get(), RSA_NO_PADDING);
37 if (outSize < 0) {
38 qWarning() << "RSA error:" << ERR_error_string(ERR_get_error(), nullptr);
39 return;
40 }
41
42 out.resize(outSize);
43 if ((uint8_t)out[0] != 0x6a || (uint8_t)out[out.size() - 1] != 0xbc || out.size() < 22) { // 20 byte SHA-1 + padding/trailer
44 qWarning() << "RSA message recovery failed:" << out.toHex() << outSize;
45 return;
46 }
47
48 m_recoveredMsg.append(out.constData() + 1, out.size() - 22);
49}
50
51void Iso9796_2Decoder::add(const uint8_t *data, int size)
52{
53 if (m_recoveredMsg.isEmpty()) { // previous failure
54 return;
55 }
56 m_recoveredMsg.append((const char*)data, size);
57}
58
59QByteArray Iso9796_2Decoder::recoveredMessage() const
60{
61 return m_recoveredMsg;
62}
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
const char * constData() const const
char * data()
void resize(qsizetype newSize, char c)
qsizetype size() const const
QByteArray toHex(char separator) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 4 2024 12:00:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.