Exemple #1
0
int main( int argc, char **argv )
{
  setenv("GNUPGHOME", KDESRCDIR "/gnupg_home", 1 );
  setenv("LC_ALL", "C", 1);
  setenv("KDEHOME", QFile::encodeName( QDir::homeDirPath() + "/.kde-unit-test" ), 1);

  KAboutData aboutData( "test_verify", "verify job test", "0.1" );
  KCmdLineArgs::init( argc, argv, &aboutData );
  KApplication app( false, false );

  const QString sigFileName = KDESRCDIR "/test.data.sig";
  const QString dataFileName = KDESRCDIR "/test.data";

  QFile sigFile( sigFileName );
  assert( sigFile.open( IO_ReadOnly ) );
  QFile dataFile( dataFileName );
  assert( dataFile.open( IO_ReadOnly ) );

  const Kleo::CryptoBackend::Protocol * const backend = Kleo::CryptoBackendFactory::instance()->protocol( "openpgp" );

  Kleo::VerifyDetachedJob *job = backend->verifyDetachedJob();
  GpgME::VerificationResult result = job->exec( sigFile.readAll(), dataFile.readAll() );
  assert( !result.error() );
  assert( result.signatures().size() == 1 );

  GpgME::Signature sig = result.signature( 0 );
  assert( (sig.summary() & GpgME::Signature::KeyMissing) == 0 );
  assert( sig.creationTime() == 1189650248L );
  assert( sig.validity() == GpgME::Signature::Full );

  const QString opaqueFileName = KDESRCDIR "/test.data.gpg";
  QFile opaqueFile( opaqueFileName );
  assert( opaqueFile.open( IO_ReadOnly ) );
  QByteArray clearText;

  Kleo::VerifyOpaqueJob *job2 = backend->verifyOpaqueJob();
  result = job2->exec( opaqueFile.readAll(), clearText );
  assert( !result.error() );
  assert( result.signatures().size() == 1 );

  sig = result.signature( 0 );
  assert( (sig.summary() & GpgME::Signature::KeyMissing) == 0 );
  assert( (sig.summary() & GpgME::Signature::Green ) );
  assert( sig.creationTime() > 0 );
  assert( sig.validity() == GpgME::Signature::Full );

  dataFile.reset();
  assert( clearText == dataFile.readAll() );

  return 0;
}
Exemple #2
0
void Signature::slotVerified(const GpgME::VerificationResult &result)
{
    d->verificationResult = result;
    d->status = Signature::NotWorked;

    if (!d->verificationResult.numSignatures()) {
        kDebug(5001) << "No signatures\n";
        emit verified(d->status);
        return;
    }

    GpgME::Signature signature = d->verificationResult.signature(0);
    d->sigSummary = signature.summary();
    d->error = signature.status().code();
    d->fingerprint = signature.fingerprint();

    kDebug(5001) << "Fingerprint:" << d->fingerprint;
    kDebug(5001) << "Signature summary:" << d->sigSummary;
    kDebug(5001) << "Error code:" << d->error;

    if (d->sigSummary & GpgME::Signature::KeyMissing) {
        kDebug(5001) << "Public key missing.";
        if (Settings::signatureAutomaticDownloading() ||
            (KMessageBox::warningYesNoCancel(0,
             i18n("The key to verify the signature is missing, do you want to download it?")) == KMessageBox::Yes)) {
            d->verifyTried = true;
            downloadKey(d->fingerprint);
            emit verified(d->status);
            return;
        }
    }

    if (!signature.status()) {
        if (d->sigSummary & GpgME::Signature::Valid) {
            d->status = Signature::Verified;
        } else if ((d->sigSummary & GpgME::Signature::Green) || (d->sigSummary == 0)) {
            d->status = Signature::VerifiedInformation;
        }
    } else if (signature.status()) {
        if ((d->sigSummary & GpgME::Signature::KeyExpired) || (d->sigSummary & GpgME::Signature::KeyRevoked)) {
            d->status = Signature::VerifiedWarning;
        }
        if (d->sigSummary & GpgME::Signature::Red) {//TODO handle more cases!
            d->status = Signature::NotVerified;
            //TODO handle that dialog better in 4.5
            KMessageBox::error(0,
                            i18n("The signature could not be verified for %1. See transfer settings for more information.", d->dest.fileName()),
                            i18n("Signature not verified"));
        }
    }

    emit verified(d->status);
}