~ubuntu-branches/ubuntu/feisty/gnupg2/feisty

« back to all changes in this revision

Viewing changes to g10/misc.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-07-11 11:38:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060711113813-zaw7unlbuh7gyxtl
Tags: 1.9.21-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* misc.c -  miscellaneous functions
2
 
 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3
 
 *               2003 Free Software Foundation, Inc.
 
1
/* misc.c - miscellaneous functions
 
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
 
3
 *               2005, 2006 Free Software Foundation, Inc.
4
4
 *
5
5
 * This file is part of GnuPG.
6
6
 *
16
16
 *
17
17
 * You should have received a copy of the GNU General Public License
18
18
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 
20
 * USA.
20
21
 */
21
22
 
22
23
#include <config.h>
25
26
#include <string.h>
26
27
#include <unistd.h>
27
28
#include <errno.h>
28
 
#include <assert.h>
29
29
#if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2
30
30
#include <asm/sysinfo.h>
31
31
#include <asm/unistd.h>
35
35
#include <sys/time.h>
36
36
#include <sys/resource.h>
37
37
#endif
 
38
#ifdef ENABLE_SELINUX_HACKS
 
39
#include <sys/stat.h>
 
40
#endif
 
41
 
 
42
#ifdef HAVE_W32_SYSTEM
 
43
#include <time.h>
 
44
#include <process.h>
 
45
#include <windows.h> 
 
46
#include <shlobj.h>
 
47
#ifndef CSIDL_APPDATA
 
48
#define CSIDL_APPDATA 0x001a
 
49
#endif
 
50
#ifndef CSIDL_LOCAL_APPDATA
 
51
#define CSIDL_LOCAL_APPDATA 0x001c
 
52
#endif
 
53
#ifndef CSIDL_FLAG_CREATE
 
54
#define CSIDL_FLAG_CREATE 0x8000
 
55
#endif
 
56
#endif /*HAVE_W32_SYSTEM*/
38
57
 
39
58
#include "gpg.h"
 
59
#ifdef HAVE_W32_SYSTEM
 
60
# include "errors.h"
 
61
# include "dynload.h"
 
62
#endif /*HAVE_W32_SYSTEM*/
40
63
#include "util.h"
41
64
#include "main.h"
42
65
#include "photoid.h"
43
66
#include "options.h"
44
67
#include "i18n.h"
45
68
 
46
 
#define MAX_EXTERN_MPI_BITS 16384
 
69
 
 
70
static int
 
71
string_count_chr (const char *string, int c)
 
72
{
 
73
  int count;
 
74
 
 
75
  for (count=0; *string; string++ )
 
76
    if ( *string == c )
 
77
      count++;
 
78
  return count;
 
79
}
 
80
 
 
81
 
 
82
 
 
83
#ifdef ENABLE_SELINUX_HACKS
 
84
/* A object and a global variable to keep track of files marked as
 
85
   secured. */
 
86
struct secured_file_item 
 
87
{
 
88
  struct secured_file_item *next;
 
89
  ino_t ino;
 
90
  dev_t dev;
 
91
};
 
92
static struct secured_file_item *secured_files;
 
93
#endif /*ENABLE_SELINUX_HACKS*/
 
94
 
47
95
 
48
96
 
49
97
#if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2
92
140
}
93
141
 
94
142
 
 
143
/* For the sake of SELinux we want to restrict access through gpg to
 
144
   certain files we keep under our own control.  This function
 
145
   registers such a file and is_secured_file may then be used to
 
146
   check whether a file has ben registered as secured. */
 
147
void
 
148
register_secured_file (const char *fname)
 
149
{
 
150
#ifdef ENABLE_SELINUX_HACKS
 
151
  struct stat buf;
 
152
  struct secured_file_item *sf;
 
153
 
 
154
  /* Note that we stop immediatley if something goes wrong here. */
 
155
  if (stat (fname, &buf))
 
156
    log_fatal (_("fstat of `%s' failed in %s: %s\n"), fname, 
 
157
               "register_secured_file", strerror (errno));
 
158
/*   log_debug ("registering `%s' i=%lu.%lu\n", fname, */
 
159
/*              (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
 
160
  for (sf=secured_files; sf; sf = sf->next)
 
161
    {
 
162
      if (sf->ino == buf.st_ino && sf->dev == buf.st_dev)
 
163
        return; /* Already registered.  */
 
164
    }
 
165
 
 
166
  sf = xmalloc (sizeof *sf);
 
167
  sf->ino = buf.st_ino;
 
168
  sf->dev = buf.st_dev;
 
169
  sf->next = secured_files;
 
170
  secured_files = sf;
 
171
#endif /*ENABLE_SELINUX_HACKS*/
 
172
}
 
173
 
 
174
/* Remove a file registered as secure. */
 
175
void
 
176
unregister_secured_file (const char *fname)
 
177
{
 
178
#ifdef ENABLE_SELINUX_HACKS
 
179
  struct stat buf;
 
180
  struct secured_file_item *sf, *sfprev;
 
181
 
 
182
  if (stat (fname, &buf))
 
183
    {
 
184
      log_error (_("fstat of `%s' failed in %s: %s\n"), fname,
 
185
                 "unregister_secured_file", strerror (errno));
 
186
      return;
 
187
    }
 
188
/*   log_debug ("unregistering `%s' i=%lu.%lu\n", fname,  */
 
189
/*              (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
 
190
  for (sfprev=NULL,sf=secured_files; sf; sfprev=sf, sf = sf->next)
 
191
    {
 
192
      if (sf->ino == buf.st_ino && sf->dev == buf.st_dev)
 
193
        {
 
194
          if (sfprev)
 
195
            sfprev->next = sf->next;
 
196
          else
 
197
            secured_files = sf->next;
 
198
          xfree (sf);
 
199
          return;
 
200
        }
 
201
    }
 
202
#endif /*ENABLE_SELINUX_HACKS*/
 
203
}
 
204
 
 
205
/* Return true if FD is corresponds to a secured file.  Using -1 for
 
206
   FS is allowed and will return false. */ 
 
207
int 
 
208
is_secured_file (int fd)
 
