Line data Source code
1 : /* util.h - Utility functions for GnuPG
2 : * Copyright (C) 2001, 2002, 2003, 2004, 2009 Free Software Foundation, Inc.
3 : *
4 : * This file is part of GnuPG.
5 : *
6 : * GnuPG is free software; you can redistribute it and/or modify it
7 : * under the terms of either
8 : *
9 : * - the GNU Lesser General Public License as published by the Free
10 : * Software Foundation; either version 3 of the License, or (at
11 : * your option) any later version.
12 : *
13 : * or
14 : *
15 : * - the GNU General Public License as published by the Free
16 : * Software Foundation; either version 2 of the License, or (at
17 : * your option) any later version.
18 : *
19 : * or both in parallel, as here.
20 : *
21 : * GnuPG is distributed in the hope that it will be useful, but
22 : * WITHOUT ANY WARRANTY; without even the implied warranty of
23 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 : * General Public License for more details.
25 : *
26 : * You should have received a copies of the GNU General Public License
27 : * and the GNU Lesser General Public License along with this program;
28 : * if not, see <http://www.gnu.org/licenses/>.
29 : */
30 :
31 : #ifndef GNUPG_COMMON_UTIL_H
32 : #define GNUPG_COMMON_UTIL_H
33 :
34 : #include <gcrypt.h> /* We need this for the memory function protos. */
35 : #include <errno.h> /* We need errno. */
36 : #include <gpg-error.h> /* We need gpg_error_t and estream. */
37 :
38 : /* These error codes are used but not defined in the required
39 : libgpg-error version. Define them here. */
40 : #if GPG_ERROR_VERSION_NUMBER < 0x011200 /* 1.18 */
41 : # define GPG_ERR_LEGACY_KEY 222
42 : # define GPG_ERR_OBJ_TERM_STATE 225
43 : # define GPG_ERR_FORBIDDEN 251
44 : #endif
45 : #if GPG_ERROR_VERSION_NUMBER < 0x011300 /* 1.19 */
46 : # define GPG_ERR_LDAP_GENERAL 721
47 : # define GPG_ERR_LDAP_ATTR_GENERAL 722
48 : # define GPG_ERR_LDAP_NAME_GENERAL 723
49 : # define GPG_ERR_LDAP_SECURITY_GENERAL 724
50 : # define GPG_ERR_LDAP_SERVICE_GENERAL 725
51 : # define GPG_ERR_LDAP_UPDATE_GENERAL 726
52 : # define GPG_ERR_LDAP_E_GENERAL 727
53 : # define GPG_ERR_LDAP_X_GENERAL 728
54 : # define GPG_ERR_LDAP_OTHER_GENERAL 729
55 : # define GPG_ERR_LDAP_X_CONNECTING 750
56 : # define GPG_ERR_LDAP_REFERRAL_LIMIT 751
57 : # define GPG_ERR_LDAP_CLIENT_LOOP 752
58 : # define GPG_ERR_LDAP_NO_RESULTS 754
59 : # define GPG_ERR_LDAP_CONTROL_NOT_FOUND 755
60 : # define GPG_ERR_LDAP_NOT_SUPPORTED 756
61 : # define GPG_ERR_LDAP_CONNECT 757
62 : # define GPG_ERR_LDAP_NO_MEMORY 758
63 : # define GPG_ERR_LDAP_PARAM 759
64 : # define GPG_ERR_LDAP_USER_CANCELLED 760
65 : # define GPG_ERR_LDAP_FILTER 761
66 : # define GPG_ERR_LDAP_AUTH_UNKNOWN 762
67 : # define GPG_ERR_LDAP_TIMEOUT 763
68 : # define GPG_ERR_LDAP_DECODING 764
69 : # define GPG_ERR_LDAP_ENCODING 765
70 : # define GPG_ERR_LDAP_LOCAL 766
71 : # define GPG_ERR_LDAP_SERVER_DOWN 767
72 : # define GPG_ERR_LDAP_SUCCESS 768
73 : # define GPG_ERR_LDAP_OPERATIONS 769
74 : # define GPG_ERR_LDAP_PROTOCOL 770
75 : # define GPG_ERR_LDAP_TIMELIMIT 771
76 : # define GPG_ERR_LDAP_SIZELIMIT 772
77 : # define GPG_ERR_LDAP_COMPARE_FALSE 773
78 : # define GPG_ERR_LDAP_COMPARE_TRUE 774
79 : # define GPG_ERR_LDAP_UNSUPPORTED_AUTH 775
80 : # define GPG_ERR_LDAP_STRONG_AUTH_RQRD 776
81 : # define GPG_ERR_LDAP_PARTIAL_RESULTS 777
82 : # define GPG_ERR_LDAP_REFERRAL 778
83 : # define GPG_ERR_LDAP_ADMINLIMIT 779
84 : # define GPG_ERR_LDAP_UNAVAIL_CRIT_EXTN 780
85 : # define GPG_ERR_LDAP_CONFIDENT_RQRD 781
86 : # define GPG_ERR_LDAP_SASL_BIND_INPROG 782
87 : # define GPG_ERR_LDAP_NO_SUCH_ATTRIBUTE 784
88 : # define GPG_ERR_LDAP_UNDEFINED_TYPE 785
89 : # define GPG_ERR_LDAP_BAD_MATCHING 786
90 : # define GPG_ERR_LDAP_CONST_VIOLATION 787
91 : # define GPG_ERR_LDAP_TYPE_VALUE_EXISTS 788
92 : # define GPG_ERR_LDAP_INV_SYNTAX 789
93 : # define GPG_ERR_LDAP_NO_SUCH_OBJ 800
94 : # define GPG_ERR_LDAP_ALIAS_PROBLEM 801
95 : # define GPG_ERR_LDAP_INV_DN_SYNTAX 802
96 : # define GPG_ERR_LDAP_IS_LEAF 803
97 : # define GPG_ERR_LDAP_ALIAS_DEREF 804
98 : # define GPG_ERR_LDAP_X_PROXY_AUTH_FAIL 815
99 : # define GPG_ERR_LDAP_BAD_AUTH 816
100 : # define GPG_ERR_LDAP_INV_CREDENTIALS 817
101 : # define GPG_ERR_LDAP_INSUFFICIENT_ACC 818
102 : # define GPG_ERR_LDAP_BUSY 819
103 : # define GPG_ERR_LDAP_UNAVAILABLE 820
104 : # define GPG_ERR_LDAP_UNWILL_TO_PERFORM 821
105 : # define GPG_ERR_LDAP_LOOP_DETECT 822
106 : # define GPG_ERR_LDAP_NAMING_VIOLATION 832
107 : # define GPG_ERR_LDAP_OBJ_CLS_VIOLATION 833
108 : # define GPG_ERR_LDAP_NOT_ALLOW_NONLEAF 834
109 : # define GPG_ERR_LDAP_NOT_ALLOW_ON_RDN 835
110 : # define GPG_ERR_LDAP_ALREADY_EXISTS 836
111 : # define GPG_ERR_LDAP_NO_OBJ_CLASS_MODS 837
112 : # define GPG_ERR_LDAP_RESULTS_TOO_LARGE 838
113 : # define GPG_ERR_LDAP_AFFECTS_MULT_DSAS 839
114 : # define GPG_ERR_LDAP_VLV 844
115 : # define GPG_ERR_LDAP_OTHER 848
116 : # define GPG_ERR_LDAP_CUP_RESOURCE_LIMIT 881
117 : # define GPG_ERR_LDAP_CUP_SEC_VIOLATION 882
118 : # define GPG_ERR_LDAP_CUP_INV_DATA 883
119 : # define GPG_ERR_LDAP_CUP_UNSUP_SCHEME 884
120 : # define GPG_ERR_LDAP_CUP_RELOAD 885
121 : # define GPG_ERR_LDAP_CANCELLED 886
122 : # define GPG_ERR_LDAP_NO_SUCH_OPERATION 887
123 : # define GPG_ERR_LDAP_TOO_LATE 888
124 : # define GPG_ERR_LDAP_CANNOT_CANCEL 889
125 : # define GPG_ERR_LDAP_ASSERTION_FAILED 890
126 : # define GPG_ERR_LDAP_PROX_AUTH_DENIED 891
127 : #endif /*GPG_ERROR_VERSION_NUMBER < 0x011300*/
128 : #if GPG_ERROR_VERSION_NUMBER < 0x011500 /* 1.21 */
129 : # define GPG_ERR_SERVER_FAILED 219
130 : # define GPG_ERR_NO_KEY 220
131 : # define GPG_ERR_NO_NAME 221
132 : # define GPG_ERR_TRUE 255
133 : # define GPG_ERR_FALSE 256
134 : #endif
135 :
136 : /* Hash function used with libksba. */
137 : #define HASH_FNC ((void (*)(void *, const void*,size_t))gcry_md_write)
138 :
139 : /* Get all the stuff from jnlib. */
140 : #include "../common/logging.h"
141 : #include "../common/argparse.h"
142 : #include "../common/stringhelp.h"
143 : #include "../common/mischelp.h"
144 : #include "../common/strlist.h"
145 : #include "../common/dotlock.h"
146 : #include "../common/utf8conv.h"
147 : #include "../common/dynload.h"
148 :
149 : #include "gettime.h"
150 :
151 : /* Redefine asprintf by our estream version which uses our own memory
152 : allocator.. */
153 : #define asprintf gpgrt_asprintf
154 : #define vasprintf gpgrt_vasprintf
155 :
156 : /* Due to a bug in mingw32's snprintf related to the 'l' modifier and
157 : for increased portability we use our snprintf on all systems. */
158 : #undef snprintf
159 : #define snprintf gpgrt_snprintf
160 :
161 :
162 : /* Replacements for macros not available with libgpg-error < 1.20. */
163 : #ifndef GPGRT_GCC_VERSION
164 :
165 : # ifndef GPGRT_ATTR_FORMAT_ARG
166 : # if __GNUC__ >= 3 /* Actually 2.8 but testing the major is easier. */
167 : # define GPGRT_ATTR_FORMAT_ARG(a) __attribute__ ((__format_arg__ (a)))
168 : # else
169 : # define GPGRT_ATTR_FORMAT_ARG(a)
170 : # endif
171 : # endif
172 :
173 : # if __GNUC__ >= 4
174 : # define GPGRT_ATTR_SENTINEL(a) __attribute__ ((sentinel(a)))
175 : # else
176 : # define GPGRT_ATTR_SENTINEL(a)
177 : # endif
178 :
179 : # if __GNUC__ >= 4
180 : # define GPGRT_ATTR_USED __attribute__ ((used))
181 : # else
182 : # define GPGRT_ATTR_USED
183 : # endif
184 :
185 : #endif /*libgpg-error < 1.20 */
186 :
187 :
188 : /* We need this type even if we are not using libreadline and or we
189 : did not include libreadline in the current file. */
190 : #ifndef GNUPG_LIBREADLINE_H_INCLUDED
191 : typedef char **rl_completion_func_t (const char *, int, int);
192 : #endif /*!GNUPG_LIBREADLINE_H_INCLUDED*/
193 :
194 :
195 : /* Handy malloc macros - please use only them. */
196 : #define xtrymalloc(a) gcry_malloc ((a))
197 : #define xtrymalloc_secure(a) gcry_malloc_secure ((a))
198 : #define xtrycalloc(a,b) gcry_calloc ((a),(b))
199 : #define xtrycalloc_secure(a,b) gcry_calloc_secure ((a),(b))
200 : #define xtryrealloc(a,b) gcry_realloc ((a),(b))
201 : #define xtrystrdup(a) gcry_strdup ((a))
202 : #define xfree(a) gcry_free ((a))
203 : #define xfree_fnc gcry_free
204 :
205 : #define xmalloc(a) gcry_xmalloc ((a))
206 : #define xmalloc_secure(a) gcry_xmalloc_secure ((a))
207 : #define xcalloc(a,b) gcry_xcalloc ((a),(b))
208 : #define xcalloc_secure(a,b) gcry_xcalloc_secure ((a),(b))
209 : #define xrealloc(a,b) gcry_xrealloc ((a),(b))
210 : #define xstrdup(a) gcry_xstrdup ((a))
211 :
212 : /* For compatibility with gpg 1.4 we also define these: */
213 : #define xmalloc_clear(a) gcry_xcalloc (1, (a))
214 : #define xmalloc_secure_clear(a) gcry_xcalloc_secure (1, (a))
215 :
216 : /* The default error source of the application. This is different
217 : from GPG_ERR_SOURCE_DEFAULT in that it does not depend on the
218 : source file and thus is usable in code shared by applications.
219 : Defined by init.c. */
220 : extern gpg_err_source_t default_errsource;
221 :
222 : /* Convenience function to return a gpg-error code for memory
223 : allocation failures. This function makes sure that an error will
224 : be returned even if accidently ERRNO is not set. */
225 : static inline gpg_error_t
226 0 : out_of_core (void)
227 : {
228 0 : return gpg_error_from_syserror ();
229 : }
230 :
231 :
232 : /*-- signal.c --*/
233 : void gnupg_init_signals (int mode, void (*fast_cleanup)(void));
234 : void gnupg_block_all_signals (void);
235 : void gnupg_unblock_all_signals (void);
236 :
237 : /*-- yesno.c --*/
238 : int answer_is_yes (const char *s);
239 : int answer_is_yes_no_default (const char *s, int def_answer);
240 : int answer_is_yes_no_quit (const char *s);
241 : int answer_is_okay_cancel (const char *s, int def_answer);
242 :
243 : /*-- xreadline.c --*/
244 : ssize_t read_line (FILE *fp,
245 : char **addr_of_buffer, size_t *length_of_buffer,
246 : size_t *max_length);
247 :
248 :
249 : /*-- b64enc.c and b64dec.c --*/
250 : struct b64state
251 : {
252 : unsigned int flags;
253 : int idx;
254 : int quad_count;
255 : FILE *fp;
256 : estream_t stream;
257 : char *title;
258 : unsigned char radbuf[4];
259 : u32 crc;
260 : int stop_seen:1;
261 : int invalid_encoding:1;
262 : gpg_error_t lasterr;
263 : };
264 :
265 : gpg_error_t b64enc_start (struct b64state *state, FILE *fp, const char *title);
266 : gpg_error_t b64enc_start_es (struct b64state *state, estream_t fp,
267 : const char *title);
268 : gpg_error_t b64enc_write (struct b64state *state,
269 : const void *buffer, size_t nbytes);
270 : gpg_error_t b64enc_finish (struct b64state *state);
271 :
272 : gpg_error_t b64dec_start (struct b64state *state, const char *title);
273 : gpg_error_t b64dec_proc (struct b64state *state, void *buffer, size_t length,
274 : size_t *r_nbytes);
275 : gpg_error_t b64dec_finish (struct b64state *state);
276 :
277 : /*-- sexputil.c */
278 : char *canon_sexp_to_string (const unsigned char *canon, size_t canonlen);
279 : void log_printcanon (const char *text,
280 : const unsigned char *sexp, size_t sexplen);
281 : void log_printsexp (const char *text, gcry_sexp_t sexp);
282 :
283 : gpg_error_t make_canon_sexp (gcry_sexp_t sexp,
284 : unsigned char **r_buffer, size_t *r_buflen);
285 : gpg_error_t make_canon_sexp_pad (gcry_sexp_t sexp, int secure,
286 : unsigned char **r_buffer, size_t *r_buflen);
287 : gpg_error_t keygrip_from_canon_sexp (const unsigned char *key, size_t keylen,
288 : unsigned char *grip);
289 : int cmp_simple_canon_sexp (const unsigned char *a, const unsigned char *b);
290 : unsigned char *make_simple_sexp_from_hexstr (const char *line,
291 : size_t *nscanned);
292 : int hash_algo_from_sigval (const unsigned char *sigval);
293 : unsigned char *make_canon_sexp_from_rsa_pk (const void *m, size_t mlen,
294 : const void *e, size_t elen,
295 : size_t *r_len);
296 : gpg_error_t get_rsa_pk_from_canon_sexp (const unsigned char *keydata,
297 : size_t keydatalen,
298 : unsigned char const **r_n,
299 : size_t *r_nlen,
300 : unsigned char const **r_e,
301 : size_t *r_elen);
302 : gpg_error_t get_pk_algo_from_canon_sexp (const unsigned char *keydata,
303 : size_t keydatalen,
304 : const char **r_algo);
305 :
306 : /*-- convert.c --*/
307 : int hex2bin (const char *string, void *buffer, size_t length);
308 : int hexcolon2bin (const char *string, void *buffer, size_t length);
309 : char *bin2hex (const void *buffer, size_t length, char *stringbuf);
310 : char *bin2hexcolon (const void *buffer, size_t length, char *stringbuf);
311 : const char *hex2str (const char *hexstring,
312 : char *buffer, size_t bufsize, size_t *buflen);
313 : char *hex2str_alloc (const char *hexstring, size_t *r_count);
314 :
315 : /*-- percent.c --*/
316 : char *percent_plus_escape (const char *string);
317 : char *percent_plus_unescape (const char *string, int nulrepl);
318 : char *percent_unescape (const char *string, int nulrepl);
319 :
320 : size_t percent_plus_unescape_inplace (char *string, int nulrepl);
321 : size_t percent_unescape_inplace (char *string, int nulrepl);
322 :
323 : /*-- openpgp-oid.c --*/
324 : gpg_error_t openpgp_oid_from_str (const char *string, gcry_mpi_t *r_mpi);
325 : char *openpgp_oid_to_str (gcry_mpi_t a);
326 : int openpgp_oid_is_ed25519 (gcry_mpi_t a);
327 : int openpgp_oid_is_crv25519 (gcry_mpi_t a);
328 : const char *openpgp_curve_to_oid (const char *name, unsigned int *r_nbits);
329 : const char *openpgp_oid_to_curve (const char *oid, int canon);
330 : const char *openpgp_enum_curves (int *idxp);
331 :
332 :
333 :
334 : /*-- homedir.c --*/
335 : const char *standard_homedir (void);
336 : const char *default_homedir (void);
337 : const char *gnupg_sysconfdir (void);
338 : const char *gnupg_bindir (void);
339 : const char *gnupg_libexecdir (void);
340 : const char *gnupg_libdir (void);
341 : const char *gnupg_datadir (void);
342 : const char *gnupg_localedir (void);
343 : const char *gnupg_cachedir (void);
344 : const char *dirmngr_sys_socket_name (void);
345 : const char *dirmngr_user_socket_name (void);
346 :
347 : /* All module names. We also include gpg and gpgsm for the sake for
348 : gpgconf. */
349 : #define GNUPG_MODULE_NAME_AGENT 1
350 : #define GNUPG_MODULE_NAME_PINENTRY 2
351 : #define GNUPG_MODULE_NAME_SCDAEMON 3
352 : #define GNUPG_MODULE_NAME_DIRMNGR 4
353 : #define GNUPG_MODULE_NAME_PROTECT_TOOL 5
354 : #define GNUPG_MODULE_NAME_CHECK_PATTERN 6
355 : #define GNUPG_MODULE_NAME_GPGSM 7
356 : #define GNUPG_MODULE_NAME_GPG 8
357 : #define GNUPG_MODULE_NAME_CONNECT_AGENT 9
358 : #define GNUPG_MODULE_NAME_GPGCONF 10
359 : #define GNUPG_MODULE_NAME_DIRMNGR_LDAP 11
360 : const char *gnupg_module_name (int which);
361 : void gnupg_module_name_flush_some (void);
362 :
363 :
364 :
365 : /*-- gpgrlhelp.c --*/
366 : void gnupg_rl_initialize (void);
367 :
368 : /*-- helpfile.c --*/
369 : char *gnupg_get_help_string (const char *key, int only_current_locale);
370 :
371 : /*-- localename.c --*/
372 : const char *gnupg_messages_locale_name (void);
373 :
374 : /*-- miscellaneous.c --*/
375 :
376 : /* This function is called at startup to tell libgcrypt to use our own
377 : logging subsystem. */
378 : void setup_libgcrypt_logging (void);
379 :
380 : /* Same as estream_asprintf but die on memory failure. */
381 : char *xasprintf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2);
382 : /* This is now an alias to estream_asprintf. */
383 : char *xtryasprintf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2);
384 :
385 : /* Replacement for gcry_cipher_algo_name. */
386 : const char *gnupg_cipher_algo_name (int algo);
387 :
388 : void obsolete_option (const char *configname, unsigned int configlineno,
389 : const char *name);
390 :
391 : const char *print_fname_stdout (const char *s);
392 : const char *print_fname_stdin (const char *s);
393 : void print_utf8_buffer3 (estream_t fp, const void *p, size_t n,
394 : const char *delim);
395 : void print_utf8_buffer2 (estream_t fp, const void *p, size_t n, int delim);
396 : void print_utf8_buffer (estream_t fp, const void *p, size_t n);
397 : void print_hexstring (FILE *fp, const void *buffer, size_t length,
398 : int reserved);
399 : char *make_printable_string (const void *p, size_t n, int delim);
400 :
401 : int is_file_compressed (const char *s, int *ret_rc);
402 :
403 : int match_multistr (const char *multistr,const char *match);
404 :
405 : int gnupg_compare_version (const char *a, const char *b);
406 :
407 : struct debug_flags_s
408 : {
409 : unsigned int flag;
410 : const char *name;
411 : };
412 : int parse_debug_flag (const char *string, unsigned int *debugvar,
413 : const struct debug_flags_s *flags);
414 :
415 :
416 : /*-- Simple replacement functions. */
417 :
418 : /* We use the gnupg_ttyname macro to be safe not to run into conflicts
419 : which an extisting but broken ttyname. */
420 : #if !defined(HAVE_TTYNAME) || defined(HAVE_BROKEN_TTYNAME)
421 : # define gnupg_ttyname(n) _gnupg_ttyname ((n))
422 : /* Systems without ttyname (W32) will merely return NULL. */
423 : static inline char *
424 : _gnupg_ttyname (int fd)
425 : {
426 : (void)fd;
427 : return NULL;
428 : }
429 : #else /*HAVE_TTYNAME*/
430 : # define gnupg_ttyname(n) ttyname ((n))
431 : #endif /*HAVE_TTYNAME */
432 :
433 : #ifdef HAVE_W32CE_SYSTEM
434 : #define getpid() GetCurrentProcessId ()
435 : char *_gnupg_getenv (const char *name); /* See sysutils.c */
436 : #define getenv(a) _gnupg_getenv ((a))
437 : char *_gnupg_setenv (const char *name); /* See sysutils.c */
438 : #define setenv(a,b,c) _gnupg_setenv ((a),(b),(c))
439 : int _gnupg_isatty (int fd);
440 : #define gnupg_isatty(a) _gnupg_isatty ((a))
441 : #else
442 : #define gnupg_isatty(a) isatty ((a))
443 : #endif
444 :
445 :
446 :
447 : /*-- Macros to replace ctype ones to avoid locale problems. --*/
448 : #define spacep(p) (*(p) == ' ' || *(p) == '\t')
449 : #define digitp(p) (*(p) >= '0' && *(p) <= '9')
450 : #define hexdigitp(a) (digitp (a) \
451 : || (*(a) >= 'A' && *(a) <= 'F') \
452 : || (*(a) >= 'a' && *(a) <= 'f'))
453 : /* Note this isn't identical to a C locale isspace() without \f and
454 : \v, but works for the purposes used here. */
455 : #define ascii_isspace(a) ((a)==' ' || (a)=='\n' || (a)=='\r' || (a)=='\t')
456 :
457 : /* The atoi macros assume that the buffer has only valid digits. */
458 : #define atoi_1(p) (*(p) - '0' )
459 : #define atoi_2(p) ((atoi_1(p) * 10) + atoi_1((p)+1))
460 : #define atoi_4(p) ((atoi_2(p) * 100) + atoi_2((p)+2))
461 : #define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
462 : *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
463 : #define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
464 : #define xtoi_4(p) ((xtoi_2(p) * 256) + xtoi_2((p)+2))
465 :
466 :
467 : /*-- Forward declaration of the commonly used server control structure. */
468 : /* (We need it here as it is used by some callback prototypes.) */
469 : struct server_control_s;
470 : typedef struct server_control_s *ctrl_t;
471 :
472 :
473 : #endif /*GNUPG_COMMON_UTIL_H*/
|