1 /* agent.h - Global definitions for the agent
2 * Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #ifdef GPG_ERR_SOURCE_DEFAULT
24 #error GPG_ERR_SOURCE_DEFAULT already defined
26 #define GPG_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_GPGAGENT
27 #include <gpg-error.h>
28 #define map_assuan_err(a) \
29 map_assuan_err_with_source (GPG_ERR_SOURCE_DEFAULT, (a))
33 #include "../common/util.h"
34 #include "../common/errors.h"
37 /* To convey some special hash algorithms we use algorithm numbers
38 reserved for application use. */
40 #define GCRY_MD_USER 1024
42 #define GCRY_MD_USER_TLS_MD5SHA1 (GCRY_MD_USER+1)
44 /* Maximum length of a digest. */
45 #define MAX_DIGEST_LEN 36
47 /* A large struct name "opt" to keep global flags */
50 unsigned int debug; /* Debug flags (DBG_foo_VALUE) */
51 int verbose; /* Verbosity level */
52 int quiet; /* Be as quiet as possible */
53 int dry_run; /* Don't change any persistent data */
54 int batch; /* Batch mode */
55 const char *homedir; /* Configuration directory name */
57 /* Environment setting gathered at program start or changed using the
58 Assuan command UPDATESTARTUPTTY. */
59 char *startup_display;
60 char *startup_ttyname;
61 char *startup_ttytype;
62 char *startup_lc_ctype;
63 char *startup_lc_messages;
66 const char *pinentry_program; /* Filename of the program to start as
68 const char *scdaemon_program; /* Filename of the program to handle
70 int disable_scdaemon; /* Never use the SCdaemon. */
71 int no_grab; /* Don't let the pinentry grab the keyboard */
73 /* The name of the file pinentry shall tocuh before exiting. If
74 this is not set the filoe name of the standard socket is used. */
75 const char *pinentry_touch_file;
77 /* The default and maximum TTL of cache entries. */
78 unsigned long def_cache_ttl; /* Default. */
79 unsigned long def_cache_ttl_ssh; /* for SSH. */
80 unsigned long max_cache_ttl; /* Default. */
81 unsigned long max_cache_ttl_ssh; /* for SSH. */
83 /* Flag disallowin bypassing of the warning. */
84 int enforce_passphrase_constraints;
85 /* The require minmum length of a passphrase. */
86 unsigned int min_passphrase_len;
87 /* The minimum number of non-alpha characters in a passphrase. */
88 unsigned int min_passphrase_nonalpha;
89 /* File name with a patternfile or NULL if not enabled. */
90 const char *check_passphrase_pattern;
93 int running_detached; /* We are running detached from the tty. */
95 int ignore_cache_for_signing;
96 int allow_mark_trusted;
97 int allow_preset_passphrase;
98 int keep_tty; /* Don't switch the TTY (for pinentry) on request */
99 int keep_display; /* Don't switch the DISPLAY (for pinentry) on request */
100 int ssh_support; /* Enable ssh-agent emulation. */
104 #define DBG_COMMAND_VALUE 1 /* debug commands i/o */
105 #define DBG_MPI_VALUE 2 /* debug mpi details */
106 #define DBG_CRYPTO_VALUE 4 /* debug low level crypto */
107 #define DBG_MEMORY_VALUE 32 /* debug memory allocation stuff */
108 #define DBG_CACHE_VALUE 64 /* debug the caching */
109 #define DBG_MEMSTAT_VALUE 128 /* show memory statistics */
110 #define DBG_HASHING_VALUE 512 /* debug hashing operations */
111 #define DBG_ASSUAN_VALUE 1024
113 #define DBG_COMMAND (opt.debug & DBG_COMMAND_VALUE)
114 #define DBG_CRYPTO (opt.debug & DBG_CRYPTO_VALUE)
115 #define DBG_MEMORY (opt.debug & DBG_MEMORY_VALUE)
116 #define DBG_CACHE (opt.debug & DBG_CACHE_VALUE)
117 #define DBG_HASHING (opt.debug & DBG_HASHING_VALUE)
118 #define DBG_ASSUAN (opt.debug & DBG_ASSUAN_VALUE)
120 struct server_local_s;
123 /* Collection of data per session (aka connection). */
124 struct server_control_s
126 /* Private data used to fire up the connection thread. We use this
127 structure do avoid an extra allocation for just a few bytes. */
132 /* Private data of the server (command.c). */
133 struct server_local_s *server_local;
135 /* Private data of the SCdaemon (call-scd.c). */
136 struct scd_local_s *scd_local;
138 int connection_fd; /* -1 or an identifier for the current connection. */
147 unsigned char value[MAX_DIGEST_LEN];
151 unsigned char keygrip[20];
154 int use_auth_call; /* Hack to send the PKAUTH command instead of the
155 PKSIGN command to the scdaemon. */
159 struct pin_entry_info_s
161 int min_digits; /* min. number of digits required or 0 for freeform entry */
162 int max_digits; /* max. number of allowed digits allowed*/
165 int (*check_cb)(struct pin_entry_info_s *); /* CB used to check the PIN */
166 void *check_cb_arg; /* optional argument which might be of use in the CB */
167 const char *cb_errtext; /* used by the cb to displaye a specific error */
168 size_t max_length; /* allocated length of the buffer */
175 PRIVATE_KEY_UNKNOWN = 0,
176 PRIVATE_KEY_CLEAR = 1,
177 PRIVATE_KEY_PROTECTED = 2,
178 PRIVATE_KEY_SHADOWED = 3
182 /* Values for the cache_mode arguments. */
185 CACHE_MODE_IGNORE = 0, /* Special mode to by pass the cache. */
186 CACHE_MODE_ANY, /* Any mode except ignore matches. */
187 CACHE_MODE_NORMAL, /* Normal cache (gpg-agent). */
188 CACHE_MODE_USER, /* GET_PASSPHRASE related cache. */
189 CACHE_MODE_SSH /* SSH related cache. */
194 /*-- gpg-agent.c --*/
195 void agent_exit (int rc) JNLIB_GCC_A_NR; /* Also implemented in other tools */
196 const char *get_agent_socket_name (void);
197 const char *get_agent_ssh_socket_name (void);
198 void agent_sighup_action (void);
201 gpg_error_t agent_write_status (ctrl_t ctrl, const char *keyword, ...);
202 void bump_key_eventcounter (void);
203 void bump_card_eventcounter (void);
204 void start_command_handler (ctrl_t, int, int);
206 /*-- command-ssh.c --*/
207 void start_command_handler_ssh (ctrl_t, int);
210 int agent_write_private_key (const unsigned char *grip,
211 const void *buffer, size_t length, int force);
212 gpg_error_t agent_key_from_file (ctrl_t ctrl,
213 const char *desc_text,
214 const unsigned char *grip,
215 unsigned char **shadow_info,
216 cache_mode_t cache_mode,
217 gcry_sexp_t *result);
218 gpg_error_t agent_public_key_from_file (ctrl_t ctrl,
219 const unsigned char *grip,
220 gcry_sexp_t *result);
221 int agent_key_available (const unsigned char *grip);
223 /*-- call-pinentry.c --*/
224 void initialize_module_call_pinentry (void);
225 void agent_query_dump_state (void);
226 void agent_reset_query (ctrl_t ctrl);
227 int pinentry_active_p (ctrl_t ctrl, int waitseconds);
228 int agent_askpin (ctrl_t ctrl,
229 const char *desc_text, const char *prompt_text,
230 const char *inital_errtext,
231 struct pin_entry_info_s *pininfo);
232 int agent_get_passphrase (ctrl_t ctrl, char **retpass,
233 const char *desc, const char *prompt,
234 const char *errtext);
235 int agent_get_confirmation (ctrl_t ctrl, const char *desc, const char *ok,
237 int agent_show_message (ctrl_t ctrl, const char *desc, const char *ok_btn);
238 int agent_popup_message_start (ctrl_t ctrl,
239 const char *desc, const char *ok_btn);
240 void agent_popup_message_stop (ctrl_t ctrl);
244 void agent_flush_cache (void);
245 int agent_put_cache (const char *key, cache_mode_t cache_mode,
246 const char *data, int ttl);
247 const char *agent_get_cache (const char *key, cache_mode_t cache_mode,
249 void agent_unlock_cache_entry (void **cache_id);
253 int agent_pksign_do (ctrl_t ctrl, const char *desc_text,
254 gcry_sexp_t *signature_sexp,
255 cache_mode_t cache_mode);
256 int agent_pksign (ctrl_t ctrl, const char *desc_text,
257 membuf_t *outbuf, cache_mode_t cache_mode);
259 /*-- pkdecrypt.c --*/
260 int agent_pkdecrypt (ctrl_t ctrl, const char *desc_text,
261 const unsigned char *ciphertext, size_t ciphertextlen,
265 int check_passphrase_constraints (ctrl_t ctrl, const char *pw);
266 int agent_genkey (ctrl_t ctrl,
267 const char *keyparam, size_t keyparmlen, membuf_t *outbuf);
268 int agent_protect_and_store (ctrl_t ctrl, gcry_sexp_t s_skey);
271 int agent_protect (const unsigned char *plainkey, const char *passphrase,
272 unsigned char **result, size_t *resultlen);
273 int agent_unprotect (const unsigned char *protectedkey, const char *passphrase,
274 unsigned char **result, size_t *resultlen);
275 int agent_private_key_type (const unsigned char *privatekey);
276 unsigned char *make_shadow_info (const char *serialno, const char *idstring);
277 int agent_shadow_key (const unsigned char *pubkey,
278 const unsigned char *shadow_info,
279 unsigned char **result);
280 int agent_get_shadow_info (const unsigned char *shadowkey,
281 unsigned char const **shadow_info);
284 /*-- trustlist.c --*/
285 void initialize_module_trustlist (void);
286 gpg_error_t agent_istrusted (ctrl_t ctrl, const char *fpr);
287 gpg_error_t agent_listtrusted (void *assuan_context);
288 gpg_error_t agent_marktrusted (ctrl_t ctrl, const char *name,
289 const char *fpr, int flag);
290 void agent_reload_trustlist (void);
293 /*-- divert-scd.c --*/
294 int divert_pksign (ctrl_t ctrl,
295 const unsigned char *digest, size_t digestlen, int algo,
296 const unsigned char *shadow_info, unsigned char **r_sig);
297 int divert_pkdecrypt (ctrl_t ctrl,
298 const unsigned char *cipher,
299 const unsigned char *shadow_info,
300 char **r_buf, size_t *r_len);
301 int divert_generic_cmd (ctrl_t ctrl,
302 const char *cmdline, void *assuan_context);
306 void initialize_module_call_scd (void);
307 void agent_scd_dump_state (void);
308 void agent_scd_check_aliveness (void);
309 int agent_reset_scd (ctrl_t ctrl);
310 int agent_card_learn (ctrl_t ctrl,
311 void (*kpinfo_cb)(void*, const char *),
313 void (*certinfo_cb)(void*, const char *),
314 void *certinfo_cb_arg,
315 void (*sinfo_cb)(void*, const char *,
316 size_t, const char *),
318 int agent_card_serialno (ctrl_t ctrl, char **r_serialno);
319 int agent_card_pksign (ctrl_t ctrl,
321 int (*getpin_cb)(void *, const char *, char*, size_t),
323 const unsigned char *indata, size_t indatalen,
324 unsigned char **r_buf, size_t *r_buflen);
325 int agent_card_pkdecrypt (ctrl_t ctrl,
327 int (*getpin_cb)(void *, const char *, char*,size_t),
329 const unsigned char *indata, size_t indatalen,
330 char **r_buf, size_t *r_buflen);
331 int agent_card_readcert (ctrl_t ctrl,
332 const char *id, char **r_buf, size_t *r_buflen);
333 int agent_card_readkey (ctrl_t ctrl, const char *id, unsigned char **r_buf);
334 gpg_error_t agent_card_getattr (ctrl_t ctrl, const char *name, char **result);
335 int agent_card_scd (ctrl_t ctrl, const char *cmdline,
336 int (*getpin_cb)(void *, const char *, char*, size_t),
337 void *getpin_cb_arg, void *assuan_context);
340 /*-- learncard.c --*/
341 int agent_handle_learn (ctrl_t ctrl, void *assuan_context);