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 tocuh before exiting. If
90 this is not set the filoe name of the standard socket is used. */
91 const char *pinentry_touch_file;
93 /* The default and maximum TTL of cache entries. */
94 unsigned long def_cache_ttl; /* Default. */
95 unsigned long def_cache_ttl_ssh; /* for SSH. */
96 unsigned long max_cache_ttl; /* Default. */
97 unsigned long max_cache_ttl_ssh; /* for SSH. */
99 /* Flag disallowing bypassing of the warning. */
100 int enforce_passphrase_constraints;
102 /* The require minmum length of a passphrase. */
103 unsigned int min_passphrase_len;
105 /* The minimum number of non-alpha characters in a passphrase. */
106 unsigned int min_passphrase_nonalpha;
108 /* File name with a patternfile or NULL if not enabled. */
109 const char *check_passphrase_pattern;
111 /* If not 0 the user is asked to change his passphrase after these
113 unsigned int max_passphrase_days;
115 /* If set, a passphrase history will be written and checked at each
116 passphrase change. */
117 int enable_passhrase_history;
119 int running_detached; /* We are running detached from the tty. */
121 /* If this global option is true, the passphrase cache is ignored
122 for signing operations. */
123 int ignore_cache_for_signing;
125 /* If this global option is true, the user is allowed to
126 interactively mark certificate in trustlist.txt as trusted. */
127 int allow_mark_trusted;
129 /* If this global option is true, the Assuan command
130 PRESET_PASSPHRASE is allowed. */
131 int allow_preset_passphrase;
133 /* If this global option is true, the Assuan option
134 pinentry-mode=loopback is allowed. */
135 int allow_loopback_pinentry;
137 /* Allow the use of an external password cache. If this option is
138 enabled (which is the default) we send an option to Pinentry
139 to allow it to enable such a cache. */
140 int allow_external_cache;
142 /* If this global option is true, the Assuan option of Pinentry
143 allow-emacs-prompt is allowed. */
144 int allow_emacs_pinentry;
146 int keep_tty; /* Don't switch the TTY (for pinentry) on request */
147 int keep_display; /* Don't switch the DISPLAY (for pinentry) on request */
149 /* This global options indicates the use of an extra socket. Note
150 that we use a hack for cleanup handling in gpg-agent.c: If the
151 value is less than 2 the name has not yet been malloced. */
154 /* This global options indicates the use of an extra socket for web
155 browsers. Note that we use a hack for cleanup handling in
156 gpg-agent.c: If the value is less than 2 the name has not yet
162 /* Bit values for the --debug option. */
163 #define DBG_COMMAND_VALUE 1 /* debug commands i/o */
164 #define DBG_MPI_VALUE 2 /* debug mpi details */
165 #define DBG_CRYPTO_VALUE 4 /* debug low level crypto */
166 #define DBG_MEMORY_VALUE 32 /* debug memory allocation stuff */
167 #define DBG_CACHE_VALUE 64 /* debug the caching */
168 #define DBG_MEMSTAT_VALUE 128 /* show memory statistics */
169 #define DBG_HASHING_VALUE 512 /* debug hashing operations */
170 #define DBG_IPC_VALUE 1024 /* Enable Assuan debugging. */
172 /* Test macros for the debug option. */
173 #define DBG_COMMAND (opt.debug & DBG_COMMAND_VALUE)
174 #define DBG_CRYPTO (opt.debug & DBG_CRYPTO_VALUE)
175 #define DBG_MEMORY (opt.debug & DBG_MEMORY_VALUE)
176 #define DBG_CACHE (opt.debug & DBG_CACHE_VALUE)
177 #define DBG_HASHING (opt.debug & DBG_HASHING_VALUE)
178 #define DBG_IPC (opt.debug & DBG_IPC_VALUE)
180 /* Forward reference for local definitions in command.c. */
181 struct server_local_s;
183 /* Declaration of objects from command-ssh.c. */
184 struct ssh_control_file_s;
185 typedef struct ssh_control_file_s *ssh_control_file_t;
187 /* Forward reference for local definitions in call-scd.c. */
190 /* Collection of data per session (aka connection). */
191 struct server_control_s
193 /* Private data used to fire up the connection thread. We use this
194 structure do avoid an extra allocation for only a few bytes while
195 spawning a new connection thread. */
200 /* Flag indicating the connection is run in restricted mode.
201 A value of 1 if used for --extra-socket,
202 a value of 2 is used for --browser-socket. */
205 /* Private data of the server (command.c). */
206 struct server_local_s *server_local;
208 /* Private data of the SCdaemon (call-scd.c). */
209 struct scd_local_s *scd_local;
211 /* Environment settings for the connection. */
212 session_env_t session_env;
216 /* The current pinentry mode. */
217 pinentry_mode_t pinentry_mode;
219 /* The TTL used for the --preset option of certain commands. */
220 int cache_ttl_opt_preset;
222 /* Information on the currently used digest (for signing commands). */
225 unsigned char value[MAX_DIGEST_LEN];
229 unsigned char keygrip[20];
232 /* A flag to enable a hack to send the PKAUTH command instead of the
233 PKSIGN command to the scdaemon. */
236 /* A flag to inhibit enforced passphrase change during an explicit
240 /* The current S2K which might be different from the calibrated
242 unsigned long s2k_count;
246 /* Information pertaining to pinentry requests. */
247 struct pin_entry_info_s
249 int min_digits; /* min. number of digits required or 0 for freeform entry */
250 int max_digits; /* max. number of allowed digits allowed*/
251 int max_tries; /* max. number of allowed tries. */
252 int failed_tries; /* Number of tries so far failed. */
253 int with_qualitybar; /* Set if the quality bar should be displayed. */
254 int with_repeat; /* Request repetition of the passphrase. */
255 int repeat_okay; /* Repetition worked. */
256 int (*check_cb)(struct pin_entry_info_s *); /* CB used to check the PIN */
257 void *check_cb_arg; /* optional argument which might be of use in the CB */
258 const char *cb_errtext; /* used by the cb to display a specific error */
259 size_t max_length; /* Allocated length of the buffer PIN. */
260 char pin[1]; /* The buffer to hold the PIN or passphrase.
261 It's actual allocated length is given by
262 MAX_LENGTH (above). */
266 /* Types of the private keys. */
269 PRIVATE_KEY_UNKNOWN = 0, /* Type of key is not known. */
270 PRIVATE_KEY_CLEAR = 1, /* The key is not protected. */
271 PRIVATE_KEY_PROTECTED = 2, /* The key is protected. */
272 PRIVATE_KEY_SHADOWED = 3, /* The key is a stub for a smartcard
274 PROTECTED_SHARED_SECRET = 4, /* RFU. */
275 PRIVATE_KEY_OPENPGP_NONE = 5 /* openpgp-native with protection "none". */
279 /* Values for the cache_mode arguments. */
282 CACHE_MODE_IGNORE = 0, /* Special mode to bypass the cache. */
283 CACHE_MODE_ANY, /* Any mode except ignore matches. */
284 CACHE_MODE_NORMAL, /* Normal cache (gpg-agent). */
285 CACHE_MODE_USER, /* GET_PASSPHRASE related cache. */
286 CACHE_MODE_SSH, /* SSH related cache. */
287 CACHE_MODE_NONCE /* This is a non-predictable nonce. */
291 /* The TTL is seconds used for adding a new nonce mode cache item. */
292 #define CACHE_TTL_NONCE 120
294 /* The TTL in seconds used by the --preset option of some commands.
295 This is the default value changeable by an OPTION command. */
296 #define CACHE_TTL_OPT_PRESET 900
299 /* The type of a function to lookup a TTL by a keygrip. */
300 typedef int (*lookup_ttl_t)(const char *hexgrip);
303 /* This is a special version of the usual _() gettext macro. It
304 assumes a server connection control variable with the name "ctrl"
305 and uses that to translate a string according to the locale set for
306 the connection. The macro LunderscoreIMPL is used by i18n to
307 actually define the inline function when needed. */
309 #define L_(a) agent_Lunderscore (ctrl, (a))
310 #define LunderscorePROTO \
311 static inline const char *agent_Lunderscore (ctrl_t ctrl, \
312 const char *string) \
313 GNUPG_GCC_ATTR_FORMAT_ARG(2);
314 #define LunderscoreIMPL \
315 static inline const char * \
316 agent_Lunderscore (ctrl_t ctrl, const char *string) \
318 return ctrl? i18n_localegettext (ctrl->lc_messages, string) \
319 /* */: gettext (string); \
326 /*-- gpg-agent.c --*/
327 void agent_exit (int rc)
328 GPGRT_ATTR_NORETURN; /* Also implemented in other tools */
329 gpg_error_t agent_copy_startup_env (ctrl_t ctrl);
330 const char *get_agent_socket_name (void);
331 const char *get_agent_ssh_socket_name (void);
332 #ifdef HAVE_W32_SYSTEM
333 void *get_agent_scd_notify_event (void);
335 void agent_sighup_action (void);
336 int map_pk_openpgp_to_gcry (int openpgp_algo);
339 gpg_error_t agent_inq_pinentry_launched (ctrl_t ctrl, unsigned long pid);
340 gpg_error_t agent_write_status (ctrl_t ctrl, const char *keyword, ...)
341 GPGRT_ATTR_SENTINEL(0);
342 gpg_error_t agent_print_status (ctrl_t ctrl, const char *keyword,
343 const char *format, ...)
344 GPGRT_ATTR_PRINTF(3,4);
345 void bump_key_eventcounter (void);
346 void bump_card_eventcounter (void);
347 void start_command_handler (ctrl_t, gnupg_fd_t, gnupg_fd_t);
348 gpg_error_t pinentry_loopback (ctrl_t, const char *keyword,
349 unsigned char **buffer, size_t *size,
352 #ifdef HAVE_W32_SYSTEM
353 int serve_mmapped_ssh_request (ctrl_t ctrl,
354 unsigned char *request, size_t maxreqlen);
355 #endif /*HAVE_W32_SYSTEM*/
357 /*-- command-ssh.c --*/
358 ssh_control_file_t ssh_open_control_file (void);
359 void ssh_close_control_file (ssh_control_file_t cf);
360 gpg_error_t ssh_read_control_file (ssh_control_file_t cf,
361 char *r_hexgrip, int *r_disabled,
362 int *r_ttl, int *r_confirm);
363 gpg_error_t ssh_search_control_file (ssh_control_file_t cf,
366 int *r_ttl, int *r_confirm);
368 void start_command_handler_ssh (ctrl_t, gnupg_fd_t);
371 int agent_write_private_key (const unsigned char *grip,
372 const void *buffer, size_t length, int force);
373 gpg_error_t agent_key_from_file (ctrl_t ctrl,
374 const char *cache_nonce,
375 const char *desc_text,
376 const unsigned char *grip,
377 unsigned char **shadow_info,
378 cache_mode_t cache_mode,
379 lookup_ttl_t lookup_ttl,
381 char **r_passphrase);
382 gpg_error_t agent_raw_key_from_file (ctrl_t ctrl, const unsigned char *grip,
383 gcry_sexp_t *result);
384 gpg_error_t agent_public_key_from_file (ctrl_t ctrl,
385 const unsigned char *grip,
386 gcry_sexp_t *result);
387 int agent_is_dsa_key (gcry_sexp_t s_key);
388 int agent_is_eddsa_key (gcry_sexp_t s_key);
389 int agent_key_available (const unsigned char *grip);
390 gpg_error_t agent_key_info_from_file (ctrl_t ctrl, const unsigned char *grip,
392 unsigned char **r_shadow_info);
393 gpg_error_t agent_delete_key (ctrl_t ctrl, const char *desc_text,
394 const unsigned char *grip, int force);
396 /*-- call-pinentry.c --*/
397 void initialize_module_call_pinentry (void);
398 void agent_query_dump_state (void);
399 void agent_reset_query (ctrl_t ctrl);
400 int pinentry_active_p (ctrl_t ctrl, int waitseconds);
401 int agent_askpin (ctrl_t ctrl,
402 const char *desc_text, const char *prompt_text,
403 const char *inital_errtext,
404 struct pin_entry_info_s *pininfo,
405 const char *keyinfo, cache_mode_t cache_mode);
406 int agent_get_passphrase (ctrl_t ctrl, char **retpass,
407 const char *desc, const char *prompt,
408 const char *errtext, int with_qualitybar,
409 const char *keyinfo, cache_mode_t cache_mode);
410 int agent_get_confirmation (ctrl_t ctrl, const char *desc, const char *ok,
411 const char *notokay, int with_cancel);
412 int agent_show_message (ctrl_t ctrl, const char *desc, const char *ok_btn);
413 int agent_popup_message_start (ctrl_t ctrl,
414 const char *desc, const char *ok_btn);
415 void agent_popup_message_stop (ctrl_t ctrl);
416 int agent_clear_passphrase (ctrl_t ctrl,
417 const char *keyinfo, cache_mode_t cache_mode);
420 void initialize_module_cache (void);
421 void deinitialize_module_cache (void);
422 void agent_flush_cache (void);
423 int agent_put_cache (const char *key, cache_mode_t cache_mode,
424 const char *data, int ttl);
425 char *agent_get_cache (const char *key, cache_mode_t cache_mode);
426 void agent_store_cache_hit (const char *key);
430 int agent_pksign_do (ctrl_t ctrl, const char *cache_nonce,
431 const char *desc_text,
432 gcry_sexp_t *signature_sexp,
433 cache_mode_t cache_mode, lookup_ttl_t lookup_ttl,
434 const void *overridedata, size_t overridedatalen);
435 int agent_pksign (ctrl_t ctrl, const char *cache_nonce,
436 const char *desc_text,
437 membuf_t *outbuf, cache_mode_t cache_mode);
439 /*-- pkdecrypt.c --*/
440 int agent_pkdecrypt (ctrl_t ctrl, const char *desc_text,
441 const unsigned char *ciphertext, size_t ciphertextlen,
442 membuf_t *outbuf, int *r_padding);
445 int check_passphrase_constraints (ctrl_t ctrl, const char *pw,
446 char **failed_constraint);
447 gpg_error_t agent_ask_new_passphrase (ctrl_t ctrl, const char *prompt,
448 char **r_passphrase);
449 int agent_genkey (ctrl_t ctrl, const char *cache_nonce,
450 const char *keyparam, size_t keyparmlen,
451 int no_protection, const char *override_passphrase,
452 int preset, membuf_t *outbuf);
453 gpg_error_t agent_protect_and_store (ctrl_t ctrl, gcry_sexp_t s_skey,
454 char **passphrase_addr);
457 unsigned long get_standard_s2k_count (void);
458 unsigned char get_standard_s2k_count_rfc4880 (void);
459 int agent_protect (const unsigned char *plainkey, const char *passphrase,
460 unsigned char **result, size_t *resultlen,
461 unsigned long s2k_count);
462 int agent_unprotect (ctrl_t ctrl,
463 const unsigned char *protectedkey, const char *passphrase,
464 gnupg_isotime_t protected_at,
465 unsigned char **result, size_t *resultlen);
466 int agent_private_key_type (const unsigned char *privatekey);
467 unsigned char *make_shadow_info (const char *serialno, const char *idstring);
468 int agent_shadow_key (const unsigned char *pubkey,
469 const unsigned char *shadow_info,
470 unsigned char **result);
471 int agent_get_shadow_info (const unsigned char *shadowkey,
472 unsigned char const **shadow_info);
473 gpg_error_t parse_shadow_info (const unsigned char *shadow_info,
474 char **r_hexsn, char **r_idstr, int *r_pinlen);
475 gpg_error_t s2k_hash_passphrase (const char *passphrase, int hashalgo,
477 const unsigned char *s2ksalt,
478 unsigned int s2kcount,
479 unsigned char *key, size_t keylen);
482 /*-- trustlist.c --*/
483 void initialize_module_trustlist (void);
484 gpg_error_t agent_istrusted (ctrl_t ctrl, const char *fpr, int *r_disabled);
485 gpg_error_t agent_listtrusted (void *assuan_context);
486 gpg_error_t agent_marktrusted (ctrl_t ctrl, const char *name,
487 const char *fpr, int flag);
488 void agent_reload_trustlist (void);
491 /*-- divert-scd.c --*/
492 int divert_pksign (ctrl_t ctrl,
493 const unsigned char *digest, size_t digestlen, int algo,
494 const unsigned char *shadow_info, unsigned char **r_sig,
496 int divert_pkdecrypt (ctrl_t ctrl,
497 const unsigned char *cipher,
498 const unsigned char *shadow_info,
499 char **r_buf, size_t *r_len, int *r_padding);
500 int divert_generic_cmd (ctrl_t ctrl,
501 const char *cmdline, void *assuan_context);
502 int divert_writekey (ctrl_t ctrl, int force, const char *serialno,
503 const char *id, const char *keydata, size_t keydatalen);
507 void initialize_module_call_scd (void);
508 void agent_scd_dump_state (void);
509 int agent_scd_check_running (void);
510 void agent_scd_check_aliveness (void);
511 int agent_reset_scd (ctrl_t ctrl);
512 int agent_card_learn (ctrl_t ctrl,
513 void (*kpinfo_cb)(void*, const char *),
515 void (*certinfo_cb)(void*, const char *),
516 void *certinfo_cb_arg,
517 void (*sinfo_cb)(void*, const char *,
518 size_t, const char *),
520 int agent_card_serialno (ctrl_t ctrl, char **r_serialno);
521 int agent_card_pksign (ctrl_t ctrl,
523 int (*getpin_cb)(void *, const char *, char*, size_t),
526 const unsigned char *indata, size_t indatalen,
527 unsigned char **r_buf, size_t *r_buflen);
528 int agent_card_pkdecrypt (ctrl_t ctrl,
530 int (*getpin_cb)(void *, const char *, char*,size_t),
532 const unsigned char *indata, size_t indatalen,
533 char **r_buf, size_t *r_buflen, int *r_padding);
534 int agent_card_readcert (ctrl_t ctrl,
535 const char *id, char **r_buf, size_t *r_buflen);
536 int agent_card_readkey (ctrl_t ctrl, const char *id, unsigned char **r_buf);
537 int agent_card_writekey (ctrl_t ctrl, int force, const char *serialno,
538 const char *id, const char *keydata,
540 int (*getpin_cb)(void *, const char *, char*, size_t),
541 void *getpin_cb_arg);
542 gpg_error_t agent_card_getattr (ctrl_t ctrl, const char *name, char **result);
543 int agent_card_scd (ctrl_t ctrl, const char *cmdline,
544 int (*getpin_cb)(void *, const char *, char*, size_t),
545 void *getpin_cb_arg, void *assuan_context);
548 /*-- learncard.c --*/
549 int agent_handle_learn (ctrl_t ctrl, int send, void *assuan_context, int force);
552 /*-- cvt-openpgp.c --*/
554 extract_private_key (gcry_sexp_t s_key, int req_private_key_data,
555 const char **r_algoname, int *r_npkey, int *r_nskey,
556 const char **r_format,
557 gcry_mpi_t *mpi_array, int arraysize,
558 gcry_sexp_t *r_curve, gcry_sexp_t *r_flags);