Imported Upstream version 2.1.1
[platform/upstream/gpg2.git] / g10 / main.h
1 /* main.h
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3  *               2008, 2009, 2010 Free Software Foundation, Inc.
4  *
5  * This file is part of GnuPG.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20 #ifndef G10_MAIN_H
21 #define G10_MAIN_H
22
23 #include "types.h"
24 #include "iobuf.h"
25 #include "keydb.h"
26 #include "util.h"
27
28 /* It could be argued that the default cipher should be 3DES rather
29    than AES128, and the default compression should be 0
30    (i.e. uncompressed) rather than 1 (zip).  However, the real world
31    issues of speed and size come into play here. */
32
33 #if GPG_USE_AES128
34 # define DEFAULT_CIPHER_ALGO     CIPHER_ALGO_AES
35 #elif GPG_USE_CAST5
36 # define DEFAULT_CIPHER_ALGO     CIPHER_ALGO_CAST5
37 #else
38 # define DEFAULT_CIPHER_ALGO     CIPHER_ALGO_3DES
39 #endif
40
41 #define DEFAULT_DIGEST_ALGO     ((GNUPG)? DIGEST_ALGO_SHA256:DIGEST_ALGO_SHA1)
42 #define DEFAULT_S2K_DIGEST_ALGO DIGEST_ALGO_SHA1
43 #ifdef HAVE_ZIP
44 # define DEFAULT_COMPRESS_ALGO   COMPRESS_ALGO_ZIP
45 #else
46 # define DEFAULT_COMPRESS_ALGO   COMPRESS_ALGO_NONE
47 #endif
48
49
50 #define S2K_DIGEST_ALGO (opt.s2k_digest_algo?opt.s2k_digest_algo:DEFAULT_S2K_DIGEST_ALGO)
51
52
53 /* Various data objects.  */
54
55 typedef struct
56 {
57   int header_okay;
58   PK_LIST pk_list;
59   DEK *symkey_dek;
60   STRING2KEY *symkey_s2k;
61   cipher_filter_context_t cfx;
62 } encrypt_filter_context_t;
63
64
65 struct groupitem
66 {
67   char *name;
68   strlist_t values;
69   struct groupitem *next;
70 };
71
72
73 /*-- gpg.c --*/
74 extern int g10_errors_seen;
75
76 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 )
77   void g10_exit(int rc) __attribute__ ((noreturn));
78 #else
79   void g10_exit(int rc);
80 #endif
81 void print_pubkey_algo_note (pubkey_algo_t algo);
82 void print_cipher_algo_note (cipher_algo_t algo);
83 void print_digest_algo_note (digest_algo_t algo);
84 void print_md5_rejected_note (void);
85
86 /*-- armor.c --*/
87 char *make_radix64_string( const byte *data, size_t len );
88
89 /*-- misc.c --*/
90 void trap_unaligned(void);
91 int disable_core_dumps(void);
92 void register_secured_file (const char *fname);
93 void unregister_secured_file (const char *fname);
94 int  is_secured_file (int fd);
95 int  is_secured_filename (const char *fname);
96 u16 checksum_u16( unsigned n );
97 u16 checksum( byte *p, unsigned n );
98 u16 checksum_mpi( gcry_mpi_t a );
99 u32 buffer_to_u32( const byte *buffer );
100 const byte *get_session_marker( size_t *rlen );
101
102 enum gcry_cipher_algos map_cipher_openpgp_to_gcry (cipher_algo_t algo);
103 #define openpgp_cipher_open(_a,_b,_c,_d) \
104   gcry_cipher_open((_a),map_cipher_openpgp_to_gcry((_b)),(_c),(_d))
105 #define openpgp_cipher_get_algo_keylen(_a) \
106   gcry_cipher_get_algo_keylen(map_cipher_openpgp_to_gcry((_a)))
107 #define openpgp_cipher_get_algo_blklen(_a) \
108   gcry_cipher_get_algo_blklen(map_cipher_openpgp_to_gcry((_a)))
109 int openpgp_cipher_blocklen (cipher_algo_t algo);
110 int openpgp_cipher_test_algo(cipher_algo_t algo);
111 const char *openpgp_cipher_algo_name (cipher_algo_t algo);
112
113 pubkey_algo_t map_pk_gcry_to_openpgp (enum gcry_pk_algos algo);
114 int openpgp_pk_test_algo (pubkey_algo_t algo);
115 int openpgp_pk_test_algo2 (pubkey_algo_t algo, unsigned int use);
116 int openpgp_pk_algo_usage ( int algo );
117 const char *openpgp_pk_algo_name (pubkey_algo_t algo);
118
119 enum gcry_md_algos map_md_openpgp_to_gcry (digest_algo_t algo);
120 int openpgp_md_test_algo (digest_algo_t algo);
121 const char *openpgp_md_algo_name (int algo);
122
123 struct expando_args
124 {
125   PKT_public_key *pk;
126   PKT_public_key *pksk;
127   byte imagetype;
128   int validity_info;
129   const char *validity_string;
130   const byte *namehash;
131 };
132
133 char *pct_expando(const char *string,struct expando_args *args);
134 void deprecated_warning(const char *configname,unsigned int configlineno,
135                         const char *option,const char *repl1,const char *repl2);
136 void deprecated_command (const char *name);
137 void obsolete_option (const char *configname, unsigned int configlineno,
138                       const char *name);
139 void obsolete_scdaemon_option (const char *configname,
140                                unsigned int configlineno, const char *name);
141
142 int string_to_cipher_algo (const char *string);
143 int string_to_digest_algo (const char *string);
144
145 const char *compress_algo_to_string(int algo);
146 int string_to_compress_algo(const char *string);
147 int check_compress_algo(int algo);
148 int default_cipher_algo(void);
149 int default_compress_algo(void);
150 const char *compliance_option_string(void);
151 void compliance_failure(void);
152
153 struct parse_options
154 {
155   char *name;
156   unsigned int bit;
157   char **value;
158   char *help;
159 };
160
161 char *optsep(char **stringp);
162 char *argsplit(char *string);
163 int parse_options(char *str,unsigned int *options,
164                   struct parse_options *opts,int noisy);
165 int has_invalid_email_chars (const char *s);
166 int is_valid_mailbox (const char *name);
167 int is_valid_user_id (const char *uid);
168 const char *get_libexecdir (void);
169 int path_access(const char *file,int mode);
170
171 int pubkey_get_npkey (pubkey_algo_t algo);
172 int pubkey_get_nskey (pubkey_algo_t algo);
173 int pubkey_get_nsig (pubkey_algo_t algo);
174 int pubkey_get_nenc (pubkey_algo_t algo);
175
176 /* Temporary helpers. */
177 unsigned int pubkey_nbits( int algo, gcry_mpi_t *pkey );
178 int mpi_print (estream_t stream, gcry_mpi_t a, int mode);
179 unsigned int ecdsa_qbits_from_Q (unsigned int qbits);
180
181
182 /*-- status.c --*/
183 void set_status_fd ( int fd );
184 int  is_status_enabled ( void );
185 void write_status ( int no );
186 void write_status_error (const char *where, gpg_error_t err);
187 void write_status_errcode (const char *where, int errcode);
188 void write_status_text ( int no, const char *text );
189 void write_status_strings (int no, const char *text,
190                            ...) GNUPG_GCC_A_SENTINEL(0);
191 void write_status_buffer ( int no,
192                            const char *buffer, size_t len, int wrap );
193 void write_status_text_and_buffer ( int no, const char *text,
194                                     const char *buffer, size_t len, int wrap );
195
196 void write_status_begin_signing (gcry_md_hd_t md);
197
198
199 int cpr_enabled(void);
200 char *cpr_get( const char *keyword, const char *prompt );
201 char *cpr_get_no_help( const char *keyword, const char *prompt );
202 char *cpr_get_utf8( const char *keyword, const char *prompt );
203 char *cpr_get_hidden( const char *keyword, const char *prompt );
204 void cpr_kill_prompt(void);
205 int  cpr_get_answer_is_yes_def (const char *keyword, const char *prompt,
206                                 int def_yes);
207 int  cpr_get_answer_is_yes( const char *keyword, const char *prompt );
208 int  cpr_get_answer_yes_no_quit( const char *keyword, const char *prompt );
209 int  cpr_get_answer_okay_cancel (const char *keyword,
210                                  const char *prompt,
211                                  int def_answer);
212
213 /*-- helptext.c --*/
214 void display_online_help( const char *keyword );
215
216 /*-- encode.c --*/
217 int setup_symkey (STRING2KEY **symkey_s2k,DEK **symkey_dek);
218 int encrypt_symmetric (const char *filename );
219 int encrypt_store (const char *filename );
220 int encrypt_crypt (ctrl_t ctrl, int filefd, const char *filename,
221                    strlist_t remusr, int use_symkey, pk_list_t provided_keys,
222                    int outputfd);
223 void encrypt_crypt_files (ctrl_t ctrl,
224                           int nfiles, char **files, strlist_t remusr);
225 int encrypt_filter (void *opaque, int control,
226                     iobuf_t a, byte *buf, size_t *ret_len);
227
228
229 /*-- sign.c --*/
230 int complete_sig (PKT_signature *sig, PKT_public_key *pksk, gcry_md_hd_t md,
231                   const char *cache_nonce);
232 int sign_file (ctrl_t ctrl, strlist_t filenames, int detached, strlist_t locusr,
233                int do_encrypt, strlist_t remusr, const char *outfile );
234 int clearsign_file( const char *fname, strlist_t locusr, const char *outfile );
235 int sign_symencrypt_file (const char *fname, strlist_t locusr);
236
237 /*-- sig-check.c --*/
238 int check_revocation_keys (PKT_public_key *pk, PKT_signature *sig);
239 int check_backsig(PKT_public_key *main_pk,PKT_public_key *sub_pk,
240                   PKT_signature *backsig);
241 int check_key_signature( KBNODE root, KBNODE node, int *is_selfsig );
242 int check_key_signature2( KBNODE root, KBNODE node, PKT_public_key *check_pk,
243                           PKT_public_key *ret_pk, int *is_selfsig,
244                           u32 *r_expiredate, int *r_expired );
245
246 /*-- delkey.c --*/
247 gpg_error_t delete_keys (strlist_t names, int secret, int allow_both);
248
249 /*-- keyedit.c --*/
250 void keyedit_menu (ctrl_t ctrl, const char *username, strlist_t locusr,
251                    strlist_t commands, int quiet, int seckey_check );
252 void keyedit_passwd (ctrl_t ctrl, const char *username);
253 void keyedit_quick_sign (ctrl_t ctrl, const char *fpr,
254                          strlist_t uids, strlist_t locusr, int local);
255 void show_basic_key_info (KBNODE keyblock);
256
257 /*-- keygen.c --*/
258 u32 parse_expire_string(const char *string);
259 u32 ask_expire_interval(int object,const char *def_expire);
260 u32 ask_expiredate(void);
261 void quick_generate_keypair (const char *uid);
262 void generate_keypair (ctrl_t ctrl, int full, const char *fname,
263                        const char *card_serialno, int card_backup_key);
264 int keygen_set_std_prefs (const char *string,int personal);
265 PKT_user_id *keygen_get_std_prefs (void);
266 int keygen_add_key_expire( PKT_signature *sig, void *opaque );
267 int keygen_add_std_prefs( PKT_signature *sig, void *opaque );
268 int keygen_upd_std_prefs( PKT_signature *sig, void *opaque );
269 int keygen_add_keyserver_url(PKT_signature *sig, void *opaque);
270 int keygen_add_notations(PKT_signature *sig,void *opaque);
271 int keygen_add_revkey(PKT_signature *sig, void *opaque);
272 gpg_error_t make_backsig (PKT_signature *sig, PKT_public_key *pk,
273                           PKT_public_key *sub_pk, PKT_public_key *sub_psk,
274                           u32 timestamp, const char *cache_nonce);
275 gpg_error_t generate_subkeypair (ctrl_t ctrl, kbnode_t pub_keyblock);
276 #ifdef ENABLE_CARD_SUPPORT
277 gpg_error_t generate_card_subkeypair (kbnode_t pub_keyblock,
278                                       int keyno, const char *serialno);
279 int save_unprotected_key_to_card (PKT_public_key *sk, int keyno);
280 #endif
281
282
283 /*-- openfile.c --*/
284 int overwrite_filep( const char *fname );
285 char *make_outfile_name( const char *iname );
286 char *ask_outfile_name( const char *name, size_t namelen );
287 int open_outfile (int inp_fd, const char *iname, int mode,
288                   int restrictedperm, iobuf_t *a);
289 char *get_matching_datafile (const char *sigfilename);
290 iobuf_t open_sigfile (const char *sigfilename, progress_filter_context_t *pfx);
291 void try_make_homedir( const char *fname );
292 char *get_openpgp_revocdir (const char *home);
293
294 /*-- seskey.c --*/
295 void make_session_key( DEK *dek );
296 gcry_mpi_t encode_session_key( int openpgp_pk_algo, DEK *dek, unsigned nbits );
297 gcry_mpi_t encode_md_value (PKT_public_key *pk,
298                             gcry_md_hd_t md, int hash_algo );
299
300 /*-- import.c --*/
301 typedef gpg_error_t (*import_screener_t)(kbnode_t keyblock, void *arg);
302
303 int parse_import_options(char *str,unsigned int *options,int noisy);
304 void import_keys (ctrl_t ctrl, char **fnames, int nnames,
305                   void *stats_hd, unsigned int options);
306 int import_keys_stream (ctrl_t ctrl, iobuf_t inp, void *stats_hd,
307                         unsigned char **fpr,
308                         size_t *fpr_len, unsigned int options);
309 int import_keys_es_stream (ctrl_t ctrl, estream_t fp, void *stats_handle,
310                            unsigned char **fpr, size_t *fpr_len,
311                            unsigned int options,
312                            import_screener_t screener, void *screener_arg);
313 gpg_error_t import_old_secring (ctrl_t ctrl, const char *fname);
314 void *import_new_stats_handle (void);
315 void import_release_stats_handle (void *p);
316 void import_print_stats (void *hd);
317
318 int collapse_uids( KBNODE *keyblock );
319
320
321 /*-- export.c --*/
322 int parse_export_options(char *str,unsigned int *options,int noisy);
323 int export_pubkeys (ctrl_t ctrl, strlist_t users, unsigned int options );
324 int export_pubkeys_stream (ctrl_t ctrl, iobuf_t out, strlist_t users,
325                            kbnode_t *keyblock_out, unsigned int options );
326 gpg_error_t export_pubkey_buffer (ctrl_t ctrl, const char *keyspec,
327                                   unsigned int options,
328                                   kbnode_t *r_keyblock,
329                                   void **r_data, size_t *r_datalen);
330 int export_seckeys (ctrl_t ctrl, strlist_t users);
331 int export_secsubkeys (ctrl_t ctrl, strlist_t users);
332
333 /*-- dearmor.c --*/
334 int dearmor_file( const char *fname );
335 int enarmor_file( const char *fname );
336
337 /*-- revoke.c --*/
338 struct revocation_reason_info;
339 int gen_standard_revoke (PKT_public_key *psk, const char *cache_nonce);
340 int gen_revoke( const char *uname );
341 int gen_desig_revoke( const char *uname, strlist_t locusr);
342 int revocation_reason_build_cb( PKT_signature *sig, void *opaque );
343 struct revocation_reason_info *
344                 ask_revocation_reason( int key_rev, int cert_rev, int hint );
345 void release_revocation_reason_info( struct revocation_reason_info *reason );
346
347 /*-- keylist.c --*/
348 void public_key_list (ctrl_t ctrl, strlist_t list, int locate_mode );
349 void secret_key_list (ctrl_t ctrl, strlist_t list );
350 void print_subpackets_colon(PKT_signature *sig);
351 void reorder_keyblock (KBNODE keyblock);
352 void list_keyblock (kbnode_t keyblock, int secret, int has_secret,
353                     int fpr, void *opaque);
354 void print_fingerprint (estream_t fp, PKT_public_key *pk, int mode);
355 void print_revokers (estream_t fp, PKT_public_key *pk);
356 void show_policy_url(PKT_signature *sig,int indent,int mode);
357 void show_keyserver_url(PKT_signature *sig,int indent,int mode);
358 void show_notation(PKT_signature *sig,int indent,int mode,int which);
359 void dump_attribs (const PKT_user_id *uid, PKT_public_key *pk);
360 void set_attrib_fd(int fd);
361 void print_seckey_info (PKT_public_key *pk);
362 void print_pubkey_info (estream_t fp, PKT_public_key *pk);
363 void print_card_key_info (estream_t fp, KBNODE keyblock);
364
365 /*-- verify.c --*/
366 void print_file_status( int status, const char *name, int what );
367 int verify_signatures (ctrl_t ctrl, int nfiles, char **files );
368 int verify_files (ctrl_t ctrl, int nfiles, char **files );
369 int gpg_verify (ctrl_t ctrl, int sig_fd, int data_fd, estream_t out_fp);
370
371 /*-- decrypt.c --*/
372 int decrypt_message (ctrl_t ctrl, const char *filename );
373 gpg_error_t decrypt_message_fd (ctrl_t ctrl, int input_fd, int output_fd);
374 void decrypt_messages (ctrl_t ctrl, int nfiles, char *files[]);
375
376 /*-- plaintext.c --*/
377 int hash_datafiles( gcry_md_hd_t md, gcry_md_hd_t md2,
378                     strlist_t files, const char *sigfilename, int textmode);
379 int hash_datafile_by_fd ( gcry_md_hd_t md, gcry_md_hd_t md2, int data_fd,
380                           int textmode );
381 PKT_plaintext *setup_plaintext_name(const char *filename,IOBUF iobuf);
382
383 /*-- signal.c --*/
384 void init_signals(void);
385 void block_all_signals(void);
386 void unblock_all_signals(void);
387
388 /*-- server.c --*/
389 int gpg_server (ctrl_t);
390 gpg_error_t gpg_proxy_pinentry_notify (ctrl_t ctrl,
391                                        const unsigned char *line);
392
393 #ifdef ENABLE_CARD_SUPPORT
394 /*-- card-util.c --*/
395 void change_pin (int no, int allow_admin);
396 void card_status (estream_t fp, char *serialno, size_t serialnobuflen);
397 void card_edit (ctrl_t ctrl, strlist_t commands);
398 gpg_error_t  card_generate_subkey (KBNODE pub_keyblock);
399 int  card_store_subkey (KBNODE node, int use);
400 #endif
401
402 #define S2K_DECODE_COUNT(_val) ((16ul + ((_val) & 15)) << (((_val) >> 4) + 6))
403
404 /*-- migrate.c --*/
405 void migrate_secring (ctrl_t ctrl);
406
407
408 #endif /*G10_MAIN_H*/