1 /* agent.h - Global definitions for the agent
2 * Copyright (C) 2001, 2002, 2003, 2005, 2011 Free Software Foundation, Inc.
3 * Copyright (C) 2015 g10 Code GmbH.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #ifdef GPG_ERR_SOURCE_DEFAULT
25 #error GPG_ERR_SOURCE_DEFAULT already defined
27 #define GPG_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_GPGAGENT
28 #include <gpg-error.h>
29 #define map_assuan_err(a) \
30 map_assuan_err_with_source (GPG_ERR_SOURCE_DEFAULT, (a))
34 #include "../common/util.h"
35 #include "../common/membuf.h"
36 #include "../common/sysutils.h" /* (gnupg_fd_t) */
37 #include "../common/session-env.h"
38 #include "../common/shareddefs.h"
40 /* To convey some special hash algorithms we use algorithm numbers
41 reserved for application use. */
42 #ifndef GCRY_MODULE_ID_USER
43 #define GCRY_MODULE_ID_USER 1024
45 #define MD_USER_TLS_MD5SHA1 (GCRY_MODULE_ID_USER+1)
47 /* Maximum length of a digest. */
48 #define MAX_DIGEST_LEN 64
50 /* The maximum length of a passphrase (in bytes). Note: this is
51 further contrained by the Assuan line length (and any other text on
52 the same line). However, the Assuan line length is 1k bytes so
53 this shouldn't be a problem in practice. */
54 #define MAX_PASSPHRASE_LEN 255
57 /* A large struct name "opt" to keep global flags */
60 unsigned int debug; /* Debug flags (DBG_foo_VALUE) */
61 int verbose; /* Verbosity level */
62 int quiet; /* Be as quiet as possible */
63 int dry_run; /* Don't change any persistent data */
64 int batch; /* Batch mode */
65 const char *homedir; /* Configuration directory name */
67 /* True if we handle sigusr2. */
70 /* Environment settings gathered at program start or changed using the
71 Assuan command UPDATESTARTUPTTY. */
72 session_env_t startup_env;
73 char *startup_lc_ctype;
74 char *startup_lc_messages;
76 /* Enable pinentry debugging (--debug 1024 should also be used). */
79 /* Filename of the program to start as pinentry. */
80 const char *pinentry_program;
82 /* Filename of the program to handle smartcard tasks. */
83 const char *scdaemon_program;
85 int disable_scdaemon; /* Never use the SCdaemon. */
87 int no_grab; /* Don't let the pinentry grab the keyboard */
89 /* The name of the file pinentry shall touch before exiting. If
90 this is not set the file name of the standard socket is used. */
91 const char *pinentry_touch_file;
93 /* A string where the first character is used by the pinentry as a
94 custom invisible character. */
95 char *pinentry_invisible_char;
97 /* The timeout value for the Pinentry in seconds. This is passed to
98 the pinentry if it is not 0. It is up to the pinentry to act
99 upon this timeout value. */
100 unsigned long pinentry_timeout;
102 /* The default and maximum TTL of cache entries. */
103 unsigned long def_cache_ttl; /* Default. */
104 unsigned long def_cache_ttl_ssh; /* for SSH. */
105 unsigned long max_cache_ttl; /* Default. */
106 unsigned long max_cache_ttl_ssh; /* for SSH. */
108 /* Flag disallowing bypassing of the warning. */
109 int enforce_passphrase_constraints;
111 /* The require minmum length of a passphrase. */
112 unsigned int min_passphrase_len;
114 /* The minimum number of non-alpha characters in a passphrase. */
115 unsigned int min_passphrase_nonalpha;
117 /* File name with a patternfile or NULL if not enabled. */
118 const char *check_passphrase_pattern;
120 /* If not 0 the user is asked to change his passphrase after these
122 unsigned int max_passphrase_days;
124 /* If set, a passphrase history will be written and checked at each
125 passphrase change. */
126 int enable_passhrase_history;
128 int running_detached; /* We are running detached from the tty. */
130 /* If this global option is true, the passphrase cache is ignored
131 for signing operations. */
132 int ignore_cache_for_signing;
134 /* If this global option is true, the user is allowed to
135 interactively mark certificate in trustlist.txt as trusted. */
136 int allow_mark_trusted;
138 /* If this global option is true, the Assuan command
139 PRESET_PASSPHRASE is allowed. */
140 int allow_preset_passphrase;
142 /* If this global option is true, the Assuan option
143 pinentry-mode=loopback is allowed. */
144 int allow_loopback_pinentry;
146 /* Allow the use of an external password cache. If this option is
147 enabled (which is the default) we send an option to Pinentry
148 to allow it to enable such a cache. */
149 int allow_external_cache;
151 /* If this global option is true, the Assuan option of Pinentry
152 allow-emacs-prompt is allowed. */
153 int allow_emacs_pinentry;
155 int keep_tty; /* Don't switch the TTY (for pinentry) on request */
156 int keep_display; /* Don't switch the DISPLAY (for pinentry) on request */
158 /* This global options indicates the use of an extra socket. Note
159 that we use a hack for cleanup handling in gpg-agent.c: If the
160 value is less than 2 the name has not yet been malloced. */
163 /* This global options indicates the use of an extra socket for web
164 browsers. Note that we use a hack for cleanup handling in
165 gpg-agent.c: If the value is less than 2 the name has not yet
171 /* Bit values for the --debug option. */
172 #define DBG_COMMAND_VALUE 1 /* debug commands i/o */
173 #define DBG_MPI_VALUE 2 /* debug mpi details */
174 #define DBG_CRYPTO_VALUE 4 /* debug low level crypto */
175 #define DBG_MEMORY_VALUE 32 /* debug memory allocation stuff */
176 #define DBG_CACHE_VALUE 64 /* debug the caching */
177 #define DBG_MEMSTAT_VALUE 128 /* show memory statistics */
178 #define DBG_HASHING_VALUE 512 /* debug hashing operations */
179 #define DBG_IPC_VALUE 1024 /* Enable Assuan debugging. */
181 /* Test macros for the debug option. */
182 #define DBG_COMMAND (opt.debug & DBG_COMMAND_VALUE)
183 #define DBG_CRYPTO (opt.debug & DBG_CRYPTO_VALUE)
184 #define DBG_MEMORY (opt.debug & DBG_MEMORY_VALUE)
185 #define DBG_CACHE (opt.debug & DBG_CACHE_VALUE)
186 #define DBG_HASHING (opt.debug & DBG_HASHING_VALUE)
187 #define DBG_IPC (opt.debug & DBG_IPC_VALUE)
189 /* Forward reference for local definitions in command.c. */
190 struct server_local_s;
192 /* Declaration of objects from command-ssh.c. */
193 struct ssh_control_file_s;
194 typedef struct ssh_control_file_s *ssh_control_file_t;
196 /* Forward reference for local definitions in call-scd.c. */
199 /* Collection of data per session (aka connection). */
200 struct server_control_s
202 /* Private data used to fire up the connection thread. We use this
203 structure do avoid an extra allocation for only a few bytes while
204 spawning a new connection thread. */
209 /* Flag indicating the connection is run in restricted mode.
210 A value of 1 if used for --extra-socket,
211 a value of 2 is used for --browser-socket. */
214 /* Private data of the server (command.c). */
215 struct server_local_s *server_local;
217 /* Private data of the SCdaemon (call-scd.c). */
218 struct scd_local_s *scd_local;
220 /* Environment settings for the connection. */
221 session_env_t session_env;
225 /* The current pinentry mode. */
226 pinentry_mode_t pinentry_mode;
228 /* The TTL used for the --preset option of certain commands. */
229 int cache_ttl_opt_preset;
231 /* Information on the currently used digest (for signing commands). */
234 unsigned char value[MAX_DIGEST_LEN];
238 unsigned char keygrip[20];
241 /* A flag to enable a hack to send the PKAUTH command instead of the
242 PKSIGN command to the scdaemon. */
245 /* A flag to inhibit enforced passphrase change during an explicit
249 /* The current S2K which might be different from the calibrated
251 unsigned long s2k_count;
255 /* Information pertaining to pinentry requests. */
256 struct pin_entry_info_s
258 int min_digits; /* min. number of digits required or 0 for freeform entry */
259 int max_digits; /* max. number of allowed digits allowed*/
260 int max_tries; /* max. number of allowed tries. */
261 int failed_tries; /* Number of tries so far failed. */
262 int with_qualitybar; /* Set if the quality bar should be displayed. */
263 int with_repeat; /* Request repetition of the passphrase. */
264 int repeat_okay; /* Repetition worked. */
265 gpg_error_t (*check_cb)(struct pin_entry_info_s *); /* CB used to check
267 void *check_cb_arg; /* optional argument which might be of use in the CB */
268 const char *cb_errtext; /* used by the cb to display a specific error */
269 size_t max_length; /* Allocated length of the buffer PIN. */
270 char pin[1]; /* The buffer to hold the PIN or passphrase.
271 It's actual allocated length is given by
272 MAX_LENGTH (above). */
276 /* Types of the private keys. */
279 PRIVATE_KEY_UNKNOWN = 0, /* Type of key is not known. */
280 PRIVATE_KEY_CLEAR = 1, /* The key is not protected. */
281 PRIVATE_KEY_PROTECTED = 2, /* The key is protected. */
282 PRIVATE_KEY_SHADOWED = 3, /* The key is a stub for a smartcard
284 PROTECTED_SHARED_SECRET = 4, /* RFU. */
285 PRIVATE_KEY_OPENPGP_NONE = 5 /* openpgp-native with protection "none". */
289 /* Values for the cache_mode arguments. */
292 CACHE_MODE_IGNORE = 0, /* Special mode to bypass the cache. */
293 CACHE_MODE_ANY, /* Any mode except ignore matches. */
294 CACHE_MODE_NORMAL, /* Normal cache (gpg-agent). */
295 CACHE_MODE_USER, /* GET_PASSPHRASE related cache. */
296 CACHE_MODE_SSH, /* SSH related cache. */
297 CACHE_MODE_NONCE /* This is a non-predictable nonce. */
301 /* The TTL is seconds used for adding a new nonce mode cache item. */
302 #define CACHE_TTL_NONCE 120
304 /* The TTL in seconds used by the --preset option of some commands.
305 This is the default value changeable by an OPTION command. */
306 #define CACHE_TTL_OPT_PRESET 900
309 /* The type of a function to lookup a TTL by a keygrip. */
310 typedef int (*lookup_ttl_t)(const char *hexgrip);
313 /* This is a special version of the usual _() gettext macro. It
314 assumes a server connection control variable with the name "ctrl"
315 and uses that to translate a string according to the locale set for
316 the connection. The macro LunderscoreIMPL is used by i18n to
317 actually define the inline function when needed. */
319 #define L_(a) agent_Lunderscore (ctrl, (a))
320 #define LunderscorePROTO \
321 static inline const char *agent_Lunderscore (ctrl_t ctrl, \
322 const char *string) \
323 GNUPG_GCC_ATTR_FORMAT_ARG(2);
324 #define LunderscoreIMPL \
325 static inline const char * \
326 agent_Lunderscore (ctrl_t ctrl, const char *string) \
328 return ctrl? i18n_localegettext (ctrl->lc_messages, string) \
329 /* */: gettext (string); \
336 /*-- gpg-agent.c --*/
337 void agent_exit (int rc)
338 GPGRT_ATTR_NORETURN; /* Also implemented in other tools */
339 void agent_set_progress_cb (void (*cb)(ctrl_t ctrl, const char *what,
340 int printchar, int current, int total),
342 gpg_error_t agent_copy_startup_env (ctrl_t ctrl);
343 const char *get_agent_socket_name (void);
344 const char *get_agent_ssh_socket_name (void);
345 #ifdef HAVE_W32_SYSTEM
346 void *get_agent_scd_notify_event (void);
348 void agent_sighup_action (void);
349 int map_pk_openpgp_to_gcry (int openpgp_algo);
352 gpg_error_t agent_inq_pinentry_launched (ctrl_t ctrl, unsigned long pid);
353 gpg_error_t agent_write_status (ctrl_t ctrl, const char *keyword, ...)
354 GPGRT_ATTR_SENTINEL(0);
355 gpg_error_t agent_print_status (ctrl_t ctrl, const char *keyword,
356 const char *format, ...)
357 GPGRT_ATTR_PRINTF(3,4);
358 void bump_key_eventcounter (void);
359 void bump_card_eventcounter (void);
360 void start_command_handler (ctrl_t, gnupg_fd_t, gnupg_fd_t);
361 gpg_error_t pinentry_loopback (ctrl_t, const char *keyword,
362 unsigned char **buffer, size_t *size,
365 #ifdef HAVE_W32_SYSTEM
366 int serve_mmapped_ssh_request (ctrl_t ctrl,
367 unsigned char *request, size_t maxreqlen);
368 #endif /*HAVE_W32_SYSTEM*/
370 /*-- command-ssh.c --*/
371 ssh_control_file_t ssh_open_control_file (void);
372 void ssh_close_control_file (ssh_control_file_t cf);
373 gpg_error_t ssh_read_control_file (ssh_control_file_t cf,
374 char *r_hexgrip, int *r_disabled,
375 int *r_ttl, int *r_confirm);
376 gpg_error_t ssh_search_control_file (ssh_control_file_t cf,
379 int *r_ttl, int *r_confirm);
381 void start_command_handler_ssh (ctrl_t, gnupg_fd_t);
384 int agent_write_private_key (const unsigned char *grip,
385 const void *buffer, size_t length, int force);
386 gpg_error_t agent_key_from_file (ctrl_t ctrl,
387 const char *cache_nonce,
388 const char *desc_text,
389 const unsigned char *grip,
390 unsigned char **shadow_info,
391 cache_mode_t cache_mode,
392 lookup_ttl_t lookup_ttl,
394 char **r_passphrase);
395 gpg_error_t agent_raw_key_from_file (ctrl_t ctrl, const unsigned char *grip,
396 gcry_sexp_t *result);
397 gpg_error_t agent_public_key_from_file (ctrl_t ctrl,
398 const unsigned char *grip,
399 gcry_sexp_t *result);
400 int agent_is_dsa_key (gcry_sexp_t s_key);
401 int agent_is_eddsa_key (gcry_sexp_t s_key);
402 int agent_key_available (const unsigned char *grip);
403 gpg_error_t agent_key_info_from_file (ctrl_t ctrl, const unsigned char *grip,
405 unsigned char **r_shadow_info);
406 gpg_error_t agent_delete_key (ctrl_t ctrl, const char *desc_text,
407 const unsigned char *grip, int force);
409 /*-- call-pinentry.c --*/
410 void initialize_module_call_pinentry (void);
411 void agent_query_dump_state (void);
412 void agent_reset_query (ctrl_t ctrl);
413 int pinentry_active_p (ctrl_t ctrl, int waitseconds);
414 gpg_error_t agent_askpin (ctrl_t ctrl,
415 const char *desc_text, const char *prompt_text,
416 const char *inital_errtext,
417 struct pin_entry_info_s *pininfo,
418 const char *keyinfo, cache_mode_t cache_mode);
419 int agent_get_passphrase (ctrl_t ctrl, char **retpass,
420 const char *desc, const char *prompt,
421 const char *errtext, int with_qualitybar,
422 const char *keyinfo, cache_mode_t cache_mode);
423 int agent_get_confirmation (ctrl_t ctrl, const char *desc, const char *ok,
424 const char *notokay, int with_cancel);
425 int agent_show_message (ctrl_t ctrl, const char *desc, const char *ok_btn);
426 int agent_popup_message_start (ctrl_t ctrl,
427 const char *desc, const char *ok_btn);
428 void agent_popup_message_stop (ctrl_t ctrl);
429 int agent_clear_passphrase (ctrl_t ctrl,
430 const char *keyinfo, cache_mode_t cache_mode);
433 void initialize_module_cache (void);
434 void deinitialize_module_cache (void);
435 void agent_flush_cache (void);
436 int agent_put_cache (const char *key, cache_mode_t cache_mode,
437 const char *data, int ttl);
438 char *agent_get_cache (const char *key, cache_mode_t cache_mode);
439 void agent_store_cache_hit (const char *key);
443 int agent_pksign_do (ctrl_t ctrl, const char *cache_nonce,
444 const char *desc_text,
445 gcry_sexp_t *signature_sexp,
446 cache_mode_t cache_mode, lookup_ttl_t lookup_ttl,
447 const void *overridedata, size_t overridedatalen);
448 int agent_pksign (ctrl_t ctrl, const char *cache_nonce,
449 const char *desc_text,
450 membuf_t *outbuf, cache_mode_t cache_mode);
452 /*-- pkdecrypt.c --*/
453 int agent_pkdecrypt (ctrl_t ctrl, const char *desc_text,
454 const unsigned char *ciphertext, size_t ciphertextlen,
455 membuf_t *outbuf, int *r_padding);
458 int check_passphrase_constraints (ctrl_t ctrl, const char *pw,
459 char **failed_constraint);
460 gpg_error_t agent_ask_new_passphrase (ctrl_t ctrl, const char *prompt,
461 char **r_passphrase);
462 int agent_genkey (ctrl_t ctrl, const char *cache_nonce,
463 const char *keyparam, size_t keyparmlen,
464 int no_protection, const char *override_passphrase,
465 int preset, membuf_t *outbuf);
466 gpg_error_t agent_protect_and_store (ctrl_t ctrl, gcry_sexp_t s_skey,
467 char **passphrase_addr);
470 unsigned long get_standard_s2k_count (void);
471 unsigned char get_standard_s2k_count_rfc4880 (void);
472 int agent_protect (const unsigned char *plainkey, const char *passphrase,
473 unsigned char **result, size_t *resultlen,
474 unsigned long s2k_count);
475 int agent_unprotect (ctrl_t ctrl,
476 const unsigned char *protectedkey, const char *passphrase,
477 gnupg_isotime_t protected_at,
478 unsigned char **result, size_t *resultlen);
479 int agent_private_key_type (const unsigned char *privatekey);
480 unsigned char *make_shadow_info (const char *serialno, const char *idstring);
481 int agent_shadow_key (const unsigned char *pubkey,
482 const unsigned char *shadow_info,
483 unsigned char **result);
484 int agent_get_shadow_info (const unsigned char *shadowkey,
485 unsigned char const **shadow_info);
486 gpg_error_t parse_shadow_info (const unsigned char *shadow_info,
487 char **r_hexsn, char **r_idstr, int *r_pinlen);
488 gpg_error_t s2k_hash_passphrase (const char *passphrase, int hashalgo,
490 const unsigned char *s2ksalt,
491 unsigned int s2kcount,
492 unsigned char *key, size_t keylen);
495 /*-- trustlist.c --*/
496 void initialize_module_trustlist (void);
497 gpg_error_t agent_istrusted (ctrl_t ctrl, const char *fpr, int *r_disabled);
498 gpg_error_t agent_listtrusted (void *assuan_context);
499 gpg_error_t agent_marktrusted (ctrl_t ctrl, const char *name,
500 const char *fpr, int flag);
501 void agent_reload_trustlist (void);
504 /*-- divert-scd.c --*/
505 int divert_pksign (ctrl_t ctrl,
506 const unsigned char *digest, size_t digestlen, int algo,
507 const unsigned char *shadow_info, unsigned char **r_sig,
509 int divert_pkdecrypt (ctrl_t ctrl,
510 const unsigned char *cipher,
511 const unsigned char *shadow_info,
512 char **r_buf, size_t *r_len, int *r_padding);
513 int divert_generic_cmd (ctrl_t ctrl,
514 const char *cmdline, void *assuan_context);
515 int divert_writekey (ctrl_t ctrl, int force, const char *serialno,
516 const char *id, const char *keydata, size_t keydatalen);
520 void initialize_module_call_scd (void);
521 void agent_scd_dump_state (void);
522 int agent_scd_check_running (void);
523 void agent_scd_check_aliveness (void);
524 int agent_reset_scd (ctrl_t ctrl);
525 int agent_card_learn (ctrl_t ctrl,
526 void (*kpinfo_cb)(void*, const char *),
528 void (*certinfo_cb)(void*, const char *),
529 void *certinfo_cb_arg,
530 void (*sinfo_cb)(void*, const char *,
531 size_t, const char *),
533 int agent_card_serialno (ctrl_t ctrl, char **r_serialno);
534 int agent_card_pksign (ctrl_t ctrl,
536 int (*getpin_cb)(void *, const char *, char*, size_t),
539 const unsigned char *indata, size_t indatalen,
540 unsigned char **r_buf, size_t *r_buflen);
541 int agent_card_pkdecrypt (ctrl_t ctrl,
543 int (*getpin_cb)(void *, const char *, char*,size_t),
545 const unsigned char *indata, size_t indatalen,
546 char **r_buf, size_t *r_buflen, int *r_padding);
547 int agent_card_readcert (ctrl_t ctrl,
548 const char *id, char **r_buf, size_t *r_buflen);
549 int agent_card_readkey (ctrl_t ctrl, const char *id, unsigned char **r_buf);
550 int agent_card_writekey (ctrl_t ctrl, int force, const char *serialno,
551 const char *id, const char *keydata,
553 int (*getpin_cb)(void *, const char *, char*, size_t),
554 void *getpin_cb_arg);
555 gpg_error_t agent_card_getattr (ctrl_t ctrl, const char *name, char **result);
556 int agent_card_scd (ctrl_t ctrl, const char *cmdline,
557 int (*getpin_cb)(void *, const char *, char*, size_t),
558 void *getpin_cb_arg, void *assuan_context);
561 /*-- learncard.c --*/
562 int agent_handle_learn (ctrl_t ctrl, int send, void *assuan_context, int force);
565 /*-- cvt-openpgp.c --*/
567 extract_private_key (gcry_sexp_t s_key, int req_private_key_data,
568 const char **r_algoname, int *r_npkey, int *r_nskey,
569 const char **r_format,
570 gcry_mpi_t *mpi_array, int arraysize,
571 gcry_sexp_t *r_curve, gcry_sexp_t *r_flags);