diff --git a/NEWS b/NEWS index a8d73a50..51d79239 100644 --- a/NEWS +++ b/NEWS @@ -1,1893 +1,1898 @@ Noteworthy changes in version 1.10.1 (unreleased) ------------------------------------------------- * New context flag "no-symkey-cache". * Interface changes relative to the 1.10.0 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - gpgme_import_result_t EXTENDED: New field 'skipped_v3_keys' - cpp: Key::locate NEW. - cpp: Data::toString NEW. + gpgme_op_encrypt_ext NEW. + gpgme_op_encrypt_ext_start NEW. + gpgme_op_encrypt_sign_ext NEW. + gpgme_op_encrypt_sign_ext_start NEW. + gpgme_import_result_t EXTENDED: New field 'skipped_v3_keys'. + cpp: Key::locate NEW. + cpp: Data::toString NEW. cpp: ImportResult::numV3KeysSkipped NEW. + Noteworthy changes in version 1.10.0 (2017-12-12) ------------------------------------------------- * Now returns more specific error codes for decryption to distinguish between bad passphrase, user canceled, and no secret key. * Now returns key origin information if available. * Added context flag "auto-key-retrieve" to selectively enable the corresponding gpg option. * Added flag is_de_vs to decryption and verify results. * py: Use SEEK_SET as default for data.seek. * cpp: Various new APIs. * Reduced spawn overhead on Linux again. Added new configure option --disable-linux-getdents to disable this feature for very old Linux versions. * Improved the Python bindings build system. * Made the test suite less fragile. * Interface changes relative to the 1.9.0 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_decrypt_result_t EXTENDED: New field 'is_de_vs'. gpgme_signature_t EXTENDED: New field 'is_de_vs'. gpgme_keyorg_t NEW. gpgme_op_delete_ext NEW. gpgme_op_delete_ext_start NEW. GPGME_DELETE_ALLOW_SECRET NEW. GPGME_DELETE_FORCE NEW. gpgme_op_conf_dir NEW. gpgme_set_ctx_flag EXTENDED: New flag 'auto-key-retrieve'. cpp: DecryptionResult::isDeVs NEW. cpp: Signature::isDeVs NEW. cpp: EngineInfo::Version::operator> NEW. cpp: Context::createKey NEW. cpp: Context::startCreateKey NEW. cpp: Context::createSubkey NEW. cpp: Context::startCreateSubkey NEW. qt: QuickJob NEW. py: DecryptResult EXTENDED: New boolean field 'is_de_vs'. py: Signature EXTENDED: New boolean field 'is_de_vs'. py: GpgError EXTENDED: Partial results in 'results'. [c=C30/A19/R0 cpp=C11/A5/R0 qt=C10/A3/R0] Noteworthy changes in version 1.9.0 (2017-03-28) ------------------------------------------------ * Clarified meaning of the 'expire' parameter of gpgme_op_createkey and gpgme_op_createsubkey. New flag to force a key without an expiration date. * New function gpgme_op_keylist_from_data_start to list keys from data objects without importing them. * New function gpgme_op_set_uid_flag to flag a key as primary. * New function gpgme_op_decrypt_ext to run decryption with special flags. This can for example be used to unwrap keys (remove only the encryption layer). * New encryption flags to wrap a key (adding an encryption layer to an OpenPGP message) or to create anonymously encrypted messages. * Support for adduid and revuid operations in the C++ bindings. * Support for smartcard key generation in the C++ bindings. * Several new functions for the Python binding. * Many smaller bug fixes. * Interface changes relative to the 1.8.0 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_op_createkey CHANGED: Meaning of 'expire' parameter. gpgme_op_createsubkey CHANGED: Meaning of 'expire' parameter. GPGME_CREATE_NOEXPIRE NEW. gpgme_key_t EXTENDED: New field 'origin'. gpgme_key_t EXTENDED: New field 'last_update'. gpgme_subkey_t EXTENDED: New field 'is_de_vs'. gpgme_user_id_t EXTENDED: New field 'origin'. gpgme_user_id_t EXTENDED: New field 'last_update'. gpgme_op_keylist_from_data_start NEW. gpgme_op_set_uid_flag_start NEW. gpgme_op_set_uid_flag NEW. gpgme_op_decrypt_ext_start NEW. gpgme_op_decrypt_ext NEW. GPGME_ENCRYPT_THROW_KEYIDS NEW. GPGME_ENCRYPT_WRAP NEW. GPGME_DECRYPT_VERIFY NEW. GPGME_DECRYPT_UNWRAP NEW. gpgme_data_rewind UN-DEPRECATE. cpp: Context::revUid(const Key&, const char*) NEW. cpp: Context::startRevUid(const Key&, const char*) NEW. cpp: Context::addUid(const Key&, const char*) NEW. cpp: Context::startAddUid(const Key&, const char*) NEW. cpp: Key::UserID::revoke() NEW. cpp: Key::addUid() NEW. cpp: Key::isDeVs NEW. cpp: GpgGenCardKeyInteractor NEW. cpp: Subkey::keyGrip NEW. cpp: Subkey::isDeVs NEW. cpp: Data::toKeys NEW. cpp: Context::setDecryptFlags NEW. cpp: Context::decrypt EXTENDED: Flags added. cpp: Context::startDecrypt EXTENDED: Flags added. cpp: Context::decryptAndVerify EXTENDED: Flags added. cpp: Context::startCombinedDecryptionAndVerification EXTENDED: Flags. cpp: Context::encryptFlags EXTENDED: New flags. qt: CryptoConfig::stringValueList() NEW. py: Context.__init__ EXTENDED: New keyword arg home_dir. py: Context.home_dir NEW. py: Context.keylist EXTENDED: New keyword arg mode. py: Context.keylist EXTENDED: New keyword arg source. py: Context.create_key NEW. py: Context.create_subkey NEW. py: Context.key_add_uid NEW. py: Context.key_revoke_uid NEW. py: Context.key_sign NEW. py: Context.key_tofu_policy NEW. py: core.pubkey_algo_string NEW. py: core.addrspec_from_uid NEW. [c=C29/A18/R0 cpp=C10/A4/R0 qt=C9/A2/R0] Noteworthy changes in version 1.8.0 (2016-11-16) ------------------------------------------------ * The module of the Python bindings has been renamed to 'gpg'. * New interface to query current software versions. * New feature to use gpg's --{show,override}session-key options. * New interface to set the sender of a mail. * qt: Added Distinguished Name parser from libkleo * The --homedir option is now used with recent gpgconf versions. * On 64 bit Windows systems gpgconf is now properly located. * The internal locking functions have been replaced by libgpg-error locking functions. * Interface changes relative to the 1.7.1 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_set_sender NEW. gpgme_get_sender NEW. gpgme_op_query_swdb NEW. gpgme_op_query_swdb_result NEW. gpgme_query_swdb_result_t NEW. gpgme_get_ctx_flag NEW. gpgme_decrypt_result_t EXTENDED: New field session_key. qt: DN NEW. qt: DN::Attribute NEW. qt: Job::context(Job*) NEW. cpp: EngineInfo::Version::Version(const char*) NEW. cpp: EngineInfo::Version::Version() NEW. cpp: SwdbResult NEW. cpp: Context::setSender(const char*) NEW. cpp: Context::getSender() NEW. [c=C28/A17/R0 cpp=C9/A3/R0 qt=C8/A1/R0] Noteworthy changes in version 1.7.1 (2016-10-18) ------------------------------------------------ * Fixed problems with the new language bindings. * New helper function gpgme_addrspec_from_uid. * Use option --exit-on-status-write-error with newer gpg versions. * qt: Missed API from the Qt Binding inclusion has been added again. * qt: abstractimportjob.h is now installed to that ImportJobs can be used again. * qt: Fixed spelling error in API (startReceive). * Interface changes relative to the 1.7.0 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_addrspec_from_uid NEW. qt: WksPublishJob::startRecieve RENAMED to ::startReceive. qt: MultiDeleteJob NEW. qt: AbstractImportJob NEW. qt: SpecialJob NEW. cpp: Signature::key(bool, bool) NEW. cpp: UserID::addrSpecFromString(const char*) NEW. cpp: UserID::addrSpec() NEW. [c=C27/A16/R0 cpp=C8/A2/R0 qt=C7/A0/R0] Noteworthy changes in version 1.7.0 (2016-09-21) ------------------------------------------------ * New language bindings for Python 2 and 3. * New language Bindings for C++ and the Qt-Framework API. * New functions gpgme_op_createkey and gpgme_op_createsubkey to make key creation easier (requires GnuPG 2.1). * New functions gpgme_op_adduid and gpgme_op_revuid to make user id management easier (requires GnuPG 2.1). * New function gpgme_op_keysign to make key signing easier (requires GnuPG 2.1). * New function gpgme_op_interact to replace the now deprecated functions gpgme_op_edit and gpgme_op_card_edit. * New function gpgme_pubkey_algo_string to convert a public key algorithm into a GnuPG 2.1 style string. * Support for GnuPG 2.1's TOFU trust model. * Notation flags are now correctly set on verify. * New global flag "require-gnupg" to set a minimal gnupg version. * More supported items in gpgme_get_dirinfo. * New function gpgme_data_set_flag and flag "size-hint". * New function gpgme_set_ctx_flag and flags "full-status" and "raw-description". * Improved gpgme_data_identify to distinguish more file types. * New flag GPGME_ENCRYPT_SYMMETRIC for gpgme_op_encrypt to allow mixed public key and symmetric encryption. * New field KEYGRIP in gpgme_subkey_t. New fields FPR in gpgme_key_t. * New flag GPGME_DATA_ENCODING_MIME to declare that the encrypted or signed data is a valid MIME part. This is to support future GnuPG versions. * Interface changes relative to the 1.6.0 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_pubkey_algo_string NEW. GPGME_PK_EDDSA NEW. gpgme_set_ctx_flag NEW. gpgme_data_set_flag NEW. gpgme_op_createkey NEW. gpgme_op_createkey_start NEW. gpgme_op_createsubkey NEW. gpgme_op_createsubkey_start NEW. gpgme_op_adduid_start NEW. gpgme_op_adduid NEW. gpgme_op_revuid_start NEW. gpgme_op_revuid NEW. gpgme_op_keysign_start NEW. gpgme_op_keysign NEW. gpgme_op_tofu_policy_start NEW. gpgme_op_tofu_policy NEW. gpgme_op_interact_start NEW. gpgme_op_interact NEW. gpgme_interact_cb_t NEW. gpgme_op_edit_start DEPRECATED. gpgme_op_edit DEPRECATED. gpgme_op_card_edit_start DEPRECATED. gpgme_op_card_edit DEPRECATED. gpgme_edit_cb_t DEPRECATED. gpgme_status_code_t DEPRECATED. gpgme_genkey_result_t EXTENDED: New fields pubkey and seckey. gpgme_signature_t EXTENDED: New field key. gpgme_key_t EXTENDED: New field fpr. gpgme_subkey_t EXTENDED: New field keygrip. gpgme_user_id_t EXTENDED: New field tofu. gpgme_tofu_policy_t NEW. gpgme_tofu_info_t NEW. GPGME_STATUS_KEY_CONSIDERED NEW. GPGME_STATUS_TOFU_USER NEW. GPGME_STATUS_TOFU_STATS NEW. GPGME_STATUS_TOFU_STATS_LONG NEW. GPGME_STATUS_NOTATION_FLAGS NEW. GPGME_KEYLIST_MODE_WITH_TOFU NEW. GPGME_DATA_TYPE_PGP_ENCRYPTED NEW. GPGME_DATA_TYPE_PGP_SIGNATURE NEW. GPGME_DATA_ENCODING_MIME NEW. GPGME_ENCRYPT_SYMMETRIC NEW. GPGME_CREATE_SIGN NEW. GPGME_CREATE_ENCR NEW. GPGME_CREATE_CERT NEW. GPGME_CREATE_AUTH NEW. GPGME_CREATE_NOPASSWD NEW. GPGME_CREATE_SELFSIGNED NEW. GPGME_CREATE_NOSTORE NEW. GPGME_CREATE_WANTPUB NEW. GPGME_CREATE_WANTSEC NEW. GPGME_CREATE_FORCE NEW. GPGME_KEYSIGN_LOCAL NEW. GPGME_KEYSIGN_LFSEP NEW. GPGME_INTERACT_CARD NEW. [c=C26/A15/R0 cpp=C6/A0/R1 qt=C6/A0/R1] Noteworthy changes in version 1.6.0 (2015-08-26) [C25/A14/R0] ------------------------------------------------ * Added gpgme_set_offline to do a key listinging w/o requiring CRL. * Added gpgme_set_status_cb to allow a user to see some status messages. * Added an export mode for secret keys. * More precise error codes are returned if GnuPG >= 2.1.8 is used. * The passphrase handler for the loopback mode has been improved and may also be used with genkey. * [w32] The standard GnuPG 2.1 install directory is now seached for gpgconf.exe before a registry specified directory and the Gpg4win install directory. * [w32] gpgme-w32spawn.exe will now only be searched in the gpgme DLL directory. * Interface changes relative to the 1.5.1 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_set_offline NEW. gpgme_get_offline NEW. gpgme_set_status_cb NEW. gpgme_get_status_cb NEW. GPGME_EXPORT_MODE_SECRET NEW GPGME_EXPORT_MODE_RAW NEW. GPGME_EXPORT_MODE_PKCS12 NEW. Noteworthy changes in version 1.5.5 (2015-06-08) [C24/A13/R4] ------------------------------------------------ * Fixed crash in key listings for user ids with a backslash. * Fixed regression for GPGSM use with GnuPG < 2.1. * Properly set signature summary for revoked OpenPGP keys. Noteworthy changes in version 1.5.4 (2015-04-13) [C24/A13/R3] ------------------------------------------------ * Fixed a possible crash in the debug code. * Fixed building for Windows with newer versions of Mingw. Noteworthy changes in version 1.5.3 (2014-12-11) [C24/A13/R2] ------------------------------------------------------------- * The export key functions do now return an error if used with the latest GnuPG version. Noteworthy changes in version 1.5.2 (2014-11-21) [C24/A13/R1] ------------------------------------------------------------- * gpgme-tool is now installed. * Fix external listing for modern keyservers. * Minor other fixes. Noteworthy changes in version 1.5.1 (2014-07-30) [C24/A13/R0] ------------------------------------------------------------- * Fixed possible overflow in gpgsm and uiserver engines. [CVE-2014-3564] * Added support for GnuPG 2.1's --with-secret option. * Interface changes relative to the 1.5.0 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPGME_KEYLIST_MODE_WITH_SECRET NEW. Noteworthy changes in version 1.5.0 (2014-05-21) [C23/A12/R0] ------------------------------------------------------------- * On Unices the engine file names are not not anymore hardwired but located via the envvar PATH. All options to set the name of the engines for the configure run are removed. * If GPGME finds the gpgconf binary it defaults to using gpg2 or whatever gpgconf tells as name for the OpenPGP engine. If gpgconf is not found, GPGME looks for an engine named "gpg". * New feature to use the gpgme I/O subsystem to run arbitrary commands. * New flag to use encryption without the default compression step. * New function to access "gpg-conf --list-dirs" * New configure option --enable-fixed-path for use by Android. * Support ECC algorithms. * Interface changes relative to the 1.4.3 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_get_dirinfo NEW. gpgme_op_spawn_start NEW. gpgme_op_spawn NEW. GPGME_PROTOCOL_SPAWN NEW. GPGME_SPAWN_DETACHED NEW. GPGME_SPAWN_ALLOW_SET_FG NEW. GPGME_ENCRYPT_NO_COMPRESS NEW. GPGME_PK_ECC NEW. GPGME_MD_SHA224 NEW. gpgme_subkey_t EXTENDED: New field curve. GPGME_STATUS_PLAINTEXT_LENGTH NEW. GPGME_STATUS_MOUNTPOINT NEW. GPGME_STATUS_PINENTRY_LAUNCHED NEW. GPGME_STATUS_ATTRIBUTE NEW. GPGME_STATUS_BEGIN_SIGNING NEW. GPGME_STATUS_KEY_NOT_CREATED NEW. Noteworthy changes in version 1.4.3 (2013-08-12) [C22/A11/R0] ------------------------------------------------------------- * The default engine names are now taken from the output of gpgconf. If gpgconf is not found the use of gpg 1 is assumed. * Under Windows the default engines names are first searched in the installation directory of the gpgme DLL. * New function gpgme_data_identify to detect the type of a message. * Interface changes relative to the 1.4.2 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_signers_count NEW. gpgme_data_type_t NEW. gpgme_data_identify NEW. Noteworthy changes in version 1.4.2 (2013-05-28) [C21/A10/R0] ------------------------------------------------------------- * Allow symmetric encryption with gpgme_op_encrypt_sign. * Fixed mismatching off_t definitions on Windows. * Interface changes relative to the 1.4.1 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_off_t NEW. gpgme_size_t NEW. GPGME_PROTOCOL_OPENPGP NEW alias. Noteworthy changes in version 1.4.1 (2013-05-01) [C20/A9/R1] ------------------------------------------------------------ * Fixed reading of gpg.conf files with excessive use of the group option. * Fixed building with the i686-w64-mingw32 toolchain. * Disabled FD passing by default for Apple. Noteworthy changes in version 1.4.0 (2013-02-26) [C20/A9/R0] ------------------------------------------------------------ * New function gpgme_set_global_flag to help debugging on Android. * New function gpgme_io_writen as a convenience wrapper around gpgme_io_write. * New functions to support the pinentry mode feature of GnuPG 2.1. * New macro GPGME_VERSION_NUMBER to allow supporting different API versions without the need for a configure test. * Several improvements for gpgme-tool. * Better logging of the common "invalid engine" error code. * Support for FD passing is now enabled by default. The configure option --disable-fd-passing may be used to disable this. * Interface changes relative to the 1.3.1 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPGME_VERSION_NUMBER NEW. gpgme_io_writen NEW. gpgme_set_global_flag NEW. gpgme_set_pinentry_mode NEW. gpgme_get_pinentry_mode NEW. gpgme_pinentry_mode_t NEW. GPGME_PINENTRY_MODE_DEFAULT NEW. GPGME_PINENTRY_MODE_ASK NEW. GPGME_PINENTRY_MODE_CANCEL NEW. GPGME_PINENTRY_MODE_ERROR NEW. GPGME_PINENTRY_MODE_LOOPBACK NEW. Noteworthy changes in version 1.3.2 (2012-05-02) ------------------------------------------------ * Remove support for libgpgme-pth. As far as we know, this was never used, and GnuPG is going to use our own npth in the future. * Fix signature summary information for a missing X.509 key. * Fix parsing of dates >= year 2038. Noteworthy changes in version 1.3.1 (2011-06-16) ------------------------------------------------ * Ported to Windows CE. * Detect GPG versions not supporting ---passwd. * Interface changes relative to the 1.3.0 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPGME_EXPORT_MODE_MINIMAL NEW GPGME_STATUS_SUCCESS NEW gpgme_err_code_from_syserror NEW gpgme_err_set_errno NEW gpgme_error_from_errno CHANGED: Return gpgme_error_t (compatible type). gpgme_error_from_syserror NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 1.3.0 (2010-01-11) ------------------------------------------------ * GPGME does not come with an internal libassuan version anymore. The external libassuan 1.1.0 release or later is required. For application programmers on systems that can resolve inter-library dependencies at runtime, this is a transparent change. * New engine GPGME_PROTOCOL_G13 to support the new g13 tool. * New engine GPGME_PROTOCOL_UISERVER to support UI Servers. * New API to change the passphrase of a key. * Interface changes relative to the 1.2.0 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPGME_STATUS_INV_SGNR NEW. GPGME_STATUS_NO_SGNR NEW. GPGME_PROTOCOL_G13 NEW. gpgme_op_g13_mount NEW. gpgme_g13_result_t NEW. GPGME_PK_ECDSA NEW. GPGME_PK_ECDH NEW. gpgme_op_passwd_start NEW. gpgme_op_passwd NEW. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 1.2.0 (2009-06-18) ------------------------------------------------ * New encryption flag GPGME_ENCRYPT_NO_ENCRYPT_TO to disable default recipients. * gpgme_new will fail if gpgme_check_version was not called, or a selftest failed (for example, if -mms-bitfields was not used on MingW32 targets). * New functions gpgme_io_read and gpgme_io_write for use with gpgme_passphrase_cb_t and gpgme_edit_cb_t functions. * New functions gpgme_result_ref and gpgme_result_unref to detach result structures from a context. * New functions gpgme_op_export_keys_start and gpgme_op_export_keys that allow to specify exported keys through gpgme_key_t objects instead of patterns. * New mode of operation gpgme_export_mode_t that allows exporting external keys. * Interface changes relative to the 1.1.7 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPGME_KEYLIST_MODE_EPHEMERAL NEW. GPGME_PROTOCOL_ASSUAN NEW. gpgme_assuan_data_cb_t NEW. gpgme_assuan_inquire_cb_t NEW. gpgme_assuan_status_cb_t NEW. gpgme_op_assuan_transact_start NEW. gpgme_op_assuan_transact NEW. gpgme_op_assuan_result NEW. gpgme_op_import_keys NEW. gpgme_op_import_keys_start NEW. gpgme_subkey_t EXTENDED: New fields is_cardkey, card_number. GPGME_ENCRYPT_NO_ENCRYPT_TO NEW. gpgme_check_version CHANGED: Is now a macro. gpgme_new EXTENDED: More failure codes. gpgme_io_read NEW. gpgme_io_write NEW. gpgme_result_ref NEW. gpgme_result_unref NEW. gpgme_export_mode_t NEW. gpgme_export_ext_start EXTENDED: Arg RESERVED is now a MODE flag. gpgme_op_export EXTENDED: Arg RESERVED is now a MODE flag. gpgme_op_export_ext_start EXTENDED: Arg RESERVED is now a MODE flag. gpgme_op_export_ext EXTENDED: Arg RESERVED is now a MODE flag. gpgme_op_export_keys_start NEW. gpgme_op_export_keys NEW. GPGME_DATA_ENCODING_URL NEW. GPGME_DATA_ENCODING_URL0 NEW. GPGME_DATA_ENCODING_URLESC NEW. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 1.1.8 (2008-12-08) ------------------------------------------------ * SIGPIPE is now again ignored as described in the manual. Fixes regresion introduced with 1.1.6. Noteworthy changes in version 1.1.7 (2008-10-17) ------------------------------------------------ * Using GPGME_KEYLIST_MODE_LOCAL combined with GPGME_KEYLIST_MODE_EXTERN is now supported; it uses the --locate-keys feature of gpg (>= 2.0.10). * The encoding of gpgme_data_t objects can affect the output encoding of export, sign and encrypt operations now (the same operations that are also affected by the ASCII mode switch). We believe this change in the ABI is innocent enough not to break existing applications (it only affects the S/MIME backend on certain operations). * The reference manual now includes the specification of "The GnuPG UI Server protocol". * A new function gpgme_cancel_async can be used to asynchronously cancel any pending operation at any time, from any thread. * Interface changes relative to the 1.1.6 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_op_encrypt CHANGED: Output encoding can affect result. gpgme_op_encrypt_start CHANGED: Output encoding can affect result. gpgme_op_encrypt_sign CHANGED: Output encoding can affect result. gpgme_op_encrypt_sign_start CHANGED: Output encoding can affect result. gpgme_op_sign CHANGED: Output encoding can affect result. gpgme_op_sign_start CHANGED: Output encoding can affect result. gpgme_op_export CHANGED: Output encoding can affect result. gpgme_op_export_start CHANGED: Output encoding can affect result. gpgme_op_export_ext CHANGED: Output encoding can affect result. gpgme_op_export_ext_start CHANGED: Output encoding can affect result. gpgme_cancel_async NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 1.1.6 (2008-01-04) ------------------------------------------------ * Bug fixes for for W32. * A new, experimental (and thus undocumented and potentially unstable) interface for accessing gpg-conf through GPGME has been added. * Interface changes relative to the 1.1.1 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_signature_t EXTENDED: New field chain_model. gpgme_op_getauditlog_start NEW. gpgme_op_getauditlog NEW. GPGME_AUDITLOG_HTML NEW. GPGME_AUDITLOG_WITH_HELP NEW. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 1.1.5 (2007-07-09) ------------------------------------------------ * Bug and portability fixes (mainly for W32). Noteworthy changes in version 1.1.4 (2007-03-05) ------------------------------------------------ * Detect and bail out on double plaintext messages. This is required so that applications can properly detect the signed parts of a message. Actual there is now a double protection as GnuPG 1.4.7 will detect this case too. Noteworthy changes in version 1.1.3 (2007-01-29) ------------------------------------------------ * Fixed a memory leak in gpgme_data_release_and_get_mem. * Fixed a bug in Windows command line quoting. Noteworthy changes in version 1.1.2 (2006-03-02) ------------------------------------------------ * Fixed a bug in the W32 glib backend. Noteworthy changes in version 1.1.1 (2006-02-23) ------------------------------------------------ * Fixed a bug in that the fingerprints of subkeys are not available. * Clarified usage of the SECRET flag in key listings. It is now reset for stub keys. * Reading signature notations and policy URLs on key signatures is supported. They can be found in the new field notations of the gpgme_key_sig_t structure. This has to be enabled with the keylist mode flag GPGME_KEYLIST_MODE_SIG_NOTATIONS. * A new gpgme_free() function solves the problem of using different allocators in a single program. This function should now be used instead calling free() to release the buffer returned by gpgme_data_release_and_get_mem. It is recommended that you always do this, but it is only necessary on certain platforms, so backwards compatibility is provided. In other words: If free() worked for you before, it will keep working. * New status codes GPGME_PKA_TRUST_GOOD and GPGME_PKA_TRUST_BAD. They are analyzed by the verify handlers and made available in the new PKA_TRUST and PKA_ADDRESS fields of the signature result structure. * Interface changes relative to the 1.1.0 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_key_sig_t EXTENDED: New field notations. GPGME_KEYLIST_MODE_SIG_NOTATIONS NEW gpgme_free NEW GPGME_STATUS_PKA_TRUST_BAD NEW GPGME_STATUS_PKA_TRUST_GOOD NEW gpgme_signature_t EXTENDED: New field pka_trust. gpgme_signature_t EXTENDED: New field pka_address. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 1.1.0 (2005-10-01) ------------------------------------------------ * You can now configure the backend engine file name and home directory to be used, as default and per context. * Information about the recipients of an encrypted text is now available at decryption time. * New status GPGME_STATUS_PLAINTEXT. This is analyzed by the decrypt and verify handlers, the information about the plaintext filename, if available is made available in the new field file_name of the respective result structure. * The code for "automagically detecting the thread library" has been removed from libgpgme. It is deprecated since version 0.4.3. Since then, you had to link against libgpgme-pthread for applications using pthread and libgpgme-pth for applications using GNU Pth. The code was removed because it caused compilation problems on systems where the pthread.h header from GNU Pth is available in addition to the system header (FreeBSD 6 and later for example). * "./autogen.sh --build-w32" does now build gpgme.dll. * [W32] The environment variable GPGME_DEBUG now uses a semicolon as delimiter. The standard install directory is used when locating gpg or gpgsm before finally falling back to the hardwired name. * There is a new flag for keys and subkeys, is_qualified, which indicates if a key can be used for qualified signatures according to local government regulations. * You can associate a filename with a data object using the new function gpgme_data_set_file_name(). This filename will be stored in the output when encrypting or signing the data and will be returned when decrypting or verifying the output data. * You can now set notation data at signature creation with the new function gpgme_sig_notation_add(). * Interface changes relative to the 1.0.3 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_set_engine_info NEW gpgme_ctx_get_engine_info NEW gpgme_ctx_set_engine_info NEW gpgme_recipient_t NEW gpgme_decrypt_result_t EXTENDED: New field recipients. gpgme_verify_result_t EXTENDED: New fields pubkey_algo, hash_algo. gpgme_decrypt_result_t EXTENDED: New field plaintext_filename. gpgme_verify_result_t EXTENDED: New field plaintext_filename. GPGME_STATUS_PLAINTEXT NEW gpgme_key_t EXTENDED: New field is_qualified. gpgme_subkey_t EXTENDED: New field is_qualified. gpgme_data_get_file_name NEW gpgme_data_set_file_name NEW gpgme_sig_notation_flags_t NEW GPGME_SIG_NOTATION_HUMAN_READABLE NEW GPGME_SIG_NOTATAION_CRITICAL NEW gpgme_sig_notation_clear NEW gpgme_sig_notation_add NEW gpgme_sig_notation_get NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 1.0.3 (2005-06-20) ------------------------------------------------ * Previousy, GPGME would use a default "include certs" of 1. This has been changed. Now GPGME will use the crypto backend engines default unless you set the value with gpgme_set_include_certs() explicitely. A new macro GPGME_INCLUDE_CERTS_DEFAULT can be used as a value to explicitely request the new default behaviour. Because the default changes, this is a slight change of the API semantics. We consider it to be a bug fix. * A bug which made GPGME hang has been fixed. If you have experienced hanging before, please try out this version and let me know if you still experience hanging problems. * Interface changes relative to the 0.9.0 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_set_include_certs CHANGED DEFAULT GPGME_INCLUDE_CERTS_DEFAULT NEW GPGME_STATUS_SIG_SUBPACKET NEW GPGME_STATUS_NEED_PASSPHRASE_PIN NEW GPGME_STATUS_SC_OP_FAILURE NEW GPGME_STATUS_SC_OP_SUCCESS NEW GPGME_STATUS_CARDCTRL NEW GPGME_STATUS_BACKUP_KEY_CREATED NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 1.0.2 (2004-12-28) ------------------------------------------------ * Changed the license of the library to the GNU Lesser General Public License (LGPL), version 2.1 or later. Noteworthy changes in version 1.0.1 (2004-10-22) ------------------------------------------------ * Only bug fixes. Noteworthy changes in version 1.0.0 (2004-09-30) ------------------------------------------------ * Version 1.0.0! We are proud to present you with a thoroughly tested and stable version of the GPGME library. A big Thank You! to all the people who made this possible. The development will be branched into a stable 1.x.y series and the head. * The gpgme.m4 macro supports checking the API version. Just prepend it to the required version string, separated by a colon. For example, this release has the version "1:1.0.0". The last release to which this version is (mostly) ABI compatible is "1:0.4.2", which is the default required version. Noteworthy changes in version 0.9.0 (2004-06-08) ------------------------------------------------ * The type gpgme_key_t has now a new field keylist_mode that contains the keylist mode that was active at the time the key was retrieved. * The type gpgme_decrypt_result_t has a new field "wrong_key_usage" that contains a flag indicating that the key should not have been used for encryption. * Verifying a signature of a revoked key gives the correct result now (GPG_ERR_CERT_REVOKED error code). * Clarified that the error code GPG_ERR_NO_DATA from the decrypt & verify operations still allows you to look at the signature verification result. * Clarified that patterns in keylisting operations have an upper limit, and thus are not suited to list many keys at once by their fingerprint. Also improve the error message if the pattern is too long for the CMS protocol to handle. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_key_t EXTENDED: New field keylist_mode. gpgme_decrypt_result_t EXTENDED: New field wrong_key_usage. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.4.7 (2004-04-29) ------------------------------------------------ * Correctly initialize the fields expired, revoked, invalid, and disabled in the gpgme_key_t structures. * A bug fix: The flag wrong_key_usage of gpgme_signature_t was accidently of type int instead unsigned int. * Interface changes relative to the 0.4.5 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_signature_t CHANGED: wrong_key_usage is unsigned int now. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.4.6 (2004-04-06) ------------------------------------------------ * Bug fixes Noteworthy changes in version 0.4.5 (2004-03-07) ------------------------------------------------ * GPGME is now compiled with LFS (large file support) by default. This means that _all_ programs using GPGME must be compiled with LFS support enabled by default. You can do this easily with autoconf, by using the AC_SYS_LARGEFILE macro. Or you can do this without autoconf by defining the preprocessor symbol _FILE_OFFSET_BITS to 64 (by passing the -D_FILE_OFFSET_BITS=64 to the C compiler command line, or by defining this preprocessor symbol before including any system header files). For more details, read the section on LFS in the manual. Up to now, it was undocumented that GPGME was not using LFS. But the public interfaces use off_t, and file descriptors are exchanged between the application and GPGME. This was an oversight, and bound to cause troubles in the future. Writing GPGME as a dual mode library that seamlessly supports LFS while keeping backwards compatibility is possible, but does not solve the problem: Many applications already expect GPGME to have LFS (they are compiled with off_t being a 64bit value). This is true in particular for the popular Gtk+ and Qt programs. So, although this is an ABI (but not an API) break, we will not change the library version to reflect that. Because the interfaces affected are probably not used yet in any GPGME 0.4 based application, we don't expect any real failures from this change. In fact, applications already using LFS will have some subtle bugs fixed. However, if you encounter an application using GPGME 0.4.x that does _not_ use LFS by default (off_t is a 32bit value), _and_ uses at least one of the functions gpgme_data_seek, gpgme_data_new_from_filepart, or a gpgme_data_seek_cb_t with gpgme_data_new_from_cbs, then indeed this library will be ABI incompatible with the program. As said above, we don't believe such a program exists. If we are in error, then you have two options: As a quick hack, you can configure GPGME with the --disable-largefile option. This will revert the change, and GPGME will not use LFS. However, GPGME will be incompatible with programs that expect GPGME to use LFS. All applications are required to use LFS when using GPGME, so this is only good as a temporary local work-around. The other option is to change the versioning of the library and recompile all applications. We have reserved a special version of the library for that, so you can do that without expecting a version clash in the future. Furthermore, everyone who does this will agree on the version to use (this is important for distribution makers). Read the comment in configure.ac (before LIBGPGME_LT_AGE) if you want to do this. Please don't do this blindly: As stated above, we think it is unlikely this measure is needed. Still, it is there if necessary. If in doubt, contact us and we will give our advise for your specific situation. * New key listing mode GPGME_KEYLIST_MODE_VALIDATE for validation of the listed keys. * New interface gpgme_cancel() that can be used to cancel asynchronous operations. * Interface changes relative to the 0.4.4 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_data_seek_cb_t CHANGED: off_t is now a largefile type. gpgme_data_seek CHANGED: off_t is now a largefile type. gpgme_data_new_from_filepart CHANGED: off_t is now a largefile type. GPGME_KEYLIST_MODE_VALIDATE NEW gpgme_cancel NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.4.4 (2004-01-12) ------------------------------------------------ * The member "class" in gpgme_key_sig_t and gpgme_new_signature_t has been renamed to "sig_class", to avoid clash with C++ compilers. In the C API, the old name "class" has been preserved for backwards compatibility, but is deprecated. * Interface changes relative to the 0.4.3 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_key_sig_t CHANGED: class deprecated, use new sig_class. gpgme_new_signature_t CHANGED: class deprecated, use new sig_class. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.4.3 (2003-10-06) ------------------------------------------------ * libgpgme should not be used for threaded programs anymore. This never worked reliably in all cases, because you had to be careful about the linking order and libtool wouldn't do that for you automatically. Instead, now you have to link against libgpgme-pthread for applications using pthread and libgpgme-pth for applications using GNU Pth. The old code for automagically detecting the thread library is still part of libgpgme, but it is DEPRECATED. * There are new automake macros AM_PATH_GPGME_PTH and AM_PATH_GPGME_PTHREAD, which support checking for thread-enabled versions of GPGME. They define GPGME_PTH_CFLAGS, GPGME_PTH_LIBS, GPGME_PTHREAD_CFLAGS and GPGME_PTHREAD_LIBS respectively. These variables of course also include the configuration for the thread package itself. Alternatively, use libtool. * gpgme_strerror_r as a thread safe variant of gpgme_strerror was added. * gpgme-config doesn't support setting the prefix or exec prefix anymore. I don't think it ever worked correctly, and it seems to be pointless. * gpgme_get_key fails with GPG_ERR_AMBIGUOUS_NAME if the key ID provided was not unique, instead returning the first matching key. * gpgme_key_t and gpgme_subkey_t have a new field, can_authenticate, that indicates if the key can be used for authentication. * gpgme_signature_t's status field is now correctly set to an error with error code GPG_ERR_NO_PUBKEY if public key is not found. * gpgme_new_signature_t's class field is now an unsigned int, rather than an unsigned long (the old class field is preserved for backwards compatibility). * A new function gpgme_set_locale() is provided to allow configuring the locale for the crypto backend. This is necessary for text terminals so that programs like the pinentry can be started with the right locale settings for the terminal the application is running on, in case the terminal has different settings than the system default (for example, if it is a remote terminal). You are highly recommended to call the following functions directly after gpgme_check_version: #include setlocale (LC_ALL, ""); gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL)); gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL)); GPGME can not do this for you, as setlocale is not thread safe, and there is no alternative. * The signal action for SIGPIPE is now set to SIG_IGN by gpgme_check_version, instead the first time a crypto engine is started (which is not well defined). * In the output of gpgme_hash_algo_name, change RMD160 to RIPEMD160, TIGER to TIGER192, CRC32-RFC1510 to CRC32RFC1510, and CRC24-RFC2440 to CRC24RFC2440. For now, these strings can be used as the MIC parameter for PGP/MIME (if appropriately modified). * Interface changes relative to the 0.4.2 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_strerror_t NEW gpgme_get_key CHANGED: Fails correctly if key ID not unique. gpgme_key_t EXTENDED: New field can_authenticate. gpgme_subkey_t EXTENDED: New field can_authenticate. gpgme_new_signature_t CHANGED: New type for class field. gpgme_set_locale NEW gpgme_hash_algo_name CHANGED: Slight adjustment of algo names. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.4.2 (2003-07-30) ------------------------------------------------ * Allow gpg-error to be in non-standard place when linking the test suite. * Configure will fail now if gpg-error can not be found. * Fixed initialized memory backed data objects for writing, which caused the test program to crash (but only on Mac OS, surprisingly). * Eliminate use of C99 constructs. * Small improvements to the manual. Noteworthy changes in version 0.4.1 (2003-06-06) ------------------------------------------------ This is the release that 0.4.0 should have been. There are many interface changes, please see below for the details. The changes are sometimes the result of new functionality, but more often express a paradigm shift. Others are an overdue cleanup to get GPGME in line with the GNU coding standards and to make the interface more self-consistent. Here is an overview on the changes: All types have been renamed to conform to the GNU coding standards, most of the time by keeping the whole name in lowercase and inserting underscores between words. All operations consistently only accept input parameters in their invocation function, and return only an error code directly. Further information about the result of the operation has to be retrieved afterwards by calling one of the result functions. This unifies the synchronous and the asynchronous interface. The error values have been completely replaced by a more sophisticated model that allows GPGME to transparently and accurately report all errors from the other GnuPG components, irregardless of process boundaries. This is achieved by using the library libgpg-errors, which is shared by all GnuPG components. This library is now required for GPGME. The results of all operations are now provided by pointers to C structs rather than by XML structs or in other ways. Objects which used to be opaque (for example a key) are now pointers to accessible structs, so no accessor functions are necessary. Backward compatibility is provided where it was possible without too much effort and did not collide with the overall sanitization effort. However, this is only for ease of transition. NO DEPRECATED FUNCTION OR DATA TYPE IS CONSIDERED A PART OF THE API OR ABI AND WILL BE DROPPED IN THE FUTURE WITHOUT CHANGING THE SONAME OF THE LIBRARY. Recommendations how to replace deprecated or removed functionality can be found within the description of each change. What follows are all changes to the interface and behaviour of GPGME in detail. * If gpgme.h is included in sources compiled by GCC 3.1 or later, deprecated attributes will warn about use of obsolete functions and type definitions. You can suppress these warnings by passing -Wno-deprecated-declarations to the gcc command. * The following types have been renamed. The old types are still available as aliases, but they are deprecated now: Old name: New name: GpgmeCtx gpgme_ctx_t GpgmeData gpgme_data_t GpgmeError gpgme_error_t GpgmeDataEncoding gpgme_data_encoding_t GpgmeSigStat gpgme_sig_stat_t GpgmeSigMode gpgme_sig_mode_t GpgmeAttr gpgme_attr_t GpgmeValidity gpgme_validity_t GpgmeProtocol gpgme_protocol_t GpgmeKey gpgme_key_t GpgmePassphraseCb gpgme_passphrase_cb_t GpgmeProgressCb gpgme_progress_cb_t GpgmeIOCb gpgme_io_cb_t GpgmeRegisterIOCb gpgme_register_io_cb_t GpgmeRemoveIOCb gpgme_remove_io_cb_t GpgmeEventIO gpgme_event_io_t GpgmeEventIOCb gpgme_event_io_cb_t GpgmeIOCbs gpgme_io_cbs GpgmeDataReadCb gpgme_data_read_cb_t GpgmeDataWriteCb gpgme_data_write_cb_t GpgmeDataSeekCb gpgme_data_seek_cb_t GpgmeDataReleaseCb gpgme_data_release_cb_t GpgmeDataCbs gpgme_data_cbs_t GpgmeTrustItem gpgme_trust_item_t GpgmeStatusCode gpgme_status_code_t * gpgme_error_t is now identical to gpg_error_t, the error type provided by libgpg-error. More about using libgpg-error with GPGME can be found in the manual. All error symbols have been removed! * All functions and types in libgpg-error have been wrapped in GPGME. The new types are gpgme_err_code_t and gpgme_err_source_t. The new functions are gpgme_err_code, gpgme_err_source, gpgme_error, gpgme_err_make, gpgme_error_from_errno, gpgme_err_make_from_errno, gpgme_err_code_from_errno, gpgme_err_code_to_errno, gpgme_strsource. * GPGME_ATTR_IS_SECRET is not anymore representable as a string. * GnuPG 1.2.2 is required. The progress callback is now also invoked for encrypt, sign, encrypt-sign, decrypt, verify, and decrypt-verify operations. For verify operations on detached signatures, the progress callback is invoked for both the detached signature and the plaintext message, though. * gpgme_passphrase_cb_t has been changed to not provide a complete description, but the UID hint, passphrase info and a flag indicating if this is a repeated attempt individually, so the user can compose his own description from this information. The passphrase is not returned as a C string, but must be written to a file descriptor directly. This allows for secure passphrase entries. The return type has been changed to gpgme_error_t value. This allowed to remove the gpgme_cancel function; just return the error code GPG_ERR_CANCELED in the passphrase callback directly. * gpgme_edit_cb_t has been changed to take a file descriptor argument. The user is expected to write the response to the file descriptor, followed by a newline. * The recipients interface has been removed. Instead, you use NULL-terminated lists of keys for specifying the recipients of an encryption operation. Use the new encryption flag GPGME_ENCRYPT_ALWAYS_TRUST if you want to override the validity of the keys (but note that in general this is not a good idea). This change has been made to the prototypes of gpgme_op_encrypt, gpgme_op_encrypt_start, gpgme_op_encrypt_sign and gpgme_op_encrypt_sign_start. The export interface has been changed to use pattern strings like the keylist interface. Thus, new functions gpgme_op_export_ext and gpgme_op_export_ext_start have been added as well. Now the prototypes of gpgme_op_export_start and gpgme_op_export finally make sense. * gpgme_op_verify and gpgme_op_decrypt_verify don't return a status summary anymore. Use gpgme_get_sig_status to retrieve the individual stati. * gpgme_io_cb_t changed from a void function to a function returning a gpgme_error_t value. However, it will always return 0, so you can safely ignore the return value. * A new I/O callback event GPGME_EVENT_START has been added. The new requirement is that you must wait until this event until you are allowed to call the I/O callback handlers previously registered for this context operation. Calling I/O callback functions for this context operation before the start event happened is unsafe because it can lead to race conditions in a multi-threaded environment. * The idle function feature has been removed. It was not precisely defined in a multi-threaded environment and is obsoleted by the user I/O callback functions. If you still need a simple way to call something while waiting on one or multiple asynchronous operations to complete, don't set the HANG flag in gpgme_wait (note that this will return to your program more often than the idle function did). * gpgme_wait can return NULL even if hang is true, if an error occurs. In that case *status contains the error code. * gpgme_get_engine_info was radically changed. Instead an XML string, an info structure of the new type gpgme_engine_info_t is returned. This makes it easier and more robust to evaluate the information in an application. * The new function gpgme_get_protocol_name can be used to convert a gpgme_protocol_t value into a string. * The status of a context operation is not checked anymore. Starting a new operation will silently cancel the previous one. Calling a function that requires you to have started an operation before without doing so is undefined. * The FPR argument to gpgme_op_genkey was removed. Instead, use the gpgme_op_genkey_result function to retrieve a gpgme_genkey_result_t pointer to a structure which contains the fingerprint. This also works with gpgme_op_genkey_start. The structure also provides other information about the generated keys. So, instead: char *fpr; err = gpgme_op_genkey (ctx, NULL, NULL, &fpr); if (!err && fpr) printf ("%s\n", fpr); you should now do: gpgme_genkey_result_t result; err = gpgme_op_genkey (ctx, NULL, NULL); if (!err) { result = gpgme_op_genkey_result (ctx); if (result->fpr) printf ("%s\n", result->fpr); } * The new gpgme_op_import_result function provides detailed information about the result of an import operation in gpgme_import_result_t and gpgme_import_status_t objects. Thus, the gpgme_op_import_ext variant is deprecated. * The new gpgme_op_sign_result function provides detailed information about the result of a signing operation in gpgme_sign_result_t, gpgme_invalid_key_t and gpgme_new_signature_t objects. * The new gpgme_op_encrypt_result function provides detailed information about the result of an encryption operation in a GpgmeEncryptResult object. * The new gpgme_op_decrypt_result function provides detailed information about the result of a decryption operation in a GpgmeDecryptResult object. * The new gpgme_op_verify_result function provides detailed information about the result of an verify operation in a GpgmeVerifyResult object. Because of this, the GPGME_SIG_STAT_* values, gpgme_get_sig_status, gpgme_get_sig_ulong_attr, gpgme_get_sig_string_attr and gpgme_get_sig_key are now deprecated, and gpgme_get_notation is removed. * GpgmeTrustItem objects have now directly accessible data, so the gpgme_trust_item_get_string_attr and gpgme_trust_item_get_ulong_attr accessor functions are deprecated. Also, reference counting is available through gpgme_trust_item_ref and gpgme_trust_item_unref (the gpgme_trust_item_release alias for the latter is deprecated). * Keys are not cached internally anymore, so the force_update argument to gpgme_get_key has been removed. * GpgmeKey objects have now directly accessible data so the gpgme_key_get_string_attr, gpgme_key_get_ulong_attr, gpgme_key_sig_get_string_attr and gpgme_key_sig_get_ulong_attr functions are deprecated. Also, gpgme_key_release is now deprecated. The gpgme_key_get_as_xml function has been dropped. * Because all interfaces using attributes are deprecated, the GpgmeAttr data type is also deprecated. * The new gpgme_op_keylist_result function provides detailed information about the result of a key listing operation in a GpgmeKeyListResult object. * Now that each function comes with its own result retrieval interface, the generic gpgme_get_op_info interface is not useful anymore and dropped. * The type and mode of data objects is not available anymore. * Interface changes relative to the 0.4.0 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GpgmeCtx DEPRECATED: Use gpgme_ctx_t. GpgmeData DEPRECATED: Use gpgme_data_t. GpgmeError DEPRECATED: Use gpgme_error_t. GpgmeDataEncoding DEPRECATED: Use gpgme_data_encoding_t. GpgmeSigStat DEPRECATED: Use gpgme_sig_stat_t. GpgmeSigMode DEPRECATED: Use gpgme_sig_mode_t. GpgmeAttr DEPRECATED: Use gpgme_attr_t. GpgmeValidity DEPRECATED: Use gpgme_validity_t. GpgmeProtocol DEPRECATED: Use gpgme_protocol_t. GpgmeKey DEPRECATED: Use gpgme_key_t. GpgmePassphraseCb DEPRECATED: Use gpgme_passphrase_cb_t. GpgmeProgressCb DEPRECATED: Use gpgme_progress_cb_t. GpgmeIOCb DEPRECATED: Use gpgme_io_cb_t. GpgmeRegisterIOCb DEPRECATED: Use gpgme_register_io_cb_t. GpgmeRemoveIOCb DEPRECATED: Use gpgme_remove_io_cb_t. GpgmeEventIO DEPRECATED: Use gpgme_event_io_t. GpgmeEventIOCb DEPRECATED: Use gpgme_event_io_cb_t. GpgmeIOCbs DEPRECATED: Use gpgme_io_cbs. GpgmeDataReadCb DEPRECATED: Use gpgme_data_read_cb_t. GpgmeDataWriteCb DEPRECATED: Use gpgme_data_write_cb_t. GpgmeDataSeekCb DEPRECATED: Use gpgme_data_seek_cb_t. GpgmeDataReleaseCb DEPRECATED: Use gpgme_data_release_cb_t. GpgmeDataCbs DEPRECATED: Use gpgme_data_cbs_t. GpgmeTrustItem DEPRECATED: Use gpgme_trust_item_t. GpgmeStatusCode DEPRECATED: Use gpgme_status_code_t. gpgme_ctx_t NEW gpgme_data_t NEW gpgme_recipients_t NEW gpgme_error_t NEW gpgme_data_encoding_t NEW gpgme_sig_stat_t NEW gpgme_sig_mode_t NEW gpgme_attr_t NEW gpgme_validity_t NEW gpgme_protocol_t NEW gpgme_key_t NEW gpgme_passphrase_cb_t NEW gpgme_progress_cb_t NEW gpgme_io_cb_t NEW gpgme_register_io_cb_t NEW gpgme_remove_io_cb_t NEW gpgme_event_io_t NEW gpgme_event_io_cb_t NEW gpgme_io_cbs NEW gpgme_data_read_cb_t NEW gpgme_data_write_cb_t NEW gpgme_data_seek_cb_t NEW gpgme_data_release_cb_t NEW gpgme_data_cbs_t NEW gpgme_trust_item_t NEW gpgme_status_code_t NEW GPGME_{some error code} REMOVED! Use GPG_ERR_* from libgpg-error. gpgme_err_code_t NEW gpgme_err_source_t NEW gpgme_err_code NEW gpgme_err_source NEW gpgme_error NEW gpgme_err_make NEW gpgme_error_from_errno NEW gpgme_err_make_from_errno NEW gpgme_err_code_from_errno NEW gpgme_err_code_to_errno NEW gpgme_strsource NEW gpgme_io_cb_t CHANGED: Return type from void to GpgmeError. gpgme_event_io_t CHANGED: New event type (all numbers changed). gpgme_passphrase_cb_t CHANGED: Desc decomposed, write directly to FD. gpgme_edit_cb_t CHANGED: Write directly to FD. gpgme_key_get_string_attr CHANGED: Don't handle GPGME_ATTR_IS_SECRET. gpgme_op_verify CHANGED: Drop R_STAT argument. gpgme_op_decrypt_verify CHANGED: Drop R_STAT argument. gpgme_wait CHANGED: Can return NULL even if hang is true. GpgmeIdleFunc REMOVED gpgme_register_idle REMOVED GpgmeRecipients REMOVED gpgme_recipients_new REMOVED gpgme_recipients_release REMOVED gpgme_recipients_add_name REMOVED gpgme_recipients_add_name_with_validity REMOVED gpgme_recipients_count REMOVED gpgme_recipients_enum_open REMOVED gpgme_recipients_enum_read REMOVED gpgme_recipients_enum_close REMOVED gpgme_encrypt_flags_t NEW GPGME_ENCRYPT_ALWAYS_TRUST NEW gpgme_op_encrypt CHANGED: Recipients passed as gpgme_key_t[]. gpgme_op_encrypt_start CHANGED: Recipients passed as gpgme_key_t[]. gpgme_op_encrypt_sign CHANGED: Recipients passed as gpgme_key_t[]. gpgme_op_encrypt_sign_start CHANGED: Recipients passed as gpgme_key_t[]. gpgme_op_export_start CHANGED: User IDs passed as patterns. gpgme_op_export CHANGED: User IDs passed as patterns. gpgme_op_export_ext_start NEW gpgme_op_export_ext NEW gpgme_keylist_mode_t NEW gpgme_sigsum_t NEW gpgme_engine_info_t NEW gpgme_get_engine_info CHANGED: Return info structure instead XML. gpgme_get_protocol_name NEW gpgme_cancel REMOVED: Return error in callback directly. gpgme_op_genkey CHANGED: FPR argument dropped. gpgme_op_genkey_result NEW gpgme_genkey_result_t NEW gpgme_op_import_ext DEPRECATED: Use gpgme_op_import_result. gpgme_op_import_result NEW gpgme_import_status_t NEW gpgme_import_result_t NEW gpgme_pubkey_algo_t NEW gpgme_hash_algo_t NEW gpgme_invalid_key_t NEW gpgme_new_signature_t NEW gpgme_sign_result_t NEW gpgme_op_sign_result NEW gpgme_pubkey_algo_name NEW gpgme_hash_algo_name NEW gpgme_encrypt_result_t NEW gpgme_op_encrypt_result NEW gpgme_decrypt_result_t NEW gpgme_op_decrypt_result NEW gpgme_verify_result_t NEW gpgme_op_verify_result NEW gpgme_get_notation REMOVED: Access verify result directly instead. gpgme_get_sig_key DEPRECATED: Use gpgme_get_key with fingerprint. gpgme_get_sig_ulong_attr DEPRECATED: Use verify result directly. gpgme_get_sig_string_attr DEPRECATED: Use verify result directly. GPGME_SIG_STAT_* DEPRECATED: Use error value in sig status. gpgme_get_sig_status DEPRECATED: Use verify result directly. gpgme_trust_item_t CHANGED: Now has user accessible data members. gpgme_trust_item_ref NEW gpgme_trust_item_unref NEW gpgme_trust_item_release DEPRECATED: Use gpgme_trust_item_unref. gpgme_trust_item_get_string_attr DEPRECATED gpgme_trust_item_get_ulong_attr DEPRECATED gpgme_get_key CHANGED: Removed force_update argument. gpgme_subkey_t NEW gpgme_key_sig_t NEW gpgme_user_id_t NEW gpgme_key_t CHANGED: Now has user accessible data members. gpgme_key_get_string_attr DEPRECATED gpgme_key_get_ulong_attr DEPRECATED gpgme_key_sig_get_string_attr DEPRECATED gpgme_key_sig_get_ulong_attr DEPRECATED gpgme_key_get_as_xml REMOVED gpgme_key_list_result_t NEW gpgme_op_keylist_result NEW gpgme_get_op_info REMOVED ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.4.0 (2002-12-23) ------------------------------------------------ * Key generation returns the fingerprint of the generated key. * New convenience function gpgme_get_key. * Supports signatures of user IDs in keys via the new GPGME_KEYLIST_MODE_SIGS keylist mode and the gpgme_key_sig_get_string_attr and gpgme_key_sig_get_ulong_attr interfaces. The XML info about a key also includes the signatures if available. * New data object interface, which is more flexible and transparent. * Interface changes relative to the 0.3.9 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GpgmeDataReadCb NEW GpgmeDataWriteCb NEW GpgmeDataSeekCb NEW GpgmeDataReleaseCb NEW GpgmeDataCbs NEW gpgme_data_read CHANGED: Match read() closely. gpgme_data_write CHANGED: Match write() closely. gpgme_data_seek NEW gpgme_data_new_from_fd NEW gpgme_data_new_from_stream NEW gpgme_data_new_from_cbs NEW gpgme_data_rewind DEPRECATED: Replaced by gpgme_data_seek(). gpgme_data_new_from_read_cb DEPRECATED: Replaced by gpgme_data_from_cbs(). gpgme_data_get_type REMOVED: No replacement. gpgme_op_verify CHANGED: Take different data objects for signed text and plain text. gpgme_op_verify_start CHANGED: See gpgme_op_verify. gpgme_check_engine REMOVED: Deprecated since 0.3.0. gpgme_op_genkey CHANGED: New parameter FPR. GPGME_KEYLIST_MODE_SIGS NEW gpgme_key_sig_get_string_attr NEW gpgme_key_sig_get_ulong_attr NEW gpgme_get_key NEW GPGME_ATTR_SIG_CLASS NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.3.16 (2003-11-19) ------------------------------------------------- * Compatibility fixes for GnuPG 1.9.x Noteworthy changes in version 0.3.15 (2003-02-18) ------------------------------------------------- * The progress status is sent via the progress callbacks in gpgme_op_edit. * Bug fix for signing operations with explicit signer settings for the CMS protocol. Noteworthy changes in version 0.3.14 (2002-12-04) ------------------------------------------------- * GPGME-Plug is now in its own package "cryptplug". * Workaround for a setlocale problem. Fixed a segv related to not correctly as closed marked file descriptors. Noteworthy changes in version 0.3.13 (2002-11-20) ------------------------------------------------- * Release due to changes in gpgmeplug. Noteworthy changes in version 0.3.12 (2002-10-15) ------------------------------------------------- * Fixed some bux with key listings. * The development has been branched to clean up some API issues. This 0.3 series will be kept for compatibility reasons; so do don't expect new features. Noteworthy changes in version 0.3.11 (2002-09-20) ------------------------------------------------- * Bug fixes. Noteworthy changes in version 0.3.10 (2002-09-02) ------------------------------------------------- * Setting the signing keys for the CMS protocol does now work. * The signers setting is honoured by gpgme_op_edit. Noteworthy changes in version 0.3.9 (2002-08-21) ------------------------------------------------ * A spec file for creating RPMs has been added. * An experimental interface to GnuPG's --edit-key functionality is introduced, see gpgme_op_edit. * The new gpgme_import_ext function provides a convenient access to the number of processed keys. * Interface changes relative to the 0.3.8 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GpgmeStatusCode NEW GpgmeEditCb NEW gpgme_op_edit_start NEW gpgme_op_edit NEW gpgme_op_import_ext NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.3.8 (2002-06-25) ------------------------------------------------ * It is possible to use an outside event loop for the I/O to the crypto engine by setting the I/O callbacks with gpgme_set_io_cbs. * Interface changes relative to the 0.3.6 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GpgmeIOCb NEW GpgmeRegisterIOCb NEW GpgmeRemoveIOCb NEW GpgmeEventIO NEW GpgmeEventIOCb NEW struct GpgmeIOCbs NEW gpgme_set_io_cbs NEW gpgme_get_io_cbs NEW GPGME_ATTR_ERRTOK NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.3.7 (2002-06-04) ------------------------------------------------ * GPGME_ATTR_OTRUST is implemented now. * A first step toward thread safeness has been achieved, see the documentation for details. Supported thread libraries are pthread and Pth. Noteworthy changes in version 0.3.6 (2002-05-03) ------------------------------------------------ * All error output of the gpgsm backend is send to the bit bucket. * The signature verification functions are extended. Instead of always returning GPGME_SIG_STATUS_GOOD, the functions new codes for expired signatures. 2 new functions may be used to retrieve more detailed information like the signature expiration time and a validity information of the key without an extra key looking. * The current passphrase callback and progress meter callback can be retrieved with the new functions gpgme_get_passphrase_cb and gpgme_get_progress_cb respectively. * Interface changes relative to the 0.3.5 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_get_passphrase_cb NEW gpgme_get_progress_cb NEW GpgmeDataEncoding NEW gpgme_data_set_encoding NEW gpgme_data_get_encoding NEW GPGME_SIG_STAT_GOOD_EXP NEW GPGME_SIG_STAT_GOOD_EXPKEY NEW gpgme_op_verify CHANGED: Returns more status codes. GPGME_ATTR_SIG_STATUS NEW gpgme_get_sig_string_attr NEW gpgme_get_sig_ulong_attr NEW gpgme_get_protocol NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.3.5 (2002-04-01) ------------------------------------------------ * gpgme_op_encrypt can be called with RECIPIENTS being 0. In this case, symmetric encryption is performed. Note that this requires a passphrase from the user. * More information is returned for X.509 certificates. * Interface changes relative to the 0.3.4 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_op_encrypt EXTENDED: Symmetric encryption possible ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.3.4 (2002-03-04) ------------------------------------------------ * gpgme_op_encrypt does now fail with GPGME_Invalid_Recipients if some recipients have been invalid, whereas earlier versions succeeded in this case. The plaintext is still encrypted for all valid recipients, so the application might take this error as a hint that the ciphertext is not usable for all requested recipients. Information about invalid recipients is available with gpgme_get_op_info. * gpgme_op_verify now allows to pass an uninitialized data object as its plaintext argument to check for normal and cleartext signatures. The plaintext is then returned in the data object. * New interfaces gpgme_set_include_certs and gpgme_get_include_certs to set and get the number of certifications to include in S/MIME signed messages. * New interfaces gpgme_op_encrypt_sign and gpgme_op_encrypt_sign_start to encrypt and sign a message in a combined operation. * New interface gpgme_op_keylist_ext_start to search for multiple patterns. * gpgme_key_get_ulong_attr supports the GPGME_ATTR_EXPIRE attribute. * Interface changes relative to the 0.3.3 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_op_encrypt CHANGED: Can fail with GPGME_Invalid_Recipients gpgme_op_verify EXTENDED: Accepts uninitialized text argument gpgme_key_get_ulong_attr EXTENDED: Supports GPGME_ATTR_EXPIRE gpgme_set_include_certs NEW gpgme_get_include_certs NEW gpgme_op_encrypt_sign NEW gpgme_op_encrypt_sign_start NEW gpgme_op_keylist_ext_start NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.3.3 (2002-02-12) ------------------------------------------------ * Fix the Makefile in jnlib. * Fix the test suite (hopefully). It should clean up all its state with `make check' now. Noteworthy changes in version 0.3.2 (2002-02-10) ------------------------------------------------ * Remove erroneous dependency on libgcrypt in jnlib. Noteworthy changes in version 0.3.1 (2002-02-09) ------------------------------------------------ * There is a Texinfo manual documenting the API. * The gpgme_set_keylist_mode function returns an error, and changed its meaning. It is no longer usable to select between normal and fast mode (newer versions of GnuPG will always be fast), but selects between local keyring, remote keyserver, or both. For this, two new macros are defined, GPGME_KEYLIST_MODE_LOCAL and GPGME_KEYLIST_MODE_EXTERN. To make it possible to modify the current setting, a fucntion gpgme_get_keylist_mode was added to retrieve the current mode. * gpgme_wait accepts a new argument STATUS to return the error status of the operation on the context. Its definition is closer to waitpid() now than before. * The LENGTH argument to gpgme_data_new_from_filepart changed its type from off_t to the unsigned size_t. * The R_HD argument to the GpgmePassphraseCb type changed its type from void* to void**. * New interface gpgme_op_trustlist_end() to match gpgme_op_keylist_end(). * The CryptPlug modules have been renamed to gpgme-openpgp and gpgme-smime, and they are installed in pkglibdir by `make install'. * An idle function can be registered with gpgme_register_idle(). * The GpgSM backend supports key generation with gpgme_op_genkey(). * Interface changes relative to the 0.3.0 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_data_new_from_filepart CHANGED: Type of LENGTH is size_t. GpgmePassphraseCb CHANGED: Type of R_HD is void **. gpgme_wait CHANGED: New argument STATUS. gpgme_set_keylist_mode CHANGED: Type of return value is GpgmeError. The function has a new meaning! gpgme_get_keylist_mode NEW GPGME_KEYLIST_MODE_LOCAL NEW GPGME_KEYLIST_MODE_EXTERN NEW gpgme_op_trustlist_next NEW GpgmeIdleFunc NEW gpgme_register_idle NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.3.0 (2001-12-19) ------------------------------------------------ * New interface gpgme_set_protocol() to set the protocol and thus the crypto engine to be used by the context. Currently, the OpenPGP and the CMS protocols are supported. They are specified by the new preprocessor symbols GPGME_PROTOCOL_OpenPGP and GPGME_PROTOCOL_CMS. A new context uses the OpenPGP engine by default. * gpgme_get_engine_info() returns information for all crypto engines compiled into the library. The XML format has changed. To reliably get the version of a crypto engine, the tag after the appropriate tag has to be looked for. * New interface gpgme_engine_check_version(), obsoleting gpgme_check_engine(). Check the version of all engines you are supporting in your software. * GpgmeKey lists the user ids in the order as they are returned by GnuPG, first the primary key with index 0, then the sub-user ids. * New operation gpgme_op_decrypt_verify() to decrypt and verify signatures simultaneously. * The new interface gpgme_op_keylist_end() terminates a pending keylist operation. A keylist operation is also terminated when gpgme_op_keylist_next() returns GPGME_EOF. * GPGME can be compiled without GnuPG being installed (`--with-gpg=PATH'), cross-compiled, or even compiled without support for GnuPG (`--without-gpg'). * GPGME can be compiled with support for GpgSM (GnuPG for S/MIME, `--with-gpgsm=PATH'). It is enabled by default if the `gpgsm' is found in the path, but it can also be compiled without support for GpgSM (`--without-gpgsm'). * CryptPlug modules for GPGME are included and can be enabled at configure time (`--enable-gpgmeplug'). There is one module which uses the GnuPG engine (`gpgmeplug') and one module which uses the GpgSM engine (`gpgsmplug'). * Interface changes relative to the latest 0.2.x release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgme_key_get_as_xml CHANGED: Sub-user ids reversed in order. gpgme_key_get_string_attr CHANGED: User ids reversed in order. gpgme_key_get_ulong_attr CHANGED: User ids reversed in order. gpgme_get_engine_info CHANGED: New format, extended content. gpgme_engine_check_version NEW gpgme_decrypt_verify_start NEW gpgme_decrypt_verify NEW gpgme_op_keylist_next NEW gpgme_set_protocol NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.2.3 (2001-09-17) ------------------------------------------------ * New function gpgme_get_op_info which can be used to get the micalg parameter needed for MOSS. * New functions gpgme_get_armor and gpgme_get_textmode. * The usual bug fixes and some minor functionality improvements. * Added a simple encryption component for MS-Windows; however the build procedure might have some problems. Noteworthy changes in version 0.2.2 (2001-06-12) ------------------------------------------------ * Implemented a key cache. * Fixed a race condition under W32 and some other bug fixes. Noteworthy changes in version 0.2.1 (2001-04-02) ------------------------------------------------ * Changed debug output and GPGME_DEBUG variable (gpgme/debug.c) * Handle GnuPG's new key capabilities output and support revocation et al. attributes * Made the W32 support more robust. Copyright 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010 g10 Code GmbH This file is free software; as a special exception the author gives unlimited permission to copy and/or distribute it, with or without modifications, as long as this notice is preserved. This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, to the extent permitted by law; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. diff --git a/doc/gpgme.texi b/doc/gpgme.texi index cbb0e649..1df9c46e 100644 --- a/doc/gpgme.texi +++ b/doc/gpgme.texi @@ -1,7996 +1,8073 @@ \input texinfo @c -*- mode: texinfo; coding: utf-8; -*- @documentencoding UTF-8 @setfilename gpgme.info @include defs.inc @settitle The `GnuPG Made Easy' Reference Manual @dircategory GNU Libraries @direntry * @acronym{GPGME}: (gpgme). Adding support for cryptography to your program. @end direntry @c Unify some of the indices. @syncodeindex tp fn @syncodeindex pg fn @copying Copyright @copyright{} 2002--2008, 2010, 2012--2017 g10 Code GmbH. @quotation Permission is granted to copy, distribute and/or modify this document under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. The text of the license can be found in the section entitled ``Copying''. @end quotation This document is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. @end copying @c Macros used by the description of the UI server protocol @macro clnt{string} @sc{c:} \string\ @end macro @macro srvr{string} @sc{s:} \string\ @end macro @c API version. @macro since{string} @sc{Since:} \string\ @end macro @c @c T I T L E P A G E @c @ifinfo This file documents the @acronym{GPGME} library. This is Edition @value{EDITION}, last updated @value{UPDATED}, of @cite{The `GnuPG Made Easy' Reference Manual}, for Version @value{VERSION}. @c NOTE: Don't forget to update the year for the TeX version, too. @insertcopying @end ifinfo @c We do not want that bastard short titlepage. @c @iftex @c @shorttitlepage The `GnuPG Made Easy' Reference Manual @c @end iftex @titlepage @center @titlefont{The `GnuPG Made Easy'} @sp 1 @center @titlefont{Reference Manual} @sp 6 @center Edition @value{EDITION} @sp 1 @center last updated @value{UPDATED} @sp 1 @center for version @value{VERSION} @page @vskip 0pt plus 1filll Published by The GnuPG Project@* c/o g10 Code GmbH@* Hüttenstr. 61@* 40699 Erkrath, Germany @insertcopying @end titlepage @page @summarycontents @contents @ifnottex @node Top @top Main Menu This is Edition @value{EDITION}, last updated @value{UPDATED}, of @cite{The `GnuPG Made Easy' Reference Manual}, for Version @value{VERSION} of the @acronym{GPGME} library. @end ifnottex @menu * Introduction:: How to use this manual. * Preparation:: What you should do before using the library. * Protocols and Engines:: Supported crypto protocols. * Algorithms:: Supported algorithms. * Error Handling:: Error numbers and their meanings. * Exchanging Data:: Passing data to and from @acronym{GPGME}. * Contexts:: Handling @acronym{GPGME} contexts. Appendices * UI Server Protocol:: The GnuPG UI Server Protocol. * Debugging:: How to solve problems. * Deprecated Functions:: Documentation of deprecated functions. * Library Copying:: The GNU Lesser General Public License says how you can copy and share `GnuPG Made Easy'. * Copying:: The GNU General Public License says how you can copy and share this manual. Indices * Concept Index:: Index of concepts and programs. * Function and Data Index:: Index of functions, variables and data types. @detailmenu --- The Detailed Node Listing --- Introduction * Getting Started:: Purpose of the manual, and how to use it. * Features:: Reasons to install and use @acronym{GPGME}. * Overview:: Basic architecture of the @acronym{GPGME} library. Preparation * Header:: What header file you need to include. * Building the Source:: Compiler options to be used. * Largefile Support (LFS):: How to use @acronym{GPGME} with LFS. * Using Automake:: Compiler options to be used the easy way. * Using Libtool:: Avoiding compiler options entirely. * Library Version Check:: Getting and verifying the library version. * Signal Handling:: How @acronym{GPGME} affects signal handling. * Multi-Threading:: How @acronym{GPGME} can be used in an MT environment. Protocols and Engines * Engine Version Check:: Verifying the engine version. * Engine Information:: Obtaining more information about the engines. * Engine Configuration:: Changing the engine configuration. * OpenPGP:: Support for the OpenPGP protocol. * Cryptographic Message Syntax:: Support for the CMS. Algorithms * Public Key Algorithms:: A list of all public key algorithms. * Hash Algorithms:: A list of all hash algorithms. Error Handling * Error Values:: The error value and what it means. * Error Codes:: A list of important error codes. * Error Sources:: A list of important error sources. * Error Strings:: How to get a descriptive string from a value. Exchanging Data * Creating Data Buffers:: Creating new data buffers. * Destroying Data Buffers:: Releasing data buffers. * Manipulating Data Buffers:: Operations on data buffers. Creating Data Buffers * Memory Based Data Buffers:: Creating memory based data buffers. * File Based Data Buffers:: Creating file based data buffers. * Callback Based Data Buffers:: Creating callback based data buffers. Manipulating Data Buffers * Data Buffer I/O Operations:: I/O operations on data buffers. * Data Buffer Meta-Data:: Meta-data manipulation of data buffers. * Data Buffer Convenience:: Convenience function for data buffers. Contexts * Creating Contexts:: Creating new @acronym{GPGME} contexts. * Destroying Contexts:: Releasing @acronym{GPGME} contexts. * Result Management:: Managing the result of crypto operations. * Context Attributes:: Setting properties of a context. * Key Management:: Managing keys with @acronym{GPGME}. * Trust Item Management:: Managing trust items with @acronym{GPGME}. * Crypto Operations:: Using a context for cryptography. * Miscellaneous:: Miscellaneous operations. * Run Control:: Controlling how operations are run. Context Attributes * Protocol Selection:: Selecting the protocol used by a context. * Crypto Engine:: Configuring the crypto engine. * Setting the Sender:: How to tell the engine the sender. * ASCII Armor:: Requesting @acronym{ASCII} armored output. * Text Mode:: Choosing canonical text mode. * Offline Mode:: Choosing offline mode. * Included Certificates:: Including a number of certificates. * Key Listing Mode:: Selecting key listing mode. * Passphrase Callback:: Getting the passphrase from the user. * Progress Meter Callback:: Being informed about the progress. * Status Message Callback:: Status messages received from gpg. * Locale:: Setting the locale of a context. Key Management * Key objects:: Description of the key structures. * Listing Keys:: Browsing the list of available keys. * Information About Keys:: Requesting detailed information about keys. * Manipulating Keys:: Operations on keys. * Generating Keys:: Creating new key pairs. * Signing Keys:: Adding key signatures to public keys. * Exporting Keys:: Retrieving key data from the key ring. * Importing Keys:: Adding keys to the key ring. * Deleting Keys:: Removing keys from the key ring. * Changing Passphrases:: Change the passphrase of a key. * Changing TOFU Data:: Changing data pertaining to TOFU. * Advanced Key Editing:: Advanced key edit operation. Trust Item Management * Listing Trust Items:: Browsing the list of available trust items. * Manipulating Trust Items:: Operations on trust items. Crypto Operations * Decrypt:: Decrypting a ciphertext. * Verify:: Verifying a signature. * Decrypt and Verify:: Decrypting a signed ciphertext. * Sign:: Creating a signature. * Encrypt:: Encrypting a plaintext. Sign * Selecting Signers:: How to choose the keys to sign with. * Creating a Signature:: How to create a signature. * Signature Notation Data:: How to add notation data to a signature. Encrypt * Encrypting a Plaintext:: How to encrypt a plaintext. Miscellaneous * Running other Programs:: Running other Programs. * Using the Assuan protocol:: Using the Assuan protocol. * Checking for updates:: How to check for software updates. Run Control * Waiting For Completion:: Waiting until an operation is completed. * Using External Event Loops:: Advanced control over what happens when. * Cancellation:: How to end pending operations prematurely. Using External Event Loops * I/O Callback Interface:: How I/O callbacks are registered. * Registering I/O Callbacks:: How to use I/O callbacks for a context. * I/O Callback Example:: An example how to use I/O callbacks. * I/O Callback Example GTK+:: How to integrate @acronym{GPGME} in GTK+. * I/O Callback Example GDK:: How to integrate @acronym{GPGME} in GDK. * I/O Callback Example Qt:: How to integrate @acronym{GPGME} in Qt. @end detailmenu @end menu @node Introduction @chapter Introduction `GnuPG Made Easy' (@acronym{GPGME}) is a C language library that allows to add support for cryptography to a program. It is designed to make access to public key crypto engines like GnuPG or GpgSM easier for applications. @acronym{GPGME} provides a high-level crypto API for encryption, decryption, signing, signature verification and key management. @acronym{GPGME} uses GnuPG and GpgSM as its backends to support OpenPGP and the Cryptographic Message Syntax (CMS). @menu * Getting Started:: Purpose of the manual, and how to use it. * Features:: Reasons to install and use @acronym{GPGME}. * Overview:: Basic architecture of the @acronym{GPGME} library. @end menu @node Getting Started @section Getting Started This manual documents the @acronym{GPGME} library programming interface. All functions and data types provided by the library are explained. The reader is assumed to possess basic knowledge about cryptography in general, and public key cryptography in particular. The underlying cryptographic engines that are used by the library are not explained, but where necessary, special features or requirements by an engine are mentioned as far as they are relevant to @acronym{GPGME} or its users. This manual can be used in several ways. If read from the beginning to the end, it gives a good introduction into the library and how it can be used in an application. Forward references are included where necessary. Later on, the manual can be used as a reference manual to get just the information needed about any particular interface of the library. Experienced programmers might want to start looking at the examples at the end of the manual, and then only read up those parts of the interface which are unclear. The documentation for the language bindings is currently not included in this manual. Those languages bindings follow the general programming model of @acronym{GPGME} but may provide some extra high level abstraction on top of the @acronym{GPGME} style API. For now please see the README files in the @file{lang/} directory of the source distribution. @node Features @section Features @acronym{GPGME} has a couple of advantages over other libraries doing a similar job, and over implementing support for GnuPG or other crypto engines into your application directly. @table @asis @item it's free software Anybody can use, modify, and redistribute it under the terms of the GNU Lesser General Public License (@pxref{Library Copying}). @item it's flexible @acronym{GPGME} provides transparent support for several cryptographic protocols by different engines. Currently, @acronym{GPGME} supports the OpenPGP protocol using GnuPG as the backend, and the Cryptographic Message Syntax using GpgSM as the backend. @item it's easy @acronym{GPGME} hides the differences between the protocols and engines from the programmer behind an easy-to-use interface. This way the programmer can focus on the other parts of the program, and still integrate strong cryptography in his application. Once support for @acronym{GPGME} has been added to a program, it is easy to add support for other crypto protocols once @acronym{GPGME} backends provide them. @item it's language friendly @acronym{GPGME} comes with languages bindings for several common programming languages: Common Lisp, C++, Python 2, and Python 3. @end table @node Overview @section Overview @acronym{GPGME} provides a data abstraction that is used to pass data to the crypto engine, and receive returned data from it. Data can be read from memory or from files, but it can also be provided by a callback function. The actual cryptographic operations are always set within a context. A context provides configuration parameters that define the behaviour of all operations performed within it. Only one operation per context is allowed at any time, but when one operation is finished, you can run the next operation in the same context. There can be more than one context, and all can run different operations at the same time. Furthermore, @acronym{GPGME} has rich key management facilities including listing keys, querying their attributes, generating, importing, exporting and deleting keys, and acquiring information about the trust path. With some precautions, @acronym{GPGME} can be used in a multi-threaded environment, although it is not completely thread safe and thus needs the support of the application. @node Preparation @chapter Preparation To use @acronym{GPGME}, you have to perform some changes to your sources and the build system. The necessary changes are small and explained in the following sections. At the end of this chapter, it is described how the library is initialized, and how the requirements of the library are verified. @menu * Header:: What header file you need to include. * Building the Source:: Compiler options to be used. * Largefile Support (LFS):: How to use @acronym{GPGME} with LFS. * Using Automake:: Compiler options to be used the easy way. * Using Libtool:: Avoiding compiler options entirely. * Library Version Check:: Getting and verifying the library version. * Signal Handling:: How @acronym{GPGME} affects signal handling. * Multi-Threading:: How @acronym{GPGME} can be used in an MT environment. @end menu @node Header @section Header @cindex header file @cindex include file All interfaces (data types and functions) of the library are defined in the header file `gpgme.h'. You must include this in all programs using the library, either directly or through some other header file, like this: @example #include @end example The name space of @acronym{GPGME} is @code{gpgme_*} for function names and data types and @code{GPGME_*} for other symbols. Symbols internal to @acronym{GPGME} take the form @code{_gpgme_*} and @code{_GPGME_*}. Because @acronym{GPGME} makes use of the GPG Error library, using @acronym{GPGME} will also use the @code{GPG_ERR_*} name space directly, and the @code{gpg_err*}, @code{gpg_str*}, and @code{gpgrt_*} name space indirectly. @node Building the Source @section Building the Source @cindex compiler options @cindex compiler flags If you want to compile a source file including the `gpgme.h' header file, you must make sure that the compiler can find it in the directory hierarchy. This is accomplished by adding the path to the directory in which the header file is located to the compilers include file search path (via the @option{-I} option). However, the path to the include file is determined at the time the source is configured. To solve this problem, gpgme ships with a small helper program @command{gpgme-config} that knows about the path to the include file and other configuration options. The options that need to be added to the compiler invocation at compile time are output by the @option{--cflags} option to @command{gpgme-config}. The following example shows how it can be used at the command line: @example gcc -c foo.c `gpgme-config --cflags` @end example Adding the output of @samp{gpgme-config --cflags} to the compiler command line will ensure that the compiler can find the @acronym{GPGME} header file. A similar problem occurs when linking the program with the library. Again, the compiler has to find the library files. For this to work, the path to the library files has to be added to the library search path (via the @option{-L} option). For this, the option @option{--libs} to @command{gpgme-config} can be used. For convenience, this option also outputs all other options that are required to link the program with @acronym{GPGME} (in particular, the @samp{-lgpgme} option). The example shows how to link @file{foo.o} with the @acronym{GPGME} library to a program @command{foo}. @example gcc -o foo foo.o `gpgme-config --libs` @end example Of course you can also combine both examples to a single command by specifying both options to @command{gpgme-config}: @example gcc -o foo foo.c `gpgme-config --cflags --libs` @end example If you need to detect the installed language bindings you can use list them using: @example gpgme-config --print-lang @end example or test for the availability using @example gpgme-config --have-lang=python && echo 'Bindings for Pythons available' @end example @node Largefile Support (LFS) @section Largefile Support (LFS) @cindex largefile support @cindex LFS @acronym{GPGME} is compiled with largefile support by default, if it is available on the system. This means that GPGME supports files larger than two gigabyte in size, if the underlying operating system can. On some systems, largefile support is already the default. On such systems, nothing special is required. However, some systems provide only support for files up to two gigabyte in size by default. Support for larger file sizes has to be specifically enabled. To make a difficult situation even more complex, such systems provide two different types of largefile support. You can either get all relevant functions replaced with alternatives that are largefile capable, or you can get new functions and data types for largefile support added. Those new functions have the same name as their smallfile counterparts, but with a suffix of 64. An example: The data type @code{off_t} is 32 bit wide on GNU/Linux PC systems. To address offsets in large files, you can either enable largefile support add-on. Then a new data type @code{off64_t} is provided, which is 64 bit wide. Or you can replace the existing @code{off_t} data type with its 64 bit wide counterpart. All occurrences of @code{off_t} are then automagically replaced. As if matters were not complex enough, there are also two different types of file descriptors in such systems. This is important because if file descriptors are exchanged between programs that use a different maximum file size, certain errors must be produced on some file descriptors to prevent subtle overflow bugs from occurring. As you can see, supporting two different maximum file sizes at the same time is not at all an easy task. However, the maximum file size does matter for @acronym{GPGME}, because some data types it uses in its interfaces are affected by that. For example, the @code{off_t} data type is used in the @code{gpgme_data_seek} function, to match its @acronym{POSIX} counterpart. This affects the call-frame of the function, and thus the ABI of the library. Furthermore, file descriptors can be exchanged between GPGME and the application. For you as the user of the library, this means that your program must be compiled in the same file size mode as the library. Luckily, there is absolutely no valid reason for new programs to not enable largefile support by default and just use that. The compatibility modes (small file sizes or dual mode) can be considered an historic artefact, only useful to allow for a transitional period. On POSIX platforms @acronym{GPGME} is compiled using largefile support by default. This means that your application must do the same, at least as far as it is relevant for using the @file{gpgme.h} header file. All types in this header files refer to their largefile counterparts, if they are different from any default types on the system. On 32 and 64 bit Windows platforms @code{off_t} is declared as 32 bit signed integer. There is no specific support for LFS in the C library. The recommendation from Microsoft is to use the native interface (@code{CreateFile} et al.) for large files. Released binary versions of @acronym{GPGME} (libgpgme-11.dll) have always been build with a 32 bit @code{off_t}. To avoid an ABI break we stick to this convention for 32 bit Windows by using @code{long} there. @acronym{GPGME} versions for 64 bit Windows have never been released and thus we are able to use @code{int64_t} instead of @code{off_t} there. For easier migration the typedef @code{gpgme_off_t} has been defined. The reason we cannot use @code{off_t} directly is that some toolchains (e.g. mingw64) introduce a POSIX compatible hack for @code{off_t}. Some widely used toolkits make use of this hack and in turn @acronym{GPGME} would need to use it also. However, this would introduce an ABI break and existing software making use of libgpgme might suffer from a severe break. Thus with version 1.4.2 we redefined all functions using @code{off_t} to use @code{gpgme_off_t} which is defined as explained above. This way we keep the ABI well defined and independent of any toolchain hacks. The bottom line is that LFS support in @acronym{GPGME} is only available on 64 bit versions of Windows. On POSIX platforms you can enable largefile support, if it is different from the default on the system the application is compiled on, by using the Autoconf macro @code{AC_SYS_LARGEFILE}. If you do this, then you don't need to worry about anything else: It will just work. In this case you might also want to use @code{AC_FUNC_FSEEKO} to take advantage of some new interfaces, and @code{AC_TYPE_OFF_T} (just in case). If you do not use Autoconf, you can define the preprocessor symbol @code{_FILE_OFFSET_BITS} to 64 @emph{before} including any header files, for example by specifying the option @code{-D_FILE_OFFSET_BITS=64} on the compiler command line. You will also want to define the preprocessor symbol @code{LARGEFILE_SOURCE} to 1 in this case, to take advantage of some new interfaces. If you do not want to do either of the above, you probably know enough about the issue to invent your own solution. Just keep in mind that the @acronym{GPGME} header file expects that largefile support is enabled, if it is available. In particular, we do not support dual mode (@code{_LARGEFILE64_SOURCE}). @node Using Automake @section Using Automake @cindex automake @cindex autoconf It is much easier if you use GNU Automake instead of writing your own Makefiles. If you do that you do not have to worry about finding and invoking the @command{gpgme-config} script at all. @acronym{GPGME} provides an extension to Automake that does all the work for you. @c A simple macro for optional variables. @macro ovar{varname} @r{[}@var{\varname\}@r{]} @end macro @defmac AM_PATH_GPGME (@ovar{minimum-version}, @ovar{action-if-found}, @ovar{action-if-not-found}) @defmacx AM_PATH_GPGME_PTH (@ovar{minimum-version}, @ovar{action-if-found}, @ovar{action-if-not-found}) @defmacx AM_PATH_GPGME_PTHREAD (@ovar{minimum-version}, @ovar{action-if-found}, @ovar{action-if-not-found}) Check whether @acronym{GPGME} (at least version @var{minimum-version}, if given) exists on the host system. If it is found, execute @var{action-if-found}, otherwise do @var{action-if-not-found}, if given. Additionally, the function defines @code{GPGME_CFLAGS} to the flags needed for compilation of the program to find the @file{gpgme.h} header file, and @code{GPGME_LIBS} to the linker flags needed to link the program to the @acronym{GPGME} library. If the used helper script does not match the target type you are building for a warning is printed and the string @code{libgcrypt} is appended to the variable @code{gpg_config_script_warn}. @code{AM_PATH_GPGME_PTH} checks for the version of @acronym{GPGME} that can be used with GNU Pth, and defines @code{GPGME_PTH_CFLAGS} and @code{GPGME_PTH_LIBS}. @code{AM_PATH_GPGME_PTHREAD} checks for the version of @acronym{GPGME} that can be used with the native pthread implementation, and defines @code{GPGME_PTHREAD_CFLAGS} and @code{GPGME_PTHREAD_LIBS}. Since version 1.8.0 this is no longer required to GPGME_PTHREAD as @acronym{GPGME} itself is thread safe. This macro searches for @command{gpgme-config} along the PATH. If you are cross-compiling, it is useful to set the environment variable @code{SYSROOT} to the top directory of your target. The macro will then first look for the helper program in the @file{bin} directory below that top directory. An absolute directory name must be used for @code{SYSROOT}. Finally, if the configure command line option @code{--with-gpgme-prefix} is used, only its value is used for the top directory below which the helper script is expected. @end defmac You can use the defined Autoconf variables like this in your @file{Makefile.am}: @example AM_CPPFLAGS = $(GPGME_CFLAGS) LDADD = $(GPGME_LIBS) @end example @node Using Libtool @section Using Libtool @cindex libtool The easiest way is to just use GNU Libtool. If you use libtool, and link to @code{libgpgme.la}, @code{libgpgme-pth.la} or @code{libgpgme-pthread.la} respectively, everything will be done automatically by Libtool. @node Library Version Check @section Library Version Check @cindex version check, of the library @deftypefun {const char *} gpgme_check_version (@w{const char *@var{required_version}}) The function @code{gpgme_check_version} has four purposes. It can be used to retrieve the version number of the library. In addition it can verify that the version number is higher than a certain required version number. In either case, the function initializes some sub-systems, and for this reason alone it must be invoked early in your program, before you make use of the other functions in @acronym{GPGME}. The last purpose is to run selftests. As a side effect for W32 based systems, the socket layer will get initialized. If @var{required_version} is @code{NULL}, the function returns a pointer to a statically allocated string containing the version number of the library. If @var{required_version} is not @code{NULL}, it should point to a string containing a version number, and the function checks that the version of the library is at least as high as the version number provided. In this case, the function returns a pointer to a statically allocated string containing the version number of the library. If @var{REQUIRED_VERSION} is not a valid version number, or if the version requirement is not met, the function returns @code{NULL}. If you use a version of a library that is backwards compatible with older releases, but contains additional interfaces which your program uses, this function provides a run-time check if the necessary features are provided by the installed version of the library. If a selftest fails, the function may still succeed. Selftest errors are returned later when invoking @code{gpgme_new} or @code{gpgme-data_new}, so that a detailed error code can be returned (historically, @code{gpgme_check_version} does not return a detailed error code). @end deftypefun @deftypefun {int} gpgme_set_global_flag @ (@w{const char *@var{name}}, @ @w{const char *@var{value}}) @since{1.4.0} On some systems it is not easy to set environment variables and thus hard to use @acronym{GPGME}'s internal trace facility for debugging. This function has been introduced as an alternative way to enable debugging and for a couple of other rarely used tweaks. It is important to assure that only one thread accesses @acronym{GPGME} functions between a call to this function and after the return from the call to @code{gpgme_check_version}. All currently supported features require that this function is called as early as possible --- even before @code{gpgme_check_version}. The features are identified by the following values for @var{name}: @table @code @item debug To enable debugging use the string ``debug'' for @var{name} and @var{value} identical to the value used with the environment variable @code{GPGME_DEBUG}. @item disable-gpgconf Using this feature with any @var{value} disables the detection of the gpgconf program and thus forces GPGME to fallback into the simple OpenPGP only mode. It may be used to force the use of GnuPG-1 on systems which have both GPG versions installed. Note that in general the use of @code{gpgme_set_engine_info} is a better way to select a specific engine version. @item gpgconf-name @itemx gpg-name Set the name of the gpgconf respective gpg binary. The defaults are @code{GNU/GnuPG/gpgconf} and @code{GNU/GnuPG/gpg}. Under Unix the leading directory part is ignored. Under Windows the leading directory part is used as the default installation directory; the @code{.exe} suffix is added by GPGME. Use forward slashed even under Windows. @item require-gnupg Set the mimimum version of the required GnuPG engine. If that version is not met, GPGME fails early instead of trying to use the existent version. The given version must be a string with major, minor, and micro number. Example: "2.1.0". @item w32-inst-dir On Windows GPGME needs to know its installation directory to find its spawn helper. This is in general no problem because a DLL has this information. Some applications however link statically to GPGME and thus GPGME can only figure out the installation directory of this application which may be wrong in certain cases. By supplying an installation directory as value to this flag, GPGME will assume that that directory is the installation directory. This flag has no effect on non-Windows platforms. @end table This function returns @code{0} on success. In contrast to other functions the non-zero return value on failure does not convey any error code. For setting ``debug'' the only possible error cause is an out of memory condition; which would exhibit itself later anyway. Thus the return value may be ignored. @end deftypefun After initializing @acronym{GPGME}, you should set the locale information to the locale required for your output terminal. This locale information is needed for example for the curses and Gtk pinentry. Here is an example of a complete initialization: @example #include #include void init_gpgme (void) @{ /* Initialize the locale environment. */ setlocale (LC_ALL, ""); gpgme_check_version (NULL); gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL)); #ifdef LC_MESSAGES gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL)); #endif @} @end example Note that you are highly recommended to initialize the locale settings like this. @acronym{GPGME} can not do this for you because it would not be thread safe. The conditional on LC_MESSAGES is only necessary for portability to W32 systems. @node Signal Handling @section Signal Handling @cindex signals @cindex signal handling The @acronym{GPGME} library communicates with child processes (the crypto engines). If a child process dies unexpectedly, for example due to a bug, or system problem, a @code{SIGPIPE} signal will be delivered to the application. The default action is to abort the program. To protect against this, @code{gpgme_check_version} sets the @code{SIGPIPE} signal action to @code{SIG_IGN}, which means that the signal will be ignored. @acronym{GPGME} will only do that if the signal action for @code{SIGPIPE} is @code{SIG_DEF} at the time @code{gpgme_check_version} is called. If it is something different, @code{GPGME} will take no action. This means that if your application does not install any signal handler for @code{SIGPIPE}, you don't need to take any precautions. If you do install a signal handler for @code{SIGPIPE}, you must be prepared to handle any @code{SIGPIPE} events that occur due to @acronym{GPGME} writing to a defunct pipe. Furthermore, if your application is multi-threaded, and you install a signal action for @code{SIGPIPE}, you must make sure you do this either before @code{gpgme_check_version} is called or afterwards. @node Multi-Threading @section Multi-Threading @cindex thread-safeness @cindex multi-threading The @acronym{GPGME} library is mostly thread-safe, and can be used in a multi-threaded environment but there are some requirements for multi-threaded use: @itemize @bullet @item The function @code{gpgme_check_version} must be called before any other function in the library, because it initializes the thread support subsystem in @acronym{GPGME}. To achieve this in multi-threaded programs, you must synchronize the memory with respect to other threads that also want to use @acronym{GPGME}. For this, it is sufficient to call @code{gpgme_check_version} before creating the other threads using @acronym{GPGME}@footnote{At least this is true for POSIX threads, as @code{pthread_create} is a function that synchronizes memory with respects to other threads. There are many functions which have this property, a complete list can be found in POSIX, IEEE Std 1003.1-2003, Base Definitions, Issue 6, in the definition of the term ``Memory Synchronization''. For other thread packages other, more relaxed or more strict rules may apply.}. @item Any @code{gpgme_data_t} and @code{gpgme_ctx_t} object must only be accessed by one thread at a time. If multiple threads want to deal with the same object, the caller has to make sure that operations on that object are fully synchronized. @item Only one thread at any time is allowed to call @code{gpgme_wait}. If multiple threads call this function, the caller must make sure that all invocations are fully synchronized. It is safe to start asynchronous operations while a thread is running in gpgme_wait. @item The function @code{gpgme_strerror} is not thread safe. You have to use @code{gpgme_strerror_r} instead. @end itemize @node Protocols and Engines @chapter Protocols and Engines @cindex protocol @cindex engine @cindex crypto engine @cindex backend @cindex crypto backend @acronym{GPGME} supports several cryptographic protocols, however, it does not implement them. Rather it uses backends (also called engines) which implement the protocol. @acronym{GPGME} uses inter-process communication to pass data back and forth between the application and the backend, but the details of the communication protocol and invocation of the backend is completely hidden by the interface. All complexity is handled by @acronym{GPGME}. Where an exchange of information between the application and the backend is necessary, @acronym{GPGME} provides the necessary callback function hooks and further interfaces. @deftp {Data type} {enum gpgme_protocol_t} @tindex gpgme_protocol_t The @code{gpgme_protocol_t} type specifies the set of possible protocol values that are supported by @acronym{GPGME}. The following protocols are supported: @table @code @item GPGME_PROTOCOL_OpenPGP @itemx GPGME_PROTOCOL_OPENPGP This specifies the OpenPGP protocol. @item GPGME_PROTOCOL_CMS This specifies the Cryptographic Message Syntax. @item GPGME_PROTOCOL_GPGCONF Under development. Please ask on @email{gnupg-devel@@gnupg.org} for help. @item GPGME_PROTOCOL_ASSUAN @since{1.2.0} This specifies the raw Assuan protocol. @item GPGME_PROTOCOL_G13 @since{1.3.0} Under development. Please ask on @email{gnupg-devel@@gnupg.org} for help. @item GPGME_PROTOCOL_UISERVER Under development. Please ask on @email{gnupg-devel@@gnupg.org} for help. @item GPGME_PROTOCOL_SPAWN @since{1.5.0} Special protocol for use with @code{gpgme_op_spawn}. @item GPGME_PROTOCOL_UNKNOWN Reserved for future extension. You may use this to indicate that the used protocol is not known to the application. Currently, @acronym{GPGME} does not accept this value in any operation, though, except for @code{gpgme_get_protocol_name}. @end table @end deftp @deftypefun {const char *} gpgme_get_protocol_name (@w{gpgme_protocol_t @var{protocol}}) The function @code{gpgme_get_protocol_name} returns a statically allocated string describing the protocol @var{protocol}, or @code{NULL} if the protocol number is not valid. @end deftypefun @menu * Engine Version Check:: Verifying the engine version. * Engine Information:: Obtaining more information about the engines. * Engine Configuration:: Changing the engine configuration. * OpenPGP:: Support for the OpenPGP protocol. * Cryptographic Message Syntax:: Support for the CMS. * Assuan:: Support for the raw Assuan protocol. @end menu @node Engine Version Check @section Engine Version Check @cindex version check, of the engines @deftypefun @w{const char *} gpgme_get_dirinfo (@w{cons char *@var{what}}) @since{1.5.0} The function @code{gpgme_get_dirinfo} returns a statically allocated string with the value associated to @var{what}. The returned values are the defaults and won't change even after @code{gpgme_set_engine_info} has been used to configure a different engine. @code{NULL} is returned if no value is available. Commonly supported values for @var{what} are: @table @code @item homedir Return the default home directory. @item sysconfdir Return the name of the system configuration directory @item bindir Return the name of the directory with GnuPG program files. @item libdir Return the name of the directory with GnuPG related library files. @item libexecdir Return the name of the directory with GnuPG helper program files. @item datadir Return the name of the directory with GnuPG shared data. @item localedir Return the name of the directory with GnuPG locale data. @item agent-socket Return the name of the socket to connect to the gpg-agent. @item agent-ssh-socket Return the name of the socket to connect to the ssh-agent component of gpg-agent. @item dirmngr-socket Return the name of the socket to connect to the dirmngr. @item uiserver-socket Return the name of the socket to connect to the user interface server. @item gpgconf-name Return the file name of the engine configuration tool. @item gpg-name Return the file name of the OpenPGP engine. @item gpgsm-name Return the file name of the CMS engine. @item g13-name Return the name of the file container encryption engine. @item gpg-wks-client-name Return the name of the Web Key Service tool. @end table @end deftypefun @deftypefun gpgme_error_t gpgme_engine_check_version (@w{gpgme_protocol_t @var{protocol}}) The function @code{gpgme_engine_check_version} verifies that the engine implementing the protocol @var{PROTOCOL} is installed in the expected path and meets the version requirement of @acronym{GPGME}. This function returns the error code @code{GPG_ERR_NO_ERROR} if the engine is available and @code{GPG_ERR_INV_ENGINE} if it is not. @end deftypefun @node Engine Information @section Engine Information @cindex engine, information about @deftp {Data type} {gpgme_engine_info_t} @tindex gpgme_protocol_t The @code{gpgme_engine_info_t} type specifies a pointer to a structure describing a crypto engine. The structure contains the following elements: @table @code @item gpgme_engine_info_t next This is a pointer to the next engine info structure in the linked list, or @code{NULL} if this is the last element. @item gpgme_protocol_t protocol This is the protocol for which the crypto engine is used. You can convert this to a string with @code{gpgme_get_protocol_name} for printing. @item const char *file_name This is a string holding the file name of the executable of the crypto engine. Currently, it is never @code{NULL}, but using @code{NULL} is reserved for future use, so always check before you use it. @item const char *home_dir This is a string holding the directory name of the crypto engine's configuration directory. If it is @code{NULL}, then the default directory is used. See @code{gpgme_get_dirinfo} on how to get the default directory. @item const char *version This is a string containing the version number of the crypto engine. It might be @code{NULL} if the version number can not be determined, for example because the executable doesn't exist or is invalid. @item const char *req_version This is a string containing the minimum required version number of the crypto engine for @acronym{GPGME} to work correctly. This is the version number that @code{gpgme_engine_check_version} verifies against. Currently, it is never @code{NULL}, but using @code{NULL} is reserved for future use, so always check before you use it. @end table @end deftp @deftypefun gpgme_error_t gpgme_get_engine_info (@w{gpgme_engine_info_t *@var{info}}) The function @code{gpgme_get_engine_info} returns a linked list of engine info structures in @var{info}. Each info structure describes the defaults of one configured backend. The memory for the info structures is allocated the first time this function is invoked, and must not be freed by the caller. This function returns the error code @code{GPG_ERR_NO_ERROR} if successful, and a system error if the memory could not be allocated. @end deftypefun Here is an example how you can provide more diagnostics if you receive an error message which indicates that the crypto engine is invalid. @example gpgme_ctx_t ctx; gpgme_error_t err; [...] if (gpgme_err_code (err) == GPG_ERR_INV_ENGINE) @{ gpgme_engine_info_t info; err = gpgme_get_engine_info (&info); if (!err) @{ while (info && info->protocol != gpgme_get_protocol (ctx)) info = info->next; if (!info) fprintf (stderr, "GPGME compiled without support for protocol %s", gpgme_get_protocol_name (info->protocol)); else if (info->file_name && !info->version) fprintf (stderr, "Engine %s not installed properly", info->file_name); else if (info->file_name && info->version && info->req_version) fprintf (stderr, "Engine %s version %s installed, " "but at least version %s required", info->file_name, info->version, info->req_version); else fprintf (stderr, "Unknown problem with engine for protocol %s", gpgme_get_protocol_name (info->protocol)); @} @} @end example @node Engine Configuration @section Engine Configuration @cindex engine, configuration of @cindex configuration of crypto backend You can change the configuration of a backend engine, and thus change the executable program and configuration directory to be used. You can make these changes the default or set them for some contexts individually. @deftypefun gpgme_error_t gpgme_set_engine_info (@w{gpgme_protocol_t @var{proto}}, @w{const char *@var{file_name}}, @w{const char *@var{home_dir}}) @since{1.1.0} The function @code{gpgme_set_engine_info} changes the default configuration of the crypto engine implementing the protocol @var{proto}. @var{file_name} is the file name of the executable program implementing this protocol, and @var{home_dir} is the directory name of the configuration directory for this crypto engine. If @var{home_dir} is @code{NULL}, the engine's default will be used. The new defaults are not applied to already created GPGME contexts. This function returns the error code @code{GPG_ERR_NO_ERROR} if successful, or an error code on failure. @end deftypefun The functions @code{gpgme_ctx_get_engine_info} and @code{gpgme_ctx_set_engine_info} can be used to change the engine configuration per context. @xref{Crypto Engine}. @node OpenPGP @section OpenPGP @cindex OpenPGP @cindex GnuPG @cindex protocol, GnuPG @cindex engine, GnuPG OpenPGP is implemented by GnuPG, the @acronym{GNU} Privacy Guard. This is the first protocol that was supported by @acronym{GPGME}. The OpenPGP protocol is specified by @code{GPGME_PROTOCOL_OpenPGP}. @node Cryptographic Message Syntax @section Cryptographic Message Syntax @cindex CMS @cindex cryptographic message syntax @cindex GpgSM @cindex protocol, CMS @cindex engine, GpgSM @cindex S/MIME @cindex protocol, S/MIME @acronym{CMS} is implemented by GpgSM, the S/MIME implementation for GnuPG. The @acronym{CMS} protocol is specified by @code{GPGME_PROTOCOL_CMS}. @node Assuan @section Assuan @cindex ASSUAN @cindex protocol, ASSUAN @cindex engine, ASSUAN Assuan is the RPC library used by the various @acronym{GnuPG} components. The Assuan protocol allows one to talk to arbitrary Assuan servers using @acronym{GPGME}. @xref{Using the Assuan protocol}. The ASSUAN protocol is specified by @code{GPGME_PROTOCOL_ASSUAN}. @node Algorithms @chapter Algorithms @cindex algorithms The crypto backends support a variety of algorithms used in public key cryptography.@footnote{Some engines also provide symmetric only encryption; see the description of the encryption function on how to use this.} The following sections list the identifiers used to denote such an algorithm. @menu * Public Key Algorithms:: A list of all public key algorithms. * Hash Algorithms:: A list of all hash algorithms. @end menu @node Public Key Algorithms @section Public Key Algorithms @cindex algorithms, public key @cindex public key algorithms Public key algorithms are used for encryption, decryption, signing and verification of signatures. @deftp {Data type} {enum gpgme_pubkey_algo_t} @tindex gpgme_pubkey_algo_t The @code{gpgme_pubkey_algo_t} type specifies the set of all public key algorithms that are supported by @acronym{GPGME}. Possible values are: @table @code @item GPGME_PK_RSA This value indicates the RSA (Rivest, Shamir, Adleman) algorithm. @item GPGME_PK_RSA_E Deprecated. This value indicates the RSA (Rivest, Shamir, Adleman) algorithm for encryption and decryption only. @item GPGME_PK_RSA_S Deprecated. This value indicates the RSA (Rivest, Shamir, Adleman) algorithm for signing and verification only. @item GPGME_PK_DSA This value indicates DSA, the Digital Signature Algorithm. @item GPGME_PK_ELG This value indicates ElGamal. @item GPGME_PK_ELG_E This value also indicates ElGamal and is used specifically in GnuPG. @item GPGME_PK_ECC @since{1.5.0} This value is a generic indicator for ellipic curve algorithms. @item GPGME_PK_ECDSA @since{1.3.0} This value indicates ECDSA, the Elliptic Curve Digital Signature Algorithm as defined by FIPS 186-2 and RFC-6637. @item GPGME_PK_ECDH @since{1.3.0} This value indicates ECDH, the Eliptic Curve Diffie-Hellmann encryption algorithm as defined by RFC-6637. @item GPGME_PK_EDDSA @since{1.7.0} This value indicates the EdDSA algorithm. @end table @end deftp @deftypefun {const char *} gpgme_pubkey_algo_name (@w{gpgme_pubkey_algo_t @var{algo}}) The function @code{gpgme_pubkey_algo_name} returns a pointer to a statically allocated string containing a description of the public key algorithm @var{algo}. This string can be used to output the name of the public key algorithm to the user. If @var{algo} is not a valid public key algorithm, @code{NULL} is returned. @end deftypefun @deftypefun {char *} gpgme_pubkey_algo_string (@w{gpgme_subkey_t @var{key}}) @since{1.7.0} The function @code{gpgme_pubkey_algo_string} is a convenience function to build and return an algorithm string in the same way GnuPG does (e.g. ``rsa2048'' or ``ed25519''). The caller must free the result using @code{gpgme_free}. On error (e.g. invalid argument or memory exhausted), the function returns NULL and sets @code{ERRNO}. @end deftypefun @node Hash Algorithms @section Hash Algorithms @cindex algorithms, hash @cindex algorithms, message digest @cindex hash algorithms @cindex message digest algorithms Hash (message digest) algorithms are used to compress a long message to make it suitable for public key cryptography. @deftp {Data type} {enum gpgme_hash_algo_t} @tindex gpgme_hash_algo_t The @code{gpgme_hash_algo_t} type specifies the set of all hash algorithms that are supported by @acronym{GPGME}. Possible values are: @table @code @item GPGME_MD_MD5 @item GPGME_MD_SHA1 @item GPGME_MD_RMD160 @item GPGME_MD_MD2 @item GPGME_MD_TIGER @item GPGME_MD_HAVAL @item GPGME_MD_SHA256 @item GPGME_MD_SHA384 @item GPGME_MD_SHA512 @item GPGME_MD_SHA224 @since{1.5.0} @item GPGME_MD_MD4 @item GPGME_MD_CRC32 @item GPGME_MD_CRC32_RFC1510 @item GPGME_MD_CRC24_RFC2440 @end table @end deftp @deftypefun {const char *} gpgme_hash_algo_name (@w{gpgme_hash_algo_t @var{algo}}) The function @code{gpgme_hash_algo_name} returns a pointer to a statically allocated string containing a description of the hash algorithm @var{algo}. This string can be used to output the name of the hash algorithm to the user. If @var{algo} is not a valid hash algorithm, @code{NULL} is returned. @end deftypefun @node Error Handling @chapter Error Handling @cindex error handling Many functions in @acronym{GPGME} can return an error if they fail. For this reason, the application should always catch the error condition and take appropriate measures, for example by releasing the resources and passing the error up to the caller, or by displaying a descriptive message to the user and cancelling the operation. Some error values do not indicate a system error or an error in the operation, but the result of an operation that failed properly. For example, if you try to decrypt a tempered message, the decryption will fail. Another error value actually means that the end of a data buffer or list has been reached. The following descriptions explain for many error codes what they mean usually. Some error values have specific meanings if returned by a certain functions. Such cases are described in the documentation of those functions. @acronym{GPGME} uses the @code{libgpg-error} library. This allows to share the error codes with other components of the GnuPG system, and thus pass error values transparently from the crypto engine, or some helper application of the crypto engine, to the user. This way no information is lost. As a consequence, @acronym{GPGME} does not use its own identifiers for error codes, but uses those provided by @code{libgpg-error}. They usually start with @code{GPG_ERR_}. However, @acronym{GPGME} does provide aliases for the functions defined in libgpg-error, which might be preferred for name space consistency. @menu * Error Values:: The error value and what it means. * Error Sources:: A list of important error sources. * Error Codes:: A list of important error codes. * Error Strings:: How to get a descriptive string from a value. @end menu @node Error Values @section Error Values @cindex error values @cindex error codes @cindex error sources @deftp {Data type} {gpgme_err_code_t} The @code{gpgme_err_code_t} type is an alias for the @code{libgpg-error} type @code{gpg_err_code_t}. The error code indicates the type of an error, or the reason why an operation failed. A list of important error codes can be found in the next section. @end deftp @deftp {Data type} {gpgme_err_source_t} The @code{gpgme_err_source_t} type is an alias for the @code{libgpg-error} type @code{gpg_err_source_t}. The error source has not a precisely defined meaning. Sometimes it is the place where the error happened, sometimes it is the place where an error was encoded into an error value. Usually the error source will give an indication to where to look for the problem. This is not always true, but it is attempted to achieve this goal. A list of important error sources can be found in the next section. @end deftp @deftp {Data type} {gpgme_error_t} The @code{gpgme_error_t} type is an alias for the @code{libgpg-error} type @code{gpg_error_t}. An error value like this has always two components, an error code and an error source. Both together form the error value. Thus, the error value can not be directly compared against an error code, but the accessor functions described below must be used. However, it is guaranteed that only 0 is used to indicate success (@code{GPG_ERR_NO_ERROR}), and that in this case all other parts of the error value are set to 0, too. Note that in @acronym{GPGME}, the error source is used purely for diagnostical purposes. Only the error code should be checked to test for a certain outcome of a function. The manual only documents the error code part of an error value. The error source is left unspecified and might be anything. @end deftp @deftypefun {static inline gpgme_err_code_t} gpgme_err_code (@w{gpgme_error_t @var{err}}) The static inline function @code{gpgme_err_code} returns the @code{gpgme_err_code_t} component of the error value @var{err}. This function must be used to extract the error code from an error value in order to compare it with the @code{GPG_ERR_*} error code macros. @end deftypefun @deftypefun {static inline gpgme_err_source_t} gpgme_err_source (@w{gpgme_error_t @var{err}}) The static inline function @code{gpgme_err_source} returns the @code{gpgme_err_source_t} component of the error value @var{err}. This function must be used to extract the error source from an error value in order to compare it with the @code{GPG_ERR_SOURCE_*} error source macros. @end deftypefun @deftypefun {static inline gpgme_error_t} gpgme_err_make (@w{gpgme_err_source_t @var{source}}, @w{gpgme_err_code_t @var{code}}) The static inline function @code{gpgme_err_make} returns the error value consisting of the error source @var{source} and the error code @var{code}. This function can be used in callback functions to construct an error value to return it to the library. @end deftypefun @deftypefun {static inline gpgme_error_t} gpgme_error (@w{gpgme_err_code_t @var{code}}) The static inline function @code{gpgme_error} returns the error value consisting of the default error source and the error code @var{code}. For @acronym{GPGME} applications, the default error source is @code{GPG_ERR_SOURCE_USER_1}. You can define @code{GPGME_ERR_SOURCE_DEFAULT} before including @file{gpgme.h} to change this default. This function can be used in callback functions to construct an error value to return it to the library. @end deftypefun The @code{libgpg-error} library provides error codes for all system error numbers it knows about. If @var{err} is an unknown error number, the error code @code{GPG_ERR_UNKNOWN_ERRNO} is used. The following functions can be used to construct error values from system errnor numbers. @deftypefun {gpgme_error_t} gpgme_err_make_from_errno (@w{gpgme_err_source_t @var{source}}, @w{int @var{err}}) The function @code{gpgme_err_make_from_errno} is like @code{gpgme_err_make}, but it takes a system error like @code{errno} instead of a @code{gpgme_err_code_t} error code. @end deftypefun @deftypefun {gpgme_error_t} gpgme_error_from_errno (@w{int @var{err}}) The function @code{gpgme_error_from_errno} is like @code{gpgme_error}, but it takes a system error like @code{errno} instead of a @code{gpgme_err_code_t} error code. @end deftypefun Sometimes you might want to map system error numbers to error codes directly, or map an error code representing a system error back to the system error number. The following functions can be used to do that. @deftypefun {gpgme_err_code_t} gpgme_err_code_from_errno (@w{int @var{err}}) The function @code{gpgme_err_code_from_errno} returns the error code for the system error @var{err}. If @var{err} is not a known system error, the function returns @code{GPG_ERR_UNKNOWN_ERRNO}. @end deftypefun @deftypefun {int} gpgme_err_code_to_errno (@w{gpgme_err_code_t @var{err}}) The function @code{gpgme_err_code_to_errno} returns the system error for the error code @var{err}. If @var{err} is not an error code representing a system error, or if this system error is not defined on this system, the function returns @code{0}. @end deftypefun @node Error Sources @section Error Sources @cindex error codes, list of The library @code{libgpg-error} defines an error source for every component of the GnuPG system. The error source part of an error value is not well defined. As such it is mainly useful to improve the diagnostic error message for the user. If the error code part of an error value is @code{0}, the whole error value will be @code{0}. In this case the error source part is of course @code{GPG_ERR_SOURCE_UNKNOWN}. The list of error sources that might occur in applications using @acronym{GPGME} is: @table @code @item GPG_ERR_SOURCE_UNKNOWN The error source is not known. The value of this error source is @code{0}. @item GPG_ERR_SOURCE_GPGME The error source is @acronym{GPGME} itself. This is the default for errors that occur in the @acronym{GPGME} library. @item GPG_ERR_SOURCE_GPG The error source is GnuPG, which is the crypto engine used for the OpenPGP protocol. @item GPG_ERR_SOURCE_GPGSM The error source is GPGSM, which is the crypto engine used for the CMS protocol. @item GPG_ERR_SOURCE_GCRYPT The error source is @code{libgcrypt}, which is used by crypto engines to perform cryptographic operations. @item GPG_ERR_SOURCE_GPGAGENT The error source is @command{gpg-agent}, which is used by crypto engines to perform operations with the secret key. @item GPG_ERR_SOURCE_PINENTRY The error source is @command{pinentry}, which is used by @command{gpg-agent} to query the passphrase to unlock a secret key. @item GPG_ERR_SOURCE_SCD The error source is the SmartCard Daemon, which is used by @command{gpg-agent} to delegate operations with the secret key to a SmartCard. @item GPG_ERR_SOURCE_KEYBOX The error source is @code{libkbx}, a library used by the crypto engines to manage local keyrings. @item GPG_ERR_SOURCE_USER_1 @item GPG_ERR_SOURCE_USER_2 @item GPG_ERR_SOURCE_USER_3 @item GPG_ERR_SOURCE_USER_4 These error sources are not used by any GnuPG component and can be used by other software. For example, applications using @acronym{GPGME} can use them to mark error values coming from callback handlers. Thus @code{GPG_ERR_SOURCE_USER_1} is the default for errors created with @code{gpgme_error} and @code{gpgme_error_from_errno}, unless you define @code{GPGME_ERR_SOURCE_DEFAULT} before including @file{gpgme.h}. @end table @node Error Codes @section Error Codes @cindex error codes, list of The library @code{libgpg-error} defines many error values. Most of them are not used by @code{GPGME} directly, but might be returned by @acronym{GPGME} because it received them from the crypto engine. The below list only includes such error codes that have a specific meaning in @code{GPGME}, or which are so common that you should know about them. @table @code @item GPG_ERR_EOF This value indicates the end of a list, buffer or file. @item GPG_ERR_NO_ERROR This value indicates success. The value of this error code is @code{0}. Also, it is guaranteed that an error value made from the error code @code{0} will be @code{0} itself (as a whole). This means that the error source information is lost for this error code, however, as this error code indicates that no error occurred, this is generally not a problem. @item GPG_ERR_GENERAL This value means that something went wrong, but either there is not enough information about the problem to return a more useful error value, or there is no separate error value for this type of problem. @item GPG_ERR_ENOMEM This value means that an out-of-memory condition occurred. @item GPG_ERR_E... System errors are mapped to GPG_ERR_FOO where FOO is the symbol for the system error. @item GPG_ERR_INV_VALUE This value means that some user provided data was out of range. This can also refer to objects. For example, if an empty @code{gpgme_data_t} object was expected, but one containing data was provided, this error value is returned. @item GPG_ERR_UNUSABLE_PUBKEY This value means that some recipients for a message were invalid. @item GPG_ERR_UNUSABLE_SECKEY This value means that some signers were invalid. @item GPG_ERR_NO_DATA This value means that a @code{gpgme_data_t} object which was expected to have content was found empty. @item GPG_ERR_CONFLICT This value means that a conflict of some sort occurred. @item GPG_ERR_NOT_IMPLEMENTED This value indicates that the specific function (or operation) is not implemented. This error should never happen. It can only occur if you use certain values or configuration options which do not work, but for which we think that they should work at some later time. @item GPG_ERR_DECRYPT_FAILED This value indicates that a decryption operation was unsuccessful. @item GPG_ERR_BAD_PASSPHRASE This value means that the user did not provide a correct passphrase when requested. @item GPG_ERR_CANCELED This value means that the operation was canceled. @item GPG_ERR_INV_ENGINE This value means that the engine that implements the desired protocol is currently not available. This can either be because the sources were configured to exclude support for this engine, or because the engine is not installed properly. @item GPG_ERR_AMBIGUOUS_NAME This value indicates that a user ID or other specifier did not specify a unique key. @item GPG_ERR_WRONG_KEY_USAGE This value indicates that a key is not used appropriately. @item GPG_ERR_CERT_REVOKED This value indicates that a key signature was revoced. @item GPG_ERR_CERT_EXPIRED This value indicates that a key signature expired. @item GPG_ERR_NO_CRL_KNOWN This value indicates that no certificate revocation list is known for the certificate. @item GPG_ERR_NO_POLICY_MATCH This value indicates that a policy issue occurred. @item GPG_ERR_NO_SECKEY This value indicates that no secret key for the user ID is available. @item GPG_ERR_MISSING_CERT This value indicates that a key could not be imported because the issuer certificate is missing. @item GPG_ERR_BAD_CERT_CHAIN This value indicates that a key could not be imported because its certificate chain is not good, for example it could be too long. @item GPG_ERR_UNSUPPORTED_ALGORITHM This value means a verification failed because the cryptographic algorithm is not supported by the crypto backend. @item GPG_ERR_BAD_SIGNATURE This value means a verification failed because the signature is bad. @item GPG_ERR_NO_PUBKEY This value means a verification failed because the public key is not available. @item GPG_ERR_USER_1 @item GPG_ERR_USER_2 @item ... @item GPG_ERR_USER_16 These error codes are not used by any GnuPG component and can be freely used by other software. Applications using @acronym{GPGME} might use them to mark specific errors returned by callback handlers if no suitable error codes (including the system errors) for these errors exist already. @end table @node Error Strings @section Error Strings @cindex error values, printing of @cindex error codes, printing of @cindex error sources, printing of @cindex error strings @deftypefun {const char *} gpgme_strerror (@w{gpgme_error_t @var{err}}) The function @code{gpgme_strerror} returns a pointer to a statically allocated string containing a description of the error code contained in the error value @var{err}. This string can be used to output a diagnostic message to the user. This function is not thread safe. Use @code{gpgme_strerror_r} in multi-threaded programs. @end deftypefun @deftypefun {int} gpgme_strerror_r (@w{gpgme_error_t @var{err}}, @w{char *@var{buf}}, @w{size_t @var{buflen}}) The function @code{gpgme_strerror_r} returns the error string for @var{err} in the user-supplied buffer @var{buf} of size @var{buflen}. This function is, in contrast to @code{gpgme_strerror}, thread-safe if a thread-safe @code{strerror_r} function is provided by the system. If the function succeeds, 0 is returned and @var{buf} contains the string describing the error. If the buffer was not large enough, ERANGE is returned and @var{buf} contains as much of the beginning of the error string as fits into the buffer. @end deftypefun @deftypefun {const char *} gpgme_strsource (@w{gpgme_error_t @var{err}}) The function @code{gpgme_strerror} returns a pointer to a statically allocated string containing a description of the error source contained in the error value @var{err}. This string can be used to output a diagnostic message to the user. @end deftypefun The following example illustrates the use of @code{gpgme_strerror}: @example gpgme_ctx_t ctx; gpgme_error_t err = gpgme_new (&ctx); if (err) @{ fprintf (stderr, "%s: creating GpgME context failed: %s: %s\n", argv[0], gpgme_strsource (err), gpgme_strerror (err)); exit (1); @} @end example @node Exchanging Data @chapter Exchanging Data @cindex data, exchanging A lot of data has to be exchanged between the user and the crypto engine, like plaintext messages, ciphertext, signatures and information about the keys. The technical details about exchanging the data information are completely abstracted by @acronym{GPGME}. The user provides and receives the data via @code{gpgme_data_t} objects, regardless of the communication protocol between @acronym{GPGME} and the crypto engine in use. @deftp {Data type} {gpgme_data_t} The @code{gpgme_data_t} type is a handle for a container for generic data, which is used by @acronym{GPGME} to exchange data with the user. @end deftp @code{gpgme_data_t} objects do not provide notifications on events. It is assumed that read and write operations are blocking until data is available. If this is undesirable, the application must ensure that all GPGME data operations always have data available, for example by using memory buffers or files rather than pipes or sockets. This might be relevant, for example, if the external event loop mechanism is used. @deftp {Data type} {gpgme_off_t} @since{1.4.1} On POSIX platforms the @code{gpgme_off_t} type is an alias for @code{off_t}; it may be used interchangeable. On Windows platforms @code{gpgme_off_t} is defined as a long (i.e. 32 bit) for 32 bit Windows and as a 64 bit signed integer for 64 bit Windows. @end deftp @deftp {Data type} {gpgme_ssize_t} The @code{gpgme_ssize_t} type is an alias for @code{ssize_t}. It has only been introduced to overcome portability problems pertaining to the declaration of @code{ssize_t} by different toolchains. @end deftp @menu * Creating Data Buffers:: Creating new data buffers. * Destroying Data Buffers:: Releasing data buffers. * Manipulating Data Buffers:: Operations on data buffers. @end menu @node Creating Data Buffers @section Creating Data Buffers @cindex data buffer, creation Data objects can be based on memory, files, or callback functions provided by the user. Not all operations are supported by all objects. @menu * Memory Based Data Buffers:: Creating memory based data buffers. * File Based Data Buffers:: Creating file based data buffers. * Callback Based Data Buffers:: Creating callback based data buffers. @end menu @node Memory Based Data Buffers @subsection Memory Based Data Buffers Memory based data objects store all data in allocated memory. This is convenient, but only practical for an amount of data that is a fraction of the available physical memory. The data has to be copied from its source and to its destination, which can often be avoided by using one of the other data object @deftypefun gpgme_error_t gpgme_data_new (@w{gpgme_data_t *@var{dh}}) The function @code{gpgme_data_new} creates a new @code{gpgme_data_t} object and returns a handle for it in @var{dh}. The data object is memory based and initially empty. The function returns the error code @code{GPG_ERR_NO_ERROR} if the data object was successfully created, @code{GPG_ERR_INV_VALUE} if @var{dh} is not a valid pointer, and @code{GPG_ERR_ENOMEM} if not enough memory is available. @end deftypefun @deftypefun gpgme_error_t gpgme_data_new_from_mem (@w{gpgme_data_t *@var{dh}}, @w{const char *@var{buffer}}, @w{size_t @var{size}}, @w{int @var{copy}}) The function @code{gpgme_data_new_from_mem} creates a new @code{gpgme_data_t} object and fills it with @var{size} bytes starting from @var{buffer}. If @var{copy} is not zero, a private copy of the data is made. If @var{copy} is zero, the data is taken from the specified buffer as needed, and the user has to ensure that the buffer remains valid for the whole life span of the data object. The function returns the error code @code{GPG_ERR_NO_ERROR} if the data object was successfully created, @code{GPG_ERR_INV_VALUE} if @var{dh} or @var{buffer} is not a valid pointer, and @code{GPG_ERR_ENOMEM} if not enough memory is available. @end deftypefun @deftypefun gpgme_error_t gpgme_data_new_from_file (@w{gpgme_data_t *@var{dh}}, @w{const char *@var{filename}}, @w{int @var{copy}}) The function @code{gpgme_data_new_from_file} creates a new @code{gpgme_data_t} object and fills it with the content of the file @var{filename}. If @var{copy} is not zero, the whole file is read in at initialization time and the file is not used anymore after that. This is the only mode supported currently. Later, a value of zero for @var{copy} might cause all reads to be delayed until the data is needed, but this is not yet implemented. The function returns the error code @code{GPG_ERR_NO_ERROR} if the data object was successfully created, @code{GPG_ERR_INV_VALUE} if @var{dh} or @var{filename} is not a valid pointer, @code{GPG_ERR_NOT_IMPLEMENTED} if @var{code} is zero, and @code{GPG_ERR_ENOMEM} if not enough memory is available. @end deftypefun @deftypefun gpgme_error_t gpgme_data_new_from_filepart (@w{gpgme_data_t *@var{dh}}, @w{const char *@var{filename}}, @w{FILE *@var{fp}}, @w{off_t @var{offset}}, @w{size_t @var{length}}) The function @code{gpgme_data_new_from_filepart} creates a new @code{gpgme_data_t} object and fills it with a part of the file specified by @var{filename} or @var{fp}. Exactly one of @var{filename} and @var{fp} must be non-zero, the other must be zero. The argument that is not zero specifies the file from which @var{length} bytes are read into the data object, starting from @var{offset}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the data object was successfully created, @code{GPG_ERR_INV_VALUE} if @var{dh} and exactly one of @var{filename} and @var{fp} is not a valid pointer, and @code{GPG_ERR_ENOMEM} if not enough memory is available. @end deftypefun @node File Based Data Buffers @subsection File Based Data Buffers File based data objects operate directly on file descriptors or streams. Only a small amount of data is stored in core at any time, so the size of the data objects is not limited by @acronym{GPGME}. @deftypefun gpgme_error_t gpgme_data_new_from_fd (@w{gpgme_data_t *@var{dh}}, @w{int @var{fd}}) The function @code{gpgme_data_new_from_fd} creates a new @code{gpgme_data_t} object and uses the file descriptor @var{fd} to read from (if used as an input data object) and write to (if used as an output data object). When using the data object as an input buffer, the function might read a bit more from the file descriptor than is actually needed by the crypto engine in the desired operation because of internal buffering. Note that GPGME assumes that the file descriptor is set to blocking mode. Errors during I/O operations, except for EINTR, are usually fatal for crypto operations. The function returns the error code @code{GPG_ERR_NO_ERROR} if the data object was successfully created, and @code{GPG_ERR_ENOMEM} if not enough memory is available. @end deftypefun @deftypefun gpgme_error_t gpgme_data_new_from_stream (@w{gpgme_data_t *@var{dh}}, @w{FILE *@var{stream}}) The function @code{gpgme_data_new_from_stream} creates a new @code{gpgme_data_t} object and uses the I/O stream @var{stream} to read from (if used as an input data object) and write to (if used as an output data object). When using the data object as an input buffer, the function might read a bit more from the stream than is actually needed by the crypto engine in the desired operation because of internal buffering. Note that GPGME assumes that the stream is in blocking mode. Errors during I/O operations, except for EINTR, are usually fatal for crypto operations. The function returns the error code @code{GPG_ERR_NO_ERROR} if the data object was successfully created, and @code{GPG_ERR_ENOMEM} if not enough memory is available. @end deftypefun @node Callback Based Data Buffers @subsection Callback Based Data Buffers If neither memory nor file based data objects are a good fit for your application, you can implement the functions a data object provides yourself and create a data object from these callback functions. @deftp {Data type} {ssize_t (*gpgme_data_read_cb_t) (@w{void *@var{handle}}, @w{void @var{*buffer}}, @w{size_t @var{size}})} @tindex gpgme_data_read_cb_t The @code{gpgme_data_read_cb_t} type is the type of functions which @acronym{GPGME} calls if it wants to read data from a user-implemented data object. The function should read up to @var{size} bytes from the current read position into the space starting at @var{buffer}. The @var{handle} is provided by the user at data object creation time. Note that GPGME assumes that the read blocks until data is available. Errors during I/O operations, except for EINTR, are usually fatal for crypto operations. The function should return the number of bytes read, 0 on EOF, and -1 on error. If an error occurs, @var{errno} should be set to describe the type of the error. @end deftp @deftp {Data type} {ssize_t (*gpgme_data_write_cb_t) (@w{void *@var{handle}}, @w{const void @var{*buffer}}, @w{size_t @var{size}})} @tindex gpgme_data_write_cb_t The @code{gpgme_data_write_cb_t} type is the type of functions which @acronym{GPGME} calls if it wants to write data to a user-implemented data object. The function should write up to @var{size} bytes to the current write position from the space starting at @var{buffer}. The @var{handle} is provided by the user at data object creation time. Note that GPGME assumes that the write blocks until data is available. Errors during I/O operations, except for EINTR, are usually fatal for crypto operations. The function should return the number of bytes written, and -1 on error. If an error occurs, @var{errno} should be set to describe the type of the error. @end deftp @deftp {Data type} {off_t (*gpgme_data_seek_cb_t) (@w{void *@var{handle}}, @w{off_t @var{offset}}, @w{int @var{whence}})} @tindex gpgme_data_seek_cb_t The @code{gpgme_data_seek_cb_t} type is the type of functions which @acronym{GPGME} calls if it wants to change the current read/write position in a user-implemented data object, just like the @code{lseek} function. The function should return the new read/write position, and -1 on error. If an error occurs, @var{errno} should be set to describe the type of the error. @end deftp @deftp {Data type} {void (*gpgme_data_release_cb_t) (@w{void *@var{handle}})} @tindex gpgme_data_release_cb_t The @code{gpgme_data_release_cb_t} type is the type of functions which @acronym{GPGME} calls if it wants to destroy a user-implemented data object. The @var{handle} is provided by the user at data object creation time. @end deftp @deftp {Data type} {struct gpgme_data_cbs} This structure is used to store the data callback interface functions described above. It has the following members: @table @code @item gpgme_data_read_cb_t read This is the function called by @acronym{GPGME} to read data from the data object. It is only required for input data object. @item gpgme_data_write_cb_t write This is the function called by @acronym{GPGME} to write data to the data object. It is only required for output data object. @item gpgme_data_seek_cb_t seek This is the function called by @acronym{GPGME} to change the current read/write pointer in the data object (if available). It is optional. @item gpgme_data_release_cb_t release This is the function called by @acronym{GPGME} to release a data object. It is optional. @end table @end deftp @deftypefun gpgme_error_t gpgme_data_new_from_cbs (@w{gpgme_data_t *@var{dh}}, @w{gpgme_data_cbs_t @var{cbs}}, @w{void *@var{handle}}) The function @code{gpgme_data_new_from_cbs} creates a new @code{gpgme_data_t} object and uses the user-provided callback functions to operate on the data object. The handle @var{handle} is passed as first argument to the callback functions. This can be used to identify this data object. The function returns the error code @code{GPG_ERR_NO_ERROR} if the data object was successfully created, and @code{GPG_ERR_ENOMEM} if not enough memory is available. @end deftypefun @node Destroying Data Buffers @section Destroying Data Buffers @cindex data buffer, destruction @deftypefun void gpgme_data_release (@w{gpgme_data_t @var{dh}}) The function @code{gpgme_data_release} destroys the data object with the handle @var{dh}. It releases all associated resources that were not provided by the user in the first place. @end deftypefun @deftypefun {char *} gpgme_data_release_and_get_mem (@w{gpgme_data_t @var{dh}}, @w{size_t *@var{length}}) The function @code{gpgme_data_release_and_get_mem} is like @code{gpgme_data_release}, except that it returns the data buffer and its length that was provided by the object. The user has to release the buffer with @code{gpgme_free}. In case the user provided the data buffer in non-copy mode, a copy will be made for this purpose. In case an error returns, or there is no suitable data buffer that can be returned to the user, the function will return @code{NULL}. In any case, the data object @var{dh} is destroyed. @end deftypefun @deftypefun void gpgme_free (@w{void *@var{buffer}}) @since{1.1.1} The function @code{gpgme_free} releases the memory returned by @code{gpgme_data_release_and_get_mem} and @code{gpgme_pubkey_algo_string}. It should be used instead of the system libraries @code{free} function in case different allocators are used by a program. This is often the case if gpgme is used under Windows as a DLL. @end deftypefun @node Manipulating Data Buffers @section Manipulating Data Buffers @cindex data buffer, manipulation Data buffers contain data and meta-data. The following operations can be used to manipulate both. @menu * Data Buffer I/O Operations:: I/O operations on data buffers. * Data Buffer Meta-Data:: Meta-data manipulation of data buffers. * Data Buffer Convenience:: Convenience function for data buffers. @end menu @node Data Buffer I/O Operations @subsection Data Buffer I/O Operations @cindex data buffer, I/O operations @cindex data buffer, read @cindex data buffer, write @cindex data buffer, seek @deftypefun ssize_t gpgme_data_read (@w{gpgme_data_t @var{dh}}, @w{void *@var{buffer}}, @w{size_t @var{length}}) The function @code{gpgme_data_read} reads up to @var{length} bytes from the data object with the handle @var{dh} into the space starting at @var{buffer}. If no error occurs, the actual amount read is returned. If the end of the data object is reached, the function returns 0. In all other cases, the function returns -1 and sets @var{errno}. @end deftypefun @deftypefun ssize_t gpgme_data_write (@w{gpgme_data_t @var{dh}}, @w{const void *@var{buffer}}, @w{size_t @var{size}}) The function @code{gpgme_data_write} writes up to @var{size} bytes starting from @var{buffer} into the data object with the handle @var{dh} at the current write position. The function returns the number of bytes actually written, or -1 if an error occurs. If an error occurs, @var{errno} is set. @end deftypefun @deftypefun off_t gpgme_data_seek (@w{gpgme_data_t @var{dh}}, @w{off_t @var{offset}}, @w{int @var{whence}}) The function @code{gpgme_data_seek} changes the current read/write position. The @var{whence} argument specifies how the @var{offset} should be interpreted. It must be one of the following symbolic constants: @table @code @item SEEK_SET Specifies that @var{offset} is a count of characters from the beginning of the data object. @item SEEK_CUR Specifies that @var{offset} is a count of characters from the current file position. This count may be positive or negative. @item SEEK_END Specifies that @var{offset} is a count of characters from the end of the data object. A negative count specifies a position within the current extent of the data object; a positive count specifies a position past the current end. If you set the position past the current end, and actually write data, you will extend the data object with zeros up to that position. @end table If successful, the function returns the resulting file position, measured in bytes from the beginning of the data object. You can use this feature together with @code{SEEK_CUR} to read the current read/write position. If the function fails, -1 is returned and @var{errno} is set. @end deftypefun @node Data Buffer Meta-Data @subsection Data Buffer Meta-Data @cindex data buffer, meta-data @cindex data buffer, file name @cindex data buffer, encoding @deftypefun {char *} gpgme_data_get_file_name (@w{gpgme_data_t @var{dh}}) @since{1.1.0} The function @code{gpgme_data_get_file_name} returns a pointer to a string containing the file name associated with the data object. The file name will be stored in the output when encrypting or signing the data and will be returned to the user when decrypting or verifying the output data. If no error occurs, the string containing the file name is returned. Otherwise, @code{NULL} will be returned. @end deftypefun @deftypefun gpgme_error_t gpgme_data_set_file_name (@w{gpgme_data_t @var{dh}}, @w{const char *@var{file_name}}) @since{1.1.0} The function @code{gpgme_data_set_file_name} sets the file name associated with the data object. The file name will be stored in the output when encrypting or signing the data and will be returned to the user when decrypting or verifying the output data. The function returns the error code @code{GPG_ERR_INV_VALUE} if @var{dh} is not a valid pointer and @code{GPG_ERR_ENOMEM} if not enough memory is available. @end deftypefun @deftp {Data type} {enum gpgme_data_encoding_t} @tindex gpgme_data_encoding_t The @code{gpgme_data_encoding_t} type specifies the encoding of a @code{gpgme_data_t} object. For input data objects, the encoding is useful to give the backend a hint on the type of data. For output data objects, the encoding can specify the output data format on certain operations. Please note that not all backends support all encodings on all operations. The following data types are available: @table @code @item GPGME_DATA_ENCODING_NONE This specifies that the encoding is not known. This is the default for a new data object. The backend will try its best to detect the encoding automatically. @item GPGME_DATA_ENCODING_BINARY This specifies that the data is encoding in binary form; i.e. there is no special encoding. @item GPGME_DATA_ENCODING_BASE64 This specifies that the data is encoded using the Base-64 encoding scheme as used by @acronym{MIME} and other protocols. @item GPGME_DATA_ENCODING_ARMOR This specifies that the data is encoded in an armored form as used by OpenPGP and PEM. @item GPGME_DATA_ENCODING_MIME @since{1.7.0} This specifies that the data is encoded as a MIME part. @item GPGME_DATA_ENCODING_URL @since{1.2.0} The data is a list of linefeed delimited URLs. This is only useful with @code{gpgme_op_import}. @item GPGME_DATA_ENCODING_URL0 @since{1.2.0} The data is a list of binary zero delimited URLs. This is only useful with @code{gpgme_op_import}. @item GPGME_DATA_ENCODING_URLESC @since{1.2.0} The data is a list of linefeed delimited URLs with all control and space characters percent escaped. This mode is is not yet implemented. @end table @end deftp @deftypefun gpgme_data_encoding_t gpgme_data_get_encoding (@w{gpgme_data_t @var{dh}}) The function @code{gpgme_data_get_encoding} returns the encoding of the data object with the handle @var{dh}. If @var{dh} is not a valid pointer (e.g. @code{NULL}) @code{GPGME_DATA_ENCODING_NONE} is returned. @end deftypefun @deftypefun gpgme_error_t gpgme_data_set_encoding (@w{gpgme_data_t @var{dh}, gpgme_data_encoding_t @var{enc}}) The function @code{gpgme_data_set_encoding} changes the encoding of the data object with the handle @var{dh} to @var{enc}. @end deftypefun @deftypefun {gpgme_error_t} gpgme_data_set_flag @ (@w{gpgme_data_t @var{dh}}, @ @w{const char *@var{name}}, @ @w{const char *@var{value}}) @since{1.7.0} Some minor properties of the data object can be controlled with flags set by this function. The properties are identified by the following values for @var{name}: @table @code @item size-hint The value is a decimal number with the length gpgme shall assume for this data object. This is useful if the data is provided by callbacks or via file descriptors but the applications knows the total size of the data. If this is set the OpenPGP engine may use this to decide on buffer allocation strategies and to provide a total value for its progress information. @end table This function returns @code{0} on success. @end deftypefun @node Data Buffer Convenience @subsection Data Buffer Convenience Functions @cindex data buffer, convenience @cindex type of data @cindex identify @deftp {Data type} {enum gpgme_data_type_t} @tindex gpgme_data_type_t @since{1.4.3} The @code{gpgme_data_type_t} type is used to return the detected type of the content of a data buffer. @end deftp @table @code @item GPGME_DATA_TYPE_INVALID This is returned by @code{gpgme_data_identify} if it was not possible to identify the data. Reasons for this might be a non-seekable stream or a memory problem. The value is 0. @item GPGME_DATA_TYPE_UNKNOWN The type of the data is not known. @item GPGME_DATA_TYPE_PGP_SIGNED The data is an OpenPGP signed message. This may be a binary signature, a detached one or a cleartext signature. @item GPGME_DATA_TYPE_PGP_ENCRYPTED @since{1.7.0} The data is an OpenPGP encrypted message. @item GPGME_DATA_TYPE_PGP_SIGNATURE @since{1.7.0} The data is an OpenPGP detached signature. @item GPGME_DATA_TYPE_PGP_OTHER This is a generic OpenPGP message. In most cases this will be encrypted data. @item GPGME_DATA_TYPE_PGP_KEY This is an OpenPGP key (private or public). @item GPGME_DATA_TYPE_CMS_SIGNED This is a CMS signed message. @item GPGME_DATA_TYPE_CMS_ENCRYPTED This is a CMS encrypted (enveloped data) message. @item GPGME_DATA_TYPE_CMS_OTHER This is used for other CMS message types. @item GPGME_DATA_TYPE_X509_CERT The data is a X.509 certificate @item GPGME_DATA_TYPE_PKCS12 The data is a PKCS#12 message. This is commonly used to exchange private keys for X.509. @end table @deftypefun gpgme_data_type_t gpgme_data_identify (@w{gpgme_data_t @var{dh}}) @since{1.4.3} The function @code{gpgme_data_identify} returns the type of the data with the handle @var{dh}. If it is not possible to perform the identification, the function returns zero (@code{GPGME_DATA_TYPE_INVALID}). Note that depending on how the data object has been created the identification may not be possible or the data object may change its internal state (file pointer moved). For file or memory based data object, the state should not change. @end deftypefun @c @c Chapter Contexts @c @node Contexts @chapter Contexts @cindex context All cryptographic operations in @acronym{GPGME} are performed within a context, which contains the internal state of the operation as well as configuration parameters. By using several contexts you can run several cryptographic operations in parallel, with different configuration. @deftp {Data type} {gpgme_ctx_t} The @code{gpgme_ctx_t} type is a handle for a @acronym{GPGME} context, which is used to hold the configuration, status and result of cryptographic operations. @end deftp @menu * Creating Contexts:: Creating new @acronym{GPGME} contexts. * Destroying Contexts:: Releasing @acronym{GPGME} contexts. * Result Management:: Managing the result of crypto operations. * Context Attributes:: Setting properties of a context. * Key Management:: Managing keys with @acronym{GPGME}. * Trust Item Management:: Managing trust items with @acronym{GPGME}. * Crypto Operations:: Using a context for cryptography. * Miscellaneous:: Miscellaneous operations * Run Control:: Controlling how operations are run. @end menu @node Creating Contexts @section Creating Contexts @cindex context, creation @deftypefun gpgme_error_t gpgme_new (@w{gpgme_ctx_t *@var{ctx}}) The function @code{gpgme_new} creates a new @code{gpgme_ctx_t} object and returns a handle for it in @var{ctx}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the context was successfully created, @code{GPG_ERR_INV_VALUE} if @var{ctx} is not a valid pointer, and @code{GPG_ERR_ENOMEM} if not enough memory is available. Also, it returns @code{GPG_ERR_NOT_OPERATIONAL} if @code{gpgme_check_version} was not called to initialize GPGME, and @code{GPG_ERR_SELFTEST_FAILED} if a selftest failed. Currently, the only selftest is for Windows MingW32 targets to see if @code{-mms-bitfields} was used (as required). @end deftypefun @node Destroying Contexts @section Destroying Contexts @cindex context, destruction @deftypefun void gpgme_release (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_release} destroys the context with the handle @var{ctx} and releases all associated resources. @end deftypefun @node Result Management @section Result Management @cindex context, result of operation The detailed result of an operation is returned in operation-specific structures such as @code{gpgme_decrypt_result_t}. The corresponding retrieval functions such as @code{gpgme_op_decrypt_result} provide static access to the results after an operation completes. Those structures shall be considered read-only and an application must not allocate such a structure on its own. The following interfaces make it possible to detach a result structure from its associated context and give it a lifetime beyond that of the current operation or context. @deftypefun void gpgme_result_ref (@w{void *@var{result}}) @since{1.2.0} The function @code{gpgme_result_ref} acquires an additional reference for the result @var{result}, which may be of any type @code{gpgme_*_result_t}. As long as the user holds a reference, the result structure is guaranteed to be valid and unmodified. @end deftypefun @deftypefun void gpgme_result_unref (@w{void *@var{result}}) @since{1.2.0} The function @code{gpgme_result_unref} releases a reference for the result @var{result}. If this was the last reference, the result structure will be destroyed and all resources associated to it will be released. @end deftypefun Note that a context may hold its own references to result structures, typically until the context is destroyed or the next operation is started. In fact, these references are accessed through the @code{gpgme_op_*_result} functions. @node Context Attributes @section Context Attributes @cindex context, attributes @menu * Protocol Selection:: Selecting the protocol used by a context. * Crypto Engine:: Configuring the crypto engine. * Setting the Sender:: How to tell the engine the sender. * ASCII Armor:: Requesting @acronym{ASCII} armored output. * Text Mode:: Choosing canonical text mode. * Offline Mode:: Choosing offline mode. * Pinentry Mode:: Choosing the pinentry mode. * Included Certificates:: Including a number of certificates. * Key Listing Mode:: Selecting key listing mode. * Passphrase Callback:: Getting the passphrase from the user. * Progress Meter Callback:: Being informed about the progress. * Status Message Callback:: Status messages received from gpg. * Locale:: Setting the locale of a context. @end menu @node Protocol Selection @subsection Protocol Selection @cindex context, selecting protocol @cindex protocol, selecting @deftypefun gpgme_error_t gpgme_set_protocol (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_protocol_t @var{proto}}) The function @code{gpgme_set_protocol} sets the protocol used within the context @var{ctx} to @var{proto}. All crypto operations will be performed by the crypto engine configured for that protocol. @xref{Protocols and Engines}. Setting the protocol with @code{gpgme_set_protocol} does not check if the crypto engine for that protocol is available and installed correctly. @xref{Engine Version Check}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the protocol could be set successfully, and @code{GPG_ERR_INV_VALUE} if @var{protocol} is not a valid protocol. @end deftypefun @deftypefun gpgme_protocol_t gpgme_get_protocol (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_get_protocol} retrieves the protocol currently use with the context @var{ctx}. @end deftypefun @node Crypto Engine @subsection Crypto Engine @cindex context, configuring engine @cindex engine, configuration per context The following functions can be used to set and retrieve the configuration of the crypto engines of a specific context. The default can also be retrieved without any particular context. @xref{Engine Information}. The default can also be changed globally. @xref{Engine Configuration}. @deftypefun gpgme_engine_info_t gpgme_ctx_get_engine_info (@w{gpgme_ctx_t @var{ctx}}) @since{1.1.0} The function @code{gpgme_ctx_get_engine_info} returns a linked list of engine info structures. Each info structure describes the configuration of one configured backend, as used by the context @var{ctx}. The result is valid until the next invocation of @code{gpgme_ctx_set_engine_info} for this particular context. This function can not fail. @end deftypefun @deftypefun gpgme_error_t gpgme_ctx_set_engine_info (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_protocol_t @var{proto}}, @w{const char *@var{file_name}}, @w{const char *@var{home_dir}}) @since{1.1.0} The function @code{gpgme_ctx_set_engine_info} changes the configuration of the crypto engine implementing the protocol @var{proto} for the context @var{ctx}. @var{file_name} is the file name of the executable program implementing this protocol, and @var{home_dir} is the directory name of the configuration directory for this crypto engine. If @var{home_dir} is @code{NULL}, the engine's default will be used. Currently this function must be used before starting the first crypto operation. It is unspecified if and when the changes will take effect if the function is called after starting the first operation on the context @var{ctx}. This function returns the error code @code{GPG_ERR_NO_ERROR} if successful, or an error code on failure. @end deftypefun @node Setting the Sender @subsection How to tell the engine the sender. @cindex context, sender @cindex sender @cindex From: Some engines can make use of the sender’s address, for example to figure out the best user id in certain trust models. For verification and signing of mails, it is thus suggested to let the engine know the sender ("From:") address. @acronym{GPGME} provides two functions to accomplish that. Note that the esoteric use of multiple "From:" addresses is not supported. @deftypefun gpgme_error_t gpgme_set_sender @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{int @var{address}}) @since{1.8.0} The function @code{gpgme_set_sender} specifies the sender address for use in sign and verify operations. @var{address} is expected to be the ``addr-spec'' part of an address but my also be a complete mailbox address, in which case this function extracts the ``addr-spec'' from it. Using @code{NULL} for @var{address} clears the sender address. The function returns 0 on success or an error code on failure. The most likely failure is that no valid ``addr-spec'' was found in @var{address}. @end deftypefun @deftypefun @w{const char *} gpgme_get_sender @ (@w{gpgme_ctx_t @var{ctx}}) @since{1.8.0} The function @code{gpgme_get_sender} returns the current sender address from the context, or NULL if none was set. The returned value is valid as long as the @var{ctx} is valid and @code{gpgme_set_sender} has not been called again. @end deftypefun @c FIXME: Unfortunately, using @acronym here breaks texi2dvi. @node ASCII Armor @subsection @acronym{ASCII} Armor @cindex context, armor mode @cindex @acronym{ASCII} armor @cindex armor mode @deftypefun void gpgme_set_armor (@w{gpgme_ctx_t @var{ctx}}, @w{int @var{yes}}) The function @code{gpgme_set_armor} specifies if the output should be @acronym{ASCII} armored. By default, output is not @acronym{ASCII} armored. @acronym{ASCII} armored output is disabled if @var{yes} is zero, and enabled otherwise. @end deftypefun @deftypefun int gpgme_get_armor (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_get_armor} returns 1 if the output is @acronym{ASCII} armored, and @code{0} if it is not, or if @var{ctx} is not a valid pointer. @end deftypefun @node Text Mode @subsection Text Mode @cindex context, text mode @cindex text mode @cindex canonical text mode @deftypefun void gpgme_set_textmode (@w{gpgme_ctx_t @var{ctx}}, @w{int @var{yes}}) The function @code{gpgme_set_textmode} specifies if canonical text mode should be used. By default, text mode is not used. Text mode is for example used for the RFC2015 signatures; note that the updated RFC 3156 mandates that the mail user agent does some preparations so that text mode is not needed anymore. This option is only relevant to the OpenPGP crypto engine, and ignored by all other engines. Canonical text mode is disabled if @var{yes} is zero, and enabled otherwise. @end deftypefun @deftypefun int gpgme_get_textmode (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_get_textmode} returns 1 if canonical text mode is enabled, and @code{0} if it is not, or if @var{ctx} is not a valid pointer. @end deftypefun @node Offline Mode @subsection Offline Mode @cindex context, offline mode @cindex offline mode @deftypefun void gpgme_set_offline (@w{gpgme_ctx_t @var{ctx}}, @w{int @var{yes}}) @since{1.6.0} The function @code{gpgme_set_offline} specifies if offline mode should be used. By default, offline mode is not used. The offline mode specifies if dirmngr should be used to do additional validation that might require connections to external services. (e.g. CRL / OCSP checks). Offline mode only affects the keylist mode @code{GPGME_KEYLIST_MODE_VALIDATE} and is only relevant to the CMS crypto engine. Offline mode is ignored otherwise. This option may be extended in the future to completely disable the use of dirmngr for any engine. Offline mode is disabled if @var{yes} is zero, and enabled otherwise. @end deftypefun @deftypefun int gpgme_get_offline (@w{gpgme_ctx_t @var{ctx}}) @since{1.6.0} The function @code{gpgme_get_offline} returns 1 if offline mode is enabled, and @code{0} if it is not, or if @var{ctx} is not a valid pointer. @end deftypefun @node Pinentry Mode @subsection Pinentry Mode @cindex context, pinentry mode @cindex pinentry mode @deftypefun gpgme_error_t gpgme_set_pinentry_mode (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_pinentry_mode_t @var{mode}}) @since{1.4.0} The function @code{gpgme_set_pinentry_mode} specifies the pinentry mode to be used. For GnuPG >= 2.1 this option is required to be set to @code{GPGME_PINENTRY_MODE_LOOPBACK} to enable the passphrase callback mechanism in GPGME through @code{gpgme_set_passphrase_cb}. @end deftypefun @deftypefun gpgme_pinentry_mode_t gpgme_get_pinentry_mode (@w{gpgme_ctx_t @var{ctx}}) @since{1.4.0} The function @code{gpgme_get_pinenty_mode} returns the mode set for the context. @end deftypefun @deftp {Data type} {enum gpgme_pinentry_mode_t} @tindex gpgme_pinentry_mode_t @since{1.4.0} The @code{gpgme_minentry_mode_t} type specifies the set of possible pinentry modes that are supported by @acronym{GPGME} if GnuPG >= 2.1 is used. The following modes are supported: @table @code @item GPGME_PINENTRY_MODE_DEFAULT @since{1.4.0} Use the default of the agent, which is ask. @item GPGME_PINENTRY_MODE_ASK @since{1.4.0} Force the use of the Pinentry. @item GPGME_PINENTRY_MODE_CANCEL @since{1.4.0} Emulate use of Pinentry's cancel button. @item GPGME_PINENTRY_MODE_ERROR @since{1.4.0} Return a Pinentry error @code{No Pinentry}. @item GPGME_PINENTRY_MODE_LOOPBACK @since{1.4.0} Redirect Pinentry queries to the caller. This enables the use of @code{gpgme_set_passphrase_cb} because pinentry queries are redirected to gpgme. Note: For 2.1.0 - 2.1.12 this mode requires @code{allow-loopback-pinentry} to be enabled in the @file{gpg-agent.conf} or an agent started with that option. @end table @end deftp @node Included Certificates @subsection Included Certificates @cindex certificates, included @deftypefun void gpgme_set_include_certs (@w{gpgme_ctx_t @var{ctx}}, @w{int @var{nr_of_certs}}) The function @code{gpgme_set_include_certs} specifies how many certificates should be included in an S/MIME signed message. By default, only the sender's certificate is included. The possible values of @var{nr_of_certs} are: @table @code @item GPGME_INCLUDE_CERTS_DEFAULT @since{1.0.3} Fall back to the default of the crypto backend. This is the default for GPGME. @item -2 Include all certificates except the root certificate. @item -1 Include all certificates. @item 0 Include no certificates. @item 1 Include the sender's certificate only. @item n Include the first n certificates of the certificates path, starting from the sender's certificate. The number @code{n} must be positive. @end table Values of @var{nr_of_certs} smaller than -2 are undefined. This option is only relevant to the CMS crypto engine, and ignored by all other engines. @end deftypefun @deftypefun int gpgme_get_include_certs (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_get_include_certs} returns the number of certificates to include into an S/MIME signed message. @end deftypefun @node Key Listing Mode @subsection Key Listing Mode @cindex key listing mode @cindex key listing, mode of @deftypefun gpgme_error_t gpgme_set_keylist_mode (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_keylist_mode_t @var{mode}}) The function @code{gpgme_set_keylist_mode} changes the default behaviour of the key listing functions. The value in @var{mode} is a bitwise-or combination of one or multiple of the following bit values: @table @code @item GPGME_KEYLIST_MODE_LOCAL The @code{GPGME_KEYLIST_MODE_LOCAL} symbol specifies that the local keyring should be searched for keys in the keylisting operation. This is the default. @item GPGME_KEYLIST_MODE_EXTERN The @code{GPGME_KEYLIST_MODE_EXTERN} symbol specifies that an external source should be searched for keys in the keylisting operation. The type of external source is dependent on the crypto engine used and whether it is combined with @code{GPGME_KEYLIST_MODE_LOCAL}. For example, it can be a remote keyserver or LDAP certificate server. @item GPGME_KEYLIST_MODE_SIGS The @code{GPGME_KEYLIST_MODE_SIGS} symbol specifies that the key signatures should be included in the listed keys. @item GPGME_KEYLIST_MODE_SIG_NOTATIONS @since{1.1.1} The @code{GPGME_KEYLIST_MODE_SIG_NOTATIONS} symbol specifies that the signature notations on key signatures should be included in the listed keys. This only works if @code{GPGME_KEYLIST_MODE_SIGS} is also enabled. @item GPGME_KEYLIST_MODE_WITH_TOFU @since{1.7.0} The @code{GPGME_KEYLIST_MODE_WITH_TOFU} symbol specifies that information pertaining to the TOFU trust model should be included in the listed keys. @item GPGME_KEYLIST_MODE_WITH_SECRET @since{1.5.1} The @code{GPGME_KEYLIST_MODE_WITH_SECRET} returns information about the presence of a corresponding secret key in a public key listing. A public key listing with this mode is slower than a standard listing but can be used instead of a second run to list the secret keys. This is only supported for GnuPG versions >= 2.1. @item GPGME_KEYLIST_MODE_EPHEMERAL @since{1.2.0} The @code{GPGME_KEYLIST_MODE_EPHEMERAL} symbol specifies that keys flagged as ephemeral are included in the listing. @item GPGME_KEYLIST_MODE_VALIDATE @since{0.4.5} The @code{GPGME_KEYLIST_MODE_VALIDATE} symbol specifies that the backend should do key or certificate validation and not just get the validity information from an internal cache. This might be an expensive operation and is in general not useful. Currently only implemented for the S/MIME backend and ignored for other backends. @end table At least one of @code{GPGME_KEYLIST_MODE_LOCAL} and @code{GPGME_KEYLIST_MODE_EXTERN} must be specified. For future binary compatibility, you should get the current mode with @code{gpgme_get_keylist_mode} and modify it by setting or clearing the appropriate bits, and then using that calculated value in the @code{gpgme_set_keylisting_mode} operation. This will leave all other bits in the mode value intact (in particular those that are not used in the current version of the library). The function returns the error code @code{GPG_ERR_NO_ERROR} if the mode could be set correctly, and @code{GPG_ERR_INV_VALUE} if @var{ctx} is not a valid pointer or @var{mode} is not a valid mode. @end deftypefun @deftypefun gpgme_keylist_mode_t gpgme_get_keylist_mode (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_get_keylist_mode} returns the current key listing mode of the context @var{ctx}. This value can then be modified and used in a subsequent @code{gpgme_set_keylist_mode} operation to only affect the desired bits (and leave all others intact). The function returns 0 if @var{ctx} is not a valid pointer, and the current mode otherwise. Note that 0 is not a valid mode value. @end deftypefun @node Passphrase Callback @subsection Passphrase Callback @cindex callback, passphrase @cindex passphrase callback @deftp {Data type} {gpgme_error_t (*gpgme_passphrase_cb_t)(void *@var{hook}, const char *@var{uid_hint}, const char *@var{passphrase_info}, @w{int @var{prev_was_bad}}, @w{int @var{fd}})} @tindex gpgme_passphrase_cb_t The @code{gpgme_passphrase_cb_t} type is the type of functions usable as passphrase callback function. The argument @var{uid_hint} might contain a string that gives an indication for which user ID the passphrase is required. If this is not available, or not applicable (in the case of symmetric encryption, for example), @var{uid_hint} will be @code{NULL}. The argument @var{passphrase_info}, if not @code{NULL}, will give further information about the context in which the passphrase is required. This information is engine and operation specific. If this is the repeated attempt to get the passphrase, because previous attempts failed, then @var{prev_was_bad} is 1, otherwise it will be 0. The user must write the passphrase, followed by a newline character, to the file descriptor @var{fd}. The function @code{gpgme_io_writen} should be used for the write operation. Note that if the user returns 0 to indicate success, the user must at least write a newline character before returning from the callback. If an error occurs, return the corresponding @code{gpgme_error_t} value. You can use the error code @code{GPG_ERR_CANCELED} to abort the operation. Otherwise, return @code{0}. Note: The passphrase_cb only works with GnuPG 1.x and 2.1.x and not with the 2.0.x series. See @code{gpgme_set_pinentry_mode} for more details on 2.1.x usage. @end deftp @deftypefun void gpgme_set_passphrase_cb (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_passphrase_cb_t @var{passfunc}}, @w{void *@var{hook_value}}) The function @code{gpgme_set_passphrase_cb} sets the function that is used when a passphrase needs to be provided by the user to @var{passfunc}. The function @var{passfunc} needs to implemented by the user, and whenever it is called, it is called with its first argument being @var{hook_value}. By default, no passphrase callback function is set. Not all crypto engines require this callback to retrieve the passphrase. It is better if the engine retrieves the passphrase from a trusted agent (a daemon process), rather than having each user to implement their own passphrase query. Some engines do not even support an external passphrase callback at all, in this case the error code @code{GPG_ERR_NOT_SUPPORTED} is returned. For GnuPG >= 2.1 the pinentry mode has to be set to @code{GPGME_PINENTRY_MODE_LOOPBACK} to enable the passphrase callback. See @code{gpgme_set_pinentry_mode}. The user can disable the use of a passphrase callback function by calling @code{gpgme_set_passphrase_cb} with @var{passfunc} being @code{NULL}. @end deftypefun @deftypefun void gpgme_get_passphrase_cb (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_passphrase_cb_t *@var{passfunc}}, @w{void **@var{hook_value}}) The function @code{gpgme_get_passphrase_cb} returns the function that is used when a passphrase needs to be provided by the user in @var{*passfunc}, and the first argument for this function in @var{*hook_value}. If no passphrase callback is set, or @var{ctx} is not a valid pointer, @code{NULL} is returned in both variables. @var{passfunc} or @var{hook_value} can be @code{NULL}. In this case, the corresponding value will not be returned. @end deftypefun @node Progress Meter Callback @subsection Progress Meter Callback @cindex callback, progress meter @cindex progress meter callback @deftp {Data type} {void (*gpgme_progress_cb_t)(void *@var{hook}, const char *@var{what}, int @var{type}, int @var{current}, int @var{total})} @tindex gpgme_progress_cb_t The @code{gpgme_progress_cb_t} type is the type of functions usable as progress callback function. The arguments are specific to the crypto engine. More information about the progress information returned from the GnuPG engine can be found in the GnuPG source code in the file @file{doc/DETAILS} in the section PROGRESS. @end deftp @deftypefun void gpgme_set_progress_cb (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_progress_cb_t @var{progfunc}}, @w{void *@var{hook_value}}) The function @code{gpgme_set_progress_cb} sets the function that is used when progress information about a cryptographic operation is available. The function @var{progfunc} needs to implemented by the user, and whenever it is called, it is called with its first argument being @var{hook_value}. By default, no progress callback function is set. Setting a callback function allows an interactive program to display progress information about a long operation to the user. The user can disable the use of a progress callback function by calling @code{gpgme_set_progress_cb} with @var{progfunc} being @code{NULL}. @end deftypefun @deftypefun void gpgme_get_progress_cb (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_progress_cb_t *@var{progfunc}}, @w{void **@var{hook_value}}) The function @code{gpgme_get_progress_cb} returns the function that is used to inform the user about the progress made in @var{*progfunc}, and the first argument for this function in @var{*hook_value}. If no progress callback is set, or @var{ctx} is not a valid pointer, @code{NULL} is returned in both variables. @var{progfunc} or @var{hook_value} can be @code{NULL}. In this case, the corresponding value will not be returned. @end deftypefun @node Status Message Callback @subsection Status Message Callback @cindex callback, status message @cindex status message callback @deftp {Data type} {gpgme_error_t (*gpgme_status_cb_t)(void *@var{hook}, const char *@var{keyword}, const char *@var{args})} @tindex gpgme_status_cb_t The @code{gpgme_status_cb_t} type is the type of function usable as a status message callback function. The argument @var{keyword} is the name of the status message while the @var{args} argument contains any arguments for the status message. If an error occurs, return the corresponding @code{gpgme_error_t} value. Otherwise, return @code{0}. @end deftp @deftypefun void gpgme_set_status_cb (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_status_cb_t @var{statusfunc}}, @w{void *@var{hook_value}}) @since{1.6.0} The function @code{gpgme_set_status_cb} sets the function that is used when a status message is received from gpg to @var{statusfunc}. The function @var{statusfunc} needs to be implemented by the user, and whenever it is called, it is called with its first argument being @var{hook_value}. By default, no status message callback function is set. The user can disable the use of a status message callback function by calling @code{gpgme_set_status_cb} with @var{statusfunc} being @code{NULL}. @end deftypefun @deftypefun void gpgme_get_status_cb (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_status_cb_t *@var{statusfunc}}, @w{void **@var{hook_value}}) @since{1.6.0} The function @code{gpgme_get_status_cb} returns the function that is used to process status messages from gpg in @var{*statusfunc}, and the first argument for this function in @var{*hook_value}. If no status message callback is set, or @var{ctx} is not a valid pointer, @code{NULL} is returned in both variables. @end deftypefun @deftypefun {gpgme_error_t} gpgme_set_ctx_flag @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{const char *@var{name}}, @ @w{const char *@var{value}}) @since{1.7.0} Some minor properties of the context can be controlled with flags set by this function. The properties are identified by the following values for @var{name}: @table @code @item "redraw" This flag is normally not changed by the caller because GPGME sets and clears it automatically: The flag is cleared before an operation and set if an operation noticed that the engine has launched a Pinentry. A Curses based application may use this information to redraw the screen; for example: @example err = gpgme_op_keylist_start (ctx, "foo@@example.org", 0); while (!err) @{ err = gpgme_op_keylist_next (ctx, &key); if (err) break; show_key (key); gpgme_key_release (key); @} if ((s = gpgme_get_ctx_flag (ctx, "redraw")) && *s) redraw_screen (); gpgme_release (ctx); @end example @item "full-status" Using a @var{value} of "1" the status callback set by gpgme_set_status_cb returns all status lines with the exception of PROGRESS lines. With the default of "0" the status callback is only called in certain situations. @item "raw-description" Setting the @var{value} to "1" returns human readable strings in a raw format. For example the non breaking space characters ("~") will not be removed from the @code{description} field of the @code{gpgme_tofu_info_t} object. @item "export-session-key" Using a @var{value} of "1" specifies that the context should try to export the symmetric session key when decrypting data. By default, or when using an empty string or "0" for @var{value}, session keys are not exported. @item "override-session-key" The string given in @var{value} is passed to the GnuPG engine to override the session key for decryption. The format of that session key is specific to GnuPG and can be retrieved during a decrypt operation when the context flag "export-session-key" is enabled. Please be aware that using this feature with GnuPG < 2.1.16 will leak the session key on many platforms via ps(1). @item "auto-key-retrieve" Setting the @var{value} to "1" asks the backend to automatically retrieve a key for signature verification if possible. Note that this option makes a "web bug" like behavior possible. Keyserver or Web Key Directory operators can see which keys you request, so by sending you a message signed by a brand new key (which you naturally will not have on your local keyring), the operator can tell both your IP address and the time when you verified the signature. @item "request-origin" The string given in @var{value} is passed to the GnuPG engines to request restrictions based on the origin of the request. Valid values are documented in the GnuPG manual and the gpg man page under the option ``--request-origin''. Requires at least GnuPG 2.2.6 to have an effect. @item "no-symkey-cache" For OpenPGP disable the passphrase cache used for symmetrical en- and decryption. This cache is based on the message specific salt value. Requires at least GnuPG 2.2.7 to have an effect. @end table This function returns @code{0} on success. @end deftypefun @deftypefun {const char *} gpgme_get_ctx_flag @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{const char *@var{name}}) @since{1.8.0} The value of flags settable by @code{gpgme_set_ctx_flag} can be retrieved by this function. If @var{name} is unknown the function returns @code{NULL}. For boolean flags an empty string is returned for False and the string "1" is returned for True; either atoi(3) or a test for an empty string can be used to get the boolean value. @end deftypefun @node Locale @subsection Locale @cindex locale, default @cindex locale, of a context A locale setting can be associated with a context. This locale is passed to the crypto engine, and used for applications like the PIN entry, which is displayed to the user when entering a passphrase is required. The default locale is used to initialize the locale setting of all contexts created afterwards. @deftypefun gpgme_error_t gpgme_set_locale (@w{gpgme_ctx_t @var{ctx}}, @w{int @var{category}}, @w{const char *@var{value}}) @since{0.4.3} The function @code{gpgme_set_locale} sets the locale of the context @var{ctx}, or the default locale if @var{ctx} is a null pointer. The locale settings that should be changed are specified by @var{category}. Supported categories are @code{LC_CTYPE}, @code{LC_MESSAGES}, and @code{LC_ALL}, which is a wildcard you can use if you want to change all the categories at once. The value to be used for the locale setting is @var{value}, which will be copied to @acronym{GPGME}'s internal data structures. @var{value} can be a null pointer, which disables setting the locale, and will make PIN entry and other applications use their default setting, which is usually not what you want. Note that the settings are only used if the application runs on a text terminal, and that the settings should fit the configuration of the output terminal. Normally, it is sufficient to initialize the default value at startup. The function returns an error if not enough memory is available. @end deftypefun @node Key Management @section Key Management @cindex key management Some of the cryptographic operations require that recipients or signers are specified. This is always done by specifying the respective keys that should be used for the operation. The following section describes how such keys can be selected and manipulated. @menu * Key objects:: Description of the key structures. * Listing Keys:: Browsing the list of available keys. * Information About Keys:: Requesting detailed information about keys. * Manipulating Keys:: Operations on keys. * Generating Keys:: Creating new key pairs. * Signing Keys:: Adding key signatures to public keys. * Exporting Keys:: Retrieving key data from the key ring. * Importing Keys:: Adding keys to the key ring. * Deleting Keys:: Removing keys from the key ring. * Changing Passphrases:: Change the passphrase of a key. * Changing TOFU Data:: Changing data pertaining to TOFU. * Advanced Key Editing:: Advanced key edit operation. @end menu @node Key objects @subsection Key objects The keys are represented in GPGME by structures which may only be read by the application but never be allocated or changed. They are valid as long as the key object itself is valid. @deftp {Data type} gpgme_key_t The @code{gpgme_key_t} type is a pointer to a key object. It has the following members: @table @code @item gpgme_keylist_mode_t keylist_mode @since{0.9.0} The keylist mode that was active when the key was retrieved. @item unsigned int revoked : 1 This is true if the key is revoked. @item unsigned int expired : 1 This is true if the key is expired. @item unsigned int disabled : 1 This is true if the key is disabled. @item unsigned int invalid : 1 This is true if the key is invalid. This might have several reasons, for a example for the S/MIME backend, it will be set during key listings if the key could not be validated due to missing certificates or unmatched policies. @item unsigned int can_encrypt : 1 This is true if the key (ie one of its subkeys) can be used for encryption. @item unsigned int can_sign : 1 This is true if the key (ie one of its subkeys) can be used to create data signatures. @item unsigned int can_certify : 1 This is true if the key (ie one of its subkeys) can be used to create key certificates. @item unsigned int can_authenticate : 1 @since{0.4.5} This is true if the key (ie one of its subkeys) can be used for authentication. @item unsigned int is_qualified : 1 @since{1.1.0} This is true if the key can be used for qualified signatures according to local government regulations. @item unsigned int secret : 1 This is true if the key is a secret key. Note, that this will always be true even if the corresponding subkey flag may be false (offline/stub keys). This is only set if a listing of secret keys has been requested or if @code{GPGME_KEYLIST_MODE_WITH_SECRET} is active. @item unsigned int origin : 5 @since{1.8.0} Reserved for the origin of this key. @item gpgme_protocol_t protocol This is the protocol supported by this key. @item char *issuer_serial If @code{protocol} is @code{GPGME_PROTOCOL_CMS}, then this is the issuer serial. @item char *issuer_name If @code{protocol} is @code{GPGME_PROTOCOL_CMS}, then this is the issuer name. @item char *chain_id If @code{protocol} is @code{GPGME_PROTOCOL_CMS}, then this is the chain ID, which can be used to built the certificate chain. @item gpgme_validity_t owner_trust If @code{protocol} is @code{GPGME_PROTOCOL_OpenPGP}, then this is the owner trust. @item gpgme_subkey_t subkeys This is a linked list with the subkeys of the key. The first subkey in the list is the primary key and usually available. @item gpgme_user_id_t uids This is a linked list with the user IDs of the key. The first user ID in the list is the main (or primary) user ID. @item char *fpr @since{1.7.0} This field gives the fingerprint of the primary key. Note that this is a copy of the fingerprint of the first subkey. For an incomplete key (for example from a verification result) a subkey may be missing but this field may be set nevertheless. @item unsigned long last_update @since{1.8.0} Reserved for the time of the last update of this key. @end table @end deftp @deftp {Data type} gpgme_subkey_t @since{1.5.0} The @code{gpgme_subkey_t} type is a pointer to a subkey structure. Subkeys are one component of a @code{gpgme_key_t} object. In fact, subkeys are those parts that contains the real information about the individual cryptographic keys that belong to the same key object. One @code{gpgme_key_t} can contain several subkeys. The first subkey in the linked list is also called the primary key. The subkey structure has the following members: @table @code @item gpgme_subkey_t next This is a pointer to the next subkey structure in the linked list, or @code{NULL} if this is the last element. @item unsigned int revoked : 1 This is true if the subkey is revoked. @item unsigned int expired : 1 This is true if the subkey is expired. @item unsigned int disabled : 1 This is true if the subkey is disabled. @item unsigned int invalid : 1 This is true if the subkey is invalid. @item unsigned int can_encrypt : 1 This is true if the subkey can be used for encryption. @item unsigned int can_sign : 1 This is true if the subkey can be used to create data signatures. @item unsigned int can_certify : 1 This is true if the subkey can be used to create key certificates. @item unsigned int can_authenticate : 1 @since{0.4.5} This is true if the subkey can be used for authentication. @item unsigned int is_qualified : 1 @since{1.1.0} This is true if the subkey can be used for qualified signatures according to local government regulations. @item unsigned int is_de_vs : 1 @since{1.8.0} This is true if the subkey complies with the rules for classified information in Germany at the restricted level (VS-NfD). This are currently RSA keys of at least 2048 bits or ECDH/ECDSA keys using a Brainpool curve. @item unsigned int secret : 1 This is true if the subkey is a secret key. Note that it will be false if the key is actually a stub key; i.e. a secret key operation is currently not possible (offline-key). This is only set if a listing of secret keys has been requested or if @code{GPGME_KEYLIST_MODE_WITH_SECRET} is active. @item gpgme_pubkey_algo_t pubkey_algo This is the public key algorithm supported by this subkey. @item unsigned int length This is the length of the subkey (in bits). @item char *keyid This is the key ID of the subkey in hexadecimal digits. @item char *fpr This is the fingerprint of the subkey in hexadecimal digits, if available. @item char *keygrip @since{1.7.0} The keygrip of the subkey in hex digit form or @code{NULL} if not availabale. @item long int timestamp This is the creation timestamp of the subkey. This is -1 if the timestamp is invalid, and 0 if it is not available. @item long int expires This is the expiration timestamp of the subkey, or 0 if the subkey does not expire. @item unsigned int is_cardkey : 1 @since{1.2.0} True if the secret key is stored on a smart card. @item char *card_number @since{1.2.0} The serial number of a smart card holding this key or @code{NULL}. @item char *curve For ECC algorithms the name of the curve. @end table @end deftp @deftp {Data type} gpgme_user_id_t A user ID is a component of a @code{gpgme_key_t} object. One key can have many user IDs. The first one in the list is the main (or primary) user ID. The user ID structure has the following members. @table @code @item gpgme_user_id_t next This is a pointer to the next user ID structure in the linked list, or @code{NULL} if this is the last element. @item unsigned int revoked : 1 This is true if the user ID is revoked. @item unsigned int invalid : 1 This is true if the user ID is invalid. @item gpgme_validity_t validity This specifies the validity of the user ID. @item char *uid This is the user ID string. @item char *name This is the name component of @code{uid}, if available. @item char *comment This is the comment component of @code{uid}, if available. @item char *email This is the email component of @code{uid}, if available. @item char *address; The mail address (addr-spec from RFC-5322) of the user ID string. This is general the same as the @code{email} part of this structure but might be slightly different. If no mail address is available @code{NULL} is stored. @item gpgme_tofu_info_t tofu @since{1.7.0} If not @code{NULL} information from the TOFU database pertaining to this user id. @item gpgme_key_sig_t signatures This is a linked list with the signatures on this user ID. @item unsigned int origin : 5 @since{1.8.0} Reserved for the origin of this user ID. @item unsigned long last_update @since{1.8.0} Reserved for the time of the last update of this user ID. @end table @end deftp @deftp {Data type} gpgme_tofu_info_t @since{1.7.0} The @code{gpgme_tofu_info_t} type is a pointer to a tofu info structure. Tofu info structures are one component of a @code{gpgme_user_id_t} object, and provide information from the TOFU database pertaining to the user ID. The tofu info structure has the following members: @table @code @item gpgme_key_sig_t next This is a pointer to the next tofu info structure in the linked list, or @code{NULL} if this is the last element. @item unsigned int validity : 3 This is the TOFU validity. It can have the following values: @table @code @item 0 The value @code{0} indicates a conflict. @item 1 The value @code{1} indicates a key without history. @item 2 The value @code{2} indicates a key with too little history. @item 3 The value @code{3} indicates a key with enough history for basic trust. @item 4 The value @code{4} indicates a key with a lot of history. @end table @item unsigned int policy : 4 This is the TOFU policy, see @code{gpgme_tofu_policy_t}. @item unsigned short signcount This is the number of signatures seen for this binding (or @code{USHRT_MAX} if there are more than that). @item unsigned short encrcount This is the number of encryptions done with this binding (or @code{USHRT_MAX} if there are more than that). @item unsigned long signfirst Number of seconds since Epoch when the first signature was seen with this binding. @item unsigned long signlast Number of seconds since Epoch when the last signature was seen with this binding. @item unsigned long encrfirst Number of seconds since Epoch when the first encryption was done with this binding. @item unsigned long encrlast Number of seconds since Epoch when the last encryption was done with this binding. @item char *description A human-readable string summarizing the TOFU data (or NULL). @end table @end deftp @deftp {Data type} gpgme_key_sig_t The @code{gpgme_key_sig_t} type is a pointer to a key signature structure. Key signatures are one component of a @code{gpgme_key_t} object, and validate user IDs on the key in the OpenPGP protocol. The signatures on a key are only available if the key was retrieved via a listing operation with the @code{GPGME_KEYLIST_MODE_SIGS} mode enabled, because it can be expensive to retrieve all signatures of a key. The signature notations on a key signature are only available if the key was retrieved via a listing operation with the @code{GPGME_KEYLIST_MODE_SIG_NOTATIONS} mode enabled, because it can be expensive to retrieve all signature notations. The key signature structure has the following members: @table @code @item gpgme_key_sig_t next This is a pointer to the next key signature structure in the linked list, or @code{NULL} if this is the last element. @item unsigned int revoked : 1 This is true if the key signature is a revocation signature. @item unsigned int expired : 1 This is true if the key signature is expired. @item unsigned int invalid : 1 This is true if the key signature is invalid. @item unsigned int exportable : 1 This is true if the key signature is exportable. @item gpgme_pubkey_algo_t pubkey_algo This is the public key algorithm used to create the signature. @item char *keyid This is the key ID of the key (in hexadecimal digits) used to create the signature. @item long int timestamp This is the creation timestamp of the key signature. This is -1 if the timestamp is invalid, and 0 if it is not available. @item long int expires This is the expiration timestamp of the key signature, or 0 if the key signature does not expire. @item gpgme_error_t status This is the status of the signature and has the same meaning as the member of the same name in a @code{gpgme_signature_t} object. @item unsigned int sig_class This specifies the signature class of the key signature. The meaning is specific to the crypto engine. @item char *uid This is the main user ID of the key used to create the signature. @item char *name This is the name component of @code{uid}, if available. @item char *comment This is the comment component of @code{uid}, if available. @item char *email This is the email component of @code{uid}, if available. @item gpgme_sig_notation_t notations This is a linked list with the notation data and policy URLs. @end table @end deftp @node Listing Keys @subsection Listing Keys @cindex listing keys @cindex key listing @cindex key listing, start @cindex key ring, list @cindex key ring, search @deftypefun gpgme_error_t gpgme_op_keylist_start (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{pattern}}, @w{int @var{secret_only}}) The function @code{gpgme_op_keylist_start} initiates a key listing operation inside the context @var{ctx}. It sets everything up so that subsequent invocations of @code{gpgme_op_keylist_next} return the keys in the list. If @var{pattern} is @code{NULL}, all available keys are returned. Otherwise, @var{pattern} contains an engine specific expression that is used to limit the list to all keys matching the pattern. Note that the total length of the pattern is restricted to an engine-specific maximum (a couple of hundred characters are usually accepted). The pattern should be used to restrict the search to a certain common name or user, not to list many specific keys at once by listing their fingerprints or key IDs. If @var{secret_only} is not @code{0}, the list is restricted to secret keys only. The context will be busy until either all keys are received (and @code{gpgme_op_keylist_next} returns @code{GPG_ERR_EOF}), or @code{gpgme_op_keylist_end} is called to finish the operation. The function returns the error code @code{GPG_ERR_INV_VALUE} if @var{ctx} is not a valid pointer, and passes through any errors that are reported by the crypto engine support routines. @end deftypefun @deftypefun gpgme_error_t gpgme_op_keylist_ext_start (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{pattern}[]}, @w{int @var{secret_only}}, @w{int @var{reserved}}) The function @code{gpgme_op_keylist_ext_start} initiates an extended key listing operation inside the context @var{ctx}. It sets everything up so that subsequent invocations of @code{gpgme_op_keylist_next} return the keys in the list. If @var{pattern} or @var{*pattern} is @code{NULL}, all available keys are returned. Otherwise, @var{pattern} is a @code{NULL} terminated array of strings that are used to limit the list to all keys matching at least one of the patterns verbatim. Note that the total length of all patterns is restricted to an engine-specific maximum (the exact limit also depends on the number of patterns and amount of quoting required, but a couple of hundred characters are usually accepted). Patterns should be used to restrict the search to a certain common name or user, not to list many specific keys at once by listing their fingerprints or key IDs. If @var{secret_only} is not @code{0}, the list is restricted to secret keys only. The value of @var{reserved} must be @code{0}. The context will be busy until either all keys are received (and @code{gpgme_op_keylist_next} returns @code{GPG_ERR_EOF}), or @code{gpgme_op_keylist_end} is called to finish the operation. The function returns the error code @code{GPG_ERR_INV_VALUE} if @var{ctx} is not a valid pointer, and passes through any errors that are reported by the crypto engine support routines. @end deftypefun @deftypefun gpgme_error_t gpgme_op_keylist_from_data_start @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{gpgme_data_t @var{data}}, @ @w{int @var{reserved}}) @since{1.8.0} The function @code{gpgme_op_keylist_from_data_start} initiates a key listing operation inside the context @var{ctx}. In contrast to the other key listing operation the keys are read from the supplied @var{data} and not from the local key database. The keys are also not imported into the local key database. The function sets everything up so that subsequent invocations of @code{gpgme_op_keylist_next} return the keys from @var{data}. The value of @var{reserved} must be @code{0}. This function requires at least GnuPG version 2.1.14 and currently works only with OpenPGP keys. The context will be busy until either all keys are received (and @code{gpgme_op_keylist_next} returns @code{GPG_ERR_EOF}), or @code{gpgme_op_keylist_end} is called to finish the operation. While the context is busy @var{data} may not be released. The function returns the error code @code{GPG_ERR_INV_VALUE} if @var{ctx} is not a valid pointer, and passes through any errors that are reported by the crypto engine support routines. @end deftypefun @deftypefun gpgme_error_t gpgme_op_keylist_next (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t *@var{r_key}}) The function @code{gpgme_op_keylist_next} returns the next key in the list created by a previous @code{gpgme_op_keylist_start} operation in the context @var{ctx}. The key will have one reference for the user. @xref{Manipulating Keys}. This is the only way to get at @code{gpgme_key_t} objects in @acronym{GPGME}. If the last key in the list has already been returned, @code{gpgme_op_keylist_next} returns @code{GPG_ERR_EOF}. The function returns the error code @code{GPG_ERR_INV_VALUE} if @var{ctx} or @var{r_key} is not a valid pointer, and @code{GPG_ERR_ENOMEM} if there is not enough memory for the operation. @end deftypefun @deftypefun gpgme_error_t gpgme_op_keylist_end (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_op_keylist_end} ends a pending key list operation in the context @var{ctx}. After the operation completed successfully, the result of the key listing operation can be retrieved with @code{gpgme_op_keylist_result}. The function returns the error code @code{GPG_ERR_INV_VALUE} if @var{ctx} is not a valid pointer, and @code{GPG_ERR_ENOMEM} if at some time during the operation there was not enough memory available. @end deftypefun The following example illustrates how all keys containing a certain string (@code{g10code}) can be listed with their key ID and the name and email address of the main user ID: @example gpgme_ctx_t ctx; gpgme_key_t key; gpgme_error_t err = gpgme_new (&ctx); if (!err) @{ err = gpgme_op_keylist_start (ctx, "g10code", 0); while (!err) @{ err = gpgme_op_keylist_next (ctx, &key); if (err) break; printf ("%s:", key->subkeys->keyid); if (key->uids && key->uids->name) printf (" %s", key->uids->name); if (key->uids && key->uids->email) printf (" <%s>", key->uids->email); putchar ('\n'); gpgme_key_release (key); @} gpgme_release (ctx); @} if (gpg_err_code (err) != GPG_ERR_EOF) @{ fprintf (stderr, "can not list keys: %s\n", gpgme_strerror (err)); exit (1); @} @end example @deftp {Data type} {gpgme_keylist_result_t} This is a pointer to a structure used to store the result of a @code{gpgme_op_keylist_*} operation. After successfully ending a key listing operation, you can retrieve the pointer to the result with @code{gpgme_op_keylist_result}. The structure contains the following member: @table @code @item unsigned int truncated : 1 This is true if the crypto backend had to truncate the result, and less than the desired keys could be listed. @end table @end deftp @deftypefun gpgme_keylist_result_t gpgme_op_keylist_result (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_op_keylist_result} returns a @code{gpgme_keylist_result_t} pointer to a structure holding the result of a @code{gpgme_op_keylist_*} operation. The pointer is only valid if the last operation on the context was a key listing operation, and if this operation finished successfully. The returned pointer is only valid until the next operation is started on the context. @end deftypefun In a simple program, for which a blocking operation is acceptable, the following function can be used to retrieve a single key. @deftypefun gpgme_error_t gpgme_get_key (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{fpr}}, @w{gpgme_key_t *@var{r_key}}, @w{int @var{secret}}) The function @code{gpgme_get_key} gets the key with the fingerprint (or key ID) @var{fpr} from the crypto backend and return it in @var{r_key}. If @var{secret} is true, get the secret key. The currently active keylist mode is used to retrieve the key. The key will have one reference for the user. If the key is not found in the keyring, @code{gpgme_get_key} returns the error code @code{GPG_ERR_EOF} and *@var{r_key} will be set to @code{NULL}. The function returns the error code @code{GPG_ERR_INV_VALUE} if @var{ctx} or @var{r_key} is not a valid pointer or @var{fpr} is not a fingerprint or key ID, @code{GPG_ERR_AMBIGUOUS_NAME} if the key ID was not a unique specifier for a key, and @code{GPG_ERR_ENOMEM} if at some time during the operation there was not enough memory available. @end deftypefun @node Information About Keys @subsection Information About Keys @cindex key, information about @cindex key, attributes @cindex attributes, of a key Please see the beginning of this section for more information about @code{gpgme_key_t} objects. @deftp {Data type} gpgme_validity_t The @code{gpgme_validity_t} type is used to specify the validity of a user ID in a key. The following validities are defined: @table @code @item GPGME_VALIDITY_UNKNOWN The user ID is of unknown validity. The string representation of this validity is ``?''. @item GPGME_VALIDITY_UNDEFINED The validity of the user ID is undefined. The string representation of this validity is ``q''. @item GPGME_VALIDITY_NEVER The user ID is never valid. The string representation of this validity is ``n''. @item GPGME_VALIDITY_MARGINAL The user ID is marginally valid. The string representation of this validity is ``m''. @item GPGME_VALIDITY_FULL The user ID is fully valid. The string representation of this validity is ``f''. @item GPGME_VALIDITY_ULTIMATE The user ID is ultimately valid. The string representation of this validity is ``u''. @end table @end deftp @node Manipulating Keys @subsection Manipulating Keys @cindex key, manipulation @deftypefun void gpgme_key_ref (@w{gpgme_key_t @var{key}}) The function @code{gpgme_key_ref} acquires an additional reference for the key @var{key}. @end deftypefun @deftypefun void gpgme_key_unref (@w{gpgme_key_t @var{key}}) The function @code{gpgme_key_unref} releases a reference for the key @var{key}. If this was the last reference, the key will be destroyed and all resources associated to it will be released. @end deftypefun @node Generating Keys @subsection Generating Keys @cindex key, creation @cindex key ring, add GPGME provides a set of functions to create public key pairs. Most of these functions require the use of GnuPG 2.1 and later; for older GnuPG versions the @code{gpgme_op_genkey} function can be used. Existing code which wants to update to the new functions or new code which shall supports older GnuPG versions may try the new functions first and provide a fallback to the old function if the error code @code{GPG_ERR_NOT_SUPPORTED} is received. @c @c gpgme_op_createkey @c @deftypefun gpgme_error_t gpgme_op_createkey @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{const char *@var{userid}}, @ @w{const char *@var{algo}}, @ @w{unsigned long @var{reserved}}, @ @w{unsigned long @var{expires}}, @ @w{gpgme_key_t @var{extrakey}}, @ @w{unsigned int @var{flags}}); @since{1.7.0} The function @code{gpgme_op_createkey} generates a new key for the procotol active in the context @var{ctx}. As of now this function does only work for OpenPGP and requires at least version 2.1.13 of GnuPG. @var{userid} is commonly the mail address associated with the key. GPGME does not require a specificy syntax but if more than a mail address is given, RFC-822 style format is suggested. The value is expected to be in UTF-8 encoding (i.e. no IDN encoding for mail addresses). This is a required parameter. @var{algo} specifies the algorithm for the new key (actually a keypair of public and private key). For a list of supported algorithms, see the GnuPG manual. If @var{algo} is @code{NULL} or the string "default", the key is generated using the default algorithm of the engine. If the string "future-default" is used the engine may use an algorithm which is planned to be the default in a future release of the engine; however existing implementation of the protocol may not be able to already handle such future algorithms. For the OpenPGP protocol, the specification of a default algorithm, without requesting a non-default usage via @var{flags}, triggers the creation of a primary key plus a secondary key (subkey). @var{reserved} must be set to zero. @var{expires} specifies the expiration time in seconds. If you supply 0, a reasonable expiration time is chosen. Use the flag @code{GPGME_CREATE_NOEXPIRE} to create keys that do not expire. Note that this parameter takes an unsigned long value and not a @code{time_t} to avoid problems on systems which use a signed 32 bit @code{time_t}. Note further that the OpenPGP protocol uses 32 bit values for timestamps and thus can only encode dates up to the year 2106. @var{extrakey} is currently not used and must be set to @code{NULL}. A future version of GPGME may use this parameter to create X.509 keys. @var{flags} can be set to the bit-wise OR of the following flags: @table @code @item GPGME_CREATE_SIGN @itemx GPGME_CREATE_ENCR @itemx GPGME_CREATE_CERT @itemx GPGME_CREATE_AUTH @since{1.7.0} Do not create the key with the default capabilities (key usage) of the requested algorithm but use those explicitly given by these flags: ``signing'', ``encryption'', ``certification'', or ``authentication''. The allowed combinations depend on the algorithm. If any of these flags are set and a default algorithm has been selected only one key is created in the case of the OpenPGP protocol. @item GPGME_CREATE_NOPASSWD @since{1.7.0} Request generation of the key without password protection. @item GPGME_CREATE_SELFSIGNED @since{1.7.0} For an X.509 key do not create a CSR but a self-signed certificate. This has not yet been implemented. @item GPGME_CREATE_NOSTORE @since{1.7.0} Do not store the created key in the local key database. This has not yet been implemented. @item GPGME_CREATE_WANTPUB @itemx GPGME_CREATE_WANTSEC @since{1.7.0} Return the public or secret key as part of the result structure. This has not yet been implemented. @item GPGME_CREATE_FORCE @since{1.7.0} The engine does not allow the creation of a key with a user ID already existing in the local key database. This flag can be used to override this check. @item GPGME_CREATE_NOEXPIRE @since{1.8.0} Request generation of keys that do not expire. @end table After the operation completed successfully, information about the created key can be retrieved with @code{gpgme_op_genkey_result}. The function returns zero on success, @code{GPG_ERR_NOT_SUPPORTED} if the engine does not support the command, or a bunch of other error codes. @end deftypefun @deftypefun gpgme_error_t gpgme_op_createkey_start @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{const char *@var{userid}}, @ @w{const char *@var{algo}}, @ @w{unsigned long @var{reserved}}, @ @w{unsigned long @var{expires}}, @ @w{gpgme_key_t @var{extrakey}}, @ @w{unsigned int @var{flags}}); @since{1.7.0} The function @code{gpgme_op_createkey_start} initiates a @code{gpgme_op_createkey} operation; see there for details. It must be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. @end deftypefun @c @c gpgme_op_createsubkey @c @deftypefun gpgme_error_t gpgme_op_createsubkey @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{gpgme_key_t @var{key}}, @ @w{const char *@var{algo}}, @ @w{unsigned long @var{reserved}}, @ @w{unsigned long @var{expires}}, @ @w{unsigned int @var{flags}}); @since{1.7.0} The function @code{gpgme_op_createsubkey} creates and adds a new subkey to the primary OpenPGP key given by @var{KEY}. The only allowed protocol in @var{ctx} is @code{GPGME_PROTOCOL_OPENPGP}. Subkeys (aka secondary keys) are a concept in the OpenPGP protocol to bind several keys to a primary key. As of now this function requires at least version 2.1.13 of GnuPG. @var{key} specifies the key to operate on. @var{algo} specifies the algorithm for the new subkey. For a list of supported algorithms, see the GnuPG manual. If @var{algo} is @code{NULL} or the string "default", the subkey is generated using the default algorithm for an encryption subkey of the engine. If the string "future-default" is used the engine may use an encryption algorithm which is planned to be the default in a future release of the engine; however existing implementation of the protocol may not be able to already handle such future algorithms. @var{reserved} must be set to zero. @var{expires} specifies the expiration time in seconds. If you supply 0, a reasonable expiration time is chosen. Use the flag @code{GPGME_CREATE_NOEXPIRE} to create keys that do not expire. Note that this parameter takes an unsigned long value and not a @code{time_t} to avoid problems on systems which use a signed 32 bit @code{time_t}. Note further that the OpenPGP protocol uses 32 bit values for timestamps and thus can only encode dates up to the year 2106. @var{flags} takes the same values as described above for @code{gpgme_op_createkey}. After the operation completed successfully, information about the created key can be retrieved with @code{gpgme_op_genkey_result}. The function returns zero on success, @code{GPG_ERR_NOT_SUPPORTED} if the engine does not support the command, or a bunch of other error codes. @end deftypefun @deftypefun gpgme_error_t gpgme_op_createsubkey_start @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{gpgme_key_t @var{key}}, @ @w{const char *@var{algo}}, @ @w{unsigned long @var{reserved}}, @ @w{unsigned long @var{expires}}, @ @w{unsigned int @var{flags}}); @since{1.7.0} The function @code{gpgme_op_createsubkey_start} initiates a @code{gpgme_op_createsubkey} operation; see there for details. It must be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. @end deftypefun @c @c gpgme_op_adduid @c @deftypefun gpgme_error_t gpgme_op_adduid @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{gpgme_key_t @var{key}}, @ @w{const char *@var{userid}}, @ @w{unsigned int @var{flags}}); @since{1.7.0} The function @code{gpgme_op_adduid} adds a new user ID to the OpenPGP key given by @var{KEY}. Adding additional user IDs after key creation is a feature of the OpenPGP protocol and thus the protocol for the context @var{ctx} must be set to OpenPGP. As of now this function requires at least version 2.1.13 of GnuPG. @var{key} specifies the key to operate on. @var{userid} is the user ID to add to the key. A user ID is commonly the mail address to be associated with the key. GPGME does not require a specificy syntax but if more than a mail address is given, RFC-822 style format is suggested. The value is expected to be in UTF-8 encoding (i.e. no IDN encoding for mail addresses). This is a required parameter. @var{flags} are currently not used and must be set to zero. The function returns zero on success, @code{GPG_ERR_NOT_SUPPORTED} if the engine does not support the command, or a bunch of other error codes. @end deftypefun @deftypefun gpgme_error_t gpgme_op_adduid_start @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{gpgme_key_t @var{key}}, @ @w{const char *@var{userid}}, @ @w{unsigned int @var{flags}}); @since{1.7.0} The function @code{gpgme_op_adduid_start} initiates a @code{gpgme_op_adduid} operation; see there for details. It must be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. @end deftypefun @c @c gpgme_op_revuid @c @deftypefun gpgme_error_t gpgme_op_revuid @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{gpgme_key_t @var{key}}, @ @w{const char *@var{userid}}, @ @w{unsigned int @var{flags}}); @since{1.7.0} The function @code{gpgme_op_revuid} revokes a user ID from the OpenPGP key given by @var{KEY}. Revoking user IDs after key creation is a feature of the OpenPGP protocol and thus the protocol for the context @var{ctx} must be set to OpenPGP. As of now this function requires at least version 2.1.13 of GnuPG. @var{key} specifies the key to operate on. @var{userid} is the user ID to be revoked from the key. The user ID must be given verbatim because the engine does an exact and case sensitive match. Thus the @code{uid} field from the user ID object (@code{gpgme_user_id_t}) is to be used. This is a required parameter. @var{flags} are currently not used and must be set to zero. Note that the engine won't allow to revoke the last valid user ID. To change a user ID is better to first add the new user ID, then revoke the old one, and finally publish the key. The function returns zero on success, @code{GPG_ERR_NOT_SUPPORTED} if the engine does not support the command, or a bunch of other error codes. @end deftypefun @deftypefun gpgme_error_t gpgme_op_revuid_start @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{gpgme_key_t @var{key}}, @ @w{const char *@var{userid}}, @ @w{unsigned int @var{flags}}); @since{1.7.0} The function @code{gpgme_op_revuid_start} initiates a @code{gpgme_op_revuid} operation; see there for details. It must be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. @end deftypefun @c @c gpgme_op_set_uid_flag @c @deftypefun gpgme_error_t gpgme_op_set_ui_flag @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{gpgme_key_t @var{key}}, @ @w{const char *@var{userid}}, @ @w{cons char * @var{name}}, @ @w{cons char * @var{value}}); @since{1.8.0} The function @code{gpgme_op_set_uid_flag} is used to set flags on a user ID from the OpenPGP key given by @var{KEY}. Setting flags on user IDs after key creation is a feature of the OpenPGP protocol and thus the protocol for the context @var{ctx} must be set to OpenPGP. @var{key} specifies the key to operate on. This parameters is required. @var{userid} is the user ID of the key to be manipulated. This user ID must be given verbatim because the engine does an exact and case sensitive match. Thus the @code{uid} field from the user ID object (@code{gpgme_user_id_t}) is to be used. This is a required parameter. @var{name} names the flag which is to be changed. The only currently supported flag is: @table @code @item primary This sets the primary key flag on the given user ID. All other primary key flag on other user IDs are removed. @var{value} must be given as NULL. For technical reasons this functions bumps the creation timestamp of all affected self-signatures up by one second. At least GnuPG version 2.1.20 is required. @end table The function returns zero on success, @code{GPG_ERR_NOT_SUPPORTED} if the engine does not support the command, or a bunch of other error codes. @end deftypefun @deftypefun gpgme_error_t gpgme_op_set_uid_flag_start @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{gpgme_key_t @var{key}}, @ @w{const char *@var{userid}}, @ @w{cons char * @var{name}}, @ @w{cons char * @var{value}}); @since{1.8.0} The function @code{gpgme_op_set_uid_flag_start} initiates a @code{gpgme_op_set_uid_flag} operation; see there for details. It must be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. @end deftypefun @c @c gpgme_op_genkey @c @deftypefun gpgme_error_t gpgme_op_genkey @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{const char *@var{parms}}, @ @w{gpgme_data_t @var{public}}, @ @w{gpgme_data_t @var{secret}}) The function @code{gpgme_op_genkey} generates a new key pair in the context @var{ctx}. The meaning of @var{public} and @var{secret} depends on the crypto backend. GPG does not support @var{public} and @var{secret}, they should be @code{NULL}. GnuPG will generate a key pair and add it to the standard key ring. The fingerprint of the generated key is available with @code{gpgme_op_genkey_result}. GpgSM requires @var{public} to be a writable data object. GpgSM will generate a secret key (which will be stored by @command{gpg-agent}, and return a certificate request in @var{public}, which then needs to be signed by the certification authority and imported before it can be used. GpgSM does not make the fingerprint available. The argument @var{parms} specifies parameters for the key in an string that looks something like XML. The details about the format of @var{parms} are specific to the crypto engine used by @var{ctx}. The first line of the parameters must be @code{} and the last line must be @code{}. Every line in between the first and last lines is treated as a Header: Value pair. In particular, no XML escaping is necessary if you need to include the characters @code{<}, @code{>}, or @code{&}. Here is an example for GnuPG as the crypto engine (all parameters of OpenPGP key generation are documented in the GPG manual): @example Key-Type: default Subkey-Type: default Name-Real: Joe Tester Name-Comment: with stupid passphrase Name-Email: joe@@foo.bar Expire-Date: 0 Passphrase: abc @end example Here is an example for GpgSM as the crypto engine (all parameters of OpenPGP key generation are documented in the GPGSM manual): @example Key-Type: RSA Key-Length: 1024 Name-DN: C=de,O=g10 code,OU=Testlab,CN=Joe 2 Tester Name-Email: joe@@foo.bar @end example Strings should be given in UTF-8 encoding. The only format supported for now is ``internal''. The content of the @code{GnupgKeyParms} container is passed verbatim to the crypto backend. Control statements are not allowed. After the operation completed successfully, the result can be retrieved with @code{gpgme_op_genkey_result}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation could be started successfully, @code{GPG_ERR_INV_VALUE} if @var{parms} is not a well-formed string (e.g. does not have the expected tag-like headers and footers), @code{GPG_ERR_NOT_SUPPORTED} if @var{public} or @var{secret} is not valid, and @code{GPG_ERR_GENERAL} if no key was created by the backend. @end deftypefun @deftypefun gpgme_error_t gpgme_op_genkey_start (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{parms}}, @w{gpgme_data_t @var{public}}, @w{gpgme_data_t @var{secret}}) The function @code{gpgme_op_genkey_start} initiates a @code{gpgme_op_genkey} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation could be started successfully, @code{GPG_ERR_INV_VALUE} if @var{parms} is not a valid XML string, and @code{GPG_ERR_NOT_SUPPORTED} if @var{public} or @var{secret} is not @code{NULL}. @end deftypefun @c @c gpgme_op_genkey_result @c @deftp {Data type} {gpgme_genkey_result_t} This is a pointer to a structure used to store the result of a @code{gpgme_op_genkey} operation. After successfully generating a key, you can retrieve the pointer to the result with @code{gpgme_op_genkey_result}. The structure contains the following members: @table @code @item unsigned int primary : 1 This flag is set to 1 if a primary key was created and to 0 if not. @item unsigned int sub : 1 This flag is set to 1 if a subkey was created and to 0 if not. @item unsigned int uid : 1 This flag is set to 1 if a user ID was created and to 0 if not. @item char *fpr This is the fingerprint of the key that was created. If both a primary and a subkey were generated, the fingerprint of the primary key will be returned. If the crypto engine does not provide the fingerprint, @code{fpr} will be a null pointer. @item gpgme_data_t pubkey @since{1.7.0} This will eventually be used to return the public key. It is currently not used. @item gpgme_data_t seckey @since{1.7.0} This will eventually be used to return the secret key. It is currently not used. @end table @end deftp @deftypefun gpgme_genkey_result_t gpgme_op_genkey_result (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_op_genkey_result} returns a @code{gpgme_genkey_result_t} pointer to a structure holding the result of a @code{gpgme_op_genkey} operation. The pointer is only valid if the last operation on the context was a @code{gpgme_op_genkey} or @code{gpgme_op_genkey_start} operation, and if this operation finished successfully. The returned pointer is only valid until the next operation is started on the context. @end deftypefun @c @c SIGNING KEYS @c @node Signing Keys @subsection Signing Keys @cindex key, signing Key signatures are a unique concept of the OpenPGP protocol. They can be used to certify the validity of a key and are used to create the Web-of-Trust (WoT). Instead of using the @code{gpgme_op_interact} function along with a finite state machine, GPGME provides a convenient function to create key signatures when using modern GnuPG versions. @c @c gpgme_op_keysign @c @deftypefun gpgme_error_t gpgme_op_keysign @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{gpgme_key_t @var{key}}, @ @w{const char *@var{userid}}, @ @w{unsigned long @var{expires}}, @ @w{unsigned int @var{flags}}); @since{1.7.0} The function @code{gpgme_op_keysign} adds a new key signature to the public key @var{KEY}. This function requires at least version 2.1.12 of GnuPG. @var{CTX} is the usual context which describes the protocol to use (which must be OpenPGP) and has also the list of signer keys to be used for the signature. The common case is to use the default key for signing other keys. If another key or more than one key shall be used for a key signature, @code{gpgme_signers_add} can be used. @xref{Selecting Signers}. @var{key} specifies the key to operate on. @var{userid} selects the user ID or user IDs to be signed. If @var{userid} is set to @code{NULL} all valid user IDs are signed. The user ID must be given verbatim because the engine does an exact and case sensitive match. Thus the @code{uid} field from the user ID object (@code{gpgme_user_id_t}) is to be used. To select more than one user ID put them all into one string separated by linefeeds characters (@code{\n}) and set the flag @code{GPGME_KEYSIGN_LFSEP}. @var{expires} specifies the expiration time of the new signature in seconds. The common case is to use 0 to not set an expiration date. However, if the configuration of the engine defines a default expiration for key signatures, that is still used unless the flag @code{GPGME_KEYSIGN_NOEXPIRE} is used. Note that this parameter takes an unsigned long value and not a @code{time_t} to avoid problems on systems which use a signed 32 bit @code{time_t}. Note further that the OpenPGP protocol uses 32 bit values for timestamps and thus can only encode dates up to the year 2106. @var{flags} can be set to the bit-wise OR of the following flags: @table @code @item GPGME_KEYSIGN_LOCAL @since{1.7.0} Instead of creating an exportable key signature, create a key signature which is is marked as non-exportable. @item GPGME_KEYSIGN_LFSEP @since{1.7.0} Although linefeeds are uncommon in user IDs this flag is required to explicitly declare that @var{userid} may contain several linefeed separated user IDs. @item GPGME_KEYSIGN_NOEXPIRE Force the creation of a key signature without an expiration date. This overrides @var{expire} and any local configuration of the engine. @end table The function returns zero on success, @code{GPG_ERR_NOT_SUPPORTED} if the engine does not support the command, or a bunch of other error codes. @end deftypefun @deftypefun gpgme_error_t gpgme_op_keysign_start @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{gpgme_key_t @var{key}}, @ @w{const char *@var{userid}}, @ @w{unsigned long @var{expires}}, @ @w{unsigned int @var{flags}}); @since{1.7.0} The function @code{gpgme_op_keysign_start} initiates a @code{gpgme_op_keysign} operation; see there for details. It must be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. @end deftypefun @c @c EXPORTING KEYS @c @node Exporting Keys @subsection Exporting Keys @cindex key, export @cindex key ring, export from Exporting keys means the same as running @command{gpg} with the command @option{--export}. However, a mode flag can be used to change the way the export works. The available mode flags are described below, they may be or-ed together. @table @code @item GPGME_EXPORT_MODE_EXTERN If this bit is set, the output is send directly to the default keyserver. This is currently only allowed for OpenPGP keys. It is good practise to not send more than a few dozens key to a keyserver at one time. Using this flag requires that the @var{keydata} argument of the export function is set to @code{NULL}. @item GPGME_EXPORT_MODE_MINIMAL @since{1.3.1} If this bit is set, the smallest possible key is exported. For OpenPGP keys it removes all signatures except for the latest self-signatures. For X.509 keys it has no effect. @item GPGME_EXPORT_MODE_SECRET @since{1.6.0} Instead of exporting the public key, the secret key is exported. This may not be combined with @code{GPGME_EXPORT_MODE_EXTERN}. For X.509 the export format is PKCS#8. @item GPGME_EXPORT_MODE_RAW @since{1.6.0} If this flag is used with @code{GPGME_EXPORT_MODE_SECRET} for an X.509 key the export format will be changed to PKCS#1. This flag may not be used with OpenPGP. @item GPGME_EXPORT_MODE_PKCS12 @since{1.6.0} If this flag is used with @code{GPGME_EXPORT_MODE_SECRET} for an X.509 key the export format will be changed to PKCS#12 which also includes the certificate. This flag may not be used with OpenPGP. @end table @deftypefun gpgme_error_t gpgme_op_export (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{pattern}}, @w{gpgme_export_mode_t @var{mode}}, @w{gpgme_data_t @var{keydata}}) The function @code{gpgme_op_export} extracts public keys and returns them in the data buffer @var{keydata}. The output format of the key data returned is determined by the @acronym{ASCII} armor attribute set for the context @var{ctx}, or, if that is not set, by the encoding specified for @var{keydata}. If @var{pattern} is @code{NULL}, all available keys are returned. Otherwise, @var{pattern} contains an engine specific expression that is used to limit the list to all keys matching the pattern. @var{mode} is usually 0; other values are described above. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation completed successfully, @code{GPG_ERR_INV_VALUE} if @var{keydata} is not a valid empty data buffer, and passes through any errors that are reported by the crypto engine support routines. @end deftypefun @deftypefun gpgme_error_t gpgme_op_export_start (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{pattern}}, @w{gpgme_export_mode_t @var{mode}}, @w{gpgme_data_t @var{keydata}}) The function @code{gpgme_op_export_start} initiates a @code{gpgme_op_export} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation could be started successfully, and @code{GPG_ERR_INV_VALUE} if @var{keydata} is not a valid empty data buffer. @end deftypefun @deftypefun gpgme_error_t gpgme_op_export_ext (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{pattern}[]}, @w{gpgme_export_mode_t @var{mode}}, @w{gpgme_data_t @var{keydata}}) The function @code{gpgme_op_export} extracts public keys and returns them in the data buffer @var{keydata}. The output format of the key data returned is determined by the @acronym{ASCII} armor attribute set for the context @var{ctx}, or, if that is not set, by the encoding specified for @var{keydata}. If @var{pattern} or @var{*pattern} is @code{NULL}, all available keys are returned. Otherwise, @var{pattern} is a @code{NULL} terminated array of strings that are used to limit the list to all keys matching at least one of the patterns verbatim. @var{mode} is usually 0; other values are described above. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation completed successfully, @code{GPG_ERR_INV_VALUE} if @var{keydata} is not a valid empty data buffer, and passes through any errors that are reported by the crypto engine support routines. @end deftypefun @deftypefun gpgme_error_t gpgme_op_export_ext_start (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{pattern}[]}, @w{gpgme_export_mode_t @var{mode}}, @w{gpgme_data_t @var{keydata}}) The function @code{gpgme_op_export_ext_start} initiates a @code{gpgme_op_export_ext} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation could be started successfully, and @code{GPG_ERR_INV_VALUE} if @var{keydata} is not a valid empty data buffer. @end deftypefun @deftypefun gpgme_error_t gpgme_op_export_keys (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t keys[]}, @w{gpgme_export_mode_t @var{mode}}, @w{gpgme_data_t @var{keydata}}) @since{1.2.0} The function @code{gpgme_op_export_keys} extracts public keys and returns them in the data buffer @var{keydata}. The output format of the key data returned is determined by the @acronym{ASCII} armor attribute set for the context @var{ctx}, or, if that is not set, by the encoding specified for @var{keydata}. The keys to export are taken form the @code{NULL} terminated array @var{keys}. Only keys of the currently selected protocol of @var{ctx} which do have a fingerprint set are considered for export. Other keys specified by the @var{keys} are ignored. In particular OpenPGP keys retrieved via an external key listing are not included. @var{mode} is usually 0; other values are described above. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation completed successfully, @code{GPG_ERR_INV_VALUE} if @var{keydata} is not a valid empty data buffer, @code{GPG_ERR_NO_DATA} if no useful keys are in @var{keys} and passes through any errors that are reported by the crypto engine support routines. @end deftypefun @deftypefun gpgme_error_t gpgme_op_export_keys_start (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t @var{keys}[]}, @w{gpgme_export_mode_t @var{mode}}, @w{gpgme_data_t @var{keydata}}) @since{1.2.0} The function @code{gpgme_op_export_keys_start} initiates a @code{gpgme_op_export_ext} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation could be started successfully, and @code{GPG_ERR_INV_VALUE} if @var{keydata} is not a valid empty data buffer, @code{GPG_ERR_NO_DATA} if no useful keys are in @var{keys} and passes through any errors that are reported by the crypto engine support routines. @end deftypefun @node Importing Keys @subsection Importing Keys @cindex key, import @cindex key ring, import to Importing keys means the same as running @command{gpg} with the command @option{--import}. @deftypefun gpgme_error_t gpgme_op_import (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_data_t @var{keydata}}) The function @code{gpgme_op_import} adds the keys in the data buffer @var{keydata} to the key ring of the crypto engine used by @var{ctx}. The format of @var{keydata} can be @acronym{ASCII} armored, for example, but the details are specific to the crypto engine. After the operation completed successfully, the result can be retrieved with @code{gpgme_op_import_result}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the import was completed successfully, @code{GPG_ERR_INV_VALUE} if @var{keydata} if @var{ctx} or @var{keydata} is not a valid pointer, and @code{GPG_ERR_NO_DATA} if @var{keydata} is an empty data buffer. @end deftypefun @deftypefun gpgme_error_t gpgme_op_import_start (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_data_t @var{keydata}}) The function @code{gpgme_op_import_start} initiates a @code{gpgme_op_import} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the import could be started successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx} or @var{keydata} is not a valid pointer, and @code{GPG_ERR_NO_DATA} if @var{keydata} is an empty data buffer. @end deftypefun @deftypefun gpgme_error_t gpgme_op_import_keys (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t *@var{keys}}) @since{1.2.0} The function @code{gpgme_op_import_keys} adds the keys described by the @code{NULL} terminated array @var{keys} to the key ring of the crypto engine used by @var{ctx}. It is used to actually import and make keys permanent which have been retrieved from an external source (i.e. using @code{GPGME_KEYLIST_MODE_EXTERN}) earlier. The external keylisting must have been made with the same context configuration (in particular the same home directory). @footnote{Thus it is a replacement for the usual workaround of exporting and then importing a key to make an X.509 key permanent.} Note that for OpenPGP this may require another access to the keyserver over the network. Only keys of the currently selected protocol of @var{ctx} are considered for import. Other keys specified by the @var{keys} are ignored. As of now all considered keys must have been retrieved using the same method, i.e. the used key listing mode must be identical. After the operation completed successfully, the result can be retrieved with @code{gpgme_op_import_result}. To move keys from one home directory to another, export and import the keydata using @code{gpgme_op_export} and @code{gpgme_op_import}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the import was completed successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx} is not a valid pointer, @code{GPG_ERR_CONFLICT} if the key listing mode does not match, and @code{GPG_ERR_NO_DATA} if no keys are considered for export. @end deftypefun @deftypefun gpgme_error_t gpgme_op_import_keys_start (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t *@var{keys}}) @since{1.2.0} The function @code{gpgme_op_import_keys_start} initiates a @code{gpgme_op_import_keys} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the import was completed successfully, @code{GPG_ERR_INV_VALUE} if @var{keydata} if @var{ctx} or @var{keydata} is not a valid pointer, @code{GPG_ERR_CONFLICT} if the key listing mode does not match, and @code{GPG_ERR_NO_DATA} if no keys are considered for export. @end deftypefun @deftp {Data type} {gpgme_import_status_t} This is a pointer to a structure used to store a part of the result of a @code{gpgme_op_import} operation. For each considered key one status is added that contains information about the result of the import. The structure contains the following members: @table @code @item gpgme_import_status_t next This is a pointer to the next status structure in the linked list, or @code{NULL} if this is the last element. @item char *fpr This is the fingerprint of the key that was considered. @item gpgme_error_t result If the import was not successful, this is the error value that caused the import to fail. Otherwise the error code is @code{GPG_ERR_NO_ERROR}. @item unsigned int status This is a bit-wise OR of the following flags that give more information about what part of the key was imported. If the key was already known, this might be 0. @table @code @item GPGME_IMPORT_NEW The key was new. @item GPGME_IMPORT_UID The key contained new user IDs. @item GPGME_IMPORT_SIG The key contained new signatures. @item GPGME_IMPORT_SUBKEY The key contained new sub keys. @item GPGME_IMPORT_SECRET The key contained a secret key. @end table @end table @end deftp @deftp {Data type} {gpgme_import_result_t} This is a pointer to a structure used to store the result of a @code{gpgme_op_import} operation. After a successful import operation, you can retrieve the pointer to the result with @code{gpgme_op_import_result}. The structure contains the following members: @table @code @item int considered The total number of considered keys. @item int no_user_id The number of keys without user ID. @item int imported The total number of imported keys. @item int imported_rsa The number of imported RSA keys. @item int unchanged The number of unchanged keys. @item int new_user_ids The number of new user IDs. @item int new_sub_keys The number of new sub keys. @item int new_signatures The number of new signatures. @item int new_revocations The number of new revocations. @item int secret_read The total number of secret keys read. @item int secret_imported The number of imported secret keys. @item int secret_unchanged The number of unchanged secret keys. @item int not_imported The number of keys not imported. @item gpgme_import_status_t imports A list of gpgme_import_status_t objects which contain more information about the keys for which an import was attempted. @item int skipped_v3_keys For security reasons modern versions of GnuPG do not anymore support v3 keys (created with PGP 2.x) and ignores them on import. This counter provides the number of such skipped v3 keys. @end table @end deftp @deftypefun gpgme_import_result_t gpgme_op_import_result (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_op_import_result} returns a @code{gpgme_import_result_t} pointer to a structure holding the result of a @code{gpgme_op_import} operation. The pointer is only valid if the last operation on the context was a @code{gpgme_op_import} or @code{gpgme_op_import_start} operation, and if this operation finished successfully. The returned pointer is only valid until the next operation is started on the context. @end deftypefun @node Deleting Keys @subsection Deleting Keys @cindex key, delete @cindex key ring, delete from @deftypefun gpgme_error_t gpgme_op_delete_ext (@w{gpgme_ctx_t @var{ctx}}, @w{const gpgme_key_t @var{key}}, @w{unsigned int @var{flags}}) @since{1.9.1} The function @code{gpgme_op_delete_ext} deletes the key @var{key} from the key ring of the crypto engine used by @var{ctx}. @var{flags} can be set to the bit-wise OR of the following flags: @table @code @item GPGME_DELETE_ALLOW_SECRET @since{1.9.1} If not set, only public keys are deleted. If set, secret keys are deleted as well, if that is supported. @item GPGME_DELETE_FORCE @since{1.9.1} If set, the user is not asked to confirm the deletion. @end table The function returns the error code @code{GPG_ERR_NO_ERROR} if the key was deleted successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx} or @var{key} is not a valid pointer, @code{GPG_ERR_NO_PUBKEY} if @var{key} could not be found in the keyring, @code{GPG_ERR_AMBIGUOUS_NAME} if the key was not specified unambiguously, and @code{GPG_ERR_CONFLICT} if the secret key for @var{key} is available, but @var{allow_secret} is zero. @end deftypefun @deftypefun gpgme_error_t gpgme_op_delete_ext_start (@w{gpgme_ctx_t @var{ctx}}, @w{const gpgme_key_t @var{key}}, @w{unsigned int @var{flags}}) @since{1.9.1} The function @code{gpgme_op_delete_ext_start} initiates a @code{gpgme_op_delete} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation was started successfully, and @code{GPG_ERR_INV_VALUE} if @var{ctx} or @var{key} is not a valid pointer. @end deftypefun The following functions allow only to use one particular flag. @deftypefun gpgme_error_t gpgme_op_delete (@w{gpgme_ctx_t @var{ctx}}, @w{const gpgme_key_t @var{key}}, @w{int @var{allow_secret}}) Similar to @code{gpgme_op_delete_ext}, but only the flag @code{GPGME_DELETE_ALLOW_SECRET} can be provided. @end deftypefun @deftypefun gpgme_error_t gpgme_op_delete_start (@w{gpgme_ctx_t @var{ctx}}, @w{const gpgme_key_t @var{key}}, @w{int @var{allow_secret}}) Similar to @code{gpgme_op_delete_ext_start}, but only the flag @code{GPGME_DELETE_ALLOW_SECRET} can be provided. @end deftypefun @node Changing Passphrases @subsection Changing Passphrases @cindex passphrase, change @deftypefun gpgme_error_t gpgme_op_passwd @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{const gpgme_key_t @var{key}}, @ @w{unsigned int @var{flags}}) @since{1.3.0} The function @code{gpgme_op_passwd} changes the passphrase of the private key associated with @var{key}. The only allowed value for @var{flags} is @code{0}. The backend engine will usually popup a window to ask for the old and the new passphrase. Thus this function is not useful in a server application (where passphrases are not required anyway). Note that old @code{gpg} engines (before version 2.0.15) do not support this command and will silently ignore it. @end deftypefun @deftypefun gpgme_error_t gpgme_op_passwd_start @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{const gpgme_key_t @var{key}}, @ @w{unsigned int @var{flags}}) @since{1.3.0} The function @code{gpgme_op_passwd_start} initiates a @code{gpgme_op_passwd} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns @code{0} if the operation was started successfully, and an error code if one of the arguments is not valid or the oepration could not be started. @end deftypefun @c @c CHANGING TOFU DATA @c @node Changing TOFU Data @subsection Changing TOFU Data @cindex validity, TOFU The OpenPGP engine features a Trust-On-First-Use (TOFU) key validation model. For resolving conflicts it is necessary to declare the policy for a key. See the GnuPG manual for details on the TOFU implementation. @deftp {Data type} {enum gpgme_tofu_policy_t} @since{1.7.0} @tindex gpgme_tofu_policy_t The @code{gpgme_tofu_policy_t} type specifies the set of possible policy values that are supported by @acronym{GPGME}: @table @code @item GPGME_TOFU_POLICY_AUTO Set the policy to ``auto''. @item GPGME_TOFU_POLICY_GOOD Set the policy to ``good''. @item GPGME_TOFU_POLICY_BAD Set the policy to ``bad''. @item GPGME_TOFU_POLICY_ASK Set the policy to ``ask''. @item GPGME_TOFU_POLICY_UNKNOWN Set the policy to ``unknown''. @end table @end deftp To change the policy for a key the following functions can be used: @deftypefun gpgme_error_t gpgme_op_tofu_policy @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{const gpgme_key_t @var{key}}, @ @w{gpgme_tofu_policy_t @var{policy}}) @since{1.7.0} The function @code{gpgme_op_tofu_policy} changes the TOFU policy of @var{key}. The valid values for @var{policy} are listed above. As of now this function does only work for OpenPGP and requires at least version 2.1.10 of GnuPG. The function returns zero on success, @code{GPG_ERR_NOT_SUPPORTED} if the engine does not support the command, or a bunch of other error codes. @end deftypefun @deftypefun gpgme_error_t gpgme_op_tofu_policy_start @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{const gpgme_key_t @var{key}}, @ @w{gpgme_tofu_policy_t @var{policy}}) @since{1.7.0} The function @code{gpgme_op_tofu_policy_start} initiates a @code{gpgme_op_tofu_policy} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns @code{0} if the operation was started successfully, and an error code if one of the arguments is not valid or the oepration could not be started. @end deftypefun @node Advanced Key Editing @subsection Advanced Key Editing @cindex key, edit @deftp {Data type} {gpgme_error_t (*gpgme_interact_cb_t) @ (@w{void *@var{handle}}, @ @w{const char *@var{status}}, @ @w{const char *@var{args}}, @ @w{int @var{fd}})} @since{1.7.0} @tindex gpgme_interact_cb_t The @code{gpgme_interact_cb_t} type is the type of functions which @acronym{GPGME} calls if it a key interact operation is on-going. The status keyword @var{status} and the argument line @var{args} are passed through by @acronym{GPGME} from the crypto engine. An empty string represents EOF. The file descriptor @var{fd} is -1 for normal status messages. If @var{status} indicates a command rather than a status message, the response to the command should be written to @var{fd}. The @var{handle} is provided by the user at start of operation. The function should return @code{GPG_ERR_FALSE} if it did not handle the status code, @code{0} for success, or any other error value. @end deftp @deftypefun gpgme_error_t gpgme_op_interact (@w{gpgme_ctx_t @var{ctx}}, @ @w{gpgme_key_t @var{key}}, @ @w{unsigned int @var{flags}}, @ @w{gpgme_interact_cb_t @var{fnc}}, @ @w{void *@var{handle}}, @ @w{gpgme_data_t @var{out}}) @since{1.7.0} The function @code{gpgme_op_interact} processes the key @var{KEY} interactively, using the interact callback function @var{FNC} with the handle @var{HANDLE}. The callback is invoked for every status and command request from the crypto engine. The output of the crypto engine is written to the data object @var{out}. Note that the protocol between the callback function and the crypto engine is specific to the crypto engine and no further support in implementing this protocol correctly is provided by @acronym{GPGME}. @var{flags} modifies the behaviour of the function; the only defined bit value is: @table @code @item GPGME_INTERACT_CARD @since{1.7.0} This is used for smartcard based keys and uses gpg’s @code{--card-edit} command. @end table The function returns @code{0} if the edit operation completes successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx} or @var{key} is not a valid pointer, and any error returned by the crypto engine or the edit callback handler. @end deftypefun @deftypefun gpgme_error_t gpgme_op_interact_start (@w{gpgme_ctx_t @var{ctx}}, @ @w{gpgme_key_t @var{key}}, @ @w{unsigned int @var{flags}}, @ @w{gpgme_interact_cb_t @var{fnc}}, @ @w{void *@var{handle}}, @ @w{gpgme_data_t @var{out}}) @since{1.7.0} The function @code{gpgme_op_interact_start} initiates a @code{gpgme_op_interact} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns @code{0} if the operation was started successfully, and @code{GPG_ERR_INV_VALUE} if @var{ctx} or @var{key} is not a valid pointer. @end deftypefun @node Trust Item Management @section Trust Item Management @cindex trust item @strong{Caution:} The trust items interface is experimental. @deftp {Data type} gpgme_trust_item_t The @code{gpgme_trust_item_t} type is a pointer to a trust item object. It has the following members: @table @code @item char *keyid This is a string describing the key to which this trust items belongs. @item int type This is the type of the trust item. A value of 1 refers to a key, a value of 2 refers to a user ID. @item int level This is the trust level. @item char *owner_trust The owner trust if @code{type} is 1. @item char *validity The calculated validity. @item char *name The user name if @code{type} is 2. @end table @end deftp @menu * Listing Trust Items:: Browsing the list of available trust items. * Manipulating Trust Items:: Operations on trust items. @end menu @node Listing Trust Items @subsection Listing Trust Items @cindex trust item list @deftypefun gpgme_error_t gpgme_op_trustlist_start (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{pattern}}, @w{int @var{max_level}}) The function @code{gpgme_op_trustlist_start} initiates a trust item listing operation inside the context @var{ctx}. It sets everything up so that subsequent invocations of @code{gpgme_op_trustlist_next} return the trust items in the list. The string @var{pattern} contains an engine specific expression that is used to limit the list to all trust items matching the pattern. It can not be the empty string. The argument @var{max_level} is currently ignored. The context will be busy until either all trust items are received (and @code{gpgme_op_trustlist_next} returns @code{GPG_ERR_EOF}), or @code{gpgme_op_trustlist_end} is called to finish the operation. The function returns the error code @code{GPG_ERR_INV_VALUE} if @var{ctx} is not a valid pointer, and passes through any errors that are reported by the crypto engine support routines. @end deftypefun @deftypefun gpgme_error_t gpgme_op_trustlist_next (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_trust_item_t *@var{r_item}}) The function @code{gpgme_op_trustlist_next} returns the next trust item in the list created by a previous @code{gpgme_op_trustlist_start} operation in the context @var{ctx}. The trust item can be destroyed with @code{gpgme_trust_item_release}. @xref{Manipulating Trust Items}. This is the only way to get at @code{gpgme_trust_item_t} objects in @acronym{GPGME}. If the last trust item in the list has already been returned, @code{gpgme_op_trustlist_next} returns @code{GPG_ERR_EOF}. The function returns the error code @code{GPG_ERR_INV_VALUE} if @var{ctx} or @var{r_item} is not a valid pointer, and @code{GPG_ERR_ENOMEM} if there is not enough memory for the operation. @end deftypefun @deftypefun gpgme_error_t gpgme_op_trustlist_end (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_op_trustlist_end} ends a pending trust list operation in the context @var{ctx}. The function returns the error code @code{GPG_ERR_INV_VALUE} if @var{ctx} is not a valid pointer, and @code{GPG_ERR_ENOMEM} if at some time during the operation there was not enough memory available. @end deftypefun @node Manipulating Trust Items @subsection Manipulating Trust Items @cindex trust item, manipulation @deftypefun void gpgme_trust_item_ref (@w{gpgme_trust_item_t @var{item}}) The function @code{gpgme_trust_item_ref} acquires an additional reference for the trust item @var{item}. @end deftypefun @deftypefun void gpgme_trust_item_unref (@w{gpgme_trust_item_t @var{item}}) The function @code{gpgme_trust_item_unref} releases a reference for the trust item @var{item}. If this was the last reference, the trust item will be destroyed and all resources associated to it will be released. @end deftypefun @node Crypto Operations @section Crypto Operations @cindex cryptographic operation Sometimes, the result of a crypto operation returns a list of invalid keys encountered in processing the request. The following structure is used to hold information about such a key. @deftp {Data type} {gpgme_invalid_key_t} This is a pointer to a structure used to store a part of the result of a crypto operation which takes user IDs as one input parameter. The structure contains the following members: @table @code @item gpgme_invalid_key_t next This is a pointer to the next invalid key structure in the linked list, or @code{NULL} if this is the last element. @item char *fpr The fingerprint or key ID of the invalid key encountered. @item gpgme_error_t reason An error code describing the reason why the key was found invalid. @end table @end deftp @menu * Decrypt:: Decrypting a ciphertext. * Verify:: Verifying a signature. * Decrypt and Verify:: Decrypting a signed ciphertext. * Sign:: Creating a signature. * Encrypt:: Encrypting a plaintext. @end menu @node Decrypt @subsection Decrypt @cindex decryption @cindex cryptographic operation, decryption @deftypefun gpgme_error_t gpgme_op_decrypt (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_data_t @var{cipher}}, @w{gpgme_data_t @var{plain}}) The function @code{gpgme_op_decrypt} decrypts the ciphertext in the data object @var{cipher} and stores it into the data object @var{plain}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the ciphertext could be decrypted successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx}, @var{cipher} or @var{plain} is not a valid pointer, @code{GPG_ERR_NO_DATA} if @var{cipher} does not contain any data to decrypt, @code{GPG_ERR_DECRYPT_FAILED} if @var{cipher} is not a valid cipher text, @code{GPG_ERR_BAD_PASSPHRASE} if the passphrase for the secret key could not be retrieved, and passes through some errors that are reported by the crypto engine support routines. @end deftypefun @deftypefun gpgme_error_t gpgme_op_decrypt_start (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_data_t @var{cipher}}, @w{gpgme_data_t @var{plain}}) The function @code{gpgme_op_decrypt_start} initiates a @code{gpgme_op_decrypt} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation could be started successfully, and @code{GPG_ERR_INV_VALUE} if @var{cipher} or @var{plain} is not a valid pointer. @end deftypefun @deftypefun gpgme_error_t gpgme_op_decrypt_ext ( @ @w{gpgme_ctx_t @var{ctx}}, @ @w{gpgme_decrypt_flags_t @var{flags}}, @ @w{gpgme_data_t @var{cipher}}, @ @w{gpgme_data_t @var{plain}}) @since{1.8.0} The function @code{gpgme_op_decrypt_ext} is the same as @code{gpgme_op_decrypt_ext} but has an additional argument @var{flags}. If @var{flags} is 0 both function behave identically. The value in @var{flags} is a bitwise-or combination of one or multiple of the following bit values: @table @code @item GPGME_DECRYPT_VERIFY @since{1.8.0} The @code{GPGME_DECRYPT_VERIFY} symbol specifies that this function shall exacty act as @code{gpgme_op_decrypt_verify}. @item GPGME_DECRYPT_UNWRAP @since{1.8.0} The @code{GPGME_DECRYPT_UNWRAP} symbol specifies that the output shall be an OpenPGP message with only the encryption layer removed. This requires GnuPG 2.1.12 and works only for OpenPGP. This is the counterpart to @code{GPGME_ENCRYPT_WRAP}. @end table The function returns the error codes as descriped for @code{gpgme_op_decrypt} respective @code{gpgme_op_encrypt}. @end deftypefun @deftypefun gpgme_error_t gpgme_op_decrypt_ext_start ( @ @w{gpgme_ctx_t @var{ctx}}, @ @w{gpgme_decrypt_flags_t @var{flags}}, @ @w{gpgme_data_t @var{cipher}}, @ @w{gpgme_data_t @var{plain}}) @since{1.8.0} The function @code{gpgme_op_decrypt_ext_start} initiates a @code{gpgme_op_decrypt_ext} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation could be started successfully, and @code{GPG_ERR_INV_VALUE} if @var{cipher} or @var{plain} is not a valid pointer. @end deftypefun @deftp {Data type} {gpgme_recipient_t} @since{1.1.0} This is a pointer to a structure used to store information about the recipient of an encrypted text which is decrypted in a @code{gpgme_op_decrypt} operation. This information (except for the status field) is even available before the operation finished successfully, for example in a passphrase callback. The structure contains the following members: @table @code @item gpgme_recipient_t next This is a pointer to the next recipient structure in the linked list, or @code{NULL} if this is the last element. @item gpgme_pubkey_algo_t The public key algorithm used in the encryption. @item char *keyid This is the key ID of the key (in hexadecimal digits) used as recipient. @item gpgme_error_t status This is an error number with the error code GPG_ERR_NO_SECKEY if the secret key for this recipient is not available, and 0 otherwise. @end table @end deftp @deftp {Data type} {gpgme_decrypt_result_t} This is a pointer to a structure used to store the result of a @code{gpgme_op_decrypt} operation. After successfully decrypting data, you can retrieve the pointer to the result with @code{gpgme_op_decrypt_result}. As with all result structures, it this structure shall be considered read-only and an application must not allocated such a strucure on its own. The structure contains the following members: @table @code @item char *unsupported_algorithm If an unsupported algorithm was encountered, this string describes the algorithm that is not supported. @item unsigned int wrong_key_usage : 1 @since{0.9.0} This is true if the key was not used according to its policy. @item gpgme_recipient_t recipients @since{1.1.0} This is a linked list of recipients to which this message was encrypted. @item char *file_name This is the filename of the original plaintext message file if it is known, otherwise this is a null pointer. @item char *session_key @since{1.8.0} A textual representation (nul-terminated string) of the session key used in symmetric encryption of the message, if the context has been set to export session keys (see @code{gpgme_set_ctx_flag, "export-session-key"}), and a session key was available for the most recent decryption operation. Otherwise, this is a null pointer. You must not try to access this member of the struct unless @code{gpgme_set_ctx_flag (ctx, "export-session-key")} returns success or @code{gpgme_get_ctx_flag (ctx, "export-session-key")} returns true (non-empty string). @end table @end deftp @deftypefun gpgme_decrypt_result_t gpgme_op_decrypt_result (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_op_decrypt_result} returns a @code{gpgme_decrypt_result_t} pointer to a structure holding the result of a @code{gpgme_op_decrypt} operation. The pointer is only valid if the last operation on the context was a @code{gpgme_op_decrypt} or @code{gpgme_op_decrypt_start} operation. If the operation failed this might be a @code{NULL} pointer. The returned pointer is only valid until the next operation is started on the context. @end deftypefun @node Verify @subsection Verify @cindex verification @cindex signature, verification @cindex cryptographic operation, verification @cindex cryptographic operation, signature check @cindex signature notation data @cindex notation data @deftypefun gpgme_error_t gpgme_op_verify (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_data_t @var{sig}}, @w{gpgme_data_t @var{signed_text}}, @w{gpgme_data_t @var{plain}}) The function @code{gpgme_op_verify} verifies that the signature in the data object @var{sig} is a valid signature. If @var{sig} is a detached signature, then the signed text should be provided in @var{signed_text} and @var{plain} should be a null pointer. Otherwise, if @var{sig} is a normal (or cleartext) signature, @var{signed_text} should be a null pointer and @var{plain} should be a writable data object that will contain the plaintext after successful verification. The results of the individual signature verifications can be retrieved with @code{gpgme_op_verify_result}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation could be completed successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx}, @var{sig} or @var{plain} is not a valid pointer, @code{GPG_ERR_NO_DATA} if @var{sig} does not contain any data to verify, and passes through any errors that are reported by the crypto engine support routines. @end deftypefun @deftypefun gpgme_error_t gpgme_op_verify_start (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_data_t @var{sig}}, @w{gpgme_data_t @var{signed_text}}, @w{gpgme_data_t @var{plain}}) The function @code{gpgme_op_verify_start} initiates a @code{gpgme_op_verify} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation could be started successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx}, @var{sig} or @var{plain} is not a valid pointer, and @code{GPG_ERR_NO_DATA} if @var{sig} or @var{plain} does not contain any data to verify. @end deftypefun @deftp {Data type} {gpgme_sig_notation_t} This is a pointer to a structure used to store a part of the result of a @code{gpgme_op_verify} operation. The structure contains the following members: @table @code @item gpgme_sig_notation_t next This is a pointer to the next new signature notation structure in the linked list, or @code{NULL} if this is the last element. @item char *name The name of the notation field. If this is @code{NULL}, then the member @code{value} will contain a policy URL. @item int name_len The length of the @code{name} field. For strings the length is counted without the trailing binary zero. @item char *value The value of the notation field. If @code{name} is @code{NULL}, then this is a policy URL. @item int value_len The length of the @code{value} field. For strings the length is counted without the trailing binary zero. @item gpgme_sig_notation_flags_t flags The accumulated flags field. This field contains the flags associated with the notation data in an accumulated form which can be used as an argument to the function @code{gpgme_sig_notation_add}. The value @code{flags} is a bitwise-or combination of one or multiple of the following bit values: @table @code @item GPGME_SIG_NOTATION_HUMAN_READABLE @since{1.1.0} The @code{GPGME_SIG_NOTATION_HUMAN_READABLE} symbol specifies that the notation data is in human readable form @item GPGME_SIG_NOTATION_CRITICAL @since{1.1.0} The @code{GPGME_SIG_NOTATION_CRITICAL} symbol specifies that the notation data is critical. @end table @item unsigned int human_readable : 1 This is true if the @code{GPGME_SIG_NOTATION_HUMAN_READABLE} flag is set and false otherwise. This flag is only valid for notation data, not for policy URLs. @item unsigned int critical : 1 This is true if the @code{GPGME_SIG_NOTATION_CRITICAL} flag is set and false otherwise. This flag is valid for notation data and policy URLs. @end table @end deftp @deftp {Data type} {gpgme_signature_t} This is a pointer to a structure used to store a part of the result of a @code{gpgme_op_verify} operation. The structure contains the following members: @table @code @item gpgme_signature_t next This is a pointer to the next new signature structure in the linked list, or @code{NULL} if this is the last element. @item gpgme_sigsum_t summary This is a bit vector giving a summary of the signature status. It provides an easy interface to a defined semantic of the signature status. Checking just one bit is sufficient to see whether a signature is valid without any restrictions. This means that you can check for GPGME_SIGSUM_VALID like this: @example if ((sig.summary & GPGME_SIGSUM_VALID)) @{ ..do stuff if valid.. @} else @{ ..do stuff if not fully valid.. @} @end example The defined bits are: @table @code @item GPGME_SIGSUM_VALID The signature is fully valid. @item GPGME_SIGSUM_GREEN The signature is good but one might want to display some extra information. Check the other bits. @item GPGME_SIGSUM_RED The signature is bad. It might be useful to check other bits and display more information, i.e. a revoked certificate might not render a signature invalid when the message was received prior to the cause for the revocation. @item GPGME_SIGSUM_KEY_REVOKED The key or at least one certificate has been revoked. @item GPGME_SIGSUM_KEY_EXPIRED The key or one of the certificates has expired. It is probably a good idea to display the date of the expiration. @item GPGME_SIGSUM_SIG_EXPIRED The signature has expired. @item GPGME_SIGSUM_KEY_MISSING Can't verify due to a missing key or certificate. @item GPGME_SIGSUM_CRL_MISSING The CRL (or an equivalent mechanism) is not available. @item GPGME_SIGSUM_CRL_TOO_OLD Available CRL is too old. @item GPGME_SIGSUM_BAD_POLICY A policy requirement was not met. @item GPGME_SIGSUM_SYS_ERROR A system error occured. @item GPGME_SIGSUM_TOFU_CONFLICT A TOFU conflict was detected. @end table @item char *fpr This is the fingerprint or key ID of the signature. @item gpgme_error_t status This is the status of the signature. In particular, the following status codes are of interest: @table @code @item GPG_ERR_NO_ERROR This status indicates that the signature is valid. For the combined result this status means that all signatures are valid. @item GPG_ERR_SIG_EXPIRED This status indicates that the signature is valid but expired. For the combined result this status means that all signatures are valid and expired. @item GPG_ERR_KEY_EXPIRED This status indicates that the signature is valid but the key used to verify the signature has expired. For the combined result this status means that all signatures are valid and all keys are expired. @item GPG_ERR_CERT_REVOKED This status indicates that the signature is valid but the key used to verify the signature has been revoked. For the combined result this status means that all signatures are valid and all keys are revoked. @item GPG_ERR_BAD_SIGNATURE This status indicates that the signature is invalid. For the combined result this status means that all signatures are invalid. @item GPG_ERR_NO_PUBKEY This status indicates that the signature could not be verified due to a missing key. For the combined result this status means that all signatures could not be checked due to missing keys. @item GPG_ERR_GENERAL This status indicates that there was some other error which prevented the signature verification. @end table @item gpgme_sig_notation_t notations This is a linked list with the notation data and policy URLs. @item unsigned long timestamp The creation timestamp of this signature. @item unsigned long exp_timestamp The expiration timestamp of this signature, or 0 if the signature does not expire. @item unsigned int wrong_key_usage : 1 This is true if the key was not used according to its policy. @item unsigned int pka_trust : 2 This is set to the trust information gained by means of the PKA system. Values are: @table @code @item 0 No PKA information available or verification not possible. @item 1 PKA verification failed. @item 2 PKA verification succeeded. @item 3 Reserved for future use. @end table Depending on the configuration of the engine, this metric may also be reflected by the validity of the signature. @item unsigned int chain_model : 1 @since{1.1.6} This is true if the validity of the signature has been checked using the chain model. In the chain model the time the signature has been created must be within the validity period of the certificate and the time the certificate itself has been created must be within the validity period of the issuing certificate. In contrast the default validation model checks the validity of signature as well at the entire certificate chain at the current time. @item gpgme_validity_t validity The validity of the signature. @item gpgme_error_t validity_reason If a signature is not valid, this provides a reason why. @item gpgme_pubkey_algo_t The public key algorithm used to create this signature. @item gpgme_hash_algo_t The hash algorithm used to create this signature. @item char *pka_address The mailbox from the PKA information or @code{NULL}. @item gpgme_key_t key @since{1.7.0} An object describing the key used to create the signature. This key object may be incomplete in that it only conveys information availabale directly with a signature. It may also be @code{NULL} if such information is not readily available. @end table @end deftp @deftp {Data type} {gpgme_verify_result_t} This is a pointer to a structure used to store the result of a @code{gpgme_op_verify} operation. After verifying a signature, you can retrieve the pointer to the result with @code{gpgme_op_verify_result}. If the operation failed this might be a @code{NULL} pointer. The structure contains the following member: @table @code @item gpgme_signature_t signatures A linked list with information about all signatures for which a verification was attempted. @item char *file_name This is the filename of the original plaintext message file if it is known, otherwise this is a null pointer. @end table @end deftp @deftypefun gpgme_verify_result_t gpgme_op_verify_result (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_op_verify_result} returns a @code{gpgme_verify_result_t} pointer to a structure holding the result of a @code{gpgme_op_verify} operation. The pointer is only valid if the last operation on the context was a @code{gpgme_op_verify}, @code{gpgme_op_verify_start}, @code{gpgme_op_decrypt_verify} or @code{gpgme_op_decrypt_verify_start} operation, and if this operation finished successfully (for @code{gpgme_op_decrypt_verify} and @code{gpgme_op_decrypt_verify_start}, the error code @code{GPG_ERR_NO_DATA} counts as successful in this context). The returned pointer is only valid until the next operation is started on the context. @end deftypefun @node Decrypt and Verify @subsection Decrypt and Verify @cindex decryption and verification @cindex verification and decryption @cindex signature check @cindex cryptographic operation, decryption and verification @deftypefun gpgme_error_t gpgme_op_decrypt_verify (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_data_t @var{cipher}}, @w{gpgme_data_t @var{plain}}) The function @code{gpgme_op_decrypt_verify} decrypts the ciphertext in the data object @var{cipher} and stores it into the data object @var{plain}. If @var{cipher} contains signatures, they will be verified. After the operation completed, @code{gpgme_op_decrypt_result} and @code{gpgme_op_verify_result} can be used to retrieve more information about the signatures. If the error code @code{GPG_ERR_NO_DATA} is returned, @var{cipher} does not contain any data to decrypt. However, it might still be signed. The information about detected signatures is available with @code{gpgme_op_verify_result} in this case. The function returns the error code @code{GPG_ERR_NO_ERROR} if the ciphertext could be decrypted successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx}, @var{cipher} or @var{plain} is not a valid pointer, @code{GPG_ERR_NO_DATA} if @var{cipher} does not contain any data to decrypt, @code{GPG_ERR_DECRYPT_FAILED} if @var{cipher} is not a valid cipher text, @code{GPG_ERR_BAD_PASSPHRASE} if the passphrase for the secret key could not be retrieved, and passes through any errors that are reported by the crypto engine support routines. @end deftypefun @deftypefun gpgme_error_t gpgme_op_decrypt_verify_start (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_data_t @var{cipher}}, @w{gpgme_data_t @var{plain}}) The function @code{gpgme_op_decrypt_verify_start} initiates a @code{gpgme_op_decrypt_verify} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation could be started successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx}, @var{cipher}, @var{plain} or @var{r_stat} is not a valid pointer, and @code{GPG_ERR_NO_DATA} if @var{cipher} does not contain any data to decrypt. @end deftypefun When processing mails it is sometimes useful to extract the actual mail address (the ``addr-spec'') from a string. GPGME provides this helper function which uses the same semantics as the internal functions in GPGME and GnuPG: @deftypefun @w{char *} gpgme_addrspec_from_uid (@w{const char *@var{uid}}) @since{1.7.1} Return the mail address (called ``addr-spec'' in RFC-5322) from the string @var{uid} which is assumed to be a user id (called ``address'' in RFC-5322). All plain ASCII characters (i.e. those with bit 7 cleared) in the result are converted to lowercase. Caller must free the result using @code{gpgme_free}. Returns @code{NULL} if no valid address was found (in which case @code{ERRNO} is set to @code{EINVAL}) or for other errors. @end deftypefun @node Sign @subsection Sign @cindex signature, creation @cindex sign @cindex cryptographic operation, signing A signature can contain signatures by one or more keys. The set of keys used to create a signatures is contained in a context, and is applied to all following signing operations in this context (until the set is changed). @menu * Selecting Signers:: How to choose the keys to sign with. * Creating a Signature:: How to create a signature. * Signature Notation Data:: How to add notation data to a signature. @end menu @node Selecting Signers @subsubsection Selecting Signers @cindex signature, selecting signers @cindex signers, selecting The key or the keys used to create a signature are stored in the context. The following functions can be used to manipulate this list. If no signer has been set into the context a default key is used for signing. @deftypefun void gpgme_signers_clear (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_signers_clear} releases a reference for each key on the signers list and removes the list of signers from the context @var{ctx}. Every context starts with an empty list. @end deftypefun @deftypefun gpgme_error_t gpgme_signers_add (@w{gpgme_ctx_t @var{ctx}}, @w{const gpgme_key_t @var{key}}) The function @code{gpgme_signers_add} adds the key @var{key} to the list of signers in the context @var{ctx}. Calling this function acquires an additional reference for the key. @end deftypefun @deftypefun @w{unsigned int} gpgme_signers_count (@w{const gpgme_ctx_t @var{ctx}}) @since{1.4.3} The function @code{gpgme_signers_count} returns the number of signer keys in the context @var{ctx}. @end deftypefun @deftypefun gpgme_key_t gpgme_signers_enum (@w{const gpgme_ctx_t @var{ctx}}, @w{int @var{seq}}) The function @code{gpgme_signers_enum} returns the @var{seq}th key in the list of signers in the context @var{ctx}. An additional reference is acquired for the user. If @var{seq} is out of range, @code{NULL} is returned. @end deftypefun @node Creating a Signature @subsubsection Creating a Signature @deftp {Data type} {enum gpgme_sig_mode_t} @tindex gpgme_sig_mode_t The @code{gpgme_sig_mode_t} type is used to specify the desired type of a signature. The following modes are available: @table @code @item GPGME_SIG_MODE_NORMAL A normal signature is made, the output includes the plaintext and the signature. @item GPGME_SIG_MODE_DETACH A detached signature is made. @item GPGME_SIG_MODE_CLEAR A clear text signature is made. The @acronym{ASCII} armor and text mode settings of the context are ignored. @end table @end deftp @deftypefun gpgme_error_t gpgme_op_sign (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_data_t @var{plain}}, @w{gpgme_data_t @var{sig}}, @w{gpgme_sig_mode_t @var{mode}}) The function @code{gpgme_op_sign} creates a signature for the text in the data object @var{plain} and returns it in the data object @var{sig}. The type of the signature created is determined by the @acronym{ASCII} armor (or, if that is not set, by the encoding specified for @var{sig}), the text mode attributes set for the context @var{ctx} and the requested signature mode @var{mode}. After the operation completed successfully, the result can be retrieved with @code{gpgme_op_sign_result}. If an S/MIME signed message is created using the CMS crypto engine, the number of certificates to include in the message can be specified with @code{gpgme_set_include_certs}. @xref{Included Certificates}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the signature could be created successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx}, @var{plain} or @var{sig} is not a valid pointer, @code{GPG_ERR_NO_DATA} if the signature could not be created, @code{GPG_ERR_BAD_PASSPHRASE} if the passphrase for the secret key could not be retrieved, @code{GPG_ERR_UNUSABLE_SECKEY} if there are invalid signers, and passes through any errors that are reported by the crypto engine support routines. @end deftypefun @deftypefun gpgme_error_t gpgme_op_sign_start (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_data_t @var{plain}}, @w{gpgme_data_t @var{sig}}, @w{gpgme_sig_mode_t @var{mode}}) The function @code{gpgme_op_sign_start} initiates a @code{gpgme_op_sign} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation could be started successfully, and @code{GPG_ERR_INV_VALUE} if @var{ctx}, @var{plain} or @var{sig} is not a valid pointer. @end deftypefun @deftp {Data type} {gpgme_new_signature_t} This is a pointer to a structure used to store a part of the result of a @code{gpgme_op_sign} operation. The structure contains the following members: @table @code @item gpgme_new_signature_t next This is a pointer to the next new signature structure in the linked list, or @code{NULL} if this is the last element. @item gpgme_sig_mode_t type The type of this signature. @item gpgme_pubkey_algo_t pubkey_algo The public key algorithm used to create this signature. @item gpgme_hash_algo_t hash_algo The hash algorithm used to create this signature. @item unsigned int sig_class The signature class of this signature. @item long int timestamp The creation timestamp of this signature. @item char *fpr The fingerprint of the key which was used to create this signature. @end table @end deftp @deftp {Data type} {gpgme_sign_result_t} This is a pointer to a structure used to store the result of a @code{gpgme_op_sign} operation. After successfully generating a signature, you can retrieve the pointer to the result with @code{gpgme_op_sign_result}. The structure contains the following members: @table @code @item gpgme_invalid_key_t invalid_signers A linked list with information about all invalid keys for which a signature could not be created. @item gpgme_new_signature_t signatures A linked list with information about all signatures created. @end table @end deftp @deftypefun gpgme_sign_result_t gpgme_op_sign_result (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_op_sign_result} returns a @code{gpgme_sign_result_t} pointer to a structure holding the result of a @code{gpgme_op_sign} operation. The pointer is only valid if the last operation on the context was a @code{gpgme_op_sign}, @code{gpgme_op_sign_start}, @code{gpgme_op_encrypt_sign} or @code{gpgme_op_encrypt_sign_start} operation. If that operation failed, the function might return a @code{NULL} pointer. The returned pointer is only valid until the next operation is started on the context. @end deftypefun @node Signature Notation Data @subsubsection Signature Notation Data @cindex notation data @cindex signature notation data @cindex policy URL Using the following functions, you can attach arbitrary notation data to a signature. This information is then available to the user when the signature is verified. @deftypefun void gpgme_sig_notation_clear (@w{gpgme_ctx_t @var{ctx}}) @since{1.1.0} The function @code{gpgme_sig_notation_clear} removes the notation data from the context @var{ctx}. Subsequent signing operations from this context will not include any notation data. Every context starts with an empty notation data list. @end deftypefun @deftypefun gpgme_error_t gpgme_sig_notation_add (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{name}}, @w{const char *@var{value}}, @w{gpgme_sig_notation_flags_t @var{flags}}) @since{1.1.0} The function @code{gpgme_sig_notation_add} adds the notation data with the name @var{name} and the value @var{value} to the context @var{ctx}. Subsequent signing operations will include this notation data, as well as any other notation data that was added since the creation of the context or the last @code{gpgme_sig_notation_clear} operation. The arguments @var{name} and @var{value} must be @code{NUL}-terminated strings in human-readable form. The flag @code{GPGME_SIG_NOTATION_HUMAN_READABLE} is implied (non-human-readable notation data is currently not supported). The strings must be in UTF-8 encoding. If @var{name} is @code{NULL}, then @var{value} should be a policy URL. The function @code{gpgme_sig_notation_add} returns the error code @code{GPG_ERR_NO_ERROR} if the notation data could be added successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx} is not a valid pointer, or if @var{name}, @var{value} and @var{flags} are an invalid combination. The function also passes through any errors that are reported by the crypto engine support routines. @end deftypefun @deftypefun gpgme_sig_notation_t gpgme_sig_notation_get (@w{const gpgme_ctx_t @var{ctx}}) @since{1.1.0} The function @code{gpgme_sig_notation_get} returns the linked list of notation data structures that are contained in the context @var{ctx}. If @var{ctx} is not a valid pointer, or there is no notation data added for this context, @code{NULL} is returned. @end deftypefun @node Encrypt @subsection Encrypt @cindex encryption @cindex cryptographic operation, encryption One plaintext can be encrypted for several recipients at the same time. The list of recipients is created independently of any context, and then passed to the encryption operation. @menu * Encrypting a Plaintext:: How to encrypt a plaintext. @end menu @node Encrypting a Plaintext @subsubsection Encrypting a Plaintext @deftypefun gpgme_error_t gpgme_op_encrypt (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t @var{recp}[]}, @w{gpgme_encrypt_flags_t @var{flags}}, @w{gpgme_data_t @var{plain}}, @w{gpgme_data_t @var{cipher}}) The function @code{gpgme_op_encrypt} encrypts the plaintext in the data object @var{plain} for the recipients @var{recp} and stores the ciphertext in the data object @var{cipher}. The type of the ciphertext created is determined by the @acronym{ASCII} armor (or, if that is not set, by the encoding specified for @var{cipher}) and the text mode attributes set for the context @var{ctx}. @var{recp} must be a @code{NULL}-terminated array of keys. The user must keep references for all keys during the whole duration of the call (but see @code{gpgme_op_encrypt_start} for the requirements with the asynchronous variant). The value in @var{flags} is a bitwise-or combination of one or multiple of the following bit values: @table @code @item GPGME_ENCRYPT_ALWAYS_TRUST The @code{GPGME_ENCRYPT_ALWAYS_TRUST} symbol specifies that all the recipients in @var{recp} should be trusted, even if the keys do not have a high enough validity in the keyring. This flag should be used with care; in general it is not a good idea to use any untrusted keys. @item GPGME_ENCRYPT_NO_ENCRYPT_TO @since{1.2.0} The @code{GPGME_ENCRYPT_NO_ENCRYPT_TO} symbol specifies that no default or hidden default recipients as configured in the crypto backend should be included. This can be useful for managing different user profiles. @item GPGME_ENCRYPT_NO_COMPRESS @since{1.5.0} The @code{GPGME_ENCRYPT_NO_COMPRESS} symbol specifies that the plaintext shall not be compressed before it is encrypted. This is in some cases useful if the length of the encrypted message may reveal information about the plaintext. @item GPGME_ENCRYPT_PREPARE @itemx GPGME_ENCRYPT_EXPECT_SIGN The @code{GPGME_ENCRYPT_PREPARE} symbol is used with the UI Server protocol to prepare an encryption (i.e. sending the @code{PREP_ENCRYPT} command). With the @code{GPGME_ENCRYPT_EXPECT_SIGN} symbol the UI Server is advised to also expect a sign command. @item GPGME_ENCRYPT_SYMMETRIC @since{1.7.0} The @code{GPGME_ENCRYPT_SYMMETRIC} symbol specifies that the output should be additionally encrypted symmetrically even if recipients are provided. This feature is only supported for for the OpenPGP crypto engine. @item GPGME_ENCRYPT_THROW_KEYIDS @since{1.8.0} The @code{GPGME_ENCRYPT_THROW_KEYIDS} symbols requests that the identifiers for the decrption keys are not included in the ciphertext. On the receiving side, the use of this flag may slow down the decryption process because all available secret keys must be tried. This flag is only honored for OpenPGP encryption. @item GPGME_ENCRYPT_WRAP @since{1.8.0} The @code{GPGME_ENCRYPT_WRAP} symbol specifies that the input is an OpenPGP message and not a plain data. This is the counterpart to @code{GPGME_DECRYPT_UNWRAP}. @end table If @code{GPG_ERR_UNUSABLE_PUBKEY} is returned, some recipients in @var{recp} are invalid, but not all. In this case the plaintext might be encrypted for all valid recipients and returned in @var{cipher} (if this happens depends on the crypto engine). More information about the invalid recipients is available with @code{gpgme_op_encrypt_result}. If @var{recp} is @code{NULL}, symmetric rather than public key encryption is performed. Symmetrically encrypted cipher text can be deciphered with @code{gpgme_op_decrypt}. Note that in this case the crypto backend needs to retrieve a passphrase from the user. Symmetric encryption is currently only supported for the OpenPGP crypto backend. The function returns the error code @code{GPG_ERR_NO_ERROR} if the ciphertext could be created successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx}, @var{recp}, @var{plain} or @var{cipher} is not a valid pointer, @code{GPG_ERR_UNUSABLE_PUBKEY} if @var{recp} contains some invalid recipients, @code{GPG_ERR_BAD_PASSPHRASE} if the passphrase for the symmetric key could not be retrieved, and passes through any errors that are reported by the crypto engine support routines. @end deftypefun @deftypefun gpgme_error_t gpgme_op_encrypt_start (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t @var{recp}[]}, @w{gpgme_encrypt_flags_t @var{flags}}, @w{gpgme_data_t @var{plain}}, @w{gpgme_data_t @var{cipher}}) The function @code{gpgme_op_encrypt_start} initiates a @code{gpgme_op_encrypt} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. References to the keys only need to be held for the duration of this call. The user can release its references to the keys after this function returns, even if the operation is not yet finished. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation could be started successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx}, @var{rset}, @var{plain} or @var{cipher} is not a valid pointer, and @code{GPG_ERR_UNUSABLE_PUBKEY} if @var{rset} does not contain any valid recipients. @end deftypefun +@deftypefun gpgme_error_t gpgme_op_encrypt_ext @ + (@w{gpgme_ctx_t @var{ctx}}, @ + @w{gpgme_key_t @var{recp}[]}, @ + @w{const char *@var{recpstring}}, @ + @w{gpgme_encrypt_flags_t @var{flags}}, @ + @w{gpgme_data_t @var{plain}}, @w{gpgme_data_t @var{cipher}}) + +@since{1.11.0} + +This is an extended version of @code{gpgme_op_encrypt} with +@var{recpstring} as additional parameter. If @var{recp} is NULL and +@var{recpstring} is not NULL, the latter is expected to be a linefeed +delimited string with the set of key specifications. In contrast to +@var{recp} the keys are given directly as strings and there is no need +to first create key objects. The keys are passed verbatim to the +backend engine. + +@end deftypefun + + +@deftypefun gpgme_error_t gpgme_op_encrypt_ext_start @ + (@w{gpgme_ctx_t @var{ctx}}, @ + @w{gpgme_key_t @var{recp}[]}, @ + @w{const char *@var{recpstring}}, @ + @w{gpgme_encrypt_flags_t @var{flags}}, @ + @w{gpgme_data_t @var{plain}}, @w{gpgme_data_t @var{cipher}}) + +@since{1.11.0} + +This is an extended version of @code{gpgme_op_encrypt_start} with +@var{recpstring} as additional parameter. If @var{recp} is NULL and +@var{recpstring} is not NULL, the latter is expected to be a linefeed +delimited string with the set of key specifications. In contrast to +@var{recp} the keys are given directly as strings and there is no need +to first create key objects. The keys are passed verbatim to the +backend engine. + +@end deftypefun + @deftp {Data type} {gpgme_encrypt_result_t} This is a pointer to a structure used to store the result of a @code{gpgme_op_encrypt} operation. After successfully encrypting data, you can retrieve the pointer to the result with @code{gpgme_op_encrypt_result}. The structure contains the following members: @table @code @item gpgme_invalid_key_t invalid_recipients A linked list with information about all invalid keys for which the data could not be encrypted. @end table @end deftp @deftypefun gpgme_encrypt_result_t gpgme_op_encrypt_result (@w{gpgme_ctx_t @var{ctx}}) The function @code{gpgme_op_encrypt_result} returns a @code{gpgme_encrypt_result_t} pointer to a structure holding the result of a @code{gpgme_op_encrypt} operation. The pointer is only valid if the last operation on the context was a @code{gpgme_op_encrypt}, @code{gpgme_op_encrypt_start}, @code{gpgme_op_sign} or @code{gpgme_op_sign_start} operation. If this operation failed, this might be a @code{NULL} pointer. The returned pointer is only valid until the next operation is started on the context. @end deftypefun @deftypefun gpgme_error_t gpgme_op_encrypt_sign (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t @var{recp}[]}, @w{gpgme_encrypt_flags_t @var{flags}}, @w{gpgme_data_t @var{plain}}, @w{gpgme_data_t @var{cipher}}) The function @code{gpgme_op_encrypt_sign} does a combined encrypt and sign operation. It is used like @code{gpgme_op_encrypt}, but the ciphertext also contains signatures for the signers listed in @var{ctx}. The combined encrypt and sign operation is currently only available for the OpenPGP crypto engine. @end deftypefun @deftypefun gpgme_error_t gpgme_op_encrypt_sign_start (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t @var{recp}[]}, @w{gpgme_encrypt_flags_t @var{flags}}, @w{gpgme_data_t @var{plain}}, @w{gpgme_data_t @var{cipher}}) The function @code{gpgme_op_encrypt_sign_start} initiates a @code{gpgme_op_encrypt_sign} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation could be started successfully, and @code{GPG_ERR_INV_VALUE} if @var{ctx}, @var{rset}, @var{plain} or @var{cipher} is not a valid pointer. @end deftypefun +@deftypefun gpgme_error_t gpgme_op_encrypt_sign_ext @ + (@w{gpgme_ctx_t @var{ctx}}, @ + @w{gpgme_key_t @var{recp}[]}, @ + @w{const char *@var{recpstring}}, @ + @w{gpgme_encrypt_flags_t @var{flags}}, @ + @w{gpgme_data_t @var{plain}}, @w{gpgme_data_t @var{cipher}}) + +@since{1.11.0} + +This is an extended version of @code{gpgme_op_encrypt_sign} with +@var{recpstring} as additional parameter. If @var{recp} is NULL and +@var{recpstring} is not NULL, the latter is expected to be a linefeed +delimited string with the set of key specifications. In contrast to +@var{recp} the keys are given directly as strings and there is no need +to first create the key objects. The keys are passed verbatim to the +backend engine. + +@end deftypefun + +@deftypefun gpgme_error_t gpgme_op_encrypt_sign_ext_start @ + (@w{gpgme_ctx_t @var{ctx}}, @ + @w{gpgme_key_t @var{recp}[]}, @ + @w{const char *@var{recpstring}}, @ + @w{gpgme_encrypt_flags_t @var{flags}}, @ + @w{gpgme_data_t @var{plain}}, @w{gpgme_data_t @var{cipher}}) + +@since{1.11.0} + +This is an extended version of @code{gpgme_op_encrypt_sign_start} with +@var{recpstring} as additional parameter. If @var{recp} is NULL and +@var{recpstring} is not NULL, the latter is expected to be a linefeed +delimited string with the set of key specifications. In contrast to +@var{recp} the keys are given directly as strings and there is no need +to first create the key objects. The keys are passed verbatim to the +backend engine. + +@end deftypefun + @node Miscellaneous @section Miscellaneous operations Here are some support functions which are sometimes useful. @menu * Running other Programs:: Running other Programs * Using the Assuan protocol:: Using the Assuan protocol * Checking for updates:: How to check for software updates @end menu @node Running other Programs @subsection Running other Programs GPGME features an internal subsystem to run the actual backend engines. Along with data abstraction object this subsystem can be used to run arbitrary simple programs which even need not be related to cryptographic features. It may for example be used to run tools which are part of the GnuPG system but are not directly accessible with the GPGME API. @deftypefun gpgme_error_t gpgme_op_spawn @ (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{file}}, @ @w{const char *@var{argv}[]}, @w{gpgme_data_t @var{datain}}, @ @w{gpgme_data_t @var{dataout}}, @w{gpgme_data_t @var{dataerr}}, @ @w{unsigned int @var{flags}}) @since{1.5.0} The function @code{gpgme_op_spawn} runs the program @var{file} with the arguments taken from the NULL terminated array @var{argv}. If no arguments are required @var{argv} may be given as @code{NULL}. In the latter case or if @code{argv[0]} is the empty string, GPGME uses the basename of @var{file} for @code{argv[0]}. The file descriptors @code{stdin}, @code{stdout}, and @code{stderr} are connected to the data objects @var{datain}, @var{dataout}, and @var{dataerr}. If NULL is passed for one of these data objects the corresponding file descriptor is connected to @file{/dev/null}. The value in @var{flags} is a bitwise-or combination of one or multiple of the following bit values: @table @code @item GPGME_SPAWN_DETACHED @since{1.5.0} Under Windows this flag inhibits the allocation of a new console for the program. This is useful for a GUI application which needs to call a command line helper tool. @item GPGME_SPAWN_ALLOW_SET_FG @since{1.5.0} Under Windows this flag allows the called program to put itself into the foreground. @end table @end deftypefun @deftypefun gpgme_error_t gpgme_op_spawn_start @ (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{file}}, @ @w{const char *@var{argv}[]}, @w{gpgme_data_t @var{datain}}, @ @w{gpgme_data_t @var{dataout}}, @w{gpgme_data_t @var{dataerr}}, @ @w{unsigned int @var{flags}}) @since{1.5.0} This is the asynchronous variant of @code{gpgme_op_spawn}. @end deftypefun @node Using the Assuan protocol @subsection Using the Assuan protocol The Assuan protocol can be used to talk to arbitrary Assuan servers. By default it is connected to the GnuPG agent, but it may be connected to arbitrary servers by using @code{gpgme_ctx_set_engine_info}, passing the location of the servers socket as @var{file_name} argument, and an empty string as @var{home_dir} argument. The Assuan protocol functions use three kinds of callbacks to transfer data: @deftp {Data type} {gpgme_error_t (*gpgme_assuan_data_cb_t) @ (@w{void *@var{opaque}}, @w{const void *@var{data}}, @ @w{size_t @var{datalen}})} @since{1.2.0} This callback receives any data sent by the server. @var{opaque} is the pointer passed to @code{gpgme_op_assuan_transact_start}, @var{data} of length @var{datalen} refers to the data sent. @end deftp @deftp {Data type} {gpgme_error_t (*gpgme_assuan_inquire_cb_t) @ (@w{void *@var{opaque}}, @w{const char *@var{name}}, @ @w{const char *@var{args}}, @w{gpgme_data_t *@var{r_data}})} @since{1.2.0} This callback is used to provide additional data to the Assuan server. @var{opaque} is the pointer passed to @code{gpgme_op_assuan_transact_start}, @var{name} and @var{args} specify what kind of data the server requested, and @var{r_data} is used to return the actual data. Note: Returning data is currently not implemented in @acronym{GPGME}. @end deftp @deftp {Data type} {gpgme_error_t (*gpgme_assuan_status_cb_t) @ (@w{void *@var{opaque}}, @w{const char *@var{status}}, @ @w{const char *@var{args}})} @since{1.2.0} This callback receives any status lines sent by the server. @var{opaque} is the pointer passed to @code{gpgme_op_assuan_transact_start}, @var{status} and @var{args} denote the status update sent. @end deftp @deftypefun gpgme_error_t gpgme_op_assuan_transact_start @ (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{command}}, @ @w{gpgme_assuan_data_cb_t @var{data_cb}}, @ @w{void * @var{data_cb_value}}, @ @w{gpgme_assuan_inquire_cb_t @var{inquire_cb}}, @ @w{void * @var{inquire_cb_value}}, @ @w{gpgme_assuan_status_cb_t @var{status_cb}}, @ @w{void * @var{status_cb_value}}) @since{1.2.0} Send the Assuan @var{command} and return results via the callbacks. Any callback may be @code{NULL}. The result of the operation may be retrieved using @code{gpgme_wait_ext}. Asynchronous variant. @end deftypefun @deftypefun gpgme_error_t gpgme_op_assuan_transact_ext @ (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{command}}, @ @w{gpgme_assuan_data_cb_t @var{data_cb}}, @ @w{void * @var{data_cb_value}}, @ @w{gpgme_assuan_inquire_cb_t @var{inquire_cb}}, @ @w{void * @var{inquire_cb_value}}, @ @w{gpgme_assuan_status_cb_t @var{status_cb}}, @ @w{void * @var{status_cb_value}}, @ @w{gpgme_error_t *@var{op_err}}) Send the Assuan @var{command} and return results via the callbacks. The result of the operation is returned in @var{op_err}. Synchronous variant. @end deftypefun @node Checking for updates @subsection How to check for software updates The GnuPG Project operates a server to query the current versions of software packages related to GnuPG. GPGME can be used to access this online database and check whether a new version of a software package is available. @deftp {Data type} {gpgme_query_swdb_result_t} @since{1.8.0} This is a pointer to a structure used to store the result of a @code{gpgme_op_query_swdb} operation. After success full call to that function, you can retrieve the pointer to the result with @code{gpgme_op_query_swdb_result}. The structure contains the following member: @table @code @item name This is the name of the package. @item iversion The currently installed version or an empty string. This value is either a copy of the argument given to @code{gpgme_op_query_swdb} or the version of the installed software as figured out by GPGME or GnuPG. @item created This gives the date the file with the list of version numbers has originally be created by the GnuPG project. @item retrieved This gives the date the file was downloaded. @item warning If this flag is set either an error has occurred or some of the information in this structure are not properly set. For example if the version number of the installed software could not be figured out, the @code{update} flag may not reflect a required update status. @item update If this flag is set an update of the software is available. @item urgent If this flag is set an available update is important. @item noinfo If this flag is set, no valid information could be retrieved. @item unknown If this flag is set the given @code{name} is not known. @item tooold If this flag is set the available information is not fresh enough. @item error If this flag is set some other error has occurred. @item version The version string of the latest released version. @item reldate The release date of the latest released version. @end table @end deftp @deftypefun gpgme_error_t gpgme_op_query_swdb @ (@w{gpgme_ctx_t @var{ctx}}, @ @w{const char *@var{name}}, @ @w{const char *@var{iversion}}, @ @w{gpgme_data_t @var{reserved}}) @since{1.8.0} Query the software version database for software package @var{name} and check against the installed version given by @var{iversion}. If @var{iversion} is given as @code{NULL} a check is only done if GPGME can figure out the version by itself (for example when using "gpgme" or "gnupg"). If @code{NULL} is used for @var{name} the current gpgme version is checked. @var{reserved} must be set to 0. @end deftypefun @deftypefun gpgme_query_swdb_result_t gpgme_op_query_swdb_result @ (@w{gpgme_ctx_t @var{ctx}}) @since{1.8.0} The function @code{gpgme_op_query_swdb_result} returns a @code{gpgme_query_swdb_result_t} pointer to a structure holding the result of a @code{gpgme_op_query_swdb} operation. The pointer is only valid if the last operation on the context was a successful call to @code{gpgme_op_query_swdb}. If that call failed, the result might be a @code{NULL} pointer. The returned pointer is only valid until the next operation is started on the context @var{ctx}. @end deftypefun @noindent Here is an example on how to check whether GnuPG is current: @example #include int main (void) @{ gpg_error_t err; gpgme_ctx_t ctx; gpgme_query_swdb_result_t result; gpgme_check_version (NULL); err = gpgme_new (&ctx); if (err) fprintf (stderr, "error creating context: %s\n", gpg_strerror (err)); else @{ gpgme_set_protocol (ctx, GPGME_PROTOCOL_GPGCONF); err = gpgme_op_query_swdb (ctx, "gnupg", NULL, 0); if (err) fprintf (stderr, "error querying swdb: %s\n", gpg_strerror (err)); else @{ result = gpgme_op_query_swdb_result (ctx); if (!result) fprintf (stderr, "error querying swdb\n"); if (!result->warning && !result->update) printf ("GnuPG version %s is current\n", result->iversion); else if (!result->warning && result->update) printf ("GnuPG version %s can be updated to %s\n", result->iversion, result->version); else fprintf (stderr, "error finding the update status\n"); @} gpgme_release (ctx); @} return 0; @} @end example @node Run Control @section Run Control @cindex run control @cindex cryptographic operation, running @acronym{GPGME} supports running operations synchronously and asynchronously. You can use asynchronous operation to set up a context up to initiating the desired operation, but delay performing it to a later point. Furthermore, you can use an external event loop to control exactly when @acronym{GPGME} runs. This ensures that @acronym{GPGME} only runs when necessary and also prevents it from blocking for a long time. @menu * Waiting For Completion:: Waiting until an operation is completed. * Using External Event Loops:: Advanced control over what happens when. * Cancellation:: How to end pending operations prematurely. @end menu @node Waiting For Completion @subsection Waiting For Completion @cindex cryptographic operation, wait for @cindex wait for completion @deftypefun gpgme_ctx_t gpgme_wait (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_error_t *@var{status}}, @w{int @var{hang}}) The function @code{gpgme_wait} continues the pending operation within the context @var{ctx}. In particular, it ensures the data exchange between @acronym{GPGME} and the crypto backend and watches over the run time status of the backend process. If @var{hang} is true, the function does not return until the operation is completed or cancelled. Otherwise the function will not block for a long time. The error status of the finished operation is returned in @var{status} if @code{gpgme_wait} does not return @code{NULL}. The @var{ctx} argument can be @code{NULL}. In that case, @code{gpgme_wait} waits for any context to complete its operation. @code{gpgme_wait} can be used only in conjunction with any context that has a pending operation initiated with one of the @code{gpgme_op_*_start} functions except @code{gpgme_op_keylist_start} and @code{gpgme_op_trustlist_start} (for which you should use the corresponding @code{gpgme_op_*_next} functions). If @var{ctx} is @code{NULL}, all of such contexts are waited upon and possibly returned. Synchronous operations running in parallel, as well as key and trust item list operations, do not affect @code{gpgme_wait}. In a multi-threaded environment, only one thread should ever call @code{gpgme_wait} at any time, irregardless if @var{ctx} is specified or not. This means that all calls to this function should be fully synchronized by locking primitives. It is safe to start asynchronous operations while a thread is running in @code{gpgme_wait}. The function returns the @var{ctx} of the context which has finished the operation. If @var{hang} is false, and the timeout expires, @code{NULL} is returned and @code{*status} will be set to 0. If an error occurs, @code{NULL} is returned and the error is returned in @code{*status}. @end deftypefun @node Using External Event Loops @subsection Using External Event Loops @cindex event loop, external @acronym{GPGME} hides the complexity of the communication between the library and the crypto engine. The price of this convenience is that the calling thread can block arbitrary long waiting for the data returned by the crypto engine. In single-threaded programs, in particular if they are interactive, this is an unwanted side-effect. OTOH, if @code{gpgme_wait} is used without the @var{hang} option being enabled, it might be called unnecessarily often, wasting CPU time that could be used otherwise. The I/O callback interface described in this section lets the user take control over what happens when. @acronym{GPGME} will provide the user with the file descriptors that should be monitored, and the callback functions that should be invoked when a file descriptor is ready for reading or writing. It is then the user's responsibility to decide when to check the file descriptors and when to invoke the callback functions. Usually this is done in an event loop, that also checks for events in other parts of the program. If the callback functions are only called when the file descriptors are ready, @acronym{GPGME} will never block. This gives the user more control over the program flow, and allows to perform other tasks when @acronym{GPGME} would block otherwise. By using this advanced mechanism, @acronym{GPGME} can be integrated smoothly into GUI toolkits like GTK+ even for single-threaded programs. @menu * I/O Callback Interface:: How I/O callbacks are registered. * Registering I/O Callbacks:: How to use I/O callbacks for a context. * I/O Callback Example:: An example how to use I/O callbacks. * I/O Callback Example GTK+:: How to use @acronym{GPGME} with GTK+. * I/O Callback Example GDK:: How to use @acronym{GPGME} with GDK. * I/O Callback Example Qt:: How to use @acronym{GPGME} with Qt. @end menu @node I/O Callback Interface @subsubsection I/O Callback Interface @deftp {Data type} {gpgme_error_t (*gpgme_io_cb_t) (@w{void *@var{data}}, @w{int @var{fd}})} @tindex gpgme_io_cb_t The @code{gpgme_io_cb_t} type is the type of functions which @acronym{GPGME} wants to register as I/O callback handlers using the @code{gpgme_register_io_cb_t} functions provided by the user. @var{data} and @var{fd} are provided by @acronym{GPGME} when the I/O callback handler is registered, and should be passed through to the handler when it is invoked by the user because it noticed activity on the file descriptor @var{fd}. The callback handler always returns @code{0}, but you should consider the return value to be reserved for later use. @end deftp @deftp {Data type} {gpgme_error_t (*gpgme_register_io_cb_t) (@w{void *@var{data}}, @w{int @var{fd}}, @w{int @var{dir}}, @w{gpgme_io_cb_t @var{fnc}}, @w{void *@var{fnc_data}}, @w{void **@var{tag}})} @tindex gpgme_register_io_cb_t The @code{gpgme_register_io_cb_t} type is the type of functions which can be called by @acronym{GPGME} to register an I/O callback function @var{fnc} for the file descriptor @var{fd} with the user. @var{fnc_data} should be passed as the first argument to @var{fnc} when the handler is invoked (the second argument should be @var{fd}). If @var{dir} is 0, @var{fnc} should be called by the user when @var{fd} is ready for writing. If @var{dir} is 1, @var{fnc} should be called when @var{fd} is ready for reading. @var{data} was provided by the user when registering the @code{gpgme_register_io_cb_t} function with @acronym{GPGME} and will always be passed as the first argument when registering a callback function. For example, the user can use this to determine the event loop to which the file descriptor should be added. @acronym{GPGME} will call this function when a crypto operation is initiated in a context for which the user has registered I/O callback handler functions with @code{gpgme_set_io_cbs}. It can also call this function when it is in an I/O callback handler for a file descriptor associated to this context. The user should return a unique handle in @var{tag} identifying this I/O callback registration, which will be passed to the @code{gpgme_register_io_cb_t} function without interpretation when the file descriptor should not be monitored anymore. @end deftp @deftp {Data type} {void (*gpgme_remove_io_cb_t) (@w{void *@var{tag}})} The @code{gpgme_remove_io_cb_t} type is the type of functions which can be called by @acronym{GPGME} to remove an I/O callback handler that was registered before. @var{tag} is the handle that was returned by the @code{gpgme_register_io_cb_t} for this I/O callback. @acronym{GPGME} can call this function when a crypto operation is in an I/O callback. It will also call this function when the context is destroyed while an operation is pending. @end deftp @deftp {Data type} {enum gpgme_event_io_t} @tindex gpgme_event_io_t The @code{gpgme_event_io_t} type specifies the type of an event that is reported to the user by @acronym{GPGME} as a consequence of an I/O operation. The following events are defined: @table @code @item GPGME_EVENT_START The operation is fully initialized now, and you can start to run the registered I/O callback handlers now. Note that registered I/O callback handlers must not be run before this event is signalled. @var{type_data} is @code{NULL} and reserved for later use. @item GPGME_EVENT_DONE The operation is finished, the last I/O callback for this operation was removed. The accompanying @var{type_data} points to a @code{struct gpgme_io_event_done_data} variable that contains the status of the operation that finished. This event is signalled after the last I/O callback has been removed. @item GPGME_EVENT_NEXT_KEY In a @code{gpgme_op_keylist_start} operation, the next key was received from the crypto engine. The accompanying @var{type_data} is a @code{gpgme_key_t} variable that contains the key with one reference for the user. @item GPGME_EVENT_NEXT_TRUSTITEM In a @code{gpgme_op_trustlist_start} operation, the next trust item was received from the crypto engine. The accompanying @var{type_data} is a @code{gpgme_trust_item_t} variable that contains the trust item with one reference for the user. @end table @end deftp @deftp {Data type} {void (*gpgme_event_io_cb_t) (@w{void *@var{data}}, @w{gpgme_event_io_t @var{type}}, @w{void *@var{type_data}})} The @code{gpgme_event_io_cb_t} type is the type of functions which can be called by @acronym{GPGME} to signal an event for an operation running in a context which has I/O callback functions registered by the user. @var{data} was provided by the user when registering the @code{gpgme_event_io_cb_t} function with @acronym{GPGME} and will always be passed as the first argument when registering a callback function. For example, the user can use this to determine the context in which this event has occurred. @var{type} will specify the type of event that has occurred. @var{type_data} specifies the event further, as described in the above list of possible @code{gpgme_event_io_t} types. @acronym{GPGME} can call this function in an I/O callback handler. @end deftp @node Registering I/O Callbacks @subsubsection Registering I/O Callbacks @deftp {Data type} {struct gpgme_io_cbs} @tindex gpgme_event_io_t This structure is used to store the I/O callback interface functions described in the previous section. It has the following members: @table @code @item gpgme_register_io_cb_t add This is the function called by @acronym{GPGME} to register an I/O callback handler. It must be specified. @item void *add_priv This is passed as the first argument to the @code{add} function when it is called by @acronym{GPGME}. For example, it can be used to determine the event loop to which the file descriptor should be added. @item gpgme_remove_io_cb_t remove This is the function called by @acronym{GPGME} to remove an I/O callback handler. It must be specified. @item gpgme_event_io_cb_t event This is the function called by @acronym{GPGME} to signal an event for an operation. It must be specified, because at least the start event must be processed. @item void *event_priv This is passed as the first argument to the @code{event} function when it is called by @acronym{GPGME}. For example, it can be used to determine the context in which the event has occurred. @end table @end deftp @deftypefun void gpgme_set_io_cbs (@w{gpgme_ctx_t @var{ctx}}, @w{struct gpgme_io_cbs *@var{io_cbs}}) The function @code{gpgme_set_io_cbs} enables the I/O callback interface for the context @var{ctx}. The I/O callback functions are specified by @var{io_cbs}. If @var{io_cbs}->@code{add} is @code{NULL}, the I/O callback interface is disabled for the context, and normal operation is restored. @end deftypefun @deftypefun void gpgme_get_io_cbs (@w{gpgme_ctx_t @var{ctx}}, @w{struct gpgme_io_cbs *@var{io_cbs}}) The function @code{gpgme_get_io_cbs} returns the I/O callback functions set with @code{gpgme_set_io_cbs} in @var{io_cbs}. @end deftypefun @node I/O Callback Example @subsubsection I/O Callback Example To actually use an external event loop, you have to implement the I/O callback functions that are used by @acronym{GPGME} to register and unregister file descriptors. Furthermore, you have to actually monitor these file descriptors for activity and call the appropriate I/O callbacks. The following example illustrates how to do that. The example uses locking to show in which way the callbacks and the event loop can run concurrently. For the event loop, we use a fixed array. For a real-world implementation, you should use a dynamically sized structure because the number of file descriptors needed for a crypto operation in @acronym{GPGME} is not predictable. @example #include #include #include #include #include #include /* The following structure holds the result of a crypto operation. */ struct op_result @{ int done; gpgme_error_t err; @}; /* The following structure holds the data associated with one I/O callback. */ struct one_fd @{ int fd; int dir; gpgme_io_cb_t fnc; void *fnc_data; void *loop; @}; struct event_loop @{ pthread_mutex_t lock; #define MAX_FDS 32 /* Unused slots are marked with FD being -1. */ struct one_fd fds[MAX_FDS]; @}; @end example The following functions implement the I/O callback interface. @example gpgme_error_t add_io_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc, void *fnc_data, void **r_tag) @{ struct event_loop *loop = data; struct one_fd *fds = loop->fds; int i; pthread_mutex_lock (&loop->lock); for (i = 0; i < MAX_FDS; i++) @{ if (fds[i].fd == -1) @{ fds[i].fd = fd; fds[i].dir = dir; fds[i].fnc = fnc; fds[i].fnc_data = fnc_data; fds[i].loop = loop; break; @} @} pthread_mutex_unlock (&loop->lock); if (i == MAX_FDS) return gpg_error (GPG_ERR_GENERAL); *r_tag = &fds[i]; return 0; @} void remove_io_cb (void *tag) @{ struct one_fd *fd = tag; struct event_loop *loop = fd->loop; pthread_mutex_lock (&loop->lock); fd->fd = -1; pthread_mutex_unlock (&loop->lock); @} void event_io_cb (void *data, gpgme_event_io_t type, void *type_data) @{ struct op_result *result = data; /* We don't support list operations here. */ if (type == GPGME_EVENT_DONE) @{ result->done = 1; result->err = *type_data; @} @} @end example The final missing piece is the event loop, which will be presented next. We only support waiting for the success of a single operation. @example int do_select (struct event_loop *loop) @{ fd_set rfds; fd_set wfds; int i, n; int any = 0; struct timeval tv; struct one_fd *fdlist = loop->fds; pthread_mutex_lock (&loop->lock); FD_ZERO (&rfds); FD_ZERO (&wfds); for (i = 0; i < MAX_FDS; i++) if (fdlist[i].fd != -1) FD_SET (fdlist[i].fd, fdlist[i].dir ? &rfds : &wfds); pthread_mutex_unlock (&loop->lock); tv.tv_sec = 0; tv.tv_usec = 1000; do @{ n = select (FD_SETSIZE, &rfds, &wfds, NULL, &tv); @} while (n < 0 && errno == EINTR); if (n < 0) return n; /* Error or timeout. */ pthread_mutex_lock (&loop->lock); for (i = 0; i < MAX_FDS && n; i++) @{ if (fdlist[i].fd != -1) @{ if (FD_ISSET (fdlist[i].fd, fdlist[i].dir ? &rfds : &wfds)) @{ assert (n); n--; any = 1; /* The I/O callback handler can register/remove callbacks, so we have to unlock the file descriptor list. */ pthread_mutex_unlock (&loop->lock); (*fdlist[i].fnc) (fdlist[i].fnc_data, fdlist[i].fd); pthread_mutex_lock (&loop->lock); @} @} @} pthread_mutex_unlock (&loop->lock); return any; @} void wait_for_op (struct event_loop *loop, struct op_result *result) @{ int ret; do @{ ret = do_select (loop); @} while (ret >= 0 && !result->done); @} @end example The main function shows how to put it all together. @example int main (int argc, char *argv[]) @{ struct event_loop loop; struct op_result result; gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t sig, text; int i; pthread_mutexattr_t attr; struct gpgme_io_cbs io_cbs = @{ add_io_cb, &loop, remove_io_cb, event_io_cb, &result @}; init_gpgme (); /* Initialize the loop structure. */ /* The mutex must be recursive, since remove_io_cb (which acquires a lock) can be called while holding a lock acquired in do_select. */ pthread_mutexattr_init (&attr); pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init (&loop.lock, &attr); pthread_mutexattr_destroy (&attr); for (i = 0; i < MAX_FDS; i++) loop.fds[i].fd = -1; /* Initialize the result structure. */ result.done = 0; err = gpgme_data_new_from_file (&sig, "signature", 1); if (!err) err = gpgme_data_new_from_file (&text, "text", 1); if (!err) err = gpgme_new (&ctx); if (!err) @{ gpgme_set_io_cbs (ctx, &io_cbs); err = gpgme_op_verify_start (ctx, sig, text, NULL); @} if (err) @{ fprintf (stderr, "gpgme error: %s: %s\n", gpgme_strsource (err), gpgme_strerror (err)); exit (1); @} wait_for_op (&loop, &result); if (!result.done) @{ fprintf (stderr, "select error\n"); exit (1); @} if (!result.err) @{ fprintf (stderr, "verification failed: %s: %s\n", gpgme_strsource (result.err), gpgme_strerror (result.err)); exit (1); @} /* Evaluate verify result. */ @dots{} return 0; @} @end example @node I/O Callback Example GTK+ @subsubsection I/O Callback Example GTK+ @cindex GTK+, using @acronym{GPGME} with The I/O callback interface can be used to integrate @acronym{GPGME} with the GTK+ event loop. The following code snippets shows how this can be done using the appropriate register and remove I/O callback functions. In this example, the private data of the register I/O callback function is unused. The event notifications is missing because it does not require any GTK+ specific setup. @example #include struct my_gpgme_io_cb @{ gpgme_io_cb_t fnc; void *fnc_data; guint input_handler_id @}; void my_gpgme_io_cb (gpointer data, gint source, GdkInputCondition condition) @{ struct my_gpgme_io_cb *iocb = data; (*(iocb->fnc)) (iocb->data, source); @} void my_gpgme_remove_io_cb (void *data) @{ struct my_gpgme_io_cb *iocb = data; gtk_input_remove (data->input_handler_id); @} void my_gpgme_register_io_callback (void *data, int fd, int dir, gpgme_io_cb_t fnc, void *fnc_data, void **tag) @{ struct my_gpgme_io_cb *iocb = g_malloc (sizeof (struct my_gpgme_io_cb)); iocb->fnc = fnc; iocb->data = fnc_data; iocb->input_handler_id = gtk_input_add_full (fd, dir ? GDK_INPUT_READ : GDK_INPUT_WRITE, my_gpgme_io_callback, 0, iocb, NULL); *tag = iocb; return 0; @} @end example @node I/O Callback Example GDK @subsubsection I/O Callback Example GDK @cindex GDK, using @acronym{GPGME} with The I/O callback interface can also be used to integrate @acronym{GPGME} with the GDK event loop. The following code snippets shows how this can be done using the appropriate register and remove I/O callback functions. In this example, the private data of the register I/O callback function is unused. The event notifications is missing because it does not require any GDK specific setup. It is very similar to the GTK+ example in the previous section. @example #include struct my_gpgme_io_cb @{ gpgme_io_cb_t fnc; void *fnc_data; gint tag; @}; void my_gpgme_io_cb (gpointer data, gint source, GdkInputCondition condition) @{ struct my_gpgme_io_cb *iocb = data; (*(iocb->fnc)) (iocb->data, source); @} void my_gpgme_remove_io_cb (void *data) @{ struct my_gpgme_io_cb *iocb = data; gdk_input_remove (data->tag); @} void my_gpgme_register_io_callback (void *data, int fd, int dir, gpgme_io_cb_t fnc, void *fnc_data, void **tag) @{ struct my_gpgme_io_cb *iocb = g_malloc (sizeof (struct my_gpgme_io_cb)); iocb->fnc = fnc; iocb->data = fnc_data; iocb->tag = gtk_input_add_full (fd, dir ? GDK_INPUT_READ : GDK_INPUT_WRITE, my_gpgme_io_callback, iocb, NULL); *tag = iocb; return 0; @} @end example @node I/O Callback Example Qt @subsubsection I/O Callback Example Qt @cindex Qt, using @acronym{GPGME} with The I/O callback interface can also be used to integrate @acronym{GPGME} with the Qt event loop. The following code snippets show how this can be done using the appropriate register and remove I/O callback functions. In this example, the private data of the register I/O callback function is unused. The event notifications is missing because it does not require any Qt specific setup. @example #include #include struct IOCB @{ IOCB( GpgmeIOCb f, void * d, QSocketNotifier * n ) : func( f ), data( d ), notifier( n ) @{@} GpgmeIOCb func; void * data; QSocketNotifier * notifier; @} class MyApp : public QApplication @{ // ... static void registerGpgmeIOCallback( void * data, int fd, int dir, GpgmeIOCb func, void * func_data, void ** tag ) @{ QSocketNotifier * n = new QSocketNotifier( fd, dir ? QSocketNotifier::Read : QSocketNotifier::Write ); connect( n, SIGNAL(activated(int)), qApp, SLOT(slotGpgmeIOCallback(int)) ); qApp->mIOCBs.push_back( IOCB( func, func_data, n ) ); *tag = (void*)n; @} static void removeGpgmeIOCallback( void * tag ) @{ if ( !tag ) return; QSocketNotifier * n = static_cast( tag ); for ( QValueList::iterator it = qApp->mIOCBs.begin() ; it != qApp->mIOCBs.end() ; ++it ) if ( it->notifier == n ) @{ delete it->notifier; qApp->mIOCBs.erase( it ); return; @} @} public slots: void slotGpgmeIOCallback( int fd ) @{ for ( QValueList::const_iterator it = mIOCBs.begin() ; it != mIOCBs.end() ; ++it ) if ( it->notifier && it->notifier->socket() == fd ) (*(it->func)) ( it->func_data, fd ); @} // ... private: QValueList mIOCBs; // ... @}; @end example @node Cancellation @subsection Cancellation @cindex cryptographic operation, aborting @cindex cryptographic operation, cancelling @cindex aborting operations @cindex cancelling operations Sometimes you do not want to wait for an operation to finish. @acronym{GPGME} provides two different functions to achieve that. The function @code{gpgme_cancel} takes effect immediately. When it returns, the operation is effectively canceled. However, it has some limitations and can not be used with synchronous operations. In contrast, the function @code{gpgme_cancel_async} can be used with any context and from any thread, but it is not guaranteed to take effect immediately. Instead, cancellation occurs at the next possible time (typically the next time I/O occurs in the target context). @deftypefun gpgme_ctx_t gpgme_cancel (@w{gpgme_ctx_t @var{ctx}}) @since{0.4.5} The function @code{gpgme_cancel} attempts to cancel a pending operation in the context @var{ctx}. This only works if you use the global event loop or your own event loop. If you use the global event loop, you must not call @code{gpgme_wait} during cancellation. After successful cancellation, you can call @code{gpgme_wait} (optionally waiting on @var{ctx}), and the context @var{ctx} will appear as if it had finished with the error code @code{GPG_ERR_CANCEL}. If you use an external event loop, you must ensure that no I/O callbacks are invoked for this context (for example by halting the event loop). On successful cancellation, all registered I/O callbacks for this context will be unregistered, and a @code{GPGME_EVENT_DONE} event with the error code @code{GPG_ERR_CANCEL} will be signalled. The function returns an error code if the cancellation failed (in this case the state of @var{ctx} is not modified). @end deftypefun @deftypefun gpgme_ctx_t gpgme_cancel_async (@w{gpgme_ctx_t @var{ctx}}) @since{1.1.7} The function @code{gpgme_cancel_async} attempts to cancel a pending operation in the context @var{ctx}. This can be called by any thread at any time after starting an operation on the context, but will not take effect immediately. The actual cancellation happens at the next time GPGME processes I/O in that context. The function returns an error code if the cancellation failed (in this case the state of @var{ctx} is not modified). @end deftypefun @c ********************************************************** @c ******************* Appendices ************************* @c ********************************************************** @include uiserver.texi @node Debugging @appendix How to solve problems @cindex debug @cindex GPGME_DEBUG Everyone knows that software often does not do what it should do and thus there is a need to track down problems. This is in particular true for applications using a complex library like @acronym{GPGME} and of course also for the library itself. Here we give a few hints on how to solve such problems. First of all you should make sure that the keys you want to use are installed in the GnuPG engine and are usable. Thus the first test is to run the desired operation using @command{gpg} or @command{gpgsm} on the command line. If you can't figure out why things don't work, you may use @acronym{GPGME}'s built in trace feature. This feature is either enabled using the environment variable @code{GPGME_DEBUG} or, if this is not possible, by calling the function @code{gpgme_set_global_flag}. The value is the trace level and an optional file name. If no file name is given the trace output is printed to @code{stderr}. @noindent For example @smallexample GPGME_DEBUG=9:/home/user/mygpgme.log @end smallexample @noindent (Note that under Windows you use a semicolon in place of the colon to separate the fields.) A trace level of 9 is pretty verbose and thus you may want to start off with a lower level. The exact definition of the trace levels and the output format may change with any release; you need to check the source code for details. In any case the trace log should be helpful to understand what is going going on. Warning: The trace log may reveal sensitive details like passphrases or other data you use in your application. If you are asked to send a log file, make sure that you run your tests only with play data. @node Deprecated Functions @appendix Deprecated Functions @cindex deprecated For backward compatibility @acronym{GPGME} has a number of functions, data types and constants which are deprecated and should not be used anymore. We document here those which are really old to help understanding old code and to allow migration to their modern counterparts. @strong{Warning:} These interfaces will be removed in a future version of @acronym{GPGME}. @deftypefun void gpgme_key_release (@w{gpgme_key_t @var{key}}) The function @code{gpgme_key_release} is equivalent to @code{gpgme_key_unref}. @end deftypefun @deftypefun void gpgme_trust_item_release (@w{gpgme_trust_item_t @var{item}}) The function @code{gpgme_trust_item_release} is an alias for @code{gpgme_trust_item_unref}. @end deftypefun @deftypefun gpgme_error_t gpgme_op_import_ext (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_data_t @var{keydata}}, @w{int *@var{nr}}) @since{0.3.9} The function @code{gpgme_op_import_ext} is equivalent to: @example gpgme_error_t err = gpgme_op_import (ctx, keydata); if (!err) @{ gpgme_import_result_t result = gpgme_op_import_result (ctx); *nr = result->considered; @} @end example @end deftypefun @deftp {Data type} {gpgme_error_t (*gpgme_edit_cb_t) @ (@w{void *@var{handle}}, @ @w{gpgme_status_code_t @var{status}}, @ @w{const char *@var{args}}, @ @w{int @var{fd}})} @tindex gpgme_edit_cb_t The @code{gpgme_edit_cb_t} type is the type of functions which @acronym{GPGME} calls if it a key edit operation is on-going. The status code @var{status} and the argument line @var{args} are passed through by @acronym{GPGME} from the crypto engine. The file descriptor @var{fd} is -1 for normal status messages. If @var{status} indicates a command rather than a status message, the response to the command should be written to @var{fd}. The @var{handle} is provided by the user at start of operation. The function should return @code{GPG_ERR_FALSE} if it did not handle the status code, @code{0} for success, or any other error value. @end deftp @deftypefun gpgme_error_t gpgme_op_edit (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t @var{key}}, @w{gpgme_edit_cb_t @var{fnc}}, @w{void *@var{handle}}, @w{gpgme_data_t @var{out}}) @since{0.3.9} Note: This function is deprecated, please use @code{gpgme_op_interact} instead. The function @code{gpgme_op_edit} processes the key @var{KEY} interactively, using the edit callback function @var{FNC} with the handle @var{HANDLE}. The callback is invoked for every status and command request from the crypto engine. The output of the crypto engine is written to the data object @var{out}. Note that the protocol between the callback function and the crypto engine is specific to the crypto engine and no further support in implementing this protocol correctly is provided by @acronym{GPGME}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the edit operation completes successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx} or @var{key} is not a valid pointer, and any error returned by the crypto engine or the edit callback handler. @end deftypefun @deftypefun gpgme_error_t gpgme_op_edit_start (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t @var{key}}, @w{gpgme_edit_cb_t @var{fnc}}, @w{void *@var{handle}}, @w{gpgme_data_t @var{out}}) @since{0.3.9} Note: This function is deprecated, please use @code{gpgme_op_interact_start} instead. The function @code{gpgme_op_edit_start} initiates a @code{gpgme_op_edit} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation was started successfully, and @code{GPG_ERR_INV_VALUE} if @var{ctx} or @var{key} is not a valid pointer. @end deftypefun @deftypefun gpgme_error_t gpgme_op_card_edit (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t @var{key}}, @w{gpgme_edit_cb_t @var{fnc}}, @w{void *@var{handle}}, @w{gpgme_data_t @var{out}}) Note: This function is deprecated, please use @code{gpgme_op_interact} with the flag @code{GPGME_INTERACT_CARD} instead. The function @code{gpgme_op_card_edit} is analogous to @code{gpgme_op_edit}, but should be used to process the smart card corresponding to the key @var{key}. @end deftypefun @deftypefun gpgme_error_t gpgme_op_card_edit_start (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t @var{key}}, @w{gpgme_edit_cb_t @var{fnc}}, @w{void *@var{handle}}, @w{gpgme_data_t @var{out}}) Note: This function is deprecated, please use @code{gpgme_op_interact_start} with the flag @code{GPGME_INTERACT_CARD} instead. The function @code{gpgme_op_card_edit_start} initiates a @code{gpgme_op_card_edit} operation. It can be completed by calling @code{gpgme_wait} on the context. @xref{Waiting For Completion}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation was started successfully, and @code{GPG_ERR_INV_VALUE} if @var{ctx} or @var{key} is not a valid pointer. @end deftypefun @deftypefun gpgme_error_t gpgme_data_new_with_read_cb (@w{gpgme_data_t *@var{dh}}, @w{int (*@var{readfunc})} (@w{void *@var{hook}}, @w{char *@var{buffer}}, @w{size_t @var{count}}, @w{size_t *@var{nread}}), @w{void *@var{hook_value}}) The function @code{gpgme_data_new_with_read_cb} creates a new @code{gpgme_data_t} object and uses the callback function @var{readfunc} to retrieve the data on demand. As the callback function can supply the data in any way it wants, this is the most flexible data type @acronym{GPGME} provides. However, it can not be used to write data. The callback function receives @var{hook_value} as its first argument whenever it is invoked. It should return up to @var{count} bytes in @var{buffer}, and return the number of bytes actually read in @var{nread}. It may return @code{0} in @var{nread} if no data is currently available. To indicate @code{EOF} the function should return with an error code of @code{-1} and set @var{nread} to @code{0}. The callback function may support to reset its internal read pointer if it is invoked with @var{buffer} and @var{nread} being @code{NULL} and @var{count} being @code{0}. The function returns the error code @code{GPG_ERR_NO_ERROR} if the data object was successfully created, @code{GPG_ERR_INV_VALUE} if @var{dh} or @var{readfunc} is not a valid pointer, and @code{GPG_ERR_ENOMEM} if not enough memory is available. @end deftypefun @deftypefun gpgme_error_t gpgme_data_rewind (@w{gpgme_data_t @var{dh}}) The function @code{gpgme_data_rewind} is equivalent to: @example return (gpgme_data_seek (dh, 0, SEEK_SET) == -1) ? gpgme_error_from_errno (errno) : 0; @end example @end deftypefun @deftp {Data type} gpgme_attr_t The @code{gpgme_attr_t} type is used to specify a key or trust item attribute. The following attributes are defined: @table @code @item GPGME_ATTR_KEYID This is the key ID of a sub key. It is representable as a string. For trust items, the trust item refers to the key with this ID. @item GPGME_ATTR_FPR This is the fingerprint of a sub key. It is representable as a string. @item GPGME_ATTR_ALGO This is the crypto algorithm for which the sub key can be used. It is representable as a string and as a number. The numbers correspond to the @code{enum gcry_pk_algos} values in the gcrypt library. @item GPGME_ATTR_LEN This is the key length of a sub key. It is representable as a number. @item GPGME_ATTR_CREATED This is the timestamp at creation time of a sub key. It is representable as a number. @item GPGME_ATTR_EXPIRE This is the expiration time of a sub key. It is representable as a number. @item GPGME_ATTR_OTRUST XXX FIXME (also for trust items) @item GPGME_ATTR_USERID This is a user ID. There can be more than one user IDs in a @var{gpgme_key_t} object. The first one (with index 0) is the primary user ID. The user ID is representable as a number. For trust items, this is the user ID associated with this trust item. @item GPGME_ATTR_NAME This is the name belonging to a user ID. It is representable as a string. @item GPGME_ATTR_EMAIL This is the email address belonging to a user ID. It is representable as a string. @item GPGME_ATTR_COMMENT This is the comment belonging to a user ID. It is representable as a string. @item GPGME_ATTR_VALIDITY This is the validity belonging to a user ID. It is representable as a string and as a number. See below for a list of available validities. For trust items, this is the validity that is associated with this trust item. @item GPGME_ATTR_UID_REVOKED This specifies if a user ID is revoked. It is representable as a number, and is @code{1} if the user ID is revoked, and @code{0} otherwise. @item GPGME_ATTR_UID_INVALID This specifies if a user ID is invalid. It is representable as a number, and is @code{1} if the user ID is invalid, and @code{0} otherwise. @item GPGME_ATTR_LEVEL This is the trust level of a trust item. @item GPGME_ATTR_TYPE This returns information about the type of key. For the string function this will eother be "PGP" or "X.509". The integer function returns 0 for PGP and 1 for X.509. It is also used for the type of a trust item. @item GPGME_ATTR_IS_SECRET This specifies if the key is a secret key. It is representable as a number, and is @code{1} if the key is revoked, and @code{0} otherwise. @item GPGME_ATTR_KEY_REVOKED This specifies if a sub key is revoked. It is representable as a number, and is @code{1} if the key is revoked, and @code{0} otherwise. @item GPGME_ATTR_KEY_INVALID This specifies if a sub key is invalid. It is representable as a number, and is @code{1} if the key is invalid, and @code{0} otherwise. @item GPGME_ATTR_KEY_EXPIRED This specifies if a sub key is expired. It is representable as a number, and is @code{1} if the key is expired, and @code{0} otherwise. @item GPGME_ATTR_KEY_DISABLED This specifies if a sub key is disabled. It is representable as a number, and is @code{1} if the key is disabled, and @code{0} otherwise. @item GPGME_ATTR_KEY_CAPS This is a description of the capabilities of a sub key. It is representable as a string. The string contains the letter ``e'' if the key can be used for encryption, ``s'' if the key can be used for signatures, and ``c'' if the key can be used for certifications. @item GPGME_ATTR_CAN_ENCRYPT This specifies if a sub key can be used for encryption. It is representable as a number, and is @code{1} if the sub key can be used for encryption, and @code{0} otherwise. @item GPGME_ATTR_CAN_SIGN This specifies if a sub key can be used to create data signatures. It is representable as a number, and is @code{1} if the sub key can be used for signatures, and @code{0} otherwise. @item GPGME_ATTR_CAN_CERTIFY This specifies if a sub key can be used to create key certificates. It is representable as a number, and is @code{1} if the sub key can be used for certifications, and @code{0} otherwise. @item GPGME_ATTR_SERIAL The X.509 issuer serial attribute of the key. It is representable as a string. @item GPGME_ATTR_ISSUE The X.509 issuer name attribute of the key. It is representable as a string. @item GPGME_ATTR_CHAINID The X.509 chain ID can be used to build the certification chain. It is representable as a string. @end table @end deftp @deftypefun {const char *} gpgme_key_get_string_attr (@w{gpgme_key_t @var{key}}, @w{gpgme_attr_t @var{what}}, @w{const void *@var{reserved}}, @w{int @var{idx}}) The function @code{gpgme_key_get_string_attr} returns the value of the string-representable attribute @var{what} of key @var{key}. If the attribute is an attribute of a sub key or an user ID, @var{idx} specifies the sub key or user ID of which the attribute value is returned. The argument @var{reserved} is reserved for later use and should be @code{NULL}. The string returned is only valid as long as the key is valid. The function returns @code{0} if an attribute can't be returned as a string, @var{key} is not a valid pointer, @var{idx} out of range, or @var{reserved} not @code{NULL}. @end deftypefun @deftypefun {unsigned long} gpgme_key_get_ulong_attr (@w{gpgme_key_t @var{key}}, @w{gpgme_attr_t @var{what}}, @w{const void *@var{reserved}}, @w{int @var{idx}}) The function @code{gpgme_key_get_ulong_attr} returns the value of the number-representable attribute @var{what} of key @var{key}. If the attribute is an attribute of a sub key or an user ID, @var{idx} specifies the sub key or user ID of which the attribute value is returned. The argument @var{reserved} is reserved for later use and should be @code{NULL}. The function returns @code{0} if the attribute can't be returned as a number, @var{key} is not a valid pointer, @var{idx} out of range, or @var{reserved} not @code{NULL}. @end deftypefun @c @c Key Signatures @c The signatures on a key are only available if the key was retrieved via a listing operation with the @code{GPGME_KEYLIST_MODE_SIGS} mode enabled, because it is expensive to retrieve all signatures of a key. So, before using the below interfaces to retrieve the signatures on a key, you have to make sure that the key was listed with signatures enabled. One convenient, but blocking, way to do this is to use the function @code{gpgme_get_key}. @deftp {Data type} gpgme_attr_t The @code{gpgme_attr_t} type is used to specify a key signature attribute. The following attributes are defined: @table @code @item GPGME_ATTR_KEYID This is the key ID of the key which was used for the signature. It is representable as a string. @item GPGME_ATTR_ALGO This is the crypto algorithm used to create the signature. It is representable as a string and as a number. The numbers correspond to the @code{enum gcry_pk_algos} values in the gcrypt library. @item GPGME_ATTR_CREATED This is the timestamp at creation time of the signature. It is representable as a number. @item GPGME_ATTR_EXPIRE This is the expiration time of the signature. It is representable as a number. @item GPGME_ATTR_USERID This is the user ID associated with the signing key. The user ID is representable as a number. @item GPGME_ATTR_NAME This is the name belonging to a user ID. It is representable as a string. @item GPGME_ATTR_EMAIL This is the email address belonging to a user ID. It is representable as a string. @item GPGME_ATTR_COMMENT This is the comment belonging to a user ID. It is representable as a string. @item GPGME_ATTR_KEY_REVOKED This specifies if a key signature is a revocation signature. It is representable as a number, and is @code{1} if the key is revoked, and @code{0} otherwise. @c @item GPGME_ATTR_KEY_EXPIRED @c This specifies if a key signature is expired. It is representable as @c a number, and is @code{1} if the key is revoked, and @code{0} @c otherwise. @c @item GPGME_ATTR_SIG_CLASS This specifies the signature class of a key signature. It is representable as a number. The meaning is specific to the crypto engine. @item GPGME_ATTR_SIG_CLASS This specifies the signature class of a key signature. It is representable as a number. The meaning is specific to the crypto engine. @item GPGME_ATTR_SIG_STATUS This is the same value as returned by @code{gpgme_get_sig_status}. @end table @end deftp @deftypefun {const char *} gpgme_key_sig_get_string_attr (@w{gpgme_key_t @var{key}}, @w{int @var{uid_idx}}, @w{gpgme_attr_t @var{what}}, @w{const void *@var{reserved}}, @w{int @var{idx}}) The function @code{gpgme_key_sig_get_string_attr} returns the value of the string-representable attribute @var{what} of the signature @var{idx} on the user ID @var{uid_idx} in the key @var{key}. The argument @var{reserved} is reserved for later use and should be @code{NULL}. The string returned is only valid as long as the key is valid. The function returns @code{0} if an attribute can't be returned as a string, @var{key} is not a valid pointer, @var{uid_idx} or @var{idx} out of range, or @var{reserved} not @code{NULL}. @end deftypefun @deftypefun {unsigned long} gpgme_key_sig_get_ulong_attr (@w{gpgme_key_t @var{key}}, @w{int @var{uid_idx}}, @w{gpgme_attr_t @var{what}}, @w{const void *@var{reserved}}, @w{int @var{idx}}) The function @code{gpgme_key_sig_get_ulong_attr} returns the value of the number-representable attribute @var{what} of the signature @var{idx} on the user ID @var{uid_idx} in the key @var{key}. The argument @var{reserved} is reserved for later use and should be @code{NULL}. The function returns @code{0} if an attribute can't be returned as a string, @var{key} is not a valid pointer, @var{uid_idx} or @var{idx} out of range, or @var{reserved} not @code{NULL}. @end deftypefun @c node Information About Trust Items @c subsection Information About Trust Items @c cindex trust item, information about @c cindex trust item, attributes @c cindex attributes, of a trust item Trust items have attributes which can be queried using the interfaces below. The attribute identifiers are shared with those for key attributes. @xref{Information About Keys}. @deftypefun {const char *} gpgme_trust_item_get_string_attr (@w{gpgme_trust_item_t @var{item}}, @w{gpgme_attr_t @var{what}}, @w{const void *@var{reserved}}, @w{int @var{idx}}) The function @code{gpgme_trust_item_get_string_attr} returns the value of the string-representable attribute @var{what} of trust item @var{item}. The arguments @var{idx} and @var{reserved} are reserved for later use and should be @code{0} and @code{NULL} respectively. The string returned is only valid as long as the key is valid. The function returns @code{0} if an attribute can't be returned as a string, @var{key} is not a valid pointer, @var{idx} out of range, or @var{reserved} not @code{NULL}. @end deftypefun @deftypefun int gpgme_trust_item_get_int_attr (@w{gpgme_trust_item_t @var{item}}, @w{gpgme_attr_t @var{what}}, @w{const void *@var{reserved}}, @w{int @var{idx}}) The function @code{gpgme_trust_item_get_int_attr} returns the value of the number-representable attribute @var{what} of trust item @var{item}. If the attribute occurs more than once in the trust item, the index is specified by @var{idx}. However, currently no such attribute exists, so @var{idx} should be @code{0}. The argument @var{reserved} is reserved for later use and should be @code{NULL}. The function returns @code{0} if the attribute can't be returned as a number, @var{key} is not a valid pointer, @var{idx} out of range, or @var{reserved} not @code{NULL}. @end deftypefun @deftp {Data type} {enum gpgme_sig_stat_t} @tindex gpgme_sig_stat_t The @code{gpgme_sig_stat_t} type holds the result of a signature check, or the combined result of all signatures. The following results are possible: @table @code @item GPGME_SIG_STAT_NONE This status should not occur in normal operation. @item GPGME_SIG_STAT_GOOD This status indicates that the signature is valid. For the combined result this status means that all signatures are valid. @item GPGME_SIG_STAT_GOOD_EXP This status indicates that the signature is valid but expired. For the combined result this status means that all signatures are valid and expired. @item GPGME_SIG_STAT_GOOD_EXPKEY This status indicates that the signature is valid but the key used to verify the signature has expired. For the combined result this status means that all signatures are valid and all keys are expired. @item GPGME_SIG_STAT_BAD This status indicates that the signature is invalid. For the combined result this status means that all signatures are invalid. @item GPGME_SIG_STAT_NOKEY This status indicates that the signature could not be verified due to a missing key. For the combined result this status means that all signatures could not be checked due to missing keys. @item GPGME_SIG_STAT_NOSIG This status indicates that the signature data provided was not a real signature. @item GPGME_SIG_STAT_ERROR This status indicates that there was some other error which prevented the signature verification. @item GPGME_SIG_STAT_DIFF For the combined result this status means that at least two signatures have a different status. You can get each key's status with @code{gpgme_get_sig_status}. @end table @end deftp @deftypefun {const char *} gpgme_get_sig_status (@w{gpgme_ctx_t @var{ctx}}, @w{int @var{idx}}, @w{gpgme_sig_stat_t *@var{r_stat}}, @w{time_t *@var{r_created}}) The function @code{gpgme_get_sig_status} is equivalent to: @example gpgme_verify_result_t result; gpgme_signature_t sig; result = gpgme_op_verify_result (ctx); sig = result->signatures; while (sig && idx) @{ sig = sig->next; idx--; @} if (!sig || idx) return NULL; if (r_stat) @{ switch (gpg_err_code (sig->status)) @{ case GPG_ERR_NO_ERROR: *r_stat = GPGME_SIG_STAT_GOOD; break; case GPG_ERR_BAD_SIGNATURE: *r_stat = GPGME_SIG_STAT_BAD; break; case GPG_ERR_NO_PUBKEY: *r_stat = GPGME_SIG_STAT_NOKEY; break; case GPG_ERR_NO_DATA: *r_stat = GPGME_SIG_STAT_NOSIG; break; case GPG_ERR_SIG_EXPIRED: *r_stat = GPGME_SIG_STAT_GOOD_EXP; break; case GPG_ERR_KEY_EXPIRED: *r_stat = GPGME_SIG_STAT_GOOD_EXPKEY; break; default: *r_stat = GPGME_SIG_STAT_ERROR; break; @} @} if (r_created) *r_created = sig->timestamp; return sig->fpr; @end example @end deftypefun @deftypefun {const char *} gpgme_get_sig_string_attr (@w{gpgme_ctx_t @var{ctx}}, @w{int @var{idx}}, @w{gpgme_attr_t @var{what}}, @w{int @var{whatidx}}) The function @code{gpgme_get_sig_string_attr} is equivalent to: @example gpgme_verify_result_t result; gpgme_signature_t sig; result = gpgme_op_verify_result (ctx); sig = result->signatures; while (sig && idx) @{ sig = sig->next; idx--; @} if (!sig || idx) return NULL; switch (what) @{ case GPGME_ATTR_FPR: return sig->fpr; case GPGME_ATTR_ERRTOK: if (whatidx == 1) return sig->wrong_key_usage ? "Wrong_Key_Usage" : ""; else return ""; default: break; @} return NULL; @end example @end deftypefun @deftypefun {const char *} gpgme_get_sig_ulong_attr (@w{gpgme_ctx_t @var{ctx}}, @w{int @var{idx}}, @w{gpgme_attr_t @var{waht}}, @w{int @var{whatidx}}) The function @code{gpgme_get_sig_ulong_attr} is equivalent to: @example gpgme_verify_result_t result; gpgme_signature_t sig; result = gpgme_op_verify_result (ctx); sig = result->signatures; while (sig && idx) @{ sig = sig->next; idx--; @} if (!sig || idx) return 0; switch (what) @{ case GPGME_ATTR_CREATED: return sig->timestamp; case GPGME_ATTR_EXPIRE: return sig->exp_timestamp; case GPGME_ATTR_VALIDITY: return (unsigned long) sig->validity; case GPGME_ATTR_SIG_STATUS: switch (sig->status) @{ case GPG_ERR_NO_ERROR: return GPGME_SIG_STAT_GOOD; case GPG_ERR_BAD_SIGNATURE: return GPGME_SIG_STAT_BAD; case GPG_ERR_NO_PUBKEY: return GPGME_SIG_STAT_NOKEY; case GPG_ERR_NO_DATA: return GPGME_SIG_STAT_NOSIG; case GPG_ERR_SIG_EXPIRED: return GPGME_SIG_STAT_GOOD_EXP; case GPG_ERR_KEY_EXPIRED: return GPGME_SIG_STAT_GOOD_EXPKEY; default: return GPGME_SIG_STAT_ERROR; @} case GPGME_ATTR_SIG_SUMMARY: return sig->summary; default: break; @} return 0; @end example @end deftypefun @deftypefun {const char *} gpgme_get_sig_key (@w{gpgme_ctx_t @var{ctx}}, @w{int @var{idx}}, @w{gpgme_key_t *@var{r_key}}) The function @code{gpgme_get_sig_key} is equivalent to: @example gpgme_verify_result_t result; gpgme_signature_t sig; result = gpgme_op_verify_result (ctx); sig = result->signatures; while (sig && idx) @{ sig = sig->next; idx--; @} if (!sig || idx) return gpg_error (GPG_ERR_EOF); return gpgme_get_key (ctx, sig->fpr, r_key, 0); @end example @end deftypefun @include lesser.texi @include gpl.texi @node Concept Index @unnumbered Concept Index @printindex cp @node Function and Data Index @unnumbered Function and Data Index @printindex fn @bye diff --git a/src/encrypt-sign.c b/src/encrypt-sign.c index af6de63e..4db46e25 100644 --- a/src/encrypt-sign.c +++ b/src/encrypt-sign.c @@ -1,179 +1,218 @@ /* encrypt-sign.c - encrypt and verify functions Copyright (C) 2000 Werner Koch (dd9jn) Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH This file is part of GPGME. GPGME is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. GPGME is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #if HAVE_CONFIG_H #include #endif #include #include #include #include "gpgme.h" #include "debug.h" #include "context.h" #include "ops.h" static gpgme_error_t encrypt_sign_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_error_t err; err = _gpgme_progress_status_handler (priv, code, args); if (!err) err = _gpgme_encrypt_status_handler (priv, code, args); if (!err) err = _gpgme_sign_status_handler (priv, code, args); return err; } static gpgme_error_t encrypt_sym_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_error_t err; err = _gpgme_progress_status_handler (priv, code, args); if (!err) err = _gpgme_sign_status_handler (priv, code, args); if (!err) err = _gpgme_passphrase_status_handler (priv, code, args); return err; } static gpgme_error_t encrypt_sign_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t recp[], + const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher) { gpgme_error_t err; int symmetric; err = _gpgme_op_reset (ctx, synchronous); if (err) return err; - symmetric = !recp || (flags & GPGME_ENCRYPT_SYMMETRIC); + symmetric = (!recp && !recpstring) || (flags & GPGME_ENCRYPT_SYMMETRIC); if (!plain) return gpg_error (GPG_ERR_NO_DATA); if (!cipher) return gpg_error (GPG_ERR_INV_VALUE); if (recp && !*recp) return gpg_error (GPG_ERR_INV_VALUE); err = _gpgme_op_encrypt_init_result (ctx); if (err) return err; err = _gpgme_op_sign_init_result (ctx); if (err) return err; if (ctx->passphrase_cb) { err = _gpgme_engine_set_command_handler (ctx->engine, _gpgme_passphrase_command_handler, ctx, NULL); if (err) return err; } _gpgme_engine_set_status_handler (ctx->engine, symmetric ? encrypt_sym_status_handler : encrypt_sign_status_handler, ctx); - return _gpgme_engine_op_encrypt_sign (ctx->engine, recp, flags, plain, + return _gpgme_engine_op_encrypt_sign (ctx->engine, recp, recpstring, + flags, plain, cipher, ctx->use_armor, ctx /* FIXME */); } -/* Encrypt plaintext PLAIN within CTX for the recipients RECP and - store the resulting ciphertext in CIPHER. Also sign the ciphertext - with the signers in CTX. */ +/* Old version of gpgme_op_encrypt_sign_ext_start w/o RECPSTRING. */ gpgme_error_t gpgme_op_encrypt_sign_start (gpgme_ctx_t ctx, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher) +{ + return gpgme_op_encrypt_sign_ext_start (ctx, recp, NULL, + flags, plain, cipher); +} + + +/* Old version of gpgme_op_encrypt_sign_ext w/o RECPSTRING. */ +gpgme_error_t +gpgme_op_encrypt_sign (gpgme_ctx_t ctx, gpgme_key_t recp[], + gpgme_encrypt_flags_t flags, + gpgme_data_t plain, gpgme_data_t cipher) +{ + return gpgme_op_encrypt_sign_ext (ctx, recp, NULL, flags, plain, cipher); +} + + +/* Encrypt plaintext PLAIN within CTX for the recipients RECP and + * store the resulting ciphertext in CIPHER. Also sign the ciphertext + * with the signers in CTX. */ +gpgme_error_t +gpgme_op_encrypt_sign_ext (gpgme_ctx_t ctx, gpgme_key_t recp[], + const char *recpstring, + gpgme_encrypt_flags_t flags, + gpgme_data_t plain, gpgme_data_t cipher) { gpgme_error_t err; - TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_sign_start", ctx, + TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_sign", ctx, "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); - if (_gpgme_debug_trace () && recp) + if (_gpgme_debug_trace () && (recp || recpstring)) { - int i = 0; - - while (recp[i]) - { - TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i], - (recp[i]->subkeys && recp[i]->subkeys->fpr) ? - recp[i]->subkeys->fpr : "invalid"); - i++; - } + if (recp) + { + int i = 0; + + while (recp[i]) + { + TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i], + (recp[i]->subkeys && recp[i]->subkeys->fpr) ? + recp[i]->subkeys->fpr : "invalid"); + i++; + } + } + else + { + TRACE_LOG1 ("recipients = '%s'", recpstring); + } } - err = encrypt_sign_start (ctx, 0, recp, flags, plain, cipher); - return err; + err = encrypt_sign_start (ctx, 1, recp, recpstring, flags, plain, cipher); + if (!err) + err = _gpgme_wait_one (ctx); + return TRACE_ERR (err); } /* Encrypt plaintext PLAIN within CTX for the recipients RECP and store the resulting ciphertext in CIPHER. Also sign the ciphertext with the signers in CTX. */ gpgme_error_t -gpgme_op_encrypt_sign (gpgme_ctx_t ctx, gpgme_key_t recp[], - gpgme_encrypt_flags_t flags, - gpgme_data_t plain, gpgme_data_t cipher) +gpgme_op_encrypt_sign_ext_start (gpgme_ctx_t ctx, gpgme_key_t recp[], + const char *recpstring, + gpgme_encrypt_flags_t flags, + gpgme_data_t plain, gpgme_data_t cipher) { gpgme_error_t err; - TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_sign", ctx, + TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_sign_start", ctx, "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); - if (_gpgme_debug_trace () && recp) + if (_gpgme_debug_trace () && (recp || recpstring)) { - int i = 0; - - while (recp[i]) - { - TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i], - (recp[i]->subkeys && recp[i]->subkeys->fpr) ? - recp[i]->subkeys->fpr : "invalid"); - i++; - } + if (recp) + { + int i = 0; + + while (recp[i]) + { + TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i], + (recp[i]->subkeys && recp[i]->subkeys->fpr) ? + recp[i]->subkeys->fpr : "invalid"); + i++; + } + } + else + { + TRACE_LOG1 ("recipients = '%s'", recpstring); + } } - err = encrypt_sign_start (ctx, 1, recp, flags, plain, cipher); - if (!err) - err = _gpgme_wait_one (ctx); - return TRACE_ERR (err); + err = encrypt_sign_start (ctx, 0, recp, recpstring, flags, plain, cipher); + return err; } diff --git a/src/encrypt.c b/src/encrypt.c index 40236544..2318497e 100644 --- a/src/encrypt.c +++ b/src/encrypt.c @@ -1,323 +1,363 @@ /* encrypt.c - Encrypt function. Copyright (C) 2000 Werner Koch (dd9jn) Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH This file is part of GPGME. GPGME is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. GPGME is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #if HAVE_CONFIG_H #include #endif #include #include #include #include "gpgme.h" #include "debug.h" #include "context.h" #include "ops.h" typedef struct { struct _gpgme_op_encrypt_result result; /* The error code from a FAILURE status line or 0. */ gpg_error_t failure_code; /* The fingerprint from the last KEY_CONSIDERED status line. */ char *kc_fpr; /* The flags from the last KEY_CONSIDERED status line. */ unsigned int kc_flags; /* A pointer to the next pointer of the last invalid recipient in the list. This makes appending new invalid recipients painless while preserving the order. */ gpgme_invalid_key_t *lastp; } *op_data_t; static void release_op_data (void *hook) { op_data_t opd = (op_data_t) hook; gpgme_invalid_key_t invalid_recipient = opd->result.invalid_recipients; while (invalid_recipient) { gpgme_invalid_key_t next = invalid_recipient->next; if (invalid_recipient->fpr) free (invalid_recipient->fpr); free (invalid_recipient); invalid_recipient = next; } free (opd->kc_fpr); } gpgme_encrypt_result_t gpgme_op_encrypt_result (gpgme_ctx_t ctx) { void *hook; op_data_t opd; gpgme_error_t err; TRACE_BEG (DEBUG_CTX, "gpgme_op_encrypt_result", ctx); err = _gpgme_op_data_lookup (ctx, OPDATA_ENCRYPT, &hook, -1, NULL); opd = hook; if (err || !opd) { TRACE_SUC0 ("result=(null)"); return NULL; } if (_gpgme_debug_trace ()) { gpgme_invalid_key_t invkeys = opd->result.invalid_recipients; int i = 0; while (invkeys) { TRACE_LOG3 ("invalid_recipients[%i] = %s (%s)", i, invkeys->fpr ? invkeys->fpr : "(null)", gpg_strerror (invkeys->reason)); invkeys = invkeys->next; i++; } } TRACE_SUC1 ("result=%p", &opd->result); return &opd->result; } gpgme_error_t _gpgme_encrypt_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_data_lookup (ctx, OPDATA_ENCRYPT, &hook, -1, NULL); opd = hook; if (err) return err; switch (code) { case GPGME_STATUS_FAILURE: opd->failure_code = _gpgme_parse_failure (args); break; case GPGME_STATUS_EOF: if (opd->result.invalid_recipients) return gpg_error (GPG_ERR_UNUSABLE_PUBKEY); if (opd->failure_code) return opd->failure_code; break; case GPGME_STATUS_KEY_CONSIDERED: /* This is emitted during gpg's key lookup to give information * about the lookup results. We store the last one so it can be * used in connection with INV_RECP. */ free (opd->kc_fpr); opd->kc_fpr = NULL; err = _gpgme_parse_key_considered (args, &opd->kc_fpr, &opd->kc_flags); if (err) return err; break; case GPGME_STATUS_INV_RECP: err = _gpgme_parse_inv_recp (args, 0, opd->kc_fpr, opd->kc_flags, opd->lastp); if (err) return err; opd->lastp = &(*opd->lastp)->next; free (opd->kc_fpr); opd->kc_fpr = NULL; break; case GPGME_STATUS_NO_RECP: /* Should not happen, because we require at least one recipient. */ return gpg_error (GPG_ERR_GENERAL); default: break; } return 0; } static gpgme_error_t encrypt_sym_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_error_t err; err = _gpgme_progress_status_handler (priv, code, args); if (!err) err = _gpgme_passphrase_status_handler (priv, code, args); return err; } static gpgme_error_t encrypt_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_error_t err; err = _gpgme_progress_status_handler (priv, code, args); if (!err) err = _gpgme_encrypt_status_handler (priv, code, args); return err; } gpgme_error_t _gpgme_op_encrypt_init_result (gpgme_ctx_t ctx) { gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_data_lookup (ctx, OPDATA_ENCRYPT, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return err; opd->lastp = &opd->result.invalid_recipients; return 0; } static gpgme_error_t encrypt_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t recp[], + const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher) { gpgme_error_t err; int symmetric = 0; err = _gpgme_op_reset (ctx, synchronous); if (err) return err; err = _gpgme_op_encrypt_init_result (ctx); if (err) return err; - symmetric = !recp || (flags & GPGME_ENCRYPT_SYMMETRIC); + symmetric = (!recp && !recpstring) || (flags & GPGME_ENCRYPT_SYMMETRIC); if (!plain) return gpg_error (GPG_ERR_NO_DATA); if (!cipher) return gpg_error (GPG_ERR_INV_VALUE); - if (recp && ! *recp) + if (recp && !*recp) return gpg_error (GPG_ERR_INV_VALUE); if (symmetric && ctx->passphrase_cb) { /* Symmetric encryption requires a passphrase. */ err = _gpgme_engine_set_command_handler (ctx->engine, _gpgme_passphrase_command_handler, ctx, NULL); if (err) return err; } _gpgme_engine_set_status_handler (ctx->engine, symmetric ? encrypt_sym_status_handler : encrypt_status_handler, ctx); - return _gpgme_engine_op_encrypt (ctx->engine, recp, flags, plain, cipher, - ctx->use_armor); + return _gpgme_engine_op_encrypt (ctx->engine, recp, recpstring, + flags, plain, cipher, ctx->use_armor); } +/* Old version of gpgme_op_encrypt_ext without RECPSTRING. */ +gpgme_error_t +gpgme_op_encrypt (gpgme_ctx_t ctx, gpgme_key_t recp[], + gpgme_encrypt_flags_t flags, + gpgme_data_t plain, gpgme_data_t cipher) +{ + return gpgme_op_encrypt_ext (ctx, recp, NULL, flags, plain, cipher); +} + + +/* Old version of gpgme_op_encrypt_ext_start without RECPSTRING. */ gpgme_error_t gpgme_op_encrypt_start (gpgme_ctx_t ctx, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher) +{ + return gpgme_op_encrypt_ext_start (ctx, recp, NULL, flags, plain, cipher); +} + + +/* Encrypt plaintext PLAIN within CTX for the recipients RECP and + * store the resulting ciphertext in CIPHER. RECPSTRING can be used + * instead of the RECP array to directly specify recipients as LF + * delimited strings; these may be any kind of recipient specification + * patterns as supported by the backend. */ +gpgme_error_t +gpgme_op_encrypt_ext (gpgme_ctx_t ctx, gpgme_key_t recp[], + const char *recpstring, + gpgme_encrypt_flags_t flags, + gpgme_data_t plain, gpgme_data_t cipher) { gpgme_error_t err; - TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_start", ctx, + TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt", ctx, "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); - if (_gpgme_debug_trace () && recp) + if (_gpgme_debug_trace () && (recp || recpstring)) { - int i = 0; + if (recp) + { + int i = 0; - while (recp[i]) - { - TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i], + while (recp[i]) + { + TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i], (recp[i]->subkeys && recp[i]->subkeys->fpr) ? - recp[i]->subkeys->fpr : "invalid"); - i++; - } + recp[i]->subkeys->fpr : "invalid"); + i++; + } + } + else + { + TRACE_LOG1 ("recipients = '%s'", recpstring); + } } - err = encrypt_start (ctx, 0, recp, flags, plain, cipher); + err = encrypt_start (ctx, 1, recp, recpstring, flags, plain, cipher); + if (!err) + err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } -/* Encrypt plaintext PLAIN within CTX for the recipients RECP and - store the resulting ciphertext in CIPHER. */ gpgme_error_t -gpgme_op_encrypt (gpgme_ctx_t ctx, gpgme_key_t recp[], - gpgme_encrypt_flags_t flags, - gpgme_data_t plain, gpgme_data_t cipher) +gpgme_op_encrypt_ext_start (gpgme_ctx_t ctx, gpgme_key_t recp[], + const char *recpstring, + gpgme_encrypt_flags_t flags, + gpgme_data_t plain, gpgme_data_t cipher) { gpgme_error_t err; - TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt", ctx, + TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_start", ctx, "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); - if (_gpgme_debug_trace () && recp) + if (_gpgme_debug_trace () && (recp || recpstring)) { - int i = 0; - - while (recp[i]) - { - TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i], - (recp[i]->subkeys && recp[i]->subkeys->fpr) ? - recp[i]->subkeys->fpr : "invalid"); - i++; - } + if (recp) + { + int i = 0; + + while (recp[i]) + { + TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i], + (recp[i]->subkeys && recp[i]->subkeys->fpr) ? + recp[i]->subkeys->fpr : "invalid"); + i++; + } + } + else + { + TRACE_LOG1 ("recipients = '%s'", recpstring); + } } - err = encrypt_start (ctx, 1, recp, flags, plain, cipher); - if (!err) - err = _gpgme_wait_one (ctx); + err = encrypt_start (ctx, 0, recp, recpstring, flags, plain, cipher); return TRACE_ERR (err); } diff --git a/src/engine-backend.h b/src/engine-backend.h index 97cf6a10..f6926662 100644 --- a/src/engine-backend.h +++ b/src/engine-backend.h @@ -1,186 +1,188 @@ /* engine-backend.h - A crypto backend for the engine interface. Copyright (C) 2002, 2003, 2004, 2009 g10 Code GmbH This file is part of GPGME. GPGME is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. GPGME is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, see . */ #ifndef ENGINE_BACKEND_H #define ENGINE_BACKEND_H #include "engine.h" struct engine_ops { /* Static functions. */ /* Return the default file name for the binary of this engine. */ const char *(*get_file_name) (void); /* Return the default home dir for the binary of this engine. If this function pointer is not set, the standard default home dir of the engine is used. */ const char *(*get_home_dir) (void); /* Returns a malloced string containing the version of the engine with the given binary file name (or the default if FILE_NAME is NULL. */ char *(*get_version) (const char *file_name); /* Returns a statically allocated string containing the required version. */ const char *(*get_req_version) (void); gpgme_error_t (*new) (void **r_engine, const char *file_name, const char *home_dir, const char *version); /* Member functions. */ void (*release) (void *engine); gpgme_error_t (*reset) (void *engine); void (*set_status_cb) (void *engine, gpgme_status_cb_t cb, void *cb_value); void (*set_status_handler) (void *engine, engine_status_handler_t fnc, void *fnc_value); gpgme_error_t (*set_command_handler) (void *engine, engine_command_handler_t fnc, void *fnc_value, gpgme_data_t data); gpgme_error_t (*set_colon_line_handler) (void *engine, engine_colon_line_handler_t fnc, void *fnc_value); gpgme_error_t (*set_locale) (void *engine, int category, const char *value); gpgme_error_t (*set_protocol) (void *engine, gpgme_protocol_t protocol); void (*set_engine_flags) (void *engine, gpgme_ctx_t ctx); gpgme_error_t (*decrypt) (void *engine, gpgme_decrypt_flags_t flags, gpgme_data_t ciph, gpgme_data_t plain, int export_session_key, const char *override_session_key, int auto_key_retrieve); gpgme_error_t (*delete) (void *engine, gpgme_key_t key, unsigned int flags); gpgme_error_t (*edit) (void *engine, int type, gpgme_key_t key, gpgme_data_t out, gpgme_ctx_t ctx /* FIXME */); gpgme_error_t (*encrypt) (void *engine, gpgme_key_t recp[], + const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor); gpgme_error_t (*encrypt_sign) (void *engine, gpgme_key_t recp[], + const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor, gpgme_ctx_t ctx /* FIXME */); gpgme_error_t (*export) (void *engine, const char *pattern, gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor); gpgme_error_t (*export_ext) (void *engine, const char *pattern[], gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor); gpgme_error_t (*genkey) (void *engine, const char *userid, const char *algo, unsigned long reserved, unsigned long expires, gpgme_key_t key, unsigned int flags, gpgme_data_t help_data, unsigned int extraflags, gpgme_data_t pubkey, gpgme_data_t seckey); gpgme_error_t (*import) (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray); gpgme_error_t (*keylist) (void *engine, const char *pattern, int secret_only, gpgme_keylist_mode_t mode, int engine_flags); gpgme_error_t (*keylist_ext) (void *engine, const char *pattern[], int secret_only, int reserved, gpgme_keylist_mode_t mode, int engine_flags); gpgme_error_t (*keylist_data) (void *engine, gpgme_data_t data); gpgme_error_t (*keysign) (void *engine, gpgme_key_t key, const char *userid, unsigned long expires, unsigned int flags, gpgme_ctx_t ctx); gpgme_error_t (*tofu_policy) (void *engine, gpgme_key_t key, gpgme_tofu_policy_t policy); gpgme_error_t (*sign) (void *engine, gpgme_data_t in, gpgme_data_t out, gpgme_sig_mode_t mode, int use_armor, int use_textmode, int include_certs, gpgme_ctx_t ctx /* FIXME */); gpgme_error_t (*trustlist) (void *engine, const char *pattern); gpgme_error_t (*verify) (void *engine, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext, gpgme_ctx_t ctx); gpgme_error_t (*getauditlog) (void *engine, gpgme_data_t output, unsigned int flags); gpgme_error_t (*opassuan_transact) (void *engine, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value); gpgme_error_t (*conf_load) (void *engine, gpgme_conf_comp_t *conf_p); gpgme_error_t (*conf_save) (void *engine, gpgme_conf_comp_t conf); gpgme_error_t (*conf_dir) (void *engine, const char *what, char **result); gpgme_error_t (*query_swdb) (void *engine, const char *name, const char *iversion, gpgme_query_swdb_result_t result); void (*set_io_cbs) (void *engine, gpgme_io_cbs_t io_cbs); void (*io_event) (void *engine, gpgme_event_io_t type, void *type_data); /* Cancel the whole engine session. */ gpgme_error_t (*cancel) (void *engine); /* Cancel only the current operation, not the whole session. */ gpgme_error_t (*cancel_op) (void *engine); /* Change the passphrase for KEY. */ gpgme_error_t (*passwd) (void *engine, gpgme_key_t key, unsigned int flags); /* Set the pinentry mode. */ gpgme_error_t (*set_pinentry_mode) (void *engine, gpgme_pinentry_mode_t mode); /* The spawn command. */ gpgme_error_t (*opspawn) (void * engine, const char *file, const char *argv[], gpgme_data_t datain, gpgme_data_t dataout, gpgme_data_t dataerr, unsigned int flags); }; extern struct engine_ops _gpgme_engine_ops_gpg; /* OpenPGP. */ extern struct engine_ops _gpgme_engine_ops_gpgsm; /* CMS. */ extern struct engine_ops _gpgme_engine_ops_gpgconf; /* gpg-conf. */ extern struct engine_ops _gpgme_engine_ops_assuan; /* Low-level Assuan. */ extern struct engine_ops _gpgme_engine_ops_g13; /* Crypto VFS. */ #ifdef ENABLE_UISERVER extern struct engine_ops _gpgme_engine_ops_uiserver; #endif extern struct engine_ops _gpgme_engine_ops_spawn; /* Spawn engine. */ /* Prototypes for extra functions in engine-gpgconf.c */ gpgme_error_t _gpgme_conf_arg_new (gpgme_conf_arg_t *arg_p, gpgme_conf_type_t type, const void *value); void _gpgme_conf_arg_release (gpgme_conf_arg_t arg, gpgme_conf_type_t type); gpgme_error_t _gpgme_conf_opt_change (gpgme_conf_opt_t opt, int reset, gpgme_conf_arg_t arg); void _gpgme_conf_release (gpgme_conf_comp_t conf); gpgme_error_t _gpgme_conf_load (void *engine, gpgme_conf_comp_t *conf_p); #endif /* ENGINE_BACKEND_H */ diff --git a/src/engine-gpg.c b/src/engine-gpg.c index a37d1f7b..809806c4 100644 --- a/src/engine-gpg.c +++ b/src/engine-gpg.c @@ -1,3178 +1,3230 @@ /* engine-gpg.c - Gpg Engine. Copyright (C) 2000 Werner Koch (dd9jn) Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2012, 2013 g10 Code GmbH This file is part of GPGME. GPGME is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. GPGME is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, see . */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #include #ifdef HAVE_UNISTD_H # include #endif #ifdef HAVE_LOCALE_H #include #endif #include "gpgme.h" #include "util.h" #include "ops.h" #include "wait.h" #include "context.h" /*temp hack until we have GpmeData methods to do I/O */ #include "priv-io.h" #include "sema.h" #include "debug.h" #include "data.h" #include "engine-backend.h" /* This type is used to build a list of gpg arguments and data sources/sinks. */ struct arg_and_data_s { struct arg_and_data_s *next; gpgme_data_t data; /* If this is not NULL, use arg below. */ int inbound; /* True if this is used for reading from gpg. */ int dup_to; int print_fd; /* Print the fd number and not the special form of it. */ int *arg_locp; /* Write back the argv idx of this argument when building command line to this location. */ char arg[1]; /* Used if data above is not used. */ }; struct fd_data_map_s { gpgme_data_t data; int inbound; /* true if this is used for reading from gpg */ int dup_to; int fd; /* the fd to use */ int peer_fd; /* the other side of the pipe */ int arg_loc; /* The index into the argv for translation purposes. */ void *tag; }; /* NB.: R_LINE is allocated an gpgrt function and thus gpgrt_free * shall be used to release it. This takes care of custom memory * allocators and avoids problems on Windows with different runtimes * used for libgpg-error/gpgrt and gpgme. */ typedef gpgme_error_t (*colon_preprocessor_t) (char *line, char **rline); struct engine_gpg { char *file_name; char *version; char *lc_messages; char *lc_ctype; struct arg_and_data_s *arglist; struct arg_and_data_s **argtail; struct { int fd[2]; int arg_loc; size_t bufsize; char *buffer; size_t readpos; int eof; engine_status_handler_t fnc; void *fnc_value; gpgme_status_cb_t mon_cb; void *mon_cb_value; void *tag; } status; /* This is a kludge - see the comment at colon_line_handler. */ struct { int fd[2]; int arg_loc; size_t bufsize; char *buffer; size_t readpos; int eof; engine_colon_line_handler_t fnc; /* this indicate use of this structrue */ void *fnc_value; void *tag; colon_preprocessor_t preprocess_fnc; } colon; char **argv; struct fd_data_map_s *fd_data_map; /* stuff needed for interactive (command) mode */ struct { int used; int fd; void *cb_data; int idx; /* Index in fd_data_map */ gpgme_status_code_t code; /* last code */ char *keyword; /* what has been requested (malloced) */ engine_command_handler_t fnc; void *fnc_value; /* The kludges never end. This is used to couple command handlers with output data in edit key mode. */ gpgme_data_t linked_data; int linked_idx; } cmd; struct gpgme_io_cbs io_cbs; gpgme_pinentry_mode_t pinentry_mode; char request_origin[10]; struct { unsigned int no_symkey_cache : 1; } flags; /* NULL or the data object fed to --override_session_key-fd. */ gpgme_data_t override_session_key; }; typedef struct engine_gpg *engine_gpg_t; static void gpg_io_event (void *engine, gpgme_event_io_t type, void *type_data) { engine_gpg_t gpg = engine; TRACE3 (DEBUG_ENGINE, "gpgme:gpg_io_event", gpg, "event %p, type %d, type_data %p", gpg->io_cbs.event, type, type_data); if (gpg->io_cbs.event) (*gpg->io_cbs.event) (gpg->io_cbs.event_priv, type, type_data); } static void close_notify_handler (int fd, void *opaque) { engine_gpg_t gpg = opaque; assert (fd != -1); if (gpg->status.fd[0] == fd) { if (gpg->status.tag) (*gpg->io_cbs.remove) (gpg->status.tag); gpg->status.fd[0] = -1; } else if (gpg->status.fd[1] == fd) gpg->status.fd[1] = -1; else if (gpg->colon.fd[0] == fd) { if (gpg->colon.tag) (*gpg->io_cbs.remove) (gpg->colon.tag); gpg->colon.fd[0] = -1; } else if (gpg->colon.fd[1] == fd) gpg->colon.fd[1] = -1; else if (gpg->cmd.fd == fd) gpg->cmd.fd = -1; else if (gpg->fd_data_map) { int i; for (i = 0; gpg->fd_data_map[i].data; i++) { if (gpg->fd_data_map[i].fd == fd) { if (gpg->fd_data_map[i].tag) (*gpg->io_cbs.remove) (gpg->fd_data_map[i].tag); gpg->fd_data_map[i].fd = -1; break; } if (gpg->fd_data_map[i].peer_fd == fd) { gpg->fd_data_map[i].peer_fd = -1; break; } } } } /* If FRONT is true, push at the front of the list. Use this for options added late in the process. */ static gpgme_error_t _add_arg (engine_gpg_t gpg, const char *prefix, const char *arg, size_t arglen, int front, int *arg_locp) { struct arg_and_data_s *a; size_t prefixlen = prefix? strlen (prefix) : 0; assert (gpg); assert (arg); a = malloc (sizeof *a + prefixlen + arglen); if (!a) return gpg_error_from_syserror (); a->data = NULL; a->dup_to = -1; a->arg_locp = arg_locp; if (prefixlen) memcpy (a->arg, prefix, prefixlen); memcpy (a->arg + prefixlen, arg, arglen); a->arg[prefixlen + arglen] = 0; if (front) { a->next = gpg->arglist; if (!gpg->arglist) { /* If this is the first argument, we need to update the tail pointer. */ gpg->argtail = &a->next; } gpg->arglist = a; } else { a->next = NULL; *gpg->argtail = a; gpg->argtail = &a->next; } return 0; } static gpgme_error_t add_arg_ext (engine_gpg_t gpg, const char *arg, int front) { return _add_arg (gpg, NULL, arg, strlen (arg), front, NULL); } static gpgme_error_t add_arg_with_locp (engine_gpg_t gpg, const char *arg, int *locp) { return _add_arg (gpg, NULL, arg, strlen (arg), 0, locp); } static gpgme_error_t add_arg (engine_gpg_t gpg, const char *arg) { return _add_arg (gpg, NULL, arg, strlen (arg), 0, NULL); } static gpgme_error_t add_arg_pfx (engine_gpg_t gpg, const char *prefix, const char *arg) { return _add_arg (gpg, prefix, arg, strlen (arg), 0, NULL); } static gpgme_error_t add_arg_len (engine_gpg_t gpg, const char *prefix, const char *arg, size_t arglen) { return _add_arg (gpg, prefix, arg, arglen, 0, NULL); } static gpgme_error_t add_data (engine_gpg_t gpg, gpgme_data_t data, int dup_to, int inbound) { struct arg_and_data_s *a; assert (gpg); assert (data); a = malloc (sizeof *a - 1); if (!a) return gpg_error_from_syserror (); a->next = NULL; a->data = data; a->inbound = inbound; a->arg_locp = NULL; if (dup_to == -2) { a->print_fd = 1; a->dup_to = -1; } else { a->print_fd = 0; a->dup_to = dup_to; } *gpg->argtail = a; gpg->argtail = &a->next; return 0; } /* Return true if the engine's version is at least VERSION. */ static int have_gpg_version (engine_gpg_t gpg, const char *version) { return _gpgme_compare_versions (gpg->version, version); } static char * gpg_get_version (const char *file_name) { return _gpgme_get_program_version (file_name ? file_name : _gpgme_get_default_gpg_name ()); } static const char * gpg_get_req_version (void) { return "1.4.0"; } static void free_argv (char **argv) { int i; for (i = 0; argv[i]; i++) free (argv[i]); free (argv); } static void free_fd_data_map (struct fd_data_map_s *fd_data_map) { int i; if (!fd_data_map) return; for (i = 0; fd_data_map[i].data; i++) { if (fd_data_map[i].fd != -1) _gpgme_io_close (fd_data_map[i].fd); if (fd_data_map[i].peer_fd != -1) _gpgme_io_close (fd_data_map[i].peer_fd); /* Don't release data because this is only a reference. */ } free (fd_data_map); } static gpgme_error_t gpg_cancel (void *engine) { engine_gpg_t gpg = engine; if (!gpg) return gpg_error (GPG_ERR_INV_VALUE); /* If gpg may be waiting for a cmd, close the cmd fd first. On Windows, close operations block on the reader/writer thread. */ if (gpg->cmd.used) { if (gpg->cmd.fd != -1) _gpgme_io_close (gpg->cmd.fd); else if (gpg->fd_data_map && gpg->fd_data_map[gpg->cmd.idx].fd != -1) _gpgme_io_close (gpg->fd_data_map[gpg->cmd.idx].fd); } if (gpg->status.fd[0] != -1) _gpgme_io_close (gpg->status.fd[0]); if (gpg->status.fd[1] != -1) _gpgme_io_close (gpg->status.fd[1]); if (gpg->colon.fd[0] != -1) _gpgme_io_close (gpg->colon.fd[0]); if (gpg->colon.fd[1] != -1) _gpgme_io_close (gpg->colon.fd[1]); if (gpg->fd_data_map) { free_fd_data_map (gpg->fd_data_map); gpg->fd_data_map = NULL; } return 0; } static void gpg_release (void *engine) { engine_gpg_t gpg = engine; if (!gpg) return; gpg_cancel (engine); if (gpg->file_name) free (gpg->file_name); if (gpg->version) free (gpg->version); if (gpg->lc_messages) free (gpg->lc_messages); if (gpg->lc_ctype) free (gpg->lc_ctype); while (gpg->arglist) { struct arg_and_data_s *next = gpg->arglist->next; free (gpg->arglist); gpg->arglist = next; } if (gpg->status.buffer) free (gpg->status.buffer); if (gpg->colon.buffer) free (gpg->colon.buffer); if (gpg->argv) free_argv (gpg->argv); if (gpg->cmd.keyword) free (gpg->cmd.keyword); gpgme_data_release (gpg->override_session_key); free (gpg); } static gpgme_error_t gpg_new (void **engine, const char *file_name, const char *home_dir, const char *version) { engine_gpg_t gpg; gpgme_error_t rc = 0; char *dft_display = NULL; char dft_ttyname[64]; char *dft_ttytype = NULL; char *env_tty = NULL; gpg = calloc (1, sizeof *gpg); if (!gpg) return gpg_error_from_syserror (); if (file_name) { gpg->file_name = strdup (file_name); if (!gpg->file_name) { rc = gpg_error_from_syserror (); goto leave; } } if (version) { gpg->version = strdup (version); if (!gpg->version) { rc = gpg_error_from_syserror (); goto leave; } } gpg->argtail = &gpg->arglist; gpg->status.fd[0] = -1; gpg->status.fd[1] = -1; gpg->colon.fd[0] = -1; gpg->colon.fd[1] = -1; gpg->cmd.fd = -1; gpg->cmd.idx = -1; gpg->cmd.linked_data = NULL; gpg->cmd.linked_idx = -1; /* Allocate the read buffer for the status pipe. */ gpg->status.bufsize = 1024; gpg->status.readpos = 0; gpg->status.buffer = malloc (gpg->status.bufsize); if (!gpg->status.buffer) { rc = gpg_error_from_syserror (); goto leave; } /* In any case we need a status pipe - create it right here and don't handle it with our generic gpgme_data_t mechanism. */ if (_gpgme_io_pipe (gpg->status.fd, 1) == -1) { rc = gpg_error_from_syserror (); goto leave; } if (_gpgme_io_set_close_notify (gpg->status.fd[0], close_notify_handler, gpg) || _gpgme_io_set_close_notify (gpg->status.fd[1], close_notify_handler, gpg)) { rc = gpg_error (GPG_ERR_GENERAL); goto leave; } gpg->status.eof = 0; if (home_dir) { rc = add_arg (gpg, "--homedir"); if (!rc) rc = add_arg (gpg, home_dir); if (rc) goto leave; } rc = add_arg (gpg, "--status-fd"); if (rc) goto leave; { char buf[25]; _gpgme_io_fd2str (buf, sizeof (buf), gpg->status.fd[1]); rc = add_arg_with_locp (gpg, buf, &gpg->status.arg_loc); if (rc) goto leave; } rc = add_arg (gpg, "--no-tty"); if (!rc) rc = add_arg (gpg, "--charset"); if (!rc) rc = add_arg (gpg, "utf8"); if (!rc) rc = add_arg (gpg, "--enable-progress-filter"); if (!rc && have_gpg_version (gpg, "2.1.11")) rc = add_arg (gpg, "--exit-on-status-write-error"); if (rc) goto leave; rc = _gpgme_getenv ("DISPLAY", &dft_display); if (rc) goto leave; if (dft_display) { rc = add_arg (gpg, "--display"); if (!rc) rc = add_arg (gpg, dft_display); free (dft_display); if (rc) goto leave; } rc = _gpgme_getenv ("GPG_TTY", &env_tty); if (isatty (1) || env_tty || rc) { int err = 0; if (rc) goto leave; else if (env_tty) { snprintf (dft_ttyname, sizeof (dft_ttyname), "%s", env_tty); free (env_tty); } else err = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); /* Even though isatty() returns 1, ttyname_r() may fail in many ways, e.g., when /dev/pts is not accessible under chroot. */ if (!err) { if (*dft_ttyname) { rc = add_arg (gpg, "--ttyname"); if (!rc) rc = add_arg (gpg, dft_ttyname); } else rc = 0; if (!rc) { rc = _gpgme_getenv ("TERM", &dft_ttytype); if (rc) goto leave; if (dft_ttytype) { rc = add_arg (gpg, "--ttytype"); if (!rc) rc = add_arg (gpg, dft_ttytype); } free (dft_ttytype); } if (rc) goto leave; } } leave: if (rc) gpg_release (gpg); else *engine = gpg; return rc; } /* Copy flags from CTX into the engine object. */ static void gpg_set_engine_flags (void *engine, const gpgme_ctx_t ctx) { engine_gpg_t gpg = engine; if (ctx->request_origin && have_gpg_version (gpg, "2.2.6")) { if (strlen (ctx->request_origin) + 1 > sizeof gpg->request_origin) strcpy (gpg->request_origin, "xxx"); /* Too long - force error */ else strcpy (gpg->request_origin, ctx->request_origin); } else if (ctx->no_symkey_cache && have_gpg_version (gpg, "2.2.7")) { gpg->flags.no_symkey_cache = 1; } else *gpg->request_origin = 0; } static gpgme_error_t gpg_set_locale (void *engine, int category, const char *value) { engine_gpg_t gpg = engine; if (0) ; #ifdef LC_CTYPE else if (category == LC_CTYPE) { if (gpg->lc_ctype) { free (gpg->lc_ctype); gpg->lc_ctype = NULL; } if (value) { gpg->lc_ctype = strdup (value); if (!gpg->lc_ctype) return gpg_error_from_syserror (); } } #endif #ifdef LC_MESSAGES else if (category == LC_MESSAGES) { if (gpg->lc_messages) { free (gpg->lc_messages); gpg->lc_messages = NULL; } if (value) { gpg->lc_messages = strdup (value); if (!gpg->lc_messages) return gpg_error_from_syserror (); } } #endif /* LC_MESSAGES */ else return gpg_error (GPG_ERR_INV_VALUE); return 0; } /* This sets a status callback for monitoring status lines before they * are passed to a caller set handler. */ static void gpg_set_status_cb (void *engine, gpgme_status_cb_t cb, void *cb_value) { engine_gpg_t gpg = engine; gpg->status.mon_cb = cb; gpg->status.mon_cb_value = cb_value; } /* Note, that the status_handler is allowed to modifiy the args value. */ static void gpg_set_status_handler (void *engine, engine_status_handler_t fnc, void *fnc_value) { engine_gpg_t gpg = engine; gpg->status.fnc = fnc; gpg->status.fnc_value = fnc_value; } /* Kludge to process --with-colon output. */ static gpgme_error_t gpg_set_colon_line_handler (void *engine, engine_colon_line_handler_t fnc, void *fnc_value) { engine_gpg_t gpg = engine; gpg->colon.bufsize = 1024; gpg->colon.readpos = 0; gpg->colon.buffer = malloc (gpg->colon.bufsize); if (!gpg->colon.buffer) return gpg_error_from_syserror (); if (_gpgme_io_pipe (gpg->colon.fd, 1) == -1) { int saved_err = gpg_error_from_syserror (); free (gpg->colon.buffer); gpg->colon.buffer = NULL; return saved_err; } if (_gpgme_io_set_close_notify (gpg->colon.fd[0], close_notify_handler, gpg) || _gpgme_io_set_close_notify (gpg->colon.fd[1], close_notify_handler, gpg)) return gpg_error (GPG_ERR_GENERAL); gpg->colon.eof = 0; gpg->colon.fnc = fnc; gpg->colon.fnc_value = fnc_value; return 0; } static gpgme_error_t command_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; engine_gpg_t gpg = (engine_gpg_t) data->handler_value; gpgme_error_t err; int processed = 0; assert (gpg->cmd.used); assert (gpg->cmd.code); assert (gpg->cmd.fnc); err = gpg->cmd.fnc (gpg->cmd.fnc_value, gpg->cmd.code, gpg->cmd.keyword, fd, &processed); gpg->cmd.code = 0; /* And sleep again until read_status will wake us up again. */ /* XXX We must check if there are any more fds active after removing this one. */ (*gpg->io_cbs.remove) (gpg->fd_data_map[gpg->cmd.idx].tag); gpg->cmd.fd = gpg->fd_data_map[gpg->cmd.idx].fd; gpg->fd_data_map[gpg->cmd.idx].fd = -1; if (err) return err; /* We always need to send at least a newline character. */ if (!processed) _gpgme_io_write (fd, "\n", 1); return 0; } /* The Fnc will be called to get a value for one of the commands with a key KEY. If the Code passed to FNC is 0, the function may release resources associated with the returned value from another call. To match such a second call to a first call, the returned value from the first call is passed as keyword. */ static gpgme_error_t gpg_set_command_handler (void *engine, engine_command_handler_t fnc, void *fnc_value, gpgme_data_t linked_data) { engine_gpg_t gpg = engine; gpgme_error_t rc; rc = add_arg (gpg, "--command-fd"); if (rc) return rc; /* This is a hack. We don't have a real data object. The only thing that matters is that we use something unique, so we use the address of the cmd structure in the gpg object. */ rc = add_data (gpg, (void *) &gpg->cmd, -2, 0); if (rc) return rc; gpg->cmd.fnc = fnc; gpg->cmd.cb_data = (void *) &gpg->cmd; gpg->cmd.fnc_value = fnc_value; gpg->cmd.linked_data = linked_data; gpg->cmd.used = 1; return 0; } static gpgme_error_t build_argv (engine_gpg_t gpg, const char *pgmname) { gpgme_error_t err; struct arg_and_data_s *a; struct fd_data_map_s *fd_data_map; size_t datac=0, argc=0; char **argv; int need_special = 0; int use_agent = 0; char *p; if (_gpgme_in_gpg_one_mode ()) { /* In GnuPG-1 mode we don't want to use the agent with a malformed environment variable. This is only a very basic test but sufficient to make our life in the regression tests easier. With GnuPG-2 the agent is anyway required and on modern installations GPG_AGENT_INFO is optional. */ err = _gpgme_getenv ("GPG_AGENT_INFO", &p); if (err) return err; use_agent = (p && strchr (p, ':')); if (p) free (p); } if (gpg->argv) { free_argv (gpg->argv); gpg->argv = NULL; } if (gpg->fd_data_map) { free_fd_data_map (gpg->fd_data_map); gpg->fd_data_map = NULL; } argc++; /* For argv[0]. */ for (a = gpg->arglist; a; a = a->next) { argc++; if (a->data) { /*fprintf (stderr, "build_argv: data\n" );*/ datac++; if (a->dup_to == -1 && !a->print_fd) need_special = 1; } else { /* fprintf (stderr, "build_argv: arg=`%s'\n", a->arg );*/ } } if (need_special) argc++; if (use_agent) argc++; if (gpg->pinentry_mode) argc++; if (!gpg->cmd.used) argc++; /* --batch */ argc += 3; /* --no-sk-comments, --request-origin, --no-symkey-cache */ argv = calloc (argc + 1, sizeof *argv); if (!argv) return gpg_error_from_syserror (); fd_data_map = calloc (datac + 1, sizeof *fd_data_map); if (!fd_data_map) { int saved_err = gpg_error_from_syserror (); free_argv (argv); return saved_err; } argc = datac = 0; argv[argc] = strdup (_gpgme_get_basename (pgmname)); /* argv[0] */ if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; if (need_special) { argv[argc] = strdup ("--enable-special-filenames"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } if (use_agent) { argv[argc] = strdup ("--use-agent"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } if (*gpg->request_origin) { argv[argc] = _gpgme_strconcat ("--request-origin=", gpg->request_origin, NULL); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } if (gpg->flags.no_symkey_cache) { argv[argc] = strdup ("--no-symkey-cache"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } if (gpg->pinentry_mode && have_gpg_version (gpg, "2.1.0")) { const char *s = NULL; switch (gpg->pinentry_mode) { case GPGME_PINENTRY_MODE_DEFAULT: break; case GPGME_PINENTRY_MODE_ASK: s = "--pinentry-mode=ask"; break; case GPGME_PINENTRY_MODE_CANCEL: s = "--pinentry-mode=cancel"; break; case GPGME_PINENTRY_MODE_ERROR: s = "--pinentry-mode=error"; break; case GPGME_PINENTRY_MODE_LOOPBACK:s = "--pinentry-mode=loopback"; break; } if (s) { argv[argc] = strdup (s); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } } if (!gpg->cmd.used) { argv[argc] = strdup ("--batch"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } argv[argc] = strdup ("--no-sk-comments"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; for (a = gpg->arglist; a; a = a->next) { if (a->arg_locp) *(a->arg_locp) = argc; if (a->data) { /* Create a pipe to pass it down to gpg. */ fd_data_map[datac].inbound = a->inbound; /* Create a pipe. */ { int fds[2]; if (_gpgme_io_pipe (fds, fd_data_map[datac].inbound ? 1 : 0) == -1) { int saved_errno = errno; free (fd_data_map); free_argv (argv); return gpg_error (saved_errno); } if (_gpgme_io_set_close_notify (fds[0], close_notify_handler, gpg) || _gpgme_io_set_close_notify (fds[1], close_notify_handler, gpg)) { /* We leak fd_data_map and the fds. This is not easy to avoid and given that we reach this here only after a malloc failure for a small object, it is probably better not to do anything. */ return gpg_error (GPG_ERR_GENERAL); } /* If the data_type is FD, we have to do a dup2 here. */ if (fd_data_map[datac].inbound) { fd_data_map[datac].fd = fds[0]; fd_data_map[datac].peer_fd = fds[1]; } else { fd_data_map[datac].fd = fds[1]; fd_data_map[datac].peer_fd = fds[0]; } } /* Hack to get hands on the fd later. */ if (gpg->cmd.used) { if (gpg->cmd.cb_data == a->data) { assert (gpg->cmd.idx == -1); gpg->cmd.idx = datac; } else if (gpg->cmd.linked_data == a->data) { assert (gpg->cmd.linked_idx == -1); gpg->cmd.linked_idx = datac; } } fd_data_map[datac].data = a->data; fd_data_map[datac].dup_to = a->dup_to; if (a->dup_to == -1) { char *ptr; int buflen = 25; argv[argc] = malloc (buflen); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } ptr = argv[argc]; if (!a->print_fd) { *(ptr++) = '-'; *(ptr++) = '&'; buflen -= 2; } _gpgme_io_fd2str (ptr, buflen, fd_data_map[datac].peer_fd); fd_data_map[datac].arg_loc = argc; argc++; } datac++; } else { argv[argc] = strdup (a->arg); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } } gpg->argv = argv; gpg->fd_data_map = fd_data_map; return 0; } static gpgme_error_t add_io_cb (engine_gpg_t gpg, int fd, int dir, gpgme_io_cb_t handler, void *data, void **tag) { gpgme_error_t err; err = (*gpg->io_cbs.add) (gpg->io_cbs.add_priv, fd, dir, handler, data, tag); if (err) return err; if (!dir) /* FIXME Kludge around poll() problem. */ err = _gpgme_io_set_nonblocking (fd); return err; } /* Handle the status output of GnuPG. This function does read entire lines and passes them as C strings to the callback function (we can use C Strings because the status output is always UTF-8 encoded). Of course we have to buffer the lines to cope with long lines e.g. with a large user ID. Note: We can optimize this to only cope with status line code we know about and skip all other stuff without buffering (i.e. without extending the buffer). */ static gpgme_error_t read_status (engine_gpg_t gpg) { char *p; int nread; size_t bufsize = gpg->status.bufsize; char *buffer = gpg->status.buffer; size_t readpos = gpg->status.readpos; gpgme_error_t err; assert (buffer); if (bufsize - readpos < 256) { /* Need more room for the read. */ bufsize += 1024; buffer = realloc (buffer, bufsize); if (!buffer) return gpg_error_from_syserror (); } nread = _gpgme_io_read (gpg->status.fd[0], buffer + readpos, bufsize-readpos); if (nread == -1) return gpg_error_from_syserror (); if (!nread) { err = 0; gpg->status.eof = 1; if (gpg->status.mon_cb) err = gpg->status.mon_cb (gpg->status.mon_cb_value, "", ""); if (gpg->status.fnc) { char emptystring[1] = {0}; err = gpg->status.fnc (gpg->status.fnc_value, GPGME_STATUS_EOF, emptystring); if (gpg_err_code (err) == GPG_ERR_FALSE) err = 0; /* Drop special error code. */ } return err; } while (nread > 0) { for (p = buffer + readpos; nread; nread--, p++) { if (*p == '\n') { /* (we require that the last line is terminated by a LF) */ if (p > buffer && p[-1] == '\r') p[-1] = 0; *p = 0; if (!strncmp (buffer, "[GNUPG:] ", 9) && buffer[9] >= 'A' && buffer[9] <= 'Z') { char *rest; gpgme_status_code_t r; rest = strchr (buffer + 9, ' '); if (!rest) rest = p; /* Set to an empty string. */ else *rest++ = 0; r = _gpgme_parse_status (buffer + 9); if (gpg->status.mon_cb && r != GPGME_STATUS_PROGRESS) { /* Note that we call the monitor even if we do * not know the status code (r < 0). */ err = gpg->status.mon_cb (gpg->status.mon_cb_value, buffer + 9, rest); if (err) return err; } if (r >= 0) { if (gpg->cmd.used && (r == GPGME_STATUS_GET_BOOL || r == GPGME_STATUS_GET_LINE || r == GPGME_STATUS_GET_HIDDEN)) { gpg->cmd.code = r; if (gpg->cmd.keyword) free (gpg->cmd.keyword); gpg->cmd.keyword = strdup (rest); if (!gpg->cmd.keyword) return gpg_error_from_syserror (); /* This should be the last thing we have received and the next thing will be that the command handler does its action. */ if (nread > 1) TRACE0 (DEBUG_CTX, "gpgme:read_status", 0, "error: unexpected data"); add_io_cb (gpg, gpg->cmd.fd, 0, command_handler, gpg, &gpg->fd_data_map[gpg->cmd.idx].tag); gpg->fd_data_map[gpg->cmd.idx].fd = gpg->cmd.fd; gpg->cmd.fd = -1; } else if (gpg->status.fnc) { err = gpg->status.fnc (gpg->status.fnc_value, r, rest); if (gpg_err_code (err) == GPG_ERR_FALSE) err = 0; /* Drop special error code. */ if (err) return err; } if (r == GPGME_STATUS_END_STREAM) { if (gpg->cmd.used) { /* Before we can actually add the command fd, we might have to flush the linked output data pipe. */ if (gpg->cmd.linked_idx != -1 && gpg->fd_data_map[gpg->cmd.linked_idx].fd != -1) { struct io_select_fd_s fds; fds.fd = gpg->fd_data_map[gpg->cmd.linked_idx].fd; fds.for_read = 1; fds.for_write = 0; fds.opaque = NULL; do { fds.signaled = 0; _gpgme_io_select (&fds, 1, 1); if (fds.signaled) _gpgme_data_inbound_handler (gpg->cmd.linked_data, fds.fd); } while (fds.signaled); } /* XXX We must check if there are any more fds active after removing this one. */ (*gpg->io_cbs.remove) (gpg->fd_data_map[gpg->cmd.idx].tag); gpg->cmd.fd = gpg->fd_data_map[gpg->cmd.idx].fd; gpg->fd_data_map[gpg->cmd.idx].fd = -1; } } } } /* To reuse the buffer for the next line we have to shift the remaining data to the buffer start and restart the loop Hmmm: We can optimize this function by looking forward in the buffer to see whether a second complete line is available and in this case avoid the memmove for this line. */ nread--; p++; if (nread) memmove (buffer, p, nread); readpos = 0; break; /* the for loop */ } else readpos++; } } /* Update the gpg object. */ gpg->status.bufsize = bufsize; gpg->status.buffer = buffer; gpg->status.readpos = readpos; return 0; } static gpgme_error_t status_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; engine_gpg_t gpg = (engine_gpg_t) data->handler_value; int err; assert (fd == gpg->status.fd[0]); err = read_status (gpg); if (err) return err; if (gpg->status.eof) _gpgme_io_close (fd); return 0; } static gpgme_error_t read_colon_line (engine_gpg_t gpg) { char *p; int nread; size_t bufsize = gpg->colon.bufsize; char *buffer = gpg->colon.buffer; size_t readpos = gpg->colon.readpos; assert (buffer); if (bufsize - readpos < 256) { /* Need more room for the read. */ bufsize += 1024; buffer = realloc (buffer, bufsize); if (!buffer) return gpg_error_from_syserror (); } nread = _gpgme_io_read (gpg->colon.fd[0], buffer+readpos, bufsize-readpos); if (nread == -1) return gpg_error_from_syserror (); if (!nread) { gpg->colon.eof = 1; assert (gpg->colon.fnc); gpg->colon.fnc (gpg->colon.fnc_value, NULL); return 0; } while (nread > 0) { for (p = buffer + readpos; nread; nread--, p++) { if ( *p == '\n' ) { /* (we require that the last line is terminated by a LF) and we skip empty lines. Note: we use UTF8 encoding and escaping of special characters. We require at least one colon to cope with some other printed information. */ *p = 0; if (*buffer && strchr (buffer, ':')) { char *line = NULL; if (gpg->colon.preprocess_fnc) { gpgme_error_t err; err = gpg->colon.preprocess_fnc (buffer, &line); if (err) return err; } assert (gpg->colon.fnc); if (line) { char *linep = line; char *endp; do { endp = strchr (linep, '\n'); if (endp) *endp++ = 0; gpg->colon.fnc (gpg->colon.fnc_value, linep); linep = endp; } while (linep && *linep); gpgrt_free (line); } else gpg->colon.fnc (gpg->colon.fnc_value, buffer); } /* To reuse the buffer for the next line we have to shift the remaining data to the buffer start and restart the loop Hmmm: We can optimize this function by looking forward in the buffer to see whether a second complete line is available and in this case avoid the memmove for this line. */ nread--; p++; if (nread) memmove (buffer, p, nread); readpos = 0; break; /* The for loop. */ } else readpos++; } } /* Update the gpg object. */ gpg->colon.bufsize = bufsize; gpg->colon.buffer = buffer; gpg->colon.readpos = readpos; return 0; } /* This colonline handler thing is not the clean way to do it. It might be better to enhance the gpgme_data_t object to act as a wrapper for a callback. Same goes for the status thing. For now we use this thing here because it is easier to implement. */ static gpgme_error_t colon_line_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; engine_gpg_t gpg = (engine_gpg_t) data->handler_value; gpgme_error_t rc = 0; assert (fd == gpg->colon.fd[0]); rc = read_colon_line (gpg); if (rc) return rc; if (gpg->colon.eof) _gpgme_io_close (fd); return 0; } static gpgme_error_t start (engine_gpg_t gpg) { gpgme_error_t rc; int i, n; int status; struct spawn_fd_item_s *fd_list; pid_t pid; const char *pgmname; if (!gpg) return gpg_error (GPG_ERR_INV_VALUE); if (!gpg->file_name && !_gpgme_get_default_gpg_name ()) return trace_gpg_error (GPG_ERR_INV_ENGINE); if (gpg->lc_ctype) { rc = add_arg_ext (gpg, gpg->lc_ctype, 1); if (!rc) rc = add_arg_ext (gpg, "--lc-ctype", 1); if (rc) return rc; } if (gpg->lc_messages) { rc = add_arg_ext (gpg, gpg->lc_messages, 1); if (!rc) rc = add_arg_ext (gpg, "--lc-messages", 1); if (rc) return rc; } pgmname = gpg->file_name ? gpg->file_name : _gpgme_get_default_gpg_name (); rc = build_argv (gpg, pgmname); if (rc) return rc; /* status_fd, colon_fd and end of list. */ n = 3; for (i = 0; gpg->fd_data_map[i].data; i++) n++; fd_list = calloc (n, sizeof *fd_list); if (! fd_list) return gpg_error_from_syserror (); /* Build the fd list for the child. */ n = 0; fd_list[n].fd = gpg->status.fd[1]; fd_list[n].dup_to = -1; fd_list[n].arg_loc = gpg->status.arg_loc; n++; if (gpg->colon.fnc) { fd_list[n].fd = gpg->colon.fd[1]; fd_list[n].dup_to = 1; n++; } for (i = 0; gpg->fd_data_map[i].data; i++) { fd_list[n].fd = gpg->fd_data_map[i].peer_fd; fd_list[n].dup_to = gpg->fd_data_map[i].dup_to; fd_list[n].arg_loc = gpg->fd_data_map[i].arg_loc; n++; } fd_list[n].fd = -1; fd_list[n].dup_to = -1; status = _gpgme_io_spawn (pgmname, gpg->argv, (IOSPAWN_FLAG_DETACHED |IOSPAWN_FLAG_ALLOW_SET_FG), fd_list, NULL, NULL, &pid); { int saved_err = gpg_error_from_syserror (); free (fd_list); if (status == -1) return saved_err; } /*_gpgme_register_term_handler ( closure, closure_value, pid );*/ rc = add_io_cb (gpg, gpg->status.fd[0], 1, status_handler, gpg, &gpg->status.tag); if (rc) /* FIXME: kill the child */ return rc; if (gpg->colon.fnc) { assert (gpg->colon.fd[0] != -1); rc = add_io_cb (gpg, gpg->colon.fd[0], 1, colon_line_handler, gpg, &gpg->colon.tag); if (rc) /* FIXME: kill the child */ return rc; } for (i = 0; gpg->fd_data_map[i].data; i++) { if (gpg->cmd.used && i == gpg->cmd.idx) { /* Park the cmd fd. */ gpg->cmd.fd = gpg->fd_data_map[i].fd; gpg->fd_data_map[i].fd = -1; } else { rc = add_io_cb (gpg, gpg->fd_data_map[i].fd, gpg->fd_data_map[i].inbound, gpg->fd_data_map[i].inbound ? _gpgme_data_inbound_handler : _gpgme_data_outbound_handler, gpg->fd_data_map[i].data, &gpg->fd_data_map[i].tag); if (rc) /* FIXME: kill the child */ return rc; } } gpg_io_event (gpg, GPGME_EVENT_START, NULL); /* fixme: check what data we can release here */ return 0; } /* Add the --input-size-hint option if requested. */ static gpgme_error_t add_input_size_hint (engine_gpg_t gpg, gpgme_data_t data) { gpgme_error_t err; gpgme_off_t value = _gpgme_data_get_size_hint (data); char numbuf[50]; /* Large enough for even 2^128 in base-10. */ char *p; if (!value || !have_gpg_version (gpg, "2.1.15")) return 0; err = add_arg (gpg, "--input-size-hint"); if (!err) { p = numbuf + sizeof numbuf; *--p = 0; do { *--p = '0' + (value % 10); value /= 10; } while (value); err = add_arg (gpg, p); } return err; } static gpgme_error_t gpg_decrypt (void *engine, gpgme_decrypt_flags_t flags, gpgme_data_t ciph, gpgme_data_t plain, int export_session_key, const char *override_session_key, int auto_key_retrieve) { engine_gpg_t gpg = engine; gpgme_error_t err; err = add_arg (gpg, "--decrypt"); if (!err && (flags & GPGME_DECRYPT_UNWRAP)) { if (!have_gpg_version (gpg, "2.1.12")) err = gpg_error (GPG_ERR_NOT_SUPPORTED); else err = add_arg (gpg, "--unwrap"); } if (!err && export_session_key) err = add_arg (gpg, "--show-session-key"); if (!err && auto_key_retrieve) err = add_arg (gpg, "--auto-key-retrieve"); if (!err && override_session_key && *override_session_key) { if (have_gpg_version (gpg, "2.1.16")) { gpgme_data_release (gpg->override_session_key); TRACE2 (DEBUG_ENGINE, "override", gpg, "seskey='%s' len=%zu\n", override_session_key, strlen (override_session_key)); err = gpgme_data_new_from_mem (&gpg->override_session_key, override_session_key, strlen (override_session_key), 1); if (!err) { err = add_arg (gpg, "--override-session-key-fd"); if (!err) err = add_data (gpg, gpg->override_session_key, -2, 0); } } else { /* Using that option may leak the session key via ps(1). */ err = add_arg (gpg, "--override-session-key"); if (!err) err = add_arg (gpg, override_session_key); } } /* Tell the gpg object about the data. */ if (!err) err = add_arg (gpg, "--output"); if (!err) err = add_arg (gpg, "-"); if (!err) err = add_data (gpg, plain, 1, 1); if (!err) err = add_input_size_hint (gpg, ciph); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, ciph, -1, 0); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_delete (void *engine, gpgme_key_t key, unsigned int flags) { engine_gpg_t gpg = engine; gpgme_error_t err = 0; int allow_secret = flags & GPGME_DELETE_ALLOW_SECRET; int force = flags & GPGME_DELETE_FORCE; if (force) err = add_arg (gpg, "--yes"); if (!err) err = add_arg (gpg, allow_secret ? "--delete-secret-and-public-key" : "--delete-key"); if (!err) err = add_arg (gpg, "--"); if (!err) { if (!key->subkeys || !key->subkeys->fpr) return gpg_error (GPG_ERR_INV_VALUE); else err = add_arg (gpg, key->subkeys->fpr); } if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_passwd (void *engine, gpgme_key_t key, unsigned int flags) { engine_gpg_t gpg = engine; gpgme_error_t err; (void)flags; if (!key || !key->subkeys || !key->subkeys->fpr) return gpg_error (GPG_ERR_INV_CERT_OBJ); err = add_arg (gpg, "--passwd"); if (!err) err = add_arg (gpg, key->subkeys->fpr); if (!err) err = start (gpg); return err; } static gpgme_error_t append_args_from_signers (engine_gpg_t gpg, gpgme_ctx_t ctx /* FIXME */) { gpgme_error_t err = 0; int i; gpgme_key_t key; for (i = 0; (key = gpgme_signers_enum (ctx, i)); i++) { const char *s = key->subkeys ? key->subkeys->keyid : NULL; if (s) { if (!err) err = add_arg (gpg, "-u"); if (!err) err = add_arg (gpg, s); } gpgme_key_unref (key); if (err) break; } return err; } static gpgme_error_t append_args_from_sender (engine_gpg_t gpg, gpgme_ctx_t ctx) { gpgme_error_t err; if (ctx->sender && have_gpg_version (gpg, "2.1.15")) { err = add_arg (gpg, "--sender"); if (!err) err = add_arg (gpg, ctx->sender); } else err = 0; return err; } static gpgme_error_t append_args_from_sig_notations (engine_gpg_t gpg, gpgme_ctx_t ctx /* FIXME */) { gpgme_error_t err = 0; gpgme_sig_notation_t notation; notation = gpgme_sig_notation_get (ctx); while (!err && notation) { if (notation->name && !(notation->flags & GPGME_SIG_NOTATION_HUMAN_READABLE)) err = gpg_error (GPG_ERR_INV_VALUE); else if (notation->name) { char *arg; /* Maximum space needed is one byte for the "critical" flag, the name, one byte for '=', the value, and a terminating '\0'. */ arg = malloc (1 + notation->name_len + 1 + notation->value_len + 1); if (!arg) err = gpg_error_from_syserror (); if (!err) { char *argp = arg; if (notation->critical) *(argp++) = '!'; memcpy (argp, notation->name, notation->name_len); argp += notation->name_len; *(argp++) = '='; /* We know that notation->name is '\0' terminated. */ strcpy (argp, notation->value); } if (!err) err = add_arg (gpg, "--sig-notation"); if (!err) err = add_arg (gpg, arg); if (arg) free (arg); } else { /* This is a policy URL. */ char *value; if (notation->critical) { value = malloc (1 + notation->value_len + 1); if (!value) err = gpg_error_from_syserror (); else { value[0] = '!'; /* We know that notation->value is '\0' terminated. */ strcpy (&value[1], notation->value); } } else value = notation->value; if (!err) err = add_arg (gpg, "--sig-policy-url"); if (!err) err = add_arg (gpg, value); if (value != notation->value) free (value); } notation = notation->next; } return err; } static gpgme_error_t gpg_edit (void *engine, int type, gpgme_key_t key, gpgme_data_t out, gpgme_ctx_t ctx /* FIXME */) { engine_gpg_t gpg = engine; gpgme_error_t err; err = add_arg (gpg, "--with-colons"); if (!err) err = append_args_from_signers (gpg, ctx); if (!err) err = add_arg (gpg, type == 0 ? "--edit-key" : "--card-edit"); if (!err) err = add_data (gpg, out, 1, 1); if (!err) err = add_arg (gpg, "--"); if (!err && type == 0) { const char *s = key->subkeys ? key->subkeys->fpr : NULL; if (!s) err = gpg_error (GPG_ERR_INV_VALUE); else err = add_arg (gpg, s); } if (!err) err = start (gpg); return err; } static gpgme_error_t append_args_from_recipients (engine_gpg_t gpg, gpgme_key_t recp[]) { gpgme_error_t err = 0; int i = 0; while (recp[i]) { if (!recp[i]->subkeys || !recp[i]->subkeys->fpr) err = gpg_error (GPG_ERR_INV_VALUE); if (!err) err = add_arg (gpg, "-r"); if (!err) err = add_arg (gpg, recp[i]->subkeys->fpr); if (err) break; i++; } return err; } +/* Take recipients from the LF delimited STRING and add -r args. */ +static gpg_error_t +append_args_from_recipients_string (engine_gpg_t gpg, const char *string) +{ + gpg_error_t err = 0; + int any = 0; + const char *s; + int n; + + do + { + /* Skip leading white space */ + while (*string == ' ' || *string == '\t') + string++; + if (!*string) + break; + + /* Look for the LF. */ + s = strchr (string, '\n'); + if (s) + n = s - string; + else + n = strlen (string); + while (n && (string[n-1] == ' ' || string[n-1] == '\t')) + n--; + + /* Add arg if it is not empty. */ + if (n) + { + err = add_arg (gpg, "-r"); + if (!err) + err = add_arg_len (gpg, NULL, string, n); + if (!err) + any = 1; + } + + string += n + !!s; + } + while (!err); + + if (!err && !any) + err = gpg_error (GPG_ERR_MISSING_KEY); + return err; +} + + static gpgme_error_t -gpg_encrypt (void *engine, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, +gpg_encrypt (void *engine, gpgme_key_t recp[], const char *recpstring, + gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor) { engine_gpg_t gpg = engine; gpgme_error_t err = 0; - if (recp) + if (recp || recpstring) err = add_arg (gpg, "--encrypt"); - if (!err && ((flags & GPGME_ENCRYPT_SYMMETRIC) || !recp)) + if (!err && ((flags & GPGME_ENCRYPT_SYMMETRIC) || (!recp && !recpstring))) err = add_arg (gpg, "--symmetric"); if (!err && use_armor) err = add_arg (gpg, "--armor"); if (!err && (flags & GPGME_ENCRYPT_WRAP)) { /* gpg is current not able to detect already compressed * packets. Thus when using * gpg --unwrap -d | gpg --no-literal -e * the encryption would add an additional compression layer. * We better suppress that. */ flags |= GPGME_ENCRYPT_NO_COMPRESS; err = add_arg (gpg, "--no-literal"); } if (!err && (flags & GPGME_ENCRYPT_NO_COMPRESS)) err = add_arg (gpg, "--compress-algo=none"); if (!err && (flags & GPGME_ENCRYPT_THROW_KEYIDS)) err = add_arg (gpg, "--throw-keyids"); if (gpgme_data_get_encoding (plain) == GPGME_DATA_ENCODING_MIME && have_gpg_version (gpg, "2.1.14")) err = add_arg (gpg, "--mimemode"); - if (recp) + if (recp || recpstring) { /* If we know that all recipients are valid (full or ultimate trust) we can suppress further checks. */ if (!err && (flags & GPGME_ENCRYPT_ALWAYS_TRUST)) err = add_arg (gpg, "--always-trust"); if (!err && (flags & GPGME_ENCRYPT_NO_ENCRYPT_TO)) err = add_arg (gpg, "--no-encrypt-to"); - if (!err) + if (!err && !recp && recpstring) + err = append_args_from_recipients_string (gpg, recpstring); + else if (!err) err = append_args_from_recipients (gpg, recp); } /* Tell the gpg object about the data. */ if (!err) err = add_arg (gpg, "--output"); if (!err) err = add_arg (gpg, "-"); if (!err) err = add_data (gpg, ciph, 1, 1); if (gpgme_data_get_file_name (plain)) { if (!err) err = add_arg (gpg, "--set-filename"); if (!err) err = add_arg (gpg, gpgme_data_get_file_name (plain)); } if (!err) err = add_input_size_hint (gpg, plain); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, plain, -1, 0); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_encrypt_sign (void *engine, gpgme_key_t recp[], + const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor, gpgme_ctx_t ctx /* FIXME */) { engine_gpg_t gpg = engine; gpgme_error_t err = 0; - if (recp) + if (recp || recpstring) err = add_arg (gpg, "--encrypt"); - if (!err && ((flags & GPGME_ENCRYPT_SYMMETRIC) || !recp)) + if (!err && ((flags & GPGME_ENCRYPT_SYMMETRIC) || (!recp && !recpstring))) err = add_arg (gpg, "--symmetric"); if (!err) err = add_arg (gpg, "--sign"); if (!err && use_armor) err = add_arg (gpg, "--armor"); if (!err && (flags & GPGME_ENCRYPT_NO_COMPRESS)) err = add_arg (gpg, "--compress-algo=none"); if (!err && (flags & GPGME_ENCRYPT_THROW_KEYIDS)) err = add_arg (gpg, "--throw-keyids"); if (gpgme_data_get_encoding (plain) == GPGME_DATA_ENCODING_MIME && have_gpg_version (gpg, "2.1.14")) err = add_arg (gpg, "--mimemode"); - if (recp) + if (recp || recpstring) { /* If we know that all recipients are valid (full or ultimate trust) we can suppress further checks. */ if (!err && (flags & GPGME_ENCRYPT_ALWAYS_TRUST)) err = add_arg (gpg, "--always-trust"); if (!err && (flags & GPGME_ENCRYPT_NO_ENCRYPT_TO)) err = add_arg (gpg, "--no-encrypt-to"); - if (!err) + if (!err && !recp && recpstring) + err = append_args_from_recipients_string (gpg, recpstring); + else if (!err) err = append_args_from_recipients (gpg, recp); } if (!err) err = append_args_from_signers (gpg, ctx); if (!err) err = append_args_from_sender (gpg, ctx); if (!err) err = append_args_from_sig_notations (gpg, ctx); /* Tell the gpg object about the data. */ if (!err) err = add_arg (gpg, "--output"); if (!err) err = add_arg (gpg, "-"); if (!err) err = add_data (gpg, ciph, 1, 1); if (gpgme_data_get_file_name (plain)) { if (!err) err = add_arg (gpg, "--set-filename"); if (!err) err = add_arg (gpg, gpgme_data_get_file_name (plain)); } if (!err) err = add_input_size_hint (gpg, plain); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, plain, -1, 0); if (!err) err = start (gpg); return err; } static gpgme_error_t export_common (engine_gpg_t gpg, gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor) { gpgme_error_t err = 0; if ((mode & ~(GPGME_EXPORT_MODE_EXTERN |GPGME_EXPORT_MODE_MINIMAL |GPGME_EXPORT_MODE_SECRET))) return gpg_error (GPG_ERR_NOT_SUPPORTED); if ((mode & GPGME_EXPORT_MODE_MINIMAL)) err = add_arg (gpg, "--export-options=export-minimal"); if (err) ; else if ((mode & GPGME_EXPORT_MODE_EXTERN)) { err = add_arg (gpg, "--send-keys"); } else { if ((mode & GPGME_EXPORT_MODE_SECRET)) err = add_arg (gpg, "--export-secret-keys"); else err = add_arg (gpg, "--export"); if (!err && use_armor) err = add_arg (gpg, "--armor"); if (!err) err = add_data (gpg, keydata, 1, 1); } if (!err) err = add_arg (gpg, "--"); return err; } static gpgme_error_t gpg_export (void *engine, const char *pattern, gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor) { engine_gpg_t gpg = engine; gpgme_error_t err; err = export_common (gpg, mode, keydata, use_armor); if (!err && pattern && *pattern) err = add_arg (gpg, pattern); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_export_ext (void *engine, const char *pattern[], gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor) { engine_gpg_t gpg = engine; gpgme_error_t err; err = export_common (gpg, mode, keydata, use_armor); if (pattern) { while (!err && *pattern && **pattern) err = add_arg (gpg, *(pattern++)); } if (!err) err = start (gpg); return err; } /* Helper to add algo, usage, and expire to the list of args. */ static gpgme_error_t gpg_add_algo_usage_expire (engine_gpg_t gpg, const char *algo, unsigned long expires, unsigned int flags) { gpg_error_t err; /* This condition is only required to allow the use of gpg < 2.1.16 */ if (algo || (flags & (GPGME_CREATE_SIGN | GPGME_CREATE_ENCR | GPGME_CREATE_CERT | GPGME_CREATE_AUTH | GPGME_CREATE_NOEXPIRE)) || expires) { err = add_arg (gpg, algo? algo : "default"); if (!err) { char tmpbuf[5*4+1]; snprintf (tmpbuf, sizeof tmpbuf, "%s%s%s%s", (flags & GPGME_CREATE_SIGN)? " sign":"", (flags & GPGME_CREATE_ENCR)? " encr":"", (flags & GPGME_CREATE_CERT)? " cert":"", (flags & GPGME_CREATE_AUTH)? " auth":""); err = add_arg (gpg, *tmpbuf? tmpbuf : "default"); } if (!err) { if ((flags & GPGME_CREATE_NOEXPIRE)) err = add_arg (gpg, "never"); else if (expires == 0) err = add_arg (gpg, "-"); else { char tmpbuf[8+20]; snprintf (tmpbuf, sizeof tmpbuf, "seconds=%lu", expires); err = add_arg (gpg, tmpbuf); } } } else err = 0; return err; } static gpgme_error_t gpg_createkey_from_param (engine_gpg_t gpg, gpgme_data_t help_data, unsigned int extraflags) { gpgme_error_t err; err = add_arg (gpg, "--gen-key"); if (!err && (extraflags & GENKEY_EXTRAFLAG_ARMOR)) err = add_arg (gpg, "--armor"); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, help_data, -1, 0); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_createkey (engine_gpg_t gpg, const char *userid, const char *algo, unsigned long expires, unsigned int flags, unsigned int extraflags) { gpgme_error_t err; err = add_arg (gpg, "--quick-gen-key"); if (!err && (extraflags & GENKEY_EXTRAFLAG_ARMOR)) err = add_arg (gpg, "--armor"); if (!err && (flags & GPGME_CREATE_NOPASSWD)) { err = add_arg (gpg, "--passphrase"); if (!err) err = add_arg (gpg, ""); if (!err) err = add_arg (gpg, "--batch"); } if (!err && (flags & GPGME_CREATE_FORCE)) err = add_arg (gpg, "--yes"); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_arg (gpg, userid); if (!err) err = gpg_add_algo_usage_expire (gpg, algo, expires, flags); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_addkey (engine_gpg_t gpg, const char *algo, unsigned long expires, gpgme_key_t key, unsigned int flags, unsigned int extraflags) { gpgme_error_t err; if (!key || !key->fpr) return gpg_error (GPG_ERR_INV_ARG); err = add_arg (gpg, "--quick-addkey"); if (!err && (extraflags & GENKEY_EXTRAFLAG_ARMOR)) err = add_arg (gpg, "--armor"); if (!err && (flags & GPGME_CREATE_NOPASSWD)) { err = add_arg (gpg, "--passphrase"); if (!err) err = add_arg (gpg, ""); if (!err) err = add_arg (gpg, "--batch"); } if (!err) err = add_arg (gpg, "--"); if (!err) err = add_arg (gpg, key->fpr); if (!err) err = gpg_add_algo_usage_expire (gpg, algo, expires, flags); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_adduid (engine_gpg_t gpg, gpgme_key_t key, const char *userid, unsigned int extraflags) { gpgme_error_t err; if (!key || !key->fpr || !userid) return gpg_error (GPG_ERR_INV_ARG); if ((extraflags & GENKEY_EXTRAFLAG_SETPRIMARY)) { if (!have_gpg_version (gpg, "2.1.20")) err = gpg_error (GPG_ERR_NOT_SUPPORTED); else err = add_arg (gpg, "--quick-set-primary-uid"); } else if ((extraflags & GENKEY_EXTRAFLAG_REVOKE)) err = add_arg (gpg, "--quick-revuid"); else err = add_arg (gpg, "--quick-adduid"); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_arg (gpg, key->fpr); if (!err) err = add_arg (gpg, userid); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_genkey (void *engine, const char *userid, const char *algo, unsigned long reserved, unsigned long expires, gpgme_key_t key, unsigned int flags, gpgme_data_t help_data, unsigned int extraflags, gpgme_data_t pubkey, gpgme_data_t seckey) { engine_gpg_t gpg = engine; gpgme_error_t err; (void)reserved; if (!gpg) return gpg_error (GPG_ERR_INV_VALUE); /* If HELP_DATA is given the use of the old interface * (gpgme_op_genkey) has been requested. The other modes are: * * USERID && !KEY - Create a new keyblock. * !USERID && KEY - Add a new subkey to KEY (gpg >= 2.1.14) * USERID && KEY && !ALGO - Add a new user id to KEY (gpg >= 2.1.14). * or set a flag on a user id. */ if (help_data) { /* We need a special mechanism to get the fd of a pipe here, so that we can use this for the %pubring and %secring parameters. We don't have this yet, so we implement only the adding to the standard keyrings. */ if (pubkey || seckey) err = gpg_error (GPG_ERR_NOT_IMPLEMENTED); else err = gpg_createkey_from_param (gpg, help_data, extraflags); } else if (!have_gpg_version (gpg, "2.1.13")) err = gpg_error (GPG_ERR_NOT_SUPPORTED); else if (userid && !key) err = gpg_createkey (gpg, userid, algo, expires, flags, extraflags); else if (!userid && key) err = gpg_addkey (gpg, algo, expires, key, flags, extraflags); else if (userid && key && !algo) err = gpg_adduid (gpg, key, userid, extraflags); else err = gpg_error (GPG_ERR_INV_VALUE); return err; } /* Return the next DELIM delimited string from DATA as a C-string. The caller needs to provide the address of a pointer variable which he has to set to NULL before the first call. After the last call to this function, this function needs to be called once more with DATA set to NULL so that the function can release its internal state. After that the pointer variable is free for use again. Note that we use a delimiter and thus a trailing delimiter is not required. DELIM may not be changed after the first call. */ static const char * string_from_data (gpgme_data_t data, int delim, void **helpptr, gpgme_error_t *r_err) { #define MYBUFLEN 2000 /* Fixme: We don't support URLs longer than that. */ struct { int eof_seen; int nbytes; /* Length of the last returned string including the delimiter. */ int buflen; /* Valid length of BUF. */ char buf[MYBUFLEN+1]; /* Buffer with one byte extra space. */ } *self; char *p; int nread; *r_err = 0; if (!data) { if (*helpptr) { free (*helpptr); *helpptr = NULL; } return NULL; } if (*helpptr) self = *helpptr; else { self = malloc (sizeof *self); if (!self) { *r_err = gpg_error_from_syserror (); return NULL; } *helpptr = self; self->eof_seen = 0; self->nbytes = 0; self->buflen = 0; } if (self->eof_seen) return NULL; assert (self->nbytes <= self->buflen); memmove (self->buf, self->buf + self->nbytes, self->buflen - self->nbytes); self->buflen -= self->nbytes; self->nbytes = 0; do { /* Fixme: This is fairly infective scanning because we may scan the buffer several times. */ p = memchr (self->buf, delim, self->buflen); if (p) { *p = 0; self->nbytes = p - self->buf + 1; return self->buf; } if ( !(MYBUFLEN - self->buflen) ) { /* Not enough space - URL too long. */ *r_err = gpg_error (GPG_ERR_TOO_LARGE); return NULL; } nread = gpgme_data_read (data, self->buf + self->buflen, MYBUFLEN - self->buflen); if (nread < 0) { *r_err = gpg_error_from_syserror (); return NULL; } self->buflen += nread; } while (nread); /* EOF reached. If we have anything in the buffer, append a Nul and return it. */ self->eof_seen = 1; if (self->buflen) { self->buf[self->buflen] = 0; /* (we allocated one extra byte) */ return self->buf; } return NULL; #undef MYBUFLEN } static gpgme_error_t gpg_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray) { engine_gpg_t gpg = engine; gpgme_error_t err; int idx; gpgme_data_encoding_t dataenc; if (keydata && keyarray) return gpg_error (GPG_ERR_INV_VALUE); /* Only one is allowed. */ dataenc = gpgme_data_get_encoding (keydata); if (keyarray) { err = add_arg (gpg, "--recv-keys"); if (!err) err = add_arg (gpg, "--"); for (idx=0; !err && keyarray[idx]; idx++) { if (keyarray[idx]->protocol != GPGME_PROTOCOL_OpenPGP) ; else if (!keyarray[idx]->subkeys) ; else if (keyarray[idx]->subkeys->fpr && *keyarray[idx]->subkeys->fpr) err = add_arg (gpg, keyarray[idx]->subkeys->fpr); else if (*keyarray[idx]->subkeys->keyid) err = add_arg (gpg, keyarray[idx]->subkeys->keyid); } } else if (dataenc == GPGME_DATA_ENCODING_URL || dataenc == GPGME_DATA_ENCODING_URL0) { void *helpptr; const char *string; gpgme_error_t xerr; int delim = (dataenc == GPGME_DATA_ENCODING_URL)? '\n': 0; /* FIXME: --fetch-keys is probably not correct because it can't grok all kinds of URLs. On Unix it should just work but on Windows we will build the command line and that may fail for some embedded control characters. It is anyway limited to the maximum size of the command line. We need another command which can take its input from a file. Maybe we should use an option to gpg to modify such commands (ala --multifile). */ err = add_arg (gpg, "--fetch-keys"); if (!err) err = add_arg (gpg, "--"); helpptr = NULL; while (!err && (string = string_from_data (keydata, delim, &helpptr, &xerr))) err = add_arg (gpg, string); if (!err) err = xerr; string_from_data (NULL, delim, &helpptr, &xerr); } else if (dataenc == GPGME_DATA_ENCODING_URLESC) { /* Already escaped URLs are not yet supported. */ err = gpg_error (GPG_ERR_NOT_IMPLEMENTED); } else { err = add_arg (gpg, "--import"); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, keydata, -1, 0); } if (!err) err = start (gpg); return err; } /* The output for external keylistings in GnuPG is different from all the other key listings. We catch this here with a special preprocessor that reformats the colon handler lines. */ static gpgme_error_t gpg_keylist_preprocess (char *line, char **r_line) { enum { RT_NONE, RT_INFO, RT_PUB, RT_UID } rectype = RT_NONE; #define NR_FIELDS 16 char *field[NR_FIELDS]; int fields = 0; size_t n; *r_line = NULL; while (line && fields < NR_FIELDS) { field[fields++] = line; line = strchr (line, ':'); if (line) *(line++) = '\0'; } if (!strcmp (field[0], "info")) rectype = RT_INFO; else if (!strcmp (field[0], "pub")) rectype = RT_PUB; else if (!strcmp (field[0], "uid")) rectype = RT_UID; else rectype = RT_NONE; switch (rectype) { case RT_INFO: /* FIXME: Eventually, check the version number at least. */ return 0; case RT_PUB: if (fields < 7) return 0; /* The format is: pub:::::: as defined in 5.2. Machine Readable Indexes of the OpenPGP HTTP Keyserver Protocol (draft). Modern versions of the SKS keyserver return the fingerprint instead of the keyid. We detect this here and use the v4 fingerprint format to convert it to a key id. We want: pub:o::::::::::::: */ n = strlen (field[1]); if (n > 16) { if (gpgrt_asprintf (r_line, "pub:o%s:%s:%s:%s:%s:%s::::::::\n" "fpr:::::::::%s:", field[6], field[3], field[2], field[1] + n - 16, field[4], field[5], field[1]) < 0) return gpg_error_from_syserror (); } else { if (gpgrt_asprintf (r_line, "pub:o%s:%s:%s:%s:%s:%s::::::::", field[6], field[3], field[2], field[1], field[4], field[5]) < 0) return gpg_error_from_syserror (); } return 0; case RT_UID: /* The format is: uid:::: as defined in 5.2. Machine Readable Indexes of the OpenPGP HTTP Keyserver Protocol (draft). For an ldap keyserver the format is: uid: We want: uid:o::::::::: */ { /* The user ID is percent escaped, but we want c-coded. Because we have to replace each '%HL' by '\xHL', we need at most 4/3 th the number of bytes. But because we also need to escape the backslashes we allocate twice as much. */ char *uid = malloc (2 * strlen (field[1]) + 1); char *src; char *dst; if (! uid) return gpg_error_from_syserror (); src = field[1]; dst = uid; while (*src) { if (*src == '%') { *(dst++) = '\\'; *(dst++) = 'x'; src++; /* Copy the next two bytes unconditionally. */ if (*src) *(dst++) = *(src++); if (*src) *(dst++) = *(src++); } else if (*src == '\\') { *dst++ = '\\'; *dst++ = '\\'; src++; } else *(dst++) = *(src++); } *dst = '\0'; if (fields < 4) { if (gpgrt_asprintf (r_line, "uid:o::::::::%s:", uid) < 0) return gpg_error_from_syserror (); } else { if (gpgrt_asprintf (r_line, "uid:o%s::::%s:%s:::%s:", field[4], field[2], field[3], uid) < 0) return gpg_error_from_syserror (); } } return 0; case RT_NONE: /* Unknown record. */ break; } return 0; } static gpg_error_t gpg_keylist_build_options (engine_gpg_t gpg, int secret_only, gpgme_keylist_mode_t mode) { gpg_error_t err; err = add_arg (gpg, "--with-colons"); /* Since gpg 2.1.15 fingerprints are always printed, thus there is * no more need to explicitly request them. */ if (!have_gpg_version (gpg, "2.1.15")) { if (!err) err = add_arg (gpg, "--fixed-list-mode"); if (!err) err = add_arg (gpg, "--with-fingerprint"); if (!err) err = add_arg (gpg, "--with-fingerprint"); } if (!err && (mode & GPGME_KEYLIST_MODE_WITH_TOFU) && have_gpg_version (gpg, "2.1.16")) err = add_arg (gpg, "--with-tofu-info"); if (!err && (mode & GPGME_KEYLIST_MODE_WITH_SECRET)) err = add_arg (gpg, "--with-secret"); if (!err && (mode & GPGME_KEYLIST_MODE_SIGS) && (mode & GPGME_KEYLIST_MODE_SIG_NOTATIONS)) { err = add_arg (gpg, "--list-options"); if (!err) err = add_arg (gpg, "show-sig-subpackets=\"20,26\""); } if (!err) { if ( (mode & GPGME_KEYLIST_MODE_EXTERN) ) { if (secret_only) err = gpg_error (GPG_ERR_NOT_SUPPORTED); else if ( (mode & GPGME_KEYLIST_MODE_LOCAL)) { /* The local+extern mode is special. It works only with gpg >= 2.0.10. FIXME: We should check that we have such a version to that we can return a proper error code. The problem is that we don't know the context here and thus can't access the cached version number for the engine info structure. */ err = add_arg (gpg, "--locate-keys"); if ((mode & GPGME_KEYLIST_MODE_SIGS)) err = add_arg (gpg, "--with-sig-check"); } else { err = add_arg (gpg, "--search-keys"); gpg->colon.preprocess_fnc = gpg_keylist_preprocess; } } else { err = add_arg (gpg, secret_only ? "--list-secret-keys" : ((mode & GPGME_KEYLIST_MODE_SIGS) ? "--check-sigs" : "--list-keys")); } } if (!err) err = add_arg (gpg, "--"); return err; } static gpgme_error_t gpg_keylist (void *engine, const char *pattern, int secret_only, gpgme_keylist_mode_t mode, int engine_flags) { engine_gpg_t gpg = engine; gpgme_error_t err; (void)engine_flags; err = gpg_keylist_build_options (gpg, secret_only, mode); if (!err && pattern && *pattern) err = add_arg (gpg, pattern); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_keylist_ext (void *engine, const char *pattern[], int secret_only, int reserved, gpgme_keylist_mode_t mode, int engine_flags) { engine_gpg_t gpg = engine; gpgme_error_t err; (void)engine_flags; if (reserved) return gpg_error (GPG_ERR_INV_VALUE); err = gpg_keylist_build_options (gpg, secret_only, mode); if (pattern) { while (!err && *pattern && **pattern) err = add_arg (gpg, *(pattern++)); } if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_keylist_data (void *engine, gpgme_data_t data) { engine_gpg_t gpg = engine; gpgme_error_t err; if (!have_gpg_version (gpg, "2.1.14")) return gpg_error (GPG_ERR_NOT_SUPPORTED); err = add_arg (gpg, "--with-colons"); if (!err) err = add_arg (gpg, "--with-fingerprint"); if (!err) err = add_arg (gpg, "--import-options"); if (!err) err = add_arg (gpg, "import-show"); if (!err) err = add_arg (gpg, "--dry-run"); if (!err) err = add_arg (gpg, "--import"); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, data, -1, 0); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_keysign (void *engine, gpgme_key_t key, const char *userid, unsigned long expire, unsigned int flags, gpgme_ctx_t ctx) { engine_gpg_t gpg = engine; gpgme_error_t err; const char *s; if (!key || !key->fpr) return gpg_error (GPG_ERR_INV_ARG); if (!have_gpg_version (gpg, "2.1.12")) return gpg_error (GPG_ERR_NOT_SUPPORTED); if ((flags & GPGME_KEYSIGN_LOCAL)) err = add_arg (gpg, "--quick-lsign-key"); else err = add_arg (gpg, "--quick-sign-key"); if (!err) err = append_args_from_signers (gpg, ctx); /* If an expiration time has been given use that. If none has been * given the default from gpg.conf is used. To make sure not to set * an expiration time at all the flag GPGME_KEYSIGN_NOEXPIRE can be * used. */ if (!err && (expire || (flags & GPGME_KEYSIGN_NOEXPIRE))) { char tmpbuf[8+20]; if ((flags & GPGME_KEYSIGN_NOEXPIRE)) expire = 0; snprintf (tmpbuf, sizeof tmpbuf, "seconds=%lu", expire); err = add_arg (gpg, "--default-cert-expire"); if (!err) err = add_arg (gpg, tmpbuf); } if (!err) err = add_arg (gpg, "--"); if (!err) err = add_arg (gpg, key->fpr); if (!err && userid) { if ((flags & GPGME_KEYSIGN_LFSEP)) { for (; !err && (s = strchr (userid, '\n')); userid = s + 1) if ((s - userid)) err = add_arg_len (gpg, "=", userid, s - userid); if (!err && *userid) err = add_arg_pfx (gpg, "=", userid); } else err = add_arg_pfx (gpg, "=", userid); } if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_tofu_policy (void *engine, gpgme_key_t key, gpgme_tofu_policy_t policy) { engine_gpg_t gpg = engine; gpgme_error_t err; const char *policystr = NULL; if (!key || !key->fpr) return gpg_error (GPG_ERR_INV_ARG); switch (policy) { case GPGME_TOFU_POLICY_NONE: break; case GPGME_TOFU_POLICY_AUTO: policystr = "auto"; break; case GPGME_TOFU_POLICY_GOOD: policystr = "good"; break; case GPGME_TOFU_POLICY_BAD: policystr = "bad"; break; case GPGME_TOFU_POLICY_ASK: policystr = "ask"; break; case GPGME_TOFU_POLICY_UNKNOWN: policystr = "unknown"; break; } if (!policystr) return gpg_error (GPG_ERR_INV_VALUE); if (!have_gpg_version (gpg, "2.1.10")) return gpg_error (GPG_ERR_NOT_SUPPORTED); err = add_arg (gpg, "--tofu-policy"); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_arg (gpg, policystr); if (!err) err = add_arg (gpg, key->fpr); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_sign (void *engine, gpgme_data_t in, gpgme_data_t out, gpgme_sig_mode_t mode, int use_armor, int use_textmode, int include_certs, gpgme_ctx_t ctx /* FIXME */) { engine_gpg_t gpg = engine; gpgme_error_t err; (void)include_certs; if (mode == GPGME_SIG_MODE_CLEAR) err = add_arg (gpg, "--clearsign"); else { err = add_arg (gpg, "--sign"); if (!err && mode == GPGME_SIG_MODE_DETACH) err = add_arg (gpg, "--detach"); if (!err && use_armor) err = add_arg (gpg, "--armor"); if (!err) { if (gpgme_data_get_encoding (in) == GPGME_DATA_ENCODING_MIME && have_gpg_version (gpg, "2.1.14")) err = add_arg (gpg, "--mimemode"); else if (use_textmode) err = add_arg (gpg, "--textmode"); } } if (!err) err = append_args_from_signers (gpg, ctx); if (!err) err = append_args_from_sender (gpg, ctx); if (!err) err = append_args_from_sig_notations (gpg, ctx); if (gpgme_data_get_file_name (in)) { if (!err) err = add_arg (gpg, "--set-filename"); if (!err) err = add_arg (gpg, gpgme_data_get_file_name (in)); } /* Tell the gpg object about the data. */ if (!err) err = add_input_size_hint (gpg, in); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, in, -1, 0); if (!err) err = add_data (gpg, out, 1, 1); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_trustlist (void *engine, const char *pattern) { engine_gpg_t gpg = engine; gpgme_error_t err; err = add_arg (gpg, "--with-colons"); if (!err) err = add_arg (gpg, "--list-trust-path"); /* Tell the gpg object about the data. */ if (!err) err = add_arg (gpg, "--"); if (!err) err = add_arg (gpg, pattern); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_verify (void *engine, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext, gpgme_ctx_t ctx) { engine_gpg_t gpg = engine; gpgme_error_t err; err = append_args_from_sender (gpg, ctx); if (!err && ctx->auto_key_retrieve) err = add_arg (gpg, "--auto-key-retrieve"); if (err) ; else if (plaintext) { /* Normal or cleartext signature. */ err = add_arg (gpg, "--output"); if (!err) err = add_arg (gpg, "-"); if (!err) err = add_input_size_hint (gpg, sig); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, sig, -1, 0); if (!err) err = add_data (gpg, plaintext, 1, 1); } else { err = add_arg (gpg, "--verify"); if (!err) err = add_input_size_hint (gpg, signed_text); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, sig, -1, 0); if (!err && signed_text) err = add_data (gpg, signed_text, -1, 0); } if (!err) err = start (gpg); return err; } static void gpg_set_io_cbs (void *engine, gpgme_io_cbs_t io_cbs) { engine_gpg_t gpg = engine; gpg->io_cbs = *io_cbs; } static gpgme_error_t gpg_set_pinentry_mode (void *engine, gpgme_pinentry_mode_t mode) { engine_gpg_t gpg = engine; gpg->pinentry_mode = mode; return 0; } struct engine_ops _gpgme_engine_ops_gpg = { /* Static functions. */ _gpgme_get_default_gpg_name, NULL, gpg_get_version, gpg_get_req_version, gpg_new, /* Member functions. */ gpg_release, NULL, /* reset */ gpg_set_status_cb, gpg_set_status_handler, gpg_set_command_handler, gpg_set_colon_line_handler, gpg_set_locale, NULL, /* set_protocol */ gpg_set_engine_flags, /* set_engine_flags */ gpg_decrypt, gpg_delete, gpg_edit, gpg_encrypt, gpg_encrypt_sign, gpg_export, gpg_export_ext, gpg_genkey, gpg_import, gpg_keylist, gpg_keylist_ext, gpg_keylist_data, gpg_keysign, gpg_tofu_policy, /* tofu_policy */ gpg_sign, gpg_trustlist, gpg_verify, NULL, /* getauditlog */ NULL, /* opassuan_transact */ NULL, /* conf_load */ NULL, /* conf_save */ NULL, /* conf_dir */ NULL, /* query_swdb */ gpg_set_io_cbs, gpg_io_event, gpg_cancel, NULL, /* cancel_op */ gpg_passwd, gpg_set_pinentry_mode, NULL /* opspawn */ }; diff --git a/src/engine-gpgsm.c b/src/engine-gpgsm.c index b8e44e7c..da7e524f 100644 --- a/src/engine-gpgsm.c +++ b/src/engine-gpgsm.c @@ -1,2170 +1,2222 @@ /* engine-gpgsm.c - GpgSM engine. Copyright (C) 2000 Werner Koch (dd9jn) Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2009, 2010 g10 Code GmbH This file is part of GPGME. GPGME is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. GPGME is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #if HAVE_CONFIG_H #include #endif #include #include #ifdef HAVE_SYS_TYPES_H # include #endif #include #ifdef HAVE_UNISTD_H # include #endif #ifdef HAVE_LOCALE_H #include #endif #include /* FIXME */ #include #include "gpgme.h" #include "util.h" #include "ops.h" #include "wait.h" #include "priv-io.h" #include "sema.h" #include "data.h" #include "assuan.h" #include "debug.h" #include "engine-backend.h" typedef struct { int fd; /* FD we talk about. */ int server_fd;/* Server FD for this connection. */ int dir; /* Inbound/Outbound, maybe given implicit? */ void *data; /* Handler-specific data. */ void *tag; /* ID from the user for gpgme_remove_io_callback. */ char server_fd_str[15]; /* Same as SERVER_FD but as a string. We need this because _gpgme_io_fd2str can't be used on a closed descriptor. */ } iocb_data_t; struct engine_gpgsm { assuan_context_t assuan_ctx; int lc_ctype_set; int lc_messages_set; iocb_data_t status_cb; /* Input, output etc are from the servers perspective. */ iocb_data_t input_cb; gpgme_data_t input_helper_data; /* Input helper data object. */ void *input_helper_memory; /* Input helper memory block. */ iocb_data_t output_cb; iocb_data_t message_cb; struct { engine_status_handler_t fnc; void *fnc_value; gpgme_status_cb_t mon_cb; void *mon_cb_value; } status; struct { engine_colon_line_handler_t fnc; void *fnc_value; struct { char *line; int linesize; int linelen; } attic; int any; /* any data line seen */ } colon; gpgme_data_t inline_data; /* Used to collect D lines. */ char request_origin[10]; struct gpgme_io_cbs io_cbs; }; typedef struct engine_gpgsm *engine_gpgsm_t; static void gpgsm_io_event (void *engine, gpgme_event_io_t type, void *type_data); static char * gpgsm_get_version (const char *file_name) { return _gpgme_get_program_version (file_name ? file_name : _gpgme_get_default_gpgsm_name ()); } static const char * gpgsm_get_req_version (void) { return "2.0.4"; } static void close_notify_handler (int fd, void *opaque) { engine_gpgsm_t gpgsm = opaque; assert (fd != -1); if (gpgsm->status_cb.fd == fd) { if (gpgsm->status_cb.tag) (*gpgsm->io_cbs.remove) (gpgsm->status_cb.tag); gpgsm->status_cb.fd = -1; gpgsm->status_cb.tag = NULL; } else if (gpgsm->input_cb.fd == fd) { if (gpgsm->input_cb.tag) (*gpgsm->io_cbs.remove) (gpgsm->input_cb.tag); gpgsm->input_cb.fd = -1; gpgsm->input_cb.tag = NULL; if (gpgsm->input_helper_data) { gpgme_data_release (gpgsm->input_helper_data); gpgsm->input_helper_data = NULL; } if (gpgsm->input_helper_memory) { free (gpgsm->input_helper_memory); gpgsm->input_helper_memory = NULL; } } else if (gpgsm->output_cb.fd == fd) { if (gpgsm->output_cb.tag) (*gpgsm->io_cbs.remove) (gpgsm->output_cb.tag); gpgsm->output_cb.fd = -1; gpgsm->output_cb.tag = NULL; } else if (gpgsm->message_cb.fd == fd) { if (gpgsm->message_cb.tag) (*gpgsm->io_cbs.remove) (gpgsm->message_cb.tag); gpgsm->message_cb.fd = -1; gpgsm->message_cb.tag = NULL; } } /* This is the default inquiry callback. We use it to handle the Pinentry notifications. */ static gpgme_error_t default_inq_cb (engine_gpgsm_t gpgsm, const char *line) { (void)gpgsm; if (!strncmp (line, "PINENTRY_LAUNCHED", 17) && (line[17]==' '||!line[17])) { _gpgme_allow_set_foreground_window ((pid_t)strtoul (line+17, NULL, 10)); } return 0; } static gpgme_error_t gpgsm_cancel (void *engine) { engine_gpgsm_t gpgsm = engine; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); if (gpgsm->status_cb.fd != -1) _gpgme_io_close (gpgsm->status_cb.fd); if (gpgsm->input_cb.fd != -1) _gpgme_io_close (gpgsm->input_cb.fd); if (gpgsm->output_cb.fd != -1) _gpgme_io_close (gpgsm->output_cb.fd); if (gpgsm->message_cb.fd != -1) _gpgme_io_close (gpgsm->message_cb.fd); if (gpgsm->assuan_ctx) { assuan_release (gpgsm->assuan_ctx); gpgsm->assuan_ctx = NULL; } return 0; } static void gpgsm_release (void *engine) { engine_gpgsm_t gpgsm = engine; if (!gpgsm) return; gpgsm_cancel (engine); free (gpgsm->colon.attic.line); free (gpgsm); } static gpgme_error_t gpgsm_new (void **engine, const char *file_name, const char *home_dir, const char *version) { gpgme_error_t err = 0; engine_gpgsm_t gpgsm; const char *pgmname; const char *argv[5]; int argc; #if !USE_DESCRIPTOR_PASSING int fds[2]; int child_fds[4]; #endif char *dft_display = NULL; char dft_ttyname[64]; char *env_tty = NULL; char *dft_ttytype = NULL; char *optstr; (void)version; /* Not yet used. */ gpgsm = calloc (1, sizeof *gpgsm); if (!gpgsm) return gpg_error_from_syserror (); gpgsm->status_cb.fd = -1; gpgsm->status_cb.dir = 1; gpgsm->status_cb.tag = 0; gpgsm->status_cb.data = gpgsm; gpgsm->input_cb.fd = -1; gpgsm->input_cb.dir = 0; gpgsm->input_cb.tag = 0; gpgsm->input_cb.server_fd = -1; *gpgsm->input_cb.server_fd_str = 0; gpgsm->output_cb.fd = -1; gpgsm->output_cb.dir = 1; gpgsm->output_cb.tag = 0; gpgsm->output_cb.server_fd = -1; *gpgsm->output_cb.server_fd_str = 0; gpgsm->message_cb.fd = -1; gpgsm->message_cb.dir = 0; gpgsm->message_cb.tag = 0; gpgsm->message_cb.server_fd = -1; *gpgsm->message_cb.server_fd_str = 0; gpgsm->status.fnc = 0; gpgsm->colon.fnc = 0; gpgsm->colon.attic.line = 0; gpgsm->colon.attic.linesize = 0; gpgsm->colon.attic.linelen = 0; gpgsm->colon.any = 0; gpgsm->inline_data = NULL; gpgsm->io_cbs.add = NULL; gpgsm->io_cbs.add_priv = NULL; gpgsm->io_cbs.remove = NULL; gpgsm->io_cbs.event = NULL; gpgsm->io_cbs.event_priv = NULL; #if !USE_DESCRIPTOR_PASSING if (_gpgme_io_pipe (fds, 0) < 0) { err = gpg_error_from_syserror (); goto leave; } gpgsm->input_cb.fd = fds[1]; gpgsm->input_cb.server_fd = fds[0]; if (_gpgme_io_pipe (fds, 1) < 0) { err = gpg_error_from_syserror (); goto leave; } gpgsm->output_cb.fd = fds[0]; gpgsm->output_cb.server_fd = fds[1]; if (_gpgme_io_pipe (fds, 0) < 0) { err = gpg_error_from_syserror (); goto leave; } gpgsm->message_cb.fd = fds[1]; gpgsm->message_cb.server_fd = fds[0]; child_fds[0] = gpgsm->input_cb.server_fd; child_fds[1] = gpgsm->output_cb.server_fd; child_fds[2] = gpgsm->message_cb.server_fd; child_fds[3] = -1; #endif pgmname = file_name ? file_name : _gpgme_get_default_gpgsm_name (); argc = 0; argv[argc++] = _gpgme_get_basename (pgmname); if (home_dir) { argv[argc++] = "--homedir"; argv[argc++] = home_dir; } argv[argc++] = "--server"; argv[argc++] = NULL; err = assuan_new_ext (&gpgsm->assuan_ctx, GPG_ERR_SOURCE_GPGME, &_gpgme_assuan_malloc_hooks, _gpgme_assuan_log_cb, NULL); if (err) goto leave; assuan_ctx_set_system_hooks (gpgsm->assuan_ctx, &_gpgme_assuan_system_hooks); #if USE_DESCRIPTOR_PASSING err = assuan_pipe_connect (gpgsm->assuan_ctx, pgmname, argv, NULL, NULL, NULL, ASSUAN_PIPE_CONNECT_FDPASSING); #else { assuan_fd_t achild_fds[4]; int i; /* For now... */ for (i = 0; i < 4; i++) achild_fds[i] = (assuan_fd_t) child_fds[i]; err = assuan_pipe_connect (gpgsm->assuan_ctx, pgmname, argv, achild_fds, NULL, NULL, 0); /* For now... */ for (i = 0; i < 4; i++) child_fds[i] = (int) achild_fds[i]; } /* On Windows, handles are inserted in the spawned process with DuplicateHandle, and child_fds contains the server-local names for the inserted handles when assuan_pipe_connect returns. */ if (!err) { /* Note: We don't use _gpgme_io_fd2str here. On W32 the returned handles are real W32 system handles, not whatever GPGME uses internally (which may be a system handle, a C library handle or a GLib/Qt channel. Confusing, yes, but remember these are server-local names, so they are not part of GPGME at all. */ snprintf (gpgsm->input_cb.server_fd_str, sizeof gpgsm->input_cb.server_fd_str, "%d", child_fds[0]); snprintf (gpgsm->output_cb.server_fd_str, sizeof gpgsm->output_cb.server_fd_str, "%d", child_fds[1]); snprintf (gpgsm->message_cb.server_fd_str, sizeof gpgsm->message_cb.server_fd_str, "%d", child_fds[2]); } #endif if (err) goto leave; err = _gpgme_getenv ("DISPLAY", &dft_display); if (err) goto leave; if (dft_display) { if (gpgrt_asprintf (&optstr, "OPTION display=%s", dft_display) < 0) { free (dft_display); err = gpg_error_from_syserror (); goto leave; } free (dft_display); err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; } err = _gpgme_getenv ("GPG_TTY", &env_tty); if (isatty (1) || env_tty || err) { int rc = 0; if (err) goto leave; else if (env_tty) { snprintf (dft_ttyname, sizeof (dft_ttyname), "%s", env_tty); free (env_tty); } else rc = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); /* Even though isatty() returns 1, ttyname_r() may fail in many ways, e.g., when /dev/pts is not accessible under chroot. */ if (!rc) { if (gpgrt_asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0) { err = gpg_error_from_syserror (); goto leave; } err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; err = _gpgme_getenv ("TERM", &dft_ttytype); if (err) goto leave; if (dft_ttytype) { if (gpgrt_asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype)< 0) { free (dft_ttytype); err = gpg_error_from_syserror (); goto leave; } free (dft_ttytype); err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; } } } /* Ask gpgsm to enable the audit log support. */ if (!err) { err = assuan_transact (gpgsm->assuan_ctx, "OPTION enable-audit-log=1", NULL, NULL, NULL, NULL, NULL, NULL); if (gpg_err_code (err) == GPG_ERR_UNKNOWN_OPTION) err = 0; /* This is an optional feature of gpgsm. */ } #ifdef HAVE_W32_SYSTEM /* Under Windows we need to use AllowSetForegroundWindow. Tell gpgsm to tell us when it needs it. */ if (!err) { err = assuan_transact (gpgsm->assuan_ctx, "OPTION allow-pinentry-notify", NULL, NULL, NULL, NULL, NULL, NULL); if (gpg_err_code (err) == GPG_ERR_UNKNOWN_OPTION) err = 0; /* This is a new feature of gpgsm. */ } #endif /*HAVE_W32_SYSTEM*/ #if !USE_DESCRIPTOR_PASSING if (!err && (_gpgme_io_set_close_notify (gpgsm->input_cb.fd, close_notify_handler, gpgsm) || _gpgme_io_set_close_notify (gpgsm->output_cb.fd, close_notify_handler, gpgsm) || _gpgme_io_set_close_notify (gpgsm->message_cb.fd, close_notify_handler, gpgsm))) { err = gpg_error (GPG_ERR_GENERAL); goto leave; } #endif leave: /* Close the server ends of the pipes (because of this, we must use the stored server_fd_str in the function start). Our ends are closed in gpgsm_release(). */ #if !USE_DESCRIPTOR_PASSING if (gpgsm->input_cb.server_fd != -1) _gpgme_io_close (gpgsm->input_cb.server_fd); if (gpgsm->output_cb.server_fd != -1) _gpgme_io_close (gpgsm->output_cb.server_fd); if (gpgsm->message_cb.server_fd != -1) _gpgme_io_close (gpgsm->message_cb.server_fd); #endif if (err) gpgsm_release (gpgsm); else *engine = gpgsm; return err; } /* Copy flags from CTX into the engine object. */ static void gpgsm_set_engine_flags (void *engine, const gpgme_ctx_t ctx) { engine_gpgsm_t gpgsm = engine; if (ctx->request_origin) { if (strlen (ctx->request_origin) + 1 > sizeof gpgsm->request_origin) strcpy (gpgsm->request_origin, "xxx"); /* Too long - force error */ else strcpy (gpgsm->request_origin, ctx->request_origin); } else *gpgsm->request_origin = 0; } static gpgme_error_t gpgsm_set_locale (void *engine, int category, const char *value) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; char *optstr; const char *catstr; /* FIXME: If value is NULL, we need to reset the option to default. But we can't do this. So we error out here. GPGSM needs support for this. */ if (0) ; #ifdef LC_CTYPE else if (category == LC_CTYPE) { catstr = "lc-ctype"; if (!value && gpgsm->lc_ctype_set) return gpg_error (GPG_ERR_INV_VALUE); if (value) gpgsm->lc_ctype_set = 1; } #endif #ifdef LC_MESSAGES else if (category == LC_MESSAGES) { catstr = "lc-messages"; if (!value && gpgsm->lc_messages_set) return gpg_error (GPG_ERR_INV_VALUE); if (value) gpgsm->lc_messages_set = 1; } #endif /* LC_MESSAGES */ else return gpg_error (GPG_ERR_INV_VALUE); /* FIXME: Reset value to default. */ if (!value) return 0; if (gpgrt_asprintf (&optstr, "OPTION %s=%s", catstr, value) < 0) err = gpg_error_from_syserror (); else { err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); } return err; } static gpgme_error_t gpgsm_assuan_simple_command (engine_gpgsm_t gpgsm, const char *cmd, engine_status_handler_t status_fnc, void *status_fnc_value) { assuan_context_t ctx = gpgsm->assuan_ctx; gpg_error_t err, cb_err; char *line; size_t linelen; err = assuan_write_line (ctx, cmd); if (err) return err; cb_err = 0; do { err = assuan_read_line (ctx, &line, &linelen); if (err) break; if (*line == '#' || !linelen) continue; if (linelen >= 2 && line[0] == 'O' && line[1] == 'K' && (line[2] == '\0' || line[2] == ' ')) break; else if (linelen >= 4 && line[0] == 'E' && line[1] == 'R' && line[2] == 'R' && line[3] == ' ') { /* We prefer a callback generated error because that one is more related to gpgme and thus probably more important than the error returned by the engine. */ err = cb_err? cb_err : atoi (&line[4]); cb_err = 0; } else if (linelen >= 2 && line[0] == 'S' && line[1] == ' ') { /* After an error from a status callback we skip all further status lines. */ if (!cb_err) { char *rest; gpgme_status_code_t r; rest = strchr (line + 2, ' '); if (!rest) rest = line + linelen; /* set to an empty string */ else *(rest++) = 0; r = _gpgme_parse_status (line + 2); if (gpgsm->status.mon_cb && r != GPGME_STATUS_PROGRESS) { /* Note that we call the monitor even if we do * not know the status code (r < 0). */ cb_err = gpgsm->status.mon_cb (gpgsm->status.mon_cb_value, line + 2, rest); } if (r >= 0 && status_fnc && !cb_err) cb_err = status_fnc (status_fnc_value, r, rest); } } else { /* Invalid line or INQUIRY. We can't do anything else than to stop. As with ERR we prefer a status callback generated error code, though. */ err = cb_err ? cb_err : gpg_error (GPG_ERR_GENERAL); cb_err = 0; } } while (!err); /* We only want the first error from the status handler, thus we * take the one saved in CB_ERR. */ if (!err && cb_err) err = cb_err; return err; } typedef enum { INPUT_FD, OUTPUT_FD, MESSAGE_FD } fd_type_t; static void gpgsm_clear_fd (engine_gpgsm_t gpgsm, fd_type_t fd_type) { #if !USE_DESCRIPTOR_PASSING switch (fd_type) { case INPUT_FD: _gpgme_io_close (gpgsm->input_cb.fd); break; case OUTPUT_FD: _gpgme_io_close (gpgsm->output_cb.fd); break; case MESSAGE_FD: _gpgme_io_close (gpgsm->message_cb.fd); break; } #else (void)gpgsm; (void)fd_type; #endif } #define COMMANDLINELEN 40 static gpgme_error_t gpgsm_set_fd (engine_gpgsm_t gpgsm, fd_type_t fd_type, const char *opt) { gpg_error_t err = 0; char line[COMMANDLINELEN]; const char *which; iocb_data_t *iocb_data; #if USE_DESCRIPTOR_PASSING int dir; #endif switch (fd_type) { case INPUT_FD: which = "INPUT"; iocb_data = &gpgsm->input_cb; break; case OUTPUT_FD: which = "OUTPUT"; iocb_data = &gpgsm->output_cb; break; case MESSAGE_FD: which = "MESSAGE"; iocb_data = &gpgsm->message_cb; break; default: return gpg_error (GPG_ERR_INV_VALUE); } #if USE_DESCRIPTOR_PASSING dir = iocb_data->dir; /* We try to short-cut the communication by giving GPGSM direct access to the file descriptor, rather than using a pipe. */ iocb_data->server_fd = _gpgme_data_get_fd (iocb_data->data); if (iocb_data->server_fd < 0) { int fds[2]; if (_gpgme_io_pipe (fds, dir) < 0) return gpg_error_from_syserror (); iocb_data->fd = dir ? fds[0] : fds[1]; iocb_data->server_fd = dir ? fds[1] : fds[0]; if (_gpgme_io_set_close_notify (iocb_data->fd, close_notify_handler, gpgsm)) { err = gpg_error (GPG_ERR_GENERAL); goto leave_set_fd; } } err = assuan_sendfd (gpgsm->assuan_ctx, iocb_data->server_fd); if (err) goto leave_set_fd; _gpgme_io_close (iocb_data->server_fd); iocb_data->server_fd = -1; if (opt) snprintf (line, COMMANDLINELEN, "%s FD %s", which, opt); else snprintf (line, COMMANDLINELEN, "%s FD", which); #else if (opt) snprintf (line, COMMANDLINELEN, "%s FD=%s %s", which, iocb_data->server_fd_str, opt); else snprintf (line, COMMANDLINELEN, "%s FD=%s", which, iocb_data->server_fd_str); #endif err = gpgsm_assuan_simple_command (gpgsm, line, NULL, NULL); #if USE_DESCRIPTOR_PASSING leave_set_fd: if (err) { _gpgme_io_close (iocb_data->fd); iocb_data->fd = -1; if (iocb_data->server_fd != -1) { _gpgme_io_close (iocb_data->server_fd); iocb_data->server_fd = -1; } } #endif return err; } static const char * map_data_enc (gpgme_data_t d) { switch (gpgme_data_get_encoding (d)) { case GPGME_DATA_ENCODING_NONE: break; case GPGME_DATA_ENCODING_BINARY: return "--binary"; case GPGME_DATA_ENCODING_BASE64: return "--base64"; case GPGME_DATA_ENCODING_ARMOR: return "--armor"; default: break; } return NULL; } static gpgme_error_t status_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; engine_gpgsm_t gpgsm = (engine_gpgsm_t) data->handler_value; gpgme_error_t err = 0; char *line; size_t linelen; do { err = assuan_read_line (gpgsm->assuan_ctx, &line, &linelen); if (err) { /* Try our best to terminate the connection friendly. */ /* assuan_write_line (gpgsm->assuan_ctx, "BYE"); */ TRACE3 (DEBUG_CTX, "gpgme:status_handler", gpgsm, "fd 0x%x: error from assuan (%d) getting status line : %s", fd, err, gpg_strerror (err)); } else if (linelen >= 3 && line[0] == 'E' && line[1] == 'R' && line[2] == 'R' && (line[3] == '\0' || line[3] == ' ')) { if (line[3] == ' ') err = atoi (&line[4]); if (! err) err = gpg_error (GPG_ERR_GENERAL); TRACE2 (DEBUG_CTX, "gpgme:status_handler", gpgsm, "fd 0x%x: ERR line - mapped to: %s", fd, err ? gpg_strerror (err) : "ok"); /* Try our best to terminate the connection friendly. */ /* assuan_write_line (gpgsm->assuan_ctx, "BYE"); */ } else if (linelen >= 2 && line[0] == 'O' && line[1] == 'K' && (line[2] == '\0' || line[2] == ' ')) { if (gpgsm->status.fnc) { char emptystring[1] = {0}; err = gpgsm->status.fnc (gpgsm->status.fnc_value, GPGME_STATUS_EOF, emptystring); if (gpg_err_code (err) == GPG_ERR_FALSE) err = 0; /* Drop special error code. */ } if (!err && gpgsm->colon.fnc && gpgsm->colon.any) { /* We must tell a colon function about the EOF. We do this only when we have seen any data lines. Note that this inlined use of colon data lines will eventually be changed into using a regular data channel. */ gpgsm->colon.any = 0; err = gpgsm->colon.fnc (gpgsm->colon.fnc_value, NULL); } TRACE2 (DEBUG_CTX, "gpgme:status_handler", gpgsm, "fd 0x%x: OK line - final status: %s", fd, err ? gpg_strerror (err) : "ok"); _gpgme_io_close (gpgsm->status_cb.fd); return err; } else if (linelen > 2 && line[0] == 'D' && line[1] == ' ' && gpgsm->colon.fnc) { /* We are using the colon handler even for plain inline data - strange name for that function but for historic reasons we keep it. */ /* FIXME We can't use this for binary data because we assume this is a string. For the current usage of colon output it is correct. */ char *src = line + 2; char *end = line + linelen; char *dst; char **aline = &gpgsm->colon.attic.line; int *alinelen = &gpgsm->colon.attic.linelen; if (gpgsm->colon.attic.linesize < *alinelen + linelen + 1) { char *newline = realloc (*aline, *alinelen + linelen + 1); if (!newline) err = gpg_error_from_syserror (); else { *aline = newline; gpgsm->colon.attic.linesize = *alinelen + linelen + 1; } } if (!err) { dst = *aline + *alinelen; while (!err && src < end) { if (*src == '%' && src + 2 < end) { /* Handle escaped characters. */ ++src; *dst = _gpgme_hextobyte (src); (*alinelen)++; src += 2; } else { *dst = *src++; (*alinelen)++; } if (*dst == '\n') { /* Terminate the pending line, pass it to the colon handler and reset it. */ gpgsm->colon.any = 1; if (*alinelen > 1 && *(dst - 1) == '\r') dst--; *dst = '\0'; /* FIXME How should we handle the return code? */ err = gpgsm->colon.fnc (gpgsm->colon.fnc_value, *aline); if (!err) { dst = *aline; *alinelen = 0; } } else dst++; } } TRACE2 (DEBUG_CTX, "gpgme:status_handler", gpgsm, "fd 0x%x: D line; final status: %s", fd, err? gpg_strerror (err):"ok"); } else if (linelen > 2 && line[0] == 'D' && line[1] == ' ' && gpgsm->inline_data) { char *src = line + 2; char *end = line + linelen; char *dst = src; gpgme_ssize_t nwritten; linelen = 0; while (src < end) { if (*src == '%' && src + 2 < end) { /* Handle escaped characters. */ ++src; *dst++ = _gpgme_hextobyte (src); src += 2; } else *dst++ = *src++; linelen++; } src = line + 2; while (linelen > 0) { nwritten = gpgme_data_write (gpgsm->inline_data, src, linelen); if (!nwritten || (nwritten < 0 && errno != EINTR) || nwritten > linelen) { err = gpg_error_from_syserror (); break; } src += nwritten; linelen -= nwritten; } TRACE2 (DEBUG_CTX, "gpgme:status_handler", gpgsm, "fd 0x%x: D inlinedata; final status: %s", fd, err? gpg_strerror (err):"ok"); } else if (linelen > 2 && line[0] == 'S' && line[1] == ' ') { char *rest; gpgme_status_code_t r; rest = strchr (line + 2, ' '); if (!rest) rest = line + linelen; /* set to an empty string */ else *(rest++) = 0; r = _gpgme_parse_status (line + 2); if (r >= 0) { if (gpgsm->status.fnc) { err = gpgsm->status.fnc (gpgsm->status.fnc_value, r, rest); if (gpg_err_code (err) == GPG_ERR_FALSE) err = 0; /* Drop special error code. */ } } else fprintf (stderr, "[UNKNOWN STATUS]%s %s", line + 2, rest); TRACE3 (DEBUG_CTX, "gpgme:status_handler", gpgsm, "fd 0x%x: S line (%s) - final status: %s", fd, line+2, err? gpg_strerror (err):"ok"); } else if (linelen >= 7 && line[0] == 'I' && line[1] == 'N' && line[2] == 'Q' && line[3] == 'U' && line[4] == 'I' && line[5] == 'R' && line[6] == 'E' && (line[7] == '\0' || line[7] == ' ')) { char *keyword = line+7; while (*keyword == ' ') keyword++;; default_inq_cb (gpgsm, keyword); assuan_write_line (gpgsm->assuan_ctx, "END"); } } while (!err && assuan_pending_line (gpgsm->assuan_ctx)); return err; } static gpgme_error_t add_io_cb (engine_gpgsm_t gpgsm, iocb_data_t *iocbd, gpgme_io_cb_t handler) { gpgme_error_t err; TRACE_BEG2 (DEBUG_ENGINE, "engine-gpgsm:add_io_cb", gpgsm, "fd %d, dir %d", iocbd->fd, iocbd->dir); err = (*gpgsm->io_cbs.add) (gpgsm->io_cbs.add_priv, iocbd->fd, iocbd->dir, handler, iocbd->data, &iocbd->tag); if (err) return TRACE_ERR (err); if (!iocbd->dir) /* FIXME Kludge around poll() problem. */ err = _gpgme_io_set_nonblocking (iocbd->fd); return TRACE_ERR (err); } static gpgme_error_t start (engine_gpgsm_t gpgsm, const char *command) { gpgme_error_t err; assuan_fd_t afdlist[5]; int fdlist[5]; int nfds; int i; if (*gpgsm->request_origin) { char *cmd; cmd = _gpgme_strconcat ("OPTION request-origin=", gpgsm->request_origin, NULL); if (!cmd) return gpg_error_from_syserror (); err = gpgsm_assuan_simple_command (gpgsm, cmd, NULL, NULL); free (cmd); if (err && gpg_err_code (err) != GPG_ERR_UNKNOWN_OPTION) return err; } /* We need to know the fd used by assuan for reads. We do this by using the assumption that the first returned fd from assuan_get_active_fds() is always this one. */ nfds = assuan_get_active_fds (gpgsm->assuan_ctx, 0 /* read fds */, afdlist, DIM (afdlist)); if (nfds < 1) return gpg_error (GPG_ERR_GENERAL); /* FIXME */ /* For now... */ for (i = 0; i < nfds; i++) fdlist[i] = (int) afdlist[i]; /* We "duplicate" the file descriptor, so we can close it here (we can't close fdlist[0], as that is closed by libassuan, and closing it here might cause libassuan to close some unrelated FD later). Alternatively, we could special case status_fd and register/unregister it manually as needed, but this increases code duplication and is more complicated as we can not use the close notifications etc. A third alternative would be to let Assuan know that we closed the FD, but that complicates the Assuan interface. */ gpgsm->status_cb.fd = _gpgme_io_dup (fdlist[0]); if (gpgsm->status_cb.fd < 0) return gpg_error_from_syserror (); if (_gpgme_io_set_close_notify (gpgsm->status_cb.fd, close_notify_handler, gpgsm)) { _gpgme_io_close (gpgsm->status_cb.fd); gpgsm->status_cb.fd = -1; return gpg_error (GPG_ERR_GENERAL); } err = add_io_cb (gpgsm, &gpgsm->status_cb, status_handler); if (!err && gpgsm->input_cb.fd != -1) err = add_io_cb (gpgsm, &gpgsm->input_cb, _gpgme_data_outbound_handler); if (!err && gpgsm->output_cb.fd != -1) err = add_io_cb (gpgsm, &gpgsm->output_cb, _gpgme_data_inbound_handler); if (!err && gpgsm->message_cb.fd != -1) err = add_io_cb (gpgsm, &gpgsm->message_cb, _gpgme_data_outbound_handler); if (!err) err = assuan_write_line (gpgsm->assuan_ctx, command); if (!err) gpgsm_io_event (gpgsm, GPGME_EVENT_START, NULL); return err; } #if USE_DESCRIPTOR_PASSING static gpgme_error_t gpgsm_reset (void *engine) { engine_gpgsm_t gpgsm = engine; /* IF we have an active connection we must send a reset because we need to reset the list of signers. Note that RESET does not reset OPTION commands. */ return (gpgsm->assuan_ctx ? gpgsm_assuan_simple_command (gpgsm, "RESET", NULL, NULL) : 0); } #endif static gpgme_error_t gpgsm_decrypt (void *engine, gpgme_decrypt_flags_t flags, gpgme_data_t ciph, gpgme_data_t plain, int export_session_key, const char *override_session_key, int auto_key_retrieve) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; (void)flags; /* gpgsm is not capable of exporting session keys right now, so we * will ignore this if requested. */ (void)export_session_key; (void)override_session_key; /* --auto-key-retrieve is also not supported. */ (void)auto_key_retrieve; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); gpgsm->input_cb.data = ciph; err = gpgsm_set_fd (gpgsm, INPUT_FD, map_data_enc (gpgsm->input_cb.data)); if (err) return gpg_error (GPG_ERR_GENERAL); /* FIXME */ gpgsm->output_cb.data = plain; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, 0); if (err) return gpg_error (GPG_ERR_GENERAL); /* FIXME */ gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (engine, "DECRYPT"); return err; } static gpgme_error_t gpgsm_delete (void *engine, gpgme_key_t key, unsigned int flags) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; char *fpr = key->subkeys ? key->subkeys->fpr : NULL; char *linep = fpr; char *line; int length = 8; /* "DELKEYS " */ (void)flags; if (!fpr) return gpg_error (GPG_ERR_INV_VALUE); while (*linep) { length++; if (*linep == '%' || *linep == ' ' || *linep == '+') length += 2; linep++; } length++; line = malloc (length); if (!line) return gpg_error_from_syserror (); strcpy (line, "DELKEYS "); linep = &line[8]; while (*fpr) { switch (*fpr) { case '%': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = '5'; break; case ' ': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = '0'; break; case '+': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = 'B'; break; default: *(linep++) = *fpr; break; } fpr++; } *linep = '\0'; gpgsm_clear_fd (gpgsm, OUTPUT_FD); gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, line); free (line); return err; } static gpgme_error_t set_recipients (engine_gpgsm_t gpgsm, gpgme_key_t recp[]) { gpgme_error_t err = 0; char *line; int linelen; int invalid_recipients = 0; int i; linelen = 10 + 40 + 1; /* "RECIPIENT " + guess + '\0'. */ line = malloc (10 + 40 + 1); if (!line) return gpg_error_from_syserror (); strcpy (line, "RECIPIENT "); for (i =0; !err && recp[i]; i++) { char *fpr; int newlen; if (!recp[i]->subkeys || !recp[i]->subkeys->fpr) { invalid_recipients++; continue; } fpr = recp[i]->subkeys->fpr; newlen = 11 + strlen (fpr); if (linelen < newlen) { char *newline = realloc (line, newlen); if (! newline) { int saved_err = gpg_error_from_syserror (); free (line); return saved_err; } line = newline; linelen = newlen; } strcpy (&line[10], fpr); err = gpgsm_assuan_simple_command (gpgsm, line, gpgsm->status.fnc, gpgsm->status.fnc_value); /* FIXME: This requires more work. */ if (gpg_err_code (err) == GPG_ERR_NO_PUBKEY) invalid_recipients++; else if (err) { free (line); return err; } } free (line); return gpg_error (invalid_recipients ? GPG_ERR_UNUSABLE_PUBKEY : GPG_ERR_NO_ERROR); } +/* Take recipients from the LF delimited STRING and send RECIPIENT + * commands to gpgsm. */ static gpgme_error_t -gpgsm_encrypt (void *engine, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, +set_recipients_from_string (engine_gpgsm_t gpgsm, const char *string) +{ + gpg_error_t err = 0; + char *line = NULL; + int no_pubkey = 0; + const char *s; + int n; + + for (;;) + { + while (*string == ' ' || *string == '\t') + string++; + if (!*string) + break; + + s = strchr (string, '\n'); + if (s) + n = s - string; + else + n = strlen (string); + while (n && (string[n-1] == ' ' || string[n-1] == '\t')) + n--; + + gpgrt_free (line); + if (gpgrt_asprintf (&line, "RECIPIENT %.*s", n, string) < 0) + { + err = gpg_error_from_syserror (); + break; + } + string += n + !!s; + + err = gpgsm_assuan_simple_command (gpgsm, line, gpgsm->status.fnc, + gpgsm->status.fnc_value); + + /* Fixme: Improve error reporting. */ + if (gpg_err_code (err) == GPG_ERR_NO_PUBKEY) + no_pubkey++; + else if (err) + break; + } + gpgrt_free (line); + return err? err : no_pubkey? gpg_error (GPG_ERR_NO_PUBKEY) : 0; +} + + +static gpgme_error_t +gpgsm_encrypt (void *engine, gpgme_key_t recp[], const char *recpstring, + gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); if (!recp) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); - if (flags & GPGME_ENCRYPT_NO_ENCRYPT_TO) + if ((flags & GPGME_ENCRYPT_NO_ENCRYPT_TO)) { err = gpgsm_assuan_simple_command (gpgsm, "OPTION no-encrypt-to", NULL, NULL); if (err) return err; } gpgsm->input_cb.data = plain; err = gpgsm_set_fd (gpgsm, INPUT_FD, map_data_enc (gpgsm->input_cb.data)); if (err) return err; gpgsm->output_cb.data = ciph; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, use_armor ? "--armor" : map_data_enc (gpgsm->output_cb.data)); if (err) return err; gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; - err = set_recipients (gpgsm, recp); + if (!recp && recpstring) + err = set_recipients_from_string (gpgsm, recpstring); + else + err = set_recipients (gpgsm, recp); if (!err) err = start (gpgsm, "ENCRYPT"); return err; } static gpgme_error_t gpgsm_export (void *engine, const char *pattern, gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err = 0; char *cmd; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); if (!pattern) pattern = ""; cmd = malloc (7 + 9 + 9 + strlen (pattern) + 1); if (!cmd) return gpg_error_from_syserror (); strcpy (cmd, "EXPORT "); if ((mode & GPGME_EXPORT_MODE_SECRET)) { strcat (cmd, "--secret "); if ((mode & GPGME_EXPORT_MODE_RAW)) strcat (cmd, "--raw "); else if ((mode & GPGME_EXPORT_MODE_PKCS12)) strcat (cmd, "--pkcs12 "); } strcat (cmd, pattern); gpgsm->output_cb.data = keydata; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, use_armor ? "--armor" : map_data_enc (gpgsm->output_cb.data)); if (err) return err; gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, cmd); free (cmd); return err; } static gpgme_error_t gpgsm_export_ext (void *engine, const char *pattern[], gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err = 0; char *line; /* Length is "EXPORT " + "--secret " + "--pkcs12 " + p + '\0'. */ int length = 7 + 9 + 9 + 1; char *linep; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); if (pattern && *pattern) { const char **pat = pattern; while (*pat) { const char *patlet = *pat; while (*patlet) { length++; if (*patlet == '%' || *patlet == ' ' || *patlet == '+') length += 2; patlet++; } pat++; length++; } } line = malloc (length); if (!line) return gpg_error_from_syserror (); strcpy (line, "EXPORT "); if ((mode & GPGME_EXPORT_MODE_SECRET)) { strcat (line, "--secret "); if ((mode & GPGME_EXPORT_MODE_RAW)) strcat (line, "--raw "); else if ((mode & GPGME_EXPORT_MODE_PKCS12)) strcat (line, "--pkcs12 "); } linep = &line[strlen (line)]; if (pattern && *pattern) { while (*pattern) { const char *patlet = *pattern; while (*patlet) { switch (*patlet) { case '%': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = '5'; break; case ' ': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = '0'; break; case '+': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = 'B'; break; default: *(linep++) = *patlet; break; } patlet++; } pattern++; if (*pattern) *linep++ = ' '; } } *linep = '\0'; gpgsm->output_cb.data = keydata; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, use_armor ? "--armor" : map_data_enc (gpgsm->output_cb.data)); if (err) return err; gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, line); free (line); return err; } static gpgme_error_t gpgsm_genkey (void *engine, const char *userid, const char *algo, unsigned long reserved, unsigned long expires, gpgme_key_t key, unsigned int flags, gpgme_data_t help_data, unsigned int extraflags, gpgme_data_t pubkey, gpgme_data_t seckey) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; (void)reserved; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); if (help_data) { if (!pubkey || seckey) return gpg_error (GPG_ERR_INV_VALUE); gpgsm->input_cb.data = help_data; err = gpgsm_set_fd (gpgsm, INPUT_FD, map_data_enc (gpgsm->input_cb.data)); if (err) return err; gpgsm->output_cb.data = pubkey; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, (extraflags & GENKEY_EXTRAFLAG_ARMOR)? "--armor" : map_data_enc (gpgsm->output_cb.data)); if (err) return err; gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, "GENKEY"); return err; } (void)userid; (void)algo; (void)expires; (void)key; (void)flags; /* The new interface has not yet been implemented, */ return gpg_error (GPG_ERR_NOT_IMPLEMENTED); } static gpgme_error_t gpgsm_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; gpgme_data_encoding_t dataenc; int idx; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); if (keydata && keyarray) return gpg_error (GPG_ERR_INV_VALUE); /* Only one is allowed. */ dataenc = gpgme_data_get_encoding (keydata); if (keyarray) { size_t buflen; char *buffer, *p; /* Fist check whether the engine already features the --re-import option. */ err = gpgsm_assuan_simple_command (gpgsm, "GETINFO cmd_has_option IMPORT re-import", NULL, NULL); if (err) return gpg_error (GPG_ERR_NOT_SUPPORTED); /* Create an internal data object with a list of all fingerprints. The data object and its memory (to avoid an extra copy by gpgme_data_new_from_mem) are stored in two variables which are released by the close_notify_handler. */ for (idx=0, buflen=0; keyarray[idx]; idx++) { if (keyarray[idx]->protocol == GPGME_PROTOCOL_CMS && keyarray[idx]->subkeys && keyarray[idx]->subkeys->fpr && *keyarray[idx]->subkeys->fpr) buflen += strlen (keyarray[idx]->subkeys->fpr) + 1; } /* Allocate a bufer with extra space for the trailing Nul introduced by the use of stpcpy. */ buffer = malloc (buflen+1); if (!buffer) return gpg_error_from_syserror (); for (idx=0, p = buffer; keyarray[idx]; idx++) { if (keyarray[idx]->protocol == GPGME_PROTOCOL_CMS && keyarray[idx]->subkeys && keyarray[idx]->subkeys->fpr && *keyarray[idx]->subkeys->fpr) p = stpcpy (stpcpy (p, keyarray[idx]->subkeys->fpr), "\n"); } err = gpgme_data_new_from_mem (&gpgsm->input_helper_data, buffer, buflen, 0); if (err) { free (buffer); return err; } gpgsm->input_helper_memory = buffer; gpgsm->input_cb.data = gpgsm->input_helper_data; err = gpgsm_set_fd (gpgsm, INPUT_FD, map_data_enc (gpgsm->input_cb.data)); if (err) { gpgme_data_release (gpgsm->input_helper_data); gpgsm->input_helper_data = NULL; free (gpgsm->input_helper_memory); gpgsm->input_helper_memory = NULL; return err; } gpgsm_clear_fd (gpgsm, OUTPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; return start (gpgsm, "IMPORT --re-import"); } else if (dataenc == GPGME_DATA_ENCODING_URL || dataenc == GPGME_DATA_ENCODING_URL0 || dataenc == GPGME_DATA_ENCODING_URLESC) { return gpg_error (GPG_ERR_NOT_IMPLEMENTED); } else { gpgsm->input_cb.data = keydata; err = gpgsm_set_fd (gpgsm, INPUT_FD, map_data_enc (gpgsm->input_cb.data)); if (err) return err; gpgsm_clear_fd (gpgsm, OUTPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; return start (gpgsm, "IMPORT"); } } static gpgme_error_t gpgsm_keylist (void *engine, const char *pattern, int secret_only, gpgme_keylist_mode_t mode, int engine_flags) { engine_gpgsm_t gpgsm = engine; char *line; gpgme_error_t err; int list_mode = 0; if (mode & GPGME_KEYLIST_MODE_LOCAL) list_mode |= 1; if (mode & GPGME_KEYLIST_MODE_EXTERN) list_mode |= 2; if (!pattern) pattern = ""; /* Hack to make sure that the agent is started. Only if the agent has been started an application may connect to the agent via GPGME_PROTOCOL_ASSUAN - for example to look for smartcards. We do this only if a secret key listing has been requested. In general this is not needed because a secret key listing starts the agent. However on a fresh installation no public keys are available and thus there is no need for gpgsm to ask the agent whether a secret key exists for the public key. */ if (secret_only || (mode & GPGME_KEYLIST_MODE_WITH_SECRET)) gpgsm_assuan_simple_command (gpgsm, "GETINFO agent-check", NULL, NULL); /* Always send list-mode option because RESET does not reset it. */ if (gpgrt_asprintf (&line, "OPTION list-mode=%d", (list_mode & 3)) < 0) return gpg_error_from_syserror (); err = gpgsm_assuan_simple_command (gpgsm, line, NULL, NULL); gpgrt_free (line); if (err) return err; /* Always send key validation because RESET does not reset it. */ /* Use the validation mode if requested. We don't check for an error yet because this is a pretty fresh gpgsm features. */ gpgsm_assuan_simple_command (gpgsm, (mode & GPGME_KEYLIST_MODE_VALIDATE)? "OPTION with-validation=1": "OPTION with-validation=0" , NULL, NULL); /* Include the ephemeral keys if requested. We don't check for an error yet because this is a pretty fresh gpgsm features. */ gpgsm_assuan_simple_command (gpgsm, (mode & GPGME_KEYLIST_MODE_EPHEMERAL)? "OPTION with-ephemeral-keys=1": "OPTION with-ephemeral-keys=0" , NULL, NULL); gpgsm_assuan_simple_command (gpgsm, (mode & GPGME_KEYLIST_MODE_WITH_SECRET)? "OPTION with-secret=1": "OPTION with-secret=0" , NULL, NULL); gpgsm_assuan_simple_command (gpgsm, (engine_flags & GPGME_ENGINE_FLAG_OFFLINE)? "OPTION offline=1": "OPTION offline=0" , NULL, NULL); /* Length is "LISTSECRETKEYS " + p + '\0'. */ line = malloc (15 + strlen (pattern) + 1); if (!line) return gpg_error_from_syserror (); if (secret_only) { strcpy (line, "LISTSECRETKEYS "); strcpy (&line[15], pattern); } else { strcpy (line, "LISTKEYS "); strcpy (&line[9], pattern); } gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, OUTPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, line); free (line); return err; } static gpgme_error_t gpgsm_keylist_ext (void *engine, const char *pattern[], int secret_only, int reserved, gpgme_keylist_mode_t mode, int engine_flags) { engine_gpgsm_t gpgsm = engine; char *line; gpgme_error_t err; /* Length is "LISTSECRETKEYS " + p + '\0'. */ int length = 15 + 1; char *linep; int any_pattern = 0; int list_mode = 0; if (reserved) return gpg_error (GPG_ERR_INV_VALUE); if (mode & GPGME_KEYLIST_MODE_LOCAL) list_mode |= 1; if (mode & GPGME_KEYLIST_MODE_EXTERN) list_mode |= 2; /* Always send list-mode option because RESET does not reset it. */ if (gpgrt_asprintf (&line, "OPTION list-mode=%d", (list_mode & 3)) < 0) return gpg_error_from_syserror (); err = gpgsm_assuan_simple_command (gpgsm, line, NULL, NULL); gpgrt_free (line); if (err) return err; /* Always send key validation because RESET does not reset it. */ /* Use the validation mode if required. We don't check for an error yet because this is a pretty fresh gpgsm features. */ gpgsm_assuan_simple_command (gpgsm, (mode & GPGME_KEYLIST_MODE_VALIDATE)? "OPTION with-validation=1": "OPTION with-validation=0" , NULL, NULL); gpgsm_assuan_simple_command (gpgsm, (mode & GPGME_KEYLIST_MODE_WITH_SECRET)? "OPTION with-secret=1": "OPTION with-secret=0" , NULL, NULL); gpgsm_assuan_simple_command (gpgsm, (engine_flags & GPGME_ENGINE_FLAG_OFFLINE)? "OPTION offline=1": "OPTION offline=0" , NULL, NULL); if (pattern && *pattern) { const char **pat = pattern; while (*pat) { const char *patlet = *pat; while (*patlet) { length++; if (*patlet == '%' || *patlet == ' ' || *patlet == '+') length += 2; patlet++; } pat++; length++; } } line = malloc (length); if (!line) return gpg_error_from_syserror (); if (secret_only) { strcpy (line, "LISTSECRETKEYS "); linep = &line[15]; } else { strcpy (line, "LISTKEYS "); linep = &line[9]; } if (pattern && *pattern) { while (*pattern) { const char *patlet = *pattern; while (*patlet) { switch (*patlet) { case '%': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = '5'; break; case ' ': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = '0'; break; case '+': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = 'B'; break; default: *(linep++) = *patlet; break; } patlet++; } any_pattern = 1; *linep++ = ' '; pattern++; } } if (any_pattern) linep--; *linep = '\0'; gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, OUTPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, line); free (line); return err; } static gpgme_error_t gpgsm_sign (void *engine, gpgme_data_t in, gpgme_data_t out, gpgme_sig_mode_t mode, int use_armor, int use_textmode, int include_certs, gpgme_ctx_t ctx /* FIXME */) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; char *assuan_cmd; int i; gpgme_key_t key; (void)use_textmode; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); /* FIXME: This does not work as RESET does not reset it so we can't revert back to default. */ if (include_certs != GPGME_INCLUDE_CERTS_DEFAULT) { /* FIXME: Make sure that if we run multiple operations, that we can reset any previously set value in case the default is requested. */ if (gpgrt_asprintf (&assuan_cmd, "OPTION include-certs %i", include_certs) < 0) return gpg_error_from_syserror (); err = gpgsm_assuan_simple_command (gpgsm, assuan_cmd, NULL, NULL); gpgrt_free (assuan_cmd); if (err) return err; } for (i = 0; (key = gpgme_signers_enum (ctx, i)); i++) { const char *s = key->subkeys ? key->subkeys->fpr : NULL; if (s && strlen (s) < 80) { char buf[100]; strcpy (stpcpy (buf, "SIGNER "), s); err = gpgsm_assuan_simple_command (gpgsm, buf, gpgsm->status.fnc, gpgsm->status.fnc_value); } else err = gpg_error (GPG_ERR_INV_VALUE); gpgme_key_unref (key); if (err) return err; } gpgsm->input_cb.data = in; err = gpgsm_set_fd (gpgsm, INPUT_FD, map_data_enc (gpgsm->input_cb.data)); if (err) return err; gpgsm->output_cb.data = out; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, use_armor ? "--armor" : map_data_enc (gpgsm->output_cb.data)); if (err) return err; gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, mode == GPGME_SIG_MODE_DETACH ? "SIGN --detached" : "SIGN"); return err; } static gpgme_error_t gpgsm_verify (void *engine, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext, gpgme_ctx_t ctx) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; (void)ctx; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); gpgsm->input_cb.data = sig; err = gpgsm_set_fd (gpgsm, INPUT_FD, map_data_enc (gpgsm->input_cb.data)); if (err) return err; if (plaintext) { /* Normal or cleartext signature. */ gpgsm->output_cb.data = plaintext; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, 0); gpgsm_clear_fd (gpgsm, MESSAGE_FD); } else { /* Detached signature. */ gpgsm->message_cb.data = signed_text; err = gpgsm_set_fd (gpgsm, MESSAGE_FD, 0); gpgsm_clear_fd (gpgsm, OUTPUT_FD); } gpgsm->inline_data = NULL; if (!err) err = start (gpgsm, "VERIFY"); return err; } /* Send the GETAUDITLOG command. The result is saved to a gpgme data object. */ static gpgme_error_t gpgsm_getauditlog (void *engine, gpgme_data_t output, unsigned int flags) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err = 0; if (!gpgsm || !output) return gpg_error (GPG_ERR_INV_VALUE); #if USE_DESCRIPTOR_PASSING gpgsm->output_cb.data = output; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, 0); if (err) return err; gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; # define CMD "GETAUDITLOG" #else gpgsm_clear_fd (gpgsm, OUTPUT_FD); gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = output; # define CMD "GETAUDITLOG --data" #endif err = start (gpgsm, (flags & GPGME_AUDITLOG_HTML)? CMD " --html" : CMD); return err; } /* This sets a status callback for monitoring status lines before they * are passed to a caller set handler. */ static void gpgsm_set_status_cb (void *engine, gpgme_status_cb_t cb, void *cb_value) { engine_gpgsm_t gpgsm = engine; gpgsm->status.mon_cb = cb; gpgsm->status.mon_cb_value = cb_value; } static void gpgsm_set_status_handler (void *engine, engine_status_handler_t fnc, void *fnc_value) { engine_gpgsm_t gpgsm = engine; gpgsm->status.fnc = fnc; gpgsm->status.fnc_value = fnc_value; } static gpgme_error_t gpgsm_set_colon_line_handler (void *engine, engine_colon_line_handler_t fnc, void *fnc_value) { engine_gpgsm_t gpgsm = engine; gpgsm->colon.fnc = fnc; gpgsm->colon.fnc_value = fnc_value; gpgsm->colon.any = 0; return 0; } static void gpgsm_set_io_cbs (void *engine, gpgme_io_cbs_t io_cbs) { engine_gpgsm_t gpgsm = engine; gpgsm->io_cbs = *io_cbs; } static void gpgsm_io_event (void *engine, gpgme_event_io_t type, void *type_data) { engine_gpgsm_t gpgsm = engine; TRACE3 (DEBUG_ENGINE, "gpgme:gpgsm_io_event", gpgsm, "event %p, type %d, type_data %p", gpgsm->io_cbs.event, type, type_data); if (gpgsm->io_cbs.event) (*gpgsm->io_cbs.event) (gpgsm->io_cbs.event_priv, type, type_data); } static gpgme_error_t gpgsm_passwd (void *engine, gpgme_key_t key, unsigned int flags) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; char *line; (void)flags; if (!key || !key->subkeys || !key->subkeys->fpr) return gpg_error (GPG_ERR_INV_CERT_OBJ); if (gpgrt_asprintf (&line, "PASSWD -- %s", key->subkeys->fpr) < 0) return gpg_error_from_syserror (); gpgsm_clear_fd (gpgsm, OUTPUT_FD); gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, line); gpgrt_free (line); return err; } struct engine_ops _gpgme_engine_ops_gpgsm = { /* Static functions. */ _gpgme_get_default_gpgsm_name, NULL, gpgsm_get_version, gpgsm_get_req_version, gpgsm_new, /* Member functions. */ gpgsm_release, #if USE_DESCRIPTOR_PASSING gpgsm_reset, #else NULL, /* reset */ #endif gpgsm_set_status_cb, gpgsm_set_status_handler, NULL, /* set_command_handler */ gpgsm_set_colon_line_handler, gpgsm_set_locale, NULL, /* set_protocol */ gpgsm_set_engine_flags, gpgsm_decrypt, gpgsm_delete, /* decrypt_verify */ NULL, /* edit */ gpgsm_encrypt, NULL, /* encrypt_sign */ gpgsm_export, gpgsm_export_ext, gpgsm_genkey, gpgsm_import, gpgsm_keylist, gpgsm_keylist_ext, NULL, /* keylist_data */ NULL, /* keysign */ NULL, /* tofu_policy */ gpgsm_sign, NULL, /* trustlist */ gpgsm_verify, gpgsm_getauditlog, NULL, /* opassuan_transact */ NULL, /* conf_load */ NULL, /* conf_save */ NULL, /* conf_dir */ NULL, /* query_swdb */ gpgsm_set_io_cbs, gpgsm_io_event, gpgsm_cancel, NULL, /* cancel_op */ gpgsm_passwd, NULL, /* set_pinentry_mode */ NULL /* opspawn */ }; diff --git a/src/engine-uiserver.c b/src/engine-uiserver.c index fd5ac174..d8f4fce3 100644 --- a/src/engine-uiserver.c +++ b/src/engine-uiserver.c @@ -1,1402 +1,1455 @@ /* engine-uiserver.c - Uiserver engine. Copyright (C) 2000 Werner Koch (dd9jn) Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2009 g10 Code GmbH This file is part of GPGME. GPGME is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. GPGME is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* Peculiar: Use special keys from email address for recipient and signer (==sender). Use no data objects with encryption for prep_encrypt. */ #if HAVE_CONFIG_H #include #endif #include #include #ifdef HAVE_SYS_TYPES_H # include #endif #include #ifdef HAVE_UNISTD_H # include #endif #include #include /* FIXME */ #include #include "gpgme.h" #include "util.h" #include "ops.h" #include "wait.h" #include "priv-io.h" #include "sema.h" #include "data.h" #include "assuan.h" #include "debug.h" #include "engine-backend.h" typedef struct { int fd; /* FD we talk about. */ int server_fd;/* Server FD for this connection. */ int dir; /* Inbound/Outbound, maybe given implicit? */ void *data; /* Handler-specific data. */ void *tag; /* ID from the user for gpgme_remove_io_callback. */ char server_fd_str[15]; /* Same as SERVER_FD but as a string. We need this because _gpgme_io_fd2str can't be used on a closed descriptor. */ } iocb_data_t; struct engine_uiserver { assuan_context_t assuan_ctx; int lc_ctype_set; int lc_messages_set; gpgme_protocol_t protocol; iocb_data_t status_cb; /* Input, output etc are from the servers perspective. */ iocb_data_t input_cb; gpgme_data_t input_helper_data; /* Input helper data object. */ void *input_helper_memory; /* Input helper memory block. */ iocb_data_t output_cb; iocb_data_t message_cb; struct { engine_status_handler_t fnc; void *fnc_value; gpgme_status_cb_t mon_cb; void *mon_cb_value; } status; struct { engine_colon_line_handler_t fnc; void *fnc_value; struct { char *line; int linesize; int linelen; } attic; int any; /* any data line seen */ } colon; gpgme_data_t inline_data; /* Used to collect D lines. */ struct gpgme_io_cbs io_cbs; }; typedef struct engine_uiserver *engine_uiserver_t; static void uiserver_io_event (void *engine, gpgme_event_io_t type, void *type_data); static char * uiserver_get_version (const char *file_name) { (void)file_name; return NULL; } static const char * uiserver_get_req_version (void) { return NULL; } static void close_notify_handler (int fd, void *opaque) { engine_uiserver_t uiserver = opaque; assert (fd != -1); if (uiserver->status_cb.fd == fd) { if (uiserver->status_cb.tag) (*uiserver->io_cbs.remove) (uiserver->status_cb.tag); uiserver->status_cb.fd = -1; uiserver->status_cb.tag = NULL; } else if (uiserver->input_cb.fd == fd) { if (uiserver->input_cb.tag) (*uiserver->io_cbs.remove) (uiserver->input_cb.tag); uiserver->input_cb.fd = -1; uiserver->input_cb.tag = NULL; if (uiserver->input_helper_data) { gpgme_data_release (uiserver->input_helper_data); uiserver->input_helper_data = NULL; } if (uiserver->input_helper_memory) { free (uiserver->input_helper_memory); uiserver->input_helper_memory = NULL; } } else if (uiserver->output_cb.fd == fd) { if (uiserver->output_cb.tag) (*uiserver->io_cbs.remove) (uiserver->output_cb.tag); uiserver->output_cb.fd = -1; uiserver->output_cb.tag = NULL; } else if (uiserver->message_cb.fd == fd) { if (uiserver->message_cb.tag) (*uiserver->io_cbs.remove) (uiserver->message_cb.tag); uiserver->message_cb.fd = -1; uiserver->message_cb.tag = NULL; } } /* This is the default inquiry callback. We use it to handle the Pinentry notifications. */ static gpgme_error_t default_inq_cb (engine_uiserver_t uiserver, const char *line) { (void)uiserver; if (!strncmp (line, "PINENTRY_LAUNCHED", 17) && (line[17]==' '||!line[17])) { _gpgme_allow_set_foreground_window ((pid_t)strtoul (line+17, NULL, 10)); } return 0; } static gpgme_error_t uiserver_cancel (void *engine) { engine_uiserver_t uiserver = engine; if (!uiserver) return gpg_error (GPG_ERR_INV_VALUE); if (uiserver->status_cb.fd != -1) _gpgme_io_close (uiserver->status_cb.fd); if (uiserver->input_cb.fd != -1) _gpgme_io_close (uiserver->input_cb.fd); if (uiserver->output_cb.fd != -1) _gpgme_io_close (uiserver->output_cb.fd); if (uiserver->message_cb.fd != -1) _gpgme_io_close (uiserver->message_cb.fd); if (uiserver->assuan_ctx) { assuan_release (uiserver->assuan_ctx); uiserver->assuan_ctx = NULL; } return 0; } static void uiserver_release (void *engine) { engine_uiserver_t uiserver = engine; if (!uiserver) return; uiserver_cancel (engine); free (uiserver->colon.attic.line); free (uiserver); } static gpgme_error_t uiserver_new (void **engine, const char *file_name, const char *home_dir, const char *version) { gpgme_error_t err = 0; engine_uiserver_t uiserver; char *dft_display = NULL; char dft_ttyname[64]; char *env_tty = NULL; char *dft_ttytype = NULL; char *optstr; (void)home_dir; (void)version; /* Not yet used. */ uiserver = calloc (1, sizeof *uiserver); if (!uiserver) return gpg_error_from_syserror (); uiserver->protocol = GPGME_PROTOCOL_DEFAULT; uiserver->status_cb.fd = -1; uiserver->status_cb.dir = 1; uiserver->status_cb.tag = 0; uiserver->status_cb.data = uiserver; uiserver->input_cb.fd = -1; uiserver->input_cb.dir = 0; uiserver->input_cb.tag = 0; uiserver->input_cb.server_fd = -1; *uiserver->input_cb.server_fd_str = 0; uiserver->output_cb.fd = -1; uiserver->output_cb.dir = 1; uiserver->output_cb.tag = 0; uiserver->output_cb.server_fd = -1; *uiserver->output_cb.server_fd_str = 0; uiserver->message_cb.fd = -1; uiserver->message_cb.dir = 0; uiserver->message_cb.tag = 0; uiserver->message_cb.server_fd = -1; *uiserver->message_cb.server_fd_str = 0; uiserver->status.fnc = 0; uiserver->colon.fnc = 0; uiserver->colon.attic.line = 0; uiserver->colon.attic.linesize = 0; uiserver->colon.attic.linelen = 0; uiserver->colon.any = 0; uiserver->inline_data = NULL; uiserver->io_cbs.add = NULL; uiserver->io_cbs.add_priv = NULL; uiserver->io_cbs.remove = NULL; uiserver->io_cbs.event = NULL; uiserver->io_cbs.event_priv = NULL; err = assuan_new_ext (&uiserver->assuan_ctx, GPG_ERR_SOURCE_GPGME, &_gpgme_assuan_malloc_hooks, _gpgme_assuan_log_cb, NULL); if (err) goto leave; assuan_ctx_set_system_hooks (uiserver->assuan_ctx, &_gpgme_assuan_system_hooks); err = assuan_socket_connect (uiserver->assuan_ctx, file_name ? file_name : _gpgme_get_default_uisrv_socket (), 0, ASSUAN_SOCKET_SERVER_FDPASSING); if (err) goto leave; err = _gpgme_getenv ("DISPLAY", &dft_display); if (err) goto leave; if (dft_display) { if (gpgrt_asprintf (&optstr, "OPTION display=%s", dft_display) < 0) { err = gpg_error_from_syserror (); free (dft_display); goto leave; } free (dft_display); err = assuan_transact (uiserver->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; } err = _gpgme_getenv ("GPG_TTY", &env_tty); if (isatty (1) || env_tty || err) { int rc = 0; if (err) goto leave; else if (env_tty) { snprintf (dft_ttyname, sizeof (dft_ttyname), "%s", env_tty); free (env_tty); } else rc = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); /* Even though isatty() returns 1, ttyname_r() may fail in many ways, e.g., when /dev/pts is not accessible under chroot. */ if (!rc) { if (gpgrt_asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0) { err = gpg_error_from_syserror (); goto leave; } err = assuan_transact (uiserver->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; err = _gpgme_getenv ("TERM", &dft_ttytype); if (err) goto leave; if (dft_ttytype) { if (gpgrt_asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype)< 0) { err = gpg_error_from_syserror (); free (dft_ttytype); goto leave; } free (dft_ttytype); err = assuan_transact (uiserver->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; } } } #ifdef HAVE_W32_SYSTEM /* Under Windows we need to use AllowSetForegroundWindow. Tell uiserver to tell us when it needs it. */ if (!err) { err = assuan_transact (uiserver->assuan_ctx, "OPTION allow-pinentry-notify", NULL, NULL, NULL, NULL, NULL, NULL); if (gpg_err_code (err) == GPG_ERR_UNKNOWN_OPTION) err = 0; /* This is a new feature of uiserver. */ } #endif /*HAVE_W32_SYSTEM*/ leave: if (err) uiserver_release (uiserver); else *engine = uiserver; return err; } static gpgme_error_t uiserver_set_locale (void *engine, int category, const char *value) { engine_uiserver_t uiserver = engine; gpgme_error_t err; char *optstr; const char *catstr; /* FIXME: If value is NULL, we need to reset the option to default. But we can't do this. So we error out here. UISERVER needs support for this. */ if (category == LC_CTYPE) { catstr = "lc-ctype"; if (!value && uiserver->lc_ctype_set) return gpg_error (GPG_ERR_INV_VALUE); if (value) uiserver->lc_ctype_set = 1; } #ifdef LC_MESSAGES else if (category == LC_MESSAGES) { catstr = "lc-messages"; if (!value && uiserver->lc_messages_set) return gpg_error (GPG_ERR_INV_VALUE); if (value) uiserver->lc_messages_set = 1; } #endif /* LC_MESSAGES */ else return gpg_error (GPG_ERR_INV_VALUE); /* FIXME: Reset value to default. */ if (!value) return 0; if (gpgrt_asprintf (&optstr, "OPTION %s=%s", catstr, value) < 0) err = gpg_error_from_syserror (); else { err = assuan_transact (uiserver->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); } return err; } static gpgme_error_t uiserver_set_protocol (void *engine, gpgme_protocol_t protocol) { engine_uiserver_t uiserver = engine; if (protocol != GPGME_PROTOCOL_OpenPGP && protocol != GPGME_PROTOCOL_CMS && protocol != GPGME_PROTOCOL_DEFAULT) return gpg_error (GPG_ERR_INV_VALUE); uiserver->protocol = protocol; return 0; } static gpgme_error_t uiserver_assuan_simple_command (engine_uiserver_t uiserver, const char *cmd, engine_status_handler_t status_fnc, void *status_fnc_value) { assuan_context_t ctx = uiserver->assuan_ctx; gpg_error_t err; char *line; size_t linelen; err = assuan_write_line (ctx, cmd); if (err) return err; do { err = assuan_read_line (ctx, &line, &linelen); if (err) return err; if (*line == '#' || !linelen) continue; if (linelen >= 2 && line[0] == 'O' && line[1] == 'K' && (line[2] == '\0' || line[2] == ' ')) return 0; else if (linelen >= 4 && line[0] == 'E' && line[1] == 'R' && line[2] == 'R' && line[3] == ' ') err = atoi (&line[4]); else if (linelen >= 2 && line[0] == 'S' && line[1] == ' ') { char *rest; gpgme_status_code_t r; rest = strchr (line + 2, ' '); if (!rest) rest = line + linelen; /* set to an empty string */ else *(rest++) = 0; r = _gpgme_parse_status (line + 2); if (uiserver->status.mon_cb && r != GPGME_STATUS_PROGRESS) { /* Note that we call the monitor even if we do * not know the status code (r < 0). */ err = uiserver->status.mon_cb (uiserver->status.mon_cb_value, line + 2, rest); } if (err) ; else if (r >= 0 && status_fnc) err = status_fnc (status_fnc_value, r, rest); else err = gpg_error (GPG_ERR_GENERAL); } else err = gpg_error (GPG_ERR_GENERAL); } while (!err); return err; } typedef enum { INPUT_FD, OUTPUT_FD, MESSAGE_FD } fd_type_t; #define COMMANDLINELEN 40 static gpgme_error_t uiserver_set_fd (engine_uiserver_t uiserver, fd_type_t fd_type, const char *opt) { gpg_error_t err = 0; char line[COMMANDLINELEN]; const char *which; iocb_data_t *iocb_data; int dir; switch (fd_type) { case INPUT_FD: which = "INPUT"; iocb_data = &uiserver->input_cb; break; case OUTPUT_FD: which = "OUTPUT"; iocb_data = &uiserver->output_cb; break; case MESSAGE_FD: which = "MESSAGE"; iocb_data = &uiserver->message_cb; break; default: return gpg_error (GPG_ERR_INV_VALUE); } dir = iocb_data->dir; /* We try to short-cut the communication by giving UISERVER direct access to the file descriptor, rather than using a pipe. */ iocb_data->server_fd = _gpgme_data_get_fd (iocb_data->data); if (iocb_data->server_fd < 0) { int fds[2]; if (_gpgme_io_pipe (fds, 0) < 0) return gpg_error_from_syserror (); iocb_data->fd = dir ? fds[0] : fds[1]; iocb_data->server_fd = dir ? fds[1] : fds[0]; if (_gpgme_io_set_close_notify (iocb_data->fd, close_notify_handler, uiserver)) { err = gpg_error (GPG_ERR_GENERAL); goto leave_set_fd; } } err = assuan_sendfd (uiserver->assuan_ctx, iocb_data->server_fd); if (err) goto leave_set_fd; _gpgme_io_close (iocb_data->server_fd); iocb_data->server_fd = -1; if (opt) snprintf (line, COMMANDLINELEN, "%s FD %s", which, opt); else snprintf (line, COMMANDLINELEN, "%s FD", which); err = uiserver_assuan_simple_command (uiserver, line, NULL, NULL); leave_set_fd: if (err) { _gpgme_io_close (iocb_data->fd); iocb_data->fd = -1; if (iocb_data->server_fd != -1) { _gpgme_io_close (iocb_data->server_fd); iocb_data->server_fd = -1; } } return err; } static const char * map_data_enc (gpgme_data_t d) { switch (gpgme_data_get_encoding (d)) { case GPGME_DATA_ENCODING_NONE: break; case GPGME_DATA_ENCODING_BINARY: return "--binary"; case GPGME_DATA_ENCODING_BASE64: return "--base64"; case GPGME_DATA_ENCODING_ARMOR: return "--armor"; default: break; } return NULL; } static gpgme_error_t status_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; engine_uiserver_t uiserver = (engine_uiserver_t) data->handler_value; gpgme_error_t err = 0; char *line; size_t linelen; do { err = assuan_read_line (uiserver->assuan_ctx, &line, &linelen); if (err) { /* Try our best to terminate the connection friendly. */ /* assuan_write_line (uiserver->assuan_ctx, "BYE"); */ TRACE3 (DEBUG_CTX, "gpgme:status_handler", uiserver, "fd 0x%x: error from assuan (%d) getting status line : %s", fd, err, gpg_strerror (err)); } else if (linelen >= 3 && line[0] == 'E' && line[1] == 'R' && line[2] == 'R' && (line[3] == '\0' || line[3] == ' ')) { if (line[3] == ' ') err = atoi (&line[4]); if (! err) err = gpg_error (GPG_ERR_GENERAL); TRACE2 (DEBUG_CTX, "gpgme:status_handler", uiserver, "fd 0x%x: ERR line - mapped to: %s", fd, err ? gpg_strerror (err) : "ok"); /* Try our best to terminate the connection friendly. */ /* assuan_write_line (uiserver->assuan_ctx, "BYE"); */ } else if (linelen >= 2 && line[0] == 'O' && line[1] == 'K' && (line[2] == '\0' || line[2] == ' ')) { if (uiserver->status.fnc) { char emptystring[1] = {0}; err = uiserver->status.fnc (uiserver->status.fnc_value, GPGME_STATUS_EOF, emptystring); if (gpg_err_code (err) == GPG_ERR_FALSE) err = 0; /* Drop special error code. */ } if (!err && uiserver->colon.fnc && uiserver->colon.any) { /* We must tell a colon function about the EOF. We do this only when we have seen any data lines. Note that this inlined use of colon data lines will eventually be changed into using a regular data channel. */ uiserver->colon.any = 0; err = uiserver->colon.fnc (uiserver->colon.fnc_value, NULL); } TRACE2 (DEBUG_CTX, "gpgme:status_handler", uiserver, "fd 0x%x: OK line - final status: %s", fd, err ? gpg_strerror (err) : "ok"); _gpgme_io_close (uiserver->status_cb.fd); return err; } else if (linelen > 2 && line[0] == 'D' && line[1] == ' ' && uiserver->colon.fnc) { /* We are using the colon handler even for plain inline data - strange name for that function but for historic reasons we keep it. */ /* FIXME We can't use this for binary data because we assume this is a string. For the current usage of colon output it is correct. */ char *src = line + 2; char *end = line + linelen; char *dst; char **aline = &uiserver->colon.attic.line; int *alinelen = &uiserver->colon.attic.linelen; if (uiserver->colon.attic.linesize < *alinelen + linelen + 1) { char *newline = realloc (*aline, *alinelen + linelen + 1); if (!newline) err = gpg_error_from_syserror (); else { *aline = newline; uiserver->colon.attic.linesize = *alinelen + linelen + 1; } } if (!err) { dst = *aline + *alinelen; while (!err && src < end) { if (*src == '%' && src + 2 < end) { /* Handle escaped characters. */ ++src; *dst = _gpgme_hextobyte (src); (*alinelen)++; src += 2; } else { *dst = *src++; (*alinelen)++; } if (*dst == '\n') { /* Terminate the pending line, pass it to the colon handler and reset it. */ uiserver->colon.any = 1; if (*alinelen > 1 && *(dst - 1) == '\r') dst--; *dst = '\0'; /* FIXME How should we handle the return code? */ err = uiserver->colon.fnc (uiserver->colon.fnc_value, *aline); if (!err) { dst = *aline; *alinelen = 0; } } else dst++; } } TRACE2 (DEBUG_CTX, "gpgme:status_handler", uiserver, "fd 0x%x: D line; final status: %s", fd, err? gpg_strerror (err):"ok"); } else if (linelen > 2 && line[0] == 'D' && line[1] == ' ' && uiserver->inline_data) { char *src = line + 2; char *end = line + linelen; char *dst = src; gpgme_ssize_t nwritten; linelen = 0; while (src < end) { if (*src == '%' && src + 2 < end) { /* Handle escaped characters. */ ++src; *dst++ = _gpgme_hextobyte (src); src += 2; } else *dst++ = *src++; linelen++; } src = line + 2; while (linelen > 0) { nwritten = gpgme_data_write (uiserver->inline_data, src, linelen); if (!nwritten || (nwritten < 0 && errno != EINTR) || nwritten > linelen) { err = gpg_error_from_syserror (); break; } src += nwritten; linelen -= nwritten; } TRACE2 (DEBUG_CTX, "gpgme:status_handler", uiserver, "fd 0x%x: D inlinedata; final status: %s", fd, err? gpg_strerror (err):"ok"); } else if (linelen > 2 && line[0] == 'S' && line[1] == ' ') { char *rest; gpgme_status_code_t r; rest = strchr (line + 2, ' '); if (!rest) rest = line + linelen; /* set to an empty string */ else *(rest++) = 0; r = _gpgme_parse_status (line + 2); if (r >= 0) { if (uiserver->status.fnc) { err = uiserver->status.fnc (uiserver->status.fnc_value, r, rest); if (gpg_err_code (err) == GPG_ERR_FALSE) err = 0; /* Drop special error code. */ } } else fprintf (stderr, "[UNKNOWN STATUS]%s %s", line + 2, rest); TRACE3 (DEBUG_CTX, "gpgme:status_handler", uiserver, "fd 0x%x: S line (%s) - final status: %s", fd, line+2, err? gpg_strerror (err):"ok"); } else if (linelen >= 7 && line[0] == 'I' && line[1] == 'N' && line[2] == 'Q' && line[3] == 'U' && line[4] == 'I' && line[5] == 'R' && line[6] == 'E' && (line[7] == '\0' || line[7] == ' ')) { char *keyword = line+7; while (*keyword == ' ') keyword++;; default_inq_cb (uiserver, keyword); assuan_write_line (uiserver->assuan_ctx, "END"); } } while (!err && assuan_pending_line (uiserver->assuan_ctx)); return err; } static gpgme_error_t add_io_cb (engine_uiserver_t uiserver, iocb_data_t *iocbd, gpgme_io_cb_t handler) { gpgme_error_t err; TRACE_BEG2 (DEBUG_ENGINE, "engine-uiserver:add_io_cb", uiserver, "fd %d, dir %d", iocbd->fd, iocbd->dir); err = (*uiserver->io_cbs.add) (uiserver->io_cbs.add_priv, iocbd->fd, iocbd->dir, handler, iocbd->data, &iocbd->tag); if (err) return TRACE_ERR (err); if (!iocbd->dir) /* FIXME Kludge around poll() problem. */ err = _gpgme_io_set_nonblocking (iocbd->fd); return TRACE_ERR (err); } static gpgme_error_t start (engine_uiserver_t uiserver, const char *command) { gpgme_error_t err; int fdlist[5]; int nfds; /* We need to know the fd used by assuan for reads. We do this by using the assumption that the first returned fd from assuan_get_active_fds() is always this one. */ nfds = assuan_get_active_fds (uiserver->assuan_ctx, 0 /* read fds */, fdlist, DIM (fdlist)); if (nfds < 1) return gpg_error (GPG_ERR_GENERAL); /* FIXME */ /* We "duplicate" the file descriptor, so we can close it here (we can't close fdlist[0], as that is closed by libassuan, and closing it here might cause libassuan to close some unrelated FD later). Alternatively, we could special case status_fd and register/unregister it manually as needed, but this increases code duplication and is more complicated as we can not use the close notifications etc. A third alternative would be to let Assuan know that we closed the FD, but that complicates the Assuan interface. */ uiserver->status_cb.fd = _gpgme_io_dup (fdlist[0]); if (uiserver->status_cb.fd < 0) return gpg_error_from_syserror (); if (_gpgme_io_set_close_notify (uiserver->status_cb.fd, close_notify_handler, uiserver)) { _gpgme_io_close (uiserver->status_cb.fd); uiserver->status_cb.fd = -1; return gpg_error (GPG_ERR_GENERAL); } err = add_io_cb (uiserver, &uiserver->status_cb, status_handler); if (!err && uiserver->input_cb.fd != -1) err = add_io_cb (uiserver, &uiserver->input_cb, _gpgme_data_outbound_handler); if (!err && uiserver->output_cb.fd != -1) err = add_io_cb (uiserver, &uiserver->output_cb, _gpgme_data_inbound_handler); if (!err && uiserver->message_cb.fd != -1) err = add_io_cb (uiserver, &uiserver->message_cb, _gpgme_data_outbound_handler); if (!err) err = assuan_write_line (uiserver->assuan_ctx, command); if (!err) uiserver_io_event (uiserver, GPGME_EVENT_START, NULL); return err; } static gpgme_error_t uiserver_reset (void *engine) { engine_uiserver_t uiserver = engine; /* We must send a reset because we need to reset the list of signers. Note that RESET does not reset OPTION commands. */ return uiserver_assuan_simple_command (uiserver, "RESET", NULL, NULL); } static gpgme_error_t uiserver_decrypt (void *engine, gpgme_decrypt_flags_t flags, gpgme_data_t ciph, gpgme_data_t plain, int export_session_key, const char *override_session_key, int auto_key_retrieve) { engine_uiserver_t uiserver = engine; gpgme_error_t err; const char *protocol; char *cmd; int verify = !!(flags & GPGME_DECRYPT_VERIFY); (void)override_session_key; /* Fixme: We need to see now to add this * to the UI server protocol */ (void)auto_key_retrieve; /* Not yet supported. */ if (!uiserver) return gpg_error (GPG_ERR_INV_VALUE); if (uiserver->protocol == GPGME_PROTOCOL_DEFAULT) protocol = ""; else if (uiserver->protocol == GPGME_PROTOCOL_OpenPGP) protocol = " --protocol=OpenPGP"; else if (uiserver->protocol == GPGME_PROTOCOL_CMS) protocol = " --protocol=CMS"; else return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL); if (gpgrt_asprintf (&cmd, "DECRYPT%s%s%s", protocol, verify ? "" : " --no-verify", export_session_key ? " --export-session-key" : "") < 0) return gpg_error_from_syserror (); uiserver->input_cb.data = ciph; err = uiserver_set_fd (uiserver, INPUT_FD, map_data_enc (uiserver->input_cb.data)); if (err) { gpgrt_free (cmd); return gpg_error (GPG_ERR_GENERAL); /* FIXME */ } uiserver->output_cb.data = plain; err = uiserver_set_fd (uiserver, OUTPUT_FD, 0); if (err) { gpgrt_free (cmd); return gpg_error (GPG_ERR_GENERAL); /* FIXME */ } uiserver->inline_data = NULL; err = start (engine, cmd); gpgrt_free (cmd); return err; } static gpgme_error_t set_recipients (engine_uiserver_t uiserver, gpgme_key_t recp[]) { gpgme_error_t err = 0; char *line; int linelen; int invalid_recipients = 0; int i; linelen = 10 + 40 + 1; /* "RECIPIENT " + guess + '\0'. */ line = malloc (10 + 40 + 1); if (!line) return gpg_error_from_syserror (); strcpy (line, "RECIPIENT "); for (i=0; !err && recp[i]; i++) { char *uid; int newlen; /* We use only the first user ID of the key. */ if (!recp[i]->uids || !(uid=recp[i]->uids->uid) || !*uid) { invalid_recipients++; continue; } newlen = 11 + strlen (uid); if (linelen < newlen) { char *newline = realloc (line, newlen); if (! newline) { int saved_err = gpg_error_from_syserror (); free (line); return saved_err; } line = newline; linelen = newlen; } /* FIXME: need to do proper escaping */ strcpy (&line[10], uid); err = uiserver_assuan_simple_command (uiserver, line, uiserver->status.fnc, uiserver->status.fnc_value); /* FIXME: This might requires more work. */ if (gpg_err_code (err) == GPG_ERR_NO_PUBKEY) invalid_recipients++; else if (err) { free (line); return err; } } free (line); return gpg_error (invalid_recipients ? GPG_ERR_UNUSABLE_PUBKEY : GPG_ERR_NO_ERROR); } +/* Take recipients from the LF delimited STRING and send RECIPIENT + * commands to gpgsm. */ static gpgme_error_t -uiserver_encrypt (void *engine, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, +set_recipients_from_string (engine_uiserver_t uiserver, const char *string) +{ + gpg_error_t err = 0; + char *line = NULL; + int no_pubkey = 0; + const char *s; + int n; + + for (;;) + { + while (*string == ' ' || *string == '\t') + string++; + if (!*string) + break; + + s = strchr (string, '\n'); + if (s) + n = s - string; + else + n = strlen (string); + while (n && (string[n-1] == ' ' || string[n-1] == '\t')) + n--; + + gpgrt_free (line); + if (gpgrt_asprintf (&line, "RECIPIENT %.*s", n, string) < 0) + { + err = gpg_error_from_syserror (); + break; + } + string += n + !!s; + + err = uiserver_assuan_simple_command (uiserver, line, + uiserver->status.fnc, + uiserver->status.fnc_value); + + /* Fixme: Improve error reporting. */ + if (gpg_err_code (err) == GPG_ERR_NO_PUBKEY) + no_pubkey++; + else if (err) + break; + } + gpgrt_free (line); + return err? err : no_pubkey? gpg_error (GPG_ERR_NO_PUBKEY) : 0; +} + + +static gpgme_error_t +uiserver_encrypt (void *engine, gpgme_key_t recp[], const char *recpstring, + gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor) { engine_uiserver_t uiserver = engine; gpgme_error_t err; const char *protocol; char *cmd; if (!uiserver) return gpg_error (GPG_ERR_INV_VALUE); if (uiserver->protocol == GPGME_PROTOCOL_DEFAULT) protocol = ""; else if (uiserver->protocol == GPGME_PROTOCOL_OpenPGP) protocol = " --protocol=OpenPGP"; else if (uiserver->protocol == GPGME_PROTOCOL_CMS) protocol = " --protocol=CMS"; else return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL); if (flags & GPGME_ENCRYPT_PREPARE) { if (!recp || plain || ciph) return gpg_error (GPG_ERR_INV_VALUE); if (gpgrt_asprintf (&cmd, "PREP_ENCRYPT%s%s", protocol, (flags & GPGME_ENCRYPT_EXPECT_SIGN) ? " --expect-sign" : "") < 0) return gpg_error_from_syserror (); } else { if (!plain || !ciph) return gpg_error (GPG_ERR_INV_VALUE); if (gpgrt_asprintf (&cmd, "ENCRYPT%s", protocol) < 0) return gpg_error_from_syserror (); } if (plain) { uiserver->input_cb.data = plain; err = uiserver_set_fd (uiserver, INPUT_FD, map_data_enc (uiserver->input_cb.data)); if (err) { gpgrt_free (cmd); return err; } } if (ciph) { uiserver->output_cb.data = ciph; err = uiserver_set_fd (uiserver, OUTPUT_FD, use_armor ? "--armor" : map_data_enc (uiserver->output_cb.data)); if (err) { gpgrt_free (cmd); return err; } } uiserver->inline_data = NULL; - if (recp) + if (recp || recpstring) { - err = set_recipients (uiserver, recp); + if (recp) + err = set_recipients (uiserver, recp); + else + err = set_recipients_from_string (uiserver, recpstring); if (err) { gpgrt_free (cmd); return err; } } err = start (uiserver, cmd); gpgrt_free (cmd); return err; } static gpgme_error_t uiserver_sign (void *engine, gpgme_data_t in, gpgme_data_t out, gpgme_sig_mode_t mode, int use_armor, int use_textmode, int include_certs, gpgme_ctx_t ctx /* FIXME */) { engine_uiserver_t uiserver = engine; gpgme_error_t err = 0; const char *protocol; char *cmd; gpgme_key_t key; (void)use_textmode; (void)include_certs; if (!uiserver || !in || !out) return gpg_error (GPG_ERR_INV_VALUE); if (uiserver->protocol == GPGME_PROTOCOL_DEFAULT) protocol = ""; else if (uiserver->protocol == GPGME_PROTOCOL_OpenPGP) protocol = " --protocol=OpenPGP"; else if (uiserver->protocol == GPGME_PROTOCOL_CMS) protocol = " --protocol=CMS"; else return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL); if (gpgrt_asprintf (&cmd, "SIGN%s%s", protocol, (mode == GPGME_SIG_MODE_DETACH) ? " --detached" : "") < 0) return gpg_error_from_syserror (); key = gpgme_signers_enum (ctx, 0); if (key) { const char *s = NULL; if (key && key->uids) s = key->uids->email; if (s && strlen (s) < 80) { char buf[100]; strcpy (stpcpy (buf, "SENDER --info "), s); err = uiserver_assuan_simple_command (uiserver, buf, uiserver->status.fnc, uiserver->status.fnc_value); } else err = gpg_error (GPG_ERR_INV_VALUE); gpgme_key_unref (key); if (err) { gpgrt_free (cmd); return err; } } uiserver->input_cb.data = in; err = uiserver_set_fd (uiserver, INPUT_FD, map_data_enc (uiserver->input_cb.data)); if (err) { gpgrt_free (cmd); return err; } uiserver->output_cb.data = out; err = uiserver_set_fd (uiserver, OUTPUT_FD, use_armor ? "--armor" : map_data_enc (uiserver->output_cb.data)); if (err) { gpgrt_free (cmd); return err; } uiserver->inline_data = NULL; err = start (uiserver, cmd); gpgrt_free (cmd); return err; } /* FIXME: Missing a way to specify --silent. */ static gpgme_error_t uiserver_verify (void *engine, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext, gpgme_ctx_t ctx) { engine_uiserver_t uiserver = engine; gpgme_error_t err; const char *protocol; char *cmd; (void)ctx; /* FIXME: We should to add a --sender option to the * UISever protocol. */ if (!uiserver) return gpg_error (GPG_ERR_INV_VALUE); if (uiserver->protocol == GPGME_PROTOCOL_DEFAULT) protocol = ""; else if (uiserver->protocol == GPGME_PROTOCOL_OpenPGP) protocol = " --protocol=OpenPGP"; else if (uiserver->protocol == GPGME_PROTOCOL_CMS) protocol = " --protocol=CMS"; else return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL); if (gpgrt_asprintf (&cmd, "VERIFY%s", protocol) < 0) return gpg_error_from_syserror (); uiserver->input_cb.data = sig; err = uiserver_set_fd (uiserver, INPUT_FD, map_data_enc (uiserver->input_cb.data)); if (err) { gpgrt_free (cmd); return err; } if (plaintext) { /* Normal or cleartext signature. */ uiserver->output_cb.data = plaintext; err = uiserver_set_fd (uiserver, OUTPUT_FD, 0); } else { /* Detached signature. */ uiserver->message_cb.data = signed_text; err = uiserver_set_fd (uiserver, MESSAGE_FD, 0); } uiserver->inline_data = NULL; if (!err) err = start (uiserver, cmd); gpgrt_free (cmd); return err; } /* This sets a status callback for monitoring status lines before they * are passed to a caller set handler. */ static void uiserver_set_status_cb (void *engine, gpgme_status_cb_t cb, void *cb_value) { engine_uiserver_t uiserver = engine; uiserver->status.mon_cb = cb; uiserver->status.mon_cb_value = cb_value; } static void uiserver_set_status_handler (void *engine, engine_status_handler_t fnc, void *fnc_value) { engine_uiserver_t uiserver = engine; uiserver->status.fnc = fnc; uiserver->status.fnc_value = fnc_value; } static gpgme_error_t uiserver_set_colon_line_handler (void *engine, engine_colon_line_handler_t fnc, void *fnc_value) { engine_uiserver_t uiserver = engine; uiserver->colon.fnc = fnc; uiserver->colon.fnc_value = fnc_value; uiserver->colon.any = 0; return 0; } static void uiserver_set_io_cbs (void *engine, gpgme_io_cbs_t io_cbs) { engine_uiserver_t uiserver = engine; uiserver->io_cbs = *io_cbs; } static void uiserver_io_event (void *engine, gpgme_event_io_t type, void *type_data) { engine_uiserver_t uiserver = engine; TRACE3 (DEBUG_ENGINE, "gpgme:uiserver_io_event", uiserver, "event %p, type %d, type_data %p", uiserver->io_cbs.event, type, type_data); if (uiserver->io_cbs.event) (*uiserver->io_cbs.event) (uiserver->io_cbs.event_priv, type, type_data); } struct engine_ops _gpgme_engine_ops_uiserver = { /* Static functions. */ _gpgme_get_default_uisrv_socket, NULL, uiserver_get_version, uiserver_get_req_version, uiserver_new, /* Member functions. */ uiserver_release, uiserver_reset, uiserver_set_status_cb, uiserver_set_status_handler, NULL, /* set_command_handler */ uiserver_set_colon_line_handler, uiserver_set_locale, uiserver_set_protocol, NULL, /* set_engine_flags */ uiserver_decrypt, NULL, /* delete */ NULL, /* edit */ uiserver_encrypt, NULL, /* encrypt_sign */ NULL, /* export */ NULL, /* export_ext */ NULL, /* genkey */ NULL, /* import */ NULL, /* keylist */ NULL, /* keylist_ext */ NULL, /* keylist_data */ NULL, /* keysign */ NULL, /* tofu_policy */ uiserver_sign, NULL, /* trustlist */ uiserver_verify, NULL, /* getauditlog */ NULL, /* opassuan_transact */ NULL, /* conf_load */ NULL, /* conf_save */ NULL, /* conf_dir */ NULL, /* query_swdb */ uiserver_set_io_cbs, uiserver_io_event, uiserver_cancel, NULL, /* cancel_op */ NULL, /* passwd */ NULL, /* set_pinentry_mode */ NULL /* opspawn */ }; diff --git a/src/engine.c b/src/engine.c index e51384f0..b716ca24 100644 --- a/src/engine.c +++ b/src/engine.c @@ -1,1129 +1,1131 @@ /* engine.c - GPGME engine support. Copyright (C) 2000 Werner Koch (dd9jn) Copyright (C) 2001, 2002, 2003, 2004, 2006, 2009, 2010 g10 Code GmbH This file is part of GPGME. GPGME is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. GPGME is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, see . */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "gpgme.h" #include "util.h" #include "sema.h" #include "ops.h" #include "debug.h" #include "engine.h" #include "engine-backend.h" struct engine { struct engine_ops *ops; void *engine; }; static struct engine_ops *engine_ops[] = { &_gpgme_engine_ops_gpg, /* OpenPGP. */ &_gpgme_engine_ops_gpgsm, /* CMS. */ &_gpgme_engine_ops_gpgconf, /* gpg-conf. */ &_gpgme_engine_ops_assuan, /* Low-Level Assuan. */ &_gpgme_engine_ops_g13, /* Crypto VFS. */ #ifdef ENABLE_UISERVER &_gpgme_engine_ops_uiserver, /* UI-Server. */ #else NULL, #endif &_gpgme_engine_ops_spawn }; /* The engine info. */ static gpgme_engine_info_t engine_info; DEFINE_STATIC_LOCK (engine_info_lock); /* If non-NULL, the minimal version required for all engines. */ static char *engine_minimal_version; /* Get the file name of the engine for PROTOCOL. */ static const char * engine_get_file_name (gpgme_protocol_t proto) { if (proto > DIM (engine_ops)) return NULL; if (engine_ops[proto] && engine_ops[proto]->get_file_name) return (*engine_ops[proto]->get_file_name) (); else return NULL; } /* Get the standard home dir of the engine for PROTOCOL. */ static const char * engine_get_home_dir (gpgme_protocol_t proto) { if (proto > DIM (engine_ops)) return NULL; if (engine_ops[proto] && engine_ops[proto]->get_home_dir) return (*engine_ops[proto]->get_home_dir) (); else return NULL; } /* Get a malloced string containing the version number of the engine * for PROTOCOL. If this function returns NULL for a valid protocol, * it should be assumed that the engine is a pseudo engine. */ static char * engine_get_version (gpgme_protocol_t proto, const char *file_name) { if (proto > DIM (engine_ops)) return NULL; if (engine_ops[proto] && engine_ops[proto]->get_version) return (*engine_ops[proto]->get_version) (file_name); else return NULL; } /* Get the required version number of the engine for PROTOCOL. This * may be NULL. */ static const char * engine_get_req_version (gpgme_protocol_t proto) { if (proto > DIM (engine_ops)) return NULL; if (engine_ops[proto] && engine_ops[proto]->get_req_version) return (*engine_ops[proto]->get_req_version) (); else return NULL; } /* Verify the version requirement for the engine for PROTOCOL. */ gpgme_error_t gpgme_engine_check_version (gpgme_protocol_t proto) { gpgme_error_t err; gpgme_engine_info_t info; int result; LOCK (engine_info_lock); info = engine_info; if (!info) { /* Make sure it is initialized. */ UNLOCK (engine_info_lock); err = gpgme_get_engine_info (&info); if (err) return err; LOCK (engine_info_lock); } while (info && info->protocol != proto) info = info->next; if (!info) result = 0; else result = _gpgme_compare_versions (info->version, info->req_version); UNLOCK (engine_info_lock); return result ? 0 : trace_gpg_error (GPG_ERR_INV_ENGINE); } /* Release the engine info INFO. */ void _gpgme_engine_info_release (gpgme_engine_info_t info) { while (info) { gpgme_engine_info_t next_info = info->next; if (info->file_name) free (info->file_name); if (info->home_dir) free (info->home_dir); if (info->version) free (info->version); free (info); info = next_info; } } /* This is an internal function to set a mimimal required version. * This function must only be called by gpgme_set_global_flag. * Returns 0 on success. */ int _gpgme_set_engine_minimal_version (const char *value) { free (engine_minimal_version); if (value) { engine_minimal_version = strdup (value); return !engine_minimal_version; } else { engine_minimal_version = NULL; return 0; } } /* Get the information about the configured and installed engines. A pointer to the first engine in the statically allocated linked list is returned in *INFO. If an error occurs, it is returned. The returned data is valid until the next gpgme_set_engine_info. */ gpgme_error_t gpgme_get_engine_info (gpgme_engine_info_t *info) { gpgme_error_t err; LOCK (engine_info_lock); if (!engine_info) { gpgme_engine_info_t *lastp = &engine_info; gpgme_protocol_t proto_list[] = { GPGME_PROTOCOL_OpenPGP, GPGME_PROTOCOL_CMS, GPGME_PROTOCOL_GPGCONF, GPGME_PROTOCOL_ASSUAN, GPGME_PROTOCOL_G13, GPGME_PROTOCOL_UISERVER, GPGME_PROTOCOL_SPAWN }; unsigned int proto; err = 0; for (proto = 0; proto < DIM (proto_list); proto++) { const char *ofile_name = engine_get_file_name (proto_list[proto]); const char *ohome_dir = engine_get_home_dir (proto_list[proto]); char *version = engine_get_version (proto_list[proto], NULL); char *file_name; char *home_dir; if (!ofile_name) continue; file_name = strdup (ofile_name); if (!file_name) err = gpg_error_from_syserror (); if (ohome_dir) { home_dir = strdup (ohome_dir); if (!home_dir && !err) err = gpg_error_from_syserror (); } else home_dir = NULL; *lastp = calloc (1, sizeof (*engine_info)); if (!*lastp && !err) err = gpg_error_from_syserror (); /* Check against the optional minimal engine version. */ if (!err && version && engine_minimal_version && !_gpgme_compare_versions (version, engine_minimal_version)) { #if GPG_ERROR_VERSION_NUMBER < 0x011900 /* 1.25 */ err = gpg_error (GPG_ERR_NO_ENGINE); #else err = gpg_error (GPG_ERR_ENGINE_TOO_OLD); #endif } /* Now set the dummy version for pseudo engines. */ if (!err && !version) { version = strdup ("1.0.0"); if (!version) err = gpg_error_from_syserror (); } if (err) { _gpgme_engine_info_release (engine_info); engine_info = NULL; if (file_name) free (file_name); if (home_dir) free (home_dir); if (version) free (version); UNLOCK (engine_info_lock); return err; } (*lastp)->protocol = proto_list[proto]; (*lastp)->file_name = file_name; (*lastp)->home_dir = home_dir; (*lastp)->version = version; (*lastp)->req_version = engine_get_req_version (proto_list[proto]); if (!(*lastp)->req_version) (*lastp)->req_version = "1.0.0"; /* Dummy for pseudo engines. */ (*lastp)->next = NULL; lastp = &(*lastp)->next; } } *info = engine_info; UNLOCK (engine_info_lock); return 0; } /* Get a deep copy of the engine info and return it in INFO. */ gpgme_error_t _gpgme_engine_info_copy (gpgme_engine_info_t *r_info) { gpgme_error_t err = 0; gpgme_engine_info_t info; gpgme_engine_info_t new_info; gpgme_engine_info_t *lastp; LOCK (engine_info_lock); info = engine_info; if (!info) { /* Make sure it is initialized. */ UNLOCK (engine_info_lock); err = gpgme_get_engine_info (&info); if (err) return err; LOCK (engine_info_lock); } new_info = NULL; lastp = &new_info; while (info) { char *file_name; char *home_dir; char *version; assert (info->file_name); file_name = strdup (info->file_name); if (!file_name) err = gpg_error_from_syserror (); if (info->home_dir) { home_dir = strdup (info->home_dir); if (!home_dir && !err) err = gpg_error_from_syserror (); } else home_dir = NULL; if (info->version) { version = strdup (info->version); if (!version && !err) err = gpg_error_from_syserror (); } else version = NULL; *lastp = malloc (sizeof (*engine_info)); if (!*lastp && !err) err = gpg_error_from_syserror (); if (err) { _gpgme_engine_info_release (new_info); if (file_name) free (file_name); if (home_dir) free (home_dir); if (version) free (version); UNLOCK (engine_info_lock); return err; } (*lastp)->protocol = info->protocol; (*lastp)->file_name = file_name; (*lastp)->home_dir = home_dir; (*lastp)->version = version; (*lastp)->req_version = info->req_version; (*lastp)->next = NULL; lastp = &(*lastp)->next; info = info->next; } *r_info = new_info; UNLOCK (engine_info_lock); return 0; } /* Set the engine info for the info list INFO, protocol PROTO, to the file name FILE_NAME and the home directory HOME_DIR. */ gpgme_error_t _gpgme_set_engine_info (gpgme_engine_info_t info, gpgme_protocol_t proto, const char *file_name, const char *home_dir) { char *new_file_name; char *new_home_dir; char *new_version; /* FIXME: Use some PROTO_MAX definition. */ if (proto > DIM (engine_ops)) return gpg_error (GPG_ERR_INV_VALUE); while (info && info->protocol != proto) info = info->next; if (!info) return trace_gpg_error (GPG_ERR_INV_ENGINE); /* Prepare new members. */ if (file_name) new_file_name = strdup (file_name); else { const char *ofile_name = engine_get_file_name (proto); assert (ofile_name); new_file_name = strdup (ofile_name); } if (!new_file_name) return gpg_error_from_syserror (); if (home_dir) { new_home_dir = strdup (home_dir); if (!new_home_dir) { free (new_file_name); return gpg_error_from_syserror (); } } else { const char *ohome_dir = engine_get_home_dir (proto); if (ohome_dir) { new_home_dir = strdup (ohome_dir); if (!new_home_dir) { free (new_file_name); return gpg_error_from_syserror (); } } else new_home_dir = NULL; } new_version = engine_get_version (proto, new_file_name); if (!new_version) { new_version = strdup ("1.0.0"); /* Fake one for dummy entries. */ if (!new_version) { free (new_file_name); free (new_home_dir); } } /* Remove the old members. */ assert (info->file_name); free (info->file_name); if (info->home_dir) free (info->home_dir); if (info->version) free (info->version); /* Install the new members. */ info->file_name = new_file_name; info->home_dir = new_home_dir; info->version = new_version; return 0; } /* Set the default engine info for the protocol PROTO to the file name FILE_NAME and the home directory HOME_DIR. */ gpgme_error_t gpgme_set_engine_info (gpgme_protocol_t proto, const char *file_name, const char *home_dir) { gpgme_error_t err; gpgme_engine_info_t info; LOCK (engine_info_lock); info = engine_info; if (!info) { /* Make sure it is initialized. */ UNLOCK (engine_info_lock); err = gpgme_get_engine_info (&info); if (err) return err; LOCK (engine_info_lock); } err = _gpgme_set_engine_info (info, proto, file_name, home_dir); UNLOCK (engine_info_lock); return err; } gpgme_error_t _gpgme_engine_new (gpgme_engine_info_t info, engine_t *r_engine) { engine_t engine; if (!info->file_name || !info->version) return trace_gpg_error (GPG_ERR_INV_ENGINE); engine = calloc (1, sizeof *engine); if (!engine) return gpg_error_from_syserror (); engine->ops = engine_ops[info->protocol]; if (engine->ops->new) { gpgme_error_t err; err = (*engine->ops->new) (&engine->engine, info->file_name, info->home_dir, info->version); if (err) { free (engine); return err; } } else engine->engine = NULL; *r_engine = engine; return 0; } gpgme_error_t _gpgme_engine_reset (engine_t engine) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->reset) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->reset) (engine->engine); } void _gpgme_engine_release (engine_t engine) { if (!engine) return; if (engine->ops->release) (*engine->ops->release) (engine->engine); free (engine); } /* Set a status callback which is used to monitor the status values * before they are passed to a handler set with * _gpgme_engine_set_status_handler. */ void _gpgme_engine_set_status_cb (engine_t engine, gpgme_status_cb_t cb, void *cb_value) { if (!engine) return; if (engine->ops->set_status_cb) (*engine->ops->set_status_cb) (engine->engine, cb, cb_value); } void _gpgme_engine_set_status_handler (engine_t engine, engine_status_handler_t fnc, void *fnc_value) { if (!engine) return; if (engine->ops->set_status_handler) (*engine->ops->set_status_handler) (engine->engine, fnc, fnc_value); } gpgme_error_t _gpgme_engine_set_command_handler (engine_t engine, engine_command_handler_t fnc, void *fnc_value, gpgme_data_t linked_data) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->set_command_handler) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->set_command_handler) (engine->engine, fnc, fnc_value, linked_data); } gpgme_error_t _gpgme_engine_set_colon_line_handler (engine_t engine, engine_colon_line_handler_t fnc, void *fnc_value) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->set_colon_line_handler) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->set_colon_line_handler) (engine->engine, fnc, fnc_value); } gpgme_error_t _gpgme_engine_set_locale (engine_t engine, int category, const char *value) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->set_locale) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->set_locale) (engine->engine, category, value); } gpgme_error_t _gpgme_engine_set_protocol (engine_t engine, gpgme_protocol_t protocol) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->set_protocol) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->set_protocol) (engine->engine, protocol); } /* Pass information about the current context to the engine. The * engine may use this context to retrieve context specific flags. * Important: The engine is required to immediately copy the required * flags to its own context! * * This function will eventually be used to reduce the number of * explicit passed flags. */ void _gpgme_engine_set_engine_flags (engine_t engine, gpgme_ctx_t ctx) { if (!engine) return; if (!engine->ops->set_engine_flags) return; (*engine->ops->set_engine_flags) (engine->engine, ctx); } gpgme_error_t _gpgme_engine_op_decrypt (engine_t engine, gpgme_decrypt_flags_t flags, gpgme_data_t ciph, gpgme_data_t plain, int export_session_key, const char *override_session_key, int auto_key_retrieve) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->decrypt) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->decrypt) (engine->engine, flags, ciph, plain, export_session_key, override_session_key, auto_key_retrieve); } gpgme_error_t _gpgme_engine_op_delete (engine_t engine, gpgme_key_t key, unsigned int flags) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->delete) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->delete) (engine->engine, key, flags); } gpgme_error_t _gpgme_engine_op_edit (engine_t engine, int type, gpgme_key_t key, gpgme_data_t out, gpgme_ctx_t ctx /* FIXME */) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->edit) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->edit) (engine->engine, type, key, out, ctx); } gpgme_error_t _gpgme_engine_op_encrypt (engine_t engine, gpgme_key_t recp[], + const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->encrypt) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); - return (*engine->ops->encrypt) (engine->engine, recp, flags, plain, ciph, - use_armor); + return (*engine->ops->encrypt) (engine->engine, recp, recpstring, + flags, plain, ciph, use_armor); } gpgme_error_t _gpgme_engine_op_encrypt_sign (engine_t engine, gpgme_key_t recp[], + const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor, gpgme_ctx_t ctx /* FIXME */) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->encrypt_sign) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); - return (*engine->ops->encrypt_sign) (engine->engine, recp, flags, - plain, ciph, use_armor, ctx); + return (*engine->ops->encrypt_sign) (engine->engine, recp, recpstring, + flags, plain, ciph, use_armor, ctx); } gpgme_error_t _gpgme_engine_op_export (engine_t engine, const char *pattern, gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->export) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->export) (engine->engine, pattern, mode, keydata, use_armor); } gpgme_error_t _gpgme_engine_op_export_ext (engine_t engine, const char *pattern[], unsigned int reserved, gpgme_data_t keydata, int use_armor) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->export_ext) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->export_ext) (engine->engine, pattern, reserved, keydata, use_armor); } gpgme_error_t _gpgme_engine_op_genkey (engine_t engine, const char *userid, const char *algo, unsigned long reserved, unsigned long expires, gpgme_key_t key, unsigned int flags, gpgme_data_t help_data, unsigned int extraflags, gpgme_data_t pubkey, gpgme_data_t seckey) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->genkey) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->genkey) (engine->engine, userid, algo, reserved, expires, key, flags, help_data, extraflags, pubkey, seckey); } gpgme_error_t _gpgme_engine_op_keysign (engine_t engine, gpgme_key_t key, const char *userid, unsigned long expires, unsigned int flags, gpgme_ctx_t ctx) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->keysign) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->keysign) (engine->engine, key, userid, expires, flags, ctx); } gpgme_error_t _gpgme_engine_op_tofu_policy (engine_t engine, gpgme_key_t key, gpgme_tofu_policy_t policy) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->tofu_policy) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->tofu_policy) (engine->engine, key, policy); } gpgme_error_t _gpgme_engine_op_import (engine_t engine, gpgme_data_t keydata, gpgme_key_t *keyarray) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->import) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->import) (engine->engine, keydata, keyarray); } gpgme_error_t _gpgme_engine_op_keylist (engine_t engine, const char *pattern, int secret_only, gpgme_keylist_mode_t mode, int engine_flags) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->keylist) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->keylist) (engine->engine, pattern, secret_only, mode, engine_flags); } gpgme_error_t _gpgme_engine_op_keylist_ext (engine_t engine, const char *pattern[], int secret_only, int reserved, gpgme_keylist_mode_t mode, int engine_flags) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->keylist_ext) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->keylist_ext) (engine->engine, pattern, secret_only, reserved, mode, engine_flags); } gpgme_error_t _gpgme_engine_op_keylist_data (engine_t engine, gpgme_data_t data) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->keylist_data) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->keylist_data) (engine->engine, data); } gpgme_error_t _gpgme_engine_op_sign (engine_t engine, gpgme_data_t in, gpgme_data_t out, gpgme_sig_mode_t mode, int use_armor, int use_textmode, int include_certs, gpgme_ctx_t ctx /* FIXME */) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->sign) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->sign) (engine->engine, in, out, mode, use_armor, use_textmode, include_certs, ctx); } gpgme_error_t _gpgme_engine_op_trustlist (engine_t engine, const char *pattern) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->trustlist) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->trustlist) (engine->engine, pattern); } gpgme_error_t _gpgme_engine_op_verify (engine_t engine, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext, gpgme_ctx_t ctx) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->verify) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->verify) (engine->engine, sig, signed_text, plaintext, ctx); } gpgme_error_t _gpgme_engine_op_getauditlog (engine_t engine, gpgme_data_t output, unsigned int flags) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->getauditlog) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->getauditlog) (engine->engine, output, flags); } gpgme_error_t _gpgme_engine_op_assuan_transact (engine_t engine, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->opassuan_transact) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->opassuan_transact) (engine->engine, command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value); } gpgme_error_t _gpgme_engine_op_conf_load (engine_t engine, gpgme_conf_comp_t *conf_p) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->conf_load) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->conf_load) (engine->engine, conf_p); } gpgme_error_t _gpgme_engine_op_conf_save (engine_t engine, gpgme_conf_comp_t conf) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->conf_save) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->conf_save) (engine->engine, conf); } gpgme_error_t _gpgme_engine_op_conf_dir (engine_t engine, const char *what, char **result) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->conf_dir) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->conf_dir) (engine->engine, what, result); } gpgme_error_t _gpgme_engine_op_query_swdb (engine_t engine, const char *name, const char *iversion, gpgme_query_swdb_result_t result) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->query_swdb) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->query_swdb) (engine->engine, name, iversion, result); } void _gpgme_engine_set_io_cbs (engine_t engine, gpgme_io_cbs_t io_cbs) { if (!engine) return; (*engine->ops->set_io_cbs) (engine->engine, io_cbs); } void _gpgme_engine_io_event (engine_t engine, gpgme_event_io_t type, void *type_data) { if (!engine) return; (*engine->ops->io_event) (engine->engine, type, type_data); } /* Cancel the session and the pending operation if any. */ gpgme_error_t _gpgme_engine_cancel (engine_t engine) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->cancel) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->cancel) (engine->engine); } /* Cancel the pending operation, but not the complete session. */ gpgme_error_t _gpgme_engine_cancel_op (engine_t engine) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->cancel_op) return 0; return (*engine->ops->cancel_op) (engine->engine); } /* Change the passphrase for KEY. */ gpgme_error_t _gpgme_engine_op_passwd (engine_t engine, gpgme_key_t key, unsigned int flags) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->passwd) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->passwd) (engine->engine, key, flags); } /* Set the pinentry mode for ENGINE to MODE. */ gpgme_error_t _gpgme_engine_set_pinentry_mode (engine_t engine, gpgme_pinentry_mode_t mode) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->set_pinentry_mode) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->set_pinentry_mode) (engine->engine, mode); } gpgme_error_t _gpgme_engine_op_spawn (engine_t engine, const char *file, const char *argv[], gpgme_data_t datain, gpgme_data_t dataout, gpgme_data_t dataerr, unsigned int flags) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->opspawn) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->opspawn) (engine->engine, file, argv, datain, dataout, dataerr, flags); } diff --git a/src/engine.h b/src/engine.h index 34bf8e90..8b692f2e 100644 --- a/src/engine.h +++ b/src/engine.h @@ -1,217 +1,219 @@ /* engine.h - GPGME engine interface. Copyright (C) 2000 Werner Koch (dd9jn) Copyright (C) 2001, 2002, 2003, 2004, 2010 g10 Code GmbH This file is part of GPGME. GPGME is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. GPGME is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef ENGINE_H #define ENGINE_H #include "gpgme.h" /* Flags used by the EXTRAFLAGS arg of _gpgme_engine_op_genkey. */ #define GENKEY_EXTRAFLAG_ARMOR 1 #define GENKEY_EXTRAFLAG_REVOKE 2 #define GENKEY_EXTRAFLAG_SETPRIMARY 4 struct engine; typedef struct engine *engine_t; typedef gpgme_error_t (*engine_status_handler_t) (void *priv, gpgme_status_code_t code, char *args); typedef gpgme_error_t (*engine_colon_line_handler_t) (void *priv, char *line); typedef gpgme_error_t (*engine_command_handler_t) (void *priv, gpgme_status_code_t code, const char *keyword, int fd, int *processed); typedef gpgme_error_t (*engine_assuan_result_cb_t) (void *priv, gpgme_error_t result); /* Helper for gpgme_set_global_flag. */ int _gpgme_set_engine_minimal_version (const char *value); /* Get a deep copy of the engine info and return it in INFO. */ gpgme_error_t _gpgme_engine_info_copy (gpgme_engine_info_t *r_info); /* Release the engine info INFO. */ void _gpgme_engine_info_release (gpgme_engine_info_t info); /* Set the engine info for the info list INFO, protocol PROTO, to the file name FILE_NAME and the home directory HOME_DIR. */ gpgme_error_t _gpgme_set_engine_info (gpgme_engine_info_t info, gpgme_protocol_t praoto, const char *file_name, const char *home_dir); gpgme_error_t _gpgme_engine_new (gpgme_engine_info_t info, engine_t *r_engine); gpgme_error_t _gpgme_engine_reset (engine_t engine); gpgme_error_t _gpgme_engine_set_locale (engine_t engine, int category, const char *value); gpgme_error_t _gpgme_engine_set_protocol (engine_t engine, gpgme_protocol_t protocol); void _gpgme_engine_set_engine_flags (engine_t engine, gpgme_ctx_t ctx); void _gpgme_engine_release (engine_t engine); void _gpgme_engine_set_status_cb (engine_t engine, gpgme_status_cb_t cb, void *cb_value); void _gpgme_engine_set_status_handler (engine_t engine, engine_status_handler_t fnc, void *fnc_value); gpgme_error_t _gpgme_engine_set_command_handler (engine_t engine, engine_command_handler_t fnc, void *fnc_value, gpgme_data_t data); gpgme_error_t _gpgme_engine_set_colon_line_handler (engine_t engine, engine_colon_line_handler_t fnc, void *fnc_value); gpgme_error_t _gpgme_engine_op_decrypt (engine_t engine, gpgme_decrypt_flags_t flags, gpgme_data_t ciph, gpgme_data_t plain, int export_session_key, const char *override_session_key, int auto_key_retrieve); gpgme_error_t _gpgme_engine_op_delete (engine_t engine, gpgme_key_t key, unsigned int flags); gpgme_error_t _gpgme_engine_op_edit (engine_t engine, int type, gpgme_key_t key, gpgme_data_t out, gpgme_ctx_t ctx /* FIXME */); gpgme_error_t _gpgme_engine_op_encrypt (engine_t engine, gpgme_key_t recp[], + const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor); gpgme_error_t _gpgme_engine_op_encrypt_sign (engine_t engine, gpgme_key_t recp[], + const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor, gpgme_ctx_t ctx /* FIXME */); gpgme_error_t _gpgme_engine_op_export (engine_t engine, const char *pattern, gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor); gpgme_error_t _gpgme_engine_op_export_ext (engine_t engine, const char *pattern[], gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor); gpgme_error_t _gpgme_engine_op_genkey (engine_t engine, const char *userid, const char *algo, unsigned long reserved, unsigned long expires, gpgme_key_t key, unsigned int flags, gpgme_data_t help_data, unsigned int extraflags, gpgme_data_t pubkey, gpgme_data_t seckey); gpgme_error_t _gpgme_engine_op_keysign (engine_t engine, gpgme_key_t key, const char *userid, unsigned long expires, unsigned int flags, gpgme_ctx_t ctx); gpgme_error_t _gpgme_engine_op_tofu_policy (engine_t engine, gpgme_key_t key, gpgme_tofu_policy_t policy); gpgme_error_t _gpgme_engine_op_import (engine_t engine, gpgme_data_t keydata, gpgme_key_t *keyarray); gpgme_error_t _gpgme_engine_op_keylist (engine_t engine, const char *pattern, int secret_only, gpgme_keylist_mode_t mode, int engine_flags); gpgme_error_t _gpgme_engine_op_keylist_ext (engine_t engine, const char *pattern[], int secret_only, int reserved, gpgme_keylist_mode_t mode, int engine_flags); gpgme_error_t _gpgme_engine_op_keylist_data (engine_t engine, gpgme_data_t data); gpgme_error_t _gpgme_engine_op_sign (engine_t engine, gpgme_data_t in, gpgme_data_t out, gpgme_sig_mode_t mode, int use_armor, int use_textmode, int include_certs, gpgme_ctx_t ctx /* FIXME */); gpgme_error_t _gpgme_engine_op_trustlist (engine_t engine, const char *pattern); gpgme_error_t _gpgme_engine_op_verify (engine_t engine, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext, gpgme_ctx_t ctx); gpgme_error_t _gpgme_engine_op_getauditlog (engine_t engine, gpgme_data_t output, unsigned int flags); gpgme_error_t _gpgme_engine_op_assuan_transact (engine_t engine, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value); gpgme_error_t _gpgme_engine_op_conf_load (engine_t engine, gpgme_conf_comp_t *conf_p); gpgme_error_t _gpgme_engine_op_conf_save (engine_t engine, gpgme_conf_comp_t conf); gpgme_error_t _gpgme_engine_op_conf_dir (engine_t engine, const char *what, char **result); gpgme_error_t _gpgme_engine_op_query_swdb (engine_t engine, const char *name, const char *iversion, gpgme_query_swdb_result_t result); void _gpgme_engine_set_io_cbs (engine_t engine, gpgme_io_cbs_t io_cbs); void _gpgme_engine_io_event (engine_t engine, gpgme_event_io_t type, void *type_data); gpgme_error_t _gpgme_engine_cancel (engine_t engine); gpgme_error_t _gpgme_engine_cancel_op (engine_t engine); gpgme_error_t _gpgme_engine_op_passwd (engine_t engine, gpgme_key_t key, unsigned int flags); gpgme_error_t _gpgme_engine_set_pinentry_mode (engine_t engine, gpgme_pinentry_mode_t mode); gpgme_error_t _gpgme_engine_op_spawn (engine_t engine, const char *file, const char *argv[], gpgme_data_t datain, gpgme_data_t dataout, gpgme_data_t dataerr, unsigned int flags); /* The available engine option flags. */ #define GPGME_ENGINE_FLAG_OFFLINE 1 #endif /* ENGINE_H */ diff --git a/src/gpgme-json.c b/src/gpgme-json.c index b54d9a8a..4b1cd5b8 100644 --- a/src/gpgme-json.c +++ b/src/gpgme-json.c @@ -1,1376 +1,1374 @@ /* gpgme-json.c - JSON based interface to gpgme (server) * Copyright (C) 2018 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ /* This is tool implements the Native Messaging protocol of web * browsers and provides the server part of it. A Javascript based * client can be found in lang/javascript. The used data format is * similar to the API of openpgpjs. */ #include #include #include #include #include #ifdef HAVE_LOCALE_H #include #endif #include #define GPGRT_ENABLE_ES_MACROS 1 #define GPGRT_ENABLE_LOG_MACROS 1 #define GPGRT_ENABLE_ARGPARSE_MACROS 1 #include "gpgme.h" #include "cJSON.h" #if GPGRT_VERSION_NUMBER < 0x011c00 /* 1.28 */ int main (void){fputs ("Build with Libgpg-error >= 1.28!\n", stderr);return 1;} #else /* libgpg-error >= 1.28 */ /* We don't allow a request with more than 64 MiB. */ #define MAX_REQUEST_SIZE (64 * 1024 * 1024) static void xoutofcore (const char *type) GPGRT_ATTR_NORETURN; static cjson_t error_object_v (cjson_t json, const char *message, va_list arg_ptr) GPGRT_ATTR_PRINTF(2,0); static cjson_t error_object (cjson_t json, const char *message, ...) GPGRT_ATTR_PRINTF(2,3); static char *error_object_string (const char *message, ...) GPGRT_ATTR_PRINTF(1,2); /* True if interactive mode is active. */ static int opt_interactive; /* True is debug mode is active. */ static int opt_debug; /* * Helper functions and macros */ #define xtrymalloc(a) gpgrt_malloc ((a)) #define xtrystrdup(a) gpgrt_strdup ((a)) #define xmalloc(a) ({ \ void *_r = gpgrt_malloc ((a)); \ if (!_r) \ xoutofcore ("malloc"); \ _r; }) #define xcalloc(a,b) ({ \ void *_r = gpgrt_calloc ((a), (b)); \ if (!_r) \ xoutofcore ("calloc"); \ _r; }) #define xstrdup(a) ({ \ char *_r = gpgrt_strdup ((a)); \ if (!_r) \ xoutofcore ("strdup"); \ _r; }) #define xstrconcat(a, ...) ({ \ char *_r = gpgrt_strconcat ((a), __VA_ARGS__); \ if (!_r) \ xoutofcore ("strconcat"); \ _r; }) #define xfree(a) gpgrt_free ((a)) #define spacep(p) (*(p) == ' ' || *(p) == '\t') static void xoutofcore (const char *type) { gpg_error_t err = gpg_error_from_syserror (); log_error ("%s failed: %s\n", type, gpg_strerror (err)); exit (2); } /* Call cJSON_CreateObject but terminate in case of an error. */ static cjson_t xjson_CreateObject (void) { cjson_t json = cJSON_CreateObject (); if (!json) xoutofcore ("cJSON_CreateObject"); return json; } /* Wrapper around cJSON_AddStringToObject which returns an gpg-error * code instead of the NULL or the new object. */ static gpg_error_t cjson_AddStringToObject (cjson_t object, const char *name, const char *string) { if (!cJSON_AddStringToObject (object, name, string)) return gpg_error_from_syserror (); return 0; } /* Same as cjson_AddStringToObject but prints an error message and * terminates the process. */ static void xjson_AddStringToObject (cjson_t object, const char *name, const char *string) { if (!cJSON_AddStringToObject (object, name, string)) xoutofcore ("cJSON_AddStringToObject"); } /* Wrapper around cJSON_AddBoolToObject which terminates the process * in case of an error. */ static void xjson_AddBoolToObject (cjson_t object, const char *name, int abool) { if (!cJSON_AddBoolToObject (object, name, abool)) xoutofcore ("cJSON_AddStringToObject"); return ; } /* This is similar to cJSON_AddStringToObject but takes a gpgme DATA * object and adds it under NAME as a base 64 encoded string to * OBJECT. Ownership of DATA is transferred to this function. */ static gpg_error_t add_base64_to_object (cjson_t object, const char *name, gpgme_data_t data) { #if GPGRT_VERSION_NUMBER < 0x011d00 /* 1.29 */ return gpg_error (GPG_ERR_NOT_SUPPORTED); #else gpg_err_code_t err; estream_t fp = NULL; gpgrt_b64state_t state = NULL; cjson_t j_str = NULL; void *buffer = NULL; size_t buflen; fp = es_fopenmem (0, "rwb"); if (!fp) { err = gpg_err_code_from_syserror (); goto leave; } state = gpgrt_b64enc_start (fp, ""); if (!state) { err = gpg_err_code_from_syserror (); goto leave; } gpgme_data_write (data, "", 1); /* Make sure we have a string. */ buffer = gpgme_data_release_and_get_mem (data, &buflen); data = NULL; if (!buffer) { err = gpg_error_from_syserror (); goto leave; } err = gpgrt_b64enc_write (state, buffer, buflen); if (err) goto leave; xfree (buffer); buffer = NULL; err = gpgrt_b64enc_finish (state); state = NULL; if (err) return err; es_fputc (0, fp); if (es_fclose_snatch (fp, &buffer, NULL)) { fp = NULL; err = gpg_error_from_syserror (); goto leave; } fp = NULL; j_str = cJSON_CreateStringConvey (buffer); if (!j_str) { err = gpg_error_from_syserror (); goto leave; } buffer = NULL; if (!cJSON_AddItemToObject (object, name, j_str)) { err = gpg_error_from_syserror (); cJSON_Delete (j_str); j_str = NULL; goto leave; } j_str = NULL; leave: xfree (buffer); cJSON_Delete (j_str); gpgrt_b64enc_finish (state); es_fclose (fp); gpgme_data_release (data); return err; #endif } /* Create a JSON error object. If JSON is not NULL the error message * is appended to that object. An existing "type" item will be replaced. */ static cjson_t error_object_v (cjson_t json, const char *message, va_list arg_ptr) { cjson_t response, j_tmp; char *msg; msg = gpgrt_vbsprintf (message, arg_ptr); if (!msg) xoutofcore ("error_object"); response = json? json : xjson_CreateObject (); if (!(j_tmp = cJSON_GetObjectItem (response, "type"))) xjson_AddStringToObject (response, "type", "error"); else /* Replace existing "type". */ { j_tmp = cJSON_CreateString ("error"); if (!j_tmp) xoutofcore ("cJSON_CreateString"); cJSON_ReplaceItemInObject (response, "type", j_tmp); } xjson_AddStringToObject (response, "msg", msg); xfree (msg); return response; } /* Call cJSON_Print but terminate in case of an error. */ static char * xjson_Print (cjson_t object) { char *buf; buf = cJSON_Print (object); if (!buf) xoutofcore ("cJSON_Print"); return buf; } static cjson_t error_object (cjson_t json, const char *message, ...) { cjson_t response; va_list arg_ptr; va_start (arg_ptr, message); response = error_object_v (json, message, arg_ptr); va_end (arg_ptr); return response; } static char * error_object_string (const char *message, ...) { cjson_t response; va_list arg_ptr; char *msg; va_start (arg_ptr, message); response = error_object_v (NULL, message, arg_ptr); va_end (arg_ptr); msg = xjson_Print (response); cJSON_Delete (response); return msg; } /* Get the boolean property NAME from the JSON object and store true * or valse at R_VALUE. If the name is unknown the value of DEF_VALUE * is returned. If the type of the value is not boolean, * GPG_ERR_INV_VALUE is returned and R_VALUE set to DEF_VALUE. */ static gpg_error_t get_boolean_flag (cjson_t json, const char *name, int def_value, int *r_value) { cjson_t j_item; j_item = cJSON_GetObjectItem (json, name); if (!j_item) *r_value = def_value; else if (cjson_is_true (j_item)) *r_value = 1; else if (cjson_is_false (j_item)) *r_value = 0; else { *r_value = def_value; return gpg_error (GPG_ERR_INV_VALUE); } return 0; } /* Get the boolean property PROTOCOL from the JSON object and store * its value at R_PROTOCOL. The default is OpenPGP. */ static gpg_error_t get_protocol (cjson_t json, gpgme_protocol_t *r_protocol) { cjson_t j_item; *r_protocol = GPGME_PROTOCOL_OpenPGP; j_item = cJSON_GetObjectItem (json, "protocol"); if (!j_item) ; else if (!cjson_is_string (j_item)) return gpg_error (GPG_ERR_INV_VALUE); else if (!strcmp(j_item->valuestring, "openpgp")) ; else if (!strcmp(j_item->valuestring, "cms")) *r_protocol = GPGME_PROTOCOL_CMS; else return gpg_error (GPG_ERR_UNSUPPORTED_PROTOCOL); return 0; } -/* Extract the keys from the KEYS array in the JSON object. CTX is a - * GPGME context object. On success an array with the keys is stored - * at R_KEYS. In failure an error code is returned. */ +/* Extract the keys from the "keys" array in the JSON object. On + * success a string with the keys identifiers is stored at R_KEYS. + * The keys in that string are LF delimited. On failure an error code + * is returned. */ static gpg_error_t -get_keys (gpgme_ctx_t ctx, cjson_t json, gpgme_key_t **r_keys) +get_keys (cjson_t json, char **r_keystring) { - gpg_error_t err; cjson_t j_keys, j_item; int i, nkeys; - gpgme_key_t *keys; + char *p; + size_t length; - *r_keys = NULL; + *r_keystring = NULL; j_keys = cJSON_GetObjectItem (json, "keys"); if (!j_keys) return gpg_error (GPG_ERR_NO_KEY); if (!cjson_is_array (j_keys) && !cjson_is_string (j_keys)) return gpg_error (GPG_ERR_INV_VALUE); + /* Fixme: We should better use a membuf like thing. */ + length = 1; /* For the EOS. */ if (cjson_is_string (j_keys)) - nkeys = 1; + { + nkeys = 1; + length += strlen (j_keys->valuestring); + if (strchr (j_keys->valuestring, '\n')) + return gpg_error (GPG_ERR_INV_USER_ID); + } else { nkeys = cJSON_GetArraySize (j_keys); if (!nkeys) return gpg_error (GPG_ERR_NO_KEY); for (i=0; i < nkeys; i++) { j_item = cJSON_GetArrayItem (j_keys, i); if (!j_item || !cjson_is_string (j_item)) return gpg_error (GPG_ERR_INV_VALUE); + if (i) + length++; /* Space for delimiter. */ + length += strlen (j_item->valuestring); + if (strchr (j_item->valuestring, '\n')) + return gpg_error (GPG_ERR_INV_USER_ID); } } - /* Now allocate an array to store the gpgme key objects. */ - keys = xcalloc (nkeys + 1, sizeof *keys); + p = *r_keystring = xtrymalloc (length); + if (!p) + return gpg_error_from_syserror (); if (cjson_is_string (j_keys)) { - err = gpgme_get_key (ctx, j_keys->valuestring, &keys[0], 0); - if (err) - goto leave; + strcpy (p, j_keys->valuestring); } else { for (i=0; i < nkeys; i++) { j_item = cJSON_GetArrayItem (j_keys, i); - err = gpgme_get_key (ctx, j_item->valuestring, &keys[i], 0); - if (err) - goto leave; + if (i) + *p++ = '\n'; /* Add delimiter. */ + p = stpcpy (p, j_item->valuestring); } } - err = 0; - *r_keys = keys; - keys = NULL; - - leave: - if (keys) - { - for (i=0; keys[i]; i++) - gpgme_key_unref (keys[i]); - xfree (keys); - } - return err; + return 0; } + /* * GPGME support functions. */ /* Helper for get_context. */ static gpgme_ctx_t _create_new_context (gpgme_protocol_t proto) { gpg_error_t err; gpgme_ctx_t ctx; err = gpgme_new (&ctx); if (err) log_fatal ("error creating GPGME context: %s\n", gpg_strerror (err)); gpgme_set_protocol (ctx, proto); gpgme_set_ctx_flag (ctx, "request-origin", "browser"); return ctx; } /* Return a context object for protocol PROTO. This is currently a * statuically allocated context initialized for PROTO. Termnates * process on failure. */ static gpgme_ctx_t get_context (gpgme_protocol_t proto) { static gpgme_ctx_t ctx_openpgp, ctx_cms; if (proto == GPGME_PROTOCOL_OpenPGP) { if (!ctx_openpgp) ctx_openpgp = _create_new_context (proto); return ctx_openpgp; } else if (proto == GPGME_PROTOCOL_CMS) { if (!ctx_cms) ctx_cms = _create_new_context (proto); return ctx_cms; } else log_bug ("invalid protocol %d requested\n", proto); } /* Free context object retrieved by get_context. */ static void release_context (gpgme_ctx_t ctx) { /* Nothing to do right now. */ (void)ctx; } /* Given a Base-64 encoded string object in JSON return a gpgme data * object at R_DATA. */ static gpg_error_t data_from_base64_string (gpgme_data_t *r_data, cjson_t json) { #if GPGRT_VERSION_NUMBER < 0x011d00 /* 1.29 */ *r_data = NULL; return gpg_error (GPG_ERR_NOT_SUPPORTED); #else gpg_error_t err; size_t len; char *buf = NULL; gpgrt_b64state_t state = NULL; gpgme_data_t data = NULL; *r_data = NULL; /* A quick check on the JSON. */ if (!cjson_is_string (json)) { err = gpg_error (GPG_ERR_INV_VALUE); goto leave; } state = gpgrt_b64dec_start (NULL); if (!state) { err = gpg_err_code_from_syserror (); goto leave; } /* Fixme: Data duplication - we should see how to snatch the memory * from the json object. */ len = strlen (json->valuestring); buf = xtrystrdup (json->valuestring); if (!buf) { err = gpg_error_from_syserror (); goto leave; } err = gpgrt_b64dec_proc (state, buf, len, &len); if (err) goto leave; err = gpgrt_b64dec_finish (state); state = NULL; if (err) goto leave; err = gpgme_data_new_from_mem (&data, buf, len, 1); if (err) goto leave; *r_data = data; data = NULL; leave: xfree (data); xfree (buf); gpgrt_b64dec_finish (state); return err; #endif } /* * Implementaion of the commands. */ static const char hlp_encrypt[] = "op: \"encrypt\"\n" "keys: Array of strings with the fingerprints or user-ids\n" " of the keys to encrypt the data. For a single key\n" " a String may be used instead of an array.\n" "data: Input data. \n" "\n" "Optional parameters:\n" "protocol: Either \"openpgp\" (default) or \"cms\".\n" "\n" "Optional boolean flags (default is false):\n" "base64: Input data is base64 encoded.\n" "armor: Request output in armored format.\n" "always-trust: Request --always-trust option.\n" "no-encrypt-to: Do not use a default recipient.\n" "no-compress: Do not compress the plaintext first.\n" "throw-keyids: Request the --throw-keyids option.\n" "wrap: Assume the input is an OpenPGP message.\n" "\n" "Response on success:\n" "type: \"ciphertext\"\n" "data: Unless armor mode is used a Base64 encoded binary\n" " ciphertext. In armor mode a string with an armored\n" " OpenPGP or a PEM message.\n" "base64: Boolean indicating whether data is base64 encoded."; static gpg_error_t op_encrypt (cjson_t request, cjson_t result) { gpg_error_t err; gpgme_ctx_t ctx = NULL; gpgme_protocol_t protocol; int opt_base64; - gpgme_key_t *keys = NULL; + char *keystring = NULL; cjson_t j_input; gpgme_data_t input = NULL; gpgme_data_t output = NULL; - int abool, i; + int abool; gpgme_encrypt_flags_t encrypt_flags = 0; if ((err = get_protocol (request, &protocol))) goto leave; ctx = get_context (protocol); if ((err = get_boolean_flag (request, "base64", 0, &opt_base64))) goto leave; if ((err = get_boolean_flag (request, "armor", 0, &abool))) goto leave; gpgme_set_armor (ctx, abool); if ((err = get_boolean_flag (request, "always-trust", 0, &abool))) goto leave; if (abool) encrypt_flags |= GPGME_ENCRYPT_ALWAYS_TRUST; if ((err = get_boolean_flag (request, "no-encrypt-to", 0,&abool))) goto leave; if (abool) encrypt_flags |= GPGME_ENCRYPT_NO_ENCRYPT_TO; if ((err = get_boolean_flag (request, "no-compress", 0, &abool))) goto leave; if (abool) encrypt_flags |= GPGME_ENCRYPT_NO_COMPRESS; if ((err = get_boolean_flag (request, "throw-keyids", 0, &abool))) goto leave; if (abool) encrypt_flags |= GPGME_ENCRYPT_THROW_KEYIDS; if ((err = get_boolean_flag (request, "wrap", 0, &abool))) goto leave; if (abool) encrypt_flags |= GPGME_ENCRYPT_WRAP; /* Get the keys. */ - err = get_keys (ctx, request, &keys); + err = get_keys (request, &keystring); if (err) { /* Provide a custom error response. */ error_object (result, "Error getting keys: %s", gpg_strerror (err)); goto leave; } /* Get the data. Note that INPUT is a shallow data object with the * storage hold in REQUEST. */ j_input = cJSON_GetObjectItem (request, "data"); if (!j_input) { err = gpg_error (GPG_ERR_NO_DATA); goto leave; } if (!cjson_is_string (j_input)) { err = gpg_error (GPG_ERR_INV_VALUE); goto leave; } if (opt_base64) { err = data_from_base64_string (&input, j_input); if (err) { error_object (result, "Error decoding Base-64 encoded 'data': %s", gpg_strerror (err)); goto leave; } } else { err = gpgme_data_new_from_mem (&input, j_input->valuestring, strlen (j_input->valuestring), 0); if (err) { error_object (result, "Error getting 'data': %s", gpg_strerror (err)); goto leave; } } /* Create an output data object. */ err = gpgme_data_new (&output); if (err) { error_object (result, "Error creating output data object: %s", gpg_strerror (err)); goto leave; } /* Encrypt. */ - err = gpgme_op_encrypt (ctx, keys, encrypt_flags, input, output); + err = gpgme_op_encrypt_ext (ctx, NULL, keystring, encrypt_flags, + input, output); /* encrypt_result = gpgme_op_encrypt_result (ctx); */ if (err) { error_object (result, "Encryption failed: %s", gpg_strerror (err)); goto leave; } gpgme_data_release (input); input = NULL; xjson_AddStringToObject (result, "type", "ciphertext"); /* If armoring is used we do not need to base64 the output. */ xjson_AddBoolToObject (result, "base64", !gpgme_get_armor (ctx)); if (gpgme_get_armor (ctx)) { char *buffer; /* Make sure that we really have a string. */ gpgme_data_write (output, "", 1); buffer = gpgme_data_release_and_get_mem (output, NULL); if (!buffer) { err = gpg_error_from_syserror (); goto leave; } err = cjson_AddStringToObject (result, "data", buffer); gpgme_free (buffer); if (err) goto leave; } else { err = add_base64_to_object (result, "data", output); output = NULL; if (err) goto leave; } leave: - if (keys) - { - for (i=0; keys[i]; i++) - gpgme_key_unref (keys[i]); - xfree (keys); - } + xfree (keystring); release_context (ctx); gpgme_data_release (input); return err; } static const char hlp_help[] = "The tool expects a JSON object with the request and responds with\n" "another JSON object. Even on error a JSON object is returned. The\n" "property \"op\" is mandatory and its string value selects the\n" "operation; if the property \"help\" with the value \"true\" exists, the\n" "operation is not performned but a string with the documentation\n" "returned. To list all operations it is allowed to leave out \"op\" in\n" "help mode. Supported values for \"op\" are:\n\n" " encrypt Encrypt data.\n" " help Help overview."; static gpg_error_t op_help (cjson_t request, cjson_t result) { cjson_t j_tmp; char *buffer = NULL; const char *msg; j_tmp = cJSON_GetObjectItem (request, "interactive_help"); if (opt_interactive && j_tmp && cjson_is_string (j_tmp)) msg = buffer = xstrconcat (hlp_help, "\n", j_tmp->valuestring, NULL); else msg = hlp_help; xjson_AddStringToObject (result, "type", "help"); xjson_AddStringToObject (result, "msg", msg); xfree (buffer); return 0; } /* Process a request and return the response. The response is a newly * allocated string or NULL in case of an error. */ static char * process_request (const char *request) { static struct { const char *op; gpg_error_t (*handler)(cjson_t request, cjson_t result); const char * const helpstr; } optbl[] = { { "encrypt", op_encrypt, hlp_encrypt }, { "help", op_help, hlp_help }, { NULL } }; size_t erroff; cjson_t json; cjson_t j_tmp, j_op; cjson_t response; int helpmode; const char *op; char *res; int idx; response = xjson_CreateObject (); json = cJSON_Parse (request, &erroff); if (!json) { log_string (GPGRT_LOGLVL_INFO, request); log_info ("invalid JSON object at offset %zu\n", erroff); error_object (response, "invalid JSON object at offset %zu\n", erroff); goto leave; } j_tmp = cJSON_GetObjectItem (json, "help"); helpmode = (j_tmp && cjson_is_true (j_tmp)); j_op = cJSON_GetObjectItem (json, "op"); if (!j_op || !cjson_is_string (j_op)) { if (!helpmode) { error_object (response, "Property \"op\" missing"); goto leave; } op = "help"; /* Help summary. */ } else op = j_op->valuestring; for (idx=0; optbl[idx].op; idx++) if (!strcmp (op, optbl[idx].op)) break; if (optbl[idx].op) { if (helpmode && strcmp (op, "help")) { xjson_AddStringToObject (response, "type", "help"); xjson_AddStringToObject (response, "op", op); xjson_AddStringToObject (response, "msg", optbl[idx].helpstr); } else { gpg_error_t err; err = optbl[idx].handler (json, response); if (err) { if (!(j_tmp = cJSON_GetObjectItem (response, "type")) || !cjson_is_string (j_tmp) || strcmp (j_tmp->valuestring, "error")) { /* No error type response - provide a generic one. */ error_object (response, "Operation failed: %s", gpg_strerror (err)); } xjson_AddStringToObject (response, "op", op); } } } else /* Operation not supported. */ { error_object (response, "Unknown operation '%s'", op); xjson_AddStringToObject (response, "op", op); } leave: cJSON_Delete (json); if (opt_interactive) res = cJSON_Print (response); else res = cJSON_PrintUnformatted (response); if (!res) log_error ("Printing JSON data failed\n"); cJSON_Delete (response); return res; } /* * Driver code */ /* Return a malloced line or NULL on EOF. Terminate on read * error. */ static char * get_line (void) { char *line = NULL; size_t linesize = 0; gpg_error_t err; size_t maxlength = 2048; int n; const char *s; char *p; again: n = es_read_line (es_stdin, &line, &linesize, &maxlength); if (n < 0) { err = gpg_error_from_syserror (); log_error ("error reading line: %s\n", gpg_strerror (err)); exit (1); } if (!n) { xfree (line); line = NULL; return NULL; /* EOF */ } if (!maxlength) { log_info ("line too long - skipped\n"); goto again; } if (memchr (line, 0, n)) log_info ("warning: line shortened due to embedded Nul character\n"); if (line[n-1] == '\n') line[n-1] = 0; /* Trim leading spaces. */ for (s=line; spacep (s); s++) ; if (s != line) { for (p=line; *s;) *p++ = *s++; *p = 0; n = p - line; } return line; } /* Process meta commands used with the standard REPL. */ static char * process_meta_commands (const char *request) { char *result = NULL; while (spacep (request)) request++; if (!strncmp (request, "help", 4) && (spacep (request+4) || !request[4])) result = process_request ("{ \"op\": \"help\"," " \"interactive_help\": " "\"\\nMeta commands:\\n" " ,help This help\\n" " ,quit Terminate process\"" "}"); else if (!strncmp (request, "quit", 4) && (spacep (request+4) || !request[4])) exit (0); else log_info ("invalid meta command\n"); return result; } /* If STRING has a help response, return the MSG property in a human * readable format. */ static char * get_help_msg (const char *string) { cjson_t json, j_type, j_msg; const char *msg; char *buffer = NULL; char *p; json = cJSON_Parse (string, NULL); if (json) { j_type = cJSON_GetObjectItem (json, "type"); if (j_type && cjson_is_string (j_type) && !strcmp (j_type->valuestring, "help")) { j_msg = cJSON_GetObjectItem (json, "msg"); if (j_msg || cjson_is_string (j_msg)) { msg = j_msg->valuestring; buffer = malloc (strlen (msg)+1); if (buffer) { for (p=buffer; *msg; msg++) { if (*msg == '\\' && msg[1] == '\n') *p++ = '\n'; else *p++ = *msg; } *p = 0; } } } cJSON_Delete (json); } return buffer; } /* An interactive standard REPL. */ static void interactive_repl (void) { char *line = NULL; char *request = NULL; char *response = NULL; char *p; int first; es_setvbuf (es_stdin, NULL, _IONBF, 0); #if GPGRT_VERSION_NUMBER >= 0x011d00 /* 1.29 */ es_fprintf (es_stderr, "%s %s ready (enter \",help\" for help)\n", gpgrt_strusage (11), gpgrt_strusage (13)); #endif do { es_fputs ("> ", es_stderr); es_fflush (es_stderr); es_fflush (es_stdout); xfree (line); line = get_line (); es_fflush (es_stderr); es_fflush (es_stdout); first = !request; if (line && *line) { if (!request) request = xstrdup (line); else request = xstrconcat (request, "\n", line, NULL); } if (!line) es_fputs ("\n", es_stderr); if (!line || !*line || (first && *request == ',')) { /* Process the input. */ xfree (response); response = NULL; if (request && *request == ',') { response = process_meta_commands (request+1); } else if (request) { response = process_request (request); } xfree (request); request = NULL; if (response) { if (opt_interactive) { char *msg = get_help_msg (response); if (msg) { xfree (response); response = msg; } } es_fputs ("===> ", es_stderr); es_fflush (es_stderr); for (p=response; *p; p++) { if (*p == '\n') { es_fflush (es_stdout); es_fputs ("\n===> ", es_stderr); es_fflush (es_stderr); } else es_putc (*p, es_stdout); } es_fflush (es_stdout); es_fputs ("\n", es_stderr); } } } while (line); xfree (request); xfree (response); xfree (line); } /* Read and process a single request. */ static void read_and_process_single_request (void) { char *line = NULL; char *request = NULL; char *response = NULL; size_t n; for (;;) { xfree (line); line = get_line (); if (line && *line) request = (request? xstrconcat (request, "\n", line, NULL) /**/ : xstrdup (line)); if (!line) { if (request) { xfree (response); response = process_request (request); if (response) { es_fputs (response, es_stdout); if ((n = strlen (response)) && response[n-1] != '\n') es_fputc ('\n', es_stdout); } es_fflush (es_stdout); } break; } } xfree (response); xfree (request); xfree (line); } /* The Native Messaging processing loop. */ static void native_messaging_repl (void) { gpg_error_t err; uint32_t nrequest, nresponse; char *request = NULL; char *response = NULL; size_t n; /* Due to the length octets we need to switch the I/O stream into * binary mode. */ es_set_binary (es_stdin); es_set_binary (es_stdout); es_setbuf (es_stdin, NULL); /* stdin needs to be unbuffered! */ for (;;) { /* Read length. Note that the protocol uses native endianess. * Is it allowed to call such a thing a well thought out * protocol? */ if (es_read (es_stdin, &nrequest, sizeof nrequest, &n)) { err = gpg_error_from_syserror (); log_error ("error reading request header: %s\n", gpg_strerror (err)); break; } if (!n) break; /* EOF */ if (n != sizeof nrequest) { log_error ("error reading request header: short read\n"); break; } if (nrequest > MAX_REQUEST_SIZE) { log_error ("error reading request: request too long (%zu MiB)\n", (size_t)nrequest / (1024*1024)); /* Fixme: Shall we read the request to the bit bucket and * return an error reponse or just return an error reponse * and terminate? Needs some testing. */ break; } /* Read request. */ request = xtrymalloc (nrequest); if (!request) { err = gpg_error_from_syserror (); log_error ("error reading request: Not enough memory for %zu MiB)\n", (size_t)nrequest / (1024*1024)); /* FIXME: See comment above. */ break; } if (es_read (es_stdin, request, nrequest, &n)) { err = gpg_error_from_syserror (); log_error ("error reading request: %s\n", gpg_strerror (err)); break; } if (n != nrequest) { /* That is a protocol violation. */ xfree (response); response = error_object_string ("Invalid request:" " short read (%zu of %zu bytes)\n", n, (size_t)nrequest); } else /* Process request */ { if (opt_debug) log_debug ("request='%s'\n", request); xfree (response); response = process_request (request); if (opt_debug) log_debug ("response='%s'\n", response); } nresponse = strlen (response); /* Write response */ if (es_write (es_stdout, &nresponse, sizeof nresponse, &n)) { err = gpg_error_from_syserror (); log_error ("error writing request header: %s\n", gpg_strerror (err)); break; } if (n != sizeof nrequest) { log_error ("error writing request header: short write\n"); break; } if (es_write (es_stdout, response, nresponse, &n)) { err = gpg_error_from_syserror (); log_error ("error writing request: %s\n", gpg_strerror (err)); break; } if (n != nresponse) { log_error ("error writing request: short write\n"); break; } if (es_fflush (es_stdout) || es_ferror (es_stdout)) { err = gpg_error_from_syserror (); log_error ("error writing request: %s\n", gpg_strerror (err)); break; } } xfree (response); xfree (request); } static const char * my_strusage( int level ) { const char *p; switch (level) { case 9: p = "LGPL-2.1-or-later"; break; case 11: p = "gpgme-json"; break; case 13: p = PACKAGE_VERSION; break; case 14: p = "Copyright (C) 2018 g10 Code GmbH"; break; case 19: p = "Please report bugs to <" PACKAGE_BUGREPORT ">.\n"; break; case 1: case 40: p = "Usage: gpgme-json [OPTIONS]"; break; case 41: p = "Native messaging based GPGME operations.\n"; break; case 42: p = "1"; /* Flag print 40 as part of 41. */ break; default: p = NULL; break; } return p; } int main (int argc, char *argv[]) { #if GPGRT_VERSION_NUMBER < 0x011d00 /* 1.29 */ fprintf (stderr, "WARNING: Old libgpg-error - using limited mode\n"); native_messaging_repl (); #else /* This is a modern libgp-error. */ enum { CMD_DEFAULT = 0, CMD_INTERACTIVE = 'i', CMD_SINGLE = 's', CMD_LIBVERSION = 501, } cmd = CMD_DEFAULT; enum { OPT_DEBUG = 600 }; static gpgrt_opt_t opts[] = { ARGPARSE_c (CMD_INTERACTIVE, "interactive", "Interactive REPL"), ARGPARSE_c (CMD_SINGLE, "single", "Single request mode"), ARGPARSE_c (CMD_LIBVERSION, "lib-version", "Show library version"), ARGPARSE_s_n(OPT_DEBUG, "debug", "Flyswatter"), ARGPARSE_end() }; gpgrt_argparse_t pargs = { &argc, &argv}; gpgrt_set_strusage (my_strusage); #ifdef HAVE_SETLOCALE setlocale (LC_ALL, ""); #endif gpgme_check_version (NULL); #ifdef LC_CTYPE gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL)); #endif #ifdef LC_MESSAGES gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL)); #endif while (gpgrt_argparse (NULL, &pargs, opts)) { switch (pargs.r_opt) { case CMD_INTERACTIVE: opt_interactive = 1; /* Fall trough. */ case CMD_SINGLE: case CMD_LIBVERSION: cmd = pargs.r_opt; break; case OPT_DEBUG: opt_debug = 1; break; default: pargs.err = ARGPARSE_PRINT_WARNING; break; } } gpgrt_argparse (NULL, &pargs, NULL); if (!opt_debug) { const char *s = getenv ("GPGME_JSON_DEBUG"); if (s && atoi (s) > 0) opt_debug = 1; } if (opt_debug) { const char *home = getenv ("HOME"); char *file = xstrconcat ("socket://", home? home:"/tmp", "/.gnupg/S.gpgme-json.log", NULL); log_set_file (file); xfree (file); } if (opt_debug) { int i; for (i=0; argv[i]; i++) log_debug ("argv[%d]='%s'\n", i, argv[i]); } switch (cmd) { case CMD_DEFAULT: native_messaging_repl (); break; case CMD_SINGLE: read_and_process_single_request (); break; case CMD_INTERACTIVE: interactive_repl (); break; case CMD_LIBVERSION: printf ("Version from header: %s (0x%06x)\n", GPGME_VERSION, GPGME_VERSION_NUMBER); printf ("Version from binary: %s\n", gpgme_check_version (NULL)); printf ("Copyright blurb ...:%s\n", gpgme_check_version ("\x01\x01")); break; } if (opt_debug) log_debug ("ready"); #endif /* This is a modern libgp-error. */ return 0; } #endif /* libgpg-error >= 1.28 */ diff --git a/src/gpgme.def b/src/gpgme.def index cad30f6c..a01d89a2 100644 --- a/src/gpgme.def +++ b/src/gpgme.def @@ -1,271 +1,276 @@ ; gpgme.def - List of symbols to export. ; Copyright (C) 2005 g10 Code GmbH ; ; This file is part of GPGME. ; ; GPGME is free software; you can redistribute it and/or modify ; it under the terms of the GNU Lesser general Public License as ; published by the Free Software Foundation; either version 2.1 of ; the License, or (at your option) any later version. ; ; GPGME is distributed in the hope that it will be useful, ; but WITHOUT ANY WARRANTY; without even the implied warranty of ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; GNU Lesser General Public License for more details. ; ; You should have received a copy of the GNU Lesser General Public ; License along with this program; if not, write to the Free Software ; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA EXPORTS gpgme_check_version @1 gpgme_get_engine_info @2 gpgme_engine_check_version @3 gpgme_err_code_from_errno @4 gpgme_err_code_to_errno @5 gpgme_err_make_from_errno @6 gpgme_error_from_errno @7 gpgme_strerror @8 gpgme_strerror_r @9 gpgme_strsource @10 gpgme_data_get_encoding @11 gpgme_data_new @12 gpgme_data_new_from_cbs @13 gpgme_data_new_from_fd @14 gpgme_data_new_from_file @15 gpgme_data_new_from_filepart @16 gpgme_data_new_from_mem @17 gpgme_data_new_from_stream @18 gpgme_data_read @19 gpgme_data_release @20 gpgme_data_release_and_get_mem @21 gpgme_data_seek @22 gpgme_data_set_encoding @23 gpgme_data_write @24 gpgme_get_protocol_name @25 gpgme_hash_algo_name @26 gpgme_pubkey_algo_name @27 gpgme_new @28 gpgme_get_armor @29 gpgme_get_include_certs @30 gpgme_get_io_cbs @31 gpgme_get_keylist_mode @32 gpgme_get_passphrase_cb @33 gpgme_get_progress_cb @34 gpgme_get_protocol @35 gpgme_get_textmode @36 gpgme_release @37 gpgme_set_armor @38 gpgme_set_include_certs @39 gpgme_set_io_cbs @40 gpgme_set_keylist_mode @41 gpgme_set_locale @42 gpgme_set_passphrase_cb @43 gpgme_set_progress_cb @44 gpgme_set_protocol @45 gpgme_set_textmode @46 gpgme_signers_add @47 gpgme_signers_clear @48 gpgme_signers_enum @49 gpgme_key_ref @50 gpgme_key_unref @51 gpgme_key_release @52 gpgme_trust_item_ref @53 gpgme_trust_item_unref @54 gpgme_cancel @55 gpgme_op_card_edit @56 gpgme_op_card_edit_start @57 gpgme_op_decrypt @58 gpgme_op_decrypt_result @59 gpgme_op_decrypt_start @60 gpgme_op_decrypt_verify @61 gpgme_op_decrypt_verify_start @62 gpgme_op_delete @63 gpgme_op_delete_start @64 gpgme_op_edit @65 gpgme_op_edit_start @66 gpgme_op_encrypt @67 gpgme_op_encrypt_result @68 gpgme_op_encrypt_sign @69 gpgme_op_encrypt_sign_start @70 gpgme_op_encrypt_start @71 gpgme_op_export @72 gpgme_op_export_ext @73 gpgme_op_export_ext_start @74 gpgme_op_export_start @75 gpgme_op_genkey @76 gpgme_op_genkey_result @77 gpgme_op_genkey_start @78 gpgme_get_key @79 gpgme_op_import @80 gpgme_op_import_result @81 gpgme_op_import_start @82 gpgme_op_keylist_end @83 gpgme_op_keylist_ext_start @84 gpgme_op_keylist_next @85 gpgme_op_keylist_result @86 gpgme_op_keylist_start @87 gpgme_op_sign @88 gpgme_op_sign_result @89 gpgme_op_sign_start @90 gpgme_op_trustlist_end @91 gpgme_op_trustlist_next @92 gpgme_op_trustlist_start @93 gpgme_op_verify @94 gpgme_op_verify_result @95 gpgme_op_verify_start @96 gpgme_wait @97 gpgme_data_new_with_read_cb @98 gpgme_data_rewind @99 gpgme_get_sig_status @100 gpgme_get_sig_string_attr @101 gpgme_get_sig_ulong_attr @102 gpgme_get_sig_key @103 gpgme_key_get_string_attr @104 gpgme_key_get_ulong_attr @105 gpgme_key_sig_get_string_attr @106 gpgme_key_sig_get_ulong_attr @107 gpgme_op_import_ext @108 gpgme_trust_item_get_int_attr @109 gpgme_trust_item_get_string_attr @110 gpgme_trust_item_release @111 gpgme_set_engine_info @112 gpgme_ctx_get_engine_info @113 gpgme_ctx_set_engine_info @114 gpgme_data_set_file_name @115 gpgme_data_get_file_name @116 gpgme_sig_notation_clear @117 gpgme_sig_notation_add @118 gpgme_sig_notation_get @119 gpgme_free @120 gpgme_get_giochannel @121 gpgme_get_fdptr @122 gpgme_op_getauditlog_start @123 gpgme_op_getauditlog @124 gpgme_conf_release @125 gpgme_conf_arg_new @126 gpgme_conf_arg_release @127 gpgme_conf_opt_change @128 gpgme_op_conf_load @129 gpgme_op_conf_save @130 gpgme_cancel_async @131 gpgme_op_assuan_result @132 gpgme_op_assuan_transact_start @133 gpgme_op_assuan_transact @134 gpgme_check_version_internal @135 gpgme_io_read @136 gpgme_io_write @137 gpgme_result_ref @138 gpgme_result_unref @139 gpgme_op_import_keys @140 gpgme_op_import_keys_start @141 gpgme_op_export_keys @142 gpgme_op_export_keys_start @143 gpgme_op_assuan_transact_ext @144 gpgme_wait_ext @145 gpgme_op_vfs_mount_result @146 gpgme_op_vfs_mount @147 gpgme_op_vfs_create @148 gpgme_key_from_uid @149 gpgme_set_sub_protocol @150 gpgme_get_sub_protocol @151 gpgme_op_passwd_start @152 gpgme_op_passwd @153 gpgme_err_code_from_syserror @154 gpgme_err_set_errno @155 gpgme_set_global_flag @156 gpgme_io_writen @157 gpgme_set_pinentry_mode @158 gpgme_get_pinentry_mode @159 gpgme_signers_count @160 gpgme_data_identify @161 gpgme_get_dirinfo @162 gpgme_op_spawn_start @163 gpgme_op_spawn @164 gpgme_set_offline @165 gpgme_get_offline @166 gpgme_set_status_cb @167 gpgme_get_status_cb @168 gpgme_pubkey_algo_string @169 gpgme_set_ctx_flag @170 gpgme_data_set_flag @171 gpgme_op_createkey_start @172 gpgme_op_createkey @173 gpgme_op_createsubkey_start @174 gpgme_op_createsubkey @175 gpgme_op_adduid_start @176 gpgme_op_adduid @177 gpgme_op_revuid_start @178 gpgme_op_revuid @179 gpgme_op_keysign_start @180 gpgme_op_keysign @181 gpgme_op_tofu_policy_start @182 gpgme_op_tofu_policy @183 gpgme_op_interact_start @184 gpgme_op_interact @185 gpgme_addrspec_from_uid @186 gpgme_set_sender @187 gpgme_get_sender @188 gpgme_op_query_swdb @189 gpgme_op_query_swdb_result @190 gpgme_get_ctx_flag @191 gpgme_op_keylist_from_data_start @192 gpgme_op_set_uid_flag_start @193 gpgme_op_set_uid_flag @194 gpgme_op_decrypt_ext @195 gpgme_op_decrypt_ext_start @196 gpgme_op_delete_ext @197 gpgme_op_delete_ext_start @198 gpgme_op_conf_dir @199 + gpgme_op_encrypt_ext @200 + gpgme_op_encrypt_ext_start @201 + gpgme_op_encrypt_sign_ext @202 + gpgme_op_encrypt_sign_ext_start @203 + ; END diff --git a/src/gpgme.h.in b/src/gpgme.h.in index e3198798..8aba5baa 100644 --- a/src/gpgme.h.in +++ b/src/gpgme.h.in @@ -1,2762 +1,2786 @@ /* gpgme.h - Public interface to GnuPG Made Easy. -*- c -*- * Copyright (C) 2000 Werner Koch (dd9jn) * Copyright (C) 2001-2017 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * * Generated from gpgme.h.in for @GPGME_CONFIG_HOST@. */ #ifndef GPGME_H #define GPGME_H /* Include stdio.h for the FILE type definition. */ #include #include #include #ifdef __cplusplus extern "C" { #if 0 /*(Make Emacsen's auto-indent happy.)*/ } #endif #endif /* __cplusplus */ /* The version of this header should match the one of the library. Do not use this symbol in your application, use gpgme_check_version instead. The purpose of this macro is to let autoconf (using the AM_PATH_GPGME macro) check that this header matches the installed library. */ #define GPGME_VERSION "@PACKAGE_VERSION@" /* The version number of this header. It may be used to handle minor API incompatibilities. */ #define GPGME_VERSION_NUMBER @VERSION_NUMBER@ /* System specific typedefs. */ @INSERT__TYPEDEFS_FOR_GPGME_H@ /* * Check for compiler features. */ #ifdef GPGRT_INLINE # define _GPGME_INLINE GPGRT_INLINE #elif defined(__GNUC__) # define _GPGME_INLINE __inline__ #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L # define _GPGME_INLINE inline #else # define _GPGME_INLINE #endif /* The deprecated macro takes the version number of GPGME which * introduced the deprecation as parameter for documentation. */ #ifdef GPGRT_ATTR_DEPRECATED # define _GPGME_DEPRECATED(a,b) GPGRT_ATTR_DEPRECATED #elif defined(__GNUC__) # define _GPGME_GCC_VERSION (__GNUC__ * 10000 \ + __GNUC_MINOR__ * 100 \ + __GNUC_PATCHLEVEL__) # if _GPGME_GCC_VERSION > 30100 # define _GPGME_DEPRECATED(a,b) __attribute__ ((__deprecated__)) # else # define _GPGME_DEPRECATED(a,b) # endif #else # define _GPGME_DEPRECATED(a,b) #endif /* The macro _GPGME_DEPRECATED_OUTSIDE_GPGME suppresses warnings for fields we must access in GPGME for ABI compatibility. */ #ifdef _GPGME_IN_GPGME #define _GPGME_DEPRECATED_OUTSIDE_GPGME(a,b) #else #define _GPGME_DEPRECATED_OUTSIDE_GPGME(a,b) _GPGME_DEPRECATED(a,b) #endif /* Check for a matching _FILE_OFFSET_BITS definition. */ #if @NEED__FILE_OFFSET_BITS@ #ifndef _FILE_OFFSET_BITS #error GPGME was compiled with _FILE_OFFSET_BITS = @NEED__FILE_OFFSET_BITS@, please see the section "Largefile support (LFS)" in the GPGME manual. #else #if (_FILE_OFFSET_BITS) != (@NEED__FILE_OFFSET_BITS@) #error GPGME was compiled with a different value for _FILE_OFFSET_BITS, namely @NEED__FILE_OFFSET_BITS@, please see the section "Largefile support (LFS)" in the GPGME manual. #endif #endif #endif /* * Some opaque data types used by GPGME. */ /* The context holds some global state and configuration options, as well as the results of a crypto operation. */ struct gpgme_context; typedef struct gpgme_context *gpgme_ctx_t; /* The data object is used by GPGME to exchange arbitrary data. */ struct gpgme_data; typedef struct gpgme_data *gpgme_data_t; /* * Wrappers for the libgpg-error library. */ typedef gpg_error_t gpgme_error_t; typedef gpg_err_code_t gpgme_err_code_t; typedef gpg_err_source_t gpgme_err_source_t; static _GPGME_INLINE gpgme_error_t gpgme_err_make (gpgme_err_source_t source, gpgme_err_code_t code) { return gpg_err_make (source, code); } /* The user can define GPGME_ERR_SOURCE_DEFAULT before including this file to specify a default source for gpgme_error. */ #ifndef GPGME_ERR_SOURCE_DEFAULT #define GPGME_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_USER_1 #endif static _GPGME_INLINE gpgme_error_t gpgme_error (gpgme_err_code_t code) { return gpgme_err_make (GPGME_ERR_SOURCE_DEFAULT, code); } static _GPGME_INLINE gpgme_err_code_t gpgme_err_code (gpgme_error_t err) { return gpg_err_code (err); } static _GPGME_INLINE gpgme_err_source_t gpgme_err_source (gpgme_error_t err) { return gpg_err_source (err); } /* Return a pointer to a string containing a description of the error code in the error value ERR. This function is not thread safe. */ const char *gpgme_strerror (gpgme_error_t err); /* Return the error string for ERR in the user-supplied buffer BUF of size BUFLEN. This function is, in contrast to gpg_strerror, thread-safe if a thread-safe strerror_r() function is provided by the system. If the function succeeds, 0 is returned and BUF contains the string describing the error. If the buffer was not large enough, ERANGE is returned and BUF contains as much of the beginning of the error string as fits into the buffer. */ int gpgme_strerror_r (gpg_error_t err, char *buf, size_t buflen); /* Return a pointer to a string containing a description of the error source in the error value ERR. */ const char *gpgme_strsource (gpgme_error_t err); /* Retrieve the error code for the system error ERR. This returns GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report this). */ gpgme_err_code_t gpgme_err_code_from_errno (int err); /* Retrieve the system error for the error code CODE. This returns 0 if CODE is not a system error code. */ int gpgme_err_code_to_errno (gpgme_err_code_t code); /* Retrieve the error code directly from the ERRNO variable. This returns GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report this) and GPG_ERR_MISSING_ERRNO if ERRNO has the value 0. */ gpgme_err_code_t gpgme_err_code_from_syserror (void); /* Set the ERRNO variable. This function is the preferred way to set ERRNO due to peculiarities on WindowsCE. */ void gpgme_err_set_errno (int err); /* Return an error value with the error source SOURCE and the system error ERR. FIXME: Should be inline. */ gpgme_error_t gpgme_err_make_from_errno (gpgme_err_source_t source, int err); /* Return an error value with the system error ERR. FIXME: Should be inline. */ gpgme_error_t gpgme_error_from_errno (int err); static _GPGME_INLINE gpgme_error_t gpgme_error_from_syserror (void) { return gpgme_error (gpgme_err_code_from_syserror ()); } /* * Various constants and types */ /* The possible encoding mode of gpgme_data_t objects. */ typedef enum { GPGME_DATA_ENCODING_NONE = 0, /* Not specified. */ GPGME_DATA_ENCODING_BINARY = 1, GPGME_DATA_ENCODING_BASE64 = 2, GPGME_DATA_ENCODING_ARMOR = 3, /* Either PEM or OpenPGP Armor. */ GPGME_DATA_ENCODING_URL = 4, /* LF delimited URL list. */ GPGME_DATA_ENCODING_URLESC = 5, /* Ditto, but percent escaped. */ GPGME_DATA_ENCODING_URL0 = 6, /* Nul delimited URL list. */ GPGME_DATA_ENCODING_MIME = 7 /* Data is a MIME part. */ } gpgme_data_encoding_t; /* Known data types. */ typedef enum { GPGME_DATA_TYPE_INVALID = 0, /* Not detected. */ GPGME_DATA_TYPE_UNKNOWN = 1, GPGME_DATA_TYPE_PGP_SIGNED = 0x10, GPGME_DATA_TYPE_PGP_ENCRYPTED= 0x11, GPGME_DATA_TYPE_PGP_OTHER = 0x12, GPGME_DATA_TYPE_PGP_KEY = 0x13, GPGME_DATA_TYPE_PGP_SIGNATURE= 0x18, /* Detached signature */ GPGME_DATA_TYPE_CMS_SIGNED = 0x20, GPGME_DATA_TYPE_CMS_ENCRYPTED= 0x21, GPGME_DATA_TYPE_CMS_OTHER = 0x22, GPGME_DATA_TYPE_X509_CERT = 0x23, GPGME_DATA_TYPE_PKCS12 = 0x24, } gpgme_data_type_t; /* Public key algorithms. */ typedef enum { GPGME_PK_RSA = 1, GPGME_PK_RSA_E = 2, GPGME_PK_RSA_S = 3, GPGME_PK_ELG_E = 16, GPGME_PK_DSA = 17, GPGME_PK_ECC = 18, GPGME_PK_ELG = 20, GPGME_PK_ECDSA = 301, GPGME_PK_ECDH = 302, GPGME_PK_EDDSA = 303 } gpgme_pubkey_algo_t; /* Hash algorithms (the values match those from libgcrypt). */ typedef enum { GPGME_MD_NONE = 0, GPGME_MD_MD5 = 1, GPGME_MD_SHA1 = 2, GPGME_MD_RMD160 = 3, GPGME_MD_MD2 = 5, GPGME_MD_TIGER = 6, /* TIGER/192. */ GPGME_MD_HAVAL = 7, /* HAVAL, 5 pass, 160 bit. */ GPGME_MD_SHA256 = 8, GPGME_MD_SHA384 = 9, GPGME_MD_SHA512 = 10, GPGME_MD_SHA224 = 11, GPGME_MD_MD4 = 301, GPGME_MD_CRC32 = 302, GPGME_MD_CRC32_RFC1510 = 303, GPGME_MD_CRC24_RFC2440 = 304 } gpgme_hash_algo_t; /* The available signature modes. */ typedef enum { GPGME_SIG_MODE_NORMAL = 0, GPGME_SIG_MODE_DETACH = 1, GPGME_SIG_MODE_CLEAR = 2 } gpgme_sig_mode_t; /* The available validities for a trust item or key. */ typedef enum { GPGME_VALIDITY_UNKNOWN = 0, GPGME_VALIDITY_UNDEFINED = 1, GPGME_VALIDITY_NEVER = 2, GPGME_VALIDITY_MARGINAL = 3, GPGME_VALIDITY_FULL = 4, GPGME_VALIDITY_ULTIMATE = 5 } gpgme_validity_t; /* The TOFU policies. */ typedef enum { GPGME_TOFU_POLICY_NONE = 0, GPGME_TOFU_POLICY_AUTO = 1, GPGME_TOFU_POLICY_GOOD = 2, GPGME_TOFU_POLICY_UNKNOWN = 3, GPGME_TOFU_POLICY_BAD = 4, GPGME_TOFU_POLICY_ASK = 5 } gpgme_tofu_policy_t; /* The key origin values. */ typedef enum { GPGME_KEYORG_UNKNOWN = 0, GPGME_KEYORG_KS = 1, GPGME_KEYORG_DANE = 3, GPGME_KEYORG_WKD = 4, GPGME_KEYORG_URL = 5, GPGME_KEYORG_FILE = 6, GPGME_KEYORG_SELF = 7, GPGME_KEYORG_OTHER = 31 } gpgme_keyorg_t; /* The available protocols. */ typedef enum { GPGME_PROTOCOL_OpenPGP = 0, /* The default mode. */ GPGME_PROTOCOL_CMS = 1, GPGME_PROTOCOL_GPGCONF = 2, /* Special code for gpgconf. */ GPGME_PROTOCOL_ASSUAN = 3, /* Low-level access to an Assuan server. */ GPGME_PROTOCOL_G13 = 4, GPGME_PROTOCOL_UISERVER= 5, GPGME_PROTOCOL_SPAWN = 6, /* Direct access to any program. */ GPGME_PROTOCOL_DEFAULT = 254, GPGME_PROTOCOL_UNKNOWN = 255 } gpgme_protocol_t; /* Convenience macro for the surprisingly mixed spelling. */ #define GPGME_PROTOCOL_OPENPGP GPGME_PROTOCOL_OpenPGP /* The available keylist mode flags. */ #define GPGME_KEYLIST_MODE_LOCAL 1 #define GPGME_KEYLIST_MODE_EXTERN 2 #define GPGME_KEYLIST_MODE_SIGS 4 #define GPGME_KEYLIST_MODE_SIG_NOTATIONS 8 #define GPGME_KEYLIST_MODE_WITH_SECRET 16 #define GPGME_KEYLIST_MODE_WITH_TOFU 32 #define GPGME_KEYLIST_MODE_EPHEMERAL 128 #define GPGME_KEYLIST_MODE_VALIDATE 256 typedef unsigned int gpgme_keylist_mode_t; /* The pinentry modes. */ typedef enum { GPGME_PINENTRY_MODE_DEFAULT = 0, GPGME_PINENTRY_MODE_ASK = 1, GPGME_PINENTRY_MODE_CANCEL = 2, GPGME_PINENTRY_MODE_ERROR = 3, GPGME_PINENTRY_MODE_LOOPBACK = 4 } gpgme_pinentry_mode_t; /* The available export mode flags. */ #define GPGME_EXPORT_MODE_EXTERN 2 #define GPGME_EXPORT_MODE_MINIMAL 4 #define GPGME_EXPORT_MODE_SECRET 16 #define GPGME_EXPORT_MODE_RAW 32 #define GPGME_EXPORT_MODE_PKCS12 64 typedef unsigned int gpgme_export_mode_t; /* Flags for the audit log functions. */ #define GPGME_AUDITLOG_HTML 1 #define GPGME_AUDITLOG_WITH_HELP 128 /* The available signature notation flags. */ #define GPGME_SIG_NOTATION_HUMAN_READABLE 1 #define GPGME_SIG_NOTATION_CRITICAL 2 typedef unsigned int gpgme_sig_notation_flags_t; /* An object to hold information about notation data. This structure * shall be considered read-only and an application must not allocate * such a structure on its own. */ struct _gpgme_sig_notation { struct _gpgme_sig_notation *next; /* If NAME is a null pointer, then VALUE contains a policy URL rather than a notation. */ char *name; /* The value of the notation data. */ char *value; /* The length of the name of the notation data. */ int name_len; /* The length of the value of the notation data. */ int value_len; /* The accumulated flags. */ gpgme_sig_notation_flags_t flags; /* Notation data is human-readable. */ unsigned int human_readable : 1; /* Notation data is critical. */ unsigned int critical : 1; /* Internal to GPGME, do not use. */ int _unused : 30; }; typedef struct _gpgme_sig_notation *gpgme_sig_notation_t; /* * Public structures. */ /* The engine information structure. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_engine_info { struct _gpgme_engine_info *next; /* The protocol ID. */ gpgme_protocol_t protocol; /* The file name of the engine binary. */ char *file_name; /* The version string of the installed engine. */ char *version; /* The minimum version required for GPGME. */ const char *req_version; /* The home directory used, or NULL if default. */ char *home_dir; }; typedef struct _gpgme_engine_info *gpgme_engine_info_t; /* An object with TOFU information. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_tofu_info { struct _gpgme_tofu_info *next; /* The TOFU validity: * 0 := conflict * 1 := key without history * 2 := key with too little history * 3 := key with enough history for basic trust * 4 := key with a lot of history */ unsigned int validity : 3; /* The TOFU policy (gpgme_tofu_policy_t). */ unsigned int policy : 4; unsigned int _rfu : 25; /* Number of signatures seen for this binding. Capped at USHRT_MAX. */ unsigned short signcount; /* Number of encryptions done with this binding. Capped at USHRT_MAX. */ unsigned short encrcount; /* Number of seconds since Epoch when the first and the most * recently seen message were verified/decrypted. 0 means unknown. */ unsigned long signfirst; unsigned long signlast; unsigned long encrfirst; unsigned long encrlast; /* If non-NULL a human readable string summarizing the TOFU data. */ char *description; }; typedef struct _gpgme_tofu_info *gpgme_tofu_info_t; /* A subkey from a key. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_subkey { struct _gpgme_subkey *next; /* True if subkey is revoked. */ unsigned int revoked : 1; /* True if subkey is expired. */ unsigned int expired : 1; /* True if subkey is disabled. */ unsigned int disabled : 1; /* True if subkey is invalid. */ unsigned int invalid : 1; /* True if subkey can be used for encryption. */ unsigned int can_encrypt : 1; /* True if subkey can be used for signing. */ unsigned int can_sign : 1; /* True if subkey can be used for certification. */ unsigned int can_certify : 1; /* True if subkey is secret. */ unsigned int secret : 1; /* True if subkey can be used for authentication. */ unsigned int can_authenticate : 1; /* True if subkey is qualified for signatures according to German law. */ unsigned int is_qualified : 1; /* True if the secret key is stored on a smart card. */ unsigned int is_cardkey : 1; /* True if the key is compliant to the de-vs mode. */ unsigned int is_de_vs : 1; /* Internal to GPGME, do not use. */ unsigned int _unused : 20; /* Public key algorithm supported by this subkey. */ gpgme_pubkey_algo_t pubkey_algo; /* Length of the subkey. */ unsigned int length; /* The key ID of the subkey. */ char *keyid; /* Internal to GPGME, do not use. */ char _keyid[16 + 1]; /* The fingerprint of the subkey in hex digit form. */ char *fpr; /* The creation timestamp, -1 if invalid, 0 if not available. */ long int timestamp; /* The expiration timestamp, 0 if the subkey does not expire. */ long int expires; /* The serial number of a smart card holding this key or NULL. */ char *card_number; /* The name of the curve for ECC algorithms or NULL. */ char *curve; /* The keygrip of the subkey in hex digit form or NULL if not availabale. */ char *keygrip; }; typedef struct _gpgme_subkey *gpgme_subkey_t; /* A signature on a user ID. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_key_sig { struct _gpgme_key_sig *next; /* True if the signature is a revocation signature. */ unsigned int revoked : 1; /* True if the signature is expired. */ unsigned int expired : 1; /* True if the signature is invalid. */ unsigned int invalid : 1; /* True if the signature should be exported. */ unsigned int exportable : 1; /* Internal to GPGME, do not use. */ unsigned int _unused : 28; /* The public key algorithm used to create the signature. */ gpgme_pubkey_algo_t pubkey_algo; /* The key ID of key used to create the signature. */ char *keyid; /* Internal to GPGME, do not use. */ char _keyid[16 + 1]; /* The creation timestamp, -1 if invalid, 0 if not available. */ long int timestamp; /* The expiration timestamp, 0 if the subkey does not expire. */ long int expires; /* Same as in gpgme_signature_t. */ gpgme_error_t status; #ifdef __cplusplus unsigned int _obsolete_class _GPGME_DEPRECATED(0,4); #else /* Must be set to SIG_CLASS below. */ unsigned int class _GPGME_DEPRECATED_OUTSIDE_GPGME(0,4); #endif /* The user ID string. */ char *uid; /* The name part of the user ID. */ char *name; /* The email part of the user ID. */ char *email; /* The comment part of the user ID. */ char *comment; /* Crypto backend specific signature class. */ unsigned int sig_class; /* Notation data and policy URLs. */ gpgme_sig_notation_t notations; /* Internal to GPGME, do not use. */ gpgme_sig_notation_t _last_notation; }; typedef struct _gpgme_key_sig *gpgme_key_sig_t; /* An user ID from a key. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_user_id { struct _gpgme_user_id *next; /* True if the user ID is revoked. */ unsigned int revoked : 1; /* True if the user ID is invalid. */ unsigned int invalid : 1; /* Internal to GPGME, do not use. */ unsigned int _unused : 25; /* Origin of this user ID. */ unsigned int origin : 5; /* The validity of the user ID. */ gpgme_validity_t validity; /* The user ID string. */ char *uid; /* The name part of the user ID. */ char *name; /* The email part of the user ID. */ char *email; /* The comment part of the user ID. */ char *comment; /* The signatures of the user ID. */ gpgme_key_sig_t signatures; /* Internal to GPGME, do not use. */ gpgme_key_sig_t _last_keysig; /* The mail address (addr-spec from RFC5322) of the UID string. * This is general the same as the EMAIL part of this struct but * might be slightly different. If no mail address is available * NULL is stored. */ char *address; /* The malloced TOFU information or NULL. */ gpgme_tofu_info_t tofu; /* Time of the last refresh of this user id. 0 if unknown. */ unsigned long last_update; }; typedef struct _gpgme_user_id *gpgme_user_id_t; /* A key from the keyring. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_key { /* Internal to GPGME, do not use. */ unsigned int _refs; /* True if key is revoked. */ unsigned int revoked : 1; /* True if key is expired. */ unsigned int expired : 1; /* True if key is disabled. */ unsigned int disabled : 1; /* True if key is invalid. */ unsigned int invalid : 1; /* True if key can be used for encryption. */ unsigned int can_encrypt : 1; /* True if key can be used for signing. */ unsigned int can_sign : 1; /* True if key can be used for certification. */ unsigned int can_certify : 1; /* True if key is secret. */ unsigned int secret : 1; /* True if key can be used for authentication. */ unsigned int can_authenticate : 1; /* True if subkey is qualified for signatures according to German law. */ unsigned int is_qualified : 1; /* Internal to GPGME, do not use. */ unsigned int _unused : 17; /* Origin of this key. */ unsigned int origin : 5; /* This is the protocol supported by this key. */ gpgme_protocol_t protocol; /* If protocol is GPGME_PROTOCOL_CMS, this string contains the issuer serial. */ char *issuer_serial; /* If protocol is GPGME_PROTOCOL_CMS, this string contains the issuer name. */ char *issuer_name; /* If protocol is GPGME_PROTOCOL_CMS, this string contains the chain ID. */ char *chain_id; /* If protocol is GPGME_PROTOCOL_OpenPGP, this field contains the owner trust. */ gpgme_validity_t owner_trust; /* The subkeys of the key. */ gpgme_subkey_t subkeys; /* The user IDs of the key. */ gpgme_user_id_t uids; /* Internal to GPGME, do not use. */ gpgme_subkey_t _last_subkey; /* Internal to GPGME, do not use. */ gpgme_user_id_t _last_uid; /* The keylist mode that was active when listing the key. */ gpgme_keylist_mode_t keylist_mode; /* This field gives the fingerprint of the primary key. Note that * this is a copy of the FPR of the first subkey. We need it here * to allow for an incomplete key object. */ char *fpr; /* Time of the last refresh of the entire key. 0 if unknown. */ unsigned long last_update; }; typedef struct _gpgme_key *gpgme_key_t; /* An invalid key object. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_invalid_key { struct _gpgme_invalid_key *next; /* The string used to request the key. Despite the name this may * not be a fingerprint. */ char *fpr; /* The error code. */ gpgme_error_t reason; }; typedef struct _gpgme_invalid_key *gpgme_invalid_key_t; /* * Types for callback functions. */ /* Request a passphrase from the user. */ typedef gpgme_error_t (*gpgme_passphrase_cb_t) (void *hook, const char *uid_hint, const char *passphrase_info, int prev_was_bad, int fd); /* Inform the user about progress made. */ typedef void (*gpgme_progress_cb_t) (void *opaque, const char *what, int type, int current, int total); /* Status messages from gpg. */ typedef gpgme_error_t (*gpgme_status_cb_t) (void *opaque, const char *keyword, const char *args); /* Interact with the user about an edit operation. */ typedef gpgme_error_t (*gpgme_interact_cb_t) (void *opaque, const char *keyword, const char *args, int fd); /* * Context management functions. */ /* Create a new context and return it in CTX. */ gpgme_error_t gpgme_new (gpgme_ctx_t *ctx); /* Release the context CTX. */ void gpgme_release (gpgme_ctx_t ctx); /* Set the flag NAME for CTX to VALUE. */ gpgme_error_t gpgme_set_ctx_flag (gpgme_ctx_t ctx, const char *name, const char *value); /* Get the value of the flag NAME from CTX. */ const char *gpgme_get_ctx_flag (gpgme_ctx_t ctx, const char *name); /* Set the protocol to be used by CTX to PROTO. */ gpgme_error_t gpgme_set_protocol (gpgme_ctx_t ctx, gpgme_protocol_t proto); /* Get the protocol used with CTX */ gpgme_protocol_t gpgme_get_protocol (gpgme_ctx_t ctx); /* Set the crypto protocol to be used by CTX to PROTO. gpgme_set_protocol actually sets the backend engine. This sets the crypto protocol used in engines that support more than one crypto prococol (for example, an UISERVER can support OpenPGP and CMS). This is reset to the default with gpgme_set_protocol. */ gpgme_error_t gpgme_set_sub_protocol (gpgme_ctx_t ctx, gpgme_protocol_t proto); /* Get the sub protocol. */ gpgme_protocol_t gpgme_get_sub_protocol (gpgme_ctx_t ctx); /* Get the string describing protocol PROTO, or NULL if invalid. */ const char *gpgme_get_protocol_name (gpgme_protocol_t proto); /* If YES is non-zero, enable armor mode in CTX, disable it otherwise. */ void gpgme_set_armor (gpgme_ctx_t ctx, int yes); /* Return non-zero if armor mode is set in CTX. */ int gpgme_get_armor (gpgme_ctx_t ctx); /* If YES is non-zero, enable text mode in CTX, disable it otherwise. */ void gpgme_set_textmode (gpgme_ctx_t ctx, int yes); /* Return non-zero if text mode is set in CTX. */ int gpgme_get_textmode (gpgme_ctx_t ctx); /* If YES is non-zero, enable offline mode in CTX, disable it otherwise. */ void gpgme_set_offline (gpgme_ctx_t ctx, int yes); /* Return non-zero if offline mode is set in CTX. */ int gpgme_get_offline (gpgme_ctx_t ctx); /* Use whatever the default of the backend crypto engine is. */ #define GPGME_INCLUDE_CERTS_DEFAULT -256 /* Include up to NR_OF_CERTS certificates in an S/MIME message. */ void gpgme_set_include_certs (gpgme_ctx_t ctx, int nr_of_certs); /* Return the number of certs to include in an S/MIME message. */ int gpgme_get_include_certs (gpgme_ctx_t ctx); /* Set keylist mode in CTX to MODE. */ gpgme_error_t gpgme_set_keylist_mode (gpgme_ctx_t ctx, gpgme_keylist_mode_t mode); /* Get keylist mode in CTX. */ gpgme_keylist_mode_t gpgme_get_keylist_mode (gpgme_ctx_t ctx); /* Set the pinentry mode for CTX to MODE. */ gpgme_error_t gpgme_set_pinentry_mode (gpgme_ctx_t ctx, gpgme_pinentry_mode_t mode); /* Get the pinentry mode of CTX. */ gpgme_pinentry_mode_t gpgme_get_pinentry_mode (gpgme_ctx_t ctx); /* Set the passphrase callback function in CTX to CB. HOOK_VALUE is passed as first argument to the passphrase callback function. */ void gpgme_set_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t cb, void *hook_value); /* Get the current passphrase callback function in *CB and the current hook value in *HOOK_VALUE. */ void gpgme_get_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t *cb, void **hook_value); /* Set the progress callback function in CTX to CB. HOOK_VALUE is passed as first argument to the progress callback function. */ void gpgme_set_progress_cb (gpgme_ctx_t c, gpgme_progress_cb_t cb, void *hook_value); /* Get the current progress callback function in *CB and the current hook value in *HOOK_VALUE. */ void gpgme_get_progress_cb (gpgme_ctx_t ctx, gpgme_progress_cb_t *cb, void **hook_value); /* Set the status callback function in CTX to CB. HOOK_VALUE is passed as first argument to the status callback function. */ void gpgme_set_status_cb (gpgme_ctx_t c, gpgme_status_cb_t cb, void *hook_value); /* Get the current status callback function in *CB and the current hook value in *HOOK_VALUE. */ void gpgme_get_status_cb (gpgme_ctx_t ctx, gpgme_status_cb_t *cb, void **hook_value); /* This function sets the locale for the context CTX, or the default locale if CTX is a null pointer. */ gpgme_error_t gpgme_set_locale (gpgme_ctx_t ctx, int category, const char *value); /* Get the information about the configured engines. A pointer to the first engine in the statically allocated linked list is returned. The returned data is valid until the next gpgme_ctx_set_engine_info. */ gpgme_engine_info_t gpgme_ctx_get_engine_info (gpgme_ctx_t ctx); /* Set the engine info for the context CTX, protocol PROTO, to the file name FILE_NAME and the home directory HOME_DIR. */ gpgme_error_t gpgme_ctx_set_engine_info (gpgme_ctx_t ctx, gpgme_protocol_t proto, const char *file_name, const char *home_dir); /* Delete all signers from CTX. */ void gpgme_signers_clear (gpgme_ctx_t ctx); /* Add KEY to list of signers in CTX. */ gpgme_error_t gpgme_signers_add (gpgme_ctx_t ctx, const gpgme_key_t key); /* Return the number of signers in CTX. */ unsigned int gpgme_signers_count (const gpgme_ctx_t ctx); /* Return the SEQth signer's key in CTX. */ gpgme_key_t gpgme_signers_enum (const gpgme_ctx_t ctx, int seq); /* Clear all notation data from the context. */ void gpgme_sig_notation_clear (gpgme_ctx_t ctx); /* Add the human-readable notation data with name NAME and value VALUE to the context CTX, using the flags FLAGS. If NAME is NULL, then VALUE should be a policy URL. The flag GPGME_SIG_NOTATION_HUMAN_READABLE is forced to be true for notation data, and false for policy URLs. */ gpgme_error_t gpgme_sig_notation_add (gpgme_ctx_t ctx, const char *name, const char *value, gpgme_sig_notation_flags_t flags); /* Get the sig notations for this context. */ gpgme_sig_notation_t gpgme_sig_notation_get (gpgme_ctx_t ctx); /* Store a sender address in the context. */ gpgme_error_t gpgme_set_sender (gpgme_ctx_t ctx, const char *address); /* Get the sender address from the context. */ const char *gpgme_get_sender (gpgme_ctx_t ctx); /* * Run control. */ /* The type of an I/O callback function. */ typedef gpgme_error_t (*gpgme_io_cb_t) (void *data, int fd); /* The type of a function that can register FNC as the I/O callback function for the file descriptor FD with direction dir (0: for writing, 1: for reading). FNC_DATA should be passed as DATA to FNC. The function should return a TAG suitable for the corresponding gpgme_remove_io_cb_t, and an error value. */ typedef gpgme_error_t (*gpgme_register_io_cb_t) (void *data, int fd, int dir, gpgme_io_cb_t fnc, void *fnc_data, void **tag); /* The type of a function that can remove a previously registered I/O callback function given TAG as returned by the register function. */ typedef void (*gpgme_remove_io_cb_t) (void *tag); typedef enum { GPGME_EVENT_START, GPGME_EVENT_DONE, GPGME_EVENT_NEXT_KEY, GPGME_EVENT_NEXT_TRUSTITEM } gpgme_event_io_t; struct gpgme_io_event_done_data { /* A fatal IPC error or an operational error in state-less protocols. */ gpgme_error_t err; /* An operational errors in session-based protocols. */ gpgme_error_t op_err; }; typedef struct gpgme_io_event_done_data *gpgme_io_event_done_data_t; /* The type of a function that is called when a context finished an operation. */ typedef void (*gpgme_event_io_cb_t) (void *data, gpgme_event_io_t type, void *type_data); struct gpgme_io_cbs { gpgme_register_io_cb_t add; void *add_priv; gpgme_remove_io_cb_t remove; gpgme_event_io_cb_t event; void *event_priv; }; typedef struct gpgme_io_cbs *gpgme_io_cbs_t; /* Set the I/O callback functions in CTX to IO_CBS. */ void gpgme_set_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs); /* Get the current I/O callback functions. */ void gpgme_get_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs); /* Wrappers around the internal I/O functions for use with gpgme_passphrase_cb_t and gpgme_interact_cb_t. */ @API__SSIZE_T@ gpgme_io_read (int fd, void *buffer, size_t count); @API__SSIZE_T@ gpgme_io_write (int fd, const void *buffer, size_t count); int gpgme_io_writen (int fd, const void *buffer, size_t count); /* Process the pending operation and, if HANG is non-zero, wait for the pending operation to finish. */ gpgme_ctx_t gpgme_wait (gpgme_ctx_t ctx, gpgme_error_t *status, int hang); gpgme_ctx_t gpgme_wait_ext (gpgme_ctx_t ctx, gpgme_error_t *status, gpgme_error_t *op_err, int hang); /* Cancel a pending asynchronous operation. */ gpgme_error_t gpgme_cancel (gpgme_ctx_t ctx); /* Cancel a pending operation asynchronously. */ gpgme_error_t gpgme_cancel_async (gpgme_ctx_t ctx); /* * Functions to handle data objects. */ /* Read up to SIZE bytes into buffer BUFFER from the data object with the handle HANDLE. Return the number of characters read, 0 on EOF and -1 on error. If an error occurs, errno is set. */ typedef @API__SSIZE_T@ (*gpgme_data_read_cb_t) (void *handle, void *buffer, size_t size); /* Write up to SIZE bytes from buffer BUFFER to the data object with the handle HANDLE. Return the number of characters written, or -1 on error. If an error occurs, errno is set. */ typedef @API__SSIZE_T@ (*gpgme_data_write_cb_t) (void *handle, const void *buffer, size_t size); /* Set the current position from where the next read or write starts in the data object with the handle HANDLE to OFFSET, relativ to WHENCE. Returns the new offset in bytes from the beginning of the data object. */ typedef @API__OFF_T@ (*gpgme_data_seek_cb_t) (void *handle, @API__OFF_T@ offset, int whence); /* Close the data object with the handle HANDLE. */ typedef void (*gpgme_data_release_cb_t) (void *handle); struct gpgme_data_cbs { gpgme_data_read_cb_t read; gpgme_data_write_cb_t write; gpgme_data_seek_cb_t seek; gpgme_data_release_cb_t release; }; typedef struct gpgme_data_cbs *gpgme_data_cbs_t; /* Read up to SIZE bytes into buffer BUFFER from the data object with the handle DH. Return the number of characters read, 0 on EOF and -1 on error. If an error occurs, errno is set. */ @API__SSIZE_T@ gpgme_data_read (gpgme_data_t dh, void *buffer, size_t size); /* Write up to SIZE bytes from buffer BUFFER to the data object with the handle DH. Return the number of characters written, or -1 on error. If an error occurs, errno is set. */ @API__SSIZE_T@ gpgme_data_write (gpgme_data_t dh, const void *buffer, size_t size); /* Set the current position from where the next read or write starts in the data object with the handle DH to OFFSET, relativ to WHENCE. Returns the new offset in bytes from the beginning of the data object. */ @API__OFF_T@ gpgme_data_seek (gpgme_data_t dh, @API__OFF_T@ offset, int whence); /* Create a new data buffer and return it in R_DH. */ gpgme_error_t gpgme_data_new (gpgme_data_t *r_dh); /* Destroy the data buffer DH. */ void gpgme_data_release (gpgme_data_t dh); /* Create a new data buffer filled with SIZE bytes starting from BUFFER. If COPY is zero, copying is delayed until necessary, and the data is taken from the original location when needed. */ gpgme_error_t gpgme_data_new_from_mem (gpgme_data_t *r_dh, const char *buffer, size_t size, int copy); /* Destroy the data buffer DH and return a pointer to its content. The memory has be to released with gpgme_free() by the user. It's size is returned in R_LEN. */ char *gpgme_data_release_and_get_mem (gpgme_data_t dh, size_t *r_len); /* Release the memory returned by gpgme_data_release_and_get_mem() and some other functions. */ void gpgme_free (void *buffer); gpgme_error_t gpgme_data_new_from_cbs (gpgme_data_t *dh, gpgme_data_cbs_t cbs, void *handle); gpgme_error_t gpgme_data_new_from_fd (gpgme_data_t *dh, int fd); gpgme_error_t gpgme_data_new_from_stream (gpgme_data_t *dh, FILE *stream); /* Return the encoding attribute of the data buffer DH */ gpgme_data_encoding_t gpgme_data_get_encoding (gpgme_data_t dh); /* Set the encoding attribute of data buffer DH to ENC */ gpgme_error_t gpgme_data_set_encoding (gpgme_data_t dh, gpgme_data_encoding_t enc); /* Get the file name associated with the data object with handle DH, or NULL if there is none. */ char *gpgme_data_get_file_name (gpgme_data_t dh); /* Set the file name associated with the data object with handle DH to FILE_NAME. */ gpgme_error_t gpgme_data_set_file_name (gpgme_data_t dh, const char *file_name); /* Set a flag for the data object DH. See the manual for details. */ gpg_error_t gpgme_data_set_flag (gpgme_data_t dh, const char *name, const char *value); /* Try to identify the type of the data in DH. */ gpgme_data_type_t gpgme_data_identify (gpgme_data_t dh, int reserved); /* Create a new data buffer filled with the content of file FNAME. COPY must be non-zero. For delayed read, please use gpgme_data_new_from_fd or gpgme_data_new_from_stream instead. */ gpgme_error_t gpgme_data_new_from_file (gpgme_data_t *r_dh, const char *fname, int copy); /* Create a new data buffer filled with LENGTH bytes starting from OFFSET within the file FNAME or stream FP (exactly one must be non-zero). */ gpgme_error_t gpgme_data_new_from_filepart (gpgme_data_t *r_dh, const char *fname, FILE *fp, @API__OFF_T@ offset, size_t length); /* Convenience function to do a gpgme_data_seek (dh, 0, SEEK_SET). */ gpgme_error_t gpgme_data_rewind (gpgme_data_t dh); /* * Key and trust functions. */ /* Get the key with the fingerprint FPR from the crypto backend. If SECRET is true, get the secret key. */ gpgme_error_t gpgme_get_key (gpgme_ctx_t ctx, const char *fpr, gpgme_key_t *r_key, int secret); /* Create a dummy key to specify an email address. */ gpgme_error_t gpgme_key_from_uid (gpgme_key_t *key, const char *name); /* Acquire a reference to KEY. */ void gpgme_key_ref (gpgme_key_t key); /* Release a reference to KEY. If this was the last one the key is destroyed. */ void gpgme_key_unref (gpgme_key_t key); void gpgme_key_release (gpgme_key_t key); /* * Encryption. */ /* An object to return results from an encryption operation. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_encrypt_result { /* The list of invalid recipients. */ gpgme_invalid_key_t invalid_recipients; }; typedef struct _gpgme_op_encrypt_result *gpgme_encrypt_result_t; /* Retrieve a pointer to the result of the encrypt operation. */ gpgme_encrypt_result_t gpgme_op_encrypt_result (gpgme_ctx_t ctx); /* The valid encryption flags. */ typedef enum { GPGME_ENCRYPT_ALWAYS_TRUST = 1, GPGME_ENCRYPT_NO_ENCRYPT_TO = 2, GPGME_ENCRYPT_PREPARE = 4, GPGME_ENCRYPT_EXPECT_SIGN = 8, GPGME_ENCRYPT_NO_COMPRESS = 16, GPGME_ENCRYPT_SYMMETRIC = 32, GPGME_ENCRYPT_THROW_KEYIDS = 64, GPGME_ENCRYPT_WRAP = 128 } gpgme_encrypt_flags_t; /* Encrypt plaintext PLAIN within CTX for the recipients RECP and store the resulting ciphertext in CIPHER. */ gpgme_error_t gpgme_op_encrypt_start (gpgme_ctx_t ctx, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, - gpgme_data_t plain, gpgme_data_t cipher); + gpgme_data_t plain, + gpgme_data_t cipher); gpgme_error_t gpgme_op_encrypt (gpgme_ctx_t ctx, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, - gpgme_data_t plain, gpgme_data_t cipher); + gpgme_data_t plain, + gpgme_data_t cipher); +gpgme_error_t gpgme_op_encrypt_ext_start (gpgme_ctx_t ctx, gpgme_key_t recp[], + const char *recpstring, + gpgme_encrypt_flags_t flags, + gpgme_data_t plain, + gpgme_data_t cipher); +gpgme_error_t gpgme_op_encrypt_ext (gpgme_ctx_t ctx, gpgme_key_t recp[], + const char *recpstring, + gpgme_encrypt_flags_t flags, + gpgme_data_t plain, + gpgme_data_t cipher); /* Encrypt plaintext PLAIN within CTX for the recipients RECP and store the resulting ciphertext in CIPHER. Also sign the ciphertext with the signers in CTX. */ gpgme_error_t gpgme_op_encrypt_sign_start (gpgme_ctx_t ctx, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher); gpgme_error_t gpgme_op_encrypt_sign (gpgme_ctx_t ctx, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, - gpgme_data_t plain, gpgme_data_t cipher); + gpgme_data_t plain, + gpgme_data_t cipher); +gpgme_error_t gpgme_op_encrypt_sign_ext_start (gpgme_ctx_t ctx, + gpgme_key_t recp[], + const char *recpstring, + gpgme_encrypt_flags_t flags, + gpgme_data_t plain, + gpgme_data_t cipher); +gpgme_error_t gpgme_op_encrypt_sign_ext (gpgme_ctx_t ctx, gpgme_key_t recp[], + const char *recpstring, + gpgme_encrypt_flags_t flags, + gpgme_data_t plain, + gpgme_data_t cipher); /* * Decryption. */ /* An object to hold information about a recipient. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_recipient { struct _gpgme_recipient *next; /* The key ID of key for which the text was encrypted. */ char *keyid; /* Internal to GPGME, do not use. */ char _keyid[16 + 1]; /* The public key algorithm of the recipient key. */ gpgme_pubkey_algo_t pubkey_algo; /* The status of the recipient. */ gpgme_error_t status; }; typedef struct _gpgme_recipient *gpgme_recipient_t; /* An object to return results from a decryption operation. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_decrypt_result { char *unsupported_algorithm; /* Key should not have been used for encryption. */ unsigned int wrong_key_usage : 1; /* True if the message was encrypted in compliance to the de-vs * mode. */ unsigned int is_de_vs : 1; /* Internal to GPGME, do not use. */ int _unused : 30; gpgme_recipient_t recipients; /* The original file name of the plaintext message, if available. */ char *file_name; /* A textual representation of the session key used to decrypt the * message, if available */ char *session_key; }; typedef struct _gpgme_op_decrypt_result *gpgme_decrypt_result_t; /* Retrieve a pointer to the result of the decrypt operation. */ gpgme_decrypt_result_t gpgme_op_decrypt_result (gpgme_ctx_t ctx); /* The valid decryption flags. */ typedef enum { GPGME_DECRYPT_VERIFY = 1, GPGME_DECRYPT_UNWRAP = 128 } gpgme_decrypt_flags_t; /* Decrypt ciphertext CIPHER within CTX and store the resulting plaintext in PLAIN. */ gpgme_error_t gpgme_op_decrypt_start (gpgme_ctx_t ctx, gpgme_data_t cipher, gpgme_data_t plain); gpgme_error_t gpgme_op_decrypt (gpgme_ctx_t ctx, gpgme_data_t cipher, gpgme_data_t plain); /* Decrypt ciphertext CIPHER and make a signature verification within CTX and store the resulting plaintext in PLAIN. */ gpgme_error_t gpgme_op_decrypt_verify_start (gpgme_ctx_t ctx, gpgme_data_t cipher, gpgme_data_t plain); gpgme_error_t gpgme_op_decrypt_verify (gpgme_ctx_t ctx, gpgme_data_t cipher, gpgme_data_t plain); /* Decrypt ciphertext CIPHER within CTX and store the resulting * plaintext in PLAIN. With the flag GPGME_DECRYPT_VERIFY also do a * signature verification pn the plaintext. */ gpgme_error_t gpgme_op_decrypt_ext_start (gpgme_ctx_t ctx, gpgme_decrypt_flags_t flags, gpgme_data_t cipher, gpgme_data_t plain); gpgme_error_t gpgme_op_decrypt_ext (gpgme_ctx_t ctx, gpgme_decrypt_flags_t flags, gpgme_data_t cipher, gpgme_data_t plain); /* * Signing. */ /* An object with signatures data. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_new_signature { struct _gpgme_new_signature *next; /* The type of the signature. */ gpgme_sig_mode_t type; /* The public key algorithm used to create the signature. */ gpgme_pubkey_algo_t pubkey_algo; /* The hash algorithm used to create the signature. */ gpgme_hash_algo_t hash_algo; /* Internal to GPGME, do not use. Must be set to the same value as CLASS below. */ unsigned long _obsolete_class; /* Signature creation time. */ long int timestamp; /* The fingerprint of the signature. */ char *fpr; #ifdef __cplusplus unsigned int _obsolete_class_2; #else /* Must be set to SIG_CLASS below. */ unsigned int class _GPGME_DEPRECATED_OUTSIDE_GPGME(0,4); #endif /* Crypto backend specific signature class. */ unsigned int sig_class; }; typedef struct _gpgme_new_signature *gpgme_new_signature_t; /* An object to return results from a signing operation. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_sign_result { /* The list of invalid signers. */ gpgme_invalid_key_t invalid_signers; gpgme_new_signature_t signatures; }; typedef struct _gpgme_op_sign_result *gpgme_sign_result_t; /* Retrieve a pointer to the result of the signing operation. */ gpgme_sign_result_t gpgme_op_sign_result (gpgme_ctx_t ctx); /* Sign the plaintext PLAIN and store the signature in SIG. */ gpgme_error_t gpgme_op_sign_start (gpgme_ctx_t ctx, gpgme_data_t plain, gpgme_data_t sig, gpgme_sig_mode_t mode); gpgme_error_t gpgme_op_sign (gpgme_ctx_t ctx, gpgme_data_t plain, gpgme_data_t sig, gpgme_sig_mode_t mode); /* * Verify. */ /* Flags used for the SUMMARY field in a gpgme_signature_t. */ typedef enum { GPGME_SIGSUM_VALID = 0x0001, /* The signature is fully valid. */ GPGME_SIGSUM_GREEN = 0x0002, /* The signature is good. */ GPGME_SIGSUM_RED = 0x0004, /* The signature is bad. */ GPGME_SIGSUM_KEY_REVOKED = 0x0010, /* One key has been revoked. */ GPGME_SIGSUM_KEY_EXPIRED = 0x0020, /* One key has expired. */ GPGME_SIGSUM_SIG_EXPIRED = 0x0040, /* The signature has expired. */ GPGME_SIGSUM_KEY_MISSING = 0x0080, /* Can't verify: key missing. */ GPGME_SIGSUM_CRL_MISSING = 0x0100, /* CRL not available. */ GPGME_SIGSUM_CRL_TOO_OLD = 0x0200, /* Available CRL is too old. */ GPGME_SIGSUM_BAD_POLICY = 0x0400, /* A policy was not met. */ GPGME_SIGSUM_SYS_ERROR = 0x0800, /* A system error occurred. */ GPGME_SIGSUM_TOFU_CONFLICT=0x1000 /* Tofu conflict detected. */ } gpgme_sigsum_t; /* An object to hold the verification status of a signature. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_signature { struct _gpgme_signature *next; /* A summary of the signature status. */ gpgme_sigsum_t summary; /* The fingerprint of the signature. This can be a subkey. */ char *fpr; /* The status of the signature. */ gpgme_error_t status; /* Notation data and policy URLs. */ gpgme_sig_notation_t notations; /* Signature creation time. */ unsigned long timestamp; /* Signature expiration time or 0. */ unsigned long exp_timestamp; /* Key should not have been used for signing. */ unsigned int wrong_key_usage : 1; /* PKA status: 0 = not available, 1 = bad, 2 = okay, 3 = RFU. */ unsigned int pka_trust : 2; /* Validity has been verified using the chain model. */ unsigned int chain_model : 1; /* True if the signature is in compliance to the de-vs mode. */ unsigned int is_de_vs : 1; /* Internal to GPGME, do not use. */ int _unused : 27; gpgme_validity_t validity; gpgme_error_t validity_reason; /* The public key algorithm used to create the signature. */ gpgme_pubkey_algo_t pubkey_algo; /* The hash algorithm used to create the signature. */ gpgme_hash_algo_t hash_algo; /* The mailbox from the PKA information or NULL. */ char *pka_address; /* If non-NULL, a possible incomplete key object with the data * available for the signature. */ gpgme_key_t key; }; typedef struct _gpgme_signature *gpgme_signature_t; /* An object to return the results of a verify operation. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_verify_result { gpgme_signature_t signatures; /* The original file name of the plaintext message, if available. */ char *file_name; }; typedef struct _gpgme_op_verify_result *gpgme_verify_result_t; /* Retrieve a pointer to the result of the verify operation. */ gpgme_verify_result_t gpgme_op_verify_result (gpgme_ctx_t ctx); /* Verify within CTX that SIG is a valid signature for TEXT. */ gpgme_error_t gpgme_op_verify_start (gpgme_ctx_t ctx, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext); gpgme_error_t gpgme_op_verify (gpgme_ctx_t ctx, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext); /* * Import/Export */ #define GPGME_IMPORT_NEW 1 /* The key was new. */ #define GPGME_IMPORT_UID 2 /* The key contained new user IDs. */ #define GPGME_IMPORT_SIG 4 /* The key contained new signatures. */ #define GPGME_IMPORT_SUBKEY 8 /* The key contained new sub keys. */ #define GPGME_IMPORT_SECRET 16 /* The key contained a secret key. */ /* An object to hold results for one imported key. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_import_status { struct _gpgme_import_status *next; /* Fingerprint. */ char *fpr; /* If a problem occurred, the reason why the key could not be imported. Otherwise GPGME_No_Error. */ gpgme_error_t result; /* The result of the import, the GPGME_IMPORT_* values bit-wise ORed. 0 means the key was already known and no new components have been added. */ unsigned int status; }; typedef struct _gpgme_import_status *gpgme_import_status_t; /* Import result object. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_import_result { /* Number of considered keys. */ int considered; /* Keys without user ID. */ int no_user_id; /* Imported keys. */ int imported; /* Imported RSA keys. */ int imported_rsa; /* Unchanged keys. */ int unchanged; /* Number of new user ids. */ int new_user_ids; /* Number of new sub keys. */ int new_sub_keys; /* Number of new signatures. */ int new_signatures; /* Number of new revocations. */ int new_revocations; /* Number of secret keys read. */ int secret_read; /* Number of secret keys imported. */ int secret_imported; /* Number of secret keys unchanged. */ int secret_unchanged; /* Number of new keys skipped. */ int skipped_new_keys; /* Number of keys not imported. */ int not_imported; /* List of keys for which an import was attempted. */ gpgme_import_status_t imports; /* Number of v3 keys skipped. */ int skipped_v3_keys; }; typedef struct _gpgme_op_import_result *gpgme_import_result_t; /* Retrieve a pointer to the result of the import operation. */ gpgme_import_result_t gpgme_op_import_result (gpgme_ctx_t ctx); /* Import the key in KEYDATA into the keyring. */ gpgme_error_t gpgme_op_import_start (gpgme_ctx_t ctx, gpgme_data_t keydata); gpgme_error_t gpgme_op_import (gpgme_ctx_t ctx, gpgme_data_t keydata); /* Import the keys from the array KEYS into the keyring. */ gpgme_error_t gpgme_op_import_keys_start (gpgme_ctx_t ctx, gpgme_key_t keys[]); gpgme_error_t gpgme_op_import_keys (gpgme_ctx_t ctx, gpgme_key_t keys[]); /* Export the keys found by PATTERN into KEYDATA. */ gpgme_error_t gpgme_op_export_start (gpgme_ctx_t ctx, const char *pattern, gpgme_export_mode_t mode, gpgme_data_t keydata); gpgme_error_t gpgme_op_export (gpgme_ctx_t ctx, const char *pattern, gpgme_export_mode_t mode, gpgme_data_t keydata); gpgme_error_t gpgme_op_export_ext_start (gpgme_ctx_t ctx, const char *pattern[], gpgme_export_mode_t mode, gpgme_data_t keydata); gpgme_error_t gpgme_op_export_ext (gpgme_ctx_t ctx, const char *pattern[], gpgme_export_mode_t mode, gpgme_data_t keydata); /* Export the keys from the array KEYS into KEYDATA. */ gpgme_error_t gpgme_op_export_keys_start (gpgme_ctx_t ctx, gpgme_key_t keys[], gpgme_export_mode_t mode, gpgme_data_t keydata); gpgme_error_t gpgme_op_export_keys (gpgme_ctx_t ctx, gpgme_key_t keys[], gpgme_export_mode_t mode, gpgme_data_t keydata); /* * Key generation. */ /* Flags for the key creation functions. */ #define GPGME_CREATE_SIGN (1 << 0) /* Allow usage: signing. */ #define GPGME_CREATE_ENCR (1 << 1) /* Allow usage: encryption. */ #define GPGME_CREATE_CERT (1 << 2) /* Allow usage: certification. */ #define GPGME_CREATE_AUTH (1 << 3) /* Allow usage: authentication. */ #define GPGME_CREATE_NOPASSWD (1 << 7) /* Create w/o passphrase. */ #define GPGME_CREATE_SELFSIGNED (1 << 8) /* Create self-signed cert. */ #define GPGME_CREATE_NOSTORE (1 << 9) /* Do not store the key. */ #define GPGME_CREATE_WANTPUB (1 << 10) /* Return the public key. */ #define GPGME_CREATE_WANTSEC (1 << 11) /* Return the secret key. */ #define GPGME_CREATE_FORCE (1 << 12) /* Force creation. */ #define GPGME_CREATE_NOEXPIRE (1 << 13) /* Create w/o expiration. */ /* An object to return result from a key generation. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_genkey_result { /* A primary key was generated. */ unsigned int primary : 1; /* A sub key was generated. */ unsigned int sub : 1; /* A user id was generated. */ unsigned int uid : 1; /* Internal to GPGME, do not use. */ unsigned int _unused : 29; /* The fingerprint of the generated key. */ char *fpr; /* A memory data object with the created public key. Only set when * GPGME_CREATE_WANTPUB has been used. */ gpgme_data_t pubkey; /* A memory data object with the created secret key. Only set when * GPGME_CREATE_WANTSEC has been used. */ gpgme_data_t seckey; }; typedef struct _gpgme_op_genkey_result *gpgme_genkey_result_t; /* Generate a new keypair and add it to the keyring. PUBKEY and SECKEY should be null for now. PARMS specifies what keys should be generated. */ gpgme_error_t gpgme_op_genkey_start (gpgme_ctx_t ctx, const char *parms, gpgme_data_t pubkey, gpgme_data_t seckey); gpgme_error_t gpgme_op_genkey (gpgme_ctx_t ctx, const char *parms, gpgme_data_t pubkey, gpgme_data_t seckey); /* Generate a key pair using the modern interface. */ gpgme_error_t gpgme_op_createkey_start (gpgme_ctx_t ctx, const char *userid, const char *algo, unsigned long reserved, unsigned long expires, gpgme_key_t certkey, unsigned int flags); gpgme_error_t gpgme_op_createkey (gpgme_ctx_t ctx, const char *userid, const char *algo, unsigned long reserved, unsigned long expires, gpgme_key_t certkey, unsigned int flags); /* Add a new subkey to KEY. */ gpgme_error_t gpgme_op_createsubkey_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *algo, unsigned long reserved, unsigned long expires, unsigned int flags); gpgme_error_t gpgme_op_createsubkey (gpgme_ctx_t ctx, gpgme_key_t key, const char *algo, unsigned long reserved, unsigned long expires, unsigned int flags); /* Add USERID to an existing KEY. */ gpgme_error_t gpgme_op_adduid_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned int reserved); gpgme_error_t gpgme_op_adduid (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned int reserved); /* Revoke a USERID from a KEY. */ gpgme_error_t gpgme_op_revuid_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned int reserved); gpgme_error_t gpgme_op_revuid (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned int reserved); /* Set a flag on the USERID of KEY. See the manual for supported flags. */ gpgme_error_t gpgme_op_set_uid_flag_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, const char *name, const char *value); gpgme_error_t gpgme_op_set_uid_flag (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, const char *name, const char *value); /* Retrieve a pointer to the result of a genkey, createkey, or * createsubkey operation. */ gpgme_genkey_result_t gpgme_op_genkey_result (gpgme_ctx_t ctx); /* Delete KEY from the keyring. If ALLOW_SECRET is non-zero, secret keys are also deleted. */ gpgme_error_t gpgme_op_delete_start (gpgme_ctx_t ctx, const gpgme_key_t key, int allow_secret); gpgme_error_t gpgme_op_delete (gpgme_ctx_t ctx, const gpgme_key_t key, int allow_secret); /* Flags for the key delete functions. */ #define GPGME_DELETE_ALLOW_SECRET (1 << 0) /* Also delete secret key. */ #define GPGME_DELETE_FORCE (1 << 1) /* Do not ask user to confirm. */ gpgme_error_t gpgme_op_delete_ext_start (gpgme_ctx_t ctx, const gpgme_key_t key, unsigned int flags); gpgme_error_t gpgme_op_delete_ext (gpgme_ctx_t ctx, const gpgme_key_t key, unsigned int flags); /* * Key signing interface */ /* Flags for the key signing functions. */ #define GPGME_KEYSIGN_LOCAL (1 << 7) /* Create a local signature. */ #define GPGME_KEYSIGN_LFSEP (1 << 8) /* Indicate LF separated user ids. */ #define GPGME_KEYSIGN_NOEXPIRE (1 << 9) /* Force no expiration. */ /* Sign the USERID of KEY using the current set of signers. */ gpgme_error_t gpgme_op_keysign_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned long expires, unsigned int flags); gpgme_error_t gpgme_op_keysign (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned long expires, unsigned int flags); /* * Key edit interface */ /* Flags to select the mode of the interact. */ #define GPGME_INTERACT_CARD (1 << 0) /* Use --card-edit mode. */ /* Edit the KEY. Send status and command requests to FNC and output of edit commands to OUT. */ gpgme_error_t gpgme_op_interact_start (gpgme_ctx_t ctx, gpgme_key_t key, unsigned int flags, gpgme_interact_cb_t fnc, void *fnc_value, gpgme_data_t out); gpgme_error_t gpgme_op_interact (gpgme_ctx_t ctx, gpgme_key_t key, unsigned int flags, gpgme_interact_cb_t fnc, void *fnc_value, gpgme_data_t out); /* Set the Tofu policy of KEY to POLCIY. */ gpgme_error_t gpgme_op_tofu_policy_start (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_tofu_policy_t policy); gpgme_error_t gpgme_op_tofu_policy (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_tofu_policy_t policy); /* * Key listing */ /* An object to return results from a key listing operation. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_keylist_result { unsigned int truncated : 1; /* Internal to GPGME, do not use. */ unsigned int _unused : 31; }; typedef struct _gpgme_op_keylist_result *gpgme_keylist_result_t; /* Retrieve a pointer to the result of the key listing operation. */ gpgme_keylist_result_t gpgme_op_keylist_result (gpgme_ctx_t ctx); /* Start a keylist operation within CTX, searching for keys which * match PATTERN. If SECRET_ONLY is true, only secret keys are * returned. */ gpgme_error_t gpgme_op_keylist_start (gpgme_ctx_t ctx, const char *pattern, int secret_only); gpgme_error_t gpgme_op_keylist_ext_start (gpgme_ctx_t ctx, const char *pattern[], int secret_only, int reserved); /* List the keys contained in DATA. */ gpgme_error_t gpgme_op_keylist_from_data_start (gpgme_ctx_t ctx, gpgme_data_t data, int reserved); /* Return the next key from the keylist in R_KEY. */ gpgme_error_t gpgme_op_keylist_next (gpgme_ctx_t ctx, gpgme_key_t *r_key); /* Terminate a pending keylist operation within CTX. */ gpgme_error_t gpgme_op_keylist_end (gpgme_ctx_t ctx); /* * Protecting keys */ /* Change the passphrase for KEY. FLAGS is reserved for future use and must be passed as 0. */ gpgme_error_t gpgme_op_passwd_start (gpgme_ctx_t ctx, gpgme_key_t key, unsigned int flags); gpgme_error_t gpgme_op_passwd (gpgme_ctx_t ctx, gpgme_key_t key, unsigned int flags); /* * Trust items and operations. */ /* An object to hold data of a trust item. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_trust_item { /* Internal to GPGME, do not use. */ unsigned int _refs; /* The key ID to which the trust item belongs. */ char *keyid; /* Internal to GPGME, do not use. */ char _keyid[16 + 1]; /* The type of the trust item, 1 refers to a key, 2 to a user ID. */ int type; /* The trust level. */ int level; /* The owner trust if TYPE is 1. */ char *owner_trust; /* Internal to GPGME, do not use. */ char _owner_trust[2]; /* The calculated validity. */ char *validity; /* Internal to GPGME, do not use. */ char _validity[2]; /* The user name if TYPE is 2. */ char *name; }; typedef struct _gpgme_trust_item *gpgme_trust_item_t; /* Start a trustlist operation within CTX, searching for trust items which match PATTERN. */ gpgme_error_t gpgme_op_trustlist_start (gpgme_ctx_t ctx, const char *pattern, int max_level); /* Return the next trust item from the trustlist in R_ITEM. */ gpgme_error_t gpgme_op_trustlist_next (gpgme_ctx_t ctx, gpgme_trust_item_t *r_item); /* Terminate a pending trustlist operation within CTX. */ gpgme_error_t gpgme_op_trustlist_end (gpgme_ctx_t ctx); /* Acquire a reference to ITEM. */ void gpgme_trust_item_ref (gpgme_trust_item_t item); /* Release a reference to ITEM. If this was the last one the trust item is destroyed. */ void gpgme_trust_item_unref (gpgme_trust_item_t item); /* * Audit log */ /* Return the auditlog for the current session. This may be called after a successful or failed operation. If no audit log is available GPG_ERR_NO_DATA is returned. */ gpgme_error_t gpgme_op_getauditlog_start (gpgme_ctx_t ctx, gpgme_data_t output, unsigned int flags); gpgme_error_t gpgme_op_getauditlog (gpgme_ctx_t ctx, gpgme_data_t output, unsigned int flags); /* * Spawn interface */ /* Flags for the spawn operations. */ #define GPGME_SPAWN_DETACHED 1 #define GPGME_SPAWN_ALLOW_SET_FG 2 #define GPGME_SPAWN_SHOW_WINDOW 4 /* Run the command FILE with the arguments in ARGV. Connect stdin to DATAIN, stdout to DATAOUT, and STDERR to DATAERR. If one the data streams is NULL, connect to /dev/null instead. */ gpgme_error_t gpgme_op_spawn_start (gpgme_ctx_t ctx, const char *file, const char *argv[], gpgme_data_t datain, gpgme_data_t dataout, gpgme_data_t dataerr, unsigned int flags); gpgme_error_t gpgme_op_spawn (gpgme_ctx_t ctx, const char *file, const char *argv[], gpgme_data_t datain, gpgme_data_t dataout, gpgme_data_t dataerr, unsigned int flags); /* * Low-level Assuan protocol access. */ typedef gpgme_error_t (*gpgme_assuan_data_cb_t) (void *opaque, const void *data, size_t datalen); typedef gpgme_error_t (*gpgme_assuan_inquire_cb_t) (void *opaque, const char *name, const char *args, gpgme_data_t *r_data); typedef gpgme_error_t (*gpgme_assuan_status_cb_t) (void *opaque, const char *status, const char *args); /* Send the Assuan COMMAND and return results via the callbacks. Asynchronous variant. */ gpgme_error_t gpgme_op_assuan_transact_start (gpgme_ctx_t ctx, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t stat_cb, void *stat_cb_value); /* Send the Assuan COMMAND and return results via the callbacks. Synchronous variant. */ gpgme_error_t gpgme_op_assuan_transact_ext (gpgme_ctx_t ctx, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t stat_cb, void *stat_cb_value, gpgme_error_t *op_err); /* * Crypto container support. */ /* An object to return results from a VFS mount operation. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_vfs_mount_result { char *mount_dir; }; typedef struct _gpgme_op_vfs_mount_result *gpgme_vfs_mount_result_t; gpgme_vfs_mount_result_t gpgme_op_vfs_mount_result (gpgme_ctx_t ctx); /* The container is automatically unmounted when the context is reset or destroyed. Transmission errors are returned directly, operational errors are returned in OP_ERR. */ gpgme_error_t gpgme_op_vfs_mount (gpgme_ctx_t ctx, const char *container_file, const char *mount_dir, unsigned int flags, gpgme_error_t *op_err); gpgme_error_t gpgme_op_vfs_create (gpgme_ctx_t ctx, gpgme_key_t recp[], const char *container_file, unsigned int flags, gpgme_error_t *op_err); /* * Interface to gpgconf(1). */ /* The expert level at which a configuration option or group of options should be displayed. See the gpgconf(1) documentation for more details. */ typedef enum { GPGME_CONF_BASIC = 0, GPGME_CONF_ADVANCED = 1, GPGME_CONF_EXPERT = 2, GPGME_CONF_INVISIBLE = 3, GPGME_CONF_INTERNAL = 4 } gpgme_conf_level_t; /* The data type of a configuration option argument. See the gpgconf(1) documentation for more details. */ typedef enum { /* Basic types. */ GPGME_CONF_NONE = 0, GPGME_CONF_STRING = 1, GPGME_CONF_INT32 = 2, GPGME_CONF_UINT32 = 3, /* Complex types. */ GPGME_CONF_FILENAME = 32, GPGME_CONF_LDAP_SERVER = 33, GPGME_CONF_KEY_FPR = 34, GPGME_CONF_PUB_KEY = 35, GPGME_CONF_SEC_KEY = 36, GPGME_CONF_ALIAS_LIST = 37 } gpgme_conf_type_t; /* For now, compatibility. */ #define GPGME_CONF_PATHNAME GPGME_CONF_FILENAME /* This represents a single argument for a configuration option. Which of the members of value is used depends on the ALT_TYPE. */ typedef struct gpgme_conf_arg { struct gpgme_conf_arg *next; /* True if the option appears without an (optional) argument. */ unsigned int no_arg; union { unsigned int count; unsigned int uint32; int int32; char *string; } value; } *gpgme_conf_arg_t; /* The flags of a configuration option. See the gpgconf documentation for details. */ #define GPGME_CONF_GROUP (1 << 0) #define GPGME_CONF_OPTIONAL (1 << 1) #define GPGME_CONF_LIST (1 << 2) #define GPGME_CONF_RUNTIME (1 << 3) #define GPGME_CONF_DEFAULT (1 << 4) #define GPGME_CONF_DEFAULT_DESC (1 << 5) #define GPGME_CONF_NO_ARG_DESC (1 << 6) #define GPGME_CONF_NO_CHANGE (1 << 7) /* The representation of a single configuration option. See the gpg-conf documentation for details. */ typedef struct gpgme_conf_opt { struct gpgme_conf_opt *next; /* The option name. */ char *name; /* The flags for this option. */ unsigned int flags; /* The level of this option. */ gpgme_conf_level_t level; /* The localized description of this option. */ char *description; /* The type and alternate type of this option. */ gpgme_conf_type_t type; gpgme_conf_type_t alt_type; /* The localized (short) name of the argument, if any. */ char *argname; /* The default value. */ gpgme_conf_arg_t default_value; char *default_description; /* The default value if the option is not set. */ gpgme_conf_arg_t no_arg_value; char *no_arg_description; /* The current value if the option is set. */ gpgme_conf_arg_t value; /* The new value, if any. NULL means reset to default. */ int change_value; gpgme_conf_arg_t new_value; /* Free for application use. */ void *user_data; } *gpgme_conf_opt_t; /* The representation of a component that can be configured. See the gpg-conf documentation for details. */ typedef struct gpgme_conf_comp { struct gpgme_conf_comp *next; /* Internal to GPGME, do not use! */ gpgme_conf_opt_t *_last_opt_p; /* The component name. */ char *name; /* A human-readable description for the component. */ char *description; /* The program name (an absolute path to the program). */ char *program_name; /* A linked list of options for this component. */ struct gpgme_conf_opt *options; } *gpgme_conf_comp_t; /* Allocate a new gpgme_conf_arg_t. If VALUE is NULL, a "no arg default" is prepared. If type is a string type, VALUE should point to the string. Else, it should point to an unsigned or signed integer respectively. */ gpgme_error_t gpgme_conf_arg_new (gpgme_conf_arg_t *arg_p, gpgme_conf_type_t type, const void *value); /* This also releases all chained argument structures! */ void gpgme_conf_arg_release (gpgme_conf_arg_t arg, gpgme_conf_type_t type); /* Register a change for the value of OPT to ARG. If RESET is 1 (do not use any values but 0 or 1), ARG is ignored and the option is not changed (reverting a previous change). Otherwise, if ARG is NULL, the option is cleared or reset to its default. The change is done with gpgconf's --runtime option to immediately take effect. */ gpgme_error_t gpgme_conf_opt_change (gpgme_conf_opt_t opt, int reset, gpgme_conf_arg_t arg); /* Release a set of configurations. */ void gpgme_conf_release (gpgme_conf_comp_t conf); /* Retrieve the current configurations. */ gpgme_error_t gpgme_op_conf_load (gpgme_ctx_t ctx, gpgme_conf_comp_t *conf_p); /* Save the configuration of component comp. This function does not follow chained components! */ gpgme_error_t gpgme_op_conf_save (gpgme_ctx_t ctx, gpgme_conf_comp_t comp); /* Retrieve the configured directory. */ gpgme_error_t gpgme_op_conf_dir(gpgme_ctx_t ctx, const char *what, char **result); /* Information about software versions. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ typedef struct _gpgme_op_query_swdb_result { /* RFU */ struct _gpgme_op_query_swdb_result *next; /* The name of the package (e.g. "gpgme", "gnupg") */ char *name; /* The version number of the installed version. */ char *iversion; /* The time the online info was created. */ unsigned long created; /* The time the online info was retrieved. */ unsigned long retrieved; /* This bit is set if an error occured or some of the information * in this structure may not be set. */ unsigned int warning : 1; /* An update is available. */ unsigned int update : 1; /* The update is important. */ unsigned int urgent : 1; /* No information at all available. */ unsigned int noinfo : 1; /* The package name is not known. */ unsigned int unknown : 1; /* The information here is too old. */ unsigned int tooold : 1; /* Other error. */ unsigned int error : 1; unsigned int _reserved : 25; /* The version number of the latest released version. */ char *version; /* The release date of that version. */ unsigned long reldate; } *gpgme_query_swdb_result_t; /* Run the gpgconf --query-swdb command. */ gpgme_error_t gpgme_op_query_swdb (gpgme_ctx_t ctx, const char *name, const char *iversion, unsigned int reserved); /* Return the result from the last query_swdb operation. */ gpgme_query_swdb_result_t gpgme_op_query_swdb_result (gpgme_ctx_t ctx); /* * Various functions. */ /* Set special global flags; consult the manual before use. */ int gpgme_set_global_flag (const char *name, const char *value); /* Check that the library fulfills the version requirement. Note: This is here only for the case where a user takes a pointer from the old version of this function. The new version and macro for run-time checks are below. */ const char *gpgme_check_version (const char *req_version); /* Check that the library fulfills the version requirement and check for struct layout mismatch involving bitfields. */ const char *gpgme_check_version_internal (const char *req_version, size_t offset_sig_validity); #define gpgme_check_version(req_version) \ gpgme_check_version_internal (req_version, \ offsetof (struct _gpgme_signature, validity)) /* Return the default values for various directories. */ const char *gpgme_get_dirinfo (const char *what); /* Get the information about the configured and installed engines. A pointer to the first engine in the statically allocated linked list is returned in *INFO. If an error occurs, it is returned. The returned data is valid until the next gpgme_set_engine_info. */ gpgme_error_t gpgme_get_engine_info (gpgme_engine_info_t *engine_info); /* Set the default engine info for the protocol PROTO to the file name FILE_NAME and the home directory HOME_DIR. */ gpgme_error_t gpgme_set_engine_info (gpgme_protocol_t proto, const char *file_name, const char *home_dir); /* Verify that the engine implementing PROTO is installed and available. */ gpgme_error_t gpgme_engine_check_version (gpgme_protocol_t proto); /* Reference counting for result objects. */ void gpgme_result_ref (void *result); void gpgme_result_unref (void *result); /* Return a public key algorithm string (e.g. "rsa2048"). Caller must free using gpgme_free. */ char *gpgme_pubkey_algo_string (gpgme_subkey_t subkey); /* Return a statically allocated string with the name of the public key algorithm ALGO, or NULL if that name is not known. */ const char *gpgme_pubkey_algo_name (gpgme_pubkey_algo_t algo); /* Return a statically allocated string with the name of the hash algorithm ALGO, or NULL if that name is not known. */ const char *gpgme_hash_algo_name (gpgme_hash_algo_t algo); /* Return the addr-spec from a user id. Caller must free the result * with gpgme_free. */ char *gpgme_addrspec_from_uid (const char *uid); /* * Deprecated types, constants and functions. */ /* The possible stati for gpgme_op_edit. The use of that function and * these status codes are deprecated in favor of gpgme_op_interact. */ typedef enum { GPGME_STATUS_EOF = 0, /* mkstatus processing starts here */ GPGME_STATUS_ENTER = 1, GPGME_STATUS_LEAVE = 2, GPGME_STATUS_ABORT = 3, GPGME_STATUS_GOODSIG = 4, GPGME_STATUS_BADSIG = 5, GPGME_STATUS_ERRSIG = 6, GPGME_STATUS_BADARMOR = 7, GPGME_STATUS_RSA_OR_IDEA = 8, /* (legacy) */ GPGME_STATUS_KEYEXPIRED = 9, GPGME_STATUS_KEYREVOKED = 10, GPGME_STATUS_TRUST_UNDEFINED = 11, GPGME_STATUS_TRUST_NEVER = 12, GPGME_STATUS_TRUST_MARGINAL = 13, GPGME_STATUS_TRUST_FULLY = 14, GPGME_STATUS_TRUST_ULTIMATE = 15, GPGME_STATUS_SHM_INFO = 16, /* (legacy) */ GPGME_STATUS_SHM_GET = 17, /* (legacy) */ GPGME_STATUS_SHM_GET_BOOL = 18, /* (legacy) */ GPGME_STATUS_SHM_GET_HIDDEN = 19, /* (legacy) */ GPGME_STATUS_NEED_PASSPHRASE = 20, GPGME_STATUS_VALIDSIG = 21, GPGME_STATUS_SIG_ID = 22, GPGME_STATUS_ENC_TO = 23, GPGME_STATUS_NODATA = 24, GPGME_STATUS_BAD_PASSPHRASE = 25, GPGME_STATUS_NO_PUBKEY = 26, GPGME_STATUS_NO_SECKEY = 27, GPGME_STATUS_NEED_PASSPHRASE_SYM = 28, GPGME_STATUS_DECRYPTION_FAILED = 29, GPGME_STATUS_DECRYPTION_OKAY = 30, GPGME_STATUS_MISSING_PASSPHRASE = 31, GPGME_STATUS_GOOD_PASSPHRASE = 32, GPGME_STATUS_GOODMDC = 33, GPGME_STATUS_BADMDC = 34, GPGME_STATUS_ERRMDC = 35, GPGME_STATUS_IMPORTED = 36, GPGME_STATUS_IMPORT_OK = 37, GPGME_STATUS_IMPORT_PROBLEM = 38, GPGME_STATUS_IMPORT_RES = 39, GPGME_STATUS_FILE_START = 40, GPGME_STATUS_FILE_DONE = 41, GPGME_STATUS_FILE_ERROR = 42, GPGME_STATUS_BEGIN_DECRYPTION = 43, GPGME_STATUS_END_DECRYPTION = 44, GPGME_STATUS_BEGIN_ENCRYPTION = 45, GPGME_STATUS_END_ENCRYPTION = 46, GPGME_STATUS_DELETE_PROBLEM = 47, GPGME_STATUS_GET_BOOL = 48, GPGME_STATUS_GET_LINE = 49, GPGME_STATUS_GET_HIDDEN = 50, GPGME_STATUS_GOT_IT = 51, GPGME_STATUS_PROGRESS = 52, GPGME_STATUS_SIG_CREATED = 53, GPGME_STATUS_SESSION_KEY = 54, GPGME_STATUS_NOTATION_NAME = 55, GPGME_STATUS_NOTATION_DATA = 56, GPGME_STATUS_POLICY_URL = 57, GPGME_STATUS_BEGIN_STREAM = 58, /* (legacy) */ GPGME_STATUS_END_STREAM = 59, /* (legacy) */ GPGME_STATUS_KEY_CREATED = 60, GPGME_STATUS_USERID_HINT = 61, GPGME_STATUS_UNEXPECTED = 62, GPGME_STATUS_INV_RECP = 63, GPGME_STATUS_NO_RECP = 64, GPGME_STATUS_ALREADY_SIGNED = 65, GPGME_STATUS_SIGEXPIRED = 66, /* (legacy) */ GPGME_STATUS_EXPSIG = 67, GPGME_STATUS_EXPKEYSIG = 68, GPGME_STATUS_TRUNCATED = 69, GPGME_STATUS_ERROR = 70, GPGME_STATUS_NEWSIG = 71, GPGME_STATUS_REVKEYSIG = 72, GPGME_STATUS_SIG_SUBPACKET = 73, GPGME_STATUS_NEED_PASSPHRASE_PIN = 74, GPGME_STATUS_SC_OP_FAILURE = 75, GPGME_STATUS_SC_OP_SUCCESS = 76, GPGME_STATUS_CARDCTRL = 77, GPGME_STATUS_BACKUP_KEY_CREATED = 78, GPGME_STATUS_PKA_TRUST_BAD = 79, GPGME_STATUS_PKA_TRUST_GOOD = 80, GPGME_STATUS_PLAINTEXT = 81, GPGME_STATUS_INV_SGNR = 82, GPGME_STATUS_NO_SGNR = 83, GPGME_STATUS_SUCCESS = 84, GPGME_STATUS_DECRYPTION_INFO = 85, GPGME_STATUS_PLAINTEXT_LENGTH = 86, GPGME_STATUS_MOUNTPOINT = 87, GPGME_STATUS_PINENTRY_LAUNCHED = 88, GPGME_STATUS_ATTRIBUTE = 89, GPGME_STATUS_BEGIN_SIGNING = 90, GPGME_STATUS_KEY_NOT_CREATED = 91, GPGME_STATUS_INQUIRE_MAXLEN = 92, GPGME_STATUS_FAILURE = 93, GPGME_STATUS_KEY_CONSIDERED = 94, GPGME_STATUS_TOFU_USER = 95, GPGME_STATUS_TOFU_STATS = 96, GPGME_STATUS_TOFU_STATS_LONG = 97, GPGME_STATUS_NOTATION_FLAGS = 98, GPGME_STATUS_DECRYPTION_COMPLIANCE_MODE = 99, GPGME_STATUS_VERIFICATION_COMPLIANCE_MODE = 100 } gpgme_status_code_t; /* The callback type used by the deprecated functions gpgme_op_edit * and gpgme_op_card_edit. */ typedef gpgme_error_t (*gpgme_edit_cb_t) (void *opaque, gpgme_status_code_t status, const char *args, int fd); gpgme_error_t gpgme_op_edit_start (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_edit_cb_t fnc, void *fnc_value, gpgme_data_t out) _GPGME_DEPRECATED(1,7); gpgme_error_t gpgme_op_edit (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_edit_cb_t fnc, void *fnc_value, gpgme_data_t out) _GPGME_DEPRECATED(1,7); gpgme_error_t gpgme_op_card_edit_start (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_edit_cb_t fnc, void *fnc_value, gpgme_data_t out) _GPGME_DEPRECATED(1,7); gpgme_error_t gpgme_op_card_edit (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_edit_cb_t fnc, void *fnc_value, gpgme_data_t out) _GPGME_DEPRECATED(1,7); /* The possible signature stati. Deprecated, use error value in sig status. */ typedef enum { GPGME_SIG_STAT_NONE = 0, GPGME_SIG_STAT_GOOD = 1, GPGME_SIG_STAT_BAD = 2, GPGME_SIG_STAT_NOKEY = 3, GPGME_SIG_STAT_NOSIG = 4, GPGME_SIG_STAT_ERROR = 5, GPGME_SIG_STAT_DIFF = 6, GPGME_SIG_STAT_GOOD_EXP = 7, GPGME_SIG_STAT_GOOD_EXPKEY = 8 } _gpgme_sig_stat_t; typedef _gpgme_sig_stat_t gpgme_sig_stat_t _GPGME_DEPRECATED(0,4); /* The available key and signature attributes. Deprecated, use the individual result structures instead. */ typedef enum { GPGME_ATTR_KEYID = 1, GPGME_ATTR_FPR = 2, GPGME_ATTR_ALGO = 3, GPGME_ATTR_LEN = 4, GPGME_ATTR_CREATED = 5, GPGME_ATTR_EXPIRE = 6, GPGME_ATTR_OTRUST = 7, GPGME_ATTR_USERID = 8, GPGME_ATTR_NAME = 9, GPGME_ATTR_EMAIL = 10, GPGME_ATTR_COMMENT = 11, GPGME_ATTR_VALIDITY = 12, GPGME_ATTR_LEVEL = 13, GPGME_ATTR_TYPE = 14, GPGME_ATTR_IS_SECRET = 15, GPGME_ATTR_KEY_REVOKED = 16, GPGME_ATTR_KEY_INVALID = 17, GPGME_ATTR_UID_REVOKED = 18, GPGME_ATTR_UID_INVALID = 19, GPGME_ATTR_KEY_CAPS = 20, GPGME_ATTR_CAN_ENCRYPT = 21, GPGME_ATTR_CAN_SIGN = 22, GPGME_ATTR_CAN_CERTIFY = 23, GPGME_ATTR_KEY_EXPIRED = 24, GPGME_ATTR_KEY_DISABLED = 25, GPGME_ATTR_SERIAL = 26, GPGME_ATTR_ISSUER = 27, GPGME_ATTR_CHAINID = 28, GPGME_ATTR_SIG_STATUS = 29, GPGME_ATTR_ERRTOK = 30, GPGME_ATTR_SIG_SUMMARY = 31, GPGME_ATTR_SIG_CLASS = 32 } _gpgme_attr_t; typedef _gpgme_attr_t gpgme_attr_t _GPGME_DEPRECATED(0,4); /* Retrieve the signature status of signature IDX in CTX after a successful verify operation in R_STAT (if non-null). The creation time stamp of the signature is returned in R_CREATED (if non-null). The function returns a string containing the fingerprint. Deprecated, use verify result directly. */ const char *gpgme_get_sig_status (gpgme_ctx_t ctx, int idx, _gpgme_sig_stat_t *r_stat, time_t *r_created) _GPGME_DEPRECATED(0,4); /* Retrieve certain attributes of a signature. IDX is the index number of the signature after a successful verify operation. WHAT is an attribute where GPGME_ATTR_EXPIRE is probably the most useful one. WHATIDX is to be passed as 0 for most attributes . */ unsigned long gpgme_get_sig_ulong_attr (gpgme_ctx_t c, int idx, _gpgme_attr_t what, int whatidx) _GPGME_DEPRECATED(0,4); const char *gpgme_get_sig_string_attr (gpgme_ctx_t c, int idx, _gpgme_attr_t what, int whatidx) _GPGME_DEPRECATED(0,4); /* Get the key used to create signature IDX in CTX and return it in R_KEY. */ gpgme_error_t gpgme_get_sig_key (gpgme_ctx_t ctx, int idx, gpgme_key_t *r_key) _GPGME_DEPRECATED(0,4); /* Create a new data buffer which retrieves the data from the callback function READ_CB. Deprecated, please use gpgme_data_new_from_cbs instead. */ gpgme_error_t gpgme_data_new_with_read_cb (gpgme_data_t *r_dh, int (*read_cb) (void*,char *, size_t,size_t*), void *read_cb_value) _GPGME_DEPRECATED(0,4); /* Return the value of the attribute WHAT of KEY, which has to be representable by a string. IDX specifies the sub key or user ID for attributes related to sub keys or user IDs. Deprecated, use key structure directly instead. */ const char *gpgme_key_get_string_attr (gpgme_key_t key, _gpgme_attr_t what, const void *reserved, int idx) _GPGME_DEPRECATED(0,4); /* Return the value of the attribute WHAT of KEY, which has to be representable by an unsigned integer. IDX specifies the sub key or user ID for attributes related to sub keys or user IDs. Deprecated, use key structure directly instead. */ unsigned long gpgme_key_get_ulong_attr (gpgme_key_t key, _gpgme_attr_t what, const void *reserved, int idx) _GPGME_DEPRECATED(0,4); /* Return the value of the attribute WHAT of a signature on user ID UID_IDX in KEY, which has to be representable by a string. IDX specifies the signature. Deprecated, use key structure directly instead. */ const char *gpgme_key_sig_get_string_attr (gpgme_key_t key, int uid_idx, _gpgme_attr_t what, const void *reserved, int idx) _GPGME_DEPRECATED(0,4); /* Return the value of the attribute WHAT of a signature on user ID UID_IDX in KEY, which has to be representable by an unsigned integer string. IDX specifies the signature. Deprecated, use key structure directly instead. */ unsigned long gpgme_key_sig_get_ulong_attr (gpgme_key_t key, int uid_idx, _gpgme_attr_t what, const void *reserved, int idx) _GPGME_DEPRECATED(0,4); gpgme_error_t gpgme_op_import_ext (gpgme_ctx_t ctx, gpgme_data_t keydata, int *nr) _GPGME_DEPRECATED(0,4); /* Release the trust item ITEM. Deprecated, use gpgme_trust_item_unref. */ void gpgme_trust_item_release (gpgme_trust_item_t item) _GPGME_DEPRECATED(0,4); /* Return the value of the attribute WHAT of ITEM, which has to be representable by a string. Deprecated, use trust item structure directly. */ const char *gpgme_trust_item_get_string_attr (gpgme_trust_item_t item, _gpgme_attr_t what, const void *reserved, int idx) _GPGME_DEPRECATED(0,4); /* Return the value of the attribute WHAT of KEY, which has to be representable by an integer. IDX specifies a running index if the attribute appears more than once in the key. Deprecated, use trust item structure directly. */ int gpgme_trust_item_get_int_attr (gpgme_trust_item_t item, _gpgme_attr_t what, const void *reserved, int idx) _GPGME_DEPRECATED(0,4); /* Compat. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_assuan_result { /* Deprecated. Use the second value in a DONE event or the synchronous variant gpgme_op_assuan_transact_ext. */ gpgme_error_t err _GPGME_DEPRECATED_OUTSIDE_GPGME(1,2); }; typedef struct _gpgme_op_assuan_result *gpgme_assuan_result_t; /* Return the result of the last Assuan command. */ gpgme_assuan_result_t gpgme_op_assuan_result (gpgme_ctx_t ctx) _GPGME_DEPRECATED(1,2); gpgme_error_t gpgme_op_assuan_transact (gpgme_ctx_t ctx, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value) _GPGME_DEPRECATED(1,2); typedef gpgme_ctx_t GpgmeCtx _GPGME_DEPRECATED(0,4); typedef gpgme_data_t GpgmeData _GPGME_DEPRECATED(0,4); typedef gpgme_error_t GpgmeError _GPGME_DEPRECATED(0,4); typedef gpgme_data_encoding_t GpgmeDataEncoding _GPGME_DEPRECATED(0,4); typedef gpgme_pubkey_algo_t GpgmePubKeyAlgo _GPGME_DEPRECATED(0,4); typedef gpgme_hash_algo_t GpgmeHashAlgo _GPGME_DEPRECATED(0,4); typedef gpgme_sig_stat_t GpgmeSigStat _GPGME_DEPRECATED(0,4); typedef gpgme_sig_mode_t GpgmeSigMode _GPGME_DEPRECATED(0,4); typedef gpgme_attr_t GpgmeAttr _GPGME_DEPRECATED(0,4); typedef gpgme_validity_t GpgmeValidity _GPGME_DEPRECATED(0,4); typedef gpgme_protocol_t GpgmeProtocol _GPGME_DEPRECATED(0,4); typedef gpgme_engine_info_t GpgmeEngineInfo _GPGME_DEPRECATED(0,4); typedef gpgme_subkey_t GpgmeSubkey _GPGME_DEPRECATED(0,4); typedef gpgme_key_sig_t GpgmeKeySig _GPGME_DEPRECATED(0,4); typedef gpgme_user_id_t GpgmeUserID _GPGME_DEPRECATED(0,4); typedef gpgme_key_t GpgmeKey _GPGME_DEPRECATED(0,4); typedef gpgme_passphrase_cb_t GpgmePassphraseCb _GPGME_DEPRECATED(0,4); typedef gpgme_progress_cb_t GpgmeProgressCb _GPGME_DEPRECATED(0,4); typedef gpgme_io_cb_t GpgmeIOCb _GPGME_DEPRECATED(0,4); typedef gpgme_register_io_cb_t GpgmeRegisterIOCb _GPGME_DEPRECATED(0,4); typedef gpgme_remove_io_cb_t GpgmeRemoveIOCb _GPGME_DEPRECATED(0,4); typedef gpgme_event_io_t GpgmeEventIO _GPGME_DEPRECATED(0,4); typedef gpgme_event_io_cb_t GpgmeEventIOCb _GPGME_DEPRECATED(0,4); #define GpgmeIOCbs gpgme_io_cbs typedef gpgme_data_read_cb_t GpgmeDataReadCb _GPGME_DEPRECATED(0,4); typedef gpgme_data_write_cb_t GpgmeDataWriteCb _GPGME_DEPRECATED(0,4); typedef gpgme_data_seek_cb_t GpgmeDataSeekCb _GPGME_DEPRECATED(0,4); typedef gpgme_data_release_cb_t GpgmeDataReleaseCb _GPGME_DEPRECATED(0,4); #define GpgmeDataCbs gpgme_data_cbs typedef gpgme_encrypt_result_t GpgmeEncryptResult _GPGME_DEPRECATED(0,4); typedef gpgme_sig_notation_t GpgmeSigNotation _GPGME_DEPRECATED(0,4); typedef gpgme_signature_t GpgmeSignature _GPGME_DEPRECATED(0,4); typedef gpgme_verify_result_t GpgmeVerifyResult _GPGME_DEPRECATED(0,4); typedef gpgme_import_status_t GpgmeImportStatus _GPGME_DEPRECATED(0,4); typedef gpgme_import_result_t GpgmeImportResult _GPGME_DEPRECATED(0,4); typedef gpgme_genkey_result_t GpgmeGenKeyResult _GPGME_DEPRECATED(0,4); typedef gpgme_trust_item_t GpgmeTrustItem _GPGME_DEPRECATED(0,4); typedef gpgme_status_code_t GpgmeStatusCode _GPGME_DEPRECATED(0,4); #ifdef __cplusplus } #endif #endif /* GPGME_H */ /* @emacs_local_vars_begin@ @emacs_local_vars_read_only@ @emacs_local_vars_end@ */ diff --git a/src/libgpgme.vers b/src/libgpgme.vers index a95befba..b49c86d9 100644 --- a/src/libgpgme.vers +++ b/src/libgpgme.vers @@ -1,271 +1,276 @@ # libgpgme.vers - List of symbols to export. # Copyright (C) 2002, 2004, 2005, 2009, 2012 g10 Code GmbH # # This file is part of GPGME. # # GPGME is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser general Public License as # published by the Free Software Foundation; either version 2.1 of # the License, or (at your option) any later version. # # GPGME is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this program; if not, see . #------------------------------------------------------- # Please remember to add new functions also to gpgme.def #------------------------------------------------------- GPGME_1.1 { global: gpgme_set_engine_info; gpgme_ctx_get_engine_info; gpgme_ctx_set_engine_info; gpgme_data_set_file_name; gpgme_data_get_file_name; gpgme_data_identify; gpgme_sig_notation_clear; gpgme_sig_notation_add; gpgme_sig_notation_get; gpgme_free; gpgme_op_getauditlog_start; gpgme_op_getauditlog; gpgme_conf_release; gpgme_conf_arg_new; gpgme_conf_arg_release; gpgme_conf_opt_change; gpgme_op_conf_load; gpgme_op_conf_save; gpgme_op_conf_dir; gpgme_cancel_async; gpgme_op_assuan_result; gpgme_op_assuan_transact; gpgme_op_assuan_transact_start; gpgme_check_version_internal; gpgme_io_read; gpgme_io_write; gpgme_result_ref; gpgme_result_unref; gpgme_op_import_keys; gpgme_op_import_keys_start; gpgme_op_export_keys; gpgme_op_export_keys_start; gpgme_op_assuan_transact_ext; gpgme_wait_ext; gpgme_op_vfs_mount_result; gpgme_op_vfs_mount; gpgme_op_vfs_create; gpgme_key_from_uid; gpgme_set_sub_protocol; gpgme_get_sub_protocol; gpgme_op_passwd_start; gpgme_op_passwd; gpgme_set_global_flag; gpgme_io_writen; gpgme_set_pinentry_mode; gpgme_get_pinentry_mode; gpgme_get_dirinfo; gpgme_op_spawn_start; gpgme_op_spawn; gpgme_set_offline; gpgme_get_offline; gpgme_set_status_cb; gpgme_get_status_cb; gpgme_pubkey_algo_string; gpgme_set_ctx_flag; gpgme_get_ctx_flag; gpgme_data_set_flag; gpgme_op_createkey_start; gpgme_op_createkey; gpgme_op_createsubkey_start; gpgme_op_createsubkey; gpgme_op_adduid_start; gpgme_op_adduid; gpgme_op_revuid_start; gpgme_op_revuid; gpgme_op_keysign_start; gpgme_op_keysign; gpgme_op_tofu_policy_start; gpgme_op_tofu_policy; gpgme_op_interact_start; gpgme_op_interact; gpgme_addrspec_from_uid; gpgme_set_sender; gpgme_get_sender; gpgme_op_query_swdb; gpgme_op_query_swdb_result; gpgme_op_set_uid_flag_start; gpgme_op_set_uid_flag; gpgme_op_decrypt_ext; gpgme_op_decrypt_ext_start; }; GPGME_1.0 { global: gpgme_check_version; gpgme_get_engine_info; gpgme_engine_check_version; gpgme_err_code_from_errno; gpgme_err_code_to_errno; gpgme_err_make_from_errno; gpgme_error_from_errno; gpgme_strerror; gpgme_strerror_r; gpgme_strsource; gpgme_data_get_encoding; gpgme_data_new; gpgme_data_new_from_cbs; gpgme_data_new_from_fd; gpgme_data_new_from_file; gpgme_data_new_from_filepart; gpgme_data_new_from_mem; gpgme_data_new_from_stream; gpgme_data_read; gpgme_data_release; gpgme_data_release_and_get_mem; gpgme_data_seek; gpgme_data_set_encoding; gpgme_data_write; gpgme_get_protocol_name; gpgme_hash_algo_name; gpgme_pubkey_algo_name; gpgme_new; gpgme_get_armor; gpgme_get_include_certs; gpgme_get_io_cbs; gpgme_get_keylist_mode; gpgme_get_passphrase_cb; gpgme_get_progress_cb; gpgme_get_protocol; gpgme_get_textmode; gpgme_release; gpgme_set_armor; gpgme_set_include_certs; gpgme_set_io_cbs; gpgme_set_keylist_mode; gpgme_set_locale; gpgme_set_passphrase_cb; gpgme_set_progress_cb; gpgme_set_protocol; gpgme_set_textmode; gpgme_signers_add; gpgme_signers_clear; gpgme_signers_count; gpgme_signers_enum; gpgme_key_ref; gpgme_key_unref; gpgme_key_release; gpgme_trust_item_ref; gpgme_trust_item_unref; gpgme_cancel; gpgme_op_card_edit; gpgme_op_card_edit_start; gpgme_op_decrypt; gpgme_op_decrypt_result; gpgme_op_decrypt_start; gpgme_op_decrypt_verify; gpgme_op_decrypt_verify_start; gpgme_op_delete; gpgme_op_delete_start; gpgme_op_delete_ext; gpgme_op_delete_ext_start; gpgme_op_edit; gpgme_op_edit_start; gpgme_op_encrypt; - gpgme_op_encrypt_result; + gpgme_op_encrypt_start; + gpgme_op_encrypt_ext; + gpgme_op_encrypt_ext_start; gpgme_op_encrypt_sign; + gpgme_op_encrypt_sign_ext; gpgme_op_encrypt_sign_start; - gpgme_op_encrypt_start; + gpgme_op_encrypt_sign_ext_start; + gpgme_op_encrypt_result; + gpgme_op_export; gpgme_op_export_ext; gpgme_op_export_ext_start; gpgme_op_export_start; gpgme_op_genkey; gpgme_op_genkey_result; gpgme_op_genkey_start; gpgme_get_key; gpgme_op_import; gpgme_op_import_result; gpgme_op_import_start; gpgme_op_keylist_end; gpgme_op_keylist_ext_start; gpgme_op_keylist_from_data_start; gpgme_op_keylist_next; gpgme_op_keylist_result; gpgme_op_keylist_start; gpgme_op_sign; gpgme_op_sign_result; gpgme_op_sign_start; gpgme_op_trustlist_end; gpgme_op_trustlist_next; gpgme_op_trustlist_start; gpgme_op_verify; gpgme_op_verify_result; gpgme_op_verify_start; gpgme_wait; gpgme_data_new_with_read_cb; gpgme_data_rewind; gpgme_get_sig_status; gpgme_get_sig_string_attr; gpgme_get_sig_ulong_attr; gpgme_get_sig_key; gpgme_key_get_string_attr; gpgme_key_get_ulong_attr; gpgme_key_sig_get_string_attr; gpgme_key_sig_get_ulong_attr; gpgme_op_import_ext; gpgme_trust_item_get_int_attr; gpgme_trust_item_get_string_attr; gpgme_trust_item_release; gpgme_err_code_from_syserror; gpgme_err_set_errno; local: *; }; diff --git a/tests/run-encrypt.c b/tests/run-encrypt.c index 51e2d60f..94084694 100644 --- a/tests/run-encrypt.c +++ b/tests/run-encrypt.c @@ -1,322 +1,340 @@ /* run-encrypt.c - Helper to perform an encrypt operation * Copyright (C) 2016 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #define PGM "run-encrypt" #include "run-support.h" static int verbose; +static char * +xstrdup (const char *string) +{ + char *p = strdup (string); + if (!p) + { + fprintf (stderr, "strdup failed\n"); + exit (2); + } + return p; +} + + static gpg_error_t status_cb (void *opaque, const char *keyword, const char *value) { (void)opaque; fprintf (stderr, "status_cb: %s %s\n", nonnull(keyword), nonnull(value)); return 0; } static void progress_cb (void *opaque, const char *what, int type, int current, int total) { (void)opaque; (void)type; if (total) fprintf (stderr, "progress for '%s' %u%% (%d of %d)\n", nonnull (what), (unsigned)(((double)current / total) * 100), current, total); else fprintf (stderr, "progress for '%s' %d\n", nonnull(what), current); fflush (stderr); } static void print_result (gpgme_encrypt_result_t result) { gpgme_invalid_key_t invkey; for (invkey = result->invalid_recipients; invkey; invkey = invkey->next) printf ("Encryption key `%s' not used: %s <%s>\n", nonnull (invkey->fpr), gpg_strerror (invkey->reason), gpg_strsource (invkey->reason)); } static int show_usage (int ex) { fputs ("usage: " PGM " [options] FILE\n\n" "Options:\n" " --verbose run in verbose mode\n" " --status print status lines from the backend\n" " --progress print progress info\n" " --openpgp use the OpenPGP protocol (default)\n" " --cms use the CMS protocol\n" " --uiserver use the UI server\n" " --loopback use a loopback pinentry\n" " --key NAME encrypt to key NAME\n" + " --keystring NAMES encrypt to ';' delimited NAMES\n" " --throw-keyids use this option\n" " --no-symkey-cache disable the use of that cache\n" " --wrap assume input is valid OpenPGP message\n" " --symmetric encrypt symmetric (OpenPGP only)\n" , stderr); exit (ex); } int main (int argc, char **argv) { int last_argc = -1; gpgme_error_t err; gpgme_ctx_t ctx; - const char *key_string = NULL; gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP; gpgme_data_t in, out; gpgme_encrypt_result_t result; int print_status = 0; int print_progress = 0; int use_loopback = 0; char *keyargs[10]; gpgme_key_t keys[10+1]; int keycount = 0; + char *keystring = NULL; int i; gpgme_encrypt_flags_t flags = GPGME_ENCRYPT_ALWAYS_TRUST; gpgme_off_t offset; int no_symkey_cache = 0; if (argc) { argc--; argv++; } if (DIM(keys) != DIM(keyargs)+1) abort (); while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--help")) show_usage (0); else if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strcmp (*argv, "--status")) { print_status = 1; argc--; argv++; } else if (!strcmp (*argv, "--progress")) { print_progress = 1; argc--; argv++; } else if (!strcmp (*argv, "--openpgp")) { protocol = GPGME_PROTOCOL_OpenPGP; argc--; argv++; } else if (!strcmp (*argv, "--cms")) { protocol = GPGME_PROTOCOL_CMS; argc--; argv++; } else if (!strcmp (*argv, "--uiserver")) { protocol = GPGME_PROTOCOL_UISERVER; argc--; argv++; } else if (!strcmp (*argv, "--key")) { argc--; argv++; if (!argc) show_usage (1); if (keycount == DIM (keyargs)) show_usage (1); keyargs[keycount++] = *argv; argc--; argv++; } + else if (!strcmp (*argv, "--keystring")) + { + argc--; argv++; + if (!argc) + show_usage (1); + keystring = xstrdup (*argv); + for (i=0; keystring[i]; i++) + if (keystring[i] == ';') + keystring[i] = '\n'; + argc--; argv++; + } else if (!strcmp (*argv, "--throw-keyids")) { flags |= GPGME_ENCRYPT_THROW_KEYIDS; argc--; argv++; } else if (!strcmp (*argv, "--wrap")) { flags |= GPGME_ENCRYPT_WRAP; argc--; argv++; } else if (!strcmp (*argv, "--loopback")) { use_loopback = 1; argc--; argv++; } else if (!strcmp (*argv, "--symmetric")) { flags |= GPGME_ENCRYPT_SYMMETRIC; argc--; argv++; } else if (!strcmp (*argv, "--no-symkey-cache")) { no_symkey_cache = 1; argc--; argv++; } else if (!strncmp (*argv, "--", 2)) show_usage (1); } if (argc != 1) show_usage (1); - if (key_string && protocol == GPGME_PROTOCOL_UISERVER) - { - fprintf (stderr, PGM ": ignoring --key in UI-server mode\n"); - key_string = NULL; - } - - if (!key_string) - key_string = "test"; - init_gpgme (protocol); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, protocol); gpgme_set_armor (ctx, 1); if (print_status) { gpgme_set_status_cb (ctx, status_cb, NULL); gpgme_set_ctx_flag (ctx, "full-status", "1"); } if (print_progress) gpgme_set_progress_cb (ctx, progress_cb, NULL); if (use_loopback) { gpgme_set_pinentry_mode (ctx, GPGME_PINENTRY_MODE_LOOPBACK); gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); } if (no_symkey_cache) { err = gpgme_set_ctx_flag (ctx, "no-symkey-cache", "1"); if (err) { fprintf (stderr, PGM ": error setting no-symkey-cache: %s\n", gpgme_strerror (err)); exit (1); } } for (i=0; i < keycount; i++) { err = gpgme_get_key (ctx, keyargs[i], &keys[i], 0); fail_if_err (err); } keys[i] = NULL; err = gpgme_data_new_from_file (&in, *argv, 1); if (err) { fprintf (stderr, PGM ": error reading `%s': %s\n", *argv, gpg_strerror (err)); exit (1); } offset = gpgme_data_seek (in, 0, SEEK_END); if (offset == (gpgme_off_t)(-1)) { err = gpg_error_from_syserror (); fprintf (stderr, PGM ": error seeking `%s': %s\n", *argv, gpg_strerror (err)); exit (1); } if (gpgme_data_seek (in, 0, SEEK_SET) == (gpgme_off_t)(-1)) { err = gpg_error_from_syserror (); fprintf (stderr, PGM ": error seeking `%s': %s\n", *argv, gpg_strerror (err)); exit (1); } { char numbuf[50]; char *p; p = numbuf + sizeof numbuf; *--p = 0; do { *--p = '0' + (offset % 10); offset /= 10; } while (offset); err = gpgme_data_set_flag (in, "size-hint", p); if (err) { fprintf (stderr, PGM ": error setting size-hint for `%s': %s\n", *argv, gpg_strerror (err)); exit (1); } } err = gpgme_data_new (&out); fail_if_err (err); - err = gpgme_op_encrypt (ctx, keycount ? keys : NULL, flags, in, out); + err = gpgme_op_encrypt_ext (ctx, keycount ? keys : NULL, keystring, + flags, in, out); result = gpgme_op_encrypt_result (ctx); if (result) print_result (result); if (err) { fprintf (stderr, PGM ": encrypting failed: %s\n", gpg_strerror (err)); exit (1); } fputs ("Begin Output:\n", stdout); print_data (out); fputs ("End Output.\n", stdout); gpgme_data_release (out); gpgme_data_release (in); for (i=0; i < keycount; i++) gpgme_key_unref (keys[i]); gpgme_release (ctx); + free (keystring); return 0; }