209
{
 
210
#ifdef ENABLE_SELINUX_HACKS
 
211
  struct stat buf;
 
212
  struct secured_file_item *sf;
 
213
 
 
214
  if (fd == -1)
 
215
    return 0; /* No file descriptor so it can't be secured either.  */
 
216
 
 
217
  /* Note that we print out a error here and claim that a file is
 
218
     secure if something went wrong. */
 
219
  if (fstat (fd, &buf))
 
220
    {
 
221
      log_error (_("fstat(%d) failed in %s: %s\n"), fd, 
 
222
                 "is_secured_file", strerror (errno));
 
223
      return 1;
 
224
    }
 
225
/*   log_debug ("is_secured_file (%d) i=%lu.%lu\n", fd, */
 
226
/*              (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
 
227
  for (sf=secured_files; sf; sf = sf->next)
 
228
    {
 
229
      if (sf->ino == buf.st_ino && sf->dev == buf.st_dev)
 
230
        return 1; /* Yes.  */
 
231
    }
 
232
#endif /*ENABLE_SELINUX_HACKS*/
 
233
  return 0; /* No. */
 
234
}
 
235
 
 
236
/* Return true if FNAME is corresponds to a secured file.  Using NULL,
 
237
   "" or "-" for FS is allowed and will return false. This function is
 
238
   used before creating a file, thus it won't fail if the file does
 
239
   not exist. */ 
 
240
int 
 
241
is_secured_filename (const char *fname)
 
242
{
 
243
#ifdef ENABLE_SELINUX_HACKS
 
244
  struct stat buf;
 
245
  struct secured_file_item *sf;
 
246
 
 
247
  if (iobuf_is_pipe_filename (fname) || !*fname)
 
248
    return 0; 
 
249
 
 
250
  /* Note that we print out a error here and claim that a file is
 
251
     secure if something went wrong. */
 
252
  if (stat (fname, &buf))
 
253
    {
 
254
      if (errno == ENOENT || errno == EPERM || errno == EACCES)
 
255
        return 0;
 
256
      log_error (_("fstat of `%s' failed in %s: %s\n"), fname,
 
257
                 "is_secured_filename", strerror (errno));
 
258
      return 1;
 
259
    }
 
260
/*   log_debug ("is_secured_filename (%s) i=%lu.%lu\n", fname, */
 
261
/*              (unsigned long)buf.st_dev, (unsigned long)buf.st_ino); */
 
262
  for (sf=secured_files; sf; sf = sf->next)
 
263
    {
 
264
      if (sf->ino == buf.st_ino && sf->dev == buf.st_dev)
 
265
        return 1; /* Yes.  */
 
266
    }
 
267
#endif /*ENABLE_SELINUX_HACKS*/
 
268
  return 0; /* No. */
 
269
}
 
270
 
 
271
 
95
272
 
96
273
u16
97
274
checksum_u16( unsigned n )
115
292
}
116
293
 
117
294
u16
118
 
checksum_mpi( gcry_mpi_t a )
 
295
checksum_mpi (gcry_mpi_t a)
119
296
{
120
 
  int rc;
121
297
  u16 csum;
122
298
  byte *buffer;
123
 
  size_t nbytes;
 
299
  unsigned int nbytes;
 
300
  unsigned int nbits;
124
301
 
125
 
  rc = gcry_mpi_print( GCRYMPI_FMT_PGP, NULL, 0, &nbytes, a );
126
 
  if (rc)
127
 
    BUG ();
128
 
  /* fixme: for numbers not in secure memory we should use a stack
129
 
   * based buffer and only allocate a larger one if mpi_print return
130
 
   * an error */
131
 
  buffer = gcry_is_secure(a)? gcry_xmalloc_secure(nbytes):gcry_xmalloc(nbytes);
132
 
  rc = gcry_mpi_print (GCRYMPI_FMT_PGP, buffer, nbytes, NULL, a );
133
 
  if (rc)
134
 
    BUG ();
135
 
  csum = checksum (buffer, nbytes );
136
 
  xfree (buffer );
 
302
  if ( gcry_mpi_print (GCRYMPI_FMT_PGP, NULL, 0, &nbytes, a) )
 
303
    BUG ();
 
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
 
306
   * an error. */
 
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) )
 
310
    BUG ();
 
311
  nbits = gcry_mpi_get_nbits (a);
 
312
  csum = checksum_u16 (nbits);
 
313
  csum += checksum (buffer, nbytes);
 
314
  xfree (buffer);
137
315
  return csum;
138
316
}
139
317
 
148
326
    return a;
149
327
}
150
328
 
151
 
 
152
 
static void
153
 
no_exp_algo(void)
154
 
{
155
 
    static int did_note = 0;
156
 
 
157
 
    if( !did_note ) {
158
 
        did_note = 1;
159
 
        log_info(_("Experimental algorithms should not be used!\n"));
160
 
    }
161
 
}
162
 
 
163
329
void
164
330
print_pubkey_algo_note( int algo )
165
331
{
166
 
    if( algo >= 100 && algo <= 110 )
167
 
        no_exp_algo();
 
332
  if(algo >= 100 && algo <= 110)
 
333
    {
 
334
      static int warn=0;
 
335
      if(!warn)
 
336
        {
 
337
          warn=1;
 
338
          log_info (_("WARNING: using experimental public key algorithm %s\n"),
 
339
                    gcry_pk_algo_name (algo));
 
340
        }
 
341
    }
168
342
}
169
343
 
