From 8ad37ecc297f208d0a63783c1ffae33ad4c3c81a Mon Sep 17 00:00:00 2001 From: Andre Heinecke Date: Fri, 24 Mar 2017 16:51:26 +0100 Subject: [PATCH] cpp: Use gpgme_op_decrypt_ex and add new flags. * lang/cpp/src/context.cpp: New decrypt and decryptVerify functions that take flags as arguments. Use new variants in old functions. (Context::setDecryptionFlags): New helper. (Context::Private::Private): Initialize new member. * lang/cpp/src/context_p.h (Context::Private::decryptFlags): New. * lang/cpp/src/context.h (Context::DecryptFlags): New enum. (Context::EncryptionFlags): Extend for EncryptWrap. -- The setDecryptionFlags provides a generic way to set decryption flags for the whole context. This allows existing code to just keep using the old functions and modify the decryption behavior in a central place. --- lang/cpp/src/context.cpp | 50 +++++++++++++++++++++++++++++++--------- lang/cpp/src/context.h | 22 ++++++++++++++++-- lang/cpp/src/context_p.h | 1 + 3 files changed, 60 insertions(+), 13 deletions(-) diff --git a/lang/cpp/src/context.cpp b/lang/cpp/src/context.cpp index 040e8f32..c20e5a9d 100644 --- a/lang/cpp/src/context.cpp +++ b/lang/cpp/src/context.cpp @@ -280,6 +280,11 @@ std::unique_ptr Context::createForEngine(Engine eng, Error *error) return std::unique_ptr(new Context(ctx)); } +void Context::setDecryptionFlags(DecryptionFlags flags) +{ + d->decryptFlags = flags; +} + // // // Context::Private @@ -294,7 +299,8 @@ Context::Private::Private(gpgme_ctx_t c) lastAssuanInquireData(Data::null), lastAssuanTransaction(), lastEditInteractor(), - lastCardEditInteractor() + lastCardEditInteractor(), + decryptFlags(DecryptNone) { } @@ -904,21 +910,32 @@ std::unique_ptr Context::takeLastAssuanTransaction() return std::move(d->lastAssuanTransaction); } -DecryptionResult Context::decrypt(const Data &cipherText, Data &plainText) +DecryptionResult Context::decrypt(const Data &cipherText, Data &plainText, const DecryptionFlags flags) { d->lastop = Private::Decrypt; const Data::Private *const cdp = cipherText.impl(); Data::Private *const pdp = plainText.impl(); - d->lasterr = gpgme_op_decrypt(d->ctx, cdp ? cdp->data : 0, pdp ? pdp->data : 0); + d->lasterr = gpgme_op_decrypt_ext(d->ctx, static_cast (d->decryptFlags), cdp ? cdp->data : 0, pdp ? pdp->data : 0); return DecryptionResult(d->ctx, Error(d->lasterr)); } +DecryptionResult Context::decrypt(const Data &cipherText, Data &plainText) +{ + return decrypt(cipherText, plainText, DecryptNone); +} + +Error Context::startDecryption(const Data &cipherText, Data &plainText, const DecryptionFlags flags) +{ + d->lastop = Private::Decrypt; + const Data::Private *const cdp = cipherText.impl(); + Data::Private *const pdp = plainText.impl(); + return Error(d->lasterr = gpgme_op_decrypt_ext_start(d->ctx, static_cast (d->decryptFlags), + cdp ? cdp->data : 0, pdp ? pdp->data : 0)); +} + Error Context::startDecryption(const Data &cipherText, Data &plainText) { - d->lastop = Private::Decrypt; - const Data::Private *const cdp = cipherText.impl(); - Data::Private *const pdp = plainText.impl(); - return Error(d->lasterr = gpgme_op_decrypt_start(d->ctx, cdp ? cdp->data : 0, pdp ? pdp->data : 0)); + return startDecryption(cipherText, plainText, DecryptNone); } DecryptionResult Context::decryptionResult() const @@ -973,22 +990,33 @@ VerificationResult Context::verificationResult() const } } -std::pair Context::decryptAndVerify(const Data &cipherText, Data &plainText) +std::pair Context::decryptAndVerify(const Data &cipherText, Data &plainText, DecryptionFlags flags) { d->lastop = Private::DecryptAndVerify; const Data::Private *const cdp = cipherText.impl(); Data::Private *const pdp = plainText.impl(); - d->lasterr = gpgme_op_decrypt_verify(d->ctx, cdp ? cdp->data : 0, pdp ? pdp->data : 0); + d->lasterr = gpgme_op_decrypt_ext(d->ctx, static_cast (d->decryptFlags | DecryptVerify), + cdp ? cdp->data : 0, pdp ? pdp->data : 0); return std::make_pair(DecryptionResult(d->ctx, Error(d->lasterr)), VerificationResult(d->ctx, Error(d->lasterr))); } -Error Context::startCombinedDecryptionAndVerification(const Data &cipherText, Data &plainText) +std::pair Context::decryptAndVerify(const Data &cipherText, Data &plainText) +{ + return decryptAndVerify(cipherText, plainText, DecryptNone); +} + +Error Context::startCombinedDecryptionAndVerification(const Data &cipherText, Data &plainText, DecryptionFlags flags) { d->lastop = Private::DecryptAndVerify; const Data::Private *const cdp = cipherText.impl(); Data::Private *const pdp = plainText.impl(); - return Error(d->lasterr = gpgme_op_decrypt_verify_start(d->ctx, cdp ? cdp->data : 0, pdp ? pdp->data : 0)); + return Error(d->lasterr = gpgme_op_decrypt_ext_start(d->ctx, static_cast (d->decryptFlags | DecryptVerify), cdp ? cdp->data : 0, pdp ? pdp->data : 0)); +} + +Error Context::startCombinedDecryptionAndVerification(const Data &cipherText, Data &plainText) +{ + return startCombinedDecryptionAndVerification(cipherText, plainText, DecryptNone); } unsigned int to_auditlog_flags(unsigned int flags) diff --git a/lang/cpp/src/context.h b/lang/cpp/src/context.h index b075bf1b..bec4e39a 100644 --- a/lang/cpp/src/context.h +++ b/lang/cpp/src/context.h @@ -261,14 +261,28 @@ public: // // Crypto Operations // - // + + enum DecryptionFlags { + // Keep in line with core's flags + DecryptNone = 0, + DecryptVerify = 1, + DecryptUnwrap = 128, + DecryptMaxValue = 0x80000000 + }; // // Decryption // + // Alternative way to set decryption flags as they were added only in + // 1.9.0 and so other API can still be used but with 1.9.0 additionally + // flags can be set. + void setDecryptionFlags (const DecryptionFlags flags); + DecryptionResult decrypt(const Data &cipherText, Data &plainText); GpgME::Error startDecryption(const Data &cipherText, Data &plainText); + DecryptionResult decrypt(const Data &cipherText, Data &plainText, const DecryptionFlags flags); + GpgME::Error startDecryption(const Data &cipherText, Data &plainText, const DecryptionFlags flags); DecryptionResult decryptionResult() const; // @@ -286,7 +300,9 @@ public: // std::pair decryptAndVerify(const Data &cipherText, Data &plainText); + std::pair decryptAndVerify(const Data &cipherText, Data &plainText, const DecryptionFlags flags); GpgME::Error startCombinedDecryptionAndVerification(const Data &cipherText, Data &plainText); + GpgME::Error startCombinedDecryptionAndVerification(const Data &cipherText, Data &plainText, const DecryptionFlags flags); // use verificationResult() and decryptionResult() to retrieve the result objects... // @@ -325,7 +341,9 @@ public: Prepare = 4, ExpectSign = 8, NoCompress = 16, - Symmetric = 32 + Symmetric = 32, + ThrowKeyIds = 64, + EncryptWrap = 128 }; EncryptionResult encrypt(const std::vector &recipients, const Data &plainText, Data &cipherText, EncryptionFlags flags); GpgME::Error encryptSymmetrically(const Data &plainText, Data &cipherText); diff --git a/lang/cpp/src/context_p.h b/lang/cpp/src/context_p.h index be347831..d53da0ac 100644 --- a/lang/cpp/src/context_p.h +++ b/lang/cpp/src/context_p.h @@ -77,6 +77,7 @@ public: Data lastAssuanInquireData; std::unique_ptr lastAssuanTransaction; std::unique_ptr lastEditInteractor, lastCardEditInteractor; + DecryptionFlags decryptFlags; }; } // namespace GpgME