2 context.h - wraps a gpgme key context
3 Copyright (C) 2003, 2007 Klarälvdalens Datakonsult AB
5 This file is part of GPGME++.
7 GPGME++ is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 GPGME++ is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with GPGME++; see the file COPYING.LIB. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
24 #ifndef __GPGMEPP_CONTEXT_H__
25 #define __GPGMEPP_CONTEXT_H__
30 #include "verificationresult.h" // for Signature::Notation
43 class ProgressProvider;
44 class PassphraseProvider;
45 class EventLoopInteractor;
47 class AssuanTransaction;
50 class KeyGenerationResult;
52 class DecryptionResult;
53 class VerificationResult;
55 class EncryptionResult;
60 class GPGMEPP_EXPORT Context
62 explicit Context(gpgme_ctx_t);
64 //using GpgME::Protocol;
67 // Creation and destruction:
70 static Context *createForProtocol(Protocol proto);
71 static std::unique_ptr<Context> createForEngine(Engine engine, Error *err = 0);
78 Protocol protocol() const;
80 void setArmor(bool useArmor);
83 void setTextMode(bool useTextMode);
84 bool textMode() const;
86 void setOffline(bool useOfflineMode);
89 enum CertificateInclusion {
90 DefaultCertificates = -256,
91 AllCertificatesExceptRoot = -2,
94 OnlySenderCertificate = 1
96 void setIncludeCertificates(int which);
97 int includeCertificates() const;
99 //using GpgME::KeyListMode;
100 void setKeyListMode(unsigned int keyListMode);
101 void addKeyListMode(unsigned int keyListMode);
102 unsigned int keyListMode() const;
104 /** Set the passphrase provider
106 * To avoid problems where a class using a context registers
107 * itself as the provider the Context does not take ownership
108 * of the provider and the caller must ensure that the provider
109 * is deleted if it is no longer needed.
111 void setPassphraseProvider(PassphraseProvider *provider);
112 PassphraseProvider *passphraseProvider() const;
114 /** Set the progress provider
116 * To avoid problems where a class using a context registers
117 * itself as the provider the Context does not take ownership
118 * of the provider and the caller must ensure that the provider
119 * is deleted if it is no longer needed.
121 void setProgressProvider(ProgressProvider *provider);
122 ProgressProvider *progressProvider() const;
124 void setManagedByEventLoopInteractor(bool managed);
125 bool managedByEventLoopInteractor() const;
127 GpgME::Error setLocale(int category, const char *value);
129 EngineInfo engineInfo() const;
130 GpgME::Error setEngineFileName(const char *filename);
131 GpgME::Error setEngineHomeDirectory(const char *filename);
140 GpgME::Error setPinentryMode(PinentryMode which);
141 PinentryMode pinentryMode() const;
144 friend class ::GpgME::EventLoopInteractor;
145 void installIOCallbacks(gpgme_io_cbs *iocbs);
146 void uninstallIOCallbacks();
159 GpgME::Error startKeyListing(const char *pattern = 0, bool secretOnly = false);
160 GpgME::Error startKeyListing(const char *patterns[], bool secretOnly = false);
162 Key nextKey(GpgME::Error &e);
164 KeyListResult endKeyListing();
165 KeyListResult keyListResult() const;
167 Key key(const char *fingerprint, GpgME::Error &e, bool secret = false);
173 KeyGenerationResult generateKey(const char *parameters, Data &pubKey);
174 GpgME::Error startKeyGeneration(const char *parameters, Data &pubkey);
175 KeyGenerationResult keyGenerationResult() const;
181 GpgME::Error exportPublicKeys(const char *pattern, Data &keyData);
182 GpgME::Error exportPublicKeys(const char *pattern[], Data &keyData);
183 GpgME::Error startPublicKeyExport(const char *pattern, Data &keyData);
184 GpgME::Error startPublicKeyExport(const char *pattern[], Data &keyData);
190 ImportResult importKeys(const Data &data);
191 ImportResult importKeys(const std::vector<Key> &keys);
192 GpgME::Error startKeyImport(const Data &data);
193 GpgME::Error startKeyImport(const std::vector<Key> &keys);
194 ImportResult importResult() const;
200 GpgME::Error deleteKey(const Key &key, bool allowSecretKeyDeletion = false);
201 GpgME::Error startKeyDeletion(const Key &key, bool allowSecretKeyDeletion = false);
204 // Passphrase changing
207 GpgME::Error passwd(const Key &key);
208 GpgME::Error startPasswd(const Key &key);
214 GpgME::Error edit(const Key &key, std::unique_ptr<EditInteractor> function, Data &out);
215 GpgME::Error startEditing(const Key &key, std::unique_ptr<EditInteractor> function, Data &out);
217 // using TofuInfo::Policy
218 Error setTofuPolicy(const Key &k, unsigned int policy);
219 Error setTofuPolicyStart(const Key &k, unsigned int policy);
221 EditInteractor *lastEditInteractor() const;
222 std::unique_ptr<EditInteractor> takeLastEditInteractor();
228 GpgME::Error cardEdit(const Key &key, std::unique_ptr<EditInteractor> function, Data &out);
229 GpgME::Error startCardEditing(const Key &key, std::unique_ptr<EditInteractor> function, Data &out);
231 EditInteractor *lastCardEditInteractor() const;
232 std::unique_ptr<EditInteractor> takeLastCardEditInteractor();
235 // Trust Item Management
238 GpgME::Error startTrustItemListing(const char *pattern, int maxLevel);
239 TrustItem nextTrustItem(GpgME::Error &e);
240 GpgME::Error endTrustItemListing();
243 // Assuan Transactions
246 GpgME::Error assuanTransact(const char *command, std::unique_ptr<AssuanTransaction> transaction);
247 GpgME::Error assuanTransact(const char *command);
248 GpgME::Error startAssuanTransaction(const char *command, std::unique_ptr<AssuanTransaction> transaction);
249 GpgME::Error startAssuanTransaction(const char *command);
251 AssuanTransaction *lastAssuanTransaction() const;
252 std::unique_ptr<AssuanTransaction> takeLastAssuanTransaction();
264 DecryptionResult decrypt(const Data &cipherText, Data &plainText);
265 GpgME::Error startDecryption(const Data &cipherText, Data &plainText);
266 DecryptionResult decryptionResult() const;
269 // Signature Verification
272 VerificationResult verifyDetachedSignature(const Data &signature, const Data &signedText);
273 VerificationResult verifyOpaqueSignature(const Data &signedData, Data &plainText);
274 GpgME::Error startDetachedSignatureVerification(const Data &signature, const Data &signedText);
275 GpgME::Error startOpaqueSignatureVerification(const Data &signedData, Data &plainText);
276 VerificationResult verificationResult() const;
279 // Combined Decryption and Signature Verification
282 std::pair<DecryptionResult, VerificationResult> decryptAndVerify(const Data &cipherText, Data &plainText);
283 GpgME::Error startCombinedDecryptionAndVerification(const Data &cipherText, Data &plainText);
284 // use verificationResult() and decryptionResult() to retrieve the result objects...
290 void clearSigningKeys();
291 GpgME::Error addSigningKey(const Key &signer);
292 Key signingKey(unsigned int index) const;
293 std::vector<Key> signingKeys() const;
295 void clearSignatureNotations();
296 GpgME::Error addSignatureNotation(const char *name, const char *value, unsigned int flags = 0);
297 GpgME::Error addSignaturePolicyURL(const char *url, bool critical = false);
298 const char *signaturePolicyURL() const;
299 Notation signatureNotation(unsigned int index) const;
300 std::vector<Notation> signatureNotations() const;
302 //using GpgME::SignatureMode;
303 SigningResult sign(const Data &plainText, Data &signature, SignatureMode mode);
304 GpgME::Error startSigning(const Data &plainText, Data &signature, SignatureMode mode);
305 SigningResult signingResult() const;
307 // wrapper for gpgme_set_sender
308 const char *getSender();
309 GpgME::Error setSender(const char *sender);
315 enum EncryptionFlags {
324 EncryptionResult encrypt(const std::vector<Key> &recipients, const Data &plainText, Data &cipherText, EncryptionFlags flags);
325 GpgME::Error encryptSymmetrically(const Data &plainText, Data &cipherText);
326 GpgME::Error startEncryption(const std::vector<Key> &recipients, const Data &plainText, Data &cipherText, EncryptionFlags flags);
327 EncryptionResult encryptionResult() const;
330 // Combined Signing and Encryption
333 std::pair<SigningResult, EncryptionResult> signAndEncrypt(const std::vector<Key> &recipients, const Data &plainText, Data &cipherText, EncryptionFlags flags);
334 GpgME::Error startCombinedSigningAndEncryption(const std::vector<Key> &recipients, const Data &plainText, Data &cipherText, EncryptionFlags flags);
335 // use encryptionResult() and signingResult() to retrieve the result objects...
344 AuditLogWithHelp = 128
346 GpgME::Error startGetAuditLog(Data &output, unsigned int flags = 0);
347 GpgME::Error getAuditLog(Data &output, unsigned int flags = 0);
351 // G13 crypto container operations
354 GpgME::Error createVFS(const char *containerFile, const std::vector<Key> &recipients);
355 VfsMountResult mountVFS(const char *containerFile, const char *mountDir);
363 /** Spwan the process \a file with arguments \a argv.
365 * If a data parameter is null the /dev/null will be
366 * used. (Or other platform stuff).
368 * @param file The executable to start.
369 * @param argv list of arguments file should be argv[0].
370 * @param input The data to be sent through stdin.
371 * @param output The data to be receive the stdout.
372 * @param err The data to receive stderr.
373 * @param flags Additional flags.
375 * @returns An error or empty error.
377 GpgME::Error spawn(const char *file, const char *argv[],
378 Data &input, Data &output, Data &err,
380 /** Async variant of spawn. Immediately returns after starting the
382 GpgME::Error spawnAsync(const char *file, const char *argv[],
383 Data &input, Data &output,
384 Data &err, SpawnFlags flags);
393 GpgME::Error lastError() const;
394 GpgME::Error cancelPendingOperation();
397 const Private *impl() const
406 // Helper functions that need to be context because they rely
407 // on the "Friendlyness" of context to access the gpgme types.
408 gpgme_key_t *getKeysFromRecipients(const std::vector<Key> &recipients);
413 private: // disable...
414 Context(const Context &);
415 const Context &operator=(const Context &);
418 GPGMEPP_EXPORT std::ostream &operator<<(std::ostream &os, Context::CertificateInclusion incl);
419 GPGMEPP_EXPORT std::ostream &operator<<(std::ostream &os, Context::EncryptionFlags flags);
420 GPGMEPP_EXPORT std::ostream &operator<<(std::ostream &os, Context::AuditLogFlags flags);
424 #endif // __GPGMEPP_CONTEXT_H__