Commit 75cd0e80 authored by Volker Krause's avatar Volker Krause
Browse files

Implement RSA decryption in the ISO 9796-2 decoder

Unfortunately this seems to yield nonsense still, so need to figure out
which of the parameters is wrong here.
parent 56cdf1a1
......@@ -49,6 +49,8 @@ find_package(LibXml2 MODULE)
set_package_properties("LibXml2" PROPERTIES PURPOSE "Support for extraction from HTML booking confirmations." URL "http://libxml.org")
find_package(PhoneNumber OPTIONAL_COMPONENTS PhoneNumber QUIET)
set_package_properties("PhoneNumber" PROPERTIES PURPOSE "Parsing and geo-coding of phone numbers.")
find_package(OpenSSL 1.1)
set_package_properties("OpenSSL" PROPERTIES TYPE OPTIONAL PURPOSE "VDV ticket decoding." URL "https://openssl.org")
if (NOT ANDROID)
set_package_properties(KF5CalendarCore PROPERTIES TYPE REQUIRED)
......@@ -85,6 +87,9 @@ endif()
if (TARGET PhoneNumber::PhoneNumber)
set(HAVE_PHONENUMBER ON)
endif()
if (TARGET OpenSSL::Crypto)
set(HAVE_OPENSSL_RSA ON)
endif()
add_definitions(-DTRANSLATION_DOMAIN=\"kitinerary\")
......
......@@ -128,6 +128,9 @@ endif()
if (HAVE_PHONENUMBER)
target_link_libraries(KPimItinerary PRIVATE PhoneNumber::PhoneNumber)
endif()
if (HAVE_OPENSSL_RSA)
target_link_libraries(KPimItinerary PRIVATE OpenSSL::Crypto)
endif()
ecm_generate_headers(KItinerary_FORWARDING_HEADERS
HEADER_NAMES
......
......@@ -33,6 +33,7 @@
#cmakedefine HAVE_KCAL
#cmakedefine HAVE_LIBXML2
#cmakedefine HAVE_PHONENUMBER
#cmakedefine HAVE_OPENSSL_RSA
#define CMAKE_INSTALL_FULL_LIBEXECDIR_KF5 "${CMAKE_INSTALL_FULL_LIBEXECDIR_KF5}"
......
......@@ -15,18 +15,31 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config-kitinerary.h"
#include "iso9796_2decoder_p.h"
#include <QDebug>
#ifdef HAVE_OPENSSL_RSA
#include <ssl/bn.h>
#endif
using namespace KItinerary;
Iso9796_2Decoder::Iso9796_2Decoder() = default;
Iso9796_2Decoder::Iso9796_2Decoder()
#ifdef HAVE_OPENSSL_RSA
: m_rsa(RSA_new(), RSA_free)
#endif
{
}
Iso9796_2Decoder::~Iso9796_2Decoder() = default;
void Iso9796_2Decoder::setRsaParameters(const uint8_t *modulus, uint16_t modulusSize, const uint8_t *exponent, uint16_t exponentSize)
{
#ifdef HAVE_OPENSSL_RSA
// TODO
const auto n = BN_bin2bn(modulus, modulusSize, nullptr);
const auto e = BN_bin2bn(exponent, exponentSize, nullptr);
RSA_set0_key(m_rsa.get(), n, e, nullptr); // takes ownership of n and e
#else
Q_UNUSED(modulus);
Q_UNUSED(modulusSize);
......@@ -38,7 +51,11 @@ void Iso9796_2Decoder::setRsaParameters(const uint8_t *modulus, uint16_t modulus
void Iso9796_2Decoder::addWithRecoveredMessage(const uint8_t *data, int size)
{
#ifdef HAVE_OPENSSL_RSA
// TODO
QByteArray out;
out.resize(RSA_size(m_rsa.get()));
const auto outSize = RSA_public_decrypt(size, data, (uint8_t*)out.data(), m_rsa.get(), RSA_NO_PADDING);
out.resize(outSize);
qDebug() << outSize << out.toHex();
#else
Q_UNUSED(data);
Q_UNUSED(size);
......
......@@ -18,9 +18,16 @@
#ifndef KITINERARY_ISO9796_2DECODER_P_H
#define KITINERARY_ISO9796_2DECODER_P_H
#include "config-kitinerary.h"
#include <QByteArray>
#ifdef HAVE_OPENSSL_RSA
#include <ssl/rsa.h>
#endif
#include <cstdint>
#include <memory>
namespace KItinerary {
......@@ -43,6 +50,9 @@ public:
QByteArray recoveredMessage() const;
private:
#ifdef HAVE_OPENSSL_RSA
std::unique_ptr<RSA, void(*)(RSA*)> m_rsa;
#endif
};
}
......
......@@ -85,6 +85,11 @@ void VdvTicketParser::parse(const QByteArray &data)
cvDecoder.setRsaParameters(caCert.modulus(), caCert.modulusSize(), caCert.exponent(), caCert.exponentSize());
cvDecoder.addWithRecoveredMessage(cvSig->contentData(), cvSig->contentSize());
cvDecoder.add(cvRem->contentData(), cvRem->contentSize());
const auto cvDecoded = cvDecoder.recoveredMessage();
if (cvDecoded.isEmpty()) {
qDebug() << "Failed to decode CV certificate.";
return;
}
// (3) decode the ticket data using the decoded CV certificate
// TODO
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment