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.
This commit is contained in:
Andre Heinecke 2017-03-24 16:51:26 +01:00
parent 6ac1f2cded
commit 8ad37ecc29
No known key found for this signature in database
GPG Key ID: 2978E9D40CBABA5C
3 changed files with 60 additions and 13 deletions

View File

@ -280,6 +280,11 @@ std::unique_ptr<Context> Context::createForEngine(Engine eng, Error *error)
return std::unique_ptr<Context>(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<AssuanTransaction> 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<gpgme_decrypt_flags_t> (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<gpgme_decrypt_flags_t> (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<DecryptionResult, VerificationResult> Context::decryptAndVerify(const Data &cipherText, Data &plainText)
std::pair<DecryptionResult, VerificationResult> 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<gpgme_decrypt_flags_t> (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<DecryptionResult, VerificationResult> 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<gpgme_decrypt_flags_t> (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)

View File

@ -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<DecryptionResult, VerificationResult> decryptAndVerify(const Data &cipherText, Data &plainText);
std::pair<DecryptionResult, VerificationResult> 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<Key> &recipients, const Data &plainText, Data &cipherText, EncryptionFlags flags);
GpgME::Error encryptSymmetrically(const Data &plainText, Data &cipherText);

View File

@ -77,6 +77,7 @@ public:
Data lastAssuanInquireData;
std::unique_ptr<AssuanTransaction> lastAssuanTransaction;
std::unique_ptr<EditInteractor> lastEditInteractor, lastCardEditInteractor;
DecryptionFlags decryptFlags;
};
} // namespace GpgME