Imported Upstream version 1.4.2
[platform/upstream/gpgme.git] / doc / gpgme.texi
1 \input texinfo                   @c -*- mode: texinfo; coding: latin-1; -*-
2 @documentencoding ISO-8859-1
3 @setfilename gpgme.info
4 @settitle The `GnuPG Made Easy' Reference Manual
5
6 @dircategory GNU Libraries
7 @direntry
8 * @acronym{GPGME}: (gpgme).          Adding support for cryptography to your program.
9 @end direntry
10
11 @c Unify some of the indices.
12 @syncodeindex tp fn
13 @syncodeindex pg fn
14
15 @copying
16 Copyright @copyright{} 2002, 2003, 2004, 2005, 2006, 2007,
17 2008, 2010, 2012, 2013 g10 Code GmbH.
18
19 @quotation
20 Permission is granted to copy, distribute and/or modify this document
21 under the terms of the GNU General Public License as published by the
22 Free Software Foundation; either version 3 of the License, or (at your
23 option) any later version. The text of the license can be found in the
24 section entitled ``Copying''.
25 @end quotation
26
27 This document is distributed in the hope that it will be useful, but
28 WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
30 General Public License for more details.
31 @end copying
32
33 @include version.texi
34
35 @c Macros used by the description of the UI server protocol
36 @macro clnt
37   @sc{c:} @c
38 @end macro
39 @macro srvr
40   @sc{s:} @c
41 @end macro
42
43
44 @c
45 @c  T I T L E  P A G E
46 @c
47 @ifinfo
48 This file documents the @acronym{GPGME} library.
49
50 This is Edition @value{EDITION}, last updated @value{UPDATED}, of
51 @cite{The `GnuPG Made Easy' Reference Manual}, for Version
52 @value{VERSION}.
53
54 @c NOTE: Don't forget to update the year for the TeX version, too.
55 @insertcopying
56
57 @end ifinfo
58
59 @c We do not want that bastard short titlepage.
60 @c @iftex
61 @c @shorttitlepage The `GnuPG Made Easy' Reference Manual
62 @c @end iftex
63 @titlepage
64 @center @titlefont{The `GnuPG Made Easy'}
65 @sp 1
66 @center @titlefont{Reference Manual}
67 @sp 6
68 @center Edition @value{EDITION}
69 @sp 1
70 @center last updated @value{UPDATED}
71 @sp 1
72 @center for version @value{VERSION}
73 @page
74 @vskip 0pt plus 1filll
75 Published by g10 Code GmbH@* Hüttenstr. 61@* 40699 Erkrath, Germany
76
77 @insertcopying
78 @end titlepage
79 @page
80
81 @summarycontents
82 @contents
83
84 @ifnottex
85 @node Top
86 @top Main Menu
87 This is Edition @value{EDITION}, last updated @value{UPDATED}, of
88 @cite{The `GnuPG Made Easy' Reference Manual}, for Version
89 @value{VERSION} of the @acronym{GPGME} library.
90 @end ifnottex
91
92 @menu
93 * Introduction::                  How to use this manual.
94 * Preparation::                   What you should do before using the library.
95 * Protocols and Engines::         Supported crypto protocols.
96 * Algorithms::                    Supported algorithms.
97 * Error Handling::                Error numbers and their meanings.
98 * Exchanging Data::               Passing data to and from @acronym{GPGME}.
99 * Contexts::                      Handling @acronym{GPGME} contexts.
100
101 Appendices
102
103 * UI Server Protocol::            The GnuPG UI Server Protocol.
104 * Debugging::                     How to solve problems.
105
106 * Library Copying::               The GNU Lesser General Public License says
107                                   how you can copy and share `GnuPG Made Easy'.
108 * Copying::                       The GNU General Public License says how you
109                                   can copy and share this manual.
110
111 Indices
112
113 * Concept Index::                 Index of concepts and programs.
114 * Function and Data Index::       Index of functions, variables and data types.
115
116
117 @detailmenu
118  --- The Detailed Node Listing ---
119
120 Introduction
121
122 * Getting Started::               Purpose of the manual, and how to use it.
123 * Features::                      Reasons to install and use @acronym{GPGME}.
124 * Overview::                      Basic architecture of the @acronym{GPGME} library.
125
126 Preparation
127
128 * Header::                        What header file you need to include.
129 * Building the Source::           Compiler options to be used.
130 * Largefile Support (LFS)::       How to use @acronym{GPGME} with LFS.
131 * Using Automake::                Compiler options to be used the easy way.
132 * Using Libtool::                 Avoiding compiler options entirely.
133 * Library Version Check::         Getting and verifying the library version.
134 * Signal Handling::               How @acronym{GPGME} affects signal handling.
135 * Multi Threading::               How @acronym{GPGME} can be used in an MT environment.
136
137 Protocols and Engines
138
139 * Engine Version Check::          Verifying the engine version.
140 * Engine Information::            Obtaining more information about the engines.
141 * Engine Configuration::          Changing the engine configuration.
142 * OpenPGP::                       Support for the OpenPGP protocol.
143 * Cryptographic Message Syntax::  Support for the CMS.
144
145 Algorithms
146
147 * Public Key Algorithms::         A list of all public key algorithms.
148 * Hash Algorithms::               A list of all hash algorithms.
149
150 Error Handling
151
152 * Error Values::                  The error value and what it means.
153 * Error Codes::                   A list of important error codes.
154 * Error Sources::                 A list of important error sources.
155 * Error Strings::                 How to get a descriptive string from a value.
156
157 Exchanging Data
158
159 * Creating Data Buffers::         Creating new data buffers.
160 * Destroying Data Buffers::       Releasing data buffers.
161 * Manipulating Data Buffers::     Operations on data buffers.
162
163 Creating Data Buffers
164
165 * Memory Based Data Buffers::     Creating memory based data buffers.
166 * File Based Data Buffers::       Creating file based data buffers.
167 * Callback Based Data Buffers::   Creating callback based data buffers.
168
169 Manipulating Data Buffers
170
171 * Data Buffer I/O Operations::    I/O operations on data buffers.
172 * Data Buffer Meta-Data::         Meta-data manipulation of data buffers.
173
174 Contexts
175
176 * Creating Contexts::             Creating new @acronym{GPGME} contexts.
177 * Destroying Contexts::           Releasing @acronym{GPGME} contexts.
178 * Result Management::             Managing the result of crypto operations.
179 * Context Attributes::            Setting properties of a context.
180 * Key Management::                Managing keys with @acronym{GPGME}.
181 * Trust Item Management::         Managing trust items with @acronym{GPGME}.
182 * Crypto Operations::             Using a context for cryptography.
183 * Run Control::                   Controlling how operations are run.
184
185 Context Attributes
186
187 * Protocol Selection::            Selecting the protocol used by a context.
188 * Crypto Engine::                 Configuring the crypto engine.
189 * ASCII Armor::                   Requesting @acronym{ASCII} armored output.
190 * Text Mode::                     Choosing canonical text mode.
191 * Included Certificates::         Including a number of certificates.
192 * Key Listing Mode::              Selecting key listing mode.
193 * Passphrase Callback::           Getting the passphrase from the user.
194 * Progress Meter Callback::       Being informed about the progress.
195 * Locale::                        Setting the locale of a context.
196
197 Key Management
198
199 * Listing Keys::                  Browsing the list of available keys.
200 * Information About Keys::        Requesting detailed information about keys.
201 * Key Signatures::                Listing the signatures on a key.
202 * Manipulating Keys::             Operations on keys.
203 * Generating Keys::               Creating new key pairs.
204 * Exporting Keys::                Retrieving key data from the key ring.
205 * Importing Keys::                Adding keys to the key ring.
206 * Deleting Keys::                 Removing keys from the key ring.
207 * Advanced Key Editing::          Advanced key edit operation.
208
209 Trust Item Management
210
211 * Listing Trust Items::           Browsing the list of available trust items.
212 * Information About Trust Items:: Requesting information about trust items.
213 * Manipulating Trust Items::      Operations on trust items.
214
215 Crypto Operations
216
217 * Decrypt::                       Decrypting a ciphertext.
218 * Verify::                        Verifying a signature.
219 * Decrypt and Verify::            Decrypting a signed ciphertext.
220 * Sign::                          Creating a signature.
221 * Encrypt::                       Encrypting a plaintext.
222
223 Sign
224
225 * Selecting Signers::             How to choose the keys to sign with.
226 * Creating a Signature::          How to create a signature.
227 * Signature Notation Data::       How to add notation data to a signature.
228
229 Encrypt
230
231 * Encrypting a Plaintext::        How to encrypt a plaintext.
232
233 Run Control
234
235 * Waiting For Completion::        Waiting until an operation is completed.
236 * Using External Event Loops::    Advanced control over what happens when.
237 * Cancellation::                  How to end pending operations prematurely.
238
239 Using External Event Loops
240
241 * I/O Callback Interface::        How I/O callbacks are registered.
242 * Registering I/O Callbacks::     How to use I/O callbacks for a context.
243 * I/O Callback Example::          An example how to use I/O callbacks.
244 * I/O Callback Example GTK+::     How to integrate @acronym{GPGME} in GTK+.
245 * I/O Callback Example GDK::      How to integrate @acronym{GPGME} in GDK.
246 * I/O Callback Example Qt::       How to integrate @acronym{GPGME} in Qt.
247
248 @end detailmenu
249 @end menu
250
251 @node Introduction
252 @chapter Introduction
253
254 `GnuPG Made Easy' (@acronym{GPGME}) is a C language library that
255 allows to add support for cryptography to a program.  It is designed
256 to make access to public key crypto engines like GnuPG or GpgSM easier
257 for applications.  @acronym{GPGME} provides a high-level crypto API
258 for encryption, decryption, signing, signature verification and key
259 management.
260
261 @acronym{GPGME} uses GnuPG and GpgSM as its backends to support
262 OpenPGP and the Cryptographic Message Syntax (CMS).
263
264 @menu
265 * Getting Started::               Purpose of the manual, and how to use it.
266 * Features::                      Reasons to install and use @acronym{GPGME}.
267 * Overview::                      Basic architecture of the @acronym{GPGME} library.
268 @end menu
269
270
271 @node Getting Started
272 @section Getting Started
273
274 This manual documents the @acronym{GPGME} library programming
275 interface.  All functions and data types provided by the library are
276 explained.
277
278 The reader is assumed to possess basic knowledge about cryptography in
279 general, and public key cryptography in particular.  The underlying
280 cryptographic engines that are used by the library are not explained,
281 but where necessary, special features or requirements by an engine are
282 mentioned as far as they are relevant to @acronym{GPGME} or its users.
283
284 This manual can be used in several ways.  If read from the beginning
285 to the end, it gives a good introduction into the library and how it
286 can be used in an application.  Forward references are included where
287 necessary.  Later on, the manual can be used as a reference manual to
288 get just the information needed about any particular interface of the
289 library.  Experienced programmers might want to start looking at the
290 examples at the end of the manual, and then only read up those parts
291 of the interface which are unclear.
292
293
294 @node Features
295 @section Features
296
297 @acronym{GPGME} has a couple of advantages over other libraries doing
298 a similar job, and over implementing support for GnuPG or other crypto
299 engines into your application directly.
300
301 @table @asis
302 @item it's free software
303 Anybody can use, modify, and redistribute it under the terms of the GNU
304 Lesser General Public License (@pxref{Library Copying}).
305
306 @item it's flexible
307 @acronym{GPGME} provides transparent support for several cryptographic
308 protocols by different engines.  Currently, @acronym{GPGME} supports
309 the OpenPGP protocol using GnuPG as the backend, and the Cryptographic
310 Message Syntax using GpgSM as the backend.
311
312 @item it's easy
313 @acronym{GPGME} hides the differences between the protocols and
314 engines from the programmer behind an easy-to-use interface.  This way
315 the programmer can focus on the other parts of the program, and still
316 integrate strong cryptography in his application.  Once support for
317 @acronym{GPGME} has been added to a program, it is easy to add support
318 for other crypto protocols once @acronym{GPGME} backends provide them.
319 @end table
320
321
322 @node Overview
323 @section Overview
324
325 @acronym{GPGME} provides a data abstraction that is used to pass data
326 to the crypto engine, and receive returned data from it.  Data can be
327 read from memory or from files, but it can also be provided by a
328 callback function.
329
330 The actual cryptographic operations are always set within a context.
331 A context provides configuration parameters that define the behaviour
332 of all operations performed within it.  Only one operation per context
333 is allowed at any time, but when one operation is finished, you can
334 run the next operation in the same context.  There can be more than
335 one context, and all can run different operations at the same time.
336
337 Furthermore, @acronym{GPGME} has rich key management facilities
338 including listing keys, querying their attributes, generating,
339 importing, exporting and deleting keys, and acquiring information
340 about the trust path.
341
342 With some precautions, @acronym{GPGME} can be used in a multi-threaded
343 environment, although it is not completely thread safe and thus needs
344 the support of the application.
345
346
347 @node Preparation
348 @chapter Preparation
349
350 To use @acronym{GPGME}, you have to perform some changes to your
351 sources and the build system.  The necessary changes are small and
352 explained in the following sections.  At the end of this chapter, it
353 is described how the library is initialized, and how the requirements
354 of the library are verified.
355
356 @menu
357 * Header::                        What header file you need to include.
358 * Building the Source::           Compiler options to be used.
359 * Largefile Support (LFS)::       How to use @acronym{GPGME} with LFS.
360 * Using Automake::                Compiler options to be used the easy way.
361 * Using Libtool::                 Avoiding compiler options entirely.
362 * Library Version Check::         Getting and verifying the library version.
363 * Signal Handling::               How @acronym{GPGME} affects signal handling.
364 * Multi Threading::               How @acronym{GPGME} can be used in an MT environment.
365 @end menu
366
367
368 @node Header
369 @section Header
370 @cindex header file
371 @cindex include file
372
373 All interfaces (data types and functions) of the library are defined
374 in the header file `gpgme.h'.  You must include this in all programs
375 using the library, either directly or through some other header file,
376 like this:
377
378 @example
379 #include <gpgme.h>
380 @end example
381
382 The name space of @acronym{GPGME} is @code{gpgme_*} for function names
383 and data types and @code{GPGME_*} for other symbols.  Symbols internal
384 to @acronym{GPGME} take the form @code{_gpgme_*} and @code{_GPGME_*}.
385
386 Because @acronym{GPGME} makes use of the GPG Error library, using
387 @acronym{GPGME} will also use the @code{GPG_ERR_*} name space
388 directly, and the @code{gpg_err*} and @code{gpg_str*} name space
389 indirectly.
390
391
392 @node Building the Source
393 @section Building the Source
394 @cindex compiler options
395 @cindex compiler flags
396
397 If you want to compile a source file including the `gpgme.h' header
398 file, you must make sure that the compiler can find it in the
399 directory hierarchy.  This is accomplished by adding the path to the
400 directory in which the header file is located to the compilers include
401 file search path (via the @option{-I} option).
402
403 However, the path to the include file is determined at the time the
404 source is configured.  To solve this problem, gpgme ships with a small
405 helper program @command{gpgme-config} that knows about the path to the
406 include file and other configuration options.  The options that need
407 to be added to the compiler invocation at compile time are output by
408 the @option{--cflags} option to @command{gpgme-config}.  The following
409 example shows how it can be used at the command line:
410
411 @example
412 gcc -c foo.c `gpgme-config --cflags`
413 @end example
414
415 Adding the output of @samp{gpgme-config --cflags} to the compiler
416 command line will ensure that the compiler can find the
417 @acronym{GPGME} header file.
418
419 A similar problem occurs when linking the program with the library.
420 Again, the compiler has to find the library files.  For this to work,
421 the path to the library files has to be added to the library search
422 path (via the @option{-L} option).  For this, the option
423 @option{--libs} to @command{gpgme-config} can be used.  For
424 convenience, this option also outputs all other options that are
425 required to link the program with @acronym{GPGME} (in particular, the
426 @samp{-lgpgme} option).  The example shows how to link @file{foo.o}
427 with the @acronym{GPGME} library to a program @command{foo}.
428
429 @example
430 gcc -o foo foo.o `gpgme-config --libs`
431 @end example
432
433 Of course you can also combine both examples to a single command by
434 specifying both options to @command{gpgme-config}:
435
436 @example
437 gcc -o foo foo.c `gpgme-config --cflags --libs`
438 @end example
439
440 If you want to link to one of the thread-safe versions of
441 @acronym{GPGME}, you must specify the @option{--thread} option before
442 any other option to select the thread package you want to link with.
443 Supported thread packages are @option{--thread=pth} and
444 @option{--thread=pthread}.
445
446
447 @node Largefile Support (LFS)
448 @section Largefile Support (LFS)
449 @cindex largefile support
450 @cindex LFS
451
452 @acronym{GPGME} is compiled with largefile support by default, if it
453 is available on the system.  This means that GPGME supports files
454 larger than two gigabyte in size, if the underlying operating system
455 can.  On some systems, largefile support is already the default.  On
456 such systems, nothing special is required.  However, some systems
457 provide only support for files up to two gigabyte in size by default.
458 Support for larger file sizes has to be specifically enabled.
459
460 To make a difficult situation even more complex, such systems provide
461 two different types of largefile support.  You can either get all
462 relevant functions replaced with alternatives that are largefile
463 capable, or you can get new functions and data types for largefile
464 support added.  Those new functions have the same name as their
465 smallfile counterparts, but with a suffix of 64.
466
467 An example: The data type @code{off_t} is 32 bit wide on GNU/Linux PC
468 systems.  To address offsets in large files, you can either enable
469 largefile support add-on.  Then a new data type @code{off64_t} is
470 provided, which is 64 bit wide.  Or you can replace the existing
471 @code{off_t} data type with its 64 bit wide counterpart.  All
472 occurences of @code{off_t} are then automagically replaced.
473
474 As if matters were not complex enough, there are also two different
475 types of file descriptors in such systems.  This is important because
476 if file descriptors are exchanged between programs that use a
477 different maximum file size, certain errors must be produced on some
478 file descriptors to prevent subtle overflow bugs from occuring.
479
480 As you can see, supporting two different maximum file sizes at the
481 same time is not at all an easy task.  However, the maximum file size
482 does matter for @acronym{GPGME}, because some data types it uses in
483 its interfaces are affected by that.  For example, the @code{off_t}
484 data type is used in the @code{gpgme_data_seek} function, to match its
485 @acronym{POSIX} counterpart.  This affects the call-frame of the
486 function, and thus the ABI of the library.  Furthermore, file
487 descriptors can be exchanged between GPGME and the application.
488
489 For you as the user of the library, this means that your program must
490 be compiled in the same file size mode as the library.  Luckily, there
491 is absolutely no valid reason for new programs to not enable largefile
492 support by default and just use that.  The compatibility modes (small
493 file sizes or dual mode) can be considered an historic artefact, only
494 useful to allow for a transitional period.
495
496 On POSIX platforms @acronym{GPGME} is compiled using largefile support
497 by default.  This means that your application must do the same, at
498 least as far as it is relevant for using the @file{gpgme.h} header
499 file.  All types in this header files refer to their largefile
500 counterparts, if they are different from any default types on the
501 system.
502
503 On 32 and 64 bit Windows platforms @code{off_t} is declared as 32 bit
504 signed integer.  There is no specific support for LFS in the C
505 library.  The recommendation from Microsoft is to use the native
506 interface (@code{CreateFile} et al.) for large files.  Released binary
507 versions of @acronym{GPGME} (libgpgme-11.dll) have always been build
508 with a 32 bit @code{off_t}.  To avoid an ABI break we stick to this
509 convention for 32 bit Windows by using @code{long} there.
510 @acronym{GPGME} versions for 64 bit Windows have never been released
511 and thus we are able to use @code{int64_t} instead of @code{off_t}
512 there.  For easier migration the typedef @code{gpgme_off_t} has been
513 defined.  The reason we cannot use @code{off_t} directly is that some
514 toolchains (e.g. mingw64) introduce a POSIX compatible hack for
515 @code{off_t}.  Some widely used toolkits make use of this hack and in
516 turn @acronym{GPGME} would need to use it also.  However, this would
517 introduce an ABI break and existing software making use of libgpgme
518 might suffer from a severe break.  Thus with version 1.4.2 we
519 redefined all functions using @code{off_t} to use @code{gpgme_off_t}
520 which is defined as explained above.  This way we keep the ABI well
521 defined and independent of any toolchain hacks.  The bottom line is
522 that LFS support in @acronym{GPGME} is only available on 64 bit
523 versions of Windows.
524
525 On POSIX platforms you can enable largefile support, if it is
526 different from the default on the system the application is compiled
527 on, by using the Autoconf macro @code{AC_SYS_LARGEFILE}.  If you do
528 this, then you don't need to worry about anything else: It will just
529 work.  In this case you might also want to use @code{AC_FUNC_FSEEKO}
530 to take advantage of some new interfaces, and @code{AC_TYPE_OFF_T}
531 (just in case).
532
533 If you do not use Autoconf, you can define the preprocessor symbol
534 @code{_FILE_OFFSET_BITS} to 64 @emph{before} including any header
535 files, for example by specifying the option
536 @code{-D_FILE_OFFSET_BITS=64} on the compiler command line.  You will
537 also want to define the preprocessor symbol @code{LARGEFILE_SOURCE} to
538 1 in this case, to take advantage of some new interfaces.
539
540 If you do not want to do either of the above, you probably know enough
541 about the issue to invent your own solution.  Just keep in mind that
542 the @acronym{GPGME} header file expects that largefile support is
543 enabled, if it is available.  In particular, we do not support dual
544 mode (@code{_LARGEFILE64_SOURCE}).
545
546
547 @node Using Automake
548 @section Using Automake
549 @cindex automake
550 @cindex autoconf
551
552 It is much easier if you use GNU Automake instead of writing your own
553 Makefiles.  If you do that you do not have to worry about finding and
554 invoking the @command{gpgme-config} script at all.  @acronym{GPGME}
555 provides an extension to Automake that does all the work for you.
556
557 @c A simple macro for optional variables.
558 @macro ovar{varname}
559 @r{[}@var{\varname\}@r{]}
560 @end macro
561 @defmac AM_PATH_GPGME (@ovar{minimum-version}, @ovar{action-if-found}, @ovar{action-if-not-found})
562 @defmacx AM_PATH_GPGME_PTH (@ovar{minimum-version}, @ovar{action-if-found}, @ovar{action-if-not-found})
563 @defmacx AM_PATH_GPGME_PTHREAD (@ovar{minimum-version}, @ovar{action-if-found}, @ovar{action-if-not-found})
564 Check whether @acronym{GPGME} (at least version @var{minimum-version},
565 if given) exists on the host system.  If it is found, execute
566 @var{action-if-found}, otherwise do @var{action-if-not-found}, if
567 given.
568
569 Additionally, the function defines @code{GPGME_CFLAGS} to the flags
570 needed for compilation of the program to find the @file{gpgme.h}
571 header file, and @code{GPGME_LIBS} to the linker flags needed to link
572 the program to the @acronym{GPGME} library.
573
574 @code{AM_PATH_GPGME_PTH} checks for the version of @acronym{GPGME}
575 that can be used with GNU Pth, and defines @code{GPGME_PTH_CFLAGS} and
576 @code{GPGME_PTH_LIBS}.
577
578 @code{AM_PATH_GPGME_PTHREAD} checks for the version of @acronym{GPGME}
579 that can be used with the native pthread implementation, and defines
580 @code{GPGME_PTHREAD_CFLAGS} and @code{GPGME_PTHREAD_LIBS}.
581 @end defmac
582
583 You can use the defined Autoconf variables like this in your
584 @file{Makefile.am}:
585
586 @example
587 AM_CPPFLAGS = $(GPGME_CFLAGS)
588 LDADD = $(GPGME_LIBS)
589 @end example
590
591
592 @node Using Libtool
593 @section Using Libtool
594 @cindex libtool
595
596 The easiest way is to just use GNU Libtool.  If you use libtool, and
597 link to @code{libgpgme.la}, @code{libgpgme-pth.la} or
598 @code{libgpgme-pthread.la} respectively, everything will be done
599 automatically by Libtool.
600
601
602 @node Library Version Check
603 @section Library Version Check
604 @cindex version check, of the library
605
606 @deftypefun {const char *} gpgme_check_version (@w{const char *@var{required_version}})
607 The function @code{gpgme_check_version} has four purposes.  It can be
608 used to retrieve the version number of the library.  In addition it
609 can verify that the version number is higher than a certain required
610 version number.  In either case, the function initializes some
611 sub-systems, and for this reason alone it must be invoked early in
612 your program, before you make use of the other functions in
613 @acronym{GPGME}.  The last purpose is to run selftests.
614
615 As a side effect for W32 based systems, the socket layer will get
616 initialized.
617
618
619 If @var{required_version} is @code{NULL}, the function returns a
620 pointer to a statically allocated string containing the version number
621 of the library.
622
623 If @var{required_version} is not @code{NULL}, it should point to a
624 string containing a version number, and the function checks that the
625 version of the library is at least as high as the version number
626 provided.  In this case, the function returns a pointer to a
627 statically allocated string containing the version number of the
628 library.  If @var{REQUIRED_VERSION} is not a valid version number, or
629 if the version requirement is not met, the function returns
630 @code{NULL}.
631
632 If you use a version of a library that is backwards compatible with
633 older releases, but contains additional interfaces which your program
634 uses, this function provides a run-time check if the necessary
635 features are provided by the installed version of the library.
636
637 If a selftest fails, the function may still succeed.  Selftest errors
638 are returned later when invoking @code{gpgme_new}, so that a detailed
639 error code can be returned (historically, @code{gpgme_check_version}
640 does not return a detailed error code).
641 @end deftypefun
642
643
644 @deftypefun {int} gpgme_set_global_flag  @
645             (@w{const char *@var{name}}, @
646             @w{const char *@var{value}})
647
648 On some systems it is not easy to set environment variables and thus
649 hard to use @acronym{GPGME}'s internal trace facility for debugging.
650 This function has been introduced as an alternative way to enable
651 debugging.  It is important to assure that only one thread accesses
652 @acronym{GPGME} functions between a call to this function and after
653 the return from the call to @code{gpgme_check_version}.
654
655 To enable debugging, you need to call this function as early as
656 possible --- even before @code{gpgme_check_version} --- with the
657 string ``debug'' for @var{name} and @var{value} identical to the value
658 used with the environment variable @code{GPGME_DEBUG}.
659
660 This function returns @code{0} on success.  In contrast to other
661 functions the non-zero return value on failure does not convey any
662 error code.  For setting ``debug'' the only possible error cause is an
663 out of memory condition; which would exhibit itself later anyway.
664 Thus the return value may be ignored.
665 @end deftypefun
666
667
668 After initializing @acronym{GPGME}, you should set the locale
669 information to the locale required for your output terminal.  This
670 locale information is needed for example for the curses and Gtk
671 pinentry.  Here is an example of a complete initialization:
672
673 @example
674 #include <locale.h>
675 #include <gpgme.h>
676
677 void
678 init_gpgme (void)
679 @{
680   /* Initialize the locale environment.  */
681   setlocale (LC_ALL, "");
682   gpgme_check_version (NULL);
683   gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
684 #ifdef LC_MESSAGES
685   gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));
686 #endif
687 @}
688 @end example
689
690 Note that you are highly recommended to initialize the locale settings
691 like this.  @acronym{GPGME} can not do this for you because it would
692 not be thread safe.  The conditional on LC_MESSAGES is only necessary
693 for portability to W32 systems.
694
695
696 @node Signal Handling
697 @section Signal Handling
698 @cindex signals
699 @cindex signal handling
700
701 The @acronym{GPGME} library communicates with child processes (the
702 crypto engines).  If a child process dies unexpectedly, for example
703 due to a bug, or system problem, a @code{SIGPIPE} signal will be
704 delivered to the application.  The default action is to abort the
705 program.  To protect against this, @code{gpgme_check_version} sets the
706 @code{SIGPIPE} signal action to @code{SIG_IGN}, which means that the
707 signal will be ignored.
708
709 @acronym{GPGME} will only do that if the signal action for
710 @code{SIGPIPE} is @code{SIG_DEF} at the time
711 @code{gpgme_check_version} is called.  If it is something different,
712 @code{GPGME} will take no action.
713
714 This means that if your application does not install any signal
715 handler for @code{SIGPIPE}, you don't need to take any precautions.
716 If you do install a signal handler for @code{SIGPIPE}, you must be
717 prepared to handle any @code{SIGPIPE} events that occur due to
718 @acronym{GPGME} writing to a defunct pipe.  Furthermore, if your
719 application is multi-threaded, and you install a signal action for
720 @code{SIGPIPE}, you must make sure you do this either before
721 @code{gpgme_check_version} is called or afterwards.
722
723
724 @node Multi Threading
725 @section Multi Threading
726 @cindex thread-safeness
727 @cindex multi-threading
728
729 The @acronym{GPGME} library is not entirely thread-safe, but it can
730 still be used in a multi-threaded environment if some care is taken.
731 If the following requirements are met, there should be no race
732 conditions to worry about:
733
734 @itemize @bullet
735 @item
736 @acronym{GPGME} supports the thread libraries pthread and GNU Pth.
737 The support for this has to be enabled at compile time.
738 @acronym{GPGME} will automatically detect the location in which the
739 thread libraries are installed and activate the support for them at
740 build time.
741
742 Support for other thread libraries is very easy to add.  Please
743 contact us if you have the need.
744
745 @item
746 If you want to use @acronym{GPGME} with threads, you must link to the
747 right version of the library.  The name of the right library is
748 @code{libgpgme-} followed by the name of the thread package you use.
749 For example, if you use GNU Pth, the right name is
750 @code{libgpgme-pth}.  Use the Automake macros or
751 @command{gpgme-config} program for simplicity.
752
753
754 @item
755 The function @code{gpgme_check_version} must be called before any
756 other function in the library, because it initializes the thread
757 support subsystem in @acronym{GPGME}.  To achieve this in
758 multi-threaded programs, you must synchronize the memory with respect
759 to other threads that also want to use @acronym{GPGME}.  For this, it
760 is sufficient to call @code{gpgme_check_version} before creating the
761 other threads using @acronym{GPGME}@footnote{At least this is true for
762 POSIX threads, as @code{pthread_create} is a function that
763 synchronizes memory with respects to other threads.  There are many
764 functions which have this property, a complete list can be found in
765 POSIX, IEEE Std 1003.1-2003, Base Definitions, Issue 6, in the
766 definition of the term ``Memory Synchronization''.  For other thread
767 packages other, more relaxed or more strict rules may apply.}.
768
769 @item
770 Any @code{gpgme_data_t} and @code{gpgme_ctx_t} object must only be
771 accessed by one thread at a time.  If multiple threads want to deal
772 with the same object, the caller has to make sure that operations on
773 that object are fully synchronized.
774
775 @item
776 Only one thread at any time is allowed to call @code{gpgme_wait}.  If
777 multiple threads call this function, the caller must make sure that
778 all invocations are fully synchronized.  It is safe to start
779 asynchronous operations while a thread is running in gpgme_wait.
780
781 @item
782 The function @code{gpgme_strerror} is not thread safe.  You have to
783 use @code{gpgme_strerror_r} instead.
784 @end itemize
785
786
787 @node Protocols and Engines
788 @chapter Protocols and Engines
789 @cindex protocol
790 @cindex engine
791 @cindex crypto engine
792 @cindex backend
793 @cindex crypto backend
794
795 @acronym{GPGME} supports several cryptographic protocols, however, it
796 does not implement them.  Rather it uses backends (also called
797 engines) which implement the protocol.  @acronym{GPGME} uses
798 inter-process communication to pass data back and forth between the
799 application and the backend, but the details of the communication
800 protocol and invocation of the backend is completely hidden by the
801 interface.  All complexity is handled by @acronym{GPGME}.  Where an
802 exchange of information between the application and the backend is
803 necessary, @acronym{GPGME} provides the necessary callback function
804 hooks and further interfaces.
805
806 @deftp {Data type} {enum gpgme_protocol_t}
807 @tindex gpgme_protocol_t
808 The @code{gpgme_protocol_t} type specifies the set of possible protocol
809 values that are supported by @acronym{GPGME}.  The following protocols
810 are supported:
811
812 @table @code
813 @item GPGME_PROTOCOL_OpenPGP
814 This specifies the OpenPGP protocol.
815
816 @item GPGME_PROTOCOL_CMS
817 This specifies the Cryptographic Message Syntax.
818
819 @item GPGME_PROTOCOL_ASSUAN
820 Under development.  Please ask on @email{gnupg-devel@@gnupg.org} for help.
821
822 @item GPGME_PROTOCOL_G13
823 Under development.  Please ask on @email{gnupg-devel@@gnupg.org} for help.
824
825 @item GPGME_PROTOCOL_UISERVER
826 Under development.  Please ask on @email{gnupg-devel@@gnupg.org} for help.
827
828 @item GPGME_PROTOCOL_UNKNOWN
829 Reserved for future extension.  You may use this to indicate that the
830 used protocol is not known to the application.  Currently,
831 @acronym{GPGME} does not accept this value in any operation, though,
832 except for @code{gpgme_get_protocol_name}.
833 @end table
834 @end deftp
835
836
837 @deftypefun {const char *} gpgme_get_protocol_name (@w{gpgme_protocol_t @var{protocol}})
838 The function @code{gpgme_get_protocol_name} returns a statically
839 allocated string describing the protocol @var{protocol}, or
840 @code{NULL} if the protocol number is not valid.
841 @end deftypefun
842
843 @menu
844 * Engine Version Check::          Verifying the engine version.
845 * Engine Information::            Obtaining more information about the engines.
846 * Engine Configuration::          Changing the engine configuration.
847 * OpenPGP::                       Support for the OpenPGP protocol.
848 * Cryptographic Message Syntax::  Support for the CMS.
849 @end menu
850
851
852 @node Engine Version Check
853 @section Engine Version Check
854 @cindex version check, of the engines
855
856 @deftypefun gpgme_error_t gpgme_engine_check_version (@w{gpgme_protocol_t @var{protocol}})
857 The function @code{gpgme_engine_check_version} verifies that the
858 engine implementing the protocol @var{PROTOCOL} is installed in the
859 expected path and meets the version requirement of @acronym{GPGME}.
860
861 This function returns the error code @code{GPG_ERR_NO_ERROR} if the
862 engine is available and @code{GPG_ERR_INV_ENGINE} if it is not.
863 @end deftypefun
864
865
866 @node Engine Information
867 @section Engine Information
868 @cindex engine, information about
869
870 @deftp {Data type} {gpgme_engine_info_t}
871 @tindex gpgme_protocol_t
872 The @code{gpgme_engine_info_t} type specifies a pointer to a structure
873 describing a crypto engine.  The structure contains the following
874 elements:
875
876 @table @code
877 @item gpgme_engine_info_t next
878 This is a pointer to the next engine info structure in the linked
879 list, or @code{NULL} if this is the last element.
880
881 @item gpgme_protocol_t protocol
882 This is the protocol for which the crypto engine is used.  You can
883 convert this to a string with @code{gpgme_get_protocol_name} for
884 printing.
885
886 @item const char *file_name
887 This is a string holding the file name of the executable of the crypto
888 engine.  Currently, it is never @code{NULL}, but using @code{NULL} is
889 reserved for future use, so always check before you use it.
890
891 @item const char *home_dir
892 This is a string holding the directory name of the crypto engine's
893 configuration directory.  If it is @code{NULL}, then the default
894 directory is used.
895
896 @item const char *version
897 This is a string containing the version number of the crypto engine.
898 It might be @code{NULL} if the version number can not be determined,
899 for example because the executable doesn't exist or is invalid.
900
901 @item const char *req_version
902 This is a string containing the minimum required version number of the
903 crypto engine for @acronym{GPGME} to work correctly.  This is the
904 version number that @code{gpgme_engine_check_version} verifies
905 against.  Currently, it is never @code{NULL}, but using @code{NULL} is
906 reserved for future use, so always check before you use it.
907 @end table
908 @end deftp
909
910 @deftypefun gpgme_error_t gpgme_get_engine_info (@w{gpgme_engine_info_t *@var{info}})
911 The function @code{gpgme_get_engine_info} returns a linked list of
912 engine info structures in @var{info}.  Each info structure describes
913 the defaults of one configured backend.
914
915 The memory for the info structures is allocated the first time this
916 function is invoked, and must not be freed by the caller.
917
918 This function returns the error code @code{GPG_ERR_NO_ERROR} if
919 successful, and a system error if the memory could not be allocated.
920 @end deftypefun
921
922 Here is an example how you can provide more diagnostics if you receive
923 an error message which indicates that the crypto engine is invalid.
924
925 @example
926 gpgme_ctx_t ctx;
927 gpgme_error_t err;
928
929 [...]
930
931 if (gpgme_err_code (err) == GPG_ERR_INV_ENGINE)
932   @{
933     gpgme_engine_info_t info;
934     err = gpgme_get_engine_info (&info);
935     if (!err)
936       @{
937         while (info && info->protocol != gpgme_get_protocol (ctx))
938           info = info->next;
939         if (!info)
940           fprintf (stderr, "GPGME compiled without support for protocol %s",
941                    gpgme_get_protocol_name (info->protocol));
942         else if (info->file_name && !info->version)
943           fprintf (stderr, "Engine %s not installed properly",
944                    info->file_name);
945         else if (info->file_name && info->version && info->req_version)
946           fprintf (stderr, "Engine %s version %s installed, "
947                    "but at least version %s required", info->file_name,
948                    info->version, info->req_version);
949         else
950           fprintf (stderr, "Unknown problem with engine for protocol %s",
951                    gpgme_get_protocol_name (info->protocol));
952       @}
953   @}
954 @end example
955
956
957 @node Engine Configuration
958 @section Engine Configuration
959 @cindex engine, configuration of
960 @cindex configuration of crypto backend
961
962 You can change the configuration of a backend engine, and thus change
963 the executable program and configuration directory to be used.  You
964 can make these changes the default or set them for some contexts
965 individually.
966
967 @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}})
968 The function @code{gpgme_set_engine_info} changes the default
969 configuration of the crypto engine implementing the protocol
970 @var{proto}.
971
972 @var{file_name} is the file name of the executable program
973 implementing this protocol, and @var{home_dir} is the directory name
974 of the configuration directory for this crypto engine.  If
975 @var{home_dir} is @code{NULL}, the engine's default will be used.
976
977 The new defaults are not applied to already created GPGME contexts.
978
979 This function returns the error code @code{GPG_ERR_NO_ERROR} if
980 successful, or an eror code on failure.
981 @end deftypefun
982
983 The functions @code{gpgme_ctx_get_engine_info} and
984 @code{gpgme_ctx_set_engine_info} can be used to change the engine
985 configuration per context.  @xref{Crypto Engine}.
986
987
988 @node OpenPGP
989 @section OpenPGP
990 @cindex OpenPGP
991 @cindex GnuPG
992 @cindex protocol, GnuPG
993 @cindex engine, GnuPG
994
995 OpenPGP is implemented by GnuPG, the @acronym{GNU} Privacy Guard.
996 This is the first protocol that was supported by @acronym{GPGME}.
997
998 The OpenPGP protocol is specified by @code{GPGME_PROTOCOL_OpenPGP}.
999
1000
1001 @node Cryptographic Message Syntax
1002 @section Cryptographic Message Syntax
1003 @cindex CMS
1004 @cindex cryptographic message syntax
1005 @cindex GpgSM
1006 @cindex protocol, CMS
1007 @cindex engine, GpgSM
1008 @cindex S/MIME
1009 @cindex protocol, S/MIME
1010
1011 @acronym{CMS} is implemented by GpgSM, the S/MIME implementation for
1012 GnuPG.
1013
1014 The @acronym{CMS} protocol is specified by @code{GPGME_PROTOCOL_CMS}.
1015
1016
1017 @node Algorithms
1018 @chapter Algorithms
1019 @cindex algorithms
1020
1021 The crypto backends support a variety of algorithms used in public key
1022 cryptography.@footnote{Some engines also provide symmetric only
1023 encryption; see the description of the encryption function on how to use
1024 this.}  The following sections list the identifiers used to denote such
1025 an algorithm.
1026
1027 @menu
1028 * Public Key Algorithms::         A list of all public key algorithms.
1029 * Hash Algorithms::               A list of all hash algorithms.
1030 @end menu
1031
1032
1033 @node Public Key Algorithms
1034 @section Public Key Algorithms
1035 @cindex algorithms, public key
1036 @cindex public key algorithms
1037
1038 Public key algorithms are used for encryption, decryption, signing and
1039 verification of signatures.
1040
1041 @deftp {Data type} {enum gpgme_pubkey_algo_t}
1042 @tindex gpgme_pubkey_algo_t
1043 The @code{gpgme_pubkey_algo_t} type specifies the set of all public key
1044 algorithms that are supported by @acronym{GPGME}.  Possible values
1045 are:
1046
1047 @table @code
1048 @item GPGME_PK_RSA
1049 This value indicates the RSA (Rivest, Shamir, Adleman) algorithm.
1050
1051 @item GPGME_PK_RSA_E
1052 Deprecated.  This value indicates the RSA (Rivest, Shamir, Adleman)
1053 algorithm for encryption and decryption only.
1054
1055 @item GPGME_PK_RSA_S
1056 Deprecated.  This value indicates the RSA (Rivest, Shamir, Adleman)
1057 algorithm for signing and verification only.
1058
1059 @item GPGME_PK_DSA
1060 This value indicates DSA, the Digital Signature Algorithm.
1061
1062 @item GPGME_PK_ELG
1063 This value indicates ElGamal.
1064
1065 @item GPGME_PK_ELG_E
1066 This value also indicates ElGamal and is used specifically in GnuPG.
1067
1068 @item GPGME_PK_ELG_E
1069 This value also indicates ElGamal and is used specifically in GnuPG.
1070
1071 @item GPGME_PK_ECDSA
1072 This value indicates ECDSA, the Elliptic Curve Digital Signature
1073 Algorithm as defined by FIPS 186-2.
1074
1075 @item GPGME_PK_ECDH
1076 This value indicates ECDH, the Eliptic Curve Diffie-Hellmann encryption
1077 algorithm as defined by the ECC in OpenPGP draft.
1078
1079 @end table
1080 @end deftp
1081
1082 @deftypefun {const char *} gpgme_pubkey_algo_name (@w{gpgme_pubkey_algo_t @var{algo}})
1083 The function @code{gpgme_pubkey_algo_name} returns a pointer to a
1084 statically allocated string containing a description of the public key
1085 algorithm @var{algo}.  This string can be used to output the name of
1086 the public key algorithm to the user.
1087
1088 If @var{algo} is not a valid public key algorithm, @code{NULL} is
1089 returned.
1090 @end deftypefun
1091
1092
1093 @node Hash Algorithms
1094 @section Hash Algorithms
1095 @cindex algorithms, hash
1096 @cindex algorithms, message digest
1097 @cindex hash algorithms
1098 @cindex message digest algorithms
1099
1100 Hash (message digest) algorithms are used to compress a long message
1101 to make it suitable for public key cryptography.
1102
1103 @deftp {Data type} {enum gpgme_hash_algo_t}
1104 @tindex gpgme_hash_algo_t
1105 The @code{gpgme_hash_algo_t} type specifies the set of all hash algorithms
1106 that are supported by @acronym{GPGME}.  Possible values are:
1107
1108 @table @code
1109 @item GPGME_MD_MD5
1110 @item GPGME_MD_SHA1
1111 @item GPGME_MD_RMD160
1112 @item GPGME_MD_MD2
1113 @item GPGME_MD_TIGER
1114 @item GPGME_MD_HAVAL
1115 @item GPGME_MD_SHA256
1116 @item GPGME_MD_SHA384
1117 @item GPGME_MD_SHA512
1118 @item GPGME_MD_MD4
1119 @item GPGME_MD_CRC32
1120 @item GPGME_MD_CRC32_RFC1510
1121 @item GPGME_MD_CRC24_RFC2440
1122 @end table
1123 @end deftp
1124
1125 @deftypefun {const char *} gpgme_hash_algo_name (@w{gpgme_hash_algo_t @var{algo}})
1126 The function @code{gpgme_hash_algo_name} returns a pointer to a
1127 statically allocated string containing a description of the hash
1128 algorithm @var{algo}.  This string can be used to output the name of
1129 the hash algorithm to the user.
1130
1131 If @var{algo} is not a valid hash algorithm, @code{NULL} is returned.
1132 @end deftypefun
1133
1134
1135 @node Error Handling
1136 @chapter Error Handling
1137 @cindex error handling
1138
1139 Many functions in @acronym{GPGME} can return an error if they fail.
1140 For this reason, the application should always catch the error
1141 condition and take appropriate measures, for example by releasing the
1142 resources and passing the error up to the caller, or by displaying a
1143 descriptive message to the user and cancelling the operation.
1144
1145 Some error values do not indicate a system error or an error in the
1146 operation, but the result of an operation that failed properly.  For
1147 example, if you try to decrypt a tempered message, the decryption will
1148 fail.  Another error value actually means that the end of a data
1149 buffer or list has been reached.  The following descriptions explain
1150 for many error codes what they mean usually.  Some error values have
1151 specific meanings if returned by a certain functions.  Such cases are
1152 described in the documentation of those functions.
1153
1154 @acronym{GPGME} uses the @code{libgpg-error} library.  This allows to
1155 share the error codes with other components of the GnuPG system, and
1156 thus pass error values transparently from the crypto engine, or some
1157 helper application of the crypto engine, to the user.  This way no
1158 information is lost.  As a consequence, @acronym{GPGME} does not use
1159 its own identifiers for error codes, but uses those provided by
1160 @code{libgpg-error}.  They usually start with @code{GPG_ERR_}.
1161
1162 However, @acronym{GPGME} does provide aliases for the functions
1163 defined in libgpg-error, which might be preferred for name space
1164 consistency.
1165
1166 @menu
1167 * Error Values::                  The error value and what it means.
1168 * Error Sources::                 A list of important error sources.
1169 * Error Codes::                   A list of important error codes.
1170 * Error Strings::                 How to get a descriptive string from a value.
1171 @end menu
1172
1173
1174 @node Error Values
1175 @section Error Values
1176 @cindex error values
1177 @cindex error codes
1178 @cindex error sources
1179
1180 @deftp {Data type} {gpgme_err_code_t}
1181 The @code{gpgme_err_code_t} type is an alias for the @code{libgpg-error}
1182 type @code{gpg_err_code_t}.  The error code indicates the type of an
1183 error, or the reason why an operation failed.
1184
1185 A list of important error codes can be found in the next section.
1186 @end deftp
1187
1188 @deftp {Data type} {gpgme_err_source_t}
1189 The @code{gpgme_err_source_t} type is an alias for the
1190 @code{libgpg-error} type @code{gpg_err_source_t}.  The error source
1191 has not a precisely defined meaning.  Sometimes it is the place where
1192 the error happened, sometimes it is the place where an error was
1193 encoded into an error value.  Usually the error source will give an
1194 indication to where to look for the problem.  This is not always true,
1195 but it is attempted to achieve this goal.
1196
1197 A list of important error sources can be found in the next section.
1198 @end deftp
1199
1200 @deftp {Data type} {gpgme_error_t}
1201 The @code{gpgme_error_t} type is an alias for the @code{libgpg-error}
1202 type @code{gpg_error_t}.  An error value like this has always two
1203 components, an error code and an error source.  Both together form the
1204 error value.
1205
1206 Thus, the error value can not be directly compared against an error
1207 code, but the accessor functions described below must be used.
1208 However, it is guaranteed that only 0 is used to indicate success
1209 (@code{GPG_ERR_NO_ERROR}), and that in this case all other parts of
1210 the error value are set to 0, too.
1211
1212 Note that in @acronym{GPGME}, the error source is used purely for
1213 diagnostical purposes.  Only the error code should be checked to test
1214 for a certain outcome of a function.  The manual only documents the
1215 error code part of an error value.  The error source is left
1216 unspecified and might be anything.
1217 @end deftp
1218
1219 @deftypefun {static inline gpgme_err_code_t} gpgme_err_code (@w{gpgme_error_t @var{err}})
1220 The static inline function @code{gpgme_err_code} returns the
1221 @code{gpgme_err_code_t} component of the error value @var{err}.  This
1222 function must be used to extract the error code from an error value in
1223 order to compare it with the @code{GPG_ERR_*} error code macros.
1224 @end deftypefun
1225
1226 @deftypefun {static inline gpgme_err_source_t} gpgme_err_source (@w{gpgme_error_t @var{err}})
1227 The static inline function @code{gpgme_err_source} returns the
1228 @code{gpgme_err_source_t} component of the error value @var{err}.  This
1229 function must be used to extract the error source from an error value in
1230 order to compare it with the @code{GPG_ERR_SOURCE_*} error source macros.
1231 @end deftypefun
1232
1233 @deftypefun {static inline gpgme_error_t} gpgme_err_make (@w{gpgme_err_source_t @var{source}}, @w{gpgme_err_code_t @var{code}})
1234 The static inline function @code{gpgme_err_make} returns the error
1235 value consisting of the error source @var{source} and the error code
1236 @var{code}.
1237
1238 This function can be used in callback functions to construct an error
1239 value to return it to the library.
1240 @end deftypefun
1241
1242 @deftypefun {static inline gpgme_error_t} gpgme_error (@w{gpgme_err_code_t @var{code}})
1243 The static inline function @code{gpgme_error} returns the error value
1244 consisting of the default error source and the error code @var{code}.
1245
1246 For @acronym{GPGME} applications, the default error source is
1247 @code{GPG_ERR_SOURCE_USER_1}.  You can define
1248 @code{GPGME_ERR_SOURCE_DEFAULT} before including @file{gpgme.h} to
1249 change this default.
1250
1251 This function can be used in callback functions to construct an error
1252 value to return it to the library.
1253 @end deftypefun
1254
1255 The @code{libgpg-error} library provides error codes for all system
1256 error numbers it knows about.  If @var{err} is an unknown error
1257 number, the error code @code{GPG_ERR_UNKNOWN_ERRNO} is used.  The
1258 following functions can be used to construct error values from system
1259 errnor numbers.
1260
1261 @deftypefun {gpgme_error_t} gpgme_err_make_from_errno (@w{gpgme_err_source_t @var{source}}, @w{int @var{err}})
1262 The function @code{gpgme_err_make_from_errno} is like
1263 @code{gpgme_err_make}, but it takes a system error like @code{errno}
1264 instead of a @code{gpgme_err_code_t} error code.
1265 @end deftypefun
1266
1267 @deftypefun {gpgme_error_t} gpgme_error_from_errno (@w{int @var{err}})
1268 The function @code{gpgme_error_from_errno} is like @code{gpgme_error},
1269 but it takes a system error like @code{errno} instead of a
1270 @code{gpgme_err_code_t} error code.
1271 @end deftypefun
1272
1273 Sometimes you might want to map system error numbers to error codes
1274 directly, or map an error code representing a system error back to the
1275 system error number.  The following functions can be used to do that.
1276
1277 @deftypefun {gpgme_err_code_t} gpgme_err_code_from_errno (@w{int @var{err}})
1278 The function @code{gpgme_err_code_from_errno} returns the error code
1279 for the system error @var{err}.  If @var{err} is not a known system
1280 error, the function returns @code{GPG_ERR_UNKNOWN_ERRNO}.
1281 @end deftypefun
1282
1283 @deftypefun {int} gpgme_err_code_to_errno (@w{gpgme_err_code_t @var{err}})
1284 The function @code{gpgme_err_code_to_errno} returns the system error
1285 for the error code @var{err}.  If @var{err} is not an error code
1286 representing a system error, or if this system error is not defined on
1287 this system, the function returns @code{0}.
1288 @end deftypefun
1289
1290
1291 @node Error Sources
1292 @section Error Sources
1293 @cindex error codes, list of
1294
1295 The library @code{libgpg-error} defines an error source for every
1296 component of the GnuPG system.  The error source part of an error
1297 value is not well defined.  As such it is mainly useful to improve the
1298 diagnostic error message for the user.
1299
1300 If the error code part of an error value is @code{0}, the whole error
1301 value will be @code{0}.  In this case the error source part is of
1302 course @code{GPG_ERR_SOURCE_UNKNOWN}.
1303
1304 The list of error sources that might occur in applications using
1305 @acronym{GPGME} is:
1306
1307 @table @code
1308 @item GPG_ERR_SOURCE_UNKNOWN
1309 The error source is not known.  The value of this error source is
1310 @code{0}.
1311
1312 @item GPG_ERR_SOURCE_GPGME
1313 The error source is @acronym{GPGME} itself.  This is the default for
1314 errors that occur in the @acronym{GPGME} library.
1315
1316 @item GPG_ERR_SOURCE_GPG
1317 The error source is GnuPG, which is the crypto engine used for the
1318 OpenPGP protocol.
1319
1320 @item GPG_ERR_SOURCE_GPGSM
1321 The error source is GPGSM, which is the crypto engine used for the
1322 CMS protocol.
1323
1324 @item GPG_ERR_SOURCE_GCRYPT
1325 The error source is @code{libgcrypt}, which is used by crypto engines
1326 to perform cryptographic operations.
1327
1328 @item GPG_ERR_SOURCE_GPGAGENT
1329 The error source is @command{gpg-agent}, which is used by crypto
1330 engines to perform operations with the secret key.
1331
1332 @item GPG_ERR_SOURCE_PINENTRY
1333 The error source is @command{pinentry}, which is used by
1334 @command{gpg-agent} to query the passphrase to unlock a secret key.
1335
1336 @item GPG_ERR_SOURCE_SCD
1337 The error source is the SmartCard Daemon, which is used by
1338 @command{gpg-agent} to delegate operations with the secret key to a
1339 SmartCard.
1340
1341 @item GPG_ERR_SOURCE_KEYBOX
1342 The error source is @code{libkbx}, a library used by the crypto
1343 engines to manage local keyrings.
1344
1345 @item GPG_ERR_SOURCE_USER_1
1346 @item GPG_ERR_SOURCE_USER_2
1347 @item GPG_ERR_SOURCE_USER_3
1348 @item GPG_ERR_SOURCE_USER_4
1349 These error sources are not used by any GnuPG component and can be
1350 used by other software.  For example, applications using
1351 @acronym{GPGME} can use them to mark error values coming from callback
1352 handlers.  Thus @code{GPG_ERR_SOURCE_USER_1} is the default for errors
1353 created with @code{gpgme_error} and @code{gpgme_error_from_errno},
1354 unless you define @code{GPGME_ERR_SOURCE_DEFAULT} before including
1355 @file{gpgme.h}.
1356 @end table
1357
1358
1359 @node Error Codes
1360 @section Error Codes
1361 @cindex error codes, list of
1362
1363 The library @code{libgpg-error} defines many error values.  Most of
1364 them are not used by @code{GPGME} directly, but might be returned by
1365 @acronym{GPGME} because it received them from the crypto engine.  The
1366 below list only includes such error codes that have a specific meaning
1367 in @code{GPGME}, or which are so common that you should know about
1368 them.
1369
1370 @table @code
1371 @item GPG_ERR_EOF
1372 This value indicates the end of a list, buffer or file.
1373
1374 @item GPG_ERR_NO_ERROR
1375 This value indicates success.  The value of this error code is
1376 @code{0}.  Also, it is guaranteed that an error value made from the
1377 error code @code{0} will be @code{0} itself (as a whole).  This means
1378 that the error source information is lost for this error code,
1379 however, as this error code indicates that no error occured, this is
1380 generally not a problem.
1381
1382 @item GPG_ERR_GENERAL
1383 This value means that something went wrong, but either there is not
1384 enough information about the problem to return a more useful error
1385 value, or there is no separate error value for this type of problem.
1386
1387 @item GPG_ERR_ENOMEM
1388 This value means that an out-of-memory condition occurred.
1389
1390 @item GPG_ERR_E...
1391 System errors are mapped to GPG_ERR_FOO where FOO is the symbol for
1392 the system error.
1393
1394 @item GPG_ERR_INV_VALUE
1395 This value means that some user provided data was out of range.  This
1396 can also refer to objects.  For example, if an empty
1397 @code{gpgme_data_t} object was expected, but one containing data was
1398 provided, this error value is returned.
1399
1400 @item GPG_ERR_UNUSABLE_PUBKEY
1401 This value means that some recipients for a message were invalid.
1402
1403 @item GPG_ERR_UNUSABLE_SECKEY
1404 This value means that some signers were invalid.
1405
1406 @item GPG_ERR_NO_DATA
1407 This value means that a @code{gpgme_data_t} object which was expected
1408 to have content was found empty.
1409
1410 @item GPG_ERR_CONFLICT
1411 This value means that a conflict of some sort occurred.
1412
1413 @item GPG_ERR_NOT_IMPLEMENTED
1414 This value indicates that the specific function (or operation) is not
1415 implemented.  This error should never happen.  It can only occur if
1416 you use certain values or configuration options which do not work,
1417 but for which we think that they should work at some later time.
1418
1419 @item GPG_ERR_DECRYPT_FAILED
1420 This value indicates that a decryption operation was unsuccessful.
1421
1422 @item GPG_ERR_BAD_PASSPHRASE
1423 This value means that the user did not provide a correct passphrase
1424 when requested.
1425
1426 @item GPG_ERR_CANCELED
1427 This value means that the operation was canceled.
1428
1429 @item GPG_ERR_INV_ENGINE
1430 This value means that the engine that implements the desired protocol
1431 is currently not available.  This can either be because the sources
1432 were configured to exclude support for this engine, or because the
1433 engine is not installed properly.
1434
1435 @item GPG_ERR_AMBIGUOUS_NAME
1436 This value indicates that a user ID or other specifier did not specify
1437 a unique key.
1438
1439 @item GPG_ERR_WRONG_KEY_USAGE
1440 This value indicates that a key is not used appropriately.
1441
1442 @item GPG_ERR_CERT_REVOKED
1443 This value indicates that a key signature was revoced.
1444
1445 @item GPG_ERR_CERT_EXPIRED
1446 This value indicates that a key signature expired.
1447
1448 @item GPG_ERR_NO_CRL_KNOWN
1449 This value indicates that no certificate revocation list is known for
1450 the certificate.
1451
1452 @item GPG_ERR_NO_POLICY_MATCH
1453 This value indicates that a policy issue occured.
1454
1455 @item GPG_ERR_NO_SECKEY
1456 This value indicates that no secret key for the user ID is available.
1457
1458 @item GPG_ERR_MISSING_CERT
1459 This value indicates that a key could not be imported because the
1460 issuer certificate is missing.
1461
1462 @item GPG_ERR_BAD_CERT_CHAIN
1463 This value indicates that a key could not be imported because its
1464 certificate chain is not good, for example it could be too long.
1465
1466 @item GPG_ERR_UNSUPPORTED_ALGORITHM
1467 This value means a verification failed because the cryptographic
1468 algorithm is not supported by the crypto backend.
1469
1470 @item GPG_ERR_BAD_SIGNATURE
1471 This value means a verification failed because the signature is bad.
1472
1473 @item GPG_ERR_NO_PUBKEY
1474 This value means a verification failed because the public key is not
1475 available.
1476
1477 @item GPG_ERR_USER_1
1478 @item GPG_ERR_USER_2
1479 @item ...
1480 @item GPG_ERR_USER_16
1481 These error codes are not used by any GnuPG component and can be
1482 freely used by other software.  Applications using @acronym{GPGME}
1483 might use them to mark specific errors returned by callback handlers
1484 if no suitable error codes (including the system errors) for
1485 these errors exist already.
1486 @end table
1487
1488
1489 @node Error Strings
1490 @section Error Strings
1491 @cindex error values, printing of
1492 @cindex error codes, printing of
1493 @cindex error sources, printing of
1494 @cindex error strings
1495
1496 @deftypefun {const char *} gpgme_strerror (@w{gpgme_error_t @var{err}})
1497 The function @code{gpgme_strerror} returns a pointer to a statically
1498 allocated string containing a description of the error code contained
1499 in the error value @var{err}.  This string can be used to output a
1500 diagnostic message to the user.
1501
1502 This function is not thread safe.  Use @code{gpgme_strerror_r} in
1503 multi-threaded programs.
1504 @end deftypefun
1505
1506
1507 @deftypefun {int} gpgme_strerror_r (@w{gpgme_error_t @var{err}}, @w{char *@var{buf}}, @w{size_t @var{buflen}})
1508 The function @code{gpgme_strerror_r} returns the error string for
1509 @var{err} in the user-supplied buffer @var{buf} of size @var{buflen}.
1510 This function is, in contrast to @code{gpgme_strerror}, thread-safe if
1511 a thread-safe @code{strerror_r} function is provided by the system.
1512 If the function succeeds, 0 is returned and @var{buf} contains the
1513 string describing the error.  If the buffer was not large enough,
1514 ERANGE is returned and @var{buf} contains as much of the beginning of
1515 the error string as fits into the buffer.
1516 @end deftypefun
1517
1518
1519 @deftypefun {const char *} gpgme_strsource (@w{gpgme_error_t @var{err}})
1520 The function @code{gpgme_strerror} returns a pointer to a statically
1521 allocated string containing a description of the error source
1522 contained in the error value @var{err}.  This string can be used to
1523 output a diagnostic message to the user.
1524 @end deftypefun
1525
1526 The following example illustrates the use of @code{gpgme_strerror}:
1527
1528 @example
1529 gpgme_ctx_t ctx;
1530 gpgme_error_t err = gpgme_new (&ctx);
1531 if (err)
1532   @{
1533     fprintf (stderr, "%s: creating GpgME context failed: %s: %s\n",
1534              argv[0], gpgme_strsource (err), gpgme_strerror (err));
1535     exit (1);
1536   @}
1537 @end example
1538
1539
1540 @node Exchanging Data
1541 @chapter Exchanging Data
1542 @cindex data, exchanging
1543
1544 A lot of data has to be exchanged between the user and the crypto
1545 engine, like plaintext messages, ciphertext, signatures and
1546 information about the keys.  The technical details about exchanging
1547 the data information are completely abstracted by @acronym{GPGME}.
1548 The user provides and receives the data via @code{gpgme_data_t} objects,
1549 regardless of the communication protocol between @acronym{GPGME} and
1550 the crypto engine in use.
1551
1552 @deftp {Data type} {gpgme_data_t}
1553 The @code{gpgme_data_t} type is a handle for a container for generic
1554 data, which is used by @acronym{GPGME} to exchange data with the user.
1555 @end deftp
1556
1557 @code{gpgme_data_t} objects do not provide notifications on events.
1558 It is assumed that read and write operations are blocking until data
1559 is available.  If this is undesirable, the application must ensure
1560 that all GPGME data operations always have data available, for example
1561 by using memory buffers or files rather than pipes or sockets.  This
1562 might be relevant, for example, if the external event loop mechanism
1563 is used.
1564
1565 @deftp {Data type} {gpgme_off_t}
1566 On POSIX platforms the @code{gpgme_off_t} type is an alias for
1567 @code{off_t}; it may be used interchangeable.  On Windows platforms
1568 @code{gpgme_off_t} is defined as a long (i.e. 32 bit) for 32 bit
1569 Windows and as a 64 bit signed integer for 64 bit Windows.
1570 @end deftp
1571
1572 @deftp {Data type} {gpgme_ssize_t}
1573 The @code{gpgme_ssize_t} type is an alias for @code{ssize_t}.  It has
1574 only been introduced to overcome portability problems pertaining to
1575 the declaration of @code{ssize_t} by different toolchains.
1576 @end deftp
1577
1578
1579 @menu
1580 * Creating Data Buffers::         Creating new data buffers.
1581 * Destroying Data Buffers::       Releasing data buffers.
1582 * Manipulating Data Buffers::     Operations on data buffers.
1583 @end menu
1584
1585
1586 @node Creating Data Buffers
1587 @section Creating Data Buffers
1588 @cindex data buffer, creation
1589
1590 Data objects can be based on memory, files, or callback functions
1591 provided by the user.  Not all operations are supported by all
1592 objects.
1593
1594
1595 @menu
1596 * Memory Based Data Buffers::     Creating memory based data buffers.
1597 * File Based Data Buffers::       Creating file based data buffers.
1598 * Callback Based Data Buffers::   Creating callback based data buffers.
1599 @end menu
1600
1601
1602 @node Memory Based Data Buffers
1603 @subsection Memory Based Data Buffers
1604
1605 Memory based data objects store all data in allocated memory.  This is
1606 convenient, but only practical for an amount of data that is a
1607 fraction of the available physical memory.  The data has to be copied
1608 from its source and to its destination, which can often be avoided by
1609 using one of the other data object
1610
1611 @deftypefun gpgme_error_t gpgme_data_new (@w{gpgme_data_t *@var{dh}})
1612 The function @code{gpgme_data_new} creates a new @code{gpgme_data_t}
1613 object and returns a handle for it in @var{dh}.  The data object is
1614 memory based and initially empty.
1615
1616 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
1617 data object was successfully created, @code{GPG_ERR_INV_VALUE} if
1618 @var{dh} is not a valid pointer, and @code{GPG_ERR_ENOMEM} if not
1619 enough memory is available.
1620 @end deftypefun
1621
1622 @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}})
1623 The function @code{gpgme_data_new_from_mem} creates a new
1624 @code{gpgme_data_t} object and fills it with @var{size} bytes starting
1625 from @var{buffer}.
1626
1627 If @var{copy} is not zero, a private copy of the data is made.  If
1628 @var{copy} is zero, the data is taken from the specified buffer as
1629 needed, and the user has to ensure that the buffer remains valid for
1630 the whole life span of the data object.
1631
1632 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
1633 data object was successfully created, @code{GPG_ERR_INV_VALUE} if
1634 @var{dh} or @var{buffer} is not a valid pointer, and
1635 @code{GPG_ERR_ENOMEM} if not enough memory is available.
1636 @end deftypefun
1637
1638 @deftypefun gpgme_error_t gpgme_data_new_from_file (@w{gpgme_data_t *@var{dh}}, @w{const char *@var{filename}}, @w{int @var{copy}})
1639 The function @code{gpgme_data_new_from_file} creates a new
1640 @code{gpgme_data_t} object and fills it with the content of the file
1641 @var{filename}.
1642
1643 If @var{copy} is not zero, the whole file is read in at initialization
1644 time and the file is not used anymore after that.  This is the only
1645 mode supported currently.  Later, a value of zero for @var{copy} might
1646 cause all reads to be delayed until the data is needed, but this is
1647 not yet implemented.
1648
1649 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
1650 data object was successfully created, @code{GPG_ERR_INV_VALUE} if
1651 @var{dh} or @var{filename} is not a valid pointer,
1652 @code{GPG_ERR_NOT_IMPLEMENTED} if @var{code} is zero, and
1653 @code{GPG_ERR_ENOMEM} if not enough memory is available.
1654 @end deftypefun
1655
1656 @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}})
1657 The function @code{gpgme_data_new_from_filepart} creates a new
1658 @code{gpgme_data_t} object and fills it with a part of the file specified
1659 by @var{filename} or @var{fp}.
1660
1661 Exactly one of @var{filename} and @var{fp} must be non-zero, the other
1662 must be zero.  The argument that is not zero specifies the file from
1663 which @var{length} bytes are read into the data object, starting from
1664 @var{offset}.
1665
1666 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
1667 data object was successfully created, @code{GPG_ERR_INV_VALUE} if
1668 @var{dh} and exactly one of @var{filename} and @var{fp} is not a valid
1669 pointer, and @code{GPG_ERR_ENOMEM} if not enough memory is available.
1670 @end deftypefun
1671
1672
1673 @node File Based Data Buffers
1674 @subsection File Based Data Buffers
1675
1676 File based data objects operate directly on file descriptors or
1677 streams.  Only a small amount of data is stored in core at any time,
1678 so the size of the data objects is not limited by @acronym{GPGME}.
1679
1680 @deftypefun gpgme_error_t gpgme_data_new_from_fd (@w{gpgme_data_t *@var{dh}}, @w{int @var{fd}})
1681 The function @code{gpgme_data_new_from_fd} creates a new
1682 @code{gpgme_data_t} object and uses the file descriptor @var{fd} to read
1683 from (if used as an input data object) and write to (if used as an
1684 output data object).
1685
1686 When using the data object as an input buffer, the function might read
1687 a bit more from the file descriptor than is actually needed by the
1688 crypto engine in the desired operation because of internal buffering.
1689
1690 Note that GPGME assumes that the file descriptor is set to blocking
1691 mode.  Errors during I/O operations, except for EINTR, are usually
1692 fatal for crypto operations.
1693
1694 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
1695 data object was successfully created, and @code{GPG_ERR_ENOMEM} if not
1696 enough memory is available.
1697 @end deftypefun
1698
1699 @deftypefun gpgme_error_t gpgme_data_new_from_stream (@w{gpgme_data_t *@var{dh}}, @w{FILE *@var{stream}})
1700 The function @code{gpgme_data_new_from_stream} creates a new
1701 @code{gpgme_data_t} object and uses the I/O stream @var{stream} to read
1702 from (if used as an input data object) and write to (if used as an
1703 output data object).
1704
1705 When using the data object as an input buffer, the function might read
1706 a bit more from the stream than is actually needed by the crypto
1707 engine in the desired operation because of internal buffering.
1708
1709 Note that GPGME assumes that the stream is in blocking mode.  Errors
1710 during I/O operations, except for EINTR, are usually fatal for crypto
1711 operations.
1712
1713 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
1714 data object was successfully created, and @code{GPG_ERR_ENOMEM} if not
1715 enough memory is available.
1716 @end deftypefun
1717
1718
1719 @node Callback Based Data Buffers
1720 @subsection Callback Based Data Buffers
1721
1722 If neither memory nor file based data objects are a good fit for your
1723 application, you can implement the functions a data object provides
1724 yourself and create a data object from these callback functions.
1725
1726 @deftp {Data type} {ssize_t (*gpgme_data_read_cb_t) (@w{void *@var{handle}}, @w{void @var{*buffer}}, @w{size_t @var{size}})}
1727 @tindex gpgme_data_read_cb_t
1728 The @code{gpgme_data_read_cb_t} type is the type of functions which
1729 @acronym{GPGME} calls if it wants to read data from a user-implemented
1730 data object.  The function should read up to @var{size} bytes from the
1731 current read position into the space starting at @var{buffer}.  The
1732 @var{handle} is provided by the user at data object creation time.
1733
1734 Note that GPGME assumes that the read blocks until data is available.
1735 Errors during I/O operations, except for EINTR, are usually fatal for
1736 crypto operations.
1737
1738 The function should return the number of bytes read, 0 on EOF, and -1
1739 on error.  If an error occurs, @var{errno} should be set to describe
1740 the type of the error.
1741 @end deftp
1742
1743 @deftp {Data type} {ssize_t (*gpgme_data_write_cb_t) (@w{void *@var{handle}}, @w{const void @var{*buffer}}, @w{size_t @var{size}})}
1744 @tindex gpgme_data_write_cb_t
1745 The @code{gpgme_data_write_cb_t} type is the type of functions which
1746 @acronym{GPGME} calls if it wants to write data to a user-implemented
1747 data object.  The function should write up to @var{size} bytes to the
1748 current write position from the space starting at @var{buffer}.  The
1749 @var{handle} is provided by the user at data object creation time.
1750
1751 Note that GPGME assumes that the write blocks until data is available.
1752 Errors during I/O operations, except for EINTR, are usually fatal for
1753 crypto operations.
1754
1755 The function should return the number of bytes written, and -1 on
1756 error.  If an error occurs, @var{errno} should be set to describe the
1757 type of the error.
1758 @end deftp
1759
1760 @deftp {Data type} {off_t (*gpgme_data_seek_cb_t) (@w{void *@var{handle}}, @w{off_t @var{offset}}, @w{int @var{whence}})}
1761 @tindex gpgme_data_seek_cb_t
1762 The @code{gpgme_data_seek_cb_t} type is the type of functions which
1763 @acronym{GPGME} calls if it wants to change the current read/write
1764 position in a user-implemented data object, just like the @code{lseek}
1765 function.
1766
1767 The function should return the new read/write position, and -1 on
1768 error.  If an error occurs, @var{errno} should be set to describe the
1769 type of the error.
1770 @end deftp
1771
1772 @deftp {Data type} {void (*gpgme_data_release_cb_t) (@w{void *@var{handle}})}
1773 @tindex gpgme_data_release_cb_t
1774 The @code{gpgme_data_release_cb_t} type is the type of functions which
1775 @acronym{GPGME} calls if it wants to destroy a user-implemented data
1776 object.  The @var{handle} is provided by the user at data object
1777 creation time.
1778 @end deftp
1779
1780 @deftp {Data type} {struct gpgme_data_cbs}
1781 This structure is used to store the data callback interface functions
1782 described above.  It has the following members:
1783
1784 @table @code
1785 @item gpgme_data_read_cb_t read
1786 This is the function called by @acronym{GPGME} to read data from the
1787 data object.  It is only required for input data object.
1788
1789 @item gpgme_data_write_cb_t write
1790 This is the function called by @acronym{GPGME} to write data to the
1791 data object.  It is only required for output data object.
1792
1793 @item gpgme_data_seek_cb_t seek
1794 This is the function called by @acronym{GPGME} to change the current
1795 read/write pointer in the data object (if available).  It is optional.
1796
1797 @item gpgme_data_release_cb_t release
1798 This is the function called by @acronym{GPGME} to release a data
1799 object.  It is optional.
1800 @end table
1801 @end deftp
1802
1803 @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}})
1804 The function @code{gpgme_data_new_from_cbs} creates a new
1805 @code{gpgme_data_t} object and uses the user-provided callback functions
1806 to operate on the data object.
1807
1808 The handle @var{handle} is passed as first argument to the callback
1809 functions.  This can be used to identify this data object.
1810
1811 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
1812 data object was successfully created, and @code{GPG_ERR_ENOMEM} if not
1813 enough memory is available.
1814 @end deftypefun
1815
1816 The following interface is deprecated and only provided for backward
1817 compatibility.  Don't use it.  It will be removed in a future version
1818 of @acronym{GPGME}.
1819
1820 @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}})
1821 The function @code{gpgme_data_new_with_read_cb} creates a new
1822 @code{gpgme_data_t} object and uses the callback function @var{readfunc}
1823 to retrieve the data on demand.  As the callback function can supply
1824 the data in any way it wants, this is the most flexible data type
1825 @acronym{GPGME} provides.  However, it can not be used to write data.
1826
1827 The callback function receives @var{hook_value} as its first argument
1828 whenever it is invoked.  It should return up to @var{count} bytes in
1829 @var{buffer}, and return the number of bytes actually read in
1830 @var{nread}.  It may return @code{0} in @var{nread} if no data is
1831 currently available.  To indicate @code{EOF} the function should
1832 return with an error code of @code{-1} and set @var{nread} to
1833 @code{0}.  The callback function may support to reset its internal
1834 read pointer if it is invoked with @var{buffer} and @var{nread} being
1835 @code{NULL} and @var{count} being @code{0}.
1836
1837 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
1838 data object was successfully created, @code{GPG_ERR_INV_VALUE} if
1839 @var{dh} or @var{readfunc} is not a valid pointer, and
1840 @code{GPG_ERR_ENOMEM} if not enough memory is available.
1841 @end deftypefun
1842
1843
1844 @node Destroying Data Buffers
1845 @section Destroying Data Buffers
1846 @cindex data buffer, destruction
1847
1848 @deftypefun void gpgme_data_release (@w{gpgme_data_t @var{dh}})
1849 The function @code{gpgme_data_release} destroys the data object with
1850 the handle @var{dh}.  It releases all associated resources that were
1851 not provided by the user in the first place.
1852 @end deftypefun
1853
1854 @deftypefun {char *} gpgme_data_release_and_get_mem (@w{gpgme_data_t @var{dh}}, @w{size_t *@var{length}})
1855 The function @code{gpgme_data_release_and_get_mem} is like
1856 @code{gpgme_data_release}, except that it returns the data buffer and
1857 its length that was provided by the object.
1858
1859 The user has to release the buffer with @code{gpgme_free}.  In case
1860 the user provided the data buffer in non-copy mode, a copy will be
1861 made for this purpose.
1862
1863 In case an error returns, or there is no suitable data buffer that can
1864 be returned to the user, the function will return @code{NULL}.  In any
1865 case, the data object @var{dh} is destroyed.
1866 @end deftypefun
1867
1868
1869 @deftypefun void gpgme_free (@w{void *@var{buffer}})
1870 The function @code{gpgme_free} releases the memory returned by
1871 @code{gpgme_data_release_and_get_mem}.  It should be used instead of
1872 the system libraries @code{free} function in case different allocators
1873 are used in a single program.
1874 @end deftypefun
1875
1876
1877 @node Manipulating Data Buffers
1878 @section Manipulating Data Buffers
1879 @cindex data buffer, manipulation
1880
1881 Data buffers contain data and meta-data.  The following operations can
1882 be used to manipulate both.
1883
1884
1885 @menu
1886 * Data Buffer I/O Operations::    I/O operations on data buffers.
1887 * Data Buffer Meta-Data::         Meta-data manipulation of data buffers.
1888 @end menu
1889
1890
1891 @node Data Buffer I/O Operations
1892 @subsection Data Buffer I/O Operations
1893 @cindex data buffer, I/O operations
1894 @cindex data buffer, read
1895 @cindex data buffer, write
1896 @cindex data buffer, seek
1897
1898 @deftypefun ssize_t gpgme_data_read (@w{gpgme_data_t @var{dh}}, @w{void *@var{buffer}}, @w{size_t @var{length}})
1899 The function @code{gpgme_data_read} reads up to @var{length} bytes
1900 from the data object with the handle @var{dh} into the space starting
1901 at @var{buffer}.
1902
1903 If no error occurs, the actual amount read is returned.  If the end of
1904 the data object is reached, the function returns 0.
1905
1906 In all other cases, the function returns -1 and sets @var{errno}.
1907 @end deftypefun
1908
1909 @deftypefun ssize_t gpgme_data_write (@w{gpgme_data_t @var{dh}}, @w{const void *@var{buffer}}, @w{size_t @var{size}})
1910 The function @code{gpgme_data_write} writes up to @var{size} bytes
1911 starting from @var{buffer} into the data object with the handle
1912 @var{dh} at the current write position.
1913
1914 The function returns the number of bytes actually written, or -1 if an
1915 error occurs.  If an error occurs, @var{errno} is set.
1916 @end deftypefun
1917
1918 @deftypefun off_t gpgme_data_seek (@w{gpgme_data_t @var{dh}}, @w{off_t @var{offset}}, @w{int @var{whence}})
1919 The function @code{gpgme_data_seek} changes the current read/write
1920 position.
1921
1922 The @var{whence} argument specifies how the @var{offset} should be
1923 interpreted.  It must be one of the following symbolic constants:
1924
1925 @table @code
1926 @item SEEK_SET
1927 Specifies that @var{offset} is a count of characters from the
1928 beginning of the data object.
1929
1930 @item SEEK_CUR
1931 Specifies that @var{offset} is a count of characters from the current
1932 file position.  This count may be positive or negative.
1933
1934 @item SEEK_END
1935 Specifies that @var{offset} is a count of characters from the end of
1936 the data object.  A negative count specifies a position within the
1937 current extent of the data object; a positive count specifies a
1938 position past the current end.  If you set the position past the
1939 current end, and actually write data, you will extend the data object
1940 with zeros up to that position.
1941 @end table
1942
1943 If successful, the function returns the resulting file position,
1944 measured in bytes from the beginning of the data object.  You can use
1945 this feature together with @code{SEEK_CUR} to read the current
1946 read/write position.
1947
1948 If the function fails, -1 is returned and @var{errno} is set.
1949 @end deftypefun
1950
1951 The following function is deprecated and should not be used.  It will
1952 be removed in a future version of @acronym{GPGME}.
1953
1954 @deftypefun gpgme_error_t gpgme_data_rewind (@w{gpgme_data_t @var{dh}})
1955 The function @code{gpgme_data_rewind} is equivalent to:
1956
1957 @example
1958   return (gpgme_data_seek (dh, 0, SEEK_SET) == -1)
1959     ? gpgme_error_from_errno (errno) : 0;
1960 @end example
1961 @end deftypefun
1962
1963
1964
1965
1966 @node Data Buffer Meta-Data
1967 @subsection Data Buffer Meta-Data
1968 @cindex data buffer, meta-data
1969 @cindex data buffer, file name
1970 @cindex data buffer, encoding
1971
1972 @deftypefun {char *} gpgme_data_get_file_name (@w{gpgme_data_t @var{dh}})
1973 The function @code{gpgme_data_get_file_name} returns a pointer to a
1974 string containing the file name associated with the data object.  The
1975 file name will be stored in the output when encrypting or signing the
1976 data and will be returned to the user when decrypting or verifying the
1977 output data.
1978
1979 If no error occurs, the string containing the file name is returned.
1980 Otherwise, @code{NULL} will be returned.
1981 @end deftypefun
1982
1983
1984 @deftypefun gpgme_error_t gpgme_data_set_file_name (@w{gpgme_data_t @var{dh}}, @w{const char *@var{file_name}})
1985 The function @code{gpgme_data_set_file_name} sets the file name
1986 associated with the data object.  The file name will be stored in the
1987 output when encrypting or signing the data and will be returned to the
1988 user when decrypting or verifying the output data.
1989
1990 The function returns the error code @code{GPG_ERR_INV_VALUE} if
1991 @var{dh} is not a valid pointer and @code{GPG_ERR_ENOMEM} if not
1992 enough memory is available.
1993 @end deftypefun
1994
1995
1996 @deftp {Data type} {enum gpgme_data_encoding_t}
1997 @tindex gpgme_data_encoding_t
1998 The @code{gpgme_data_encoding_t} type specifies the encoding of a
1999 @code{gpgme_data_t} object.  For input data objects, the encoding is
2000 useful to give the backend a hint on the type of data.  For output
2001 data objects, the encoding can specify the output data format on
2002 certain operations.  Please note that not all backends support all
2003 encodings on all operations.  The following data types are available:
2004
2005 @table @code
2006 @item GPGME_DATA_ENCODING_NONE
2007 This specifies that the encoding is not known.  This is the default
2008 for a new data object.  The backend will try its best to detect the
2009 encoding automatically.
2010
2011 @item GPGME_DATA_ENCODING_BINARY
2012 This specifies that the data is encoding in binary form; i.e. there is
2013 no special encoding.
2014
2015 @item GPGME_DATA_ENCODING_BASE64
2016 This specifies that the data is encoded using the Base-64 encoding
2017 scheme as used by @acronym{MIME} and other protocols.
2018
2019 @item GPGME_DATA_ENCODING_ARMOR
2020 This specifies that the data is encoded in an armored form as used by
2021 OpenPGP and PEM.
2022
2023 @item GPGME_DATA_ENCODING_URL
2024 The data is a list of linefeed delimited URLs.  This is only useful with
2025 @code{gpgme_op_import}.
2026
2027 @item GPGME_DATA_ENCODING_URL0
2028 The data is a list of binary zero delimited URLs.  This is only useful
2029 with @code{gpgme_op_import}.
2030
2031 @item GPGME_DATA_ENCODING_URLESC
2032 The data is a list of linefeed delimited URLs with all control and space
2033 characters percent escaped.  This mode is is not yet implemented.
2034
2035 @end table
2036 @end deftp
2037
2038 @deftypefun gpgme_data_encoding_t gpgme_data_get_encoding (@w{gpgme_data_t @var{dh}})
2039 The function @code{gpgme_data_get_encoding} returns the encoding of
2040 the data object with the handle @var{dh}.  If @var{dh} is not a valid
2041 pointer (e.g. @code{NULL}) @code{GPGME_DATA_ENCODING_NONE} is
2042 returned.
2043 @end deftypefun
2044
2045 @deftypefun gpgme_error_t gpgme_data_set_encoding (@w{gpgme_data_t @var{dh}, gpgme_data_encoding_t @var{enc}})
2046 The function @code{gpgme_data_set_encoding} changes the encoding of
2047 the data object with the handle @var{dh} to @var{enc}.
2048 @end deftypefun
2049
2050
2051 @c
2052 @c    Chapter Contexts
2053 @c
2054 @node Contexts
2055 @chapter Contexts
2056 @cindex context
2057
2058 All cryptographic operations in @acronym{GPGME} are performed within a
2059 context, which contains the internal state of the operation as well as
2060 configuration parameters.  By using several contexts you can run
2061 several cryptographic operations in parallel, with different
2062 configuration.
2063
2064 @deftp {Data type} {gpgme_ctx_t}
2065 The @code{gpgme_ctx_t} type is a handle for a @acronym{GPGME} context,
2066 which is used to hold the configuration, status and result of
2067 cryptographic operations.
2068 @end deftp
2069
2070 @menu
2071 * Creating Contexts::             Creating new @acronym{GPGME} contexts.
2072 * Destroying Contexts::           Releasing @acronym{GPGME} contexts.
2073 * Result Management::             Managing the result of crypto operations.
2074 * Context Attributes::            Setting properties of a context.
2075 * Key Management::                Managing keys with @acronym{GPGME}.
2076 * Trust Item Management::         Managing trust items with @acronym{GPGME}.
2077 * Crypto Operations::             Using a context for cryptography.
2078 * Run Control::                   Controlling how operations are run.
2079 @end menu
2080
2081
2082 @node Creating Contexts
2083 @section Creating Contexts
2084 @cindex context, creation
2085
2086 @deftypefun gpgme_error_t gpgme_new (@w{gpgme_ctx_t *@var{ctx}})
2087 The function @code{gpgme_new} creates a new @code{gpgme_ctx_t} object
2088 and returns a handle for it in @var{ctx}.
2089
2090 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
2091 context was successfully created, @code{GPG_ERR_INV_VALUE} if
2092 @var{ctx} is not a valid pointer, and @code{GPG_ERR_ENOMEM} if not
2093 enough memory is available.  Also, it returns
2094 @code{GPG_ERR_NOT_OPERATIONAL} if @code{gpgme_check_version} was not
2095 called to initialize GPGME, and @code{GPG_ERR_SELFTEST_FAILED} if a
2096 selftest failed.  Currently, the only selftest is for Windows MingW32
2097 targets to see if @code{-mms-bitfields} was used (as required).
2098 @end deftypefun
2099
2100
2101 @node Destroying Contexts
2102 @section Destroying Contexts
2103 @cindex context, destruction
2104
2105 @deftypefun void gpgme_release (@w{gpgme_ctx_t @var{ctx}})
2106 The function @code{gpgme_release} destroys the context with the handle
2107 @var{ctx} and releases all associated resources.
2108 @end deftypefun
2109
2110
2111 @node Result Management
2112 @section Result Management
2113 @cindex context, result of operation
2114
2115 The detailed result of an operation is returned in operation-specific
2116 structures such as @code{gpgme_decrypt_result_t}.  The corresponding
2117 retrieval functions such as @code{gpgme_op_decrypt_result} provide
2118 static access to the results after an operation completes.  The
2119 following interfaces make it possible to detach a result structure
2120 from its associated context and give it a lifetime beyond that of the
2121 current operation or context.
2122
2123 @deftypefun void gpgme_result_ref (@w{void *@var{result}})
2124 The function @code{gpgme_result_ref} acquires an additional reference
2125 for the result @var{result}, which may be of any type
2126 @code{gpgme_*_result_t}.  As long as the user holds a reference, the
2127 result structure is guaranteed to be valid and unmodified.
2128 @end deftypefun
2129
2130 @deftypefun void gpgme_result_unref (@w{void *@var{result}})
2131 The function @code{gpgme_result_unref} releases a reference for the
2132 result @var{result}.  If this was the last reference, the result
2133 structure will be destroyed and all resources associated to it will be
2134 released.
2135 @end deftypefun
2136
2137 Note that a context may hold its own references to result structures,
2138 typically until the context is destroyed or the next operation is
2139 started.  In fact, these references are accessed through the
2140 @code{gpgme_op_*_result} functions.
2141
2142
2143 @node Context Attributes
2144 @section Context Attributes
2145 @cindex context, attributes
2146
2147 @menu
2148 * Protocol Selection::            Selecting the protocol used by a context.
2149 * Crypto Engine::                 Configuring the crypto engine.
2150 * ASCII Armor::                   Requesting @acronym{ASCII} armored output.
2151 * Text Mode::                     Choosing canonical text mode.
2152 * Included Certificates::       Including a number of certificates.
2153 * Key Listing Mode::              Selecting key listing mode.
2154 * Passphrase Callback::           Getting the passphrase from the user.
2155 * Progress Meter Callback::       Being informed about the progress.
2156 * Locale::                        Setting the locale of a context.
2157 @end menu
2158
2159
2160 @node Protocol Selection
2161 @subsection Protocol Selection
2162 @cindex context, selecting protocol
2163 @cindex protocol, selecting
2164
2165 @deftypefun gpgme_error_t gpgme_set_protocol (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_protocol_t @var{proto}})
2166 The function @code{gpgme_set_protocol} sets the protocol used within
2167 the context @var{ctx} to @var{proto}.  All crypto operations will be
2168 performed by the crypto engine configured for that protocol.
2169 @xref{Protocols and Engines}.
2170
2171 Setting the protocol with @code{gpgme_set_protocol} does not check if
2172 the crypto engine for that protocol is available and installed
2173 correctly.  @xref{Engine Version Check}.
2174
2175 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
2176 protocol could be set successfully, and @code{GPG_ERR_INV_VALUE} if
2177 @var{protocol} is not a valid protocol.
2178 @end deftypefun
2179
2180 @deftypefun gpgme_protocol_t gpgme_get_protocol (@w{gpgme_ctx_t @var{ctx}})
2181 The function @code{gpgme_get_protocol} retrieves the protocol currently
2182 use with the context @var{ctx}.
2183 @end deftypefun
2184
2185
2186 @node Crypto Engine
2187 @subsection Crypto Engine
2188 @cindex context, configuring engine
2189 @cindex engine, configuration per context
2190
2191 The following functions can be used to set and retrieve the
2192 configuration of the crypto engines of a specific context.  The
2193 default can also be retrieved without any particular context.
2194 @xref{Engine Information}.  The default can also be changed globally.
2195 @xref{Engine Configuration}.
2196
2197 @deftypefun gpgme_engine_info_t gpgme_ctx_get_engine_info (@w{gpgme_ctx_t @var{ctx}})
2198 The function @code{gpgme_ctx_get_engine_info} returns a linked list of
2199 engine info structures.  Each info structure describes the
2200 configuration of one configured backend, as used by the context
2201 @var{ctx}.
2202
2203 The result is valid until the next invocation of
2204 @code{gpgme_ctx_set_engine_info} for this particular context.
2205
2206 This function can not fail.
2207 @end deftypefun
2208
2209 @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}})
2210 The function @code{gpgme_ctx_set_engine_info} changes the
2211 configuration of the crypto engine implementing the protocol
2212 @var{proto} for the context @var{ctx}.
2213
2214 @var{file_name} is the file name of the executable program
2215 implementing this protocol, and @var{home_dir} is the directory name
2216 of the configuration directory for this crypto engine.  If
2217 @var{home_dir} is @code{NULL}, the engine's default will be used.
2218
2219 Currently this function must be used before starting the first crypto
2220 operation.  It is unspecified if and when the changes will take effect
2221 if the function is called after starting the first operation on the
2222 context @var{ctx}.
2223
2224 This function returns the error code @code{GPG_ERR_NO_ERROR} if
2225 successful, or an eror code on failure.
2226 @end deftypefun
2227
2228
2229 @c FIXME: Unfortunately, using @acronym here breaks texi2dvi.
2230 @node ASCII Armor
2231 @subsection @acronym{ASCII} Armor
2232 @cindex context, armor mode
2233 @cindex @acronym{ASCII} armor
2234 @cindex armor mode
2235
2236 @deftypefun void gpgme_set_armor (@w{gpgme_ctx_t @var{ctx}}, @w{int @var{yes}})
2237 The function @code{gpgme_set_armor} specifies if the output should be
2238 @acronym{ASCII} armored.  By default, output is not @acronym{ASCII}
2239 armored.
2240
2241 @acronym{ASCII} armored output is disabled if @var{yes} is zero, and
2242 enabled otherwise.
2243 @end deftypefun
2244
2245 @deftypefun int gpgme_get_armor (@w{gpgme_ctx_t @var{ctx}})
2246 The function @code{gpgme_get_armor} returns 1 if the output is
2247 @acronym{ASCII} armored, and @code{0} if it is not, or if @var{ctx} is
2248 not a valid pointer.
2249 @end deftypefun
2250
2251
2252 @node Text Mode
2253 @subsection Text Mode
2254 @cindex context, text mode
2255 @cindex text mode
2256 @cindex canonical text mode
2257
2258 @deftypefun void gpgme_set_textmode (@w{gpgme_ctx_t @var{ctx}}, @w{int @var{yes}})
2259 The function @code{gpgme_set_textmode} specifies if canonical text mode
2260 should be used.  By default, text mode is not used.
2261
2262 Text mode is for example used for the RFC2015 signatures; note that
2263 the updated RFC 3156 mandates that the mail user agent does some
2264 preparations so that text mode is not needed anymore.
2265
2266 This option is only relevant to the OpenPGP crypto engine, and ignored
2267 by all other engines.
2268
2269 Canonical text mode is disabled if @var{yes} is zero, and enabled
2270 otherwise.
2271 @end deftypefun
2272
2273 @deftypefun int gpgme_get_textmode (@w{gpgme_ctx_t @var{ctx}})
2274 The function @code{gpgme_get_textmode} returns 1 if canonical text
2275 mode is enabled, and @code{0} if it is not, or if @var{ctx} is not a
2276 valid pointer.
2277 @end deftypefun
2278
2279
2280 @node Included Certificates
2281 @subsection Included Certificates
2282 @cindex certificates, included
2283
2284 @deftypefun void gpgme_set_include_certs (@w{gpgme_ctx_t @var{ctx}}, @w{int @var{nr_of_certs}})
2285 The function @code{gpgme_set_include_certs} specifies how many
2286 certificates should be included in an S/MIME signed message.  By
2287 default, only the sender's certificate is included.  The possible
2288 values of @var{nr_of_certs} are:
2289
2290 @table @code
2291 @item GPGME_INCLUDE_CERTS_DEFAULT
2292 Fall back to the default of the crypto backend.  This is the default
2293 for GPGME.
2294 @item -2
2295 Include all certificates except the root certificate.
2296 @item -1
2297 Include all certificates.
2298 @item 0
2299 Include no certificates.
2300 @item 1
2301 Include the sender's certificate only.
2302 @item n
2303 Include the first n certificates of the certificates path, starting
2304 from the sender's certificate.  The number @code{n} must be positive.
2305 @end table
2306
2307 Values of @var{nr_of_certs} smaller than -2 are undefined.
2308
2309 This option is only relevant to the CMS crypto engine, and ignored by
2310 all other engines.
2311 @end deftypefun
2312
2313 @deftypefun int gpgme_get_include_certs (@w{gpgme_ctx_t @var{ctx}})
2314 The function @code{gpgme_get_include_certs} returns the number of
2315 certificates to include into an S/MIME signed message.
2316 @end deftypefun
2317
2318
2319 @node Key Listing Mode
2320 @subsection Key Listing Mode
2321 @cindex key listing mode
2322 @cindex key listing, mode of
2323
2324 @deftypefun gpgme_error_t gpgme_set_keylist_mode (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_keylist_mode_t @var{mode}})
2325 The function @code{gpgme_set_keylist_mode} changes the default
2326 behaviour of the key listing functions.  The value in @var{mode} is a
2327 bitwise-or combination of one or multiple of the following bit values:
2328
2329 @table @code
2330 @item GPGME_KEYLIST_MODE_LOCAL
2331 The @code{GPGME_KEYLIST_MODE_LOCAL} symbol specifies that the local
2332 keyring should be searched for keys in the keylisting operation.  This
2333 is the default.
2334
2335 @item GPGME_KEYLIST_MODE_EXTERN
2336 The @code{GPGME_KEYLIST_MODE_EXTERN} symbol specifies that an external
2337 source should be searched for keys in the keylisting operation.  The
2338 type of external source is dependant on the crypto engine used and
2339 whether it is combined with @code{GPGME_KEYLIST_MODE_LOCAL}.  For
2340 example, it can be a remote keyserver or LDAP certificate server.
2341
2342 @item GPGME_KEYLIST_MODE_SIGS
2343 The @code{GPGME_KEYLIST_MODE_SIGS} symbol specifies that the key
2344 signatures should be included in the listed keys.
2345
2346 @item GPGME_KEYLIST_MODE_SIG_NOTATIONS
2347 The @code{GPGME_KEYLIST_MODE_SIG_NOTATIONS} symbol specifies that the
2348 signature notations on key signatures should be included in the listed
2349 keys.  This only works if @code{GPGME_KEYLIST_MODE_SIGS} is also
2350 enabled.
2351
2352 @item GPGME_KEYLIST_MODE_EPHEMERAL
2353 The @code{GPGME_KEYLIST_MODE_EPHEMERAL} symbol specifies that keys
2354 flagged as ephemeral are included in the listing.
2355
2356 @item GPGME_KEYLIST_MODE_VALIDATE
2357 The @code{GPGME_KEYLIST_MODE_VALIDATE} symbol specifies that the
2358 backend should do key or certificate validation and not just get the
2359 validity information from an internal cache.  This might be an
2360 expensive operation and is in general not useful.  Currently only
2361 implemented for the S/MIME backend and ignored for other backends.
2362
2363 @end table
2364
2365 At least one of @code{GPGME_KEYLIST_MODE_LOCAL} and
2366 @code{GPGME_KEYLIST_MODE_EXTERN} must be specified.  For future binary
2367 compatibility, you should get the current mode with
2368 @code{gpgme_get_keylist_mode} and modify it by setting or clearing the
2369 appropriate bits, and then using that calculated value in the
2370 @code{gpgme_set_keylisting_mode} operation.  This will leave all other
2371 bits in the mode value intact (in particular those that are not used
2372 in the current version of the library).
2373
2374 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
2375 mode could be set correctly, and @code{GPG_ERR_INV_VALUE} if @var{ctx}
2376 is not a valid pointer or @var{mode} is not a valid mode.
2377 @end deftypefun
2378
2379
2380 @deftypefun gpgme_keylist_mode_t gpgme_get_keylist_mode (@w{gpgme_ctx_t @var{ctx}})
2381 The function @code{gpgme_get_keylist_mode} returns the current key
2382 listing mode of the context @var{ctx}.  This value can then be
2383 modified and used in a subsequent @code{gpgme_set_keylist_mode}
2384 operation to only affect the desired bits (and leave all others
2385 intact).
2386
2387 The function returns 0 if @var{ctx} is not a valid pointer, and the
2388 current mode otherwise.  Note that 0 is not a valid mode value.
2389 @end deftypefun
2390
2391
2392 @node Passphrase Callback
2393 @subsection Passphrase Callback
2394 @cindex callback, passphrase
2395 @cindex passphrase callback
2396
2397 @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}})}
2398 @tindex gpgme_passphrase_cb_t
2399 The @code{gpgme_passphrase_cb_t} type is the type of functions usable as
2400 passphrase callback function.
2401
2402 The argument @var{uid_hint} might contain a string that gives an
2403 indication for which user ID the passphrase is required.  If this is
2404 not available, or not applicable (in the case of symmetric encryption,
2405 for example), @var{uid_hint} will be @code{NULL}.
2406
2407 The argument @var{passphrase_info}, if not @code{NULL}, will give
2408 further information about the context in which the passphrase is
2409 required.  This information is engine and operation specific.
2410
2411 If this is the repeated attempt to get the passphrase, because
2412 previous attempts failed, then @var{prev_was_bad} is 1, otherwise it
2413 will be 0.
2414
2415 The user must write the passphrase, followed by a newline character,
2416 to the file descriptor @var{fd}.  The function @code{gpgme_io_writen}
2417 should be used for the write operation.  Note that if the user returns
2418 0 to indicate success, the user must at least write a newline
2419 character before returning from the callback.
2420
2421 If an error occurs, return the corresponding @code{gpgme_error_t}
2422 value.  You can use the error code @code{GPG_ERR_CANCELED} to abort
2423 the operation.  Otherwise, return @code{0}.
2424 @end deftp
2425
2426 @deftypefun void gpgme_set_passphrase_cb (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_passphrase_cb_t @var{passfunc}}, @w{void *@var{hook_value}})
2427 The function @code{gpgme_set_passphrase_cb} sets the function that is
2428 used when a passphrase needs to be provided by the user to
2429 @var{passfunc}.  The function @var{passfunc} needs to implemented by
2430 the user, and whenever it is called, it is called with its first
2431 argument being @var{hook_value}.  By default, no passphrase callback
2432 function is set.
2433
2434 Not all crypto engines require this callback to retrieve the
2435 passphrase.  It is better if the engine retrieves the passphrase from
2436 a trusted agent (a daemon process), rather than having each user to
2437 implement their own passphrase query.  Some engines do not even
2438 support an external passphrase callback at all, in this case the error
2439 code @code{GPG_ERR_NOT_SUPPORTED} is returned.
2440
2441 The user can disable the use of a passphrase callback function by
2442 calling @code{gpgme_set_passphrase_cb} with @var{passfunc} being
2443 @code{NULL}.
2444 @end deftypefun
2445
2446 @deftypefun void gpgme_get_passphrase_cb (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_passphrase_cb_t *@var{passfunc}}, @w{void **@var{hook_value}})
2447 The function @code{gpgme_get_passphrase_cb} returns the function that
2448 is used when a passphrase needs to be provided by the user in
2449 @var{*passfunc}, and the first argument for this function in
2450 @var{*hook_value}.  If no passphrase callback is set, or @var{ctx} is
2451 not a valid pointer, @code{NULL} is returned in both variables.
2452
2453 @var{passfunc} or @var{hook_value} can be @code{NULL}.  In this case,
2454 the corresponding value will not be returned.
2455 @end deftypefun
2456
2457
2458 @node Progress Meter Callback
2459 @subsection Progress Meter Callback
2460 @cindex callback, progress meter
2461 @cindex progress meter callback
2462
2463 @deftp {Data type} {void (*gpgme_progress_cb_t)(void *@var{hook}, const char *@var{what}, int @var{type}, int @var{current}, int @var{total})}
2464 @tindex gpgme_progress_cb_t
2465 The @code{gpgme_progress_cb_t} type is the type of functions usable as
2466 progress callback function.
2467
2468 The arguments are specific to the crypto engine.  More information
2469 about the progress information returned from the GnuPG engine can be
2470 found in the GnuPG source code in the file @file{doc/DETAILS} in the
2471 section PROGRESS.
2472 @end deftp
2473
2474 @deftypefun void gpgme_set_progress_cb (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_progress_cb_t @var{progfunc}}, @w{void *@var{hook_value}})
2475 The function @code{gpgme_set_progress_cb} sets the function that is
2476 used when progress information about a cryptographic operation is
2477 available.  The function @var{progfunc} needs to implemented by the
2478 user, and whenever it is called, it is called with its first argument
2479 being @var{hook_value}.  By default, no progress callback function
2480 is set.
2481
2482 Setting a callback function allows an interactive program to display
2483 progress information about a long operation to the user.
2484
2485 The user can disable the use of a progress callback function by
2486 calling @code{gpgme_set_progress_cb} with @var{progfunc} being
2487 @code{NULL}.
2488 @end deftypefun
2489
2490 @deftypefun void gpgme_get_progress_cb (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_progress_cb_t *@var{progfunc}}, @w{void **@var{hook_value}})
2491 The function @code{gpgme_get_progress_cb} returns the function that is
2492 used to inform the user about the progress made in @var{*progfunc},
2493 and the first argument for this function in @var{*hook_value}.  If no
2494 progress callback is set, or @var{ctx} is not a valid pointer,
2495 @code{NULL} is returned in both variables.
2496
2497 @var{progfunc} or @var{hook_value} can be @code{NULL}.  In this case,
2498 the corresponding value will not be returned.
2499 @end deftypefun
2500
2501
2502 @node Locale
2503 @subsection Locale
2504 @cindex locale, default
2505 @cindex locale, of a context
2506
2507 A locale setting can be associated with a context.  This locale is
2508 passed to the crypto engine, and used for applications like the PIN
2509 entry, which is displayed to the user when entering a passphrase is
2510 required.
2511
2512 The default locale is used to initialize the locale setting of all
2513 contexts created afterwards.
2514
2515 @deftypefun gpgme_error_t gpgme_set_locale (@w{gpgme_ctx_t @var{ctx}}, @w{int @var{category}}, @w{const char *@var{value}})
2516 The function @code{gpgme_set_locale} sets the locale of the context
2517 @var{ctx}, or the default locale if @var{ctx} is a null pointer.
2518
2519 The locale settings that should be changed are specified by
2520 @var{category}.  Supported categories are @code{LC_CTYPE},
2521 @code{LC_MESSAGES}, and @code{LC_ALL}, which is a wildcard you can use
2522 if you want to change all the categories at once.
2523
2524 The value to be used for the locale setting is @var{value}, which will
2525 be copied to @acronym{GPGME}'s internal data structures.  @var{value}
2526 can be a null pointer, which disables setting the locale, and will
2527 make PIN entry and other applications use their default setting, which
2528 is usually not what you want.
2529
2530 Note that the settings are only used if the application runs on a text
2531 terminal, and that the settings should fit the configuration of the
2532 output terminal.  Normally, it is sufficient to initialize the default
2533 value at startup.
2534
2535 The function returns an error if not enough memory is available.
2536 @end deftypefun
2537
2538
2539 @node Key Management
2540 @section Key Management
2541 @cindex key management
2542
2543 Some of the cryptographic operations require that recipients or
2544 signers are specified.  This is always done by specifying the
2545 respective keys that should be used for the operation.  The following
2546 section describes how such keys can be selected and manipulated.
2547
2548 @deftp {Data type} gpgme_subkey_t
2549 The @code{gpgme_subkey_t} type is a pointer to a subkey structure.
2550 Sub keys are one component of a @code{gpgme_key_t} object.  In fact,
2551 subkeys are those parts that contains the real information about the
2552 individual cryptographic keys that belong to the same key object.  One
2553 @code{gpgme_key_t} can contain several subkeys.  The first subkey in
2554 the linked list is also called the primary key.
2555
2556 The subkey structure has the following members:
2557
2558 @table @code
2559 @item gpgme_subkey_t next
2560 This is a pointer to the next subkey structure in the linked list, or
2561 @code{NULL} if this is the last element.
2562
2563 @item unsigned int revoked : 1
2564 This is true if the subkey is revoked.
2565
2566 @item unsigned int expired : 1
2567 This is true if the subkey is expired.
2568
2569 @item unsigned int disabled : 1
2570 This is true if the subkey is disabled.
2571
2572 @item unsigned int invalid : 1
2573 This is true if the subkey is invalid.
2574
2575 @item unsigned int can_encrypt : 1
2576 This is true if the subkey can be used for encryption.
2577
2578 @item unsigned int can_sign : 1
2579 This is true if the subkey can be used to create data signatures.
2580
2581 @item unsigned int can_certify : 1
2582 This is true if the subkey can be used to create key certificates.
2583
2584 @item unsigned int can_authenticate : 1
2585 This is true if the subkey can be used for authentication.
2586
2587 @item unsigned int is_qualified : 1
2588 This is true if the subkey can be used for qualified signatures
2589 according to local government regulations.
2590
2591 @item unsigned int secret : 1
2592 This is true if the subkey is a secret key.  Note that it will be false
2593 if the key is actually a stub key; i.e. a secret key operation is
2594 currently not possible (offline-key).
2595
2596 @item gpgme_pubkey_algo_t pubkey_algo
2597 This is the public key algorithm supported by this subkey.
2598
2599 @item unsigned int length
2600 This is the length of the subkey (in bits).
2601
2602 @item char *keyid
2603 This is the key ID of the subkey in hexadecimal digits.
2604
2605 @item char *fpr
2606 This is the fingerprint of the subkey in hexadecimal digits, if
2607 available.
2608
2609 @item long int timestamp
2610 This is the creation timestamp of the subkey.  This is -1 if the
2611 timestamp is invalid, and 0 if it is not available.
2612
2613 @item long int expires
2614 This is the expiration timestamp of the subkey, or 0 if the subkey
2615 does not expire.
2616 @end table
2617 @end deftp
2618
2619 @deftp {Data type} gpgme_key_sig_t
2620 The @code{gpgme_key_sig_t} type is a pointer to a key signature structure.
2621 Key signatures are one component of a @code{gpgme_key_t} object, and
2622 validate user IDs on the key.
2623
2624 The signatures on a key are only available if the key was retrieved
2625 via a listing operation with the @code{GPGME_KEYLIST_MODE_SIGS} mode
2626 enabled, because it can be expensive to retrieve all signatures of a
2627 key.
2628
2629 The signature notations on a key signature are only available if the
2630 key was retrieved via a listing operation with the
2631 @code{GPGME_KEYLIST_MODE_SIG_NOTATIONS} mode enabled, because it can
2632 be expensive to retrieve all signature notations.
2633
2634 The key signature structure has the following members:
2635
2636 @table @code
2637 @item gpgme_key_sig_t next
2638 This is a pointer to the next key signature structure in the linked
2639 list, or @code{NULL} if this is the last element.
2640
2641 @item unsigned int revoked : 1
2642 This is true if the key signature is a revocation signature.
2643
2644 @item unsigned int expired : 1
2645 This is true if the key signature is expired.
2646
2647 @item unsigned int invalid : 1
2648 This is true if the key signature is invalid.
2649
2650 @item unsigned int exportable : 1
2651 This is true if the key signature is exportable.
2652
2653 @item gpgme_pubkey_algo_t pubkey_algo
2654 This is the public key algorithm used to create the signature.
2655
2656 @item char *keyid
2657 This is the key ID of the key (in hexadecimal digits) used to create
2658 the signature.
2659
2660 @item long int timestamp
2661 This is the creation timestamp of the key signature.  This is -1 if
2662 the timestamp is invalid, and 0 if it is not available.
2663
2664 @item long int expires
2665 This is the expiration timestamp of the key signature, or 0 if the key
2666 signature does not expire.
2667
2668 @item gpgme_error_t status
2669 This is the status of the signature and has the same meaning as the
2670 member of the same name in a @code{gpgme_signature_t} object.
2671
2672 @item unsigned int sig_class
2673 This specifies the signature class of the key signature.  The meaning
2674 is specific to the crypto engine.
2675
2676 @item char *uid
2677 This is the main user ID of the key used to create the signature.
2678
2679 @item char *name
2680 This is the name component of @code{uid}, if available.
2681
2682 @item char *comment
2683 This is the comment component of @code{uid}, if available.
2684
2685 @item char *email
2686 This is the email component of @code{uid}, if available.
2687
2688 @item gpgme_sig_notation_t notations
2689 This is a linked list with the notation data and policy URLs.
2690 @end table
2691 @end deftp
2692
2693 @deftp {Data type} gpgme_user_id_t
2694 A user ID is a component of a @code{gpgme_key_t} object.  One key can
2695 have many user IDs.  The first one in the list is the main (or
2696 primary) user ID.
2697
2698 The user ID structure has the following members.
2699
2700 @table @code
2701 @item gpgme_user_id_t next
2702 This is a pointer to the next user ID structure in the linked list, or
2703 @code{NULL} if this is the last element.
2704
2705 @item unsigned int revoked : 1
2706 This is true if the user ID is revoked.
2707
2708 @item unsigned int invalid : 1
2709 This is true if the user ID is invalid.
2710
2711 @item gpgme_validity_t validity
2712 This specifies the validity of the user ID.
2713
2714 @item char *uid
2715 This is the user ID string.
2716
2717 @item char *name
2718 This is the name component of @code{uid}, if available.
2719
2720 @item char *comment
2721 This is the comment component of @code{uid}, if available.
2722
2723 @item char *email
2724 This is the email component of @code{uid}, if available.
2725
2726 @item gpgme_key_sig_t signatures
2727 This is a linked list with the signatures on this user ID.
2728 @end table
2729 @end deftp
2730
2731 @deftp {Data type} gpgme_key_t
2732 The @code{gpgme_key_t} type is a pointer to a key object.  It has the
2733 following members:
2734
2735 @table @code
2736 @item gpgme_keylist_mode_t keylist_mode
2737 The keylist mode that was active when the key was retrieved.
2738
2739 @item unsigned int revoked : 1
2740 This is true if the key is revoked.
2741
2742 @item unsigned int expired : 1
2743 This is true if the key is expired.
2744
2745 @item unsigned int disabled : 1
2746 This is true if the key is disabled.
2747
2748 @item unsigned int invalid : 1
2749 This is true if the key is invalid. This might have several reasons,
2750 for a example for the S/MIME backend, it will be set in during key
2751 listsing if the key could not be validated due to a missing
2752 certificates or unmatched policies.
2753
2754 @item unsigned int can_encrypt : 1
2755 This is true if the key (ie one of its subkeys) can be used for
2756 encryption.
2757
2758 @item unsigned int can_sign : 1
2759 This is true if the key (ie one of its subkeys) can be used to create
2760 data signatures.
2761
2762 @item unsigned int can_certify : 1
2763 This is true if the key (ie one of its subkeys) can be used to create
2764 key certificates.
2765
2766 @item unsigned int can_authenticate : 1
2767 This is true if the key (ie one of its subkeys) can be used for
2768 authentication.
2769
2770 @item unsigned int is_qualified : 1
2771 This is true if the key can be used for qualified signatures according
2772 to local government regulations.
2773
2774 @item unsigned int secret : 1
2775 This is true if the key is a secret key.  Note, that this will always be
2776 true even if the corresponding subkey flag may be false (offline/stub
2777 keys).
2778
2779 @item gpgme_protocol_t protocol
2780 This is the protocol supported by this key.
2781
2782 @item char *issuer_serial
2783 If @code{protocol} is @code{GPGME_PROTOCOL_CMS}, then this is the
2784 issuer serial.
2785
2786 @item char *issuer_name
2787 If @code{protocol} is @code{GPGME_PROTOCOL_CMS}, then this is the
2788 issuer name.
2789
2790 @item char *chain_id
2791 If @code{protocol} is @code{GPGME_PROTOCOL_CMS}, then this is the
2792 chain ID, which can be used to built the certificate chain.
2793
2794 @item gpgme_validity_t owner_trust
2795 If @code{protocol} is @code{GPGME_PROTOCOL_OpenPGP}, then this is the
2796 owner trust.
2797
2798 @item gpgme_subkey_t subkeys
2799 This is a linked list with the subkeys of the key.  The first subkey
2800 in the list is the primary key and usually available.
2801
2802 @item gpgme_user_id_t uids
2803 This is a linked list with the user IDs of the key.  The first user ID
2804 in the list is the main (or primary) user ID.
2805 @end table
2806 @end deftp
2807
2808 @menu
2809 * Listing Keys::                  Browsing the list of available keys.
2810 * Information About Keys::        Requesting detailed information about keys.
2811 * Key Signatures::                Listing the signatures on a key.
2812 * Manipulating Keys::             Operations on keys.
2813 * Generating Keys::               Creating new key pairs.
2814 * Exporting Keys::                Retrieving key data from the key ring.
2815 * Importing Keys::                Adding keys to the key ring.
2816 * Deleting Keys::                 Removing keys from the key ring.
2817 * Changing Passphrases::          Change the passphrase of a key.
2818 * Advanced Key Editing::          Advanced key edit operation.
2819 @end menu
2820
2821
2822 @node Listing Keys
2823 @subsection Listing Keys
2824 @cindex listing keys
2825 @cindex key listing
2826 @cindex key listing, start
2827 @cindex key ring, list
2828 @cindex key ring, search
2829
2830 @deftypefun gpgme_error_t gpgme_op_keylist_start (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{pattern}}, @w{int @var{secret_only}})
2831 The function @code{gpgme_op_keylist_start} initiates a key listing
2832 operation inside the context @var{ctx}.  It sets everything up so that
2833 subsequent invocations of @code{gpgme_op_keylist_next} return the keys
2834 in the list.
2835
2836 If @var{pattern} is @code{NULL}, all available keys are returned.
2837 Otherwise, @var{pattern} contains an engine specific expression that
2838 is used to limit the list to all keys matching the pattern.  Note that
2839 the total length of the pattern is restricted to an engine-specific
2840 maximum (a couple of hundred characters are usually accepted).  The
2841 pattern should be used to restrict the search to a certain common name
2842 or user, not to list many specific keys at once by listing their
2843 fingerprints or key IDs.
2844
2845 If @var{secret_only} is not @code{0}, the list is restricted to secret
2846 keys only.
2847
2848 The context will be busy until either all keys are received (and
2849 @code{gpgme_op_keylist_next} returns @code{GPG_ERR_EOF}), or
2850 @code{gpgme_op_keylist_end} is called to finish the operation.
2851
2852 The function returns the error code @code{GPG_ERR_INV_VALUE} if
2853 @var{ctx} is not a valid pointer, and passes through any errors that
2854 are reported by the crypto engine support routines.
2855 @end deftypefun
2856
2857 @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}})
2858 The function @code{gpgme_op_keylist_ext_start} initiates an extended
2859 key listing operation inside the context @var{ctx}.  It sets
2860 everything up so that subsequent invocations of
2861 @code{gpgme_op_keylist_next} return the keys in the list.
2862
2863 If @var{pattern} or @var{*pattern} is @code{NULL}, all available keys
2864 are returned.  Otherwise, @var{pattern} is a @code{NULL} terminated
2865 array of strings that are used to limit the list to all keys matching
2866 at least one of the patterns verbatim.  Note that the total length of
2867 all patterns is restricted to an engine-specific maximum (the exact
2868 limit also depends on the number of patterns and amount of quoting
2869 required, but a couple of hundred characters are usually accepted).
2870 Patterns should be used to restrict the search to a certain common
2871 name or user, not to list many specific keys at once by listing their
2872 fingerprints or key IDs.
2873
2874 If @var{secret_only} is not @code{0}, the list is restricted to secret
2875 keys only.
2876
2877 The value of @var{reserved} must be @code{0}.
2878
2879 The context will be busy until either all keys are received (and
2880 @code{gpgme_op_keylist_next} returns @code{GPG_ERR_EOF}), or
2881 @code{gpgme_op_keylist_end} is called to finish the operation.
2882
2883 The function returns the error code @code{GPG_ERR_INV_VALUE} if
2884 @var{ctx} is not a valid pointer, and passes through any errors that
2885 are reported by the crypto engine support routines.
2886 @end deftypefun
2887
2888 @deftypefun gpgme_error_t gpgme_op_keylist_next (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t *@var{r_key}})
2889 The function @code{gpgme_op_keylist_next} returns the next key in the
2890 list created by a previous @code{gpgme_op_keylist_start} operation in
2891 the context @var{ctx}.  The key will have one reference for the user.
2892 @xref{Manipulating Keys}.
2893
2894 This is the only way to get at @code{gpgme_key_t} objects in
2895 @acronym{GPGME}.
2896
2897 If the last key in the list has already been returned,
2898 @code{gpgme_op_keylist_next} returns @code{GPG_ERR_EOF}.
2899
2900 The function returns the error code @code{GPG_ERR_INV_VALUE} if
2901 @var{ctx} or @var{r_key} is not a valid pointer, and
2902 @code{GPG_ERR_ENOMEM} if there is not enough memory for the operation.
2903 @end deftypefun
2904
2905 @deftypefun gpgme_error_t gpgme_op_keylist_end (@w{gpgme_ctx_t @var{ctx}})
2906 The function @code{gpgme_op_keylist_end} ends a pending key list
2907 operation in the context @var{ctx}.
2908
2909 After the operation completed successfully, the result of the key
2910 listing operation can be retrieved with
2911 @code{gpgme_op_keylist_result}.
2912
2913 The function returns the error code @code{GPG_ERR_INV_VALUE} if
2914 @var{ctx} is not a valid pointer, and @code{GPG_ERR_ENOMEM} if at some
2915 time during the operation there was not enough memory available.
2916 @end deftypefun
2917
2918 The following example illustrates how all keys containing a certain
2919 string (@code{g10code}) can be listed with their key ID and the name
2920 and e-mail address of the main user ID:
2921
2922 @example
2923 gpgme_ctx_t ctx;
2924 gpgme_key_t key;
2925 gpgme_error_t err = gpgme_new (&ctx);
2926
2927 if (!err)
2928   @{
2929     err = gpgme_op_keylist_start (ctx, "g10code", 0);
2930     while (!err)
2931       @{
2932         err = gpgme_op_keylist_next (ctx, &key);
2933         if (err)
2934           break;
2935         printf ("%s:", key->subkeys->keyid);
2936         if (key->uids && key->uids->name)
2937           printf (" %s", key->uids->name);
2938         if (key->uids && key->uids->email)
2939           printf (" <%s>", key->uids->email);
2940         putchar ('\n');
2941         gpgme_key_release (key);
2942       @}
2943     gpgme_release (ctx);
2944   @}
2945 if (gpg_err_code (err) != GPG_ERR_EOF)
2946   @{
2947     fprintf (stderr, "can not list keys: %s\n", gpgme_strerror (err));
2948     exit (1);
2949   @}
2950 @end example
2951
2952 @deftp {Data type} {gpgme_keylist_result_t}
2953 This is a pointer to a structure used to store the result of a
2954 @code{gpgme_op_keylist_*} operation.  After successfully ending a key
2955 listing operation, you can retrieve the pointer to the result with
2956 @code{gpgme_op_keylist_result}.  The structure contains the following
2957 member:
2958
2959 @table @code
2960 @item unsigned int truncated : 1
2961 This is true if the crypto backend had to truncate the result, and
2962 less than the desired keys could be listed.
2963 @end table
2964 @end deftp
2965
2966 @deftypefun gpgme_keylist_result_t gpgme_op_keylist_result (@w{gpgme_ctx_t @var{ctx}})
2967 The function @code{gpgme_op_keylist_result} returns a
2968 @code{gpgme_keylist_result_t} pointer to a structure holding the
2969 result of a @code{gpgme_op_keylist_*} operation.  The pointer is only
2970 valid if the last operation on the context was a key listing
2971 operation, and if this operation finished successfully.  The returned
2972 pointer is only valid until the next operation is started on the
2973 context.
2974 @end deftypefun
2975
2976 In a simple program, for which a blocking operation is acceptable, the
2977 following function can be used to retrieve a single key.
2978
2979 @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}})
2980 The function @code{gpgme_get_key} gets the key with the fingerprint
2981 (or key ID) @var{fpr} from the crypto backend and return it in
2982 @var{r_key}.  If @var{secret} is true, get the secret key.  The
2983 currently active keylist mode is used to retrieve the key.  The key
2984 will have one reference for the user.
2985
2986 If the key is not found in the keyring, @code{gpgme_get_key} returns
2987 the error code @code{GPG_ERR_EOF} and *@var{r_key} will be set to
2988 @code{NULL}.
2989
2990 The function returns the error code @code{GPG_ERR_INV_VALUE} if
2991 @var{ctx} or @var{r_key} is not a valid pointer or @var{fpr} is not a
2992 fingerprint or key ID, @code{GPG_ERR_AMBIGUOUS_NAME} if the key ID was
2993 not a unique specifier for a key, and @code{GPG_ERR_ENOMEM} if at some
2994 time during the operation there was not enough memory available.
2995 @end deftypefun
2996
2997
2998 @node Information About Keys
2999 @subsection Information About Keys
3000 @cindex key, information about
3001 @cindex key, attributes
3002 @cindex attributes, of a key
3003
3004 Please see the beginning of this section for more information about
3005 @code{gpgme_key_t} objects.
3006
3007 @deftp {Data type} gpgme_validity_t
3008 The @code{gpgme_validity_t} type is used to specify the validity of a user ID
3009 in a key.  The following validities are defined:
3010
3011 @table @code
3012 @item GPGME_VALIDITY_UNKNOWN
3013 The user ID is of unknown validity.  The string representation of this
3014 validity is ``?''.
3015
3016 @item GPGME_VALIDITY_UNDEFINED
3017 The validity of the user ID is undefined.  The string representation of this
3018 validity is ``q''.
3019
3020 @item GPGME_VALIDITY_NEVER
3021 The user ID is never valid.  The string representation of this
3022 validity is ``n''.
3023
3024 @item GPGME_VALIDITY_MARGINAL
3025 The user ID is marginally valid.  The string representation of this
3026 validity is ``m''.
3027
3028 @item GPGME_VALIDITY_FULL
3029 The user ID is fully valid.  The string representation of this
3030 validity is ``f''.
3031
3032 @item GPGME_VALIDITY_ULTIMATE
3033 The user ID is ultimately valid.  The string representation of this
3034 validity is ``u''.
3035 @end table
3036 @end deftp
3037
3038
3039 The following interfaces are deprecated and only provided for backward
3040 compatibility.  Don't use them.  They will be removed in a future
3041 version of @acronym{GPGME}.
3042
3043 @deftp {Data type} gpgme_attr_t
3044 The @code{gpgme_attr_t} type is used to specify a key or trust item
3045 attribute.  The following attributes are defined:
3046
3047 @table @code
3048 @item GPGME_ATTR_KEYID
3049 This is the key ID of a sub key.  It is representable as a string.
3050
3051 For trust items, the trust item refers to the key with this ID.
3052
3053 @item GPGME_ATTR_FPR
3054 This is the fingerprint of a sub key.  It is representable as a
3055 string.
3056
3057 @item GPGME_ATTR_ALGO
3058 This is the crypto algorithm for which the sub key can be used.  It
3059 is representable as a string and as a number.  The numbers correspond
3060 to the @code{enum gcry_pk_algos} values in the gcrypt library.
3061
3062 @item GPGME_ATTR_LEN
3063 This is the key length of a sub key.  It is representable as a
3064 number.
3065
3066 @item GPGME_ATTR_CREATED
3067 This is the timestamp at creation time of a sub key.  It is
3068 representable as a number.
3069
3070 @item GPGME_ATTR_EXPIRE
3071 This is the expiration time of a sub key.  It is representable as a
3072 number.
3073
3074 @item GPGME_ATTR_OTRUST
3075 XXX FIXME  (also for trust items)
3076
3077 @item GPGME_ATTR_USERID
3078 This is a user ID.  There can be more than one user IDs in a
3079 @var{gpgme_key_t} object.  The first one (with index 0) is the primary
3080 user ID.  The user ID is representable as a number.
3081
3082 For trust items, this is the user ID associated with this trust item.
3083
3084 @item GPGME_ATTR_NAME
3085 This is the name belonging to a user ID.  It is representable as a string.
3086
3087 @item GPGME_ATTR_EMAIL
3088 This is the email address belonging to a user ID.  It is representable
3089 as a string.
3090
3091 @item GPGME_ATTR_COMMENT
3092 This is the comment belonging to a user ID.  It is representable as a
3093 string.
3094
3095 @item GPGME_ATTR_VALIDITY
3096 This is the validity belonging to a user ID.  It is representable as a
3097 string and as a number.  See below for a list of available validities.
3098
3099 For trust items, this is the validity that is associated with this
3100 trust item.
3101
3102 @item GPGME_ATTR_UID_REVOKED
3103 This specifies if a user ID is revoked.  It is representable as a
3104 number, and is @code{1} if the user ID is revoked, and @code{0}
3105 otherwise.
3106
3107 @item GPGME_ATTR_UID_INVALID
3108 This specifies if a user ID is invalid.  It is representable as a
3109 number, and is @code{1} if the user ID is invalid, and @code{0}
3110 otherwise.
3111
3112 @item GPGME_ATTR_LEVEL
3113 This is the trust level of a trust item.
3114
3115 @item GPGME_ATTR_TYPE
3116 This returns information about the type of key.  For the string function
3117 this will eother be "PGP" or "X.509".  The integer function returns 0
3118 for PGP and 1 for X.509.  It is also used for the type of a trust item.
3119
3120 @item GPGME_ATTR_IS_SECRET
3121 This specifies if the key is a secret key.  It is representable as a
3122 number, and is @code{1} if the key is revoked, and @code{0} otherwise.
3123
3124 @item GPGME_ATTR_KEY_REVOKED
3125 This specifies if a sub key is revoked.  It is representable as a
3126 number, and is @code{1} if the key is revoked, and @code{0} otherwise.
3127
3128 @item GPGME_ATTR_KEY_INVALID
3129 This specifies if a sub key is invalid.  It is representable as a
3130 number, and is @code{1} if the key is invalid, and @code{0} otherwise.
3131
3132 @item GPGME_ATTR_KEY_EXPIRED
3133 This specifies if a sub key is expired.  It is representable as a
3134 number, and is @code{1} if the key is expired, and @code{0} otherwise.
3135
3136 @item GPGME_ATTR_KEY_DISABLED
3137 This specifies if a sub key is disabled.  It is representable as a
3138 number, and is @code{1} if the key is disabled, and @code{0} otherwise.
3139
3140 @item GPGME_ATTR_KEY_CAPS
3141 This is a description of the capabilities of a sub key.  It is
3142 representable as a string.  The string contains the letter ``e'' if
3143 the key can be used for encryption, ``s'' if the key can be used for
3144 signatures, and ``c'' if the key can be used for certifications.
3145
3146 @item GPGME_ATTR_CAN_ENCRYPT
3147 This specifies if a sub key can be used for encryption.  It is
3148 representable as a number, and is @code{1} if the sub key can be used
3149 for encryption, and @code{0} otherwise.
3150
3151 @item GPGME_ATTR_CAN_SIGN
3152 This specifies if a sub key can be used to create data signatures.  It
3153 is representable as a number, and is @code{1} if the sub key can be
3154 used for signatures, and @code{0} otherwise.
3155
3156 @item GPGME_ATTR_CAN_CERTIFY
3157 This specifies if a sub key can be used to create key certificates.
3158 It is representable as a number, and is @code{1} if the sub key can be
3159 used for certifications, and @code{0} otherwise.
3160
3161 @item GPGME_ATTR_SERIAL
3162 The X.509 issuer serial attribute of the key.  It is representable as
3163 a string.
3164
3165 @item GPGME_ATTR_ISSUE
3166 The X.509 issuer name attribute of the key.  It is representable as a
3167 string.
3168
3169 @item GPGME_ATTR_CHAINID
3170 The X.509 chain ID can be used to build the certification chain.  It
3171 is representable as a string.
3172 @end table
3173 @end deftp
3174
3175 @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}})
3176 The function @code{gpgme_key_get_string_attr} returns the value of the
3177 string-representable attribute @var{what} of key @var{key}.  If the
3178 attribute is an attribute of a sub key or an user ID, @var{idx}
3179 specifies the sub key or user ID of which the attribute value is
3180 returned.  The argument @var{reserved} is reserved for later use and
3181 should be @code{NULL}.
3182
3183 The string returned is only valid as long as the key is valid.
3184
3185 The function returns @code{0} if an attribute can't be returned as a
3186 string, @var{key} is not a valid pointer, @var{idx} out of range,
3187 or @var{reserved} not @code{NULL}.
3188 @end deftypefun
3189
3190 @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}})
3191 The function @code{gpgme_key_get_ulong_attr} returns the value of the
3192 number-representable attribute @var{what} of key @var{key}.  If the
3193 attribute is an attribute of a sub key or an user ID, @var{idx}
3194 specifies the sub key or user ID of which the attribute value is
3195 returned.  The argument @var{reserved} is reserved for later use and
3196 should be @code{NULL}.
3197
3198 The function returns @code{0} if the attribute can't be returned as a
3199 number, @var{key} is not a valid pointer, @var{idx} out of range, or
3200 @var{reserved} not @code{NULL}.
3201 @end deftypefun
3202
3203
3204 @node Key Signatures
3205 @subsection Key Signatures
3206 @cindex key, signatures
3207 @cindex signatures, on a key
3208
3209 The following interfaces are deprecated and only provided for backward
3210 compatibility.  Don't use them.  They will be removed in a future
3211 version of @acronym{GPGME}.
3212
3213 The signatures on a key are only available if the key was retrieved
3214 via a listing operation with the @code{GPGME_KEYLIST_MODE_SIGS} mode
3215 enabled, because it is expensive to retrieve all signatures of a key.
3216
3217 So, before using the below interfaces to retrieve the signatures on a
3218 key, you have to make sure that the key was listed with signatures
3219 enabled.  One convenient, but blocking, way to do this is to use the
3220 function @code{gpgme_get_key}.
3221
3222 @deftp {Data type} gpgme_attr_t
3223 The @code{gpgme_attr_t} type is used to specify a key signature
3224 attribute.  The following attributes are defined:
3225
3226 @table @code
3227 @item GPGME_ATTR_KEYID
3228 This is the key ID of the key which was used for the signature.  It is
3229 representable as a string.
3230
3231 @item GPGME_ATTR_ALGO
3232 This is the crypto algorithm used to create the signature.  It is
3233 representable as a string and as a number.  The numbers correspond to
3234 the @code{enum gcry_pk_algos} values in the gcrypt library.
3235
3236 @item GPGME_ATTR_CREATED
3237 This is the timestamp at creation time of the signature.  It is
3238 representable as a number.
3239
3240 @item GPGME_ATTR_EXPIRE
3241 This is the expiration time of the signature.  It is representable as
3242 a number.
3243
3244 @item GPGME_ATTR_USERID
3245 This is the user ID associated with the signing key.  The user ID is
3246 representable as a number.
3247
3248 @item GPGME_ATTR_NAME
3249 This is the name belonging to a user ID.  It is representable as a string.
3250
3251 @item GPGME_ATTR_EMAIL
3252 This is the email address belonging to a user ID.  It is representable
3253 as a string.
3254
3255 @item GPGME_ATTR_COMMENT
3256 This is the comment belonging to a user ID.  It is representable as a
3257 string.
3258
3259 @item GPGME_ATTR_KEY_REVOKED
3260 This specifies if a key signature is a revocation signature.  It is
3261 representable as a number, and is @code{1} if the key is revoked, and
3262 @code{0} otherwise.
3263
3264 @c @item GPGME_ATTR_KEY_EXPIRED
3265 @c This specifies if a key signature is expired.  It is representable as
3266 @c a number, and is @code{1} if the key is revoked, and @code{0}
3267 @c otherwise.
3268 @c
3269 @item GPGME_ATTR_SIG_CLASS
3270 This specifies the signature class of a key signature.  It is
3271 representable as a number.  The meaning is specific to the crypto
3272 engine.
3273
3274 @item GPGME_ATTR_SIG_CLASS
3275 This specifies the signature class of a key signature.  It is
3276 representable as a number.  The meaning is specific to the crypto
3277 engine.
3278
3279 @item GPGME_ATTR_SIG_STATUS
3280 This is the same value as returned by @code{gpgme_get_sig_status}.
3281 @end table
3282 @end deftp
3283
3284 @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}})
3285 The function @code{gpgme_key_sig_get_string_attr} returns the value of
3286 the string-representable attribute @var{what} of the signature
3287 @var{idx} on the user ID @var{uid_idx} in the key @var{key}.  The
3288 argument @var{reserved} is reserved for later use and should be
3289 @code{NULL}.
3290
3291 The string returned is only valid as long as the key is valid.
3292
3293 The function returns @code{0} if an attribute can't be returned as a
3294 string, @var{key} is not a valid pointer, @var{uid_idx} or @var{idx}
3295 out of range, or @var{reserved} not @code{NULL}.
3296 @end deftypefun
3297
3298 @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}})
3299 The function @code{gpgme_key_sig_get_ulong_attr} returns the value of
3300 the number-representable attribute @var{what} of the signature
3301 @var{idx} on the user ID @var{uid_idx} in the key @var{key}.  The
3302 argument @var{reserved} is reserved for later use and should be
3303 @code{NULL}.
3304
3305 The function returns @code{0} if an attribute can't be returned as a
3306 string, @var{key} is not a valid pointer, @var{uid_idx} or @var{idx}
3307 out of range, or @var{reserved} not @code{NULL}.
3308 @end deftypefun
3309
3310
3311 @node Manipulating Keys
3312 @subsection Manipulating Keys
3313 @cindex key, manipulation
3314
3315 @deftypefun void gpgme_key_ref (@w{gpgme_key_t @var{key}})
3316 The function @code{gpgme_key_ref} acquires an additional reference for
3317 the key @var{key}.
3318 @end deftypefun
3319
3320 @deftypefun void gpgme_key_unref (@w{gpgme_key_t @var{key}})
3321 The function @code{gpgme_key_unref} releases a reference for the key
3322 @var{key}.  If this was the last reference, the key will be destroyed
3323 and all resources associated to it will be released.
3324 @end deftypefun
3325
3326
3327 The following interface is deprecated and only provided for backward
3328 compatibility.  Don't use it.  It will be removed in a future version
3329 of @acronym{GPGME}.
3330
3331 @deftypefun void gpgme_key_release (@w{gpgme_key_t @var{key}})
3332 The function @code{gpgme_key_release} is equivalent to
3333 @code{gpgme_key_unref}.
3334 @end deftypefun
3335
3336
3337 @node Generating Keys
3338 @subsection Generating Keys
3339 @cindex key, creation
3340 @cindex key ring, add
3341
3342 @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}})
3343 The function @code{gpgme_op_genkey} generates a new key pair in the
3344 context @var{ctx}.  The meaning of @var{public} and @var{secret}
3345 depends on the crypto backend.
3346
3347 GnuPG does not support @var{public} and @var{secret}, they should be
3348 @code{NULL}.  GnuPG will generate a key pair and add it to the
3349 standard key ring.  The fingerprint of the generated key is available
3350 with @code{gpgme_op_genkey_result}.
3351
3352 GpgSM requires @var{public} to be a writable data object.  GpgSM will
3353 generate a secret key (which will be stored by @command{gpg-agent},
3354 and return a certificate request in @var{public}, which then needs to
3355 be signed by the certification authority and imported before it can be
3356 used.  GpgSM does not make the fingerprint available.
3357
3358 The argument @var{parms} specifies parameters for the key in an XML
3359 string.  The details about the format of @var{parms} are specific to
3360 the crypto engine used by @var{ctx}.  Here is an example for GnuPG as
3361 the crypto engine (all parameters of OpenPGP key generation are
3362 documented in the GPG manual):
3363
3364 @example
3365 <GnupgKeyParms format="internal">
3366 Key-Type: default
3367 Subkey-Type: default
3368 Name-Real: Joe Tester
3369 Name-Comment: with stupid passphrase
3370 Name-Email: joe@@foo.bar
3371 Expire-Date: 0
3372 Passphrase: abc
3373 </GnupgKeyParms>
3374 @end example
3375
3376 Here is an example for GpgSM as the crypto engine (all parameters of
3377 OpenPGP key generation are documented in the GPGSM manual):
3378
3379 @example
3380 <GnupgKeyParms format="internal">
3381 Key-Type: RSA
3382 Key-Length: 1024
3383 Name-DN: C=de,O=g10 code,OU=Testlab,CN=Joe 2 Tester
3384 Name-Email: joe@@foo.bar
3385 </GnupgKeyParms>
3386 @end example
3387
3388 Strings should be given in UTF-8 encoding.  The only format supported
3389 for now is ``internal''.  The content of the @code{GnupgKeyParms}
3390 container is passed verbatim to the crypto backend.  Control
3391 statements are not allowed.
3392
3393 After the operation completed successfully, the result can be
3394 retrieved with @code{gpgme_op_genkey_result}.
3395
3396 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3397 operation could be started successfully, @code{GPG_ERR_INV_VALUE} if
3398 @var{parms} is not a valid XML string, @code{GPG_ERR_NOT_SUPPORTED} if
3399 @var{public} or @var{secret} is not valid, and @code{GPG_ERR_GENERAL}
3400 if no key was created by the backend.
3401 @end deftypefun
3402
3403 @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}})
3404 The function @code{gpgme_op_genkey_start} initiates a
3405 @code{gpgme_op_genkey} operation.  It can be completed by calling
3406 @code{gpgme_wait} on the context.  @xref{Waiting For Completion}.
3407
3408 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3409 operation could be started successfully, @code{GPG_ERR_INV_VALUE} if
3410 @var{parms} is not a valid XML string, and
3411 @code{GPG_ERR_NOT_SUPPORTED} if @var{public} or @var{secret} is not
3412 @code{NULL}.
3413 @end deftypefun
3414
3415 @deftp {Data type} {gpgme_genkey_result_t}
3416 This is a pointer to a structure used to store the result of a
3417 @code{gpgme_op_genkey} operation.  After successfully generating a
3418 key, you can retrieve the pointer to the result with
3419 @code{gpgme_op_genkey_result}.  The structure contains the following
3420 members:
3421
3422 @table @code
3423 @item unsigned int primary : 1
3424 This is a flag that is set to 1 if a primary key was created and to 0
3425 if not.
3426
3427 @item unsigned int sub : 1
3428 This is a flag that is set to 1 if a subkey was created and to 0
3429 if not.
3430
3431 @item char *fpr
3432 This is the fingerprint of the key that was created.  If both a
3433 primary and a sub key were generated, the fingerprint of the primary
3434 key will be returned.  If the crypto engine does not provide the
3435 fingerprint, @code{fpr} will be a null pointer.
3436 @end table
3437 @end deftp
3438
3439 @deftypefun gpgme_genkey_result_t gpgme_op_genkey_result (@w{gpgme_ctx_t @var{ctx}})
3440 The function @code{gpgme_op_genkey_result} returns a
3441 @code{gpgme_genkey_result_t} pointer to a structure holding the result of
3442 a @code{gpgme_op_genkey} operation.  The pointer is only valid if the
3443 last operation on the context was a @code{gpgme_op_genkey} or
3444 @code{gpgme_op_genkey_start} operation, and if this operation finished
3445 successfully.  The returned pointer is only valid until the next
3446 operation is started on the context.
3447 @end deftypefun
3448
3449
3450 @node Exporting Keys
3451 @subsection Exporting Keys
3452 @cindex key, export
3453 @cindex key ring, export from
3454
3455 Exporting keys means the same as running @command{gpg} with the command
3456 @option{--export}.  However, a mode flag can be used to change the way
3457 the export works.  The available mode flags are described below, they
3458 may be or-ed together.
3459
3460 @table @code
3461
3462 @item GPGME_EXPORT_MODE_EXTERN
3463 If this bit is set, the output is send directly to the default
3464 keyserver. This is currently only allowed for OpenPGP keys.  It is good
3465 practise to not send more than a few dozens key to a keyserver at one
3466 time.  Using this flag requires that the @var{keydata} argument of the
3467 export function is set to @code{NULL}.
3468
3469 @item GPGME_EXPORT_MODE_MINIMAL
3470 If this bit is set, the smallest possible key is exported.  For OpenPGP
3471 keys it removes all signatures except for the latest self-signatures.
3472 For X.509 keys it has no effect.
3473
3474
3475 @end table
3476
3477
3478
3479 @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}})
3480 The function @code{gpgme_op_export} extracts public keys and returns
3481 them in the data buffer @var{keydata}.  The output format of the key
3482 data returned is determined by the @acronym{ASCII} armor attribute set
3483 for the context @var{ctx}, or, if that is not set, by the encoding
3484 specified for @var{keydata}.
3485
3486 If @var{pattern} is @code{NULL}, all available keys are returned.
3487 Otherwise, @var{pattern} contains an engine specific expression that
3488 is used to limit the list to all keys matching the pattern.
3489
3490 @var{mode} is usually 0; other values are described above.
3491
3492 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3493 operation completed successfully, @code{GPG_ERR_INV_VALUE} if
3494 @var{keydata} is not a valid empty data buffer, and passes through any
3495 errors that are reported by the crypto engine support routines.
3496 @end deftypefun
3497
3498 @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}})
3499 The function @code{gpgme_op_export_start} initiates a
3500 @code{gpgme_op_export} operation.  It can be completed by calling
3501 @code{gpgme_wait} on the context.  @xref{Waiting For Completion}.
3502
3503 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3504 operation could be started successfully, and @code{GPG_ERR_INV_VALUE}
3505 if @var{keydata} is not a valid empty data buffer.
3506 @end deftypefun
3507
3508 @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}})
3509 The function @code{gpgme_op_export} extracts public keys and returns
3510 them in the data buffer @var{keydata}.  The output format of the key
3511 data returned is determined by the @acronym{ASCII} armor attribute set
3512 for the context @var{ctx}, or, if that is not set, by the encoding
3513 specified for @var{keydata}.
3514
3515 If @var{pattern} or @var{*pattern} is @code{NULL}, all available keys
3516 are returned.  Otherwise, @var{pattern} is a @code{NULL} terminated
3517 array of strings that are used to limit the list to all keys matching
3518 at least one of the patterns verbatim.
3519
3520 @var{mode} is usually 0; other values are described above.
3521
3522 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3523 operation completed successfully, @code{GPG_ERR_INV_VALUE} if
3524 @var{keydata} is not a valid empty data buffer, and passes through any
3525 errors that are reported by the crypto engine support routines.
3526 @end deftypefun
3527
3528 @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}})
3529 The function @code{gpgme_op_export_ext_start} initiates a
3530 @code{gpgme_op_export_ext} operation.  It can be completed by calling
3531 @code{gpgme_wait} on the context.  @xref{Waiting For Completion}.
3532
3533 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3534 operation could be started successfully, and @code{GPG_ERR_INV_VALUE}
3535 if @var{keydata} is not a valid empty data buffer.
3536 @end deftypefun
3537
3538
3539 @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}})
3540 The function @code{gpgme_op_export_keys} extracts public keys and returns
3541 them in the data buffer @var{keydata}.  The output format of the key
3542 data returned is determined by the @acronym{ASCII} armor attribute set
3543 for the context @var{ctx}, or, if that is not set, by the encoding
3544 specified for @var{keydata}.
3545
3546 The keys to export are taken form the @code{NULL} terminated array
3547 @var{keys}.  Only keys of the the currently selected protocol of
3548 @var{ctx} which do have a fingerprint set are considered for export.
3549 Other keys specified by the @var{keys} are ignored.  In particular
3550 OpenPGP keys retrieved via an external key listing are not included.
3551
3552 @var{mode} is usually 0; other values are described above.
3553
3554 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3555 operation completed successfully, @code{GPG_ERR_INV_VALUE} if
3556 @var{keydata} is not a valid empty data buffer, @code{GPG_ERR_NO_DATA}
3557 if no useful keys are in @var{keys} and passes through any errors that
3558 are reported by the crypto engine support routines.
3559 @end deftypefun
3560
3561 @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}})
3562 The function @code{gpgme_op_export_keys_start} initiates a
3563 @code{gpgme_op_export_ext} operation.  It can be completed by calling
3564 @code{gpgme_wait} on the context.  @xref{Waiting For Completion}.
3565
3566 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3567 operation could be started successfully, and @code{GPG_ERR_INV_VALUE}
3568 if @var{keydata} is not a valid empty data buffer, @code{GPG_ERR_NO_DATA}
3569 if no useful keys are in @var{keys} and passes through any errors that
3570 are reported by the crypto engine support routines.
3571 @end deftypefun
3572
3573
3574 @node Importing Keys
3575 @subsection Importing Keys
3576 @cindex key, import
3577 @cindex key ring, import to
3578
3579 Importing keys means the same as running @command{gpg} with the command
3580 @option{--import}.
3581
3582
3583 @deftypefun gpgme_error_t gpgme_op_import (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_data_t @var{keydata}})
3584 The function @code{gpgme_op_import} adds the keys in the data buffer
3585 @var{keydata} to the key ring of the crypto engine used by @var{ctx}.
3586 The format of @var{keydata} can be @acronym{ASCII} armored, for example,
3587 but the details are specific to the crypto engine.
3588
3589 After the operation completed successfully, the result can be
3590 retrieved with @code{gpgme_op_import_result}.
3591
3592 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3593 import was completed successfully, @code{GPG_ERR_INV_VALUE} if
3594 @var{keydata} if @var{ctx} or @var{keydata} is not a valid pointer,
3595 and @code{GPG_ERR_NO_DATA} if @var{keydata} is an empty data buffer.
3596 @end deftypefun
3597
3598 @deftypefun gpgme_error_t gpgme_op_import_start (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_data_t @var{keydata}})
3599 The function @code{gpgme_op_import_start} initiates a
3600 @code{gpgme_op_import} operation.  It can be completed by calling
3601 @code{gpgme_wait} on the context.  @xref{Waiting For Completion}.
3602
3603 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3604 import could be started successfully, @code{GPG_ERR_INV_VALUE} if
3605 @var{keydata} if @var{ctx} or @var{keydata} is not a valid pointer,
3606 and @code{GPG_ERR_NO_DATA} if @var{keydata} is an empty data buffer.
3607 @end deftypefun
3608
3609 @deftypefun gpgme_error_t gpgme_op_import_keys (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t *@var{keys}})
3610 The function @code{gpgme_op_import_keys} adds the keys described by the
3611 @code{NULL} terminated array @var{keys} to the key ring of the crypto
3612 engine used by @var{ctx}.  This function is the general interface to
3613 move a key from one crypto engine to another as long as they are
3614 compatible.  In particular it is used to actually import and make keys
3615 permanent which have been retrieved from an external source (i.e. using
3616 @code{GPGME_KEYLIST_MODE_EXTERN}).  @footnote{Thus it is a replacement
3617 for the usual workaround of exporting and then importing a key to make
3618 an X.509 key permanent.}
3619
3620 Only keys of the the currently selected protocol of @var{ctx} are
3621 considered for import.  Other keys specified by the @var{keys} are
3622 ignored.  As of now all considered keys must have been retrieved using
3623 the same method, that is the used key listing mode must be identical.
3624
3625 After the operation completed successfully, the result can be
3626 retrieved with @code{gpgme_op_import_result}.
3627
3628 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3629 import was completed successfully, @code{GPG_ERR_INV_VALUE} if
3630 @var{keydata} if @var{ctx} or @var{keydata} is not a valid pointer,
3631 @code{GPG_ERR_CONFLICT} if the key listing mode does not match, and
3632 @code{GPG_ERR_NO_DATA} if no keys are considered for export.
3633 @end deftypefun
3634
3635 @deftypefun gpgme_error_t gpgme_op_import_keys_start (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_key_t *@var{keys}})
3636 The function @code{gpgme_op_import_keys_start} initiates a
3637 @code{gpgme_op_import_keys} operation.  It can be completed by calling
3638 @code{gpgme_wait} on the context.  @xref{Waiting For Completion}.
3639
3640 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3641 import was completed successfully, @code{GPG_ERR_INV_VALUE} if
3642 @var{keydata} if @var{ctx} or @var{keydata} is not a valid pointer,
3643 @code{GPG_ERR_CONFLICT} if the key listing mode does not match, and
3644 @code{GPG_ERR_NO_DATA} if no keys are considered for export.
3645 @end deftypefun
3646
3647 @deftp {Data type} {gpgme_import_status_t}
3648 This is a pointer to a structure used to store a part of the result of
3649 a @code{gpgme_op_import} operation.  For each considered key one
3650 status is added that contains information about the result of the
3651 import.  The structure contains the following members:
3652
3653 @table @code
3654 @item gpgme_import_status_t next
3655 This is a pointer to the next status structure in the linked list, or
3656 @code{NULL} if this is the last element.
3657
3658 @item char *fpr
3659 This is the fingerprint of the key that was considered.
3660
3661 @item gpgme_error_t result
3662 If the import was not successful, this is the error value that caused
3663 the import to fail.  Otherwise the error code is
3664 @code{GPG_ERR_NO_ERROR}.
3665
3666 @item unsigned int status
3667 This is a bit-wise OR of the following flags that give more
3668 information about what part of the key was imported.  If the key was
3669 already known, this might be 0.
3670
3671 @table @code
3672 @item GPGME_IMPORT_NEW
3673 The key was new.
3674
3675 @item GPGME_IMPORT_UID
3676 The key contained new user IDs.
3677
3678 @item GPGME_IMPORT_SIG
3679 The key contained new signatures.
3680
3681 @item GPGME_IMPORT_SUBKEY
3682 The key contained new sub keys.
3683
3684 @item GPGME_IMPORT_SECRET
3685 The key contained a secret key.
3686 @end table
3687 @end table
3688 @end deftp
3689
3690 @deftp {Data type} {gpgme_import_result_t}
3691 This is a pointer to a structure used to store the result of a
3692 @code{gpgme_op_import} operation.  After a successful import
3693 operation, you can retrieve the pointer to the result with
3694 @code{gpgme_op_import_result}.  The structure contains the following
3695 members:
3696
3697 @table @code
3698 @item int considered
3699 The total number of considered keys.
3700
3701 @item int no_user_id
3702 The number of keys without user ID.
3703
3704 @item int imported
3705 The total number of imported keys.
3706
3707 @item imported_rsa
3708 The number of imported RSA keys.
3709
3710 @item unchanged
3711 The number of unchanged keys.
3712
3713 @item new_user_ids
3714 The number of new user IDs.
3715
3716 @item new_sub_keys
3717 The number of new sub keys.
3718
3719 @item new_signatures
3720 The number of new signatures.
3721
3722 @item new_revocations
3723 The number of new revocations.
3724
3725 @item secret_read
3726 The total number of secret keys read.
3727
3728 @item secret_imported
3729 The number of imported secret keys.
3730
3731 @item secret_unchanged
3732 The number of unchanged secret keys.
3733
3734 @item not_imported
3735 The number of keys not imported.
3736
3737 @item gpgme_import_status_t imports
3738 A list of gpgme_import_status_t objects which contain more information
3739 about the keys for which an import was attempted.
3740 @end table
3741 @end deftp
3742
3743 @deftypefun gpgme_import_result_t gpgme_op_import_result (@w{gpgme_ctx_t @var{ctx}})
3744 The function @code{gpgme_op_import_result} returns a
3745 @code{gpgme_import_result_t} pointer to a structure holding the result
3746 of a @code{gpgme_op_import} operation.  The pointer is only valid if
3747 the last operation on the context was a @code{gpgme_op_import} or
3748 @code{gpgme_op_import_start} operation, and if this operation finished
3749 successfully.  The returned pointer is only valid until the next
3750 operation is started on the context.
3751 @end deftypefun
3752
3753 The following interface is deprecated and only provided for backward
3754 compatibility.  Don't use it.  It will be removed in a future version
3755 of @acronym{GPGME}.
3756
3757 @deftypefun gpgme_error_t gpgme_op_import_ext (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_data_t @var{keydata}}, @w{int *@var{nr}})
3758 The function @code{gpgme_op_import_ext} is equivalent to:
3759
3760 @example
3761   gpgme_error_t err = gpgme_op_import (ctx, keydata);
3762   if (!err)
3763     @{
3764       gpgme_import_result_t result = gpgme_op_import_result (ctx);
3765       *nr = result->considered;
3766     @}
3767 @end example
3768 @end deftypefun
3769
3770
3771 @node Deleting Keys
3772 @subsection Deleting Keys
3773 @cindex key, delete
3774 @cindex key ring, delete from
3775
3776 @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}})
3777 The function @code{gpgme_op_delete} deletes the key @var{key} from the
3778 key ring of the crypto engine used by @var{ctx}.  If
3779 @var{allow_secret} is @code{0}, only public keys are deleted,
3780 otherwise secret keys are deleted as well, if that is supported.
3781
3782 The function returns the error code @code{GPG_ERR_NO_ERROR} if the key
3783 was deleted successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx} or
3784 @var{key} is not a valid pointer, @code{GPG_ERR_NO_PUBKEY} if
3785 @var{key} could not be found in the keyring,
3786 @code{GPG_ERR_AMBIGUOUS_NAME} if the key was not specified
3787 unambiguously, and @code{GPG_ERR_CONFLICT} if the secret key for
3788 @var{key} is available, but @var{allow_secret} is zero.
3789 @end deftypefun
3790
3791 @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}})
3792 The function @code{gpgme_op_delete_start} initiates a
3793 @code{gpgme_op_delete} operation.  It can be completed by calling
3794 @code{gpgme_wait} on the context.  @xref{Waiting For Completion}.
3795
3796 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3797 operation was started successfully, and @code{GPG_ERR_INV_VALUE} if
3798 @var{ctx} or @var{key} is not a valid pointer.
3799 @end deftypefun
3800
3801
3802 @node Changing Passphrases
3803 @subsection  Changing Passphrases
3804 @cindex passphrase, change
3805
3806 @deftypefun gpgme_error_t gpgme_op_passwd      @
3807              (@w{gpgme_ctx_t @var{ctx}},       @
3808               @w{const gpgme_key_t @var{key}}, @
3809               @w{unsigned int @var{flags}})
3810
3811 The function @code{gpgme_op_passwd} changes the passphrase of the
3812 private key associated with @var{key}.  The only allowed value for
3813 @var{flags} is @code{0}.  The backend engine will usually popup a window
3814 to ask for the old and the new passphrase.  Thus this function is not
3815 useful in a server application (where passphrases are not required
3816 anyway).
3817
3818 Note that old @code{gpg} engines (before version 2.0.15) do not support
3819 this command and will silently ignore it.
3820 @end deftypefun
3821
3822 @deftypefun gpgme_error_t gpgme_op_passwd_start      @
3823              (@w{gpgme_ctx_t @var{ctx}},       @
3824               @w{const gpgme_key_t @var{key}}, @
3825               @w{unsigned int @var{flags}})
3826
3827 The function @code{gpgme_op_passwd_start} initiates a
3828 @code{gpgme_op_passwd} operation.    It can be completed by calling
3829 @code{gpgme_wait} on the context.  @xref{Waiting For Completion}.
3830
3831 The function returns @code{0} if the operation was started successfully,
3832 and an error code if one of the arguments is not valid or the oepration
3833 could not be started.
3834 @end deftypefun
3835
3836
3837 @node Advanced Key Editing
3838 @subsection Advanced Key Editing
3839 @cindex key, edit
3840
3841 @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}})}
3842 @tindex gpgme_edit_cb_t
3843 The @code{gpgme_edit_cb_t} type is the type of functions which
3844 @acronym{GPGME} calls if it a key edit operation is on-going.  The
3845 status code @var{status} and the argument line @var{args} are passed
3846 through by @acronym{GPGME} from the crypto engine.  The file
3847 descriptor @var{fd} is -1 for normal status messages.  If @var{status}
3848 indicates a command rather than a status message, the response to the
3849 command should be written to @var{fd}.  The @var{handle} is provided
3850 by the user at start of operation.
3851
3852 The function should return @code{GPG_ERR_NO_ERROR} or an error value.
3853 @end deftp
3854
3855 @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}})
3856 The function @code{gpgme_op_edit} processes the key @var{KEY}
3857 interactively, using the edit callback function @var{FNC} with the
3858 handle @var{HANDLE}.  The callback is invoked for every status and
3859 command request from the crypto engine.  The output of the crypto
3860 engine is written to the data object @var{out}.
3861
3862 Note that the protocol between the callback function and the crypto
3863 engine is specific to the crypto engine and no further support in
3864 implementing this protocol correctly is provided by @acronym{GPGME}.
3865
3866 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3867 edit operation completes successfully, @code{GPG_ERR_INV_VALUE} if
3868 @var{ctx} or @var{key} is not a valid pointer, and any error returned
3869 by the crypto engine or the edit callback handler.
3870 @end deftypefun
3871
3872 @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}})
3873 The function @code{gpgme_op_edit_start} initiates a
3874 @code{gpgme_op_edit} operation.  It can be completed by calling
3875 @code{gpgme_wait} on the context.  @xref{Waiting For Completion}.
3876
3877 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3878 operation was started successfully, and @code{GPG_ERR_INV_VALUE} if
3879 @var{ctx} or @var{key} is not a valid pointer.
3880 @end deftypefun
3881
3882
3883 @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}})
3884 The function @code{gpgme_op_card_edit} is analogous to
3885 @code{gpgme_op_edit}, but should be used to process the smart card corresponding to the key @var{key}.
3886 @end deftypefun
3887
3888 @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}})
3889 The function @code{gpgme_op_card_edit_start} initiates a
3890 @code{gpgme_op_card_edit} operation.  It can be completed by calling
3891 @code{gpgme_wait} on the context.  @xref{Waiting For Completion}.
3892
3893 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
3894 operation was started successfully, and @code{GPG_ERR_INV_VALUE} if
3895 @var{ctx} or @var{key} is not a valid pointer.
3896 @end deftypefun
3897
3898
3899 @node Trust Item Management
3900 @section Trust Item Management
3901 @cindex trust item
3902
3903 @strong{Caution:} The trust items interface is experimental.
3904
3905 @deftp {Data type} gpgme_trust_item_t
3906 The @code{gpgme_trust_item_t} type is a pointer to a trust item object.
3907 It has the following members:
3908
3909 @table @code
3910 @item char *keyid
3911 This is a string describing the key to which this trust items belongs.
3912
3913 @item int type
3914 This is the type of the trust item.  A value of 1 refers to a key, a
3915 value of 2 refers to a user ID.
3916
3917 @item int level
3918 This is the trust level.
3919
3920 @item char *owner_trust
3921 The owner trust if @code{type} is 1.
3922
3923 @item char *validity
3924 The calculated validity.
3925
3926 @item char *name
3927 The user name if @code{type} is 2.
3928 @end table
3929 @end deftp
3930
3931 @menu
3932 * Listing Trust Items::           Browsing the list of available trust items.
3933 * Information About Trust Items:: Requesting information about trust items.
3934 * Manipulating Trust Items::      Operations on trust items.
3935 @end menu
3936
3937
3938 @node Listing Trust Items
3939 @subsection Listing Trust Items
3940 @cindex trust item list
3941
3942 @deftypefun gpgme_error_t gpgme_op_trustlist_start (@w{gpgme_ctx_t @var{ctx}}, @w{const char *@var{pattern}}, @w{int @var{max_level}})
3943 The function @code{gpgme_op_trustlist_start} initiates a trust item
3944 listing operation inside the context @var{ctx}.  It sets everything up
3945 so that subsequent invocations of @code{gpgme_op_trustlist_next} return
3946 the trust items in the list.
3947
3948 The string @var{pattern} contains an engine specific expression that
3949 is used to limit the list to all trust items matching the pattern.  It
3950 can not be the empty string.
3951
3952 The argument @var{max_level} is currently ignored.
3953
3954 The context will be busy until either all trust items are received
3955 (and @code{gpgme_op_trustlist_next} returns @code{GPG_ERR_EOF}), or
3956 @code{gpgme_op_trustlist_end} is called to finish the operation.
3957
3958 The function returns the error code @code{GPG_ERR_INV_VALUE} if
3959 @var{ctx} is not a valid pointer, and passes through any errors that
3960 are reported by the crypto engine support routines.
3961 @end deftypefun
3962
3963 @deftypefun gpgme_error_t gpgme_op_trustlist_next (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_trust_item_t *@var{r_item}})
3964 The function @code{gpgme_op_trustlist_next} returns the next trust
3965 item in the list created by a previous @code{gpgme_op_trustlist_start}
3966 operation in the context @var{ctx}.  The trust item can be destroyed
3967 with @code{gpgme_trust_item_release}.  @xref{Manipulating Trust Items}.
3968
3969 This is the only way to get at @code{gpgme_trust_item_t} objects in
3970 @acronym{GPGME}.
3971
3972 If the last trust item in the list has already been returned,
3973 @code{gpgme_op_trustlist_next} returns @code{GPG_ERR_EOF}.
3974
3975 The function returns the error code @code{GPG_ERR_INV_VALUE} if @var{ctx} or
3976 @var{r_item} is not a valid pointer, and @code{GPG_ERR_ENOMEM} if
3977 there is not enough memory for the operation.
3978 @end deftypefun
3979
3980 @deftypefun gpgme_error_t gpgme_op_trustlist_end (@w{gpgme_ctx_t @var{ctx}})
3981 The function @code{gpgme_op_trustlist_end} ends a pending trust list
3982 operation in the context @var{ctx}.
3983
3984 The function returns the error code @code{GPG_ERR_INV_VALUE} if
3985 @var{ctx} is not a valid pointer, and @code{GPG_ERR_ENOMEM} if at some
3986 time during the operation there was not enough memory available.
3987 @end deftypefun
3988
3989
3990 @node Information About Trust Items
3991 @subsection Information About Trust Items
3992 @cindex trust item, information about
3993 @cindex trust item, attributes
3994 @cindex attributes, of a trust item
3995
3996 The following interfaces are deprecated and only provided for backward
3997 compatibility.  Don't use them.  They will be removed in a future
3998 version of @acronym{GPGME}.
3999
4000 Trust items have attributes which can be queried using the interfaces
4001 below.  The attribute identifiers are shared with those for key
4002 attributes.  @xref{Information About Keys}.
4003
4004 @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}})
4005 The function @code{gpgme_trust_item_get_string_attr} returns the value
4006 of the string-representable attribute @var{what} of trust item
4007 @var{item}.  The arguments @var{idx} and @var{reserved} are reserved
4008 for later use and should be @code{0} and @code{NULL} respectively.
4009
4010 The string returned is only valid as long as the key is valid.
4011
4012 The function returns @code{0} if an attribute can't be returned as a
4013 string, @var{key} is not a valid pointer, @var{idx} out of range,
4014 or @var{reserved} not @code{NULL}.
4015 @end deftypefun
4016
4017 @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}})
4018 The function @code{gpgme_trust_item_get_int_attr} returns the value of
4019 the number-representable attribute @var{what} of trust item
4020 @var{item}.  If the attribute occurs more than once in the trust item,
4021 the index is specified by @var{idx}.  However, currently no such
4022 attribute exists, so @var{idx} should be @code{0}.  The argument
4023 @var{reserved} is reserved for later use and should be @code{NULL}.
4024
4025 The function returns @code{0} if the attribute can't be returned as a
4026 number, @var{key} is not a valid pointer, @var{idx} out of range,
4027 or @var{reserved} not @code{NULL}.
4028 @end deftypefun
4029
4030
4031 @node Manipulating Trust Items
4032 @subsection Manipulating Trust Items
4033 @cindex trust item, manipulation
4034
4035 @deftypefun void gpgme_trust_item_ref (@w{gpgme_trust_item_t @var{item}})
4036 The function @code{gpgme_trust_item_ref} acquires an additional
4037 reference for the trust item @var{item}.
4038 @end deftypefun
4039
4040 @deftypefun void gpgme_trust_item_unref (@w{gpgme_trust_item_t @var{item}})
4041 The function @code{gpgme_trust_item_unref} releases a reference for
4042 the trust item @var{item}.  If this was the last reference, the trust
4043 item will be destroyed and all resources associated to it will be
4044 released.
4045 @end deftypefun
4046
4047
4048 The following interface is deprecated and only provided for backward
4049 compatibility.  Don't use it.  It will be removed in a future version
4050 of @acronym{GPGME}.
4051
4052 @deftypefun void gpgme_trust_item_release (@w{gpgme_trust_item_t @var{item}})
4053 The function @code{gpgme_trust_item_release} is an alias for
4054 @code{gpgme_trust_item_unref}.
4055 @end deftypefun
4056
4057
4058 @node Crypto Operations
4059 @section Crypto Operations
4060 @cindex cryptographic operation
4061
4062 Sometimes, the result of a crypto operation returns a list of invalid
4063 keys encountered in processing the request.  The following structure
4064 is used to hold information about such a key.
4065
4066 @deftp {Data type} {gpgme_invalid_key_t}
4067 This is a pointer to a structure used to store a part of the result of
4068 a crypto operation which takes user IDs as one input parameter.  The
4069 structure contains the following members:
4070
4071 @table @code
4072 @item gpgme_invalid_key_t next
4073 This is a pointer to the next invalid key structure in the linked
4074 list, or @code{NULL} if this is the last element.
4075
4076 @item char *fpr
4077 The fingerprint or key ID of the invalid key encountered.
4078
4079 @item gpgme_error_t reason
4080 An error code describing the reason why the key was found invalid.
4081 @end table
4082 @end deftp
4083
4084
4085 @menu
4086 * Decrypt::                       Decrypting a ciphertext.
4087 * Verify::                        Verifying a signature.
4088 * Decrypt and Verify::            Decrypting a signed ciphertext.
4089 * Sign::                          Creating a signature.
4090 * Encrypt::                       Encrypting a plaintext.
4091 @end menu
4092
4093
4094 @node Decrypt
4095 @subsection Decrypt
4096 @cindex decryption
4097 @cindex cryptographic operation, decryption
4098
4099 @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}})
4100 The function @code{gpgme_op_decrypt} decrypts the ciphertext in the
4101 data object @var{cipher} and stores it into the data object
4102 @var{plain}.
4103
4104 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
4105 ciphertext could be decrypted successfully, @code{GPG_ERR_INV_VALUE}
4106 if @var{ctx}, @var{cipher} or @var{plain} is not a valid pointer,
4107 @code{GPG_ERR_NO_DATA} if @var{cipher} does not contain any data to
4108 decrypt, @code{GPG_ERR_DECRYPT_FAILED} if @var{cipher} is not a valid
4109 cipher text, @code{GPG_ERR_BAD_PASSPHRASE} if the passphrase for the
4110 secret key could not be retrieved, and passes through any errors that
4111 are reported by the crypto engine support routines.
4112 @end deftypefun
4113
4114 @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}})
4115 The function @code{gpgme_op_decrypt_start} initiates a
4116 @code{gpgme_op_decrypt} operation.  It can be completed by calling
4117 @code{gpgme_wait} on the context.  @xref{Waiting For Completion}.
4118
4119 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
4120 operation could be started successfully, and @code{GPG_ERR_INV_VALUE}
4121 if @var{cipher} or @var{plain} is not a valid pointer.
4122 @end deftypefun
4123
4124 @deftp {Data type} {gpgme_recipient_t}
4125 This is a pointer to a structure used to store information about the
4126 recipient of an encrypted text which is decrypted in a
4127 @code{gpgme_op_decrypt} operation.  This information (except for the
4128 status field) is even available before the operation finished
4129 successfully, for example in a passphrase callback.  The structure
4130 contains the following members:
4131
4132 @table @code
4133 @item gpgme_recipient_t next
4134 This is a pointer to the next recipient structure in the linked list,
4135 or @code{NULL} if this is the last element.
4136
4137 @item gpgme_pubkey_algo_t
4138 The public key algorithm used in the encryption.
4139
4140 @item unsigned int wrong_key_usage : 1
4141 This is true if the key was not used according to its policy.
4142
4143 @item char *keyid
4144 This is the key ID of the key (in hexadecimal digits) used as
4145 recipient.
4146
4147 @item gpgme_error_t status
4148 This is an error number with the error code GPG_ERR_NO_SECKEY if the
4149 secret key for this recipient is not available, and 0 otherwise.
4150 @end table
4151 @end deftp
4152
4153 @deftp {Data type} {gpgme_decrypt_result_t}
4154 This is a pointer to a structure used to store the result of a
4155 @code{gpgme_op_decrypt} operation.  After successfully decrypting
4156 data, you can retrieve the pointer to the result with
4157 @code{gpgme_op_decrypt_result}.  The structure contains the following
4158 members:
4159
4160 @table @code
4161 @item char *unsupported_algorithm
4162 If an unsupported algorithm was encountered, this string describes the
4163 algorithm that is not supported.
4164
4165 @item unsigned int wrong_key_usage : 1
4166 This is true if the key was not used according to its policy.
4167
4168 @item gpgme_recipient_t recipients
4169 This is a linked list of recipients to which this message was encrypted.
4170
4171 @item char *file_name
4172 This is the filename of the original plaintext message file if it is
4173 known, otherwise this is a null pointer.
4174 @end table
4175 @end deftp
4176
4177 @deftypefun gpgme_decrypt_result_t gpgme_op_decrypt_result (@w{gpgme_ctx_t @var{ctx}})
4178 The function @code{gpgme_op_decrypt_result} returns a
4179 @code{gpgme_decrypt_result_t} pointer to a structure holding the
4180 result of a @code{gpgme_op_decrypt} operation.  The pointer is only
4181 valid if the last operation on the context was a
4182 @code{gpgme_op_decrypt} or @code{gpgme_op_decrypt_start} operation.
4183 If the operation failed this might be a @code{NULL} pointer.  The
4184 returned pointer is only valid until the next operation is started on
4185 the context.
4186 @end deftypefun
4187
4188
4189 @node Verify
4190 @subsection Verify
4191 @cindex verification
4192 @cindex signature, verification
4193 @cindex cryptographic operation, verification
4194 @cindex cryptographic operation, signature check
4195 @cindex signature notation data
4196 @cindex notation data
4197
4198 @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}})
4199 The function @code{gpgme_op_verify} verifies that the signature in the
4200 data object @var{sig} is a valid signature.  If @var{sig} is a
4201 detached signature, then the signed text should be provided in
4202 @var{signed_text} and @var{plain} should be a null pointer.
4203 Otherwise, if @var{sig} is a normal (or cleartext) signature,
4204 @var{signed_text} should be a null pointer and @var{plain} should be a
4205 writable data object that will contain the plaintext after successful
4206 verification.
4207
4208 The results of the individual signature verifications can be retrieved
4209 with @code{gpgme_op_verify_result}.
4210
4211 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
4212 operation could be completed successfully, @code{GPG_ERR_INV_VALUE} if
4213 @var{ctx}, @var{sig} or @var{plain} is not a valid pointer,
4214 @code{GPG_ERR_NO_DATA} if @var{sig} does not contain any data to
4215 verify, and passes through any errors that are reported by the crypto
4216 engine support routines.
4217 @end deftypefun
4218
4219 @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}})
4220 The function @code{gpgme_op_verify_start} initiates a
4221 @code{gpgme_op_verify} operation.  It can be completed by calling
4222 @code{gpgme_wait} on the context.  @xref{Waiting For Completion}.
4223
4224 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
4225 operation could be started successfully, @code{GPG_ERR_INV_VALUE} if
4226 @var{ctx}, @var{sig} or @var{plain} is not a valid pointer, and
4227 @code{GPG_ERR_NO_DATA} if @var{sig} or @var{plain} does not contain
4228 any data to verify.
4229 @end deftypefun
4230
4231 @deftp {Data type} {gpgme_sig_notation_t}
4232 This is a pointer to a structure used to store a part of the result of
4233 a @code{gpgme_op_verify} operation.  The structure contains the
4234 following members:
4235
4236 @table @code
4237 @item gpgme_sig_notation_t next
4238 This is a pointer to the next new signature notation structure in the
4239 linked list, or @code{NULL} if this is the last element.
4240
4241 @item char *name
4242 The name of the notation field.  If this is @code{NULL}, then the
4243 member @code{value} will contain a policy URL.
4244
4245 @item int name_len
4246 The length of the @code{name} field.  For strings the length is
4247 counted without the trailing binary zero.
4248
4249 @item char *value
4250 The value of the notation field.  If @code{name} is @code{NULL}, then
4251 this is a policy URL.
4252
4253 @item int value_len
4254 The length of the @code{value} field.  For strings the length is
4255 counted without the trailing binary zero.
4256
4257 @item gpgme_sig_notation_flags_t flags
4258 The accumulated flags field.  This field contains the flags associated
4259 with the notation data in an accumulated form which can be used as an
4260 argument to the function @code{gpgme_sig_notation_add}.  The value
4261 @code{flags} is a bitwise-or combination of one or multiple of the
4262 following bit values:
4263
4264 @table @code
4265 @item GPGME_SIG_NOTATION_HUMAN_READABLE
4266 The @code{GPGME_SIG_NOTATION_HUMAN_READABLE} symbol specifies that the
4267 notation data is in human readable form
4268
4269 @item GPGME_SIG_NOTATION_CRITICAL
4270 The @code{GPGME_SIG_NOTATION_CRITICAL} symbol specifies that the
4271 notation data is critical.
4272
4273 @end table
4274
4275 @item unsigned int human_readable : 1
4276 This is true if the @code{GPGME_SIG_NOTATION_HUMAN_READABLE} flag is
4277 set and false otherwise.  This flag is only valid for notation data,
4278 not for policy URLs.
4279
4280 @item unsigned int critical : 1
4281 This is true if the @code{GPGME_SIG_NOTATION_CRITICAL} flag is set and
4282 false otherwise.  This flag is valid for notation data and policy URLs.
4283
4284 @end table
4285 @end deftp
4286
4287 @deftp {Data type} {gpgme_signature_t}
4288 This is a pointer to a structure used to store a part of the result of
4289 a @code{gpgme_op_verify} operation.  The structure contains the
4290 following members:
4291
4292 @table @code
4293 @item gpgme_signature_t next
4294 This is a pointer to the next new signature structure in the linked
4295 list, or @code{NULL} if this is the last element.
4296
4297 @item gpgme_sigsum_t summary
4298 This is a bit vector giving a summary of the signature status.  It
4299 provides an easy interface to a defined semantic of the signature
4300 status.  Checking just one bit is sufficient to see whether a
4301 signature is valid without any restrictions.
4302
4303 The defined bits are:
4304   @table @code
4305   @item GPGME_SIGSUM_VALID
4306   The signature is fully valid.
4307
4308   @item GPGME_SIGSUM_GREEN
4309   The signature is good but one might want to display some extra
4310   information.  Check the other bits.
4311
4312   @item GPGME_SIGSUM_RED
4313   The signature is bad. It might be useful to check other bits and
4314   display more information, i.e. a revoked certificate might not render a
4315   signature invalid when the message was received prior to the cause for
4316   the revocation.
4317
4318   @item GPGME_SIGSUM_KEY_REVOKED
4319   The key or at least one certificate has been revoked.
4320
4321   @item GPGME_SIGSUM_KEY_EXPIRED
4322   The key or one of the certificates has expired. It is probably a good
4323   idea to display the date of the expiration.
4324
4325   @item GPGME_SIGSUM_SIG_EXPIRED
4326   The signature has expired.
4327
4328   @item GPGME_SIGSUM_KEY_MISSING
4329   Can't verify due to a missing key or certificate.
4330
4331   @item GPGME_SIGSUM_CRL_MISSING
4332   The CRL (or an equivalent mechanism) is not available.
4333
4334   @item GPGME_SIGSUM_CRL_TOO_OLD
4335   Available CRL is too old.
4336
4337   @item GPGME_SIGSUM_BAD_POLICY
4338   A policy requirement was not met.
4339
4340   @item GPGME_SIGSUM_SYS_ERROR
4341   A system error occured.
4342   @end table
4343
4344 @item char *fpr
4345 This is the fingerprint or key ID of the signature.
4346
4347 @item gpgme_error_t status
4348 This is the status of the signature.  In particular, the following
4349 status codes are of interest:
4350
4351   @table @code
4352   @item GPG_ERR_NO_ERROR
4353   This status indicates that the signature is valid.  For the combined
4354   result this status means that all signatures are valid.
4355
4356   @item GPG_ERR_SIG_EXPIRED
4357   This status indicates that the signature is valid but expired.  For
4358   the combined result this status means that all signatures are valid
4359   and expired.
4360
4361   @item GPG_ERR_KEY_EXPIRED
4362   This status indicates that the signature is valid but the key used to
4363   verify the signature has expired.  For the combined result this status
4364   means that all signatures are valid and all keys are expired.
4365
4366   @item GPG_ERR_CERT_REVOKED
4367   This status indicates that the signature is valid but the key used
4368   to verify the signature has been revoked.  For the combined result
4369   this status means that all signatures are valid and all keys are
4370   revoked.
4371
4372   @item GPG_ERR_BAD_SIGNATURE
4373   This status indicates that the signature is invalid.  For the combined
4374   result this status means that all signatures are invalid.
4375
4376   @item GPG_ERR_NO_PUBKEY
4377   This status indicates that the signature could not be verified due to
4378   a missing key.  For the combined result this status means that all
4379   signatures could not be checked due to missing keys.
4380
4381   @item GPG_ERR_GENERAL
4382   This status indicates that there was some other error which prevented
4383   the signature verification.
4384   @end table
4385
4386 @item gpgme_sig_notation_t notations
4387 This is a linked list with the notation data and policy URLs.
4388
4389 @item unsigned long timestamp
4390 The creation timestamp of this signature.
4391
4392 @item unsigned long exp_timestamp
4393 The expiration timestamp of this signature, or 0 if the signature does
4394 not expire.
4395
4396 @item unsigned int wrong_key_usage : 1
4397 This is true if the key was not used according to its policy.
4398
4399 @item unsigned int pka_trust : 2
4400 This is set to the trust information gained by means of the PKA system.
4401 Values are:
4402   @table @code
4403   @item 0
4404         No PKA information available or verification not possible.
4405   @item 1
4406         PKA verification failed.
4407   @item 2
4408         PKA verification succeeded.
4409   @item 3
4410         Reserved for future use.
4411   @end table
4412 Depending on the configuration of the engine, this metric may also be
4413 reflected by the validity of the signature.
4414
4415 @item unsigned int chain_model : 1
4416 This is true if the validity of the signature has been checked using the
4417 chain model.  In the chain model the time the signature has been created
4418 must be within the validity period of the certificate and the time the
4419 certificate itself has been created must be within the validity period
4420 of the issuing certificate.  In contrast the default validation model
4421 checks the validity of signature as well at the entire certificate chain
4422 at the current time.
4423
4424
4425 @item gpgme_validity_t validity
4426 The validity of the signature.
4427
4428 @item gpgme_error_t validity_reason
4429 If a signature is not valid, this provides a reason why.
4430
4431 @item gpgme_pubkey_algo_t
4432 The public key algorithm used to create this signature.
4433
4434 @item gpgme_hash_algo_t
4435 The hash algorithm used to create this signature.
4436 @end table
4437 @end deftp
4438
4439 @deftp {Data type} {gpgme_verify_result_t}
4440 This is a pointer to a structure used to store the result of a
4441 @code{gpgme_op_verify} operation.  After verifying a signature, you
4442 can retrieve the pointer to the result with
4443 @code{gpgme_op_verify_result}.  If the operation failed this might be
4444 a @code{NULL} pointer.  The structure contains the following member:
4445
4446 @table @code
4447 @item gpgme_signature_t signatures
4448 A linked list with information about all signatures for which a
4449 verification was attempted.
4450
4451 @item char *file_name
4452 This is the filename of the original plaintext message file if it is
4453 known, otherwise this is a null pointer.
4454 @end table
4455 @end deftp
4456
4457 @deftypefun gpgme_verify_result_t gpgme_op_verify_result (@w{gpgme_ctx_t @var{ctx}})
4458 The function @code{gpgme_op_verify_result} returns a
4459 @code{gpgme_verify_result_t} pointer to a structure holding the result
4460 of a @code{gpgme_op_verify} operation.  The pointer is only valid if
4461 the last operation on the context was a @code{gpgme_op_verify},
4462 @code{gpgme_op_verify_start}, @code{gpgme_op_decrypt_verify} or
4463 @code{gpgme_op_decrypt_verify_start} operation, and if this operation
4464 finished successfully (for @code{gpgme_op_decrypt_verify} and
4465 @code{gpgme_op_decrypt_verify_start}, the error code
4466 @code{GPG_ERR_NO_DATA} counts as successful in this context).  The
4467 returned pointer is only valid until the next operation is started on
4468 the context.
4469 @end deftypefun
4470
4471
4472 The following interfaces are deprecated and only provided for backward
4473 compatibility.  Don't use them.  They will be removed in a future
4474 version of @acronym{GPGME}.
4475
4476 @deftp {Data type} {enum gpgme_sig_stat_t}
4477 @tindex gpgme_sig_stat_t
4478 The @code{gpgme_sig_stat_t} type holds the result of a signature check, or
4479 the combined result of all signatures.  The following results are
4480 possible:
4481
4482 @table @code
4483 @item GPGME_SIG_STAT_NONE
4484 This status should not occur in normal operation.
4485
4486 @item GPGME_SIG_STAT_GOOD
4487 This status indicates that the signature is valid.  For the combined
4488 result this status means that all signatures are valid.
4489
4490 @item GPGME_SIG_STAT_GOOD_EXP
4491 This status indicates that the signature is valid but expired.  For
4492 the combined result this status means that all signatures are valid
4493 and expired.
4494
4495 @item GPGME_SIG_STAT_GOOD_EXPKEY
4496 This status indicates that the signature is valid but the key used to
4497 verify the signature has expired.  For the combined result this status
4498 means that all signatures are valid and all keys are expired.
4499
4500 @item GPGME_SIG_STAT_BAD
4501 This status indicates that the signature is invalid.  For the combined
4502 result this status means that all signatures are invalid.
4503
4504 @item GPGME_SIG_STAT_NOKEY
4505 This status indicates that the signature could not be verified due to
4506 a missing key.  For the combined result this status means that all
4507 signatures could not be checked due to missing keys.
4508
4509 @item GPGME_SIG_STAT_NOSIG
4510 This status indicates that the signature data provided was not a real
4511 signature.
4512
4513 @item GPGME_SIG_STAT_ERROR
4514 This status indicates that there was some other error which prevented
4515 the signature verification.
4516
4517 @item GPGME_SIG_STAT_DIFF
4518 For the combined result this status means that at least two signatures
4519 have a different status.  You can get each key's status with
4520 @code{gpgme_get_sig_status}.
4521 @end table
4522 @end deftp
4523
4524 @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}})
4525 The function @code{gpgme_get_sig_status} is equivalent to:
4526
4527 @example
4528   gpgme_verify_result_t result;
4529   gpgme_signature_t sig;
4530
4531   result = gpgme_op_verify_result (ctx);
4532   sig = result->signatures;
4533
4534   while (sig && idx)
4535     @{
4536       sig = sig->next;
4537       idx--;
4538     @}
4539   if (!sig || idx)
4540     return NULL;
4541
4542   if (r_stat)
4543     @{
4544       switch (gpg_err_code (sig->status))
4545         @{
4546         case GPG_ERR_NO_ERROR:
4547           *r_stat = GPGME_SIG_STAT_GOOD;
4548           break;
4549
4550         case GPG_ERR_BAD_SIGNATURE:
4551           *r_stat = GPGME_SIG_STAT_BAD;
4552           break;
4553
4554         case GPG_ERR_NO_PUBKEY:
4555           *r_stat = GPGME_SIG_STAT_NOKEY;
4556           break;
4557
4558         case GPG_ERR_NO_DATA:
4559           *r_stat = GPGME_SIG_STAT_NOSIG;
4560           break;
4561
4562         case GPG_ERR_SIG_EXPIRED:
4563           *r_stat = GPGME_SIG_STAT_GOOD_EXP;
4564           break;
4565
4566         case GPG_ERR_KEY_EXPIRED:
4567           *r_stat = GPGME_SIG_STAT_GOOD_EXPKEY;
4568           break;
4569
4570         default:
4571           *r_stat = GPGME_SIG_STAT_ERROR;
4572           break;
4573         @}
4574     @}
4575   if (r_created)
4576     *r_created = sig->timestamp;
4577   return sig->fpr;
4578 @end example
4579 @end deftypefun
4580
4581 @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}})
4582 The function @code{gpgme_get_sig_string_attr} is equivalent to:
4583
4584 @example
4585   gpgme_verify_result_t result;
4586   gpgme_signature_t sig;
4587
4588   result = gpgme_op_verify_result (ctx);
4589   sig = result->signatures;
4590
4591   while (sig && idx)
4592     @{
4593       sig = sig->next;
4594       idx--;
4595     @}
4596   if (!sig || idx)
4597     return NULL;
4598
4599   switch (what)
4600     @{
4601     case GPGME_ATTR_FPR:
4602       return sig->fpr;
4603
4604     case GPGME_ATTR_ERRTOK:
4605       if (whatidx == 1)
4606         return sig->wrong_key_usage ? "Wrong_Key_Usage" : "";
4607       else
4608         return "";
4609     default:
4610       break;
4611     @}
4612
4613   return NULL;
4614 @end example
4615 @end deftypefun
4616
4617 @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}})
4618 The function @code{gpgme_get_sig_ulong_attr} is equivalent to:
4619
4620 @example
4621   gpgme_verify_result_t result;
4622   gpgme_signature_t sig;
4623
4624   result = gpgme_op_verify_result (ctx);
4625   sig = result->signatures;
4626
4627   while (sig && idx)
4628     @{
4629       sig = sig->next;
4630       idx--;
4631     @}
4632   if (!sig || idx)
4633     return 0;
4634
4635   switch (what)
4636     @{
4637     case GPGME_ATTR_CREATED:
4638       return sig->timestamp;
4639
4640     case GPGME_ATTR_EXPIRE:
4641       return sig->exp_timestamp;
4642
4643     case GPGME_ATTR_VALIDITY:
4644       return (unsigned long) sig->validity;
4645
4646     case GPGME_ATTR_SIG_STATUS:
4647       switch (sig->status)
4648         @{
4649         case GPG_ERR_NO_ERROR:
4650           return GPGME_SIG_STAT_GOOD;
4651
4652         case GPG_ERR_BAD_SIGNATURE:
4653           return GPGME_SIG_STAT_BAD;
4654
4655         case GPG_ERR_NO_PUBKEY:
4656           return GPGME_SIG_STAT_NOKEY;
4657
4658         case GPG_ERR_NO_DATA:
4659           return GPGME_SIG_STAT_NOSIG;
4660
4661         case GPG_ERR_SIG_EXPIRED:
4662           return GPGME_SIG_STAT_GOOD_EXP;
4663
4664         case GPG_ERR_KEY_EXPIRED:
4665           return GPGME_SIG_STAT_GOOD_EXPKEY;
4666
4667         default:
4668           return GPGME_SIG_STAT_ERROR;
4669         @}
4670
4671     case GPGME_ATTR_SIG_SUMMARY:
4672       return sig->summary;
4673
4674     default:
4675       break;
4676     @}
4677   return 0;
4678 @end example
4679 @end deftypefun
4680
4681 @deftypefun {const char *} gpgme_get_sig_key (@w{gpgme_ctx_t @var{ctx}}, @w{int @var{idx}}, @w{gpgme_key_t *@var{r_key}})
4682 The function @code{gpgme_get_sig_key} is equivalent to:
4683
4684 @example
4685   gpgme_verify_result_t result;
4686   gpgme_signature_t sig;
4687
4688   result = gpgme_op_verify_result (ctx);
4689   sig = result->signatures;
4690
4691   while (sig && idx)
4692     @{
4693       sig = sig->next;
4694       idx--;
4695     @}
4696   if (!sig || idx)
4697     return gpg_error (GPG_ERR_EOF);
4698
4699   return gpgme_get_key (ctx, sig->fpr, r_key, 0);
4700 @end example
4701 @end deftypefun
4702
4703
4704 @node Decrypt and Verify
4705 @subsection Decrypt and Verify
4706 @cindex decryption and verification
4707 @cindex verification and decryption
4708 @cindex signature check
4709 @cindex cryptographic operation, decryption and verification
4710
4711 @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}})
4712 The function @code{gpgme_op_decrypt_verify} decrypts the ciphertext in
4713 the data object @var{cipher} and stores it into the data object
4714 @var{plain}.  If @var{cipher} contains signatures, they will be
4715 verified.
4716
4717 After the operation completed, @code{gpgme_op_decrypt_result} and
4718 @code{gpgme_op_verify_result} can be used to retrieve more information
4719 about the signatures.
4720
4721 If the error code @code{GPG_ERR_NO_DATA} is returned, @var{cipher}
4722 does not contain any data to decrypt.  However, it might still be
4723 signed.  The information about detected signatures is available with
4724 @code{gpgme_op_verify_result} in this case.
4725
4726 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
4727 ciphertext could be decrypted successfully, @code{GPG_ERR_INV_VALUE}
4728 if @var{ctx}, @var{cipher} or @var{plain} is not a valid pointer,
4729 @code{GPG_ERR_NO_DATA} if @var{cipher} does not contain any data to
4730 decrypt, @code{GPG_ERR_DECRYPT_FAILED} if @var{cipher} is not a valid
4731 cipher text, @code{GPG_ERR_BAD_PASSPHRASE} if the passphrase for the
4732 secret key could not be retrieved, and passes through any errors that
4733 are reported by the crypto engine support routines.
4734 @end deftypefun
4735
4736 @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}})
4737 The function @code{gpgme_op_decrypt_verify_start} initiates a
4738 @code{gpgme_op_decrypt_verify} operation.  It can be completed by
4739 calling @code{gpgme_wait} on the context.  @xref{Waiting For
4740 Completion}.
4741
4742 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
4743 operation could be started successfully, @code{GPG_ERR_INV_VALUE} if
4744 @var{ctx}, @var{cipher}, @var{plain} or @var{r_stat} is not a valid
4745 pointer, and @code{GPG_ERR_NO_DATA} if @var{cipher} does not contain
4746 any data to decrypt.
4747 @end deftypefun
4748
4749
4750 @node Sign
4751 @subsection Sign
4752 @cindex signature, creation
4753 @cindex sign
4754 @cindex cryptographic operation, signing
4755
4756 A signature can contain signatures by one or more keys.  The set of
4757 keys used to create a signatures is contained in a context, and is
4758 applied to all following signing operations in this context (until the
4759 set is changed).
4760
4761 @menu
4762 * Selecting Signers::             How to choose the keys to sign with.
4763 * Creating a Signature::          How to create a signature.
4764 * Signature Notation Data::       How to add notation data to a signature.
4765 @end menu
4766
4767
4768 @node Selecting Signers
4769 @subsubsection Selecting Signers
4770 @cindex signature, selecting signers
4771 @cindex signers, selecting
4772
4773 @deftypefun void gpgme_signers_clear (@w{gpgme_ctx_t @var{ctx}})
4774 The function @code{gpgme_signers_clear} releases a reference for each
4775 key on the signers list and removes the list of signers from the
4776 context @var{ctx}.
4777
4778 Every context starts with an empty list.
4779 @end deftypefun
4780
4781 @deftypefun gpgme_error_t gpgme_signers_add (@w{gpgme_ctx_t @var{ctx}}, @w{const gpgme_key_t @var{key}})
4782 The function @code{gpgme_signers_add} adds the key @var{key} to the
4783 list of signers in the context @var{ctx}.
4784
4785 Calling this function acquires an additional reference for the key.
4786 @end deftypefun
4787
4788 @deftypefun gpgme_key_t gpgme_signers_enum (@w{const gpgme_ctx_t @var{ctx}}, @w{int @var{seq}})
4789 The function @code{gpgme_signers_enum} returns the @var{seq}th key in
4790 the list of signers in the context @var{ctx}.  An additional reference
4791 is acquired for the user.
4792
4793 If @var{seq} is out of range, @code{NULL} is returned.
4794 @end deftypefun
4795
4796
4797 @node Creating a Signature
4798 @subsubsection Creating a Signature
4799
4800 @deftp {Data type} {enum gpgme_sig_mode_t}
4801 @tindex gpgme_sig_mode_t
4802 The @code{gpgme_sig_mode_t} type is used to specify the desired type of a
4803 signature.  The following modes are available:
4804
4805 @table @code
4806 @item GPGME_SIG_MODE_NORMAL
4807 A normal signature is made, the output includes the plaintext and the
4808 signature.
4809
4810 @item GPGME_SIG_MODE_DETACH
4811 A detached signature is made.
4812
4813 @item GPGME_SIG_MODE_CLEAR
4814 A clear text signature is made.  The @acronym{ASCII} armor and text
4815 mode settings of the context are ignored.
4816 @end table
4817 @end deftp
4818
4819 @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}})
4820 The function @code{gpgme_op_sign} creates a signature for the text in
4821 the data object @var{plain} and returns it in the data object
4822 @var{sig}.  The type of the signature created is determined by the
4823 @acronym{ASCII} armor (or, if that is not set, by the encoding
4824 specified for @var{sig}), the text mode attributes set for the context
4825 @var{ctx} and the requested signature mode @var{mode}.
4826
4827 After the operation completed successfully, the result can be
4828 retrieved with @code{gpgme_op_sign_result}.
4829
4830 If an S/MIME signed message is created using the CMS crypto engine,
4831 the number of certificates to include in the message can be specified
4832 with @code{gpgme_set_include_certs}.  @xref{Included Certificates}.
4833
4834 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
4835 signature could be created successfully, @code{GPG_ERR_INV_VALUE} if
4836 @var{ctx}, @var{plain} or @var{sig} is not a valid pointer,
4837 @code{GPG_ERR_NO_DATA} if the signature could not be created,
4838 @code{GPG_ERR_BAD_PASSPHRASE} if the passphrase for the secret key
4839 could not be retrieved, @code{GPG_ERR_UNUSABLE_SECKEY} if there are
4840 invalid signers, and passes through any errors that are reported by the
4841 crypto engine support routines.
4842 @end deftypefun
4843
4844 @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}})
4845 The function @code{gpgme_op_sign_start} initiates a
4846 @code{gpgme_op_sign} operation.  It can be completed by calling
4847 @code{gpgme_wait} on the context.  @xref{Waiting For Completion}.
4848
4849 The function returns the error code @code{GPG_ERR_NO_ERROR} if the operation could be
4850 started successfully, and @code{GPG_ERR_INV_VALUE} if @var{ctx},
4851 @var{plain} or @var{sig} is not a valid pointer.
4852 @end deftypefun
4853
4854 @deftp {Data type} {gpgme_new_signature_t}
4855 This is a pointer to a structure used to store a part of the result of
4856 a @code{gpgme_op_sign} operation.  The structure contains the
4857 following members:
4858
4859 @table @code
4860 @item gpgme_new_signature_t next
4861 This is a pointer to the next new signature structure in the linked
4862 list, or @code{NULL} if this is the last element.
4863
4864 @item gpgme_sig_mode_t type
4865 The type of this signature.
4866
4867 @item gpgme_pubkey_algo_t
4868 The public key algorithm used to create this signature.
4869
4870 @item gpgme_hash_algo_t
4871 The hash algorithm used to create this signature.
4872
4873 @item unsigned int sig_class
4874 The signature class of this signature.
4875
4876 @item long int timestamp
4877 The creation timestamp of this signature.
4878
4879 @item char *fpr
4880 The fingerprint of the key which was used to create this signature.
4881 @end table
4882 @end deftp
4883
4884 @deftp {Data type} {gpgme_sign_result_t}
4885 This is a pointer to a structure used to store the result of a
4886 @code{gpgme_op_sign} operation.  After successfully generating a
4887 signature, you can retrieve the pointer to the result with
4888 @code{gpgme_op_sign_result}.  The structure contains the following
4889 members:
4890
4891 @table @code
4892 @item gpgme_invalid_key_t invalid_signers
4893 A linked list with information about all invalid keys for which a
4894 signature could not be created.
4895
4896 @item gpgme_new_signature_t signatures
4897 A linked list with information about all signatures created.
4898 @end table
4899 @end deftp
4900
4901 @deftypefun gpgme_sign_result_t gpgme_op_sign_result (@w{gpgme_ctx_t @var{ctx}})
4902 The function @code{gpgme_op_sign_result} returns a
4903 @code{gpgme_sign_result_t} pointer to a structure holding the result
4904 of a @code{gpgme_op_sign} operation.  The pointer is only valid if the
4905 last operation on the context was a @code{gpgme_op_sign},
4906 @code{gpgme_op_sign_start}, @code{gpgme_op_encrypt_sign} or
4907 @code{gpgme_op_encrypt_sign_start} operation.  If that operation
4908 failed, the function might return a @code{NULL} pointer. The returned
4909 pointer is only valid until the next operation is started on the
4910 context.
4911 @end deftypefun
4912
4913
4914 @node Signature Notation Data
4915 @subsubsection Signature Notation Data
4916 @cindex notation data
4917 @cindex signature notation data
4918 @cindex policy URL
4919
4920 Using the following functions, you can attach arbitrary notation data
4921 to a signature.  This information is then available to the user when
4922 the signature is verified.
4923
4924 @deftypefun void gpgme_sig_notation_clear (@w{gpgme_ctx_t @var{ctx}})
4925 The function @code{gpgme_sig_notation_clear} removes the notation data
4926 from the context @var{ctx}.  Subsequent signing operations from this
4927 context will not include any notation data.
4928
4929 Every context starts with an empty notation data list.
4930 @end deftypefun
4931
4932 @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}})
4933 The function @code{gpgme_sig_notation_add} adds the notation data with
4934 the name @var{name} and the value @var{value} to the context
4935 @var{ctx}.
4936
4937 Subsequent signing operations will include this notation data, as well
4938 as any other notation data that was added since the creation of the
4939 context or the last @code{gpgme_sig_notation_clear} operation.
4940
4941 The arguments @var{name} and @var{value} must be @code{NUL}-terminated
4942 strings in human-readable form.  The flag
4943 @code{GPGME_SIG_NOTATION_HUMAN_READABLE} is implied
4944 (non-human-readable notation data is currently not supported).  The
4945 strings must be in UTF-8 encoding.
4946
4947 If @var{name} is @code{NULL}, then @var{value} should be a policy URL.
4948
4949 The function @code{gpgme_sig_notation_add} returns the error code
4950 @code{GPG_ERR_NO_ERROR} if the notation data could be added
4951 successfully, @code{GPG_ERR_INV_VALUE} if @var{ctx} is not a valid
4952 pointer, or if @var{name}, @var{value} and @var{flags} are an invalid
4953 combination.  The function also passes through any errors that are
4954 reported by the crypto engine support routines.
4955 @end deftypefun
4956
4957 @deftypefun gpgme_sig_notation_t gpgme_sig_notation_get (@w{const gpgme_ctx_t @var{ctx}})
4958 The function @code{gpgme_sig_notation_get} returns the linked list of
4959 notation data structures that are contained in the context @var{ctx}.
4960
4961 If @var{ctx} is not a valid pointer, or there is no notation data
4962 added for this context, @code{NULL} is returned.
4963 @end deftypefun
4964
4965
4966 @node Encrypt
4967 @subsection Encrypt
4968 @cindex encryption
4969 @cindex cryptographic operation, encryption
4970
4971 One plaintext can be encrypted for several recipients at the same
4972 time.  The list of recipients is created independently of any context,
4973 and then passed to the encryption operation.
4974
4975 @menu
4976 * Encrypting a Plaintext::        How to encrypt a plaintext.
4977 @end menu
4978
4979
4980 @node Encrypting a Plaintext
4981 @subsubsection Encrypting a Plaintext
4982
4983 @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}})
4984 The function @code{gpgme_op_encrypt} encrypts the plaintext in the
4985 data object @var{plain} for the recipients @var{recp} and stores the
4986 ciphertext in the data object @var{cipher}.  The type of the
4987 ciphertext created is determined by the @acronym{ASCII} armor (or, if
4988 that is not set, by the encoding specified for @var{cipher}) and the
4989 text mode attributes set for the context @var{ctx}.
4990
4991 @var{key} must be a @code{NULL}-terminated array of keys.  The user
4992 must keep references for all keys during the whole duration of the
4993 call (but see @code{gpgme_op_encrypt_start} for the requirements with
4994 the asynchronous variant).
4995
4996 The value in @var{flags} is a bitwise-or combination of one or
4997 multiple of the following bit values:
4998
4999 @table @code
5000 @item GPGME_ENCRYPT_ALWAYS_TRUST
5001 The @code{GPGME_ENCRYPT_ALWAYS_TRUST} symbol specifies that all the
5002 recipients in @var{recp} should be trusted, even if the keys do not
5003 have a high enough validity in the keyring.  This flag should be used
5004 with care; in general it is not a good idea to use any untrusted keys.
5005
5006 @item GPGME_ENCRYPT_NO_ENCRYPT_TO
5007 The @code{GPGME_ENCRYPT_NO_ENCRYPT_TO} symbol specifies that no
5008 default or hidden default recipients as configured in the crypto
5009 backend should be included.  This can be useful for managing different
5010 user profiles.
5011 @end table
5012
5013 If @code{GPG_ERR_UNUSABLE_PUBKEY} is returned, some recipients in
5014 @var{recp} are invalid, but not all.  In this case the plaintext might
5015 be encrypted for all valid recipients and returned in @var{cipher} (if
5016 this happens depends on the crypto engine).  More information about
5017 the invalid recipients is available with
5018 @code{gpgme_op_encrypt_result}.
5019
5020 If @var{recp} is @code{NULL}, symmetric rather than public key
5021 encryption is performed.  Symmetrically encrypted cipher text can be
5022 deciphered with @code{gpgme_op_decrypt}.  Note that in this case the
5023 crypto backend needs to retrieve a passphrase from the user.
5024 Symmetric encryption is currently only supported for the OpenPGP
5025 crypto backend.
5026
5027 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
5028 ciphertext could be created successfully, @code{GPG_ERR_INV_VALUE} if
5029 @var{ctx}, @var{recp}, @var{plain} or @var{cipher} is not a valid
5030 pointer, @code{GPG_ERR_UNUSABLE_PUBKEY} if @var{recp} contains some
5031 invalid recipients, @code{GPG_ERR_BAD_PASSPHRASE} if the passphrase
5032 for the symmetric key could not be retrieved, and passes through any
5033 errors that are reported by the crypto engine support routines.
5034 @end deftypefun
5035
5036 @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}})
5037 The function @code{gpgme_op_encrypt_start} initiates a
5038 @code{gpgme_op_encrypt} operation.  It can be completed by calling
5039 @code{gpgme_wait} on the context.  @xref{Waiting For Completion}.
5040
5041 References to the keys only need to be held for the duration of this
5042 call.  The user can release its references to the keys after this
5043 function returns, even if the operation is not yet finished.
5044
5045 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
5046 operation could be started successfully, @code{GPG_ERR_INV_VALUE} if
5047 @var{ctx}, @var{rset}, @var{plain} or @var{cipher} is not a valid
5048 pointer, and @code{GPG_ERR_UNUSABLE_PUBKEY} if @var{rset} does not
5049 contain any valid recipients.
5050 @end deftypefun
5051
5052 @deftp {Data type} {gpgme_encrypt_result_t}
5053 This is a pointer to a structure used to store the result of a
5054 @code{gpgme_op_encrypt} operation.  After successfully encrypting
5055 data, you can retrieve the pointer to the result with
5056 @code{gpgme_op_encrypt_result}.  The structure contains the following
5057 members:
5058
5059 @table @code
5060 @item gpgme_invalid_key_t invalid_recipients
5061 A linked list with information about all invalid keys for which
5062 the data could not be encrypted.
5063 @end table
5064 @end deftp
5065
5066 @deftypefun gpgme_encrypt_result_t gpgme_op_encrypt_result (@w{gpgme_ctx_t @var{ctx}})
5067 The function @code{gpgme_op_encrypt_result} returns a
5068 @code{gpgme_encrypt_result_t} pointer to a structure holding the
5069 result of a @code{gpgme_op_encrypt} operation.  The pointer is only
5070 valid if the last operation on the context was a
5071 @code{gpgme_op_encrypt}, @code{gpgme_op_encrypt_start},
5072 @code{gpgme_op_sign} or @code{gpgme_op_sign_start} operation.  If this
5073 operation failed, this might be a @code{NULL} pointer.  The returned
5074 pointer is only valid until the next operation is started on the
5075 context.
5076 @end deftypefun
5077
5078
5079 @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}})
5080 The function @code{gpgme_op_encrypt_sign} does a combined encrypt and
5081 sign operation.  It is used like @code{gpgme_op_encrypt}, but the
5082 ciphertext also contains signatures for the signers listed in
5083 @var{ctx}.
5084
5085 The combined encrypt and sign operation is currently only available
5086 for the OpenPGP crypto engine.
5087 @end deftypefun
5088
5089 @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}})
5090 The function @code{gpgme_op_encrypt_sign_start} initiates a
5091 @code{gpgme_op_encrypt_sign} operation.  It can be completed by
5092 calling @code{gpgme_wait} on the context.  @xref{Waiting For
5093 Completion}.
5094
5095 The function returns the error code @code{GPG_ERR_NO_ERROR} if the
5096 operation could be started successfully, and @code{GPG_ERR_INV_VALUE}
5097 if @var{ctx}, @var{rset}, @var{plain} or @var{cipher} is not a valid
5098 pointer.
5099 @end deftypefun
5100
5101
5102 @node Run Control
5103 @section Run Control
5104 @cindex run control
5105 @cindex cryptographic operation, running
5106
5107 @acronym{GPGME} supports running operations synchronously and
5108 asynchronously.  You can use asynchronous operation to set up a
5109 context up to initiating the desired operation, but delay performing
5110 it to a later point.
5111
5112 Furthermore, you can use an external event loop to control exactly
5113 when @acronym{GPGME} runs.  This ensures that @acronym{GPGME} only
5114 runs when necessary and also prevents it from blocking for a long
5115 time.
5116
5117 @menu
5118 * Waiting For Completion::        Waiting until an operation is completed.
5119 * Using External Event Loops::    Advanced control over what happens when.
5120 * Cancellation::                  How to end pending operations prematurely.
5121 @end menu
5122
5123
5124 @node Waiting For Completion
5125 @subsection Waiting For Completion
5126 @cindex cryptographic operation, wait for
5127 @cindex wait for completion
5128
5129 @deftypefun gpgme_ctx_t gpgme_wait (@w{gpgme_ctx_t @var{ctx}}, @w{gpgme_error_t *@var{status}}, @w{int @var{hang}})
5130 The function @code{gpgme_wait} continues the pending operation within
5131 the context @var{ctx}.  In particular, it ensures the data exchange
5132 between @acronym{GPGME} and the crypto backend and watches over the
5133 run time status of the backend process.
5134
5135 If @var{hang} is true, the function does not return until the
5136 operation is completed or cancelled.  Otherwise the function will not
5137 block for a long time.
5138
5139 The error status of the finished operation is returned in @var{status}
5140 if @code{gpgme_wait} does not return @code{NULL}.
5141
5142 The @var{ctx} argument can be @code{NULL}.  In that case,
5143 @code{gpgme_wait} waits for any context to complete its operation.
5144
5145 @code{gpgme_wait} can be used only in conjunction with any context
5146 that has a pending operation initiated with one of the
5147 @code{gpgme_op_*_start} functions except @code{gpgme_op_keylist_start}
5148 and @code{gpgme_op_trustlist_start} (for which you should use the
5149 corresponding @code{gpgme_op_*_next} functions).  If @var{ctx} is
5150 @code{NULL}, all of such contexts are waited upon and possibly
5151 returned.  Synchronous operations running in parallel, as well as key
5152 and trust item list operations, do not affect @code{gpgme_wait}.
5153
5154 In a multi-threaded environment, only one thread should ever call
5155 @code{gpgme_wait} at any time, irregardless if @var{ctx} is specified
5156 or not.  This means that all calls to this function should be fully
5157 synchronized by locking primitives.  It is safe to start asynchronous
5158 operations while a thread is running in @code{gpgme_wait}.
5159
5160 The function returns the @var{ctx} of the context which has finished
5161 the operation.  If @var{hang} is false, and the timeout expires,
5162 @code{NULL} is returned and @code{*status} will be set to 0.  If an
5163 error occurs, @code{NULL} is returned and the error is returned in
5164 @code{*status}.
5165 @end deftypefun
5166
5167
5168 @node Using External Event Loops
5169 @subsection Using External Event Loops
5170 @cindex event loop, external
5171
5172 @acronym{GPGME} hides the complexity of the communication between the
5173 library and the crypto engine.  The price of this convenience is that
5174 the calling thread can block arbitrary long waiting for the data
5175 returned by the crypto engine.  In single-threaded programs, in
5176 particular if they are interactive, this is an unwanted side-effect.
5177 OTOH, if @code{gpgme_wait} is used without the @var{hang} option being
5178 enabled, it might be called unnecessarily often, wasting CPU time that
5179 could be used otherwise.
5180
5181 The I/O callback interface described in this section lets the user
5182 take control over what happens when.  @acronym{GPGME} will provide the
5183 user with the file descriptors that should be monitored, and the
5184 callback functions that should be invoked when a file descriptor is
5185 ready for reading or writing.  It is then the user's responsibility to
5186 decide when to check the file descriptors and when to invoke the
5187 callback functions.  Usually this is done in an event loop, that also
5188 checks for events in other parts of the program.  If the callback
5189 functions are only called when the file descriptors are ready,
5190 @acronym{GPGME} will never block.  This gives the user more control
5191 over the program flow, and allows to perform other tasks when
5192 @acronym{GPGME} would block otherwise.
5193
5194 By using this advanced mechanism, @acronym{GPGME} can be integrated
5195 smoothly into GUI toolkits like GTK+ even for single-threaded
5196 programs.
5197
5198 @menu
5199 * I/O Callback Interface::        How I/O callbacks are registered.
5200 * Registering I/O Callbacks::     How to use I/O callbacks for a context.
5201 * I/O Callback Example::          An example how to use I/O callbacks.
5202 * I/O Callback Example GTK+::     How to use @acronym{GPGME} with GTK+.
5203 * I/O Callback Example GDK::      How to use @acronym{GPGME} with GDK.
5204 * I/O Callback Example Qt::       How to use @acronym{GPGME} with Qt.
5205 @end menu
5206
5207
5208 @node I/O Callback Interface
5209 @subsubsection I/O Callback Interface
5210
5211 @deftp {Data type} {gpgme_error_t (*gpgme_io_cb_t) (@w{void *@var{data}}, @w{int @var{fd}})}
5212 @tindex gpgme_io_cb_t
5213 The @code{gpgme_io_cb_t} type is the type of functions which
5214 @acronym{GPGME} wants to register as I/O callback handlers using the
5215 @code{gpgme_register_io_cb_t} functions provided by the user.
5216
5217 @var{data} and @var{fd} are provided by @acronym{GPGME} when the I/O
5218 callback handler is registered, and should be passed through to the
5219 handler when it is invoked by the user because it noticed activity on
5220 the file descriptor @var{fd}.
5221
5222 The callback handler always returns @code{0}, but you should consider
5223 the return value to be reserved for later use.
5224 @end deftp
5225
5226 @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}})}
5227 @tindex gpgme_register_io_cb_t
5228 The @code{gpgme_register_io_cb_t} type is the type of functions which can
5229 be called by @acronym{GPGME} to register an I/O callback function
5230 @var{fnc} for the file descriptor @var{fd} with the user.
5231 @var{fnc_data} should be passed as the first argument to @var{fnc}
5232 when the handler is invoked (the second argument should be @var{fd}).
5233 If @var{dir} is 0, @var{fnc} should be called by the user when
5234 @var{fd} is ready for writing.  If @var{dir} is 1, @var{fnc} should be
5235 called when @var{fd} is ready for reading.
5236
5237 @var{data} was provided by the user when registering the
5238 @code{gpgme_register_io_cb_t} function with @acronym{GPGME} and will always
5239 be passed as the first argument when registering a callback function.
5240 For example, the user can use this to determine the event loop to
5241 which the file descriptor should be added.
5242
5243 @acronym{GPGME} will call this function when a crypto operation is
5244 initiated in a context for which the user has registered I/O callback
5245 handler functions with @code{gpgme_set_io_cbs}.  It can also call this
5246 function when it is in an I/O callback handler for a file descriptor
5247 associated to this context.
5248
5249 The user should return a unique handle in @var{tag} identifying this
5250 I/O callback registration, which will be passed to the
5251 @code{gpgme_register_io_cb_t} function without interpretation when the file
5252 descriptor should not be monitored anymore.
5253 @end deftp
5254
5255 @deftp {Data type} {void (*gpgme_remove_io_cb_t) (@w{void *@var{tag}})}
5256 The @code{gpgme_remove_io_cb_t} type is the type of functions which can be
5257 called by @acronym{GPGME} to remove an I/O callback handler that was
5258 registered before.  @var{tag} is the handle that was returned by the
5259 @code{gpgme_register_io_cb_t} for this I/O callback.
5260
5261 @acronym{GPGME} can call this function when a crypto operation is in
5262 an I/O callback.  It will also call this function when the context is
5263 destroyed while an operation is pending.
5264 @end deftp
5265
5266 @deftp {Data type} {enum gpgme_event_io_t}
5267 @tindex gpgme_event_io_t
5268 The @code{gpgme_event_io_t} type specifies the type of an event that is
5269 reported to the user by @acronym{GPGME} as a consequence of an I/O
5270 operation.  The following events are defined:
5271
5272 @table @code
5273 @item GPGME_EVENT_START
5274 The operation is fully initialized now, and you can start to run the
5275 registered I/O callback handlers now.  Note that registered I/O
5276 callback handlers must not be run before this event is signalled.
5277 @var{type_data} is @code{NULL} and reserved for later use.
5278
5279 @item GPGME_EVENT_DONE
5280 The operation is finished, the last I/O callback for this operation
5281 was removed.  The accompanying @var{type_data} points to a
5282 @code{gpgme_error_t} variable that contains the status of the operation
5283 that finished.  This event is signalled after the last I/O callback
5284 has been removed.
5285
5286 @item GPGME_EVENT_NEXT_KEY
5287 In a @code{gpgme_op_keylist_start} operation, the next key was
5288 received from the crypto engine.  The accompanying @var{type_data} is
5289 a @code{gpgme_key_t} variable that contains the key with one reference
5290 for the user.
5291
5292 @item GPGME_EVENT_NEXT_TRUSTITEM
5293 In a @code{gpgme_op_trustlist_start} operation, the next trust item
5294 was received from the crypto engine.  The accompanying @var{type_data}
5295 is a @code{gpgme_trust_item_t} variable that contains the trust item with
5296 one reference for the user.
5297 @end table
5298 @end deftp
5299
5300 @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}})}
5301 The @code{gpgme_event_io_cb_t} type is the type of functions which can be
5302 called by @acronym{GPGME} to signal an event for an operation running
5303 in a context which has I/O callback functions registered by the user.
5304
5305 @var{data} was provided by the user when registering the
5306 @code{gpgme_event_io_cb_t} function with @acronym{GPGME} and will always be
5307 passed as the first argument when registering a callback function.
5308 For example, the user can use this to determine the context in which
5309 this event has occured.
5310
5311 @var{type} will specify the type of event that has occured.
5312 @var{type_data} specifies the event further, as described in the above
5313 list of possible @code{gpgme_event_io_t} types.
5314
5315 @acronym{GPGME} can call this function in an I/O callback handler.
5316 @end deftp
5317
5318
5319 @node Registering I/O Callbacks
5320 @subsubsection Registering I/O Callbacks
5321
5322 @deftp {Data type} {struct gpgme_io_cb_ts}
5323 @tindex gpgme_event_io_t
5324 This structure is used to store the I/O callback interface functions
5325 described in the previous section.  It has the following members:
5326
5327 @table @code
5328 @item gpgme_register_io_cb_t add
5329 This is the function called by @acronym{GPGME} to register an I/O
5330 callback handler.  It must be specified.
5331
5332 @item void *add_data
5333 This is passed as the first argument to the @code{add} function when
5334 it is called by @acronym{GPGME}.  For example, it can be used to
5335 determine the event loop to which the file descriptor should be added.
5336
5337 @item gpgme_remove_io_cb_t remove
5338 This is the function called by @acronym{GPGME} to remove an I/O
5339 callback handler.  It must be specified.
5340
5341 @item gpgme_event_io_cb_t event
5342 This is the function called by @acronym{GPGME} to signal an event for
5343 an operation.  It must be specified, because at least the start event
5344 must be processed.
5345
5346 @item void *event_data
5347 This is passed as the first argument to the @code{event} function when
5348 it is called by @acronym{GPGME}.  For example, it can be used to
5349 determine the context in which the event has occured.
5350 @end table
5351 @end deftp
5352
5353 @deftypefun void gpgme_set_io_cbs (@w{gpgme_ctx_t @var{ctx}}, @w{struct gpgme_io_cb_ts *@var{io_cbs}})
5354 The function @code{gpgme_set_io_cbs} enables the I/O callback
5355 interface for the context @var{ctx}.  The I/O callback functions are
5356 specified by @var{io_cbs}.
5357
5358 If @var{io_cbs}->@code{add} is @code{NULL}, the I/O callback interface
5359 is disabled for the context, and normal operation is restored.
5360 @end deftypefun
5361
5362 @deftypefun void gpgme_get_io_cbs (@w{gpgme_ctx_t @var{ctx}}, @w{struct gpgme_io_cb_ts *@var{io_cbs}})
5363 The function @code{gpgme_get_io_cbs} returns the I/O callback
5364 functions set with @code{gpgme_set_io_cbs} in @var{io_cbs}.
5365 @end deftypefun
5366
5367
5368 @node I/O Callback Example
5369 @subsubsection I/O Callback Example
5370
5371 To actually use an external event loop, you have to implement the I/O
5372 callback functions that are used by @acronym{GPGME} to register and
5373 unregister file descriptors.  Furthermore, you have to actually
5374 monitor these file descriptors for activity and call the appropriate
5375 I/O callbacks.
5376
5377 The following example illustrates how to do that.  The example uses
5378 locking to show in which way the callbacks and the event loop can
5379 run concurrently.  For the event loop, we use a fixed array.  For a
5380 real-world implementation, you should use a dynamically sized
5381 structure because the number of file descriptors needed for a crypto
5382 operation in @acronym{GPGME} is not predictable.
5383
5384 @example
5385 #include <assert.h>
5386 #include <errno.h>
5387 #include <stdlib.h>
5388 #include <pthread.h>
5389 #include <sys/types.h>
5390 #include <gpgme.h>
5391
5392 /* The following structure holds the result of a crypto operation.  */
5393 struct op_result
5394 @{
5395   int done;
5396   gpgme_error_t err;
5397 @};
5398
5399 /* The following structure holds the data associated with one I/O
5400 callback.  */
5401 struct one_fd
5402 @{
5403   int fd;
5404   int dir;
5405   gpgme_io_cb_t fnc;
5406   void *fnc_data;
5407   void *loop;
5408 @};
5409
5410 struct event_loop
5411 @{
5412   pthread_mutex_t lock;
5413 #define MAX_FDS 32
5414   /* Unused slots are marked with FD being -1.  */
5415   struct one_fd fds[MAX_FDS];
5416 @};
5417 @end example
5418
5419 The following functions implement the I/O callback interface.
5420
5421 @example
5422 gpgme_error_t
5423 add_io_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc, void *fnc_data,
5424            void **r_tag)
5425 @{
5426   struct event_loop *loop = data;
5427   struct one_fd *fds = loop->fds;
5428   int i;
5429
5430   pthread_mutex_lock (&loop->lock);
5431   for (i = 0; i < MAX_FDS; i++)
5432     @{
5433       if (fds[i].fd == -1)
5434         @{
5435           fds[i].fd = fd;
5436           fds[i].dir = dir;
5437           fds[i].fnc = fnc;
5438           fds[i].fnc_data = fnc_data;
5439           fds[i].loop = loop;
5440           break;
5441         @}
5442     @}
5443   pthread_mutex_unlock (&loop->lock);
5444   if (i == MAX_FDS)
5445     return gpg_error (GPG_ERR_GENERAL);
5446   *r_tag = &fds[i];
5447   return 0;
5448 @}
5449
5450 void
5451 remove_io_cb (void *tag)
5452 @{
5453   struct one_fd *fd = tag;
5454   struct event_loop *loop = fd->loop;
5455
5456   pthread_mutex_lock (&loop->lock);
5457   fd->fd = -1;
5458   pthread_mutex_unlock (&loop->lock);
5459 @}
5460
5461 void
5462 event_io_cb (void *data, gpgme_event_io_t type, void *type_data)
5463 @{
5464   struct op_result *result = data;
5465
5466   /* We don't support list operations here.  */
5467   if (type == GPGME_EVENT_DONE)
5468     @{
5469       result->done = 1;
5470       result->err = *type_data;
5471     @}
5472 @}
5473 @end example
5474
5475 The final missing piece is the event loop, which will be presented
5476 next.  We only support waiting for the success of a single operation.
5477
5478 @example
5479 int
5480 do_select (struct event_loop *loop)
5481 @{
5482   fd_set rfds;
5483   fd_set wfds;
5484   int i, n;
5485   int any = 0;
5486   struct one_fd *fdlist = loop->fds;
5487
5488   pthread_mutex_lock (&loop->lock);
5489   FD_ZERO (&rfds);
5490   FD_ZERO (&wfds);
5491   for (i = 0; i < MAX_FDS; i++)
5492     if (fdlist[i].fd != -1)
5493       FD_SET (fdlist[i].fd, fdlist[i].dir ? &rfds : &wfds);
5494   pthread_mutex_unlock (&loop->unlock);
5495
5496   do
5497     @{
5498       n = select (FD_SETSIZE, &rfds, &wfds, NULL, 0);
5499     @}
5500   while (n < 0 && errno == EINTR);
5501
5502   if (n < 0)
5503     return n;   /* Error or timeout.  */
5504
5505   pthread_mutex_lock (&loop->lock);
5506   for (i = 0; i < MAX_FDS && n; i++)
5507     @{
5508       if (fdlist[i].fd != -1)
5509         @{
5510           if (FD_ISSET (fdlist[i].fd, fdlist[i].dir ? &rfds : &wfds))
5511             @{
5512               assert (n);
5513               n--;
5514               any = 1;
5515               /* The I/O callback handler can register/remove callbacks,
5516                  so we have to unlock the file descriptor list.  */
5517               pthread_mutex_unlock (&loop->lock);
5518               (*fdlist[i].fnc) (fdlist[i].fnc_data, fdlist[i].fd);
5519               pthread_mutex_lock (&loop->lock);
5520             @}
5521         @}
5522     @}
5523   pthread_mutex_unlock (&loop->lock);
5524   return any;
5525 @}
5526
5527 void
5528 wait_for_op (struct event_loop *loop, struct op_result *result)
5529 @{
5530   int ret;
5531
5532   do
5533     @{
5534       ret = do_select (loop);
5535     @}
5536   while (ret >= 0 && !result->done);
5537 @}
5538 @end example
5539
5540 The main function shows how to put it all together.
5541
5542 @example
5543 int
5544 main (int argc, char *argv[])
5545 @{
5546   struct event_loop loop;
5547   struct op_result result;
5548   gpgme_ctx_t ctx;
5549   gpgme_error_t err;
5550   gpgme_data_t sig, text;
5551   int i;
5552   struct gpgme_io_cb_ts io_cbs =
5553   @{
5554     add_io_cb,
5555     &loop,
5556     remove_io_cb,
5557     event_io_cb,
5558     &result
5559   @};
5560
5561   init_gpgme (void);
5562
5563   /* Initialize the loop structure.  */
5564   pthread_mutex_init (&loop.lock, NULL);
5565   for (i = 0; i < MAX_FDS; i++)
5566     loop->fds[i].fd = -1;
5567
5568   /* Initialize the result structure.  */
5569   result.done = 0;
5570
5571   err = gpgme_data_new_from_file (&sig, "signature", 1);
5572   if (!err)
5573     err = gpgme_data_new_from_file (&text, "text", 1);
5574   if (!err)
5575     err = gpgme_new (&ctx);
5576   if (!err)
5577     @{
5578        gpgme_set_io_cbs (ctx, &io_cbs);
5579        err = gpgme_op_verify_start (ctx, sig, text, NULL);
5580     @}
5581   if (err)
5582     @{
5583       fprintf (stderr, "gpgme error: %s: %s\n",
5584                gpgme_strsource (err), gpgme_strerror (err));
5585       exit (1);
5586     @}
5587
5588   wait_for_op (&loop, &result);
5589   if (!result.done)
5590     @{
5591       fprintf (stderr, "select error\n");
5592       exit (1);
5593     @}
5594   if (!result.err)
5595     @{
5596       fprintf (stderr, "verification failed: %s: %s\n",
5597                gpgme_strsource (result.err), gpgme_strerror (result.err));
5598       exit (1);
5599     @}
5600   /* Evaluate verify result.  */
5601   @dots{}
5602   return 0;
5603 @}
5604 @end example
5605
5606
5607 @node I/O Callback Example GTK+
5608 @subsubsection I/O Callback Example GTK+
5609 @cindex GTK+, using @acronym{GPGME} with
5610
5611 The I/O callback interface can be used to integrate @acronym{GPGME}
5612 with the GTK+ event loop.  The following code snippets shows how this
5613 can be done using the appropriate register and remove I/O callback
5614 functions.  In this example, the private data of the register I/O
5615 callback function is unused.  The event notifications is missing
5616 because it does not require any GTK+ specific setup.
5617
5618 @example
5619 #include <gtk/gtk.h>
5620
5621 struct my_gpgme_io_cb
5622 @{
5623   gpgme_io_cb_t fnc;
5624   void *fnc_data;
5625   guint input_handler_id
5626 @};
5627
5628 void
5629 my_gpgme_io_cb (gpointer data, gint source, GdkInputCondition condition)
5630 @{
5631   struct my_gpgme_io_cb *iocb = data;
5632   (*(iocb->fnc)) (iocb->data, source);
5633 @}
5634
5635 void
5636 my_gpgme_remove_io_cb (void *data)
5637 @{
5638   struct my_gpgme_io_cb *iocb = data;
5639   gtk_input_remove (data->input_handler_id);
5640 @}
5641
5642 void
5643 my_gpgme_register_io_callback (void *data, int fd, int dir, gpgme_io_cb_t fnc,
5644                                void *fnc_data, void **tag)
5645 @{
5646   struct my_gpgme_io_cb *iocb = g_malloc (sizeof (struct my_gpgme_io_cb));
5647   iocb->fnc = fnc;
5648   iocb->data = fnc_data;
5649   iocb->input_handler_id = gtk_input_add_full (fd, dir
5650                                                    ? GDK_INPUT_READ
5651                                                    : GDK_INPUT_WRITE,
5652                                                my_gpgme_io_callback,
5653                                                0, iocb, NULL);
5654   *tag = iocb;
5655   return 0;
5656 @}
5657 @end example
5658
5659
5660 @node I/O Callback Example GDK
5661 @subsubsection I/O Callback Example GDK
5662 @cindex GDK, using @acronym{GPGME} with
5663
5664 The I/O callback interface can also be used to integrate
5665 @acronym{GPGME} with the GDK event loop.  The following code snippets
5666 shows how this can be done using the appropriate register and remove
5667 I/O callback functions.  In this example, the private data of the
5668 register I/O callback function is unused.  The event notifications is
5669 missing because it does not require any GDK specific setup.
5670
5671 It is very similar to the GTK+ example in the previous section.
5672
5673 @example
5674 #include <gdk/gdk.h>
5675
5676 struct my_gpgme_io_cb
5677 @{
5678   gpgme_io_cb_t fnc;
5679   void *fnc_data;
5680   gint tag;
5681 @};
5682
5683 void
5684 my_gpgme_io_cb (gpointer data, gint source, GdkInputCondition condition)
5685 @{
5686   struct my_gpgme_io_cb *iocb = data;
5687   (*(iocb->fnc)) (iocb->data, source);
5688 @}
5689
5690 void
5691 my_gpgme_remove_io_cb (void *data)
5692 @{
5693   struct my_gpgme_io_cb *iocb = data;
5694   gdk_input_remove (data->tag);
5695 @}
5696
5697 void
5698 my_gpgme_register_io_callback (void *data, int fd, int dir, gpgme_io_cb_t fnc,
5699                                void *fnc_data, void **tag)
5700 @{
5701   struct my_gpgme_io_cb *iocb = g_malloc (sizeof (struct my_gpgme_io_cb));
5702   iocb->fnc = fnc;
5703   iocb->data = fnc_data;
5704   iocb->tag = gtk_input_add_full (fd, dir ? GDK_INPUT_READ : GDK_INPUT_WRITE,
5705                                   my_gpgme_io_callback, iocb, NULL);
5706   *tag = iocb;
5707   return 0;
5708 @}
5709 @end example
5710
5711
5712 @node I/O Callback Example Qt
5713 @subsubsection I/O Callback Example Qt
5714 @cindex Qt, using @acronym{GPGME} with
5715
5716 The I/O callback interface can also be used to integrate
5717 @acronym{GPGME} with the Qt event loop.  The following code snippets
5718 show how this can be done using the appropriate register and remove
5719 I/O callback functions.  In this example, the private data of the
5720 register I/O callback function is unused.  The event notifications is
5721 missing because it does not require any Qt specific setup.
5722
5723 @example
5724 #include <qsocketnotifier.h>
5725 #include <qapplication.h>
5726
5727 struct IOCB @{
5728   IOCB( GpgmeIOCb f, void * d, QSocketNotifier * n )
5729     : func( f ), data( d ), notifier( n ) @{@}
5730   GpgmeIOCb func;
5731   void * data;
5732   QSocketNotifier * notifier;
5733 @}
5734
5735 class MyApp : public QApplication @{
5736
5737   // ...
5738
5739   static void registerGpgmeIOCallback( void * data, int fd, int dir,
5740                                        GpgmeIOCb func, void * func_data,
5741                                        void ** tag ) @{
5742     QSocketNotifier * n =
5743       new QSocketNotifier( fd, dir ? QSocketNotifier::Read
5744                                    : QSocketNotifier::Write );
5745     connect( n, SIGNAL(activated(int)),
5746              qApp, SLOT(slotGpgmeIOCallback(int)) );
5747     qApp->mIOCBs.push_back( IOCB( func, func_data, n ) );
5748     *tag = (void*)n;
5749   @}
5750
5751   static void removeGpgmeIOCallback( void * tag ) @{
5752     if ( !tag ) return;
5753     QSocketNotifier * n = static_cast<QSocketNotifier*>( tag );
5754     for ( QValueList<IOCB>::iterator it = qApp->mIOCBs.begin() ;
5755           it != qApp->mIOCBs.end() ; ++it )
5756       if ( it->notifier == n ) @{
5757         delete it->notifier;
5758         qApp->mIOCBs.erase( it );
5759         return;
5760       @}
5761   @}
5762
5763 public slots:
5764   void slotGpgmeIOCallback( int fd ) @{
5765     for ( QValueList<IOCB>::const_iterator it = mIOCBs.begin() ;
5766           it != mIOCBs.end() ; ++it )
5767       if ( it->notifier && it->notifier->socket() == fd )
5768         (*(it->func)) ( it->func_data, fd );
5769   @}
5770
5771   // ...
5772
5773 private:
5774   QValueList<IOCB> mIOCBs;
5775   // ...
5776 @};
5777 @end example
5778
5779
5780 @node Cancellation
5781 @subsection Cancellation
5782 @cindex cryptographic operation, aborting
5783 @cindex cryptographic operation, cancelling
5784 @cindex aborting operations
5785 @cindex cancelling operations
5786
5787 Sometimes you do not want to wait for an operation to finish.
5788 @acronym{GPGME} provides two different functions to achieve that.  The
5789 function @code{gpgme_cancel} takes effect immediately.  When it
5790 returns, the operation is effectively canceled.  However, it has some
5791 limitations and can not be used with synchronous operations.  In
5792 contrast, the function @code{gpgme_cancel_async} can be used with any
5793 context and from any thread, but it is not guaranteed to take effect
5794 immediately.  Instead, cancellation occurs at the next possible time
5795 (typically the next time I/O occurs in the target context).
5796
5797 @deftypefun gpgme_ctx_t gpgme_cancel (@w{gpgme_ctx_t @var{ctx}})
5798 The function @code{gpgme_cancel} attempts to cancel a pending
5799 operation in the context @var{ctx}.  This only works if you use the
5800 global event loop or your own event loop.
5801
5802 If you use the global event loop, you must not call @code{gpgme_wait}
5803 or @code{gpgme_wait} during cancellation.  After successful
5804 cancellation, you can call @code{gpgme_wait} (optionally waiting on
5805 @var{ctx}), and the context @var{ctx} will appear as if it had
5806 finished with the error code @code{GPG_ERR_CANCEL}.
5807
5808 If you use your an external event loop, you must ensure that no I/O
5809 callbacks are invoked for this context (for example by halting the
5810 event loop).  On successful cancellation, all registered I/O callbacks
5811 for this context will be unregistered, and a @code{GPGME_EVENT_DONE}
5812 event with the error code @code{GPG_ERR_CANCEL} will be signaled.
5813
5814 The function returns an error code if the cancellation failed (in this
5815 case the state of @var{ctx} is not modified).
5816 @end deftypefun
5817
5818
5819 @deftypefun gpgme_ctx_t gpgme_cancel_async (@w{gpgme_ctx_t @var{ctx}})
5820 The function @code{gpgme_cancel} attempts to cancel a pending
5821 operation in the context @var{ctx}.  This can be called by any thread
5822 at any time after starting an operation on the context, but will not
5823 take effect immediately.  The actual cancellation happens at the next
5824 time GPGME processes I/O in that context.
5825
5826 The function returns an error code if the cancellation failed (in this
5827 case the state of @var{ctx} is not modified).
5828 @end deftypefun
5829
5830 @c **********************************************************
5831 @c *******************  Appendices  *************************
5832 @c **********************************************************
5833
5834 @include uiserver.texi
5835
5836 @node Debugging
5837 @appendix How to solve problems
5838 @cindex debug
5839 @cindex GPGME_DEBUG
5840
5841 Everyone knows that software often does not do what it should do and thus
5842 there is a need to track down problems.  This is in particular true
5843 for applications using a complex library like @acronym{GPGME} and of
5844 course also for the library itself.  Here we give a few hints on how
5845 to solve such problems.
5846
5847 First of all you should make sure that the keys you want to use are
5848 installed in the GnuPG engine and are usable.  Thus the first test is
5849 to run the desired operation using @command{gpg} or @command{gpgsm} on
5850 the command line.  If you can't figure out why things don't work, you
5851 may use @acronym{GPGME}'s built in trace feature.  This feature is
5852 either enabled using the environment variable @code{GPGME_DEBUG} or,
5853 if this is not possible, by calling the function
5854 @code{gpgme_set_global_flag}.  The value is the trace level and
5855 an optional file name.
5856
5857 @noindent
5858 For example
5859 @smallexample
5860 GPGME_DEBUG=9:/home/user/mygpgme.log
5861 @end smallexample
5862 @noindent
5863 (Note that under Windows you use a semicolon in place of the colon to
5864 separate the fields.)
5865
5866 A trace level of 9 is pretty verbose and thus you may want to start
5867 off with a lower level.  The exact definition of the trace levels and
5868 the output format may change with any release; you need to check the
5869 source code for details.  In any case the trace log should be helpful
5870 to understand what is going going on.  Warning: The trace log may
5871 reveal sensitive details like passphrases or other data you use in
5872 your application.  If you are asked to send a log file, make sure that
5873 you run your tests only with play data.
5874
5875
5876
5877 @include lesser.texi
5878
5879 @include gpl.texi
5880
5881 @node Function and Data Index
5882 @unnumbered Function and Data Index
5883
5884 @printindex fn
5885
5886 @node Concept Index
5887 @unnumbered Concept Index
5888
5889 @printindex cp
5890
5891
5892 @bye