1 /* misc.c - miscellaneous functions
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 * 2005, 2006 Free Software Foundation, Inc.
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 2 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
29 #if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2
30 #include <asm/sysinfo.h>
31 #include <asm/unistd.h>
36 #include <sys/resource.h>
38 #ifdef ENABLE_SELINUX_HACKS
42 #ifdef HAVE_W32_SYSTEM
48 #define CSIDL_APPDATA 0x001a
50 #ifndef CSIDL_LOCAL_APPDATA
51 #define CSIDL_LOCAL_APPDATA 0x001c
53 #ifndef CSIDL_FLAG_CREATE
54 #define CSIDL_FLAG_CREATE 0x8000
56 #endif /*HAVE_W32_SYSTEM*/
59 #ifdef HAVE_W32_SYSTEM
62 #endif /*HAVE_W32_SYSTEM*/
67 #include "call-agent.h"
72 string_count_chr (const char *string
, int c
)
76 for (count
=0; *string
; string
++ )
84 #ifdef ENABLE_SELINUX_HACKS
85 /* A object and a global variable to keep track of files marked as
87 struct secured_file_item
89 struct secured_file_item
*next
;
93 static struct secured_file_item
*secured_files
;
94 #endif /*ENABLE_SELINUX_HACKS*/
98 #if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2
100 setsysinfo(unsigned long op
, void *buffer
, unsigned long size
,
101 int *start
, void *arg
, unsigned long flag
)
103 return syscall(__NR_osf_setsysinfo
, op
, buffer
, size
, start
, arg
, flag
);
111 buf
[0] = SSIN_UACPROC
;
112 buf
[1] = UAC_SIGBUS
| UAC_NOPRINT
;
113 setsysinfo(SSI_NVPAIRS
, buf
, 1, 0, 0, 0);
126 #ifdef HAVE_DOSISH_SYSTEM
129 #ifdef HAVE_SETRLIMIT
134 if( !setrlimit( RLIMIT_CORE
, &limit
) )
136 if( errno
!= EINVAL
&& errno
!= ENOSYS
)
137 log_fatal(_("can't disable core dumps: %s\n"), strerror(errno
) );
144 /* For the sake of SELinux we want to restrict access through gpg to
145 certain files we keep under our own control. This function
146 registers such a file and is_secured_file may then be used to
147 check whether a file has ben registered as secured. */
149 register_secured_file (const char *fname
)
151 #ifdef ENABLE_SELINUX_HACKS
153 struct secured_file_item
*sf
;
155 /* Note that we stop immediatley if something goes wrong here. */
156 if (stat (fname
, &buf
))
157 log_fatal (_("fstat of `%s' failed in %s: %s\n"), fname
,
158 "register_secured_file", strerror (errno
));
159 /* log_debug ("registering `%s' i=%lu.%lu\n", fname, */
160 /* (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
161 for (sf
=secured_files
; sf
; sf
= sf
->next
)
163 if (sf
->ino
== buf
.st_ino
&& sf
->dev
== buf
.st_dev
)
164 return; /* Already registered. */
167 sf
= xmalloc (sizeof *sf
);
168 sf
->ino
= buf
.st_ino
;
169 sf
->dev
= buf
.st_dev
;
170 sf
->next
= secured_files
;
172 #endif /*ENABLE_SELINUX_HACKS*/
175 /* Remove a file registered as secure. */
177 unregister_secured_file (const char *fname
)
179 #ifdef ENABLE_SELINUX_HACKS
181 struct secured_file_item
*sf
, *sfprev
;
183 if (stat (fname
, &buf
))
185 log_error (_("fstat of `%s' failed in %s: %s\n"), fname
,
186 "unregister_secured_file", strerror (errno
));
189 /* log_debug ("unregistering `%s' i=%lu.%lu\n", fname, */
190 /* (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
191 for (sfprev
=NULL
,sf
=secured_files
; sf
; sfprev
=sf
, sf
= sf
->next
)
193 if (sf
->ino
== buf
.st_ino
&& sf
->dev
== buf
.st_dev
)
196 sfprev
->next
= sf
->next
;
198 secured_files
= sf
->next
;
203 #endif /*ENABLE_SELINUX_HACKS*/
206 /* Return true if FD is corresponds to a secured file. Using -1 for
207 FS is allowed and will return false. */
209 is_secured_file (int fd
)
211 #ifdef ENABLE_SELINUX_HACKS
213 struct secured_file_item
*sf
;
216 return 0; /* No file descriptor so it can't be secured either. */
218 /* Note that we print out a error here and claim that a file is
219 secure if something went wrong. */
220 if (fstat (fd
, &buf
))
222 log_error (_("fstat(%d) failed in %s: %s\n"), fd
,
223 "is_secured_file", strerror (errno
));
226 /* log_debug ("is_secured_file (%d) i=%lu.%lu\n", fd, */
227 /* (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
228 for (sf
=secured_files
; sf
; sf
= sf
->next
)
230 if (sf
->ino
== buf
.st_ino
&& sf
->dev
== buf
.st_dev
)
233 #endif /*ENABLE_SELINUX_HACKS*/
237 /* Return true if FNAME is corresponds to a secured file. Using NULL,
238 "" or "-" for FS is allowed and will return false. This function is
239 used before creating a file, thus it won't fail if the file does
242 is_secured_filename (const char *fname
)
244 #ifdef ENABLE_SELINUX_HACKS
246 struct secured_file_item
*sf
;
248 if (iobuf_is_pipe_filename (fname
) || !*fname
)
251 /* Note that we print out a error here and claim that a file is
252 secure if something went wrong. */
253 if (stat (fname
, &buf
))
255 if (errno
== ENOENT
|| errno
== EPERM
|| errno
== EACCES
)
257 log_error (_("fstat of `%s' failed in %s: %s\n"), fname
,
258 "is_secured_filename", strerror (errno
));
261 /* log_debug ("is_secured_filename (%s) i=%lu.%lu\n", fname, */
262 /* (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
263 for (sf
=secured_files
; sf
; sf
= sf
->next
)
265 if (sf
->ino
== buf
.st_ino
&& sf
->dev
== buf
.st_dev
)
268 #endif /*ENABLE_SELINUX_HACKS*/
275 checksum_u16( unsigned n
)
286 checksum( byte
*p
, unsigned n
)
296 checksum_mpi (gcry_mpi_t a
)
302 if ( gcry_mpi_print (GCRYMPI_FMT_PGP
, NULL
, 0, &nbytes
, a
) )
304 /* Fixme: For numbers not in secure memory we should use a stack
305 * based buffer and only allocate a larger one if mpi_print returns
307 buffer
= (gcry_is_secure(a
)?
308 gcry_xmalloc_secure (nbytes
) : gcry_xmalloc (nbytes
));
309 if ( gcry_mpi_print (GCRYMPI_FMT_PGP
, buffer
, nbytes
, NULL
, a
) )
311 csum
= checksum (buffer
, nbytes
);
317 buffer_to_u32( const byte
*buffer
)
321 a
|= buffer
[1] << 16;
328 print_pubkey_algo_note( int algo
)
330 if(algo
>= 100 && algo
<= 110)
336 log_info (_("WARNING: using experimental public key algorithm %s\n"),
337 gcry_pk_algo_name (algo
));
343 print_cipher_algo_note( int algo
)
345 if(algo
>= 100 && algo
<= 110)
351 log_info (_("WARNING: using experimental cipher algorithm %s\n"),
352 gcry_cipher_algo_name (algo
));
358 print_digest_algo_note( int algo
)
360 if(algo
>= 100 && algo
<= 110)
366 log_info (_("WARNING: using experimental digest algorithm %s\n"),
367 gcry_md_algo_name (algo
));
370 else if(algo
==DIGEST_ALGO_MD5
)
371 log_info (_("WARNING: digest algorithm %s is deprecated\n"),
372 gcry_md_algo_name (algo
));
375 /* Return a string which is used as a kind of process ID */
377 get_session_marker( size_t *rlen
)
379 static byte marker
[SIZEOF_UNSIGNED_LONG
*2];
380 static int initialized
;
384 volatile ulong aa
, bb
; /* We really want the uninitialized value. */
388 /* Although this marker is guessable it is not easy to use this
389 * for a faked control packet because an attacker does not have
390 * enough control about the time the verification takes place.
391 * Of course, we could add just more random but than we need the
392 * random generator even for verification tasks - which does not
394 a
= aa
^ (ulong
)getpid();
395 b
= bb
^ (ulong
)time(NULL
);
396 memcpy ( marker
, &a
, SIZEOF_UNSIGNED_LONG
);
397 memcpy ( marker
+SIZEOF_UNSIGNED_LONG
, &b
, SIZEOF_UNSIGNED_LONG
);
399 *rlen
= sizeof(marker
);
404 * Wrapper around the libgcrypt function with additonal checks on
405 * the OpenPGP contraints for the algo ID.
408 openpgp_cipher_test_algo( int algo
)
410 if ( algo
< 0 || algo
> 110 )
411 return gpg_error (GPG_ERR_CIPHER_ALGO
);
412 return gcry_cipher_test_algo (algo
);
416 openpgp_pk_test_algo( int algo
)
418 if (algo
== GCRY_PK_ELG_E
)
421 if (algo
< 0 || algo
> 110)
422 return gpg_error (GPG_ERR_PUBKEY_ALGO
);
423 return gcry_pk_test_algo (algo
);
427 openpgp_pk_test_algo2( int algo
, unsigned int use
)
429 size_t use_buf
= use
;
431 if (algo
== GCRY_PK_ELG_E
)
434 if (algo
< 0 || algo
> 110)
435 return gpg_error (GPG_ERR_PUBKEY_ALGO
);
437 return gcry_pk_algo_info (algo
, GCRYCTL_TEST_ALGO
, NULL
, &use_buf
);
441 openpgp_pk_algo_usage ( int algo
)
445 /* They are hardwired in gpg 1.0. */
447 case PUBKEY_ALGO_RSA
:
448 use
= (PUBKEY_USAGE_CERT
| PUBKEY_USAGE_SIG
449 | PUBKEY_USAGE_ENC
| PUBKEY_USAGE_AUTH
);
451 case PUBKEY_ALGO_RSA_E
:
452 use
= PUBKEY_USAGE_ENC
;
454 case PUBKEY_ALGO_RSA_S
:
455 use
= PUBKEY_USAGE_CERT
| PUBKEY_USAGE_SIG
;
457 case PUBKEY_ALGO_ELGAMAL_E
:
458 use
= PUBKEY_USAGE_ENC
;
460 case PUBKEY_ALGO_DSA
:
461 use
= PUBKEY_USAGE_CERT
| PUBKEY_USAGE_SIG
| PUBKEY_USAGE_AUTH
;
470 openpgp_md_test_algo( int algo
)
472 if (algo
< 0 || algo
> 110)
473 return gpg_error (GPG_ERR_DIGEST_ALGO
);
474 return gcry_md_test_algo (algo
);
478 /* Special warning for the IDEA cipher */
480 idea_cipher_warn(int show
)
486 log_info(_("the IDEA cipher plugin is not present\n"));
487 log_info(_("please see %s for more information\n"),
488 "http://www.gnupg.org/faq/why-not-idea.html");
496 get_signature_count (PKT_secret_key
*sk
)
498 #ifdef ENABLE_CARD_SUPPORT
499 if(sk
&& sk
->is_protected
&& sk
->protect
.s2k
.mode
==1002)
501 struct agent_card_info_s info
;
502 if(agent_scd_getattr("SIG-COUNTER",&info
)==0)
503 return info
.sig_counter
;
507 /* How to do this without a card? */
512 /* Expand %-strings. Returns a string which must be xfreed. Returns
513 NULL if the string cannot be expanded (too large). */
515 pct_expando(const char *string
,struct expando_args
*args
)
517 const char *ch
=string
;
518 int idx
=0,maxlen
=0,done
=0;
519 u32 pk_keyid
[2]={0,0},sk_keyid
[2]={0,0};
523 keyid_from_pk(args
->pk
,pk_keyid
);
526 keyid_from_sk(args
->sk
,sk_keyid
);
528 /* This is used so that %k works in photoid command strings in
529 --list-secret-keys (which of course has a sk, but no pk). */
530 if(!args
->pk
&& args
->sk
)
531 keyid_from_sk(args
->sk
,pk_keyid
);
539 /* 8192 is way bigger than we'll need here */
544 ret
=xrealloc(ret
,maxlen
);
553 case 's': /* short key id */
556 sprintf(&ret
[idx
],"%08lX",(ulong
)sk_keyid
[1]);
562 case 'S': /* long key id */
565 sprintf(&ret
[idx
],"%08lX%08lX",
566 (ulong
)sk_keyid
[0],(ulong
)sk_keyid
[1]);
572 case 'k': /* short key id */
575 sprintf(&ret
[idx
],"%08lX",(ulong
)pk_keyid
[1]);
581 case 'K': /* long key id */
584 sprintf(&ret
[idx
],"%08lX%08lX",
585 (ulong
)pk_keyid
[0],(ulong
)pk_keyid
[1]);
591 case 'c': /* signature count from card, if any. */
594 sprintf(&ret
[idx
],"%lu",get_signature_count(args
->sk
));
595 idx
+=strlen(&ret
[idx
]);
600 case 'p': /* primary pk fingerprint of a sk */
601 case 'f': /* pk fingerprint */
602 case 'g': /* sk fingerprint */
604 byte array
[MAX_FINGERPRINT_LEN
];
608 if((*(ch
+1))=='p' && args
->sk
)
610 if(args
->sk
->is_primary
)
611 fingerprint_from_sk(args
->sk
,array
,&len
);
612 else if(args
->sk
->main_keyid
[0] || args
->sk
->main_keyid
[1])
615 xmalloc_clear(sizeof(PKT_public_key
));
617 if(get_pubkey_fast(pk
,args
->sk
->main_keyid
)==0)
618 fingerprint_from_pk(pk
,array
,&len
);
620 memset(array
,0,(len
=MAX_FINGERPRINT_LEN
));
624 memset(array
,0,(len
=MAX_FINGERPRINT_LEN
));
626 else if((*(ch
+1))=='f' && args
->pk
)
627 fingerprint_from_pk(args
->pk
,array
,&len
);
628 else if((*(ch
+1))=='g' && args
->sk
)
629 fingerprint_from_sk(args
->sk
,array
,&len
);
631 memset(array
,0,(len
=MAX_FINGERPRINT_LEN
));
633 if(idx
+(len
*2)<maxlen
)
637 sprintf(&ret
[idx
],"%02X",array
[i
]);
645 case 't': /* e.g. "jpg" */
646 str
=image_type_to_string(args
->imagetype
,0);
649 case 'T': /* e.g. "image/jpeg" */
651 str
=image_type_to_string(args
->imagetype
,2);
653 if(idx
+strlen(str
)<maxlen
)
655 strcpy(&ret
[idx
],str
);
670 /* Any unknown %-keys (like %i, %o, %I, and %O) are
671 passed through for later expansion. Note this also
672 handles the case where the last character in the
673 string is a '%' - the terminating \0 will end up here
674 and properly terminate the string. */
711 deprecated_warning(const char *configname
,unsigned int configlineno
,
712 const char *option
,const char *repl1
,const char *repl2
)
716 if(strncmp("--",option
,2)==0)
719 if(strncmp("--",repl1
,2)==0)
722 log_info(_("%s:%d: deprecated option \"%s\"\n"),
723 configname
,configlineno
,option
);
726 log_info(_("WARNING: \"%s\" is a deprecated option\n"),option
);
728 log_info(_("please use \"%s%s\" instead\n"),repl1
,repl2
);
733 deprecated_command (const char *name
)
735 log_info(_("WARNING: \"%s\" is a deprecated command - do not use it\n"),
741 * Wrapper around gcry_cipher_map_name to provide a fallback using the
742 * "Sn" syntax as used by the preference strings.
745 string_to_cipher_algo (const char *string
)
749 val
= gcry_cipher_map_name (string
);
750 if (!val
&& string
&& (string
[0]=='S' || string
[0]=='s'))
755 val
= strtol (string
, &endptr
, 10);
756 if (!*string
|| *endptr
|| openpgp_cipher_test_algo (val
))
764 * Wrapper around gcry_md_map_name to provide a fallback using the
765 * "Hn" syntax as used by the preference strings.
768 string_to_digest_algo (const char *string
)
772 val
= gcry_md_map_name (string
);
773 if (!val
&& string
&& (string
[0]=='H' || string
[0]=='h'))
778 val
= strtol (string
, &endptr
, 10);
779 if (!*string
|| *endptr
|| openpgp_md_test_algo (val
))
789 compress_algo_to_string(int algo
)
795 case COMPRESS_ALGO_NONE
:
799 case COMPRESS_ALGO_ZIP
:
803 case COMPRESS_ALGO_ZLIB
:
808 case COMPRESS_ALGO_BZIP2
:
818 string_to_compress_algo(const char *string
)
820 /* TRANSLATORS: See doc/TRANSLATE about this string. */
821 if(match_multistr(_("uncompressed|none"),string
))
823 else if(ascii_strcasecmp(string
,"uncompressed")==0)
825 else if(ascii_strcasecmp(string
,"none")==0)
827 else if(ascii_strcasecmp(string
,"zip")==0)
829 else if(ascii_strcasecmp(string
,"zlib")==0)
832 else if(ascii_strcasecmp(string
,"bzip2")==0)
835 else if(ascii_strcasecmp(string
,"z0")==0)
837 else if(ascii_strcasecmp(string
,"z1")==0)
839 else if(ascii_strcasecmp(string
,"z2")==0)
842 else if(ascii_strcasecmp(string
,"z3")==0)
850 check_compress_algo(int algo
)
853 if(algo
>=0 && algo
<=3)
856 if(algo
>=0 && algo
<=2)
860 return G10ERR_COMPR_ALGO
;
864 default_cipher_algo(void)
866 if(opt
.def_cipher_algo
)
867 return opt
.def_cipher_algo
;
868 else if(opt
.personal_cipher_prefs
)
869 return opt
.personal_cipher_prefs
[0].value
;
871 return opt
.s2k_cipher_algo
;
874 /* There is no default_digest_algo function, but see
878 default_compress_algo(void)
880 if(opt
.compress_algo
!=-1)
881 return opt
.compress_algo
;
882 else if(opt
.personal_compress_prefs
)
883 return opt
.personal_compress_prefs
[0].value
;
885 return DEFAULT_COMPRESS_ALGO
;
889 compliance_option_string(void)
891 switch(opt
.compliance
)
909 compliance_string(void)
911 switch(opt
.compliance
)
929 compliance_failure(void)
931 log_info(_("this message may not be usable by %s\n"),compliance_string());
932 opt
.compliance
=CO_GNUPG
;
935 /* Break a string into successive option pieces. Accepts single word
936 options and key=value argument options. */
938 optsep(char **stringp
)
945 end
=strpbrk(tok
," ,=");
951 /* what we need to do now is scan along starting with *end,
952 If the next character we see (ignoring spaces) is an =
953 sign, then there is an argument. */
964 /* There is an argument, so grab that too. At this point,
965 ptr points to the first character of the argument. */
968 /* Is it a quoted argument? */
977 end
=strpbrk(ptr
," ,");
995 /* Breaks an option value into key and value. Returns NULL if there
996 is no value. Note that "string" is modified to remove the =value
999 argsplit(char *string
)
1001 char *equals
,*arg
=NULL
;
1003 equals
=strchr(string
,'=');
1012 quote
=strchr(arg
,'"');
1017 quote
=strchr(arg
,'"');
1025 /* Trim leading spaces off of the arg */
1026 spaces
=strspn(arg
," ");
1030 /* Trim tailing spaces off of the tag */
1031 space
=strchr(string
,' ');
1039 /* Return the length of the initial token, leaving off any
1042 optlen(const char *s
)
1044 char *end
=strpbrk(s
," =");
1053 parse_options(char *str
,unsigned int *options
,
1054 struct parse_options
*opts
,int noisy
)
1058 if (str
&& !strcmp (str
, "help"))
1062 /* Figure out the longest option name so we can line these up
1064 for(i
=0;opts
[i
].name
;i
++)
1065 if(opts
[i
].help
&& maxlen
<strlen(opts
[i
].name
))
1066 maxlen
=strlen(opts
[i
].name
);
1068 for(i
=0;opts
[i
].name
;i
++)
1070 printf("%s%*s%s\n",opts
[i
].name
,
1071 maxlen
+2-(int)strlen(opts
[i
].name
),"",_(opts
[i
].help
));
1076 while((tok
=optsep(&str
)))
1084 if(ascii_strncasecmp("no-",tok
,3)==0)
1090 for(i
=0;opts
[i
].name
;i
++)
1092 size_t toklen
=optlen(tok
);
1094 if(ascii_strncasecmp(opts
[i
].name
,tok
,toklen
)==0)
1096 /* We have a match, but it might be incomplete */
1097 if(toklen
!=strlen(opts
[i
].name
))
1101 for(j
=i
+1;opts
[j
].name
;j
++)
1103 if(ascii_strncasecmp(opts
[j
].name
,tok
,toklen
)==0)
1106 log_info(_("ambiguous option `%s'\n"),otok
);
1114 *options
&=~opts
[i
].bit
;
1116 *opts
[i
].value
=NULL
;
1120 *options
|=opts
[i
].bit
;
1122 *opts
[i
].value
=argsplit(tok
);
1131 log_info(_("unknown option `%s'\n"),otok
);
1140 /* Return a new malloced string by unescaping the string S. Escaping
1141 is percent escaping and '+'/space mapping. A binary nul will
1142 silently be replaced by a 0xFF. */
1144 unescape_percent_string (const unsigned char *s
)
1148 buffer
= d
= xmalloc (strlen (s
)+1);
1151 if (*s
== '%' && s
[1] && s
[2])
1174 has_invalid_email_chars (const char *s
)
1177 const char *valid_chars
=
1178 "01234567890_-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
1186 else if ( !at_seen
&& !( !!strchr( valid_chars
, *s
) || *s
== '+' ) )
1188 else if ( at_seen
&& !strchr( valid_chars
, *s
) )
1195 /* Check whether NAME represents a valid mailbox according to
1196 RFC822. Returns true if so. */
1198 is_valid_mailbox (const char *name
)
1202 || has_invalid_email_chars (name
)
1203 || string_count_chr (name
,'@') != 1
1205 || name
[strlen(name
)-1] == '@'
1206 || name
[strlen(name
)-1] == '.'
1207 || strstr (name
, "..") );
1211 /* This is a helper function to load a Windows function from either of
1213 #ifdef HAVE_W32_SYSTEM
1215 w32_shgetfolderpath (HWND a
, int b
, HANDLE c
, DWORD d
, LPSTR e
)
1217 static int initialized
;
1218 static HRESULT (WINAPI
* func
)(HWND
,int,HANDLE
,DWORD
,LPSTR
);
1222 static char *dllnames
[] = { "shell32.dll", "shfolder.dll", NULL
};
1228 for (i
=0, handle
= NULL
; !handle
&& dllnames
[i
]; i
++)
1230 handle
= dlopen (dllnames
[i
], RTLD_LAZY
);
1233 func
= dlsym (handle
, "SHGetFolderPathA");
1244 return func (a
,b
,c
,d
,e
);
1248 #endif /*HAVE_W32_SYSTEM*/
1251 /* Return the name of the libexec directory. The name is allocated in
1252 a static area on the first use. This function won't fail. */
1254 get_libexecdir (void)
1256 #ifdef HAVE_W32_SYSTEM
1258 static char dir
[MAX_PATH
+5];
1264 if ( !GetModuleFileName ( NULL
, dir
, MAX_PATH
) )
1266 log_debug ("GetModuleFileName failed: %s\n", w32_strerror (0));
1270 p
= strrchr (dir
, DIRSEP_C
);
1275 log_debug ("bad filename `%s' returned for this process\n", dir
);
1282 /* Fallback to the hardwired value. */
1283 #endif /*HAVE_W32_SYSTEM*/
1285 return GNUPG_LIBEXECDIR
;
1288 /* Similar to access(2), but uses PATH to find the file. */
1290 path_access(const char *file
,int mode
)
1295 envpath
=getenv("PATH");
1298 #ifdef HAVE_DRIVE_LETTERS
1299 || (((file
[0]>='A' && file
[0]<='Z')
1300 || (file
[0]>='a' && file
[0]<='z'))
1306 return access(file
,mode
);
1309 /* At least as large as, but most often larger than we need. */
1310 char *buffer
=xmalloc(strlen(envpath
)+1+strlen(file
)+1);
1311 char *split
,*item
,*path
=xstrdup(envpath
);
1315 while((item
=strsep(&split
,PATHSEP_S
)))
1317 strcpy(buffer
,item
);
1319 strcat(buffer
,file
);
1320 ret
=access(buffer
,mode
);
1334 /* Temporary helper. */
1336 pubkey_get_npkey( int algo
)
1340 if (algo
== GCRY_PK_ELG_E
)
1342 if (gcry_pk_algo_info( algo
, GCRYCTL_GET_ALGO_NPKEY
, NULL
, &n
))
1347 /* Temporary helper. */
1349 pubkey_get_nskey( int algo
)
1353 if (algo
== GCRY_PK_ELG_E
)
1355 if (gcry_pk_algo_info( algo
, GCRYCTL_GET_ALGO_NSKEY
, NULL
, &n
))
1360 /* Temporary helper. */
1362 pubkey_get_nsig( int algo
)
1366 if (algo
== GCRY_PK_ELG_E
)
1368 if (gcry_pk_algo_info( algo
, GCRYCTL_GET_ALGO_NSIGN
, NULL
, &n
))
1373 /* Temporary helper. */
1375 pubkey_get_nenc( int algo
)
1379 if (algo
== GCRY_PK_ELG_E
)
1381 if (gcry_pk_algo_info( algo
, GCRYCTL_GET_ALGO_NENCR
, NULL
, &n
))
1387 /* Temporary helper. */
1389 pubkey_nbits( int algo
, gcry_mpi_t
*key
)
1394 if( algo
== GCRY_PK_DSA
) {
1395 rc
= gcry_sexp_build ( &sexp
, NULL
,
1396 "(public-key(dsa(p%m)(q%m)(g%m)(y%m)))",
1397 key
[0], key
[1], key
[2], key
[3] );
1399 else if( algo
== GCRY_PK_ELG
|| algo
== GCRY_PK_ELG_E
) {
1400 rc
= gcry_sexp_build ( &sexp
, NULL
,
1401 "(public-key(elg(p%m)(g%m)(y%m)))",
1402 key
[0], key
[1], key
[2] );
1404 else if( algo
== GCRY_PK_RSA
) {
1405 rc
= gcry_sexp_build ( &sexp
, NULL
,
1406 "(public-key(rsa(n%m)(e%m)))",
1415 nbits
= gcry_pk_get_nbits( sexp
);
1416 gcry_sexp_release( sexp
);
1422 /* FIXME: Use gcry_mpi_print directly. */
1424 mpi_print( FILE *fp
, gcry_mpi_t a
, int mode
)
1429 return fprintf(fp
, "[MPI_NULL]");
1432 n1
= gcry_mpi_get_nbits(a
);
1433 n
+= fprintf(fp
, "[%u bits]", n1
);
1436 unsigned char *buffer
;
1438 if (gcry_mpi_aprint (GCRYMPI_FMT_HEX
, &buffer
, NULL
, a
))
1440 fputs( buffer
, fp
);
1441 n
+= strlen(buffer
);
1442 gcry_free( buffer
);