1 /* main.h
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3  *               2008 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 #include "types.h"
23 #include "iobuf.h"
24 #include "mpi.h"
25 #include "cipher.h"
26 #include "keydb.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 USE_AES
34 # define DEFAULT_CIPHER_ALGO     CIPHER_ALGO_AES
35 #elif 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     DIGEST_ALGO_SHA1
42 #define DEFAULT_COMPRESS_ALGO   COMPRESS_ALGO_ZIP
43 #define DEFAULT_S2K_DIGEST_ALGO DIGEST_ALGO_SHA1
44 
45 #define S2K_DIGEST_ALGO (opt.s2k_digest_algo?opt.s2k_digest_algo:DEFAULT_S2K_DIGEST_ALGO)
46 
47 typedef struct
48 {
49   int header_okay;
50   PK_LIST pk_list;
51   DEK *symkey_dek;
52   STRING2KEY *symkey_s2k;
53   cipher_filter_context_t cfx;
54 } encrypt_filter_context_t;
55 
56 struct groupitem
57 {
58   char *name;
59   STRLIST values;
60   struct groupitem *next;
61 };
62 
63 struct weakhash
64 {
65   int algo;
66   int rejection_shown;
67   struct weakhash *next;
68 };
69 
70 
71 /*-- gpg.c --*/
72 extern int g10_errors_seen;
73 
74 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 )
75   void g10_exit(int rc) __attribute__ ((noreturn));
76 #else
77   void g10_exit(int rc);
78 #endif
79 void print_pubkey_algo_note( int algo );
80 void print_cipher_algo_note( int algo );
81 void print_digest_algo_note( int algo );
82 void additional_weak_digest (const char* digestname);
83 
84 /*-- armor.c --*/
85 char *make_radix64_string( const byte *data, size_t len );
86 int parse_key_failed_line (const void *lineptr, unsigned int len);
87 
88 /*-- misc.c --*/
89 void trap_unaligned(void);
90 int disable_core_dumps(void);
91 void register_secured_file (const char *fname);
92 void unregister_secured_file (const char *fname);
93 int  is_secured_file (int fd);
94 int  is_secured_filename (const char *fname);
95 u16 checksum_u16( unsigned n );
96 u16 checksum( byte *p, unsigned n );
97 u16 checksum_mpi( MPI a );
98 u32 buffer_to_u32( const byte *buffer );
99 const byte *get_session_marker( size_t *rlen );
100 int openpgp_cipher_test_algo( int algo );
101 int openpgp_pk_test_algo( int algo, unsigned int usage_flags );
102 int openpgp_pk_algo_usage ( int algo );
103 int openpgp_md_test_algo( int algo );
104 
105 void md5_digest_warn (int show);
106 
107 void not_in_gpg1_notice (void);
108 
109 struct expando_args
110 {
111   PKT_public_key *pk;
112   PKT_secret_key *sk;
113   byte imagetype;
114   int validity_info;
115   const char *validity_string;
116 };
117 
118 char *pct_expando(const char *string,struct expando_args *args);
119 void deprecated_warning(const char *configname,unsigned int configlineno,
120 			const char *option,const char *repl1,const char *repl2);
121 void deprecated_command (const char *name);
122 
123 const char *compress_algo_to_string(int algo);
124 int string_to_compress_algo(const char *string);
125 int check_compress_algo(int algo);
126 int default_cipher_algo(void);
127 int default_compress_algo(void);
128 const char *compliance_option_string(void);
129 void compliance_failure(void);
130 
131 struct parse_options
132 {
133   char *name;
134   unsigned int bit;
135   char **value;
136   char *help;
137 };
138 
139 char *optsep(char **stringp);
140 char *argsplit(char *string);
141 int parse_options(char *str,unsigned int *options,
142 		  struct parse_options *opts,int noisy);
143 char *unescape_percent_string (const unsigned char *s);
144 char *default_homedir (void);
145 const char *get_libexecdir (void);
146 int path_access(const char *file,int mode);
147 
148 /*-- helptext.c --*/
149 void display_online_help( const char *keyword );
150 
151 /*-- encode.c --*/
152 int setup_symkey(STRING2KEY **symkey_s2k,DEK **symkey_dek);
153 int encode_symmetric( const char *filename );
154 int encode_store( const char *filename );
155 int encode_crypt( const char *filename, STRLIST remusr, int use_symkey );
156 void encode_crypt_files(int nfiles, char **files, STRLIST remusr);
157 int encrypt_filter( void *opaque, int control,
158 		    IOBUF a, byte *buf, size_t *ret_len);
159 
160 
161 /*-- sign.c --*/
162 int complete_sig( PKT_signature *sig, PKT_secret_key *sk, MD_HANDLE md );
163 int sign_file( STRLIST filenames, int detached, STRLIST locusr,
164 	       int do_encrypt, STRLIST remusr, const char *outfile );
165 int clearsign_file( const char *fname, STRLIST locusr, const char *outfile );
166 int sign_symencrypt_file (const char *fname, STRLIST locusr);
167 
168 /*-- sig-check.c --*/
169 int check_revocation_keys (PKT_public_key *pk, PKT_signature *sig);
170 int check_backsig(PKT_public_key *main_pk,PKT_public_key *sub_pk,
171 		  PKT_signature *backsig);
172 int check_key_signature( KBNODE root, KBNODE node, int *is_selfsig );
173 int check_key_signature2( KBNODE root, KBNODE node, PKT_public_key *check_pk,
174 			  PKT_public_key *ret_pk, int *is_selfsig,
175 			  u32 *r_expiredate, int *r_expired );
176 
177 /*-- delkey.c --*/
178 int delete_keys( STRLIST names, int secret, int allow_both );
179 
180 /*-- keyedit.c --*/
181 void keyedit_menu( const char *username, STRLIST locusr,
182 		   STRLIST commands, int quiet, int seckey_check );
183 void show_basic_key_info (KBNODE keyblock);
184 
185 /*-- keygen.c --*/
186 u32 parse_expire_string(u32 timestamp,const char *string);
187 u32 ask_expire_interval(u32 timestamp,int object,const char *def_expire);
188 void generate_keypair( const char *fname, const char *card_serialno,
189                        const char *backup_encryption_dir );
190 int keygen_set_std_prefs (const char *string,int personal);
191 PKT_user_id *keygen_get_std_prefs (void);
192 int keygen_add_key_expire( PKT_signature *sig, void *opaque );
193 int keygen_add_std_prefs( PKT_signature *sig, void *opaque );
194 int keygen_upd_std_prefs( PKT_signature *sig, void *opaque );
195 int keygen_add_keyserver_url(PKT_signature *sig, void *opaque);
196 int keygen_add_notations(PKT_signature *sig,void *opaque);
197 int keygen_add_revkey(PKT_signature *sig, void *opaque);
198 int make_backsig(PKT_signature *sig,PKT_public_key *pk,
199 		 PKT_public_key *sub_pk,PKT_secret_key *sub_sk,
200                  u32 timestamp);
201 int generate_subkeypair( KBNODE pub_keyblock, KBNODE sec_keyblock );
202 #ifdef ENABLE_CARD_SUPPORT
203 int generate_card_subkeypair (KBNODE pub_keyblock, KBNODE sec_keyblock,
204                               int keyno, const char *serialno);
205 int save_unprotected_key_to_card (PKT_secret_key *sk, int keyno);
206 #endif
207 
208 /*-- openfile.c --*/
209 int overwrite_filep( const char *fname );
210 char *make_outfile_name( const char *iname );
211 char *ask_outfile_name( const char *name, size_t namelen );
212 int   open_outfile( const char *iname, int mode, IOBUF *a );
213 char *get_matching_datafile (const char *sigfilename);
214 IOBUF open_sigfile( const char *iname, progress_filter_context_t *pfx );
215 void try_make_homedir( const char *fname );
216 
217 /*-- seskey.c --*/
218 void make_session_key( DEK *dek );
219 MPI encode_session_key( DEK *dek, unsigned nbits );
220 MPI pkcs1_encode_md( MD_HANDLE md, int algo, size_t len, unsigned nbits,
221 		     const byte *asn, size_t asnlen );
222 MPI encode_md_value( PKT_public_key *pk, PKT_secret_key *sk,
223 		     MD_HANDLE md, int hash_algo );
224 
225 /*-- import.c --*/
226 
227 typedef int (*import_filter_t)(kbnode_t keyblock, void *arg);
228 
229 int parse_import_options(char *str,unsigned int *options,int noisy);
230 void import_keys( char **fnames, int nnames,
231 		  void *stats_hd, unsigned int options );
232 int import_keys_stream (IOBUF inp,void *stats_hd,unsigned char **fpr,
233                         size_t *fpr_len,unsigned int options,
234                         import_filter_t filter, void *filter_arg,
235                         int *r_gpgkeys_err);
236 void *import_new_stats_handle (void);
237 void import_release_stats_handle (void *p);
238 void import_print_stats (void *hd);
239 
240 int collapse_uids( KBNODE *keyblock );
241 
242 int auto_create_card_key_stub ( const char *serialnostr,
243                                 const unsigned char *fpr1,
244                                 const unsigned char *fpr2,
245                                 const unsigned char *fpr3);
246 
247 /*-- export.c --*/
248 int parse_export_options(char *str,unsigned int *options,int noisy);
249 int export_pubkeys( STRLIST users, unsigned int options );
250 int export_pubkeys_stream( IOBUF out, STRLIST users,
251 			   KBNODE *keyblock_out, unsigned int options );
252 int export_seckeys( STRLIST users );
253 int export_secsubkeys( STRLIST users );
254 
255 /* dearmor.c --*/
256 int dearmor_file( const char *fname );
257 int enarmor_file( const char *fname );
258 
259 /*-- revoke.c --*/
260 struct revocation_reason_info;
261 int gen_revoke( const char *uname );
262 int gen_desig_revoke( const char *uname, STRLIST locusr);
263 int revocation_reason_build_cb( PKT_signature *sig, void *opaque );
264 struct revocation_reason_info *
265 		ask_revocation_reason( int key_rev, int cert_rev, int hint );
266 void release_revocation_reason_info( struct revocation_reason_info *reason );
267 
268 /*-- keylist.c --*/
269 void public_key_list( STRLIST list );
270 void secret_key_list( STRLIST list );
271 void print_subpackets_colon(PKT_signature *sig);
272 void reorder_keyblock (KBNODE keyblock);
273 void list_keyblock( KBNODE keyblock, int secret, int fpr, void *opaque );
274 void print_fingerprint (PKT_public_key *pk, PKT_secret_key *sk, int mode);
275 void print_revokers(PKT_public_key *pk);
276 void show_policy_url(PKT_signature *sig,int indent,int mode);
277 void show_keyserver_url(PKT_signature *sig,int indent,int mode);
278 void show_notation(PKT_signature *sig,int indent,int mode,int which);
279 void dump_attribs(const PKT_user_id *uid,
280 		  PKT_public_key *pk,PKT_secret_key *sk);
281 void set_attrib_fd(int fd);
282 void print_seckey_info (PKT_secret_key *sk);
283 void print_pubkey_info (FILE *fp, PKT_public_key *pk);
284 void print_card_key_info (FILE *fp, KBNODE keyblock);
285 
286 /*-- verify.c --*/
287 void print_file_status( int status, const char *name, int what );
288 int verify_signatures( int nfiles, char **files );
289 int verify_files( int nfiles, char **files );
290 
291 /*-- decrypt.c --*/
292 int decrypt_message( const char *filename );
293 void decrypt_messages(int nfiles, char *files[]);
294 
295 /*-- plaintext.c --*/
296 int hash_datafiles( MD_HANDLE md, MD_HANDLE md2,
297 		    STRLIST files, const char *sigfilename, int textmode );
298 PKT_plaintext *setup_plaintext_name(const char *filename,IOBUF iobuf);
299 
300 /*-- pipemode.c --*/
301 void run_in_pipemode (void);
302 
303 /*-- signal.c --*/
304 void init_signals(void);
305 void pause_on_sigusr( int which );
306 void block_all_signals(void);
307 void unblock_all_signals(void);
308 
309 
310 #ifdef ENABLE_CARD_SUPPORT
311 /*-- card-util.c --*/
312 void change_pin (int no, int allow_admin);
313 void card_status (FILE *fp, char *serialno, size_t serialnobuflen);
314 void card_edit (STRLIST commands);
315 int  card_generate_subkey (KBNODE pub_keyblock, KBNODE sec_keyblock);
316 int  card_store_subkey (KBNODE node, int use);
317 #endif
318 
319 #define S2K_DECODE_COUNT(_val) ((16ul + ((_val) & 15)) << (((_val) >> 4) + 6))
320 
321 #endif /*G10_MAIN_H*/
322