Cpp: Handle empty recipients consistently
* lang/cpp/src/context.cpp (Context::getKeysFromRecipients):
New helper.
(Context::encrypt, Context::startEncryption, Context::signAndEncrypt)
(Context::startCombinedSigningAndEncryption): Use new helper.
* lang/cpp/src/context.h (Context::getKeysFromRecipients): Add
as private helper.
--
bf776ce
was incomplete as the code to handle recpients
was duplicated four times. This is now unified and constently
uses a nullptr instead of an empty array.
This commit is contained in:
parent
b602d8bc7b
commit
09667a6006
@ -1109,6 +1109,22 @@ static gpgme_encrypt_flags_t encryptflags2encryptflags(Context::EncryptionFlags
|
||||
return static_cast<gpgme_encrypt_flags_t>(result);
|
||||
}
|
||||
|
||||
gpgme_key_t *const Context::getKeysFromRecipients(const std::vector<Key> &recipients)
|
||||
{
|
||||
if (recipients.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
gpgme_key_t *ret = new gpgme_key_t[ recipients.size() + 1 ];
|
||||
gpgme_key_t *keys_it = ret;
|
||||
for (std::vector<Key>::const_iterator it = recipients.begin() ; it != recipients.end() ; ++it) {
|
||||
if (it->impl()) {
|
||||
*keys_it++ = it->impl();
|
||||
}
|
||||
}
|
||||
*keys_it++ = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
EncryptionResult Context::encrypt(const std::vector<Key> &recipients, const Data &plainText, Data &cipherText, EncryptionFlags flags)
|
||||
{
|
||||
d->lastop = Private::Encrypt;
|
||||
@ -1117,18 +1133,12 @@ EncryptionResult Context::encrypt(const std::vector<Key> &recipients, const Data
|
||||
}
|
||||
const Data::Private *const pdp = plainText.impl();
|
||||
Data::Private *const cdp = cipherText.impl();
|
||||
gpgme_key_t *const keys = new gpgme_key_t[ recipients.size() + 1 ];
|
||||
gpgme_key_t *keys_it = keys;
|
||||
for (std::vector<Key>::const_iterator it = recipients.begin() ; it != recipients.end() ; ++it) {
|
||||
if (it->impl()) {
|
||||
*keys_it++ = it->impl();
|
||||
}
|
||||
}
|
||||
*keys_it++ = 0;
|
||||
d->lasterr = gpgme_op_encrypt(d->ctx, recipients.empty() ? nullptr : keys,
|
||||
encryptflags2encryptflags(flags),
|
||||
gpgme_key_t *const keys = getKeysFromRecipients(recipients);
|
||||
d->lasterr = gpgme_op_encrypt(d->ctx, keys, encryptflags2encryptflags(flags),
|
||||
pdp ? pdp->data : 0, cdp ? cdp->data : 0);
|
||||
delete[] keys;
|
||||
if (keys) {
|
||||
delete[] keys;
|
||||
}
|
||||
return EncryptionResult(d->ctx, Error(d->lasterr));
|
||||
}
|
||||
|
||||
@ -1149,17 +1159,12 @@ Error Context::startEncryption(const std::vector<Key> &recipients, const Data &p
|
||||
}
|
||||
const Data::Private *const pdp = plainText.impl();
|
||||
Data::Private *const cdp = cipherText.impl();
|
||||
gpgme_key_t *const keys = new gpgme_key_t[ recipients.size() + 1 ];
|
||||
gpgme_key_t *keys_it = keys;
|
||||
for (std::vector<Key>::const_iterator it = recipients.begin() ; it != recipients.end() ; ++it) {
|
||||
if (it->impl()) {
|
||||
*keys_it++ = it->impl();
|
||||
}
|
||||
}
|
||||
*keys_it++ = 0;
|
||||
gpgme_key_t *const keys = getKeysFromRecipients(recipients);
|
||||
d->lasterr = gpgme_op_encrypt_start(d->ctx, keys, encryptflags2encryptflags(flags),
|
||||
pdp ? pdp->data : 0, cdp ? cdp->data : 0);
|
||||
delete[] keys;
|
||||
if (keys) {
|
||||
delete[] keys;
|
||||
}
|
||||
return Error(d->lasterr);
|
||||
}
|
||||
|
||||
@ -1177,17 +1182,12 @@ std::pair<SigningResult, EncryptionResult> Context::signAndEncrypt(const std::ve
|
||||
d->lastop = Private::SignAndEncrypt;
|
||||
const Data::Private *const pdp = plainText.impl();
|
||||
Data::Private *const cdp = cipherText.impl();
|
||||
gpgme_key_t *const keys = new gpgme_key_t[ recipients.size() + 1 ];
|
||||
gpgme_key_t *keys_it = keys;
|
||||
for (std::vector<Key>::const_iterator it = recipients.begin() ; it != recipients.end() ; ++it) {
|
||||
if (it->impl()) {
|
||||
*keys_it++ = it->impl();
|
||||
}
|
||||
}
|
||||
*keys_it++ = 0;
|
||||
gpgme_key_t *const keys = getKeysFromRecipients(recipients);
|
||||
d->lasterr = gpgme_op_encrypt_sign(d->ctx, keys, encryptflags2encryptflags(flags),
|
||||
pdp ? pdp->data : 0, cdp ? cdp->data : 0);
|
||||
delete[] keys;
|
||||
if (keys) {
|
||||
delete[] keys;
|
||||
}
|
||||
return std::make_pair(SigningResult(d->ctx, Error(d->lasterr)),
|
||||
EncryptionResult(d->ctx, Error(d->lasterr)));
|
||||
}
|
||||
@ -1197,17 +1197,12 @@ Error Context::startCombinedSigningAndEncryption(const std::vector<Key> &recipie
|
||||
d->lastop = Private::SignAndEncrypt;
|
||||
const Data::Private *const pdp = plainText.impl();
|
||||
Data::Private *const cdp = cipherText.impl();
|
||||
gpgme_key_t *const keys = new gpgme_key_t[ recipients.size() + 1 ];
|
||||
gpgme_key_t *keys_it = keys;
|
||||
for (std::vector<Key>::const_iterator it = recipients.begin() ; it != recipients.end() ; ++it) {
|
||||
if (it->impl()) {
|
||||
*keys_it++ = it->impl();
|
||||
}
|
||||
}
|
||||
*keys_it++ = 0;
|
||||
gpgme_key_t *const keys = getKeysFromRecipients(recipients);
|
||||
d->lasterr = gpgme_op_encrypt_sign_start(d->ctx, keys, encryptflags2encryptflags(flags),
|
||||
pdp ? pdp->data : 0, cdp ? cdp->data : 0);
|
||||
delete[] keys;
|
||||
if (keys) {
|
||||
delete[] keys;
|
||||
}
|
||||
return Error(d->lasterr);
|
||||
}
|
||||
|
||||
|
@ -368,6 +368,11 @@ public:
|
||||
{
|
||||
return d;
|
||||
}
|
||||
private:
|
||||
// Helper functions that need to be context because they rely
|
||||
// on the "Friendlyness" of context to access the gpgme types.
|
||||
gpgme_key_t *const getKeysFromRecipients(const std::vector<Key> &recipients);
|
||||
|
||||
private:
|
||||
Private *const d;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user