Imported Upstream version 2.1.1
[platform/upstream/gpg2.git] / g10 / misc.c
1 /* misc.c - miscellaneous functions
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3  *               2008, 2009, 2010 Free Software Foundation, Inc.
4  * Copyright (C) 2014 Werner Koch
5  *
6  * This file is part of GnuPG.
7  *
8  * GnuPG is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * GnuPG is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2
29 #include <asm/sysinfo.h>
30 #include <asm/unistd.h>
31 #endif
32 #ifdef HAVE_SETRLIMIT
33 #include <time.h>
34 #include <sys/time.h>
35 #include <sys/resource.h>
36 #endif
37 #ifdef ENABLE_SELINUX_HACKS
38 #include <sys/stat.h>
39 #endif
40
41 #ifdef HAVE_W32_SYSTEM
42 #include <time.h>
43 #include <process.h>
44 #ifdef HAVE_WINSOCK2_H
45 # include <winsock2.h>
46 #endif
47 #include <windows.h>
48 #include <shlobj.h>
49 #ifndef CSIDL_APPDATA
50 #define CSIDL_APPDATA 0x001a
51 #endif
52 #ifndef CSIDL_LOCAL_APPDATA
53 #define CSIDL_LOCAL_APPDATA 0x001c
54 #endif
55 #ifndef CSIDL_FLAG_CREATE
56 #define CSIDL_FLAG_CREATE 0x8000
57 #endif
58 #endif /*HAVE_W32_SYSTEM*/
59
60 #include "gpg.h"
61 #ifdef HAVE_W32_SYSTEM
62 # include "status.h"
63 #endif /*HAVE_W32_SYSTEM*/
64 #include "util.h"
65 #include "main.h"
66 #include "photoid.h"
67 #include "options.h"
68 #include "call-agent.h"
69 #include "i18n.h"
70
71 #include <assert.h>
72
73 static int
74 string_count_chr (const char *string, int c)
75 {
76   int count;
77
78   for (count=0; *string; string++ )
79     if ( *string == c )
80       count++;
81   return count;
82 }
83
84
85
86 #ifdef ENABLE_SELINUX_HACKS
87 /* A object and a global variable to keep track of files marked as
88    secured. */
89 struct secured_file_item
90 {
91   struct secured_file_item *next;
92   ino_t ino;
93   dev_t dev;
94 };
95 static struct secured_file_item *secured_files;
96 #endif /*ENABLE_SELINUX_HACKS*/
97
98
99
100
101 /* For the sake of SELinux we want to restrict access through gpg to
102    certain files we keep under our own control.  This function
103    registers such a file and is_secured_file may then be used to
104    check whether a file has ben registered as secured. */
105 void
106 register_secured_file (const char *fname)
107 {
108 #ifdef ENABLE_SELINUX_HACKS
109   struct stat buf;
110   struct secured_file_item *sf;
111
112   /* Note that we stop immediatley if something goes wrong here. */
113   if (stat (fname, &buf))
114     log_fatal (_("fstat of '%s' failed in %s: %s\n"), fname,
115                "register_secured_file", strerror (errno));
116 /*   log_debug ("registering '%s' i=%lu.%lu\n", fname, */
117 /*              (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
118   for (sf=secured_files; sf; sf = sf->next)
119     {
120       if (sf->ino == buf.st_ino && sf->dev == buf.st_dev)
121         return; /* Already registered.  */
122     }
123
124   sf = xmalloc (sizeof *sf);
125   sf->ino = buf.st_ino;
126   sf->dev = buf.st_dev;
127   sf->next = secured_files;
128   secured_files = sf;
129 #else /*!ENABLE_SELINUX_HACKS*/
130   (void)fname;
131 #endif /*!ENABLE_SELINUX_HACKS*/
132 }
133
134 /* Remove a file registered as secure. */
135 void
136 unregister_secured_file (const char *fname)
137 {
138 #ifdef ENABLE_SELINUX_HACKS
139   struct stat buf;
140   struct secured_file_item *sf, *sfprev;
141
142   if (stat (fname, &buf))
143     {
144       log_error (_("fstat of '%s' failed in %s: %s\n"), fname,
145                  "unregister_secured_file", strerror (errno));
146       return;
147     }
148 /*   log_debug ("unregistering '%s' i=%lu.%lu\n", fname,  */
149 /*              (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
150   for (sfprev=NULL,sf=secured_files; sf; sfprev=sf, sf = sf->next)
151     {
152       if (sf->ino == buf.st_ino && sf->dev == buf.st_dev)
153         {
154           if (sfprev)
155             sfprev->next = sf->next;
156           else
157             secured_files = sf->next;
158           xfree (sf);
159           return;
160         }
161     }
162 #else /*!ENABLE_SELINUX_HACKS*/
163   (void)fname;
164 #endif /*!ENABLE_SELINUX_HACKS*/
165 }
166
167 /* Return true if FD is corresponds to a secured file.  Using -1 for
168    FS is allowed and will return false. */
169 int
170 is_secured_file (int fd)
171 {
172 #ifdef ENABLE_SELINUX_HACKS
173   struct stat buf;
174   struct secured_file_item *sf;
175
176   if (fd == -1)
177     return 0; /* No file descriptor so it can't be secured either.  */
178
179   /* Note that we print out a error here and claim that a file is
180      secure if something went wrong. */
181   if (fstat (fd, &buf))
182     {
183       log_error (_("fstat(%d) failed in %s: %s\n"), fd,
184                  "is_secured_file", strerror (errno));
185       return 1;
186     }
187 /*   log_debug ("is_secured_file (%d) i=%lu.%lu\n", fd, */
188 /*              (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
189   for (sf=secured_files; sf; sf = sf->next)
190     {
191       if (sf->ino == buf.st_ino && sf->dev == buf.st_dev)
192         return 1; /* Yes.  */
193     }
194 #else /*!ENABLE_SELINUX_HACKS*/
195   (void)fd;
196 #endif /*!ENABLE_SELINUX_HACKS*/
197   return 0; /* No. */
198 }
199
200 /* Return true if FNAME is corresponds to a secured file.  Using NULL,
201    "" or "-" for FS is allowed and will return false. This function is
202    used before creating a file, thus it won't fail if the file does
203    not exist. */
204 int
205 is_secured_filename (const char *fname)
206 {
207 #ifdef ENABLE_SELINUX_HACKS
208   struct stat buf;
209   struct secured_file_item *sf;
210
211   if (iobuf_is_pipe_filename (fname) || !*fname)
212     return 0;
213
214   /* Note that we print out a error here and claim that a file is
215      secure if something went wrong. */
216   if (stat (fname, &buf))
217     {
218       if (errno == ENOENT || errno == EPERM || errno == EACCES)
219         return 0;
220       log_error (_("fstat of '%s' failed in %s: %s\n"), fname,
221                  "is_secured_filename", strerror (errno));
222       return 1;
223     }
224 /*   log_debug ("is_secured_filename (%s) i=%lu.%lu\n", fname, */
225 /*              (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
226   for (sf=secured_files; sf; sf = sf->next)
227     {
228       if (sf->ino == buf.st_ino && sf->dev == buf.st_dev)
229         return 1; /* Yes.  */
230     }
231 #else /*!ENABLE_SELINUX_HACKS*/
232   (void)fname;
233 #endif /*!ENABLE_SELINUX_HACKS*/
234   return 0; /* No. */
235 }
236
237
238
239 u16
240 checksum_u16( unsigned n )
241 {
242     u16 a;
243
244     a  = (n >> 8) & 0xff;
245     a += n & 0xff;
246     return a;
247 }
248
249
250 u16
251 checksum( byte *p, unsigned n )
252 {
253     u16 a;
254
255     for(a=0; n; n-- )
256         a += *p++;
257     return a;
258 }
259
260 u16
261 checksum_mpi (gcry_mpi_t a)
262 {
263   u16 csum;
264   byte *buffer;
265   size_t nbytes;
266
267   if ( gcry_mpi_print (GCRYMPI_FMT_PGP, NULL, 0, &nbytes, a) )
268     BUG ();
269   /* Fixme: For numbers not in secure memory we should use a stack
270    * based buffer and only allocate a larger one if mpi_print returns
271    * an error. */
272   buffer = (gcry_is_secure(a)?
273             gcry_xmalloc_secure (nbytes) : gcry_xmalloc (nbytes));
274   if ( gcry_mpi_print (GCRYMPI_FMT_PGP, buffer, nbytes, NULL, a) )
275     BUG ();
276   csum = checksum (buffer, nbytes);
277   xfree (buffer);
278   return csum;
279 }
280
281 u32
282 buffer_to_u32( const byte *buffer )
283 {
284     unsigned long a;
285     a =  *buffer << 24;
286     a |= buffer[1] << 16;
287     a |= buffer[2] << 8;
288     a |= buffer[3];
289     return a;
290 }
291
292 void
293 print_pubkey_algo_note (pubkey_algo_t algo)
294 {
295   if(algo >= 100 && algo <= 110)
296     {
297       static int warn=0;
298       if(!warn)
299         {
300           warn=1;
301           es_fflush (es_stdout);
302           log_info (_("WARNING: using experimental public key algorithm %s\n"),
303                     openpgp_pk_algo_name (algo));
304         }
305     }
306   else if (algo == PUBKEY_ALGO_ELGAMAL)
307     {
308       es_fflush (es_stdout);
309       log_info (_("WARNING: Elgamal sign+encrypt keys are deprecated\n"));
310     }
311 }
312
313 void
314 print_cipher_algo_note (cipher_algo_t algo)
315 {
316   if(algo >= 100 && algo <= 110)
317     {
318       static int warn=0;
319       if(!warn)
320         {
321           warn=1;
322           es_fflush (es_stdout);
323           log_info (_("WARNING: using experimental cipher algorithm %s\n"),
324                     openpgp_cipher_algo_name (algo));
325         }
326     }
327 }
328
329 void
330 print_digest_algo_note (digest_algo_t algo)
331 {
332   if(algo >= 100 && algo <= 110)
333     {
334       static int warn=0;
335       if(!warn)
336         {
337           warn=1;
338           es_fflush (es_stdout);
339           log_info (_("WARNING: using experimental digest algorithm %s\n"),
340                     gcry_md_algo_name (algo));
341         }
342     }
343   else if(algo==DIGEST_ALGO_MD5)
344     {
345       es_fflush (es_stdout);
346       log_info (_("WARNING: digest algorithm %s is deprecated\n"),
347                 gcry_md_algo_name (algo));
348     }
349 }
350
351
352 void
353 print_md5_rejected_note (void)
354 {
355   static int shown;
356
357   if (!shown)
358     {
359       es_fflush (es_stdout);
360       log_info
361         (_("Note: signatures using the %s algorithm are rejected\n"),
362          "MD5");
363       shown = 1;
364     }
365 }
366
367
368 /* Map OpenPGP algo numbers to those used by Libgcrypt.  We need to do
369    this for algorithms we implemented in Libgcrypt after they become
370    part of OpenPGP.  */
371 enum gcry_cipher_algos
372 map_cipher_openpgp_to_gcry (cipher_algo_t algo)
373 {
374   switch (algo)
375     {
376     case CIPHER_ALGO_NONE:        return GCRY_CIPHER_NONE;
377
378 #ifdef GPG_USE_IDEA
379     case CIPHER_ALGO_IDEA:        return GCRY_CIPHER_IDEA;
380 #else
381     case CIPHER_ALGO_IDEA:        return 0;
382 #endif
383
384     case CIPHER_ALGO_3DES:        return GCRY_CIPHER_3DES;
385
386 #ifdef GPG_USE_CAST5
387     case CIPHER_ALGO_CAST5:       return GCRY_CIPHER_CAST5;
388 #else
389     case CIPHER_ALGO_CAST5:       return 0;
390 #endif
391
392 #ifdef GPG_USE_BLOWFISH
393     case CIPHER_ALGO_BLOWFISH:    return GCRY_CIPHER_BLOWFISH;
394 #else
395     case CIPHER_ALGO_BLOWFISH:    return 0;
396 #endif
397
398 #ifdef GPG_USE_AES128
399     case CIPHER_ALGO_AES:         return GCRY_CIPHER_AES;
400 #else
401     case CIPHER_ALGO_AES:         return 0;
402 #endif
403
404 #ifdef GPG_USE_AES192
405     case CIPHER_ALGO_AES192:      return GCRY_CIPHER_AES192;
406 #else
407     case CIPHER_ALGO_AES192:      return 0;
408 #endif
409
410 #ifdef GPG_USE_AES256
411     case CIPHER_ALGO_AES256:      return GCRY_CIPHER_AES256;
412 #else
413     case CIPHER_ALGO_AES256:      return 0;
414 #endif
415
416 #ifdef GPG_USE_TWOFISH
417     case CIPHER_ALGO_TWOFISH:     return GCRY_CIPHER_TWOFISH;
418 #else
419     case CIPHER_ALGO_TWOFISH:     return 0;
420 #endif
421
422 #ifdef GPG_USE_CAMELLIA128
423     case CIPHER_ALGO_CAMELLIA128: return GCRY_CIPHER_CAMELLIA128;
424 #else
425     case CIPHER_ALGO_CAMELLIA128: return 0;
426 #endif
427
428 #ifdef GPG_USE_CAMELLIA192
429     case CIPHER_ALGO_CAMELLIA192: return GCRY_CIPHER_CAMELLIA192;
430 #else
431     case CIPHER_ALGO_CAMELLIA192: return 0;
432 #endif
433
434 #ifdef GPG_USE_CAMELLIA256
435     case CIPHER_ALGO_CAMELLIA256: return GCRY_CIPHER_CAMELLIA256;
436 #else
437     case CIPHER_ALGO_CAMELLIA256: return 0;
438 #endif
439     }
440   return 0;
441 }
442
443 /* The inverse function of above.  */
444 static cipher_algo_t
445 map_cipher_gcry_to_openpgp (enum gcry_cipher_algos algo)
446 {
447   switch (algo)
448     {
449     case GCRY_CIPHER_NONE:        return CIPHER_ALGO_NONE;
450     case GCRY_CIPHER_IDEA:        return CIPHER_ALGO_IDEA;
451     case GCRY_CIPHER_3DES:        return CIPHER_ALGO_3DES;
452     case GCRY_CIPHER_CAST5:       return CIPHER_ALGO_CAST5;
453     case GCRY_CIPHER_BLOWFISH:    return CIPHER_ALGO_BLOWFISH;
454     case GCRY_CIPHER_AES:         return CIPHER_ALGO_AES;
455     case GCRY_CIPHER_AES192:      return CIPHER_ALGO_AES192;
456     case GCRY_CIPHER_AES256:      return CIPHER_ALGO_AES256;
457     case GCRY_CIPHER_TWOFISH:     return CIPHER_ALGO_TWOFISH;
458     case GCRY_CIPHER_CAMELLIA128: return CIPHER_ALGO_CAMELLIA128;
459     case GCRY_CIPHER_CAMELLIA192: return CIPHER_ALGO_CAMELLIA192;
460     case GCRY_CIPHER_CAMELLIA256: return CIPHER_ALGO_CAMELLIA256;
461     default: return 0;
462     }
463 }
464
465 /* Map Gcrypt public key algorithm numbers to those used by OpenPGP.
466    FIXME: This mapping is used at only two places - we should get rid
467    of it.  */
468 pubkey_algo_t
469 map_pk_gcry_to_openpgp (enum gcry_pk_algos algo)
470 {
471   switch (algo)
472     {
473     case GCRY_PK_ECDSA:  return PUBKEY_ALGO_ECDSA;
474     case GCRY_PK_ECDH:   return PUBKEY_ALGO_ECDH;
475     default: return algo < 110 ? algo : 0;
476     }
477 }
478
479
480 /* Return the block length of an OpenPGP cipher algorithm.  */
481 int
482 openpgp_cipher_blocklen (cipher_algo_t algo)
483 {
484   /* We use the numbers from OpenPGP to be sure that we get the right
485      block length.  This is so that the packet parsing code works even
486      for unknown algorithms (for which we assume 8 due to tradition).
487
488      NOTE: If you change the the returned blocklen above 16, check
489      the callers because they may use a fixed size buffer of that
490      size. */
491   switch (algo)
492     {
493     case CIPHER_ALGO_AES:
494     case CIPHER_ALGO_AES192:
495     case CIPHER_ALGO_AES256:
496     case CIPHER_ALGO_TWOFISH:
497     case CIPHER_ALGO_CAMELLIA128:
498     case CIPHER_ALGO_CAMELLIA192:
499     case CIPHER_ALGO_CAMELLIA256:
500       return 16;
501
502     default:
503       return 8;
504     }
505 }
506
507 /****************
508  * Wrapper around the libgcrypt function with additonal checks on
509  * the OpenPGP contraints for the algo ID.
510  */
511 int
512 openpgp_cipher_test_algo (cipher_algo_t algo)
513 {
514   enum gcry_cipher_algos ga;
515
516   ga = map_cipher_openpgp_to_gcry (algo);
517   if (!ga)
518     return gpg_error (GPG_ERR_CIPHER_ALGO);
519
520   return gcry_cipher_test_algo (ga);
521 }
522
523 /* Map the OpenPGP cipher algorithm whose ID is contained in ALGORITHM to a
524    string representation of the algorithm name.  For unknown algorithm
525    IDs this function returns "?".  */
526 const char *
527 openpgp_cipher_algo_name (cipher_algo_t algo)
528 {
529   switch (algo)
530     {
531     case CIPHER_ALGO_NONE:        break;
532     case CIPHER_ALGO_IDEA:        return "IDEA";
533     case CIPHER_ALGO_3DES:        return "3DES";
534     case CIPHER_ALGO_CAST5:       return "CAST5";
535     case CIPHER_ALGO_BLOWFISH:    return "BLOWFISH";
536     case CIPHER_ALGO_AES:         return "AES";
537     case CIPHER_ALGO_AES192:      return "AES192";
538     case CIPHER_ALGO_AES256:      return "AES256";
539     case CIPHER_ALGO_TWOFISH:     return "TWOFISH";
540     case CIPHER_ALGO_CAMELLIA128: return "CAMELLIA128";
541     case CIPHER_ALGO_CAMELLIA192: return "CAMELLIA192";
542     case CIPHER_ALGO_CAMELLIA256: return "CAMELLIA256";
543     }
544   return "?";
545 }
546
547
548 /* Return 0 if ALGO is a supported OpenPGP public key algorithm.  */
549 int
550 openpgp_pk_test_algo (pubkey_algo_t algo)
551 {
552   return openpgp_pk_test_algo2 (algo, 0);
553 }
554
555
556 /* Return 0 if ALGO is a supported OpenPGP public key algorithm and
557    allows the usage USE.  */
558 int
559 openpgp_pk_test_algo2 (pubkey_algo_t algo, unsigned int use)
560 {
561   enum gcry_pk_algos ga = 0;
562   size_t use_buf = use;
563
564   switch (algo)
565     {
566 #ifdef GPG_USE_RSA
567     case PUBKEY_ALGO_RSA:       ga = GCRY_PK_RSA;   break;
568     case PUBKEY_ALGO_RSA_E:     ga = GCRY_PK_RSA_E; break;
569     case PUBKEY_ALGO_RSA_S:     ga = GCRY_PK_RSA_S; break;
570 #else
571     case PUBKEY_ALGO_RSA:       break;
572     case PUBKEY_ALGO_RSA_E:     break;
573     case PUBKEY_ALGO_RSA_S:     break;
574 #endif
575
576     case PUBKEY_ALGO_ELGAMAL_E: ga = GCRY_PK_ELG;   break;
577     case PUBKEY_ALGO_DSA:       ga = GCRY_PK_DSA;   break;
578
579 #ifdef GPG_USE_ECDH
580     case PUBKEY_ALGO_ECDH:      ga = GCRY_PK_ECC;   break;
581 #else
582     case PUBKEY_ALGO_ECDH:      break;
583 #endif
584
585 #ifdef GPG_USE_ECDSA
586     case PUBKEY_ALGO_ECDSA:     ga = GCRY_PK_ECC;   break;
587 #else
588     case PUBKEY_ALGO_ECDSA:     break;
589 #endif
590
591 #ifdef GPG_USE_EDDSA
592     case PUBKEY_ALGO_EDDSA:     ga = GCRY_PK_ECC;   break;
593 #else
594     case PUBKEY_ALGO_EDDSA:     break;
595 #endif
596
597     case PUBKEY_ALGO_ELGAMAL:
598       /* Dont't allow type 20 keys unless in rfc2440 mode.  */
599       if (RFC2440)
600         ga = GCRY_PK_ELG;
601       break;
602     }
603   if (!ga)
604     return gpg_error (GPG_ERR_PUBKEY_ALGO);
605
606   /* No check whether Libgcrypt has support for the algorithm.  */
607   return gcry_pk_algo_info (ga, GCRYCTL_TEST_ALGO, NULL, &use_buf);
608 }
609
610
611 int
612 openpgp_pk_algo_usage ( int algo )
613 {
614     int use = 0;
615
616     /* They are hardwired in gpg 1.0. */
617     switch ( algo ) {
618       case PUBKEY_ALGO_RSA:
619           use = (PUBKEY_USAGE_CERT | PUBKEY_USAGE_SIG
620                  | PUBKEY_USAGE_ENC | PUBKEY_USAGE_AUTH);
621           break;
622       case PUBKEY_ALGO_RSA_E:
623       case PUBKEY_ALGO_ECDH:
624           use = PUBKEY_USAGE_ENC;
625           break;
626       case PUBKEY_ALGO_RSA_S:
627           use = PUBKEY_USAGE_CERT | PUBKEY_USAGE_SIG;
628           break;
629       case PUBKEY_ALGO_ELGAMAL:
630           if (RFC2440)
631              use = PUBKEY_USAGE_ENC;
632           break;
633       case PUBKEY_ALGO_ELGAMAL_E:
634           use = PUBKEY_USAGE_ENC;
635           break;
636       case PUBKEY_ALGO_DSA:
637           use = PUBKEY_USAGE_CERT | PUBKEY_USAGE_SIG | PUBKEY_USAGE_AUTH;
638           break;
639       case PUBKEY_ALGO_ECDSA:
640       case PUBKEY_ALGO_EDDSA:
641           use = PUBKEY_USAGE_CERT | PUBKEY_USAGE_SIG | PUBKEY_USAGE_AUTH;
642       default:
643           break;
644     }
645     return use;
646 }
647
648 /* Map the OpenPGP pubkey algorithm whose ID is contained in ALGO to a
649    string representation of the algorithm name.  For unknown algorithm
650    IDs this function returns "?".  */
651 const char *
652 openpgp_pk_algo_name (pubkey_algo_t algo)
653 {
654   switch (algo)
655     {
656     case PUBKEY_ALGO_RSA:
657     case PUBKEY_ALGO_RSA_E:
658     case PUBKEY_ALGO_RSA_S:     return "RSA";
659     case PUBKEY_ALGO_ELGAMAL:
660     case PUBKEY_ALGO_ELGAMAL_E: return "ELG";
661     case PUBKEY_ALGO_DSA:       return "DSA";
662     case PUBKEY_ALGO_ECDH:      return "ECDH";
663     case PUBKEY_ALGO_ECDSA:     return "ECDSA";
664     case PUBKEY_ALGO_EDDSA:     return "EDDSA";
665     }
666   return "?";
667 }
668
669
670 /* Explicit mapping of OpenPGP digest algos to Libgcrypt.  */
671 /* FIXME: We do not yes use it everywhere.  */
672 enum gcry_md_algos
673 map_md_openpgp_to_gcry (digest_algo_t algo)
674 {
675   switch (algo)
676     {
677 #ifdef GPG_USE_MD5
678     case DIGEST_ALGO_MD5:    return GCRY_MD_MD5;
679 #else
680     case DIGEST_ALGO_MD5:    return 0;
681 #endif
682
683     case DIGEST_ALGO_SHA1:   return GCRY_MD_SHA1;
684
685 #ifdef GPG_USE_RMD160
686     case DIGEST_ALGO_RMD160: return GCRY_MD_RMD160;
687 #else
688     case DIGEST_ALGO_RMD160: return 0;
689 #endif
690
691 #ifdef GPG_USE_SHA224
692     case DIGEST_ALGO_SHA224: return GCRY_MD_SHA224;
693 #else
694     case DIGEST_ALGO_SHA224: return 0;
695 #endif
696
697     case DIGEST_ALGO_SHA256: return GCRY_MD_SHA256;
698
699 #ifdef GPG_USE_SHA384
700     case DIGEST_ALGO_SHA384: return GCRY_MD_SHA384;
701 #else
702     case DIGEST_ALGO_SHA384: return 0;
703 #endif
704
705 #ifdef GPG_USE_SHA512
706     case DIGEST_ALGO_SHA512: return GCRY_MD_SHA512;
707 #else
708     case DIGEST_ALGO_SHA512: return 0;
709 #endif
710     }
711   return 0;
712 }
713
714
715 /* Return 0 if ALGO is suitable and implemented OpenPGP hash
716    algorithm.  */
717 int
718 openpgp_md_test_algo (digest_algo_t algo)
719 {
720   enum gcry_md_algos ga;
721
722   ga = map_md_openpgp_to_gcry (algo);
723   if (!ga)
724     return gpg_error (GPG_ERR_DIGEST_ALGO);
725
726   return gcry_md_test_algo (ga);
727 }
728
729
730 /* Map the OpenPGP digest algorithm whose ID is contained in ALGO to a
731    string representation of the algorithm name.  For unknown algorithm
732    IDs this function returns "?".  */
733 const char *
734 openpgp_md_algo_name (int algo)
735 {
736   switch (algo)
737     {
738     case DIGEST_ALGO_MD5:    return "MD5";
739     case DIGEST_ALGO_SHA1:   return "SHA1";
740     case DIGEST_ALGO_RMD160: return "RIPEMD160";
741     case DIGEST_ALGO_SHA256: return "SHA256";
742     case DIGEST_ALGO_SHA384: return "SHA384";
743     case DIGEST_ALGO_SHA512: return "SHA512";
744     case DIGEST_ALGO_SHA224: return "SHA224";
745     }
746   return "?";
747 }
748
749
750 static unsigned long
751 get_signature_count (PKT_public_key *pk)
752 {
753 #ifdef ENABLE_CARD_SUPPORT
754   struct agent_card_info_s info;
755
756   (void)pk;
757   if (!agent_scd_getattr ("SIG-COUNTER",&info))
758     return info.sig_counter;
759   else
760     return 0;
761 #else
762   (void)pk;
763   return 0;
764 #endif
765 }
766
767 /* Expand %-strings.  Returns a string which must be xfreed.  Returns
768    NULL if the string cannot be expanded (too large). */
769 char *
770 pct_expando(const char *string,struct expando_args *args)
771 {
772   const char *ch=string;
773   int idx=0,maxlen=0,done=0;
774   u32 pk_keyid[2]={0,0},sk_keyid[2]={0,0};
775   char *ret=NULL;
776
777   if(args->pk)
778     keyid_from_pk(args->pk,pk_keyid);
779
780   if(args->pksk)
781     keyid_from_pk (args->pksk, sk_keyid);
782
783   /* This is used so that %k works in photoid command strings in
784      --list-secret-keys (which of course has a sk, but no pk). */
785   if(!args->pk && args->pksk)
786     keyid_from_pk (args->pksk, pk_keyid);
787
788   while(*ch!='\0')
789     {
790       if(!done)
791         {
792           /* 8192 is way bigger than we'll need here */
793           if(maxlen>=8192)
794             goto fail;
795
796           maxlen+=1024;
797           ret=xrealloc(ret,maxlen);
798         }
799
800       done=0;
801
802       if(*ch=='%')
803         {
804           switch(*(ch+1))
805             {
806             case 's': /* short key id */
807               if(idx+8<maxlen)
808                 {
809                   sprintf(&ret[idx],"%08lX",(ulong)sk_keyid[1]);
810                   idx+=8;
811                   done=1;
812                 }
813               break;
814
815             case 'S': /* long key id */
816               if(idx+16<maxlen)
817                 {
818                   sprintf(&ret[idx],"%08lX%08lX",
819                           (ulong)sk_keyid[0],(ulong)sk_keyid[1]);
820                   idx+=16;
821                   done=1;
822                 }
823               break;
824
825             case 'k': /* short key id */
826               if(idx+8<maxlen)
827                 {
828                   sprintf(&ret[idx],"%08lX",(ulong)pk_keyid[1]);
829                   idx+=8;
830                   done=1;
831                 }
832               break;
833
834             case 'K': /* long key id */
835               if(idx+16<maxlen)
836                 {
837                   sprintf(&ret[idx],"%08lX%08lX",
838                           (ulong)pk_keyid[0],(ulong)pk_keyid[1]);
839                   idx+=16;
840                   done=1;
841                 }
842               break;
843
844             case 'U': /* z-base-32 encoded user id hash. */
845               if (args->namehash)
846                 {
847                   char *tmp = zb32_encode (args->namehash, 8*20);
848                   if (tmp)
849                     {
850                       if (idx + strlen (tmp) < maxlen)
851                         {
852                           strcpy (ret+idx, tmp);
853                           idx += strlen (tmp);
854                         }
855                       xfree (tmp);
856                       done = 1;
857                     }
858                 }
859               break;
860
861             case 'c': /* signature count from card, if any. */
862               if(idx+10<maxlen)
863                 {
864                   sprintf (&ret[idx],"%lu", get_signature_count (args->pksk));
865                   idx+=strlen(&ret[idx]);
866                   done=1;
867                 }
868               break;
869
870             case 'f': /* Fingerprint of key being signed */
871             case 'p': /* Fingerprint of the primary key making the signature. */
872             case 'g': /* Fingerprint of thge key making the signature.  */
873               {
874                 byte array[MAX_FINGERPRINT_LEN];
875                 size_t len;
876                 int i;
877
878                 if ((*(ch+1))=='f' && args->pk)
879                   fingerprint_from_pk (args->pk, array, &len);
880                 else if ((*(ch+1))=='p' && args->pksk)
881                   {
882                     if(args->pksk->flags.primary)
883                       fingerprint_from_pk (args->pksk, array, &len);
884                     else if (args->pksk->main_keyid[0]
885                              || args->pksk->main_keyid[1])
886                       {
887                         /* Not the primary key: Find the fingerprint
888                            of the primary key.  */
889                         PKT_public_key *pk=
890                           xmalloc_clear(sizeof(PKT_public_key));
891
892                         if (!get_pubkey_fast (pk,args->pksk->main_keyid))
893                           fingerprint_from_pk (pk, array, &len);
894                         else
895                           memset (array, 0, (len=MAX_FINGERPRINT_LEN));
896                         free_public_key (pk);
897                       }
898                     else /* Oops: info about the primary key missing.  */
899                       memset(array,0,(len=MAX_FINGERPRINT_LEN));
900                   }
901                 else if((*(ch+1))=='g' && args->pksk)
902                   fingerprint_from_pk (args->pksk, array, &len);
903                 else
904                   memset(array,0,(len=MAX_FINGERPRINT_LEN));
905
906                 if(idx+(len*2)<maxlen)
907                   {
908                     for(i=0;i<len;i++)
909                       {
910                         sprintf(&ret[idx],"%02X",array[i]);
911                         idx+=2;
912                       }
913                     done=1;
914                   }
915               }
916               break;
917
918             case 'v': /* validity letters */
919               if(args->validity_info && idx+1<maxlen)
920                 {
921                   ret[idx++]=args->validity_info;
922                   ret[idx]='\0';
923                   done=1;
924                 }
925               break;
926
927               /* The text string types */
928             case 't':
929             case 'T':
930             case 'V':
931               {
932                 const char *str=NULL;
933
934                 switch(*(ch+1))
935                   {
936                   case 't': /* e.g. "jpg" */
937                     str=image_type_to_string(args->imagetype,0);
938                     break;
939
940                   case 'T': /* e.g. "image/jpeg" */
941                     str=image_type_to_string(args->imagetype,2);
942                     break;
943
944                   case 'V': /* e.g. "full", "expired", etc. */
945                     str=args->validity_string;
946                     break;
947                   }
948
949                 if(str && idx+strlen(str)<maxlen)
950                   {
951                     strcpy(&ret[idx],str);
952                     idx+=strlen(str);
953                     done=1;
954                   }
955               }
956               break;
957
958             case '%':
959               if(idx+1<maxlen)
960                 {
961                   ret[idx++]='%';
962                   ret[idx]='\0';
963                   done=1;
964                 }
965               break;
966
967               /* Any unknown %-keys (like %i, %o, %I, and %O) are
968                  passed through for later expansion.  Note this also
969                  handles the case where the last character in the
970                  string is a '%' - the terminating \0 will end up here
971                  and properly terminate the string. */
972             default:
973               if(idx+2<maxlen)
974                 {
975                   ret[idx++]='%';
976                   ret[idx++]=*(ch+1);
977                   ret[idx]='\0';
978                   done=1;
979                 }
980               break;
981               }
982
983           if(done)
984             ch++;
985         }
986       else
987         {
988           if(idx+1<maxlen)
989             {
990               ret[idx++]=*ch;
991               ret[idx]='\0';
992               done=1;
993             }
994         }
995
996       if(done)
997         ch++;
998     }
999
1000   return ret;
1001
1002  fail:
1003   xfree(ret);
1004   return NULL;
1005 }
1006
1007 void
1008 deprecated_warning(const char *configname,unsigned int configlineno,
1009                    const char *option,const char *repl1,const char *repl2)
1010 {
1011   if(configname)
1012     {
1013       if(strncmp("--",option,2)==0)
1014         option+=2;
1015
1016       if(strncmp("--",repl1,2)==0)
1017         repl1+=2;
1018
1019       log_info(_("%s:%d: deprecated option \"%s\"\n"),
1020                configname,configlineno,option);
1021     }
1022   else
1023     log_info(_("WARNING: \"%s\" is a deprecated option\n"),option);
1024
1025   log_info(_("please use \"%s%s\" instead\n"),repl1,repl2);
1026 }
1027
1028
1029 void
1030 deprecated_command (const char *name)
1031 {
1032   log_info(_("WARNING: \"%s\" is a deprecated command - do not use it\n"),
1033            name);
1034 }
1035
1036
1037 void
1038 obsolete_option (const char *configname, unsigned int configlineno,
1039                  const char *name)
1040 {
1041   if(configname)
1042     log_info (_("%s:%u: obsolete option \"%s\" - it has no effect\n"),
1043               configname, configlineno, name);
1044   else
1045     log_info (_("WARNING: \"%s%s\" is an obsolete option - it has no effect\n"),
1046               "--", name);
1047 }
1048
1049
1050 void
1051 obsolete_scdaemon_option (const char *configname, unsigned int configlineno,
1052                           const char *name)
1053 {
1054   if (configname)
1055     log_info (_("%s:%u: \"%s\" is obsolete in this file"
1056                 " - it only has effect in %s\n"),
1057               configname, configlineno, name, SCDAEMON_NAME EXTSEP_S "conf");
1058   else
1059     log_info (_("WARNING: \"%s%s\" is an obsolete option"
1060                 " - it has no effect except on %s\n"),
1061               "--", name, SCDAEMON_NAME);
1062 }
1063
1064
1065 /*
1066  * Wrapper around gcry_cipher_map_name to provide a fallback using the
1067  * "Sn" syntax as used by the preference strings.
1068  */
1069 int
1070 string_to_cipher_algo (const char *string)
1071 {
1072   int val;
1073
1074   val = map_cipher_gcry_to_openpgp (gcry_cipher_map_name (string));
1075   if (!val && string && (string[0]=='S' || string[0]=='s'))
1076     {
1077       char *endptr;
1078
1079       string++;
1080       val = strtol (string, &endptr, 10);
1081       if (!*string || *endptr || openpgp_cipher_test_algo (val))
1082         val = 0;
1083     }
1084
1085   return val;
1086 }
1087
1088 /*
1089  * Wrapper around gcry_md_map_name to provide a fallback using the
1090  * "Hn" syntax as used by the preference strings.
1091  */
1092 int
1093 string_to_digest_algo (const char *string)
1094 {
1095   int val;
1096
1097   /* FIXME: We should make use of our wrapper fucntion and not assume
1098      that there is a 1 to 1 mapping between OpenPGP and Libgcrypt.  */
1099   val = gcry_md_map_name (string);
1100   if (!val && string && (string[0]=='H' || string[0]=='h'))
1101     {
1102       char *endptr;
1103
1104       string++;
1105       val = strtol (string, &endptr, 10);
1106       if (!*string || *endptr || openpgp_md_test_algo (val))
1107         val = 0;
1108     }
1109
1110   return val;
1111 }
1112
1113
1114
1115 const char *
1116 compress_algo_to_string(int algo)
1117 {
1118   const char *s=NULL;
1119
1120   switch(algo)
1121     {
1122     case COMPRESS_ALGO_NONE:
1123       s=_("Uncompressed");
1124       break;
1125
1126     case COMPRESS_ALGO_ZIP:
1127       s="ZIP";
1128       break;
1129
1130     case COMPRESS_ALGO_ZLIB:
1131       s="ZLIB";
1132       break;
1133
1134 #ifdef HAVE_BZIP2
1135     case COMPRESS_ALGO_BZIP2:
1136       s="BZIP2";
1137       break;
1138 #endif
1139     }
1140
1141   return s;
1142 }
1143
1144 int
1145 string_to_compress_algo(const char *string)
1146 {
1147   /* TRANSLATORS: See doc/TRANSLATE about this string. */
1148   if(match_multistr(_("uncompressed|none"),string))
1149     return 0;
1150   else if(ascii_strcasecmp(string,"uncompressed")==0)
1151     return 0;
1152   else if(ascii_strcasecmp(string,"none")==0)
1153     return 0;
1154   else if(ascii_strcasecmp(string,"zip")==0)
1155     return 1;
1156   else if(ascii_strcasecmp(string,"zlib")==0)
1157     return 2;
1158 #ifdef HAVE_BZIP2
1159   else if(ascii_strcasecmp(string,"bzip2")==0)
1160     return 3;
1161 #endif
1162   else if(ascii_strcasecmp(string,"z0")==0)
1163     return 0;
1164   else if(ascii_strcasecmp(string,"z1")==0)
1165     return 1;
1166   else if(ascii_strcasecmp(string,"z2")==0)
1167     return 2;
1168 #ifdef HAVE_BZIP2
1169   else if(ascii_strcasecmp(string,"z3")==0)
1170     return 3;
1171 #endif
1172   else
1173     return -1;
1174 }
1175
1176 int
1177 check_compress_algo(int algo)
1178 {
1179   switch (algo)
1180     {
1181     case 0: return 0;
1182 #ifdef HAVE_ZIP
1183     case 1:
1184     case 2: return 0;
1185 #endif
1186 #ifdef HAVE_BZIP2
1187     case 3: return 0;
1188 #endif
1189     default: return G10ERR_COMPR_ALGO;
1190     }
1191 }
1192
1193 int
1194 default_cipher_algo(void)
1195 {
1196   if(opt.def_cipher_algo)
1197     return opt.def_cipher_algo;
1198   else if(opt.personal_cipher_prefs)
1199     return opt.personal_cipher_prefs[0].value;
1200   else
1201     return opt.s2k_cipher_algo;
1202 }
1203
1204 /* There is no default_digest_algo function, but see
1205    sign.c:hash_for() */
1206
1207 int
1208 default_compress_algo(void)
1209 {
1210   if(opt.compress_algo!=-1)
1211     return opt.compress_algo;
1212   else if(opt.personal_compress_prefs)
1213     return opt.personal_compress_prefs[0].value;
1214   else
1215     return DEFAULT_COMPRESS_ALGO;
1216 }
1217
1218 const char *
1219 compliance_option_string(void)
1220 {
1221   char *ver="???";
1222
1223   switch(opt.compliance)
1224     {
1225     case CO_GNUPG:   return "--gnupg";
1226     case CO_RFC4880: return "--openpgp";
1227     case CO_RFC2440: return "--rfc2440";
1228     case CO_PGP6:    return "--pgp6";
1229     case CO_PGP7:    return "--pgp7";
1230     case CO_PGP8:    return "--pgp8";
1231     }
1232
1233   return ver;
1234 }
1235
1236 void
1237 compliance_failure(void)
1238 {
1239   char *ver="???";
1240
1241   switch(opt.compliance)
1242     {
1243     case CO_GNUPG:
1244       ver="GnuPG";
1245       break;
1246
1247     case CO_RFC4880:
1248       ver="OpenPGP";
1249       break;
1250
1251     case CO_RFC2440:
1252       ver="OpenPGP (older)";
1253       break;
1254
1255     case CO_PGP6:
1256       ver="PGP 6.x";
1257       break;
1258
1259     case CO_PGP7:
1260       ver="PGP 7.x";
1261       break;
1262
1263     case CO_PGP8:
1264       ver="PGP 8.x";
1265       break;
1266     }
1267
1268   log_info(_("this message may not be usable by %s\n"),ver);
1269   opt.compliance=CO_GNUPG;
1270 }
1271
1272 /* Break a string into successive option pieces.  Accepts single word
1273    options and key=value argument options. */
1274 char *
1275 optsep(char **stringp)
1276 {
1277   char *tok,*end;
1278
1279   tok=*stringp;
1280   if(tok)
1281     {
1282       end=strpbrk(tok," ,=");
1283       if(end)
1284         {
1285           int sawequals=0;
1286           char *ptr=end;
1287
1288           /* what we need to do now is scan along starting with *end,
1289              If the next character we see (ignoring spaces) is an =
1290              sign, then there is an argument. */
1291
1292           while(*ptr)
1293             {
1294               if(*ptr=='=')
1295                 sawequals=1;
1296               else if(*ptr!=' ')
1297                 break;
1298               ptr++;
1299             }
1300
1301           /* There is an argument, so grab that too.  At this point,
1302              ptr points to the first character of the argument. */
1303           if(sawequals)
1304             {
1305               /* Is it a quoted argument? */
1306               if(*ptr=='"')
1307                 {
1308                   ptr++;
1309                   end=strchr(ptr,'"');
1310                   if(end)
1311                     end++;
1312                 }
1313               else
1314                 end=strpbrk(ptr," ,");
1315             }
1316
1317           if(end && *end)
1318             {
1319               *end='\0';
1320               *stringp=end+1;
1321             }
1322           else
1323             *stringp=NULL;
1324         }
1325       else
1326         *stringp=NULL;
1327     }
1328
1329   return tok;
1330 }
1331
1332 /* Breaks an option value into key and value.  Returns NULL if there
1333    is no value.  Note that "string" is modified to remove the =value
1334    part. */
1335 char *
1336 argsplit(char *string)
1337 {
1338   char *equals,*arg=NULL;
1339
1340   equals=strchr(string,'=');
1341   if(equals)
1342     {
1343       char *quote,*space;
1344
1345       *equals='\0';
1346       arg=equals+1;
1347
1348       /* Quoted arg? */
1349       quote=strchr(arg,'"');
1350       if(quote)
1351         {
1352           arg=quote+1;
1353
1354           quote=strchr(arg,'"');
1355           if(quote)
1356             *quote='\0';
1357         }
1358       else
1359         {
1360           size_t spaces;
1361
1362           /* Trim leading spaces off of the arg */
1363           spaces=strspn(arg," ");
1364           arg+=spaces;
1365         }
1366
1367       /* Trim tailing spaces off of the tag */
1368       space=strchr(string,' ');
1369       if(space)
1370         *space='\0';
1371     }
1372
1373   return arg;
1374 }
1375
1376 /* Return the length of the initial token, leaving off any
1377    argument. */
1378 static size_t
1379 optlen(const char *s)
1380 {
1381   char *end=strpbrk(s," =");
1382
1383   if(end)
1384     return end-s;
1385   else
1386     return strlen(s);
1387 }
1388
1389 int
1390 parse_options(char *str,unsigned int *options,
1391               struct parse_options *opts,int noisy)
1392 {
1393   char *tok;
1394
1395   if (str && !strcmp (str, "help"))
1396     {
1397       int i,maxlen=0;
1398
1399       /* Figure out the longest option name so we can line these up
1400          neatly. */
1401       for(i=0;opts[i].name;i++)
1402         if(opts[i].help && maxlen<strlen(opts[i].name))
1403           maxlen=strlen(opts[i].name);
1404
1405       for(i=0;opts[i].name;i++)
1406         if(opts[i].help)
1407           printf("%s%*s%s\n",opts[i].name,
1408                  maxlen+2-(int)strlen(opts[i].name),"",_(opts[i].help));
1409
1410       g10_exit(0);
1411     }
1412
1413   while((tok=optsep(&str)))
1414     {
1415       int i,rev=0;
1416       char *otok=tok;
1417
1418       if(tok[0]=='\0')
1419         continue;
1420
1421       if(ascii_strncasecmp("no-",tok,3)==0)
1422         {
1423           rev=1;
1424           tok+=3;
1425         }
1426
1427       for(i=0;opts[i].name;i++)
1428         {
1429           size_t toklen=optlen(tok);
1430
1431           if(ascii_strncasecmp(opts[i].name,tok,toklen)==0)
1432             {
1433               /* We have a match, but it might be incomplete */
1434               if(toklen!=strlen(opts[i].name))
1435                 {
1436                   int j;
1437
1438                   for(j=i+1;opts[j].name;j++)
1439                     {
1440                       if(ascii_strncasecmp(opts[j].name,tok,toklen)==0)
1441                         {
1442                           if(noisy)
1443                             log_info(_("ambiguous option '%s'\n"),otok);
1444                           return 0;
1445                         }
1446                     }
1447                 }
1448
1449               if(rev)
1450                 {
1451                   *options&=~opts[i].bit;
1452                   if(opts[i].value)
1453                     *opts[i].value=NULL;
1454                 }
1455               else
1456                 {
1457                   *options|=opts[i].bit;
1458                   if(opts[i].value)
1459                     *opts[i].value=argsplit(tok);
1460                 }
1461               break;
1462             }
1463         }
1464
1465       if(!opts[i].name)
1466         {
1467           if(noisy)
1468             log_info(_("unknown option '%s'\n"),otok);
1469           return 0;
1470         }
1471     }
1472
1473   return 1;
1474 }
1475
1476
1477 /* Check whether the string has characters not valid in an RFC-822
1478    address.  To cope with OpenPGP we ignore non-ascii characters
1479    so that for example umlauts are legal in an email address.  An
1480    OpenPGP user ID must be utf-8 encoded but there is no strict
1481    requirement for RFC-822.  Thus to avoid IDNA encoding we put the
1482    address verbatim as utf-8 into the user ID under the assumption
1483    that mail programs handle IDNA at a lower level and take OpenPGP
1484    user IDs as utf-8.  Note that we can't do an utf-8 encoding
1485    checking here because in keygen.c this function is called with the
1486    native encoding and native to utf-8 encoding is only done  later.  */
1487 int
1488 has_invalid_email_chars (const char *s)
1489 {
1490   int at_seen=0;
1491   const char *valid_chars=
1492     "01234567890_-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
1493
1494   for ( ; *s; s++ )
1495     {
1496       if ( (*s & 0x80) )
1497         continue; /* We only care about ASCII.  */
1498       if ( *s == '@' )
1499         at_seen=1;
1500       else if ( !at_seen && !(strchr (valid_chars, *s)
1501                               || strchr ("!#$%&'*+/=?^`{|}~", *s)))
1502         return 1;
1503       else if ( at_seen && !strchr( valid_chars, *s ) )
1504         return 1;
1505     }
1506   return 0;
1507 }
1508
1509
1510 /* Check whether NAME represents a valid mailbox according to
1511    RFC822. Returns true if so. */
1512 int
1513 is_valid_mailbox (const char *name)
1514 {
1515   return !( !name
1516             || !*name
1517             || has_invalid_email_chars (name)
1518             || string_count_chr (name,'@') != 1
1519             || *name == '@'
1520             || name[strlen(name)-1] == '@'
1521             || name[strlen(name)-1] == '.'
1522             || strstr (name, "..") );
1523 }
1524
1525
1526 /* Check whether UID is a valid standard user id of the form
1527      "Heinrich Heine <heinrichh@duesseldorf.de>"
1528    and return true if this is the case. */
1529 int
1530 is_valid_user_id (const char *uid)
1531 {
1532   if (!uid || !*uid)
1533     return 0;
1534
1535   return 1;
1536 }
1537
1538
1539
1540 /* Similar to access(2), but uses PATH to find the file. */
1541 int
1542 path_access(const char *file,int mode)
1543 {
1544   char *envpath;
1545   int ret=-1;
1546
1547   envpath=getenv("PATH");
1548
1549   if(!envpath
1550 #ifdef HAVE_DRIVE_LETTERS
1551      || (((file[0]>='A' && file[0]<='Z')
1552           || (file[0]>='a' && file[0]<='z'))
1553          && file[1]==':')
1554 #else
1555      || file[0]=='/'
1556 #endif
1557      )
1558     return access(file,mode);
1559   else
1560     {
1561       /* At least as large as, but most often larger than we need. */
1562       char *buffer=xmalloc(strlen(envpath)+1+strlen(file)+1);
1563       char *split,*item,*path=xstrdup(envpath);
1564
1565       split=path;
1566
1567       while((item=strsep(&split,PATHSEP_S)))
1568         {
1569           strcpy(buffer,item);
1570           strcat(buffer,"/");
1571           strcat(buffer,file);
1572           ret=access(buffer,mode);
1573           if(ret==0)
1574             break;
1575         }
1576
1577       xfree(path);
1578       xfree(buffer);
1579     }
1580
1581   return ret;
1582 }
1583
1584
1585 \f
1586 /* Return the number of public key parameters as used by OpenPGP.  */
1587 int
1588 pubkey_get_npkey (pubkey_algo_t algo)
1589 {
1590   switch (algo)
1591     {
1592     case PUBKEY_ALGO_RSA:
1593     case PUBKEY_ALGO_RSA_E:
1594     case PUBKEY_ALGO_RSA_S:     return 2;
1595     case PUBKEY_ALGO_ELGAMAL_E: return 3;
1596     case PUBKEY_ALGO_DSA:       return 4;
1597     case PUBKEY_ALGO_ECDH:      return 3;
1598     case PUBKEY_ALGO_ECDSA:     return 2;
1599     case PUBKEY_ALGO_ELGAMAL:   return 3;
1600     case PUBKEY_ALGO_EDDSA:     return 2;
1601     }
1602   return 0;
1603 }
1604
1605
1606 /* Return the number of secret key parameters as used by OpenPGP.  */
1607 int
1608 pubkey_get_nskey (pubkey_algo_t algo)
1609 {
1610   switch (algo)
1611     {
1612     case PUBKEY_ALGO_RSA:
1613     case PUBKEY_ALGO_RSA_E:
1614     case PUBKEY_ALGO_RSA_S:     return 6;
1615     case PUBKEY_ALGO_ELGAMAL_E: return 4;
1616     case PUBKEY_ALGO_DSA:       return 5;
1617     case PUBKEY_ALGO_ECDH:      return 4;
1618     case PUBKEY_ALGO_ECDSA:     return 3;
1619     case PUBKEY_ALGO_ELGAMAL:   return 4;
1620     case PUBKEY_ALGO_EDDSA:     return 3;
1621     }
1622   return 0;
1623 }
1624
1625 /* Temporary helper. */
1626 int
1627 pubkey_get_nsig (pubkey_algo_t algo)
1628 {
1629   switch (algo)
1630     {
1631     case PUBKEY_ALGO_RSA:
1632     case PUBKEY_ALGO_RSA_E:
1633     case PUBKEY_ALGO_RSA_S:     return 1;
1634     case PUBKEY_ALGO_ELGAMAL_E: return 0;
1635     case PUBKEY_ALGO_DSA:       return 2;
1636     case PUBKEY_ALGO_ECDH:      return 0;
1637     case PUBKEY_ALGO_ECDSA:     return 2;
1638     case PUBKEY_ALGO_ELGAMAL:   return 2;
1639     case PUBKEY_ALGO_EDDSA:     return 2;
1640     }
1641   return 0;
1642 }
1643
1644
1645 /* Temporary helper. */
1646 int
1647 pubkey_get_nenc (pubkey_algo_t algo)
1648 {
1649   switch (algo)
1650     {
1651     case PUBKEY_ALGO_RSA:
1652     case PUBKEY_ALGO_RSA_E:
1653     case PUBKEY_ALGO_RSA_S:     return 1;
1654     case PUBKEY_ALGO_ELGAMAL_E: return 2;
1655     case PUBKEY_ALGO_DSA:       return 0;
1656     case PUBKEY_ALGO_ECDH:      return 2;
1657     case PUBKEY_ALGO_ECDSA:     return 0;
1658     case PUBKEY_ALGO_ELGAMAL:   return 2;
1659     case PUBKEY_ALGO_EDDSA:     return 0;
1660     }
1661   return 0;
1662 }
1663
1664
1665 /* Temporary helper. */
1666 unsigned int
1667 pubkey_nbits( int algo, gcry_mpi_t *key )
1668 {
1669   int rc, nbits;
1670   gcry_sexp_t sexp;
1671
1672   if (algo == PUBKEY_ALGO_DSA
1673       && key[0] && key[1] && key[2] && key[3])
1674     {
1675       rc = gcry_sexp_build (&sexp, NULL,
1676                             "(public-key(dsa(p%m)(q%m)(g%m)(y%m)))",
1677                             key[0], key[1], key[2], key[3] );
1678     }
1679   else if ((algo == PUBKEY_ALGO_ELGAMAL || algo == PUBKEY_ALGO_ELGAMAL_E)
1680            && key[0] && key[1] && key[2])
1681     {
1682       rc = gcry_sexp_build (&sexp, NULL,
1683                             "(public-key(elg(p%m)(g%m)(y%m)))",
1684                             key[0], key[1], key[2] );
1685     }
1686   else if (is_RSA (algo)
1687            && key[0] && key[1])
1688     {
1689       rc = gcry_sexp_build (&sexp, NULL,
1690                             "(public-key(rsa(n%m)(e%m)))",
1691                             key[0], key[1] );
1692     }
1693   else if ((algo == PUBKEY_ALGO_ECDSA || algo == PUBKEY_ALGO_ECDH
1694             || algo == PUBKEY_ALGO_EDDSA)
1695            && key[0] && key[1])
1696     {
1697       char *curve = openpgp_oid_to_str (key[0]);
1698       if (!curve)
1699         rc = gpg_error_from_syserror ();
1700       else
1701         {
1702           rc = gcry_sexp_build (&sexp, NULL,
1703                                 "(public-key(ecc(curve%s)(q%m)))",
1704                                 curve, key[1]);
1705           xfree (curve);
1706         }
1707     }
1708   else
1709     return 0;
1710
1711   if (rc)
1712     BUG ();
1713
1714   nbits = gcry_pk_get_nbits (sexp);
1715   gcry_sexp_release (sexp);
1716   return nbits;
1717 }
1718
1719
1720
1721 int
1722 mpi_print (estream_t fp, gcry_mpi_t a, int mode)
1723 {
1724   int n=0;
1725
1726   if (!a)
1727     return es_fprintf (fp, "[MPI_NULL]");
1728   if (!mode)
1729     {
1730       unsigned int n1;
1731       n1 = gcry_mpi_get_nbits(a);
1732       n += es_fprintf (fp, "[%u bits]", n1);
1733     }
1734   else if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE))
1735     {
1736       unsigned int nbits;
1737       unsigned char *p = gcry_mpi_get_opaque (a, &nbits);
1738       if (!p)
1739         n += es_fprintf (fp, "[invalid opaque value]");
1740       else
1741         {
1742           nbits = (nbits + 7)/8;
1743           for (; nbits; nbits--, p++)
1744             n += es_fprintf (fp, "%02X", *p);
1745         }
1746     }
1747   else
1748     {
1749       unsigned char *buffer;
1750
1751       if (gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buffer, NULL, a))
1752         BUG ();
1753       es_fputs (buffer, fp);
1754       n += strlen (buffer);
1755       gcry_free (buffer);
1756     }
1757   return n;
1758 }
1759
1760
1761 /* pkey[1] or skey[1] is Q for ECDSA, which is an uncompressed point,
1762    i.e.  04 <x> <y> */
1763 unsigned int
1764 ecdsa_qbits_from_Q (unsigned int qbits)
1765 {
1766   if ((qbits%8) > 3)
1767     {
1768       log_error (_("ECDSA public key is expected to be in SEC encoding "
1769                    "multiple of 8 bits\n"));
1770       return 0;
1771     }
1772   qbits -= qbits%8;
1773   qbits /= 2;
1774   return qbits;
1775 }