170
344
void
171
345
print_cipher_algo_note( int algo )
172
346
{
173
 
    if( algo >= 100 && algo <= 110 )
174
 
        no_exp_algo();
175
 
    else if(    algo == CIPHER_ALGO_3DES
176
 
             || algo == CIPHER_ALGO_CAST5
177
 
             || algo == CIPHER_ALGO_BLOWFISH
178
 
             || algo == CIPHER_ALGO_TWOFISH
179
 
             || algo == CIPHER_ALGO_RIJNDAEL
180
 
             || algo == CIPHER_ALGO_RIJNDAEL192
181
 
             || algo == CIPHER_ALGO_RIJNDAEL256
182
 
           )
183
 
        ;
184
 
    else {
185
 
        static int did_note = 0;
186
 
 
187
 
        if( !did_note ) {
188
 
            did_note = 1;
189
 
            log_info(_("this cipher algorithm is deprecated; "
190
 
                       "please use a more standard one!\n"));
 
347
  if(algo >= 100 && algo <= 110)
 
348
    {
 
349
      static int warn=0;
 
350
      if(!warn)
 
351
        {
 
352
          warn=1;
 
353
          log_info (_("WARNING: using experimental cipher algorithm %s\n"),
 
354
                    gcry_cipher_algo_name (algo));
191
355
        }
192
356
    }
193
357
}
195
359
void
196
360
print_digest_algo_note( int algo )
197
361
{
198
 
    if( algo >= 100 && algo <= 110 )
199
 
        no_exp_algo();
 
362
  if(algo >= 100 && algo <= 110)
 
363
    {
 
364
      static int warn=0;
 
365
      if(!warn)
 
366
        {
 
367
          warn=1;
 
368
          log_info (_("WARNING: using experimental digest algorithm %s\n"),
 
369
                    gcry_md_algo_name (algo));
 
370
        }
 
371
    }
 
372
  else if(algo==DIGEST_ALGO_MD5)
 
373
    log_info (_("WARNING: digest algorithm %s is deprecated\n"),
 
374
              gcry_md_algo_name (algo));
200
375
}
201
376
 
202
 
 
203
377
/* Return a string which is used as a kind of process ID */
204
378
const byte *
205
379
get_session_marker( size_t *rlen )
206
380
{
207
 
    static byte marker[SIZEOF_UNSIGNED_LONG*2];
208
 
    static int initialized;
209
 
 
210
 
    if ( !initialized ) {
211
 
        volatile ulong aa, bb; /* we really want the uninitialized value */
212
 
        ulong a, b;
213
 
 
214
 
        initialized = 1;
215
 
        /* also this marker is guessable it is not easy to use this 
216
 
         * for a faked control packet because an attacker does not
217
 
         * have enough control about the time the verification does 
218
 
         * take place.  Of course, we can add just more random but 
219
 
         * than we need the random generator even for verification
220
 
         * tasks - which does not make sense. */
221
 
        a = aa ^ (ulong)getpid();
222
 
        b = bb ^ (ulong)time(NULL);
223
 
        memcpy( marker, &a, SIZEOF_UNSIGNED_LONG );
224
 
        memcpy( marker+SIZEOF_UNSIGNED_LONG, &b, SIZEOF_UNSIGNED_LONG );
 
381
  static byte marker[SIZEOF_UNSIGNED_LONG*2];
 
382
  static int initialized;
 
383
  
 
384
  if ( !initialized )
 
385
    {
 
386
      volatile ulong aa, bb; /* We really want the uninitialized value. */
 
387
      ulong a, b;
 
388
      
 
389
      initialized = 1;
 
390
      /* Although this marker is guessable it is not easy to use this
 
391
       * for a faked control packet because an attacker does not have
 
392
       * enough control about the time the verification takes place.
 
393
       * Of course, we could add just more random but than we need the
 
394
       * random generator even for verification tasks - which does not
 
395
       * make sense. */
 
396
      a = aa ^ (ulong)getpid();
 
397
      b = bb ^ (ulong)time(NULL);
 
398
      memcpy ( marker, &a, SIZEOF_UNSIGNED_LONG );
 
399
      memcpy ( marker+SIZEOF_UNSIGNED_LONG, &b, SIZEOF_UNSIGNED_LONG );
225
400
    }
226
 
    *rlen = sizeof(marker);
227
 
    return marker;
 
401
  *rlen = sizeof(marker);
 
402
  return marker;
228
403
}
229
404
 
230
405
/****************
231
 
 * Wrapper around the libgcrypt function with addional checks on
232
 
 * openPGP contraints for the algo ID.
 
406
 * Wrapper around the libgcrypt function with additonal checks on
 
407
 * the OpenPGP contraints for the algo ID.
233
408
 */
234
409
int
235
410
openpgp_cipher_test_algo( int algo )
236
411
{
237
 
    if( algo < 0 || algo > 110 )
238
 
        return GPG_ERR_CIPHER_ALGO;
239
 
    return gcry_cipher_test_algo (algo);
240
 
}
241
 
 
242
 
int
243
 
openpgp_pk_test_algo( int algo, unsigned int usage_flags )
244
 
{
245
 
  size_t value = usage_flags;
246
 
 
247
 
  if (algo == GCRY_PK_ELG_E)
248
 
    algo = GCRY_PK_ELG;
249
 
#ifdef __GNUC__
250
 
#warning need to handle the usage here?
251
 
#endif
252
 
  if (algo < 0 || algo > 110)
253
 
    return GPG_ERR_PUBKEY_ALGO;
254
 
  return gcry_pk_algo_info (algo, GCRYCTL_TEST_ALGO, NULL, &value);
 
412
  if ( algo < 0 || algo > 110 )
 
413
    return gpg_error (GPG_ERR_CIPHER_ALGO);
 
414
  return gcry_cipher_test_algo (algo);
 
415
}
 
416
 
 
417
int
 
418
openpgp_pk_test_algo( int algo )
 
419
{
 
420
  if (algo == GCRY_PK_ELG_E)
 
421
    algo = GCRY_PK_ELG;
 
422
 
 
423
  if (algo < 0 || algo > 110)
 
424
    return gpg_error (GPG_ERR_PUBKEY_ALGO);
 
425
  return gcry_pk_test_algo (algo);
 
426
}
 
427
 
 
428
int
 
429
openpgp_pk_test_algo2( int algo, unsigned int use )
 
430
{
 
431
  int use_buf = use;
 
432
 
 
433
  if (algo == GCRY_PK_ELG_E)
 
434
    algo = GCRY_PK_ELG;
 
435
 
 
436
  if (algo < 0 || algo > 110)
 
437
    return gpg_error (GPG_ERR_PUBKEY_ALGO);
 
438
 
 
439
  return gcry_pk_algo_info (algo, GCRYCTL_TEST_ALGO, NULL, &use_buf);
255
440
}
256
441
 
257
442
int 
259
444
{
260
445
    int use = 0; 
261
446
    
262
 
    /* they are hardwired in gpg 1.0 */
 
447
    /* They are hardwired in gpg 1.0. */
263
448
    switch ( algo ) {    
264
449
      case PUBKEY_ALGO_RSA:
265
 
          use = PUBKEY_USAGE_SIG | PUBKEY_USAGE_ENC | PUBKEY_USAGE_AUTH;
 
450
          use = (PUBKEY_USAGE_CERT | PUBKEY_USAGE_SIG
 
451
                 | PUBKEY_USAGE_ENC | PUBKEY_USAGE_AUTH);
266
452
          break;
267
453
      case PUBKEY_ALGO_RSA_E:
268
454
          use = PUBKEY_USAGE_ENC;
269
455
          break;
270
456
      case PUBKEY_ALGO_RSA_S:
271
 
          use = PUBKEY_USAGE_SIG;
 
457
          use = PUBKEY_USAGE_CERT | PUBKEY_USAGE_SIG;
272
458
          break;
273
459
      case PUBKEY_ALGO_ELGAMAL_E:
274
460
          use = PUBKEY_USAGE_ENC;
275
461
          break;
276
462
      case PUBKEY_ALGO_DSA:  
277
 
          use = PUBKEY_USAGE_SIG | PUBKEY_USAGE_AUTH;
278
 
          break;
279
 
      case PUBKEY_ALGO_ELGAMAL:
280
 
          use = PUBKEY_USAGE_SIG | PUBKEY_USAGE_ENC | PUBKEY_USAGE_AUTH;
 
463
          use = PUBKEY_USAGE_CERT | PUBKEY_USAGE_SIG | PUBKEY_USAGE_AUTH;
281
464
          break;
282
465
      default:
283
466
          break;
288
471
int
289
472
openpgp_md_test_algo( int algo )
290
473
{
291
 
    if( algo < 0 || algo > 110 )
292
 
        return GPG_ERR_DIGEST_ALGO;
293
 
    return gcry_md_test_algo (algo);
294
 
}
295
 
 
296
 
int
297
 
openpgp_md_map_name (const char *string)
298
 
{
299
 
  int i = gcry_md_map_name (string);
300
 
 
301
 
  if (!i && (string[0]=='H' || string[0]=='h'))
302
 
    { /* Didn't find it, so try the Hx format */
303
 
      long val;
304
 
      char *endptr;
305
 
 
306
 
      string++;
307
 
      
308
 
      val=strtol(string,&endptr,10);
309
 
      if (*string!='\0' && *endptr=='\0' && !openpgp_md_test_algo(val))
310
 
        i = val;
311
 
    }
312
 
  return i < 0 || i > 110? 0 : i;
313
 
}
314
 
 
315
 
int
316
 
openpgp_cipher_map_name (const char *string)
317
 
{
318
 
  int i = gcry_cipher_map_name (string);
319
 
 
320
 
  if (!i && (string[0]=='S' || string[0]=='s'))
321
 
    { /* Didn't find it, so try the Sx format */
322
 
      long val;
323
 
      char *endptr;
324
 
 
325
 
      string++;
326
 
      
327
 
      val=strtol(string,&endptr,10);
328
 
      if (*string!='\0' && *endptr=='\0' && !openpgp_cipher_test_algo(val))
329
 
        i = val;
330
 
    }
331
 
  return i < 0 || i > 110? 0 : i;
332
 
}
333
 
 
334
 
int
335
 
openpgp_pk_map_name (const char *string)
336
 
{
337
 
  int i = gcry_pk_map_name (string);
338
 
  return i < 0 || i > 110? 0 : i;
 
474
  if (algo < 0 || algo > 110)
 
475
    return gpg_error (GPG_ERR_DIGEST_ALGO);
 
476
  return gcry_md_test_algo (algo);
339
477
}
340
478
 
341
479
#ifdef USE_IDEA
348
486
  if(!warned || show)
349
487
    {
350
488
      log_info(_("the IDEA cipher plugin is not present\n"));
351
 
      log_info(_("please see http://www.gnupg.org/why-not-idea.html "
352
 
                 "for more information\n"));
 
489
      log_info(_("please see %s for more information\n"),
 
490
               "http://www.gnupg.org/faq/why-not-idea.html");
353
491
      warned=1;
354
492
    }
355
493
}
356
494
#endif
357
495
 
358
 
/* Expand %-strings.  Returns a string which must be m_freed.  Returns
 
496
static unsigned long get_signature_count(PKT_secret_key *sk)
 
497
{
 
498
#ifdef ENABLE_CARD_SUPPORT
 
499
  if(sk && sk->is_protected && sk->protect.s2k.mode==1002)
 
500
    {
 
501
      struct agent_card_info_s info;
 
502
      if(agent_scd_getattr("SIG-COUNTER",&info)==0)
 
503
        return info.sig_counter;
 
504
    }  
 
505
#endif
 
506
 
 
507
  /* How to do this without a card? */
 
508
 
 
509
  return 0;
 
510
}
 
511
 
 
512
/* Expand %-strings.  Returns a string which must be xfreed.  Returns
359
513
   NULL if the string cannot be expanded (too large). */
360
514
char *
361
515
pct_expando(const char *string,struct expando_args *args)
387
541
            goto fail;
388
542
 
389
543
          maxlen+=1024;
390
 
          ret= xrealloc(ret,maxlen);
 
544
          ret=xrealloc(ret,maxlen);
391
545
        }
392
546
 
393
547
      done=0;
434
588
                }
435
589
              break;
436
590
 
 
591
            case 'c': /* signature count from card, if any. */
 
592
              if(idx+10<maxlen)
 
593
                {
 
594
                  sprintf(&ret[idx],"%lu",get_signature_count(args->sk));
 
595
                  idx+=strlen(&ret[idx]);
 
596
                  done=1;
 
597
                }             
 
598
              break;
 
599
 
437
600
            case 'p': /* primary pk fingerprint of a sk */
438
601
            case 'f': /* pk fingerprint */
439
602
            case 'g': /* sk fingerprint */
442
605
                size_t len;
443
606
                int i;
444
607
 
445
 
                if( ch[1]=='p' && args->sk)
 
608
                if((*(ch+1))=='p' && args->sk)
446
609
                  {
447
610
                    if(args->sk->is_primary)
448
611
                      fingerprint_from_sk(args->sk,array,&len);
449
612
                    else if(args->sk->main_keyid[0] || args->sk->main_keyid[1])
450
613
                      {
451
 
                        PKT_public_key *pk= xcalloc(1, sizeof(PKT_public_key));
 
614
                        PKT_public_key *pk=
 
615
                          xmalloc_clear(sizeof(PKT_public_key));
452
616
 
453
617
                        if(get_pubkey_fast(pk,args->sk->main_keyid)==0)
454
618
                          fingerprint_from_pk(pk,array,&len);
459
623
                    else
460
624
                      memset(array,0,(len=MAX_FINGERPRINT_LEN));
461
625
                  }
462
 
                else if( ch[1]=='f' && args->pk)
 
626
                else if((*(ch+1))=='f' && args->pk)
463
627
                  fingerprint_from_pk(args->pk,array,&len);
464
 
                else if( ch[1]=='g' && args->sk)
 
628
                else if((*(ch+1))=='g' && args->sk)
465
629
                  fingerprint_from_sk(args->sk,array,&len);
466
630
                else
467
 
                  memset(array, 0, (len=MAX_FINGERPRINT_LEN));
 
631
                  memset(array,0,(len=MAX_FINGERPRINT_LEN));
468
632
 
469
633
                if(idx+(len*2)<maxlen)
470
634
                  {
539
703
  return ret;
540
704
 
541
705
 fail:
542
 
  xfree (ret);
 
706
  xfree(ret);
543
707
  return NULL;
544
708
}
545
709
 
546
 
int
547
 
hextobyte( const char *s )
548
 
{
549
 
    int c;
550
 
 
551
 
    if( *s >= '0' && *s <= '9' )
552
 
        c = 16 * (*s - '0');
553
 
    else if( *s >= 'A' && *s <= 'F' )
554
 
        c = 16 * (10 + *s - 'A');
555
 
    else if( *s >= 'a' && *s <= 'f' )
556
 
        c = 16 * (10 + *s - 'a');
557
 
    else
558
 
        return -1;
559
 
    s++;
560
 
    if( *s >= '0' && *s <= '9' )
561
 
        c += *s - '0';
562
 
    else if( *s >= 'A' && *s <= 'F' )
563
 
        c += 10 + *s - 'A';
564
 
    else if( *s >= 'a' && *s <= 'f' )
565
 
        c += 10 + *s - 'a';
566
 
    else
567
 
        return -1;
568
 
    return c;
569
 
}
570
 
 
571
710
void
572
711
deprecated_warning(const char *configname,unsigned int configlineno,
573
712
                   const char *option,const char *repl1,const char *repl2)
589
728
  log_info(_("please use \"%s%s\" instead\n"),repl1,repl2);
590
729
}
591
730
 
 
731
 
 
732
void
 
733
deprecated_command (const char *name)
 
734
{
 
735
  log_info(_("WARNING: \"%s\" is a deprecated command - do not use it\n"),
 
736
           name);
 
737
}
 
738
 
 
739
 
 
740
/*
 
741
 * Wrapper around gcry_cipher_map_name to provide a fallback using the
 
742
 * "Sn" syntax as used by the preference strings.
 
743
 */
 
744
int 
 
745
string_to_cipher_algo (const char *string) 
 
746
 
747
  int val;
 
748
 
 
749
  val = gcry_cipher_map_name (string);
 
750
  if (!val && string && (string[0]=='S' || string[0]=='s'))
 
751
    {
 
752
      char *endptr;
 
753
 
 
754
      string++;
 
755
      val = strtol (string, &endptr, 10);
 
756
      if (!*string || *endptr || openpgp_cipher_test_algo (val))
 
757
        val = 0;
 
758
    }
 
759
 
 
760
  return val;
 
761
}
 
