Changeset View
Changeset View
Standalone View
Standalone View
src/utils/classify.cpp
| Show All 40 Lines | |||||
| 41 | #include <QFileInfo> | 41 | #include <QFileInfo> | ||
| 42 | #include <QtAlgorithms> | 42 | #include <QtAlgorithms> | ||
| 43 | #include <QByteArrayMatcher> | 43 | #include <QByteArrayMatcher> | ||
| 44 | #include <QMap> | 44 | #include <QMap> | ||
| 45 | #include <QRegularExpression> | 45 | #include <QRegularExpression> | ||
| 46 | 46 | | |||
| 47 | #include <gpgme++/data.h> | 47 | #include <gpgme++/data.h> | ||
| 48 | #include <qgpgme/dataprovider.h> | 48 | #include <qgpgme/dataprovider.h> | ||
| 49 | #include <ksba.h> | ||||
| 49 | 50 | | |||
| 50 | #include <iterator> | 51 | #include <iterator> | ||
| 51 | #include <functional> | 52 | #include <functional> | ||
| 52 | 53 | | |||
| 53 | using namespace Kleo::Class; | 54 | using namespace Kleo::Class; | ||
| 54 | 55 | | |||
| 55 | namespace | 56 | namespace | ||
| 56 | { | 57 | { | ||
| ▲ Show 20 Lines • Show All 223 Lines • ▼ Show 20 Line(s) | 279 | if (!GpgME::hasFeature(0, GpgME::BinaryAndFineGrainedIdentify)) { | |||
| 280 | if (ourClassification != defaultClassification) { | 281 | if (ourClassification != defaultClassification) { | ||
| 281 | return ourClassification; | 282 | return ourClassification; | ||
| 282 | } | 283 | } | ||
| 283 | } | 284 | } | ||
| 284 | QGpgME::QByteArrayDataProvider dp(data); | 285 | QGpgME::QByteArrayDataProvider dp(data); | ||
| 285 | GpgME::Data gpgmeData(&dp); | 286 | GpgME::Data gpgmeData(&dp); | ||
| 286 | GpgME::Data::Type type = gpgmeData.type(); | 287 | GpgME::Data::Type type = gpgmeData.type(); | ||
| 287 | 288 | | |||
| 288 | return gpgmeTypeMap.value(type, defaultClassification); | 289 | auto classification = gpgmeTypeMap.value(type, defaultClassification); | ||
| 290 | if (isCMS(classification) && mayBeDetachedSignature(classification)) { | ||||
| 291 | if (auto sigClassification = classifyCMSSignature(data)) { | ||||
| 292 | classification &= ~AnySignature; | ||||
| 293 | classification |= sigClassification; | ||||
| 294 | } | ||||
| 295 | } | ||||
| 296 | | ||||
| 297 | return classification; | ||||
| 298 | } | ||||
| 299 | | ||||
| 300 | namespace { | ||||
| 301 | | ||||
| 302 | struct KSBAReaderDeleter | ||||
| 303 | { | ||||
| 304 | static inline void cleanup(ksba_reader_t reader) | ||||
| 305 | { | ||||
| 306 | ksba_reader_release(reader); | ||||
| 307 | } | ||||
| 308 | }; | ||||
| 309 | | ||||
| 310 | struct KSBACMSDeleter | ||||
| 311 | { | ||||
| 312 | static inline void cleanup(ksba_cms_t cms) | ||||
| 313 | { | ||||
| 314 | ksba_cms_release(cms); | ||||
| 315 | } | ||||
| 316 | }; | ||||
| 317 | | ||||
| 318 | } | ||||
| 319 | | ||||
| 320 | unsigned int Kleo::classifyCMSSignature(const QByteArray &data) | ||||
| 321 | { | ||||
| 322 | ksba_reader_t reader_ptr; | ||||
| 323 | if (ksba_reader_new(&reader_ptr)) { | ||||
| 324 | return 0; | ||||
| 325 | } | ||||
| 326 | const QScopedPointer<ksba_reader_s, KSBAReaderDeleter> reader(reader_ptr); | ||||
| 327 | if (ksba_reader_set_mem(reader.data(), data.constData(), data.size())) { | ||||
| 328 | qCWarning(LIBKLEO_LOG) << "KSBA: Failed to set reader memory"; | ||||
| 329 | return 0; | ||||
| 330 | } | ||||
| 331 | | ||||
| 332 | ksba_cms_t cms_ptr; | ||||
| 333 | if (ksba_cms_new(&cms_ptr)) { | ||||
| 334 | return 0; | ||||
| 335 | } | ||||
| 336 | const QScopedPointer<ksba_cms_s, KSBACMSDeleter> cms(cms_ptr); | ||||
| 337 | | ||||
| 338 | ksba_writer_t writer = 0; | ||||
| 339 | if (ksba_cms_set_reader_writer(cms.data(), reader.data(), writer)) { | ||||
| 340 | qCWarning(LIBKLEO_LOG) << "KSBA: Failed to setup reader and writer"; | ||||
| 341 | return 0; | ||||
| 342 | } | ||||
| 343 | | ||||
| 344 | ksba_stop_reason_t stopReason; | ||||
| 345 | do { | ||||
| 346 | if (auto rc = ksba_cms_parse(cms.data(), &stopReason)) { | ||||
| 347 | qCWarning(LIBKLEO_LOG) << "KSBA: Failed to parse CMS signature:" << gpg_strerror(rc); | ||||
| 348 | return 0; | ||||
| 349 | } | ||||
| 350 | if (stopReason == KSBA_SR_NEED_HASH) { | ||||
| 351 | return DetachedSignature; | ||||
| 352 | } | ||||
| 353 | } while (stopReason != KSBA_SR_READY); | ||||
| 354 | | ||||
| 355 | | ||||
| 356 | return OpaqueSignature; // there's no clearsign in CMS | ||||
| 289 | } | 357 | } | ||
| 290 | 358 | | |||
| 291 | QString Kleo::printableClassification(unsigned int classification) | 359 | QString Kleo::printableClassification(unsigned int classification) | ||
| 292 | { | 360 | { | ||
| 293 | QStringList parts; | 361 | QStringList parts; | ||
| 294 | if (classification & CMS) { | 362 | if (classification & CMS) { | ||
| 295 | parts.push_back(QStringLiteral("CMS")); | 363 | parts.push_back(QStringLiteral("CMS")); | ||
| 296 | } | 364 | } | ||
| ▲ Show 20 Lines • Show All 143 Lines • Show Last 20 Lines | |||||