1 /* misc.c - miscellaneous functions
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 * 2008 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 3 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, see <http://www.gnu.org/licenses/>.
27 #if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2
28 #include <asm/sysinfo.h>
29 #include <asm/unistd.h>
34 #include <sys/resource.h>
36 #ifdef ENABLE_SELINUX_HACKS
40 #ifdef HAVE_W32_SYSTEM
46 #define CSIDL_APPDATA 0x001a
48 #ifndef CSIDL_LOCAL_APPDATA
49 #define CSIDL_LOCAL_APPDATA 0x001c
51 #ifndef CSIDL_FLAG_CREATE
52 #define CSIDL_FLAG_CREATE 0x8000
54 #endif /*HAVE_W32_SYSTEM*/
57 #ifdef HAVE_W32_SYSTEM
59 #endif /*HAVE_W32_SYSTEM*/
64 #include "call-agent.h"
69 string_count_chr (const char *string
, int c
)
73 for (count
=0; *string
; string
++ )
81 #ifdef ENABLE_SELINUX_HACKS
82 /* A object and a global variable to keep track of files marked as
84 struct secured_file_item
86 struct secured_file_item
*next
;
90 static struct secured_file_item
*secured_files
;
91 #endif /*ENABLE_SELINUX_HACKS*/
96 /* For the sake of SELinux we want to restrict access through gpg to
97 certain files we keep under our own control. This function
98 registers such a file and is_secured_file may then be used to
99 check whether a file has ben registered as secured. */
101 register_secured_file (const char *fname
)
103 #ifdef ENABLE_SELINUX_HACKS
105 struct secured_file_item
*sf
;
107 /* Note that we stop immediatley if something goes wrong here. */
108 if (stat (fname
, &buf
))
109 log_fatal (_("fstat of `%s' failed in %s: %s\n"), fname
,
110 "register_secured_file", strerror (errno
));
111 /* log_debug ("registering `%s' i=%lu.%lu\n", fname, */
112 /* (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
113 for (sf
=secured_files
; sf
; sf
= sf
->next
)
115 if (sf
->ino
== buf
.st_ino
&& sf
->dev
== buf
.st_dev
)
116 return; /* Already registered. */
119 sf
= xmalloc (sizeof *sf
);
120 sf
->ino
= buf
.st_ino
;
121 sf
->dev
= buf
.st_dev
;
122 sf
->next
= secured_files
;
124 #else /*!ENABLE_SELINUX_HACKS*/
126 #endif /*!ENABLE_SELINUX_HACKS*/
129 /* Remove a file registered as secure. */
131 unregister_secured_file (const char *fname
)
133 #ifdef ENABLE_SELINUX_HACKS
135 struct secured_file_item
*sf
, *sfprev
;
137 if (stat (fname
, &buf
))
139 log_error (_("fstat of `%s' failed in %s: %s\n"), fname
,
140 "unregister_secured_file", strerror (errno
));
143 /* log_debug ("unregistering `%s' i=%lu.%lu\n", fname, */
144 /* (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
145 for (sfprev
=NULL
,sf
=secured_files
; sf
; sfprev
=sf
, sf
= sf
->next
)
147 if (sf
->ino
== buf
.st_ino
&& sf
->dev
== buf
.st_dev
)
150 sfprev
->next
= sf
->next
;
152 secured_files
= sf
->next
;
157 #else /*!ENABLE_SELINUX_HACKS*/
159 #endif /*!ENABLE_SELINUX_HACKS*/
162 /* Return true if FD is corresponds to a secured file. Using -1 for
163 FS is allowed and will return false. */
165 is_secured_file (int fd
)
167 #ifdef ENABLE_SELINUX_HACKS
169 struct secured_file_item
*sf
;
172 return 0; /* No file descriptor so it can't be secured either. */
174 /* Note that we print out a error here and claim that a file is
175 secure if something went wrong. */
176 if (fstat (fd
, &buf
))
178 log_error (_("fstat(%d) failed in %s: %s\n"), fd
,
179 "is_secured_file", strerror (errno
));
182 /* log_debug ("is_secured_file (%d) i=%lu.%lu\n", fd, */
183 /* (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
184 for (sf
=secured_files
; sf
; sf
= sf
->next
)
186 if (sf
->ino
== buf
.st_ino
&& sf
->dev
== buf
.st_dev
)
189 #else /*!ENABLE_SELINUX_HACKS*/
191 #endif /*!ENABLE_SELINUX_HACKS*/
195 /* Return true if FNAME is corresponds to a secured file. Using NULL,
196 "" or "-" for FS is allowed and will return false. This function is
197 used before creating a file, thus it won't fail if the file does
200 is_secured_filename (const char *fname
)
202 #ifdef ENABLE_SELINUX_HACKS
204 struct secured_file_item
*sf
;
206 if (iobuf_is_pipe_filename (fname
) || !*fname
)
209 /* Note that we print out a error here and claim that a file is
210 secure if something went wrong. */
211 if (stat (fname
, &buf
))
213 if (errno
== ENOENT
|| errno
== EPERM
|| errno
== EACCES
)
215 log_error (_("fstat of `%s' failed in %s: %s\n"), fname
,
216 "is_secured_filename", strerror (errno
));
219 /* log_debug ("is_secured_filename (%s) i=%lu.%lu\n", fname, */
220 /* (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
221 for (sf
=secured_files
; sf
; sf
= sf
->next
)
223 if (sf
->ino
== buf
.st_ino
&& sf
->dev
== buf
.st_dev
)
226 #else /*!ENABLE_SELINUX_HACKS*/
228 #endif /*!ENABLE_SELINUX_HACKS*/
235 checksum_u16( unsigned n
)
246 checksum( byte
*p
, unsigned n
)
256 checksum_mpi (gcry_mpi_t a
)
262 if ( gcry_mpi_print (GCRYMPI_FMT_PGP
, NULL
, 0, &nbytes
, a
) )
264 /* Fixme: For numbers not in secure memory we should use a stack
265 * based buffer and only allocate a larger one if mpi_print returns
267 buffer
= (gcry_is_secure(a
)?
268 gcry_xmalloc_secure (nbytes
) : gcry_xmalloc (nbytes
));
269 if ( gcry_mpi_print (GCRYMPI_FMT_PGP
, buffer
, nbytes
, NULL
, a
) )
271 csum
= checksum (buffer
, nbytes
);
277 buffer_to_u32( const byte
*buffer
)
281 a
|= buffer
[1] << 16;
288 print_pubkey_algo_note( int algo
)
290 if(algo
>= 100 && algo
<= 110)
296 log_info (_("WARNING: using experimental public key algorithm %s\n"),
297 gcry_pk_algo_name (algo
));
302 log_info (_("WARNING: Elgamal sign+encrypt keys are deprecated\n"));
307 print_cipher_algo_note( int algo
)
309 if(algo
>= 100 && algo
<= 110)
315 log_info (_("WARNING: using experimental cipher algorithm %s\n"),
316 openpgp_cipher_algo_name (algo
));
322 print_digest_algo_note( int algo
)
324 if(algo
>= 100 && algo
<= 110)
330 log_info (_("WARNING: using experimental digest algorithm %s\n"),
331 gcry_md_algo_name (algo
));
334 else if(algo
==DIGEST_ALGO_MD5
)
335 log_info (_("WARNING: digest algorithm %s is deprecated\n"),
336 gcry_md_algo_name (algo
));
340 /* Map OpenPGP algo numbers to those used by Libgcrypt. We need to do
341 this for algorithms we implemented in Libgcrypt after they become
344 map_cipher_openpgp_to_gcry (int algo
)
348 case CIPHER_ALGO_CAMELLIA128
: return 310;
349 case CIPHER_ALGO_CAMELLIA192
: return 311;
350 case CIPHER_ALGO_CAMELLIA256
: return 312;
351 default: return algo
;
355 /* The inverse fucntion of above. */
357 map_cipher_gcry_to_openpgp (int algo
)
361 case 310: return CIPHER_ALGO_CAMELLIA128
;
362 case 311: return CIPHER_ALGO_CAMELLIA192
;
363 case 312: return CIPHER_ALGO_CAMELLIA256
;
364 default: return algo
;
369 /* Return the block length of an OpenPGP cipher algorithm. */
371 openpgp_cipher_blocklen (int algo
)
373 /* We use the numbers from OpenPGP to be sure that we get the right
374 block length. This is so that the packet parsing code works even
375 for unknown algorithms (for which we assume 8 due to tradition).
377 NOTE: If you change the the returned blocklen above 16, check
378 the callers because they may use a fixed size buffer of that
382 case 7: case 8: case 9: /* AES */
383 case 10: /* Twofish */
384 case 11: case 12: case 13: /* Camellia */
393 * Wrapper around the libgcrypt function with additonal checks on
394 * the OpenPGP contraints for the algo ID.
397 openpgp_cipher_test_algo( int algo
)
399 /* (5 and 6 are marked reserved by rfc4880.) */
400 if ( algo
< 0 || algo
> 110 || algo
== 5 || algo
== 6 )
401 return gpg_error (GPG_ERR_CIPHER_ALGO
);
403 /* Camellia is not yet defined for OpenPGP thus only allow it if
406 if (algo
== CIPHER_ALGO_CAMELLIA128
407 || algo
== CIPHER_ALGO_CAMELLIA192
408 || algo
== CIPHER_ALGO_CAMELLIA256
)
409 return gpg_error (GPG_ERR_CIPHER_ALGO
);
412 return gcry_cipher_test_algo (map_cipher_openpgp_to_gcry (algo
));
415 /* Map the OpenPGP cipher algorithm whose ID is contained in ALGORITHM to a
416 string representation of the algorithm name. For unknown algorithm
417 IDs this function returns "?". */
419 openpgp_cipher_algo_name (int algo
)
421 return gcry_cipher_algo_name (map_cipher_openpgp_to_gcry (algo
));
425 openpgp_pk_test_algo( int algo
)
427 /* Dont't allow type 20 keys unless in rfc2440 mode. */
428 if (!RFC2440
&& algo
== 20)
429 return gpg_error (GPG_ERR_PUBKEY_ALGO
);
431 if (algo
== GCRY_PK_ELG_E
)
434 if (algo
< 0 || algo
> 110)
435 return gpg_error (GPG_ERR_PUBKEY_ALGO
);
436 return gcry_pk_test_algo (algo
);
440 openpgp_pk_test_algo2( int algo
, unsigned int use
)
442 size_t use_buf
= use
;
444 /* Dont't allow type 20 keys unless in rfc2440 mode. */
445 if (!RFC2440
&& algo
== 20)
446 return gpg_error (GPG_ERR_PUBKEY_ALGO
);
448 if (algo
== GCRY_PK_ELG_E
)
451 if (algo
< 0 || algo
> 110)
452 return gpg_error (GPG_ERR_PUBKEY_ALGO
);
454 return gcry_pk_algo_info (algo
, GCRYCTL_TEST_ALGO
, NULL
, &use_buf
);
458 openpgp_pk_algo_usage ( int algo
)
462 /* They are hardwired in gpg 1.0. */
464 case PUBKEY_ALGO_RSA
:
465 use
= (PUBKEY_USAGE_CERT
| PUBKEY_USAGE_SIG
466 | PUBKEY_USAGE_ENC
| PUBKEY_USAGE_AUTH
);
468 case PUBKEY_ALGO_RSA_E
:
469 use
= PUBKEY_USAGE_ENC
;
471 case PUBKEY_ALGO_RSA_S
:
472 use
= PUBKEY_USAGE_CERT
| PUBKEY_USAGE_SIG
;
474 case PUBKEY_ALGO_ELGAMAL
:
476 use
= PUBKEY_USAGE_ENC
;
478 case PUBKEY_ALGO_ELGAMAL_E
:
479 use
= PUBKEY_USAGE_ENC
;
481 case PUBKEY_ALGO_DSA
:
482 use
= PUBKEY_USAGE_CERT
| PUBKEY_USAGE_SIG
| PUBKEY_USAGE_AUTH
;
491 openpgp_md_test_algo( int algo
)
493 /* Note: If the list of actual supported OpenPGP algorithms changes,
494 make sure that our hard coded values at
495 print_status_begin_signing() gets updated. */
496 /* 4, 5, 6, 7 are defined by rfc2440 but will be removed from the
497 next revision of the standard. */
498 if (algo
< 0 || algo
> 110 || (algo
>= 4 && algo
<= 7))
499 return gpg_error (GPG_ERR_DIGEST_ALGO
);
500 return gcry_md_test_algo (algo
);
504 /* Special warning for the IDEA cipher */
506 idea_cipher_warn(int show
)
512 log_info(_("the IDEA cipher plugin is not present\n"));
513 log_info(_("please see %s for more information\n"),
514 "http://www.gnupg.org/faq/why-not-idea.html");
522 get_signature_count (PKT_secret_key
*sk
)
524 #ifdef ENABLE_CARD_SUPPORT
525 if(sk
&& sk
->is_protected
&& sk
->protect
.s2k
.mode
==1002)
527 struct agent_card_info_s info
;
528 if(agent_scd_getattr("SIG-COUNTER",&info
)==0)
529 return info
.sig_counter
;
533 /* How to do this without a card? */
538 /* Expand %-strings. Returns a string which must be xfreed. Returns
539 NULL if the string cannot be expanded (too large). */
541 pct_expando(const char *string
,struct expando_args
*args
)
543 const char *ch
=string
;
544 int idx
=0,maxlen
=0,done
=0;
545 u32 pk_keyid
[2]={0,0},sk_keyid
[2]={0,0};
549 keyid_from_pk(args
->pk
,pk_keyid
);
552 keyid_from_sk(args
->sk
,sk_keyid
);
554 /* This is used so that %k works in photoid command strings in
555 --list-secret-keys (which of course has a sk, but no pk). */
556 if(!args
->pk
&& args
->sk
)
557 keyid_from_sk(args
->sk
,pk_keyid
);
563 /* 8192 is way bigger than we'll need here */
568 ret
=xrealloc(ret
,maxlen
);
577 case 's': /* short key id */
580 sprintf(&ret
[idx
],"%08lX",(ulong
)sk_keyid
[1]);
586 case 'S': /* long key id */
589 sprintf(&ret
[idx
],"%08lX%08lX",
590 (ulong
)sk_keyid
[0],(ulong
)sk_keyid
[1]);
596 case 'k': /* short key id */
599 sprintf(&ret
[idx
],"%08lX",(ulong
)pk_keyid
[1]);
605 case 'K': /* long key id */
608 sprintf(&ret
[idx
],"%08lX%08lX",
609 (ulong
)pk_keyid
[0],(ulong
)pk_keyid
[1]);
615 case 'c': /* signature count from card, if any. */
618 sprintf(&ret
[idx
],"%lu",get_signature_count(args
->sk
));
619 idx
+=strlen(&ret
[idx
]);
624 case 'p': /* primary pk fingerprint of a sk */
625 case 'f': /* pk fingerprint */
626 case 'g': /* sk fingerprint */
628 byte array
[MAX_FINGERPRINT_LEN
];
632 if((*(ch
+1))=='p' && args
->sk
)
634 if(args
->sk
->is_primary
)
635 fingerprint_from_sk(args
->sk
,array
,&len
);
636 else if(args
->sk
->main_keyid
[0] || args
->sk
->main_keyid
[1])
639 xmalloc_clear(sizeof(PKT_public_key
));
641 if(get_pubkey_fast(pk
,args
->sk
->main_keyid
)==0)
642 fingerprint_from_pk(pk
,array
,&len
);
644 memset(array
,0,(len
=MAX_FINGERPRINT_LEN
));
648 memset(array
,0,(len
=MAX_FINGERPRINT_LEN
));
650 else if((*(ch
+1))=='f' && args
->pk
)
651 fingerprint_from_pk(args
->pk
,array
,&len
);
652 else if((*(ch
+1))=='g' && args
->sk
)
653 fingerprint_from_sk(args
->sk
,array
,&len
);
655 memset(array
,0,(len
=MAX_FINGERPRINT_LEN
));
657 if(idx
+(len
*2)<maxlen
)
661 sprintf(&ret
[idx
],"%02X",array
[i
]);
669 case 'v': /* validity letters */
670 if(args
->validity_info
&& idx
+1<maxlen
)
672 ret
[idx
++]=args
->validity_info
;
678 /* The text string types */
683 const char *str
=NULL
;
687 case 't': /* e.g. "jpg" */
688 str
=image_type_to_string(args
->imagetype
,0);
691 case 'T': /* e.g. "image/jpeg" */
692 str
=image_type_to_string(args
->imagetype
,2);
695 case 'V': /* e.g. "full", "expired", etc. */
696 str
=args
->validity_string
;
700 if(str
&& idx
+strlen(str
)<maxlen
)
702 strcpy(&ret
[idx
],str
);
718 /* Any unknown %-keys (like %i, %o, %I, and %O) are
719 passed through for later expansion. Note this also
720 handles the case where the last character in the
721 string is a '%' - the terminating \0 will end up here
722 and properly terminate the string. */
759 deprecated_warning(const char *configname
,unsigned int configlineno
,
760 const char *option
,const char *repl1
,const char *repl2
)
764 if(strncmp("--",option
,2)==0)
767 if(strncmp("--",repl1
,2)==0)
770 log_info(_("%s:%d: deprecated option \"%s\"\n"),
771 configname
,configlineno
,option
);
774 log_info(_("WARNING: \"%s\" is a deprecated option\n"),option
);
776 log_info(_("please use \"%s%s\" instead\n"),repl1
,repl2
);
781 deprecated_command (const char *name
)
783 log_info(_("WARNING: \"%s\" is a deprecated command - do not use it\n"),
789 obsolete_option (const char *configname
, unsigned int configlineno
,
793 log_info (_("%s:%u: obsolete option \"%s\" - it has no effect\n"),
794 configname
, configlineno
, name
);
796 log_info (_("WARNING: \"%s\" is an obsolete option - it has no effect\n"),
802 * Wrapper around gcry_cipher_map_name to provide a fallback using the
803 * "Sn" syntax as used by the preference strings.
806 string_to_cipher_algo (const char *string
)
810 val
= map_cipher_gcry_to_openpgp (gcry_cipher_map_name (string
));
811 if (!val
&& string
&& (string
[0]=='S' || string
[0]=='s'))
816 val
= strtol (string
, &endptr
, 10);
817 if (!*string
|| *endptr
|| openpgp_cipher_test_algo (val
))
825 * Wrapper around gcry_md_map_name to provide a fallback using the
826 * "Hn" syntax as used by the preference strings.
829 string_to_digest_algo (const char *string
)
833 val
= gcry_md_map_name (string
);
834 if (!val
&& string
&& (string
[0]=='H' || string
[0]=='h'))
839 val
= strtol (string
, &endptr
, 10);
840 if (!*string
|| *endptr
|| openpgp_md_test_algo (val
))
850 compress_algo_to_string(int algo
)
856 case COMPRESS_ALGO_NONE
:
860 case COMPRESS_ALGO_ZIP
:
864 case COMPRESS_ALGO_ZLIB
:
869 case COMPRESS_ALGO_BZIP2
:
879 string_to_compress_algo(const char *string
)
881 /* TRANSLATORS: See doc/TRANSLATE about this string. */
882 if(match_multistr(_("uncompressed|none"),string
))
884 else if(ascii_strcasecmp(string
,"uncompressed")==0)
886 else if(ascii_strcasecmp(string
,"none")==0)
888 else if(ascii_strcasecmp(string
,"zip")==0)
890 else if(ascii_strcasecmp(string
,"zlib")==0)
893 else if(ascii_strcasecmp(string
,"bzip2")==0)
896 else if(ascii_strcasecmp(string
,"z0")==0)
898 else if(ascii_strcasecmp(string
,"z1")==0)
900 else if(ascii_strcasecmp(string
,"z2")==0)
903 else if(ascii_strcasecmp(string
,"z3")==0)
911 check_compress_algo(int algo
)
914 if(algo
>=0 && algo
<=3)
917 if(algo
>=0 && algo
<=2)
921 return G10ERR_COMPR_ALGO
;
925 default_cipher_algo(void)
927 if(opt
.def_cipher_algo
)
928 return opt
.def_cipher_algo
;
929 else if(opt
.personal_cipher_prefs
)
930 return opt
.personal_cipher_prefs
[0].value
;
932 return opt
.s2k_cipher_algo
;
935 /* There is no default_digest_algo function, but see
939 default_compress_algo(void)
941 if(opt
.compress_algo
!=-1)
942 return opt
.compress_algo
;
943 else if(opt
.personal_compress_prefs
)
944 return opt
.personal_compress_prefs
[0].value
;
946 return DEFAULT_COMPRESS_ALGO
;
950 compliance_option_string(void)
954 switch(opt
.compliance
)
956 case CO_GNUPG
: return "--gnupg";
957 case CO_RFC4880
: return "--openpgp";
958 case CO_RFC2440
: return "--rfc2440";
959 case CO_RFC1991
: return "--rfc1991";
960 case CO_PGP2
: return "--pgp2";
961 case CO_PGP6
: return "--pgp6";
962 case CO_PGP7
: return "--pgp7";
963 case CO_PGP8
: return "--pgp8";
970 compliance_failure(void)
974 switch(opt
.compliance
)
985 ver
="OpenPGP (older)";
1009 log_info(_("this message may not be usable by %s\n"),ver
);
1010 opt
.compliance
=CO_GNUPG
;
1013 /* Break a string into successive option pieces. Accepts single word
1014 options and key=value argument options. */
1016 optsep(char **stringp
)
1023 end
=strpbrk(tok
," ,=");
1029 /* what we need to do now is scan along starting with *end,
1030 If the next character we see (ignoring spaces) is an =
1031 sign, then there is an argument. */
1042 /* There is an argument, so grab that too. At this point,
1043 ptr points to the first character of the argument. */
1046 /* Is it a quoted argument? */
1050 end
=strchr(ptr
,'"');
1055 end
=strpbrk(ptr
," ,");
1073 /* Breaks an option value into key and value. Returns NULL if there
1074 is no value. Note that "string" is modified to remove the =value
1077 argsplit(char *string
)
1079 char *equals
,*arg
=NULL
;
1081 equals
=strchr(string
,'=');
1090 quote
=strchr(arg
,'"');
1095 quote
=strchr(arg
,'"');
1103 /* Trim leading spaces off of the arg */
1104 spaces
=strspn(arg
," ");
1108 /* Trim tailing spaces off of the tag */
1109 space
=strchr(string
,' ');
1117 /* Return the length of the initial token, leaving off any
1120 optlen(const char *s
)
1122 char *end
=strpbrk(s
," =");
1131 parse_options(char *str
,unsigned int *options
,
1132 struct parse_options
*opts
,int noisy
)
1136 if (str
&& !strcmp (str
, "help"))
1140 /* Figure out the longest option name so we can line these up
1142 for(i
=0;opts
[i
].name
;i
++)
1143 if(opts
[i
].help
&& maxlen
<strlen(opts
[i
].name
))
1144 maxlen
=strlen(opts
[i
].name
);
1146 for(i
=0;opts
[i
].name
;i
++)
1148 printf("%s%*s%s\n",opts
[i
].name
,
1149 maxlen
+2-(int)strlen(opts
[i
].name
),"",_(opts
[i
].help
));
1154 while((tok
=optsep(&str
)))
1162 if(ascii_strncasecmp("no-",tok
,3)==0)
1168 for(i
=0;opts
[i
].name
;i
++)
1170 size_t toklen
=optlen(tok
);
1172 if(ascii_strncasecmp(opts
[i
].name
,tok
,toklen
)==0)
1174 /* We have a match, but it might be incomplete */
1175 if(toklen
!=strlen(opts
[i
].name
))
1179 for(j
=i
+1;opts
[j
].name
;j
++)
1181 if(ascii_strncasecmp(opts
[j
].name
,tok
,toklen
)==0)
1184 log_info(_("ambiguous option `%s'\n"),otok
);
1192 *options
&=~opts
[i
].bit
;
1194 *opts
[i
].value
=NULL
;
1198 *options
|=opts
[i
].bit
;
1200 *opts
[i
].value
=argsplit(tok
);
1209 log_info(_("unknown option `%s'\n"),otok
);
1218 /* Return a new malloced string by unescaping the string S. Escaping
1219 is percent escaping and '+'/space mapping. A binary nul will
1220 silently be replaced by a 0xFF. */
1222 unescape_percent_string (const unsigned char *s
)
1226 buffer
= d
= xmalloc (strlen (s
)+1);
1229 if (*s
== '%' && s
[1] && s
[2])
1251 /* Check whether the string has characters not valid in an RFC-822
1252 address. To cope with OpenPGP we ignore allow non-ascii characters
1253 so that for example umlauts are legal in an email address. An
1254 OpenPGP user ID must be utf-8 encoded but there is no strict
1255 requirement for RFC-822. Thus to avoid IDNA encoding we put the
1256 address verbatim as utf-8 into the user ID under the assumption
1257 that mail programs handle IDNA at a lower level and take OpenPGP
1258 user IDs as utf-8. Note that we can't do an utf-8 encoding
1259 checking here because in keygen.c this function is called with the
1260 native encoding and native to utf-8 encoding is only done later. */
1262 has_invalid_email_chars (const char *s
)
1265 const char *valid_chars
=
1266 "01234567890_-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
1271 continue; /* We only care about ASCII. */
1274 else if ( !at_seen
&& !( !!strchr( valid_chars
, *s
) || *s
== '+' ) )
1276 else if ( at_seen
&& !strchr( valid_chars
, *s
) )
1283 /* Check whether NAME represents a valid mailbox according to
1284 RFC822. Returns true if so. */
1286 is_valid_mailbox (const char *name
)
1290 || has_invalid_email_chars (name
)
1291 || string_count_chr (name
,'@') != 1
1293 || name
[strlen(name
)-1] == '@'
1294 || name
[strlen(name
)-1] == '.'
1295 || strstr (name
, "..") );
1299 /* Similar to access(2), but uses PATH to find the file. */
1301 path_access(const char *file
,int mode
)
1306 envpath
=getenv("PATH");
1309 #ifdef HAVE_DRIVE_LETTERS
1310 || (((file
[0]>='A' && file
[0]<='Z')
1311 || (file
[0]>='a' && file
[0]<='z'))
1317 return access(file
,mode
);
1320 /* At least as large as, but most often larger than we need. */
1321 char *buffer
=xmalloc(strlen(envpath
)+1+strlen(file
)+1);
1322 char *split
,*item
,*path
=xstrdup(envpath
);
1326 while((item
=strsep(&split
,PATHSEP_S
)))
1328 strcpy(buffer
,item
);
1330 strcat(buffer
,file
);
1331 ret
=access(buffer
,mode
);
1345 /* Temporary helper. */
1347 pubkey_get_npkey( int algo
)
1351 if (algo
== GCRY_PK_ELG_E
)
1353 if (gcry_pk_algo_info( algo
, GCRYCTL_GET_ALGO_NPKEY
, NULL
, &n
))
1358 /* Temporary helper. */
1360 pubkey_get_nskey( int algo
)
1364 if (algo
== GCRY_PK_ELG_E
)
1366 if (gcry_pk_algo_info( algo
, GCRYCTL_GET_ALGO_NSKEY
, NULL
, &n
))
1371 /* Temporary helper. */
1373 pubkey_get_nsig( int algo
)
1377 if (algo
== GCRY_PK_ELG_E
)
1379 if (gcry_pk_algo_info( algo
, GCRYCTL_GET_ALGO_NSIGN
, NULL
, &n
))
1384 /* Temporary helper. */
1386 pubkey_get_nenc( int algo
)
1390 if (algo
== GCRY_PK_ELG_E
)
1392 if (gcry_pk_algo_info( algo
, GCRYCTL_GET_ALGO_NENCR
, NULL
, &n
))
1398 /* Temporary helper. */
1400 pubkey_nbits( int algo
, gcry_mpi_t
*key
)
1405 if( algo
== GCRY_PK_DSA
) {
1406 rc
= gcry_sexp_build ( &sexp
, NULL
,
1407 "(public-key(dsa(p%m)(q%m)(g%m)(y%m)))",
1408 key
[0], key
[1], key
[2], key
[3] );
1410 else if( algo
== GCRY_PK_ELG
|| algo
== GCRY_PK_ELG_E
) {
1411 rc
= gcry_sexp_build ( &sexp
, NULL
,
1412 "(public-key(elg(p%m)(g%m)(y%m)))",
1413 key
[0], key
[1], key
[2] );
1415 else if( algo
== GCRY_PK_RSA
) {
1416 rc
= gcry_sexp_build ( &sexp
, NULL
,
1417 "(public-key(rsa(n%m)(e%m)))",
1426 nbits
= gcry_pk_get_nbits( sexp
);
1427 gcry_sexp_release( sexp
);
1433 /* FIXME: Use gcry_mpi_print directly. */
1435 mpi_print( FILE *fp
, gcry_mpi_t a
, int mode
)
1440 return fprintf(fp
, "[MPI_NULL]");
1443 n1
= gcry_mpi_get_nbits(a
);
1444 n
+= fprintf(fp
, "[%u bits]", n1
);
1447 unsigned char *buffer
;
1449 if (gcry_mpi_aprint (GCRYMPI_FMT_HEX
, &buffer
, NULL
, a
))
1451 fputs( buffer
, fp
);
1452 n
+= strlen(buffer
);
1453 gcry_free( buffer
);