762
 
 
763
/*
 
764
 * Wrapper around gcry_md_map_name to provide a fallback using the
 
765
 * "Hn" syntax as used by the preference strings.
 
766
 */
 
767
int 
 
768
string_to_digest_algo (const char *string) 
 
769
 
770
  int val;
 
771
 
 
772
  val = gcry_md_map_name (string);
 
773
  if (!val && string && (string[0]=='H' || string[0]=='h'))
 
774
    {
 
775
      char *endptr;
 
776
 
 
777
      string++;
 
778
      val = strtol (string, &endptr, 10);
 
779
      if (!*string || *endptr || openpgp_md_test_algo (val))
 
780
        val = 0;
 
781
    }
 
782
 
 
783
  return val;
 
784
}
 
785
 
 
786
 
 
787
 
592
788
const char *
593
789
compress_algo_to_string(int algo)
594
790
{
595
 
  const char *s="?";
 
791
  const char *s=NULL;
596
792
 
597
793
  switch(algo)
598
794
    {
599
 
    case 0:
600
 
      s="Uncompressed";
 
795
    case COMPRESS_ALGO_NONE:
 
796
      s=_("Uncompressed");
601
797
      break;
602
798
 
603
 
    case 1:
 
799
    case COMPRESS_ALGO_ZIP:
604
800
      s="ZIP";
605
801
      break;
606
802
 
607
 
    case 2:
 
803
    case COMPRESS_ALGO_ZLIB:
608
804
      s="ZLIB";
609
805
      break;
 
806
 
 
807
#ifdef HAVE_BZIP2
 
808
    case COMPRESS_ALGO_BZIP2:
 
809
      s="BZIP2";
 
810
      break;
 
811
#endif
610
812
    }
611
813
 
612
814
  return s;
615
817
int
616
818
string_to_compress_algo(const char *string)
617
819
{
618
 
  if(ascii_strcasecmp(string,"uncompressed")==0)
 
820
  /* TRANSLATORS: See doc/TRANSLATE about this string. */
 
821
  if(match_multistr(_("uncompressed|none"),string))
 
822
    return 0;
 
823
  else if(ascii_strcasecmp(string,"uncompressed")==0)
 
824
    return 0;
 
825
  else if(ascii_strcasecmp(string,"none")==0)
619
826
    return 0;
620
827
  else if(ascii_strcasecmp(string,"zip")==0)
621
828
    return 1;
622
829
  else if(ascii_strcasecmp(string,"zlib")==0)
623
830
    return 2;
 
831
#ifdef HAVE_BZIP2
 
832
  else if(ascii_strcasecmp(string,"bzip2")==0)
 
833
    return 3;
 
834
#endif
624
835
  else if(ascii_strcasecmp(string,"z0")==0)
625
836
    return 0;
626
837
  else if(ascii_strcasecmp(string,"z1")==0)
627
838
    return 1;
628
839
  else if(ascii_strcasecmp(string,"z2")==0)
629
840
    return 2;
 
841
#ifdef HAVE_BZIP2
 
842
  else if(ascii_strcasecmp(string,"z3")==0)
 
843
    return 3;
 
844
#endif
630
845
  else
631
846
    return -1;
632
847
}
634
849
int
635
850
check_compress_algo(int algo)
636
851
{
 
852
#ifdef HAVE_BZIP2
 
853
  if(algo>=0 && algo<=3)
 
854
    return 0;
 
855
#else
637
856
  if(algo>=0 && algo<=2)
638
857
    return 0;
 
858
#endif
639
859
 
640
 
  return GPG_ERR_COMPR_ALGO;
 
860
  return G10ERR_COMPR_ALGO;
641
861
}
642
862
 
643
863
int
652
872
}
653
873
 
654
874
/* There is no default_digest_algo function, but see
655
 
   sign.c:hash_for */
 
875
   sign.c:hash_for() */
656
876
 
657
877
int
658
878
default_compress_algo(void)
659
879
{
660
 
  if(opt.def_compress_algo!=-1)
661
 
    return opt.def_compress_algo;
 
880
  if(opt.compress_algo!=-1)
 
881
    return opt.compress_algo;
662
882
  else if(opt.personal_compress_prefs)
663
883
    return opt.personal_compress_prefs[0].value;
664
884
  else
712
932
  opt.compliance=CO_GNUPG;
713
933
}
714
934
 
 
935
/* Break a string into successive option pieces.  Accepts single word
 
936
   options and key=value argument options. */
 
937
char *
 
938
optsep(char **stringp)
 
939
{
 
940
  char *tok,*end;
 
941
 
 
942
  tok=*stringp;
 
943
  if(tok)
 
944
    {
 
945
      end=strpbrk(tok," ,=");
 
946
      if(end)
 
947
        {
 
948
          int sawequals=0;
 
949
          char *ptr=end;
 
950
 
 
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. */
 
954
 
 
955
          while(*ptr)
 
956
            {
 
957
              if(*ptr=='=')
 
958
                sawequals=1;
 
959
              else if(*ptr!=' ')
 
960
                break;
 
961
              ptr++;
 
962
            }
 
963
 
 
964
          /* There is an argument, so grab that too.  At this point,
 
965
             ptr points to the first character of the argument. */
 
966
          if(sawequals)
 
967
            {
 
968
              /* Is it a quoted argument? */
 
969
              if(*ptr=='"')
 
970
                {
 
971
                  ptr++;
 
972
                  end=strchr(ptr,'"');
 
973
                  if(end)
 
974
                    end++;
 
975
                }
 
976
              else
 
977
                end=strpbrk(ptr," ,");
 
978
            }
 
979
 
 
980
          if(end && *end)
 
981
            {
 
982
              *end='\0';
 
983
              *stringp=end+1;
 
984
            }
 
985
          else
 
986
            *stringp=NULL;
 
987
        }
 
988
      else
 
989
        *stringp=NULL;
 
990
    }
 
991
 
 
992
  return tok;
 
993
}
 
994
 
 
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
 
997
   part. */
 
998
char *
 
999
argsplit(char *string)
 
1000
{
 
1001
  char *equals,*arg=NULL;
 
1002
 
 
1003
  equals=strchr(string,'=');
 
1004
  if(equals)
 
1005
    {
 
1006
      char *quote,*space;
 
1007
 
 
1008
      *equals='\0';
 
1009
      arg=equals+1;
 
1010
 
 
1011
      /* Quoted arg? */
 
1012
      quote=strchr(arg,'"');
 
1013
      if(quote)
 
1014
        {
 
1015
          arg=quote+1;
 
1016
 
 
1017
          quote=strchr(arg,'"');
 
1018
          if(quote)
 
1019
            *quote='\0';
 
1020
        }
 
1021
      else
 
1022
        {
 
1023
          size_t spaces;
 
1024
 
 
1025
          /* Trim leading spaces off of the arg */
 
1026
          spaces=strspn(arg," ");
 
1027
          arg+=spaces;
 
1028
        }
 
1029
 
 
1030
      /* Trim tailing spaces off of the tag */
 
1031
      space=strchr(string,' ');
 
1032
      if(space)
 
1033
        *space='\0';
 
1034
    }
 
1035
 
 
1036
  return arg;
 
1037
}
 
1038
 
 
1039
/* Return the length of the initial token, leaving off any
 
1040
   argument. */
 
1041
static size_t
 
1042
optlen(const char *s)
 
1043
{
 
1044
  char *end=strpbrk(s," =");
 
1045
 
 
1046
  if(end)
 
1047
    return end-s;
 
1048
  else
 
1049
    return strlen(s);
 
1050
}
 
1051
 
715
1052
int
716
 
parse_options(char *str,unsigned int *options,struct parse_options *opts)
 
1053
parse_options(char *str,unsigned int *options,
 
1054
              struct parse_options *opts,int noisy)
717
1055
{
718
1056
  char *tok;
719
1057
 
720
 
  while((tok=strsep(&str," ,")))
 
1058
  if (str && !strcmp (str, "help"))
 
1059
    {
 
1060
      int i,maxlen=0;
 
1061
 
 
1062
      /* Figure out the longest option name so we can line these up
 
1063
         neatly. */
 
1064
      for(i=0;opts[i].name;i++)
 
1065
        if(opts[i].help && maxlen<strlen(opts[i].name))
 
1066
          maxlen=strlen(opts[i].name);
 
1067
 
 
1068
      for(i=0;opts[i].name;i++)
 
1069
        if(opts[i].help)
 
1070
          printf("%s%*s%s\n",opts[i].name,
 
1071
                 maxlen+2-(int)strlen(opts[i].name),"",_(opts[i].help));
 
1072
 
 
1073
      g10_exit(0);
 
1074
    }
 
1075
 
 
1076
  while((tok=optsep(&str)))
721
1077
    {
722
1078
      int i,rev=0;
 
1079
      char *otok=tok;
723
1080
 
724
1081
      if(tok[0]=='\0')
725
1082
        continue;
732
1089
 
733
1090
      for(i=0;opts[i].name;i++)
734
1091
        {
735
 
          if(ascii_strcasecmp(opts[i].name,tok)==0)
 
1092
          size_t toklen=optlen(tok);
 
1093
 
 
1094
          if(ascii_strncasecmp(opts[i].name,tok,toklen)==0)
736
1095
            {
 
1096
              /* We have a match, but it might be incomplete */
 
1097
              if(toklen!=strlen(opts[i].name))
 
1098
                {
 
1099
                  int j;
 
1100
 
 
1101
                  for(j=i+1;opts[j].name;j++)
 
1102
                    {
 
1103
                      if(ascii_strncasecmp(opts[j].name,tok,toklen)==0)
 
1104
                        {
 
1105
                          if(noisy)
 
1106
                            log_info(_("ambiguous option `%s'\n"),otok);
 
1107
                          return 0;
 
1108
                        }
 
1109
                    }
 
1110
                }
 
1111
 
737
1112
              if(rev)
738
 
                *options&=~opts[i].bit;
 
1113
                {
 
1114
                  *options&=~opts[i].bit;
 
1115
                  if(opts[i].value)
 
1116
                    *opts[i].value=NULL;
 
1117
                }
739
1118
              else
740
 
                *options|=opts[i].bit;
 
1119
                {
 
1120
                  *options|=opts[i].bit;
 
1121
                  if(opts[i].value)
 
1122
                    *opts[i].value=argsplit(tok);
 
1123
                }
741
1124
              break;
742
1125
            }
743
1126
        }
744
1127
 
745
1128
      if(!opts[i].name)
746
 
        return 0;
 
1129
        {
 
1130
          if(noisy)
 
1131
            log_info(_("unknown option `%s'\n"),otok);
 
1132
          return 0;
 
1133
        }
747
1134
    }
748
1135
 
749
1136
  return 1;
750
1137
}
751
1138
 
752
1139
 
753
 
 
 
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. */
 
1143
char *
 
1144
unescape_percent_string (const unsigned char *s)
 
1145
{
 
1146
  char *buffer, *d;
 
1147
 
 
1148
  buffer = d = xmalloc (strlen (s)+1);
 
1149
  while (*s)
 
1150
    {
 
1151
      if (*s == '%' && s[1] && s[2])
 
1152
        { 
 
1153
          s++;
 
1154
          *d = xtoi_2 (s);
 
1155
          if (!*d)
 
1156
            *d = '\xff';
 
1157
          d++;
 
1158
          s += 2;
 
1159
        }
 
1160
      else if (*s == '+')
 
1161
        {
 
1162
          *d++ = ' ';
 
1163
          s++;
 
1164
        }
 
1165
      else
 
1166
        *d++ = *s++;
 
1167
    }
 
1168
  *d = 0; 
 
1169
  return buffer;
 
1170
}
 
1171
 
 
1172
 
 
1173
int
 
1174
has_invalid_email_chars (const char *s)
 
1175
{
 
1176
  int at_seen=0;
 
1177
  const char *valid_chars=
 
1178
    "01234567890_-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
1179
 
 
1180
  for ( ; *s; s++ ) 
 
1181
    {
 
1182
      if ( *s & 0x80 )
 
1183
        return 1;
 
1184
      if ( *s == '@' )
 
1185
        at_seen=1;
 
1186
      else if ( !at_seen && !( !!strchr( valid_chars, *s ) || *s == '+' ) )
 
1187
        return 1;
 
1188
      else if ( at_seen && !strchr( valid_chars, *s ) )
 
1189
        return 1;
 
1190
    }
 
1191
  return 0;
 
1192
}
 
1193
 
 
1194
 
 
1195
/* Check whether NAME represents a valid mailbox according to
 
1196
   RFC822. Returns true if so. */
 
1197
int
 
1198
is_valid_mailbox (const char *name)
 
1199
{
 
1200
  return !( !name
 
1201
            || !*name
 
1202
            || has_invalid_email_chars (name)
 
1203
            || string_count_chr (name,'@') != 1
 
1204
            || *name == '@'
 
1205
            || name[strlen(name)-1] == '@'
 
1206
            || name[strlen(name)-1] == '.'
 
1207
            || strstr (name, "..") );
 
1208
}
 
1209
 
 
1210
 
 
1211
/* This is a helper function to load a Windows function from either of
 
1212
   one DLLs. */
 
1213
#ifdef HAVE_W32_SYSTEM
 
1214
static HRESULT
 
1215
w32_shgetfolderpath (HWND a, int b, HANDLE c, DWORD d, LPSTR e)
 
1216
{
 
1217
  static int initialized;
 
1218
  static HRESULT (WINAPI * func)(HWND,int,HANDLE,DWORD,LPSTR);
 
1219
 
 
1220
  if (!initialized)
 
1221
    {
 
1222
      static char *dllnames[] = { "shell32.dll", "shfolder.dll", NULL };
 
1223
      void *handle;
 
1224
      int i;
 
1225
 
 
1226
      initialized = 1;
 
1227
 
 
1228
      for (i=0, handle = NULL; !handle && dllnames[i]; i++)
 
1229
        {
 
1230
          handle = dlopen (dllnames[i], RTLD_LAZY);
 
1231
          if (handle)
 
1232
            {
 
1233
              func = dlsym (handle, "SHGetFolderPathA");
 
1234
              if (!func)
 
1235
                {
 
1236
                  dlclose (handle);
 
1237
                  handle = NULL;
 
1238
                }
 
1239
            }
 
1240
        }
 
1241
    }
 
1242
 
 
1243
  if (func)
 
1244
    return func (a,b,c,d,e);
 
1245
  else
 
1246
    return -1;
 
1247
}
 
1248
#endif /*HAVE_W32_SYSTEM*/
 
1249
 
 
1250
 
 
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. */
 
1253
const char *
 
1254
get_libexecdir (void)
 
1255
{
 
1256
#ifdef HAVE_W32_SYSTEM
 
1257
  static int got_dir;
 
1258
  static char dir[MAX_PATH+5];
 
1259
 
 
1260
  if (!got_dir)
 
1261
    {
 
1262
      char *p;
 
1263
 
 
1264
      if ( !GetModuleFileName ( NULL, dir, MAX_PATH) )
 
1265
        {
 
1266
          log_debug ("GetModuleFileName failed: %s\n", w32_strerror (0));
 
1267
          *dir = 0;
 
1268
        }
 
1269
      got_dir = 1;
 
1270
      p = strrchr (dir, DIRSEP_C);
 
1271
      if (p)
 
1272
        *p = 0;
 
1273
      else
 
1274
        {
 
1275
          log_debug ("bad filename `%s' returned for this process\n", dir);
 
1276
          *dir = 0; 
 
1277
        }
 
1278
    }
 
1279
 
 
1280
  if (*dir)
 
1281
    return dir;
 
1282
  /* Fallback to the hardwired value. */
 
1283
#endif /*HAVE_W32_SYSTEM*/
 
1284
 
 
1285
  return GNUPG_LIBEXECDIR;
 
1286
}
 
1287
 
 
1288
/* Similar to access(2), but uses PATH to find the file. */
 
1289
int
 
1290
path_access(const char *file,int mode)
 
1291
{
 
1292
  char *envpath;
 
1293
  int ret=-1;
 
1294
 
 
1295
  envpath=getenv("PATH");
 
1296
 
 
1297
  if(!envpath
 
1298
#ifdef HAVE_DRIVE_LETTERS
 
1299
     || (((file[0]>='A' && file[0]<='Z')
 
1300
          || (file[0]>='a' && file[0]<='z'))
 
1301
         && file[1]==':')
 
1302
#else
 
1303
     || file[0]=='/'
 
1304
#endif
 
1305
     )
 
1306
    return access(file,mode);
 
1307
  else
 
1308
    {
 
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);
 
1312
 
 
1313
      split=path;
 
1314
 
 
1315
      while((item=strsep(&split,PATHSEP_S)))
 
1316
        {
 
1317
          strcpy(buffer,item);
 
1318
          strcat(buffer,"/");
 
1319
          strcat(buffer,file);
 
1320
          ret=access(buffer,mode);
 
1321
          if(ret==0)
 
1322
            break;
 
1323
        }
 
1324
 
 
1325
      xfree(path);
 
1326
      xfree(buffer);
 
1327
    }
 
1328
 
 
1329
  return ret;
 
1330
}
 
1331
 
 
1332
 
 
1333
 
754
1334
/* Temporary helper. */
755
1335
int
756
1336
pubkey_get_npkey( int algo )
837
1417
    return nbits;
838
1418
}
839
1419
 
840
 
 
841
 
/* MPI helper functions. */
842
 
 
843
 
 
844
 
/****************
845
 
 * write an mpi to out.
846
 
 */
847
 
int
848
 
mpi_write( iobuf_t out, gcry_mpi_t a )
849
 
{
850
 
    char buffer[(MAX_EXTERN_MPI_BITS+7)/8];
851
 
    size_t nbytes;
852
 
    int rc;
853
 
 
854
 
    nbytes = (MAX_EXTERN_MPI_BITS+7)/8;
855
 
    rc = gcry_mpi_print (GCRYMPI_FMT_PGP, buffer, nbytes, &nbytes, a );
856
 
    if( !rc )
857
 
        rc = iobuf_write( out, buffer, nbytes );
858
 
 
859
 
    return rc;
860
 
}
861
 
 
862
 
/****************
863
 
 * Writyeg a MPI to out, but in this case it is an opaque one,
864
 
 * s used vor v3 protected keys.
865
 
 */
866
 
int
867
 
mpi_write_opaque( iobuf_t out, gcry_mpi_t a )
868
 
{
869
 
    size_t nbytes, nbits;
870
 
    int rc;
871
 
    char *p;
872
 
 
873
 
    assert( gcry_mpi_get_flag( a, GCRYMPI_FLAG_OPAQUE ) );
874
 
    p = gcry_mpi_get_opaque( a, &nbits );
875
 
    nbytes = (nbits+7) / 8;
876
 
    iobuf_put( out, nbits >> 8 );
877
 
    iobuf_put( out, nbits );
878
 
    rc = iobuf_write( out, p, nbytes );
879
 
    return rc;
880
 
}
881
 
 
882
 
 
883
 
/****************
884
 
 * Read an external representation of an mpi and return the MPI
885
 
 * The external format is a 16 bit unsigned value stored in network byte order,
886
 
 * giving the number of bits for the following integer. The integer is stored
887
 
 * with MSB first (left padded with zeroes to align on a byte boundary).
888
 
 */
889
 
gcry_mpi_t
890
 
mpi_read(iobuf_t inp, unsigned int *ret_nread, int secure)
891
 
{
892
 
    int c, c1, c2, i;
893
 
    unsigned int nbits, nbytes, nread=0;
894
 
    gcry_mpi_t a = NULL;
895
 
    byte *buf = NULL;
896
 
    byte *p;
897
 
 
898
 
    if( (c = c1 = iobuf_get(inp)) == -1 )
899
 
        goto leave;
900
 
    nbits = c << 8;
901
 
    if( (c = c2 = iobuf_get(inp)) == -1 )
902
 
        goto leave;
903
 
    nbits |= c;
904
 
    if( nbits > MAX_EXTERN_MPI_BITS ) {
905
 
        log_error("mpi too large (%u bits)\n", nbits);
906
 
        goto leave;
907
 
    }
908
 
    nread = 2;
909
 
    nbytes = (nbits+7) / 8;
910
 
    buf = secure? gcry_xmalloc_secure( nbytes+2 ) : gcry_xmalloc( nbytes+2 );
911
 
    p = buf;
912
 
    p[0] = c1;
913
 
    p[1] = c2;
914
 
    for( i=0 ; i < nbytes; i++ ) {
915
 
        p[i+2] = iobuf_get(inp) & 0xff;
916
 
        nread++;
917
 
    }
918
 
    nread += nbytes;
919
 
    if( gcry_mpi_scan( &a, GCRYMPI_FMT_PGP, buf, nread, &nread ) )
920
 
        a = NULL;
921
 
 
922
 
  leave:
923
 
    gcry_free(buf);
924
 
    if( nread > *ret_nread )
925
 
        log_bug("mpi larger than packet");
926
 
    else
927
 
        *ret_nread = nread;
928
 
    return a;
929
 
}
930
 
 
931
 
/****************
932
 
 * Same as mpi_read but the value is stored as an opaque MPI.
933
 
 * This function is used to read encrypted MPI of v3 packets.
934
 
 */
935
 
gcry_mpi_t
936
 
mpi_read_opaque(iobuf_t inp, unsigned *ret_nread )
937
 
{
938
 
    int c, c1, c2, i;
939
 
    unsigned nbits, nbytes, nread=0;
940
 
    gcry_mpi_t a = NULL;
941
 
    byte *buf = NULL;
942
 
    byte *p;
943
 
 
944
 
    if( (c = c1 = iobuf_get(inp)) == -1 )
945
 
        goto leave;
946
 
    nbits = c << 8;
947
 
    if( (c = c2 = iobuf_get(inp)) == -1 )
948
 
        goto leave;
949
 
    nbits |= c;
950
 
    if( nbits > MAX_EXTERN_MPI_BITS ) {
951
 
        log_error("mpi too large (%u bits)\n", nbits);
952
 
        goto leave;
953
 
    }
954
 
    nread = 2;
955
 
    nbytes = (nbits+7) / 8;
956
 
    buf = gcry_xmalloc( nbytes );
957
 
    p = buf;
958
 
    for( i=0 ; i < nbytes; i++ ) {
959
 
        p[i] = iobuf_get(inp) & 0xff;
960
 
    }
961
 
    nread += nbytes;
962
 
    a = gcry_mpi_set_opaque(NULL, buf, nbits );
963
 
    buf = NULL;
964
 
 
965
 
  leave:
966
 
    gcry_free(buf);
967
 
    if( nread > *ret_nread )
968
 
        log_bug("mpi larger than packet");
969
 
    else
970
 
        *ret_nread = nread;
971
 
    return a;
972
 
}
973
 
 
974
 
 
 
1420
 
 
1421
 
 
1422
/* FIXME: Use gcry_mpi_print directly. */
975
1423
int
976
1424
mpi_print( FILE *fp, gcry_mpi_t a, int mode )
977
1425
{
985
1433
        n += fprintf(fp, "[%u bits]", n1);
986
1434
    }
987
1435
    else {
988
 
        int rc;
989
 
        char *buffer;
 
1436
        unsigned char *buffer;
990
1437
 
991
 
        rc = gcry_mpi_aprint( GCRYMPI_FMT_HEX, &buffer, NULL, a );
992
 
        assert( !rc );
 
1438
        if (gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buffer, NULL, a))
 
1439
          BUG ();
993
1440
        fputs( buffer, fp );
994
1441
        n += strlen(buffer);
995
1442
        gcry_free( buffer );
997
1444
    return n;
998
1445
}
999
1446
 
1000