~ubuntu-branches/ubuntu/precise/gnupg2/precise-updates

« back to all changes in this revision

Viewing changes to scd/app-p15.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Urlichs
  • Date: 2005-12-08 22:13:21 UTC
  • mto: (5.1.1 edgy) (7.1.1 squeeze) (1.1.13 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20051208221321-d54343ca8hlwzkac
Tags: upstream-1.9.19
ImportĀ upstreamĀ versionĀ 1.9.19

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* app-p15.c - The pkcs#15 card application.
2
 
 *      Copyright (C) 2004 Free Software Foundation, Inc.
 
2
 *      Copyright (C) 2005 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
31
31
#include "iso7816.h"
32
32
#include "app-common.h"
33
33
#include "tlv.h"
 
34
#include "apdu.h" /* fixme: we should move the card detection to a
 
35
                     separate file */
 
36
 
 
37
/* Types of cards we know and which needs special treatment. */
 
38
typedef enum
 
39
  {
 
40
    CARD_TYPE_UNKNOWN,
 
41
    CARD_TYPE_TCOS,
 
42
    CARD_TYPE_MICARDO,
 
43
    CARD_TYPE_BELPIC   /* Belgian eID card specs. */
 
44
  } 
 
45
card_type_t;
 
46
 
 
47
/* A list card types with ATRs noticed with these cards. */
 
48
#define X(a) ((unsigned char const *)(a))
 
49
static struct 
 
50
{
 
51
  size_t atrlen;
 
52
  unsigned char const *atr;
 
53
  card_type_t type;
 
54
} card_atr_list[] = {
 
55
  { 19, X("\x3B\xBA\x13\x00\x81\x31\x86\x5D\x00\x64\x05\x0A\x02\x01\x31\x80"
 
56
          "\x90\x00\x8B"), 
 
57
    CARD_TYPE_TCOS },  /* SLE44 */
 
58
  { 19, X("\x3B\xBA\x14\x00\x81\x31\x86\x5D\x00\x64\x05\x14\x02\x02\x31\x80"
 
59
          "\x90\x00\x91"), 
 
60
    CARD_TYPE_TCOS }, /* SLE66S */
 
61
  { 19, X("\x3B\xBA\x96\x00\x81\x31\x86\x5D\x00\x64\x05\x60\x02\x03\x31\x80"
 
62
          "\x90\x00\x66"),
 
63
    CARD_TYPE_TCOS }, /* SLE66P */
 
64
  { 27, X("\x3B\xFF\x94\x00\xFF\x80\xB1\xFE\x45\x1F\x03\x00\x68\xD2\x76\x00"
 
65
          "\x00\x28\xFF\x05\x1E\x31\x80\x00\x90\x00\x23"),
 
66
    CARD_TYPE_MICARDO }, /* German BMI card */
 
67
  { 19, X("\x3B\x6F\x00\xFF\x00\x68\xD2\x76\x00\x00\x28\xFF\x05\x1E\x31\x80"
 
68
          "\x00\x90\x00"),
 
69
    CARD_TYPE_MICARDO }, /* German BMI card (ATR due to reader problem) */
 
70
  { 26, X("\x3B\xFE\x94\x00\xFF\x80\xB1\xFA\x45\x1F\x03\x45\x73\x74\x45\x49"
 
71
          "\x44\x20\x76\x65\x72\x20\x31\x2E\x30\x43"),
 
72
    CARD_TYPE_MICARDO }, /* EstEID (Estonian Big Brother card) */
 
73
 
 
74
  { 0 }
 
75
};
 
76
#undef X
 
77
 
 
78
 
 
79
/* The AID of PKCS15. */
 
80
static char const pkcs15_aid[] = { 0xA0, 0, 0, 0, 0x63,
 
81
                                   0x50, 0x4B, 0x43, 0x53, 0x2D, 0x31, 0x35 };
 
82
 
 
83
/* The Belgian eID variant - they didn't understood why a shared AID
 
84
   is useful for a standard.  Oh well. */
 
85
static char const pkcs15be_aid[] = { 0xA0, 0, 0, 0x01, 0x77,
 
86
                                   0x50, 0x4B, 0x43, 0x53, 0x2D, 0x31, 0x35 };
 
87
 
 
88
 
 
89
/* The PIN types as defined in pkcs#15 v1.1 */
 
90
typedef enum 
 
91
  {
 
92
    PIN_TYPE_BCD = 0,
 
93
    PIN_TYPE_ASCII_NUMERIC = 1,
 
94
    PIN_TYPE_UTF8 = 2,
 
95
    PIN_TYPE_HALF_NIBBLE_BCD = 3,
 
96
    PIN_TYPE_ISO9564_1 = 4
 
97
  } pin_type_t;
 
98
 
 
99
 
 
100
/* A bit array with for the key usage flags from the
 
101
   commonKeyAttributes. */
 
102
struct keyusage_flags_s
 
103
{
 
104
    unsigned int encrypt: 1;
 
105
    unsigned int decrypt: 1;
 
106
    unsigned int sign: 1;
 
107
    unsigned int sign_recover: 1;
 
108
    unsigned int wrap: 1;
 
109
    unsigned int unwrap: 1;
 
110
    unsigned int verify: 1;
 
111
    unsigned int verify_recover: 1;
 
112
    unsigned int derive: 1;
 
113
    unsigned int non_repudiation: 1;
 
114
};
 
115
typedef struct keyusage_flags_s keyusage_flags_t;
 
116
 
 
117
 
 
118
 
 
119
/* This is an object to store information about a Certificate
 
120
   Directory File (CDF) in a format suitable for further processing by
 
121
   us. To keep memory management, simple we use a linked list of
 
122
   items; i.e. one such object represents one certificate and the list
 
123
   the entire CDF. */
 
124
struct cdf_object_s 
 
125
{
 
126
  /* Link to next item when used in a linked list. */
 
127
  struct cdf_object_s *next; 
 
128
 
 
129
  /* Length and allocated buffer with the Id of this object. */
 
130
  size_t objidlen;
 
131
  unsigned char *objid;
 
132
 
 
133
  /* To avoid reading a certificate more than once, we cache it in an
 
134
     allocated memory IMAGE of IMAGELEN. */
 
135
  size_t imagelen;
 
136
  unsigned char *image;
 
137
  
 
138
  /* Set to true if a length and offset is available. */
 
139
  int have_off;
 
140
  /* The offset and length of the object.  They are only valid if
 
141
     HAVE_OFF is true and set to 0 if HAVE_OFF is false. */
 
142
  unsigned long off, len;
 
143
 
 
144
  /* The length of the path as given in the CDF and the path itself.
 
145
     path[0] is the top DF (usually 0x3f00). The path will never be
 
146
     empty. */
 
147
  size_t pathlen;
 
148
  unsigned short path[1];
 
149
};
 
150
typedef struct cdf_object_s *cdf_object_t;
 
151
 
 
152
 
 
153
/* This is an object to store information about a Private Key
 
154
   Directory File (PrKDF) in a format suitable for further processing
 
155
   by us. To keep memory management, simple we use a linked list of
 
156
   items; i.e. one such object represents one certificate and the list
 
157
   the entire PrKDF. */
 
158
struct prkdf_object_s 
 
159
{
 
160
  /* Link to next item when used in a linked list. */
 
161
  struct prkdf_object_s *next; 
 
162
 
 
163
  /* Length and allocated buffer with the Id of this object. */
 
164
  size_t objidlen;
 
165
  unsigned char *objid;
 
166
 
 
167
  /* Length and allocated buffer with the authId of this object or
 
168
     NULL if no authID is known. */
 
169
  size_t authidlen;
 
170
  unsigned char *authid;
 
171
 
 
172
  /* The key's usage flags. */
 
173
  keyusage_flags_t usageflags;
 
174
 
 
175
  /* The keyReference and a flag telling whether it is valid. */ 
 
176
  unsigned long key_reference;
 
177
  int key_reference_valid;
 
178
 
 
179
  /* Set to true if a length and offset is available. */
 
180
  int have_off;
 
181
  /* The offset and length of the object.  They are only valid if
 
182
     HAVE_OFF is true and set to 0 if HAVE_OFF is false. */
 
183
  unsigned long off, len;
 
184
 
 
185
  /* The length of the path as given in the PrKDF and the path itself.
 
186
     path[0] is the top DF (usually 0x3f00). */
 
187
  size_t pathlen;
 
188
  unsigned short path[1];
 
189
};
 
190
typedef struct prkdf_object_s *prkdf_object_t;
 
191
 
 
192
 
 
193
/* This is an object to store information about a Authentication
 
194
   Object Directory File (AODF) in a format suitable for further
 
195
   processing by us. To keep memory management, simple we use a linked
 
196
   list of items; i.e. one such object represents one authentication
 
197
   object and the list the entire AOKDF. */
 
198
struct aodf_object_s 
 
199
{
 
200
  /* Link to next item when used in a linked list. */
 
201
  struct aodf_object_s *next; 
 
202
 
 
203
  /* Length and allocated buffer with the Id of this object. */
 
204
  size_t objidlen;
 
205
  unsigned char *objid;
 
206
 
 
207
  /* Length and allocated buffer with the authId of this object or
 
208
     NULL if no authID is known. */
 
209
  size_t authidlen;
 
210
  unsigned char *authid;
 
211
 
 
212
  /* The PIN Flags. */
 
213
  struct 
 
214
  {
 
215
    unsigned int case_sensitive: 1;
 
216
    unsigned int local: 1;
 
217
    unsigned int change_disabled: 1;
 
218
    unsigned int unblock_disabled: 1;
 
219
    unsigned int initialized: 1;
 
220
    unsigned int needs_padding: 1;
 
221
    unsigned int unblocking_pin: 1;
 
222
    unsigned int so_pin: 1;
 
223
    unsigned int disable_allowed: 1;
 
224
    unsigned int integrity_protected: 1;
 
225
    unsigned int confidentiality_protected: 1;
 
226
    unsigned int exchange_ref_data: 1;
 
227
  } pinflags;
 
228
 
 
229
  /* The PIN Type. */
 
230
  pin_type_t pintype;
 
231
 
 
232
  /* The minimum length of a PIN. */
 
233
  unsigned long min_length;
 
234
 
 
235
  /* The stored length of a PIN. */
 
236
  unsigned long stored_length;
 
237
 
 
238
  /* The maximum length of a PIN and a flag telling whether it is valid. */
 
239
  unsigned long max_length;
 
240
  int max_length_valid;
 
241
 
 
242
  /* The pinReference and a flag telling whether it is valid. */ 
 
243
  unsigned long pin_reference;
 
244
  int pin_reference_valid;
 
245
 
 
246
  /* The padChar and a flag telling whether it is valid. */
 
247
  char pad_char;
 
248
  int pad_char_valid;
 
249
 
 
250
 
 
251
  /* Set to true if a length and offset is available. */
 
252
  int have_off;
 
253
  /* The offset and length of the object.  They are only valid if
 
254
     HAVE_OFF is true and set to 0 if HAVE_OFF is false. */
 
255
  unsigned long off, len;
 
256
 
 
257
  /* The length of the path as given in the Aodf and the path itself.
 
258
     path[0] is the top DF (usually 0x3f00). PATH is optional and thus
 
259
     may be NULL.  Malloced.*/
 
260
  size_t pathlen;
 
261
  unsigned short *path;
 
262
};
 
263
typedef struct aodf_object_s *aodf_object_t;
34
264
 
35
265
 
36
266
/* Context local to this application. */
37
267
struct app_local_s 
38
268
{
39
 
  unsigned short home_df;  /* The home DF. Note, that we don't yet
40
 
                              support a multilevel hierachy.  Thus we
41
 
                              assume this is directly below the MF.  */
 
269
  /* The home DF. Note, that we don't yet support a multilevel
 
270
     hierachy.  Thus we assume this is directly below the MF.  */
 
271
  unsigned short home_df;
 
272
 
 
273
  /* The type of the card. */
 
274
  card_type_t card_type;
 
275
 
 
276
  /* Flag indicating whether we may use direct path selection. */
 
277
  int direct_path_selection;
 
278
 
 
279
  /* Structure with the EFIDs of the objects described in the ODF
 
280
     file. */
42
281
  struct
43
282
  {
44
283
    unsigned short private_keys;
52
291
    unsigned short auth_objects;
53
292
  } odf;  
54
293
 
 
294
  /* The PKCS#15 serialnumber from EF(TokeiNFo) or NULL.  Malloced. */
 
295
  unsigned char *serialno;
 
296
  size_t serialnolen;
 
297
 
 
298
  /* Information on all certificates. */
 
299
  cdf_object_t certificate_info;
 
300
  /* Information on all trusted certificates. */
 
301
  cdf_object_t trusted_certificate_info;
 
302
  /* Information on all useful certificates. */
 
303
  cdf_object_t useful_certificate_info;
 
304
 
 
305
  /* Information on all private keys. */
 
306
  prkdf_object_t private_key_info;
 
307
 
 
308
  /* Information on all authentication objects. */
 
309
  aodf_object_t auth_object_info;
55
310
 
56
311
};
57
312
 
58
313
 
59
 
 
60
 
 
61
 
/* Do a select and a read for the file with EFID.  EFID is a
 
314
/*** Local prototypes.  ***/
 
315
static gpg_error_t readcert_by_cdf (app_t app, cdf_object_t cdf,
 
316
                                    unsigned char **r_cert, size_t *r_certlen);
 
317
 
 
318
 
 
319
 
 
320
/* Release the CDF object A  */
 
321
static void
 
322
release_cdflist (cdf_object_t a)
 
323
{
 
324
  while (a)
 
325
    {
 
326
      cdf_object_t tmp = a->next;
 
327
      xfree (a->image);
 
328
      xfree (a->objid);
 
329
      xfree (a);
 
330
      a = tmp;
 
331
    }
 
332
}
 
333
 
 
334
/* Release the PrKDF object A.  */
 
335
static void
 
336
release_prkdflist (prkdf_object_t a)
 
337
{
 
338
  while (a)
 
339
    {
 
340
      prkdf_object_t tmp = a->next;
 
341
      xfree (a->objid);
 
342
      xfree (a->authid);
 
343
      xfree (a);
 
344
      a = tmp;
 
345
    }
 
346
}
 
347
 
 
348
/* Release just one aodf object. */
 
349
void
 
350
release_aodf_object (aodf_object_t a)
 
351
{
 
352
  if (a)
 
353
    {
 
354
      xfree (a->objid);
 
355
      xfree (a->authid);
 
356
      xfree (a->path);
 
357
      xfree (a);
 
358
    }
 
359
}
 
360
 
 
361
/* Release the AODF list A.  */
 
362
static void
 
363
release_aodflist (aodf_object_t a)
 
364
{
 
365
  while (a)
 
366
    {
 
367
      aodf_object_t tmp = a->next;
 
368
      release_aodf_object (a);
 
369
      a = tmp;
 
370
    }
 
371
}
 
372
 
 
373
 
 
374
/* Release all local resources.  */
 
375
static void
 
376
do_deinit (app_t app)
 
377
{
 
378
  if (app && app->app_local)
 
379
    {
 
380
      release_cdflist (app->app_local->certificate_info);
 
381
      release_cdflist (app->app_local->trusted_certificate_info);
 
382
      release_cdflist (app->app_local->useful_certificate_info);
 
383
      release_prkdflist (app->app_local->private_key_info);
 
384
      release_aodflist (app->app_local->auth_object_info);
 
385
      xfree (app->app_local->serialno);
 
386
      xfree (app->app_local);
 
387
      app->app_local = NULL;
 
388
    }
 
389
}
 
390
 
 
391
 
 
392
 
 
393
/* Do a select and a read for the file with EFID.  EFID_DESC is a
62
394
   desctription of the EF to be used with error messages.  On success
63
395
   BUFFER and BUFLEN contain the entire content of the EF.  The caller
64
 
   must free BUFFER but only on success. */
 
396
   must free BUFFER only on success. */
65
397
static gpg_error_t 
66
398
select_and_read_binary (int slot, unsigned short efid, const char *efid_desc,
67
399
                        unsigned char **buffer, size_t *buflen)
86
418
}
87
419
 
88
420
 
89
 
 
90
 
 
 
421
/* This function calls select file to read a file using a complete
 
422
   path which may or may not start at the master file (MF). */ 
 
423
static gpg_error_t
 
424
select_ef_by_path (app_t app, const unsigned short *path, size_t pathlen)
 
425
{
 
426
  gpg_error_t err;
 
427
  int i, j;
 
428
 
 
429
  if (!pathlen)
 
430
    return gpg_error (GPG_ERR_INV_VALUE);
 
431
 
 
432
  if (pathlen && *path != 0x3f00 )
 
433
    log_debug ("WARNING: relative path selection not yet implemented\n");
 
434
      
 
435
  if (app->app_local->direct_path_selection)
 
436
    {
 
437
      err = iso7816_select_path (app->slot, path+1, pathlen-1, NULL, NULL);
 
438
      if (err)
 
439
        {
 
440
          log_error ("error selecting path ");
 
441
          for (j=0; j < pathlen; j++)
 
442
            log_printf ("%04hX", path[j]);
 
443
          log_printf (": %s\n", gpg_strerror (err));
 
444
          return err;
 
445
        }
 
446
    }
 
447
  else
 
448
    {
 
449
      /* FIXME: Need code to remember the last PATH so that we can decide
 
450
         what select commands to send in case the path does not start off
 
451
         with 3F00.  We might also want to use direct path selection if
 
452
         supported by the card. */
 
453
      for (i=0; i < pathlen; i++)
 
454
        {
 
455
          err = iso7816_select_file (app->slot, path[i],
 
456
                                     !(i+1 == pathlen), NULL, NULL);
 
457
          if (err)
 
458
            {
 
459
              log_error ("error selecting part %d from path ", i);
 
460
              for (j=0; j < pathlen; j++)
 
461
                log_printf ("%04hX", path[j]);
 
462
              log_printf (": %s\n", gpg_strerror (err));
 
463
              return err;
 
464
            }
 
465
        }
 
466
    }
 
467
  return 0;
 
468
}
 
469
 
 
470
/* Parse a cert Id string (or a key Id string) and return the binary
 
471
   object Id string in a newly allocated buffer stored at R_OBJID and
 
472
   R_OBJIDLEN.  On Error NULL will be stored there and an error code
 
473
   returned. On success caller needs to free the buffer at R_OBJID. */
 
474
static gpg_error_t
 
475
parse_certid (app_t app, const char *certid,
 
476
              unsigned char **r_objid, size_t *r_objidlen)
 
477
{
 
478
  char tmpbuf[10];
 
479
  const char *s;
 
480
  size_t objidlen;
 
481
  unsigned char *objid;
 
482
  int i;
 
483
 
 
484
  *r_objid = NULL;
 
485
  *r_objidlen = 0;
 
486
 
 
487
  if (app->app_local->home_df)
 
488
    sprintf (tmpbuf, "P15-%04hX.", (app->app_local->home_df & 0xffff));
 
489
  else
 
490
    strcpy (tmpbuf, "P15.");
 
491
  if (strncmp (certid, tmpbuf, strlen (tmpbuf)) )
 
492
    {
 
493
      if (!strncmp (certid, "P15.", 4)
 
494
          || (!strncmp (certid, "P15-", 4)
 
495
              && hexdigitp (certid+4)
 
496
              && hexdigitp (certid+5)
 
497
              && hexdigitp (certid+6)
 
498
              && hexdigitp (certid+7)
 
499
              && certid[8] == '.'))
 
500
        return gpg_error (GPG_ERR_NOT_FOUND);
 
501
      return gpg_error (GPG_ERR_INV_ID);
 
502
    }
 
503
  certid += strlen (tmpbuf);
 
504
 
 
505
  for (s=certid, objidlen=0; hexdigitp (s); s++, objidlen++)
 
506
    ;
 
507
  if (*s || !objidlen || (objidlen%2))
 
508
    return gpg_error (GPG_ERR_INV_ID);
 
509
  objidlen /= 2;
 
510
  objid = xtrymalloc (objidlen);
 
511
  if (!objid)
 
512
    return gpg_error_from_errno (errno);
 
513
  for (s=certid, i=0; i < objidlen; i++, s+=2)
 
514
    objid[i] = xtoi_2 (s);
 
515
  *r_objid = objid;
 
516
  *r_objidlen = objidlen;
 
517
  return 0;
 
518
}
 
519
 
 
520
 
 
521
/* Find a certificate object by the certificate ID CERTID and store a
 
522
   pointer to it at R_CDF. */
 
523
static gpg_error_t
 
524
cdf_object_from_certid (app_t app, const char *certid, cdf_object_t *r_cdf)
 
525
{
 
526
  gpg_error_t err;
 
527
  size_t objidlen;
 
528
  unsigned char *objid;
 
529
  cdf_object_t cdf;
 
530
 
 
531
  err = parse_certid (app, certid, &objid, &objidlen);
 
532
  if (err)
 
533
    return err;
 
534
 
 
535
  for (cdf = app->app_local->certificate_info; cdf; cdf = cdf->next)
 
536
    if (cdf->objidlen == objidlen && !memcmp (cdf->objid, objid, objidlen))
 
537
      break;
 
538
  if (!cdf)
 
539
    for (cdf = app->app_local->trusted_certificate_info; cdf; cdf = cdf->next)
 
540
      if (cdf->objidlen == objidlen && !memcmp (cdf->objid, objid, objidlen))
 
541
        break;
 
542
  if (!cdf)
 
543
    for (cdf = app->app_local->useful_certificate_info; cdf; cdf = cdf->next)
 
544
      if (cdf->objidlen == objidlen && !memcmp (cdf->objid, objid, objidlen))
 
545
        break;
 
546
  xfree (objid);
 
547
  if (!cdf)
 
548
    return gpg_error (GPG_ERR_NOT_FOUND);
 
549
  *r_cdf = cdf;
 
550
  return 0;
 
551
}
 
552
 
 
553
 
 
554
/* Find a private key object by the key Id string KEYIDSTR and store a
 
555
   pointer to it at R_PRKDF. */
 
556
static gpg_error_t
 
557
prkdf_object_from_keyidstr (app_t app, const char *keyidstr,
 
558
                            prkdf_object_t *r_prkdf)
 
559
{
 
560
  gpg_error_t err;
 
561
  size_t objidlen;
 
562
  unsigned char *objid;
 
563
  prkdf_object_t prkdf;
 
564
 
 
565
  err = parse_certid (app, keyidstr, &objid, &objidlen);
 
566
  if (err)
 
567
    return err;
 
568
 
 
569
  for (prkdf = app->app_local->private_key_info; prkdf; prkdf = prkdf->next)
 
570
    if (prkdf->objidlen == objidlen && !memcmp (prkdf->objid, objid, objidlen))
 
571
      break;
 
572
  xfree (objid);
 
573
  if (!prkdf)
 
574
    return gpg_error (GPG_ERR_NOT_FOUND);
 
575
  *r_prkdf = prkdf;
 
576
  return 0;
 
577
}
 
578
 
 
579
 
 
580
 
 
581
 
91
582
/* Read and parse the Object Directory File and store away the
92
 
   pointers.
 
583
   pointers. ODF_FID shall contain the FID of the ODF.
93
584
 
94
585
   Example of such a file:
95
586
 
99
590
   A7 06 30 04 04 02 60 37  = DataObjects
100
591
   A8 06 30 04 04 02 60 38  = AuthObjects
101
592
    
102
 
   These are all PathOrObjects using the path CHOICE.  The paths are
103
 
   octet strings of length 2.  Using this Path CHOICE is recommended,
104
 
   so we only implement that for now.
 
593
   These are all PathOrObjects using the path CHOICE element.  The
 
594
   paths are octet strings of length 2.  Using this Path CHOICE
 
595
   element is recommended, so we only implement that for now.
105
596
*/
106
597
static gpg_error_t
107
 
read_ef_odf (app_t app)
 
598
read_ef_odf (app_t app, unsigned short odf_fid)
108
599
{
109
600
  gpg_error_t err;
110
601
  unsigned char *buffer, *p;
111
602
  size_t buflen;
112
603
  unsigned short value;
 
604
  size_t offset;
113
605
 
114
 
  err = select_and_read_binary (app->slot, 0x5031, "ODF", &buffer, &buflen);
 
606
  err = select_and_read_binary (app->slot, odf_fid, "ODF", &buffer, &buflen);
115
607
  if (err)
116
608
    return err;
117
609
 
118
 
  if (len < 8)
 
610
  if (buflen < 8)
119
611
    {
120
612
      log_error ("error: ODF too short\n");
121
613
      xfree (buffer);
122
614
      return gpg_error (GPG_ERR_INV_OBJ);
123
615
    }
124
 
  for (p=buffer; buflen >= 8; p += 8, buflen -= 8)
 
616
  p = buffer;
 
617
  while (buflen && *p && *p != 0xff)
125
618
    {
126
 
      if ( (p[0] & 0xf0) != 0xA0
127
 
           || memcmp (p+1, "\x06\x30\x04\x04\x02", 5) )
 
619
      if ( buflen >= 8
 
620
           && (p[0] & 0xf0) == 0xA0
 
621
           && !memcmp (p+1, "\x06\x30\x04\x04\x02", 5) )
 
622
        {
 
623
          offset = 6;
 
624
        }
 
625
      else if ( buflen >= 12 
 
626
                && (p[0] & 0xf0) == 0xA0
 
627
                && !memcmp (p+1, "\x0a\x30\x08\x04\x06\x3F\x00", 7)
 
628
                && app->app_local->home_df == ((p[8]<<8)|p[9]) )
 
629
        {
 
630
          /* We only allow a full path if all files are at the same
 
631
             level and below the home directory.  The extend this we
 
632
             would need to make use of new data type capable of
 
633
             keeping a full path. */
 
634
          offset = 10;
 
635
        }
 
636
      else
128
637
        {
129
638
          log_error ("ODF format is not supported by us\n");
130
639
          xfree (buffer);
145
654
        }
146
655
      if (value)
147
656
        {
148
 
          log_error ("duplicate object type %d in ODF ignored\n",(p[0)&0x0f));
 
657
          log_error ("duplicate object type %d in ODF ignored\n",(p[0]&0x0f));
149
658
          continue;
150
659
        }
151
 
      value = ((p[6] << 8) | p[7]);
 
660
      value = ((p[offset] << 8) | p[offset+1]);
152
661
      switch ((p[0] & 0x0f))
153
662
        {
154
663
        case 0: app->app_local->odf.private_keys = value; break;
161
670
        case 7: app->app_local->odf.data_objects = value; break;
162
671
        case 8: app->app_local->odf.auth_objects = value; break;
163
672
        default: 
164
 
          log_error ("unknown object type %d in ODF ignored\n", (p[0)&0x0f));
 
673
          log_error ("unknown object type %d in ODF ignored\n", (p[0]&0x0f));
165
674
        }
 
675
      offset += 2;
 
676
 
 
677
      if (buflen < offset)
 
678
        break;
 
679
      p += offset;
 
680
      buflen -= offset;
166
681
    }
167
682
 
168
683
  if (buflen)
173
688
}
174
689
 
175
690
 
 
691
/* Parse the BIT STRING with the keyUsageFlags from teh
 
692
   CommonKeyAttributes. */
 
693
static gpg_error_t
 
694
parse_keyusage_flags (const unsigned char *der, size_t derlen,
 
695
                      keyusage_flags_t *usageflags)
 
696
{
 
697
  unsigned int bits, mask;
 
698
  int i, unused, full;
 
699
 
 
700
  memset (usageflags, 0, sizeof *usageflags);
 
701
  if (!derlen)
 
702
    return gpg_error (GPG_ERR_INV_OBJ);
 
703
          
 
704
  unused = *der++; derlen--;
 
705
  if ((!derlen && unused) || unused/8 > derlen)
 
706
    return gpg_error (GPG_ERR_ENCODING_PROBLEM);
 
707
  full = derlen - (unused+7)/8;
 
708
  unused %= 8;
 
709
  mask = 0;
 
710
  for (i=1; unused; i <<= 1, unused--)
 
711
    mask |= i;
 
712
  
 
713
  /* First octet */
 
714
  if (derlen)
 
715
    {
 
716
      bits = *der++; derlen--;
 
717
      if (full)
 
718
        full--;
 
719
      else 
 
720
        {
 
721
          bits &= ~mask;
 
722
          mask = 0; 
 
723
        }
 
724
    }
 
725
  else
 
726
    bits = 0;
 
727
  if ((bits & 0x80)) usageflags->encrypt = 1;
 
728
  if ((bits & 0x40)) usageflags->decrypt = 1;
 
729
  if ((bits & 0x20)) usageflags->sign = 1;  
 
730
  if ((bits & 0x10)) usageflags->sign_recover = 1;
 
731
  if ((bits & 0x08)) usageflags->wrap = 1;
 
732
  if ((bits & 0x04)) usageflags->unwrap = 1; 
 
733
  if ((bits & 0x02)) usageflags->verify = 1;  
 
734
  if ((bits & 0x01)) usageflags->verify_recover = 1;
 
735
 
 
736
  /* Second octet. */
 
737
  if (derlen)
 
738
    {
 
739
      bits = *der++; derlen--;
 
740
      if (full)
 
741
        full--;
 
742
      else 
 
743
        {
 
744
          bits &= ~mask;
 
745
          mask = 0; 
 
746
        }
 
747
    }
 
748
  else
 
749
    bits = 0;
 
750
  if ((bits & 0x80)) usageflags->derive = 1;
 
751
  if ((bits & 0x40)) usageflags->non_repudiation = 1;
 
752
 
 
753
  return 0;
 
754
}
176
755
 
177
756
/* Read and  parse the Private Key Directory Files. */
178
757
/*
195
774
00 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00   ................
196
775
00 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00   ................
197
776
 
198
 
*/
199
 
static gpg_error_t
200
 
read_ef_prkdf (app_t app)
201
 
{
202
 
 
203
 
 
204
 
}
205
 
 
206
 
/* Read and  parse the Public Key Directory Files. */
207
 
static gpg_error_t
208
 
read_ef_pukdf (app_t app)
209
 
{
210
 
 
211
 
 
212
 
}
213
 
 
214
 
 
215
 
/* Read and parse the Certificate Directory Files. */
216
 
/* 
217
 
 
218
 
6035 (certificates)
219
 
 
220
 
30 2A 30 15 0C 0C 43 5F 58  35 30 39 2E 43 48 2E   0*0...C_X509.CH.
221
 
44 53 03 02 06 40 04 01 0A  30 03 04 01 01 A1 0C   DS...@...0......
222
 
30 0A 30 08 04 06 3F 00 40  16 C0 00 30 2A 30 15   0.0...?.@...0*0.
223
 
0C 0C 43 5F 58 35 30 39 2E  43 48 2E 4B 45 03 02   ..C_X509.CH.KE..
224
 
06 40 04 01 0A 30 03 04 01  0C A1 0C 30 0A 30 08   .@...0......0.0.
225
 
04 06 3F 00 40 16 C2 00 30  2B 30 16 0C 0D 43 5F   ..?.@...0+0...C_
226
 
58 35 30 39 2E 43 48 2E 41  55 54 03 02 06 40 04   X509.CH.AUT...@.
227
 
01 0A 30 03 04 01 0D A1 0C  30 0A 30 08 04 06 3F   ..0......0.0...?
228
 
00 40 16 C5 00 30 2E 30 19  0C 10 43 5F 58 35 30   .@...0.0...C_X50
229
 
39 2E 43 48 2E 44 53 2D 53  50 58 03 02 06 40 04   9.CH.DS-SPX...@.
230
 
01 0A 30 03 04 01 02 A1 0C  30 0A 30 08 04 06 3F   ..0......0.0...?
231
 
00 40 16 C1 20 00 00 00 00  00 00 00 00 00 00 00   .@.. ...........
232
 
00 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00   ................
233
 
00 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00   ................
234
 
00 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00   ................
235
 
00 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00   ................
236
 
 
237
 
   0   42: SEQUENCE {
238
 
   2   21:   SEQUENCE {   -- commonObjectAttributes
239
 
   4   12:     UTF8String 'C_X509.CH.DS'
240
 
  18    2:     BIT STRING 6 unused bits
241
 
         :       '10'B (bit 1)
242
 
  22    1:     OCTET STRING 0A
243
 
         :     }
244
 
  25    3:   SEQUENCE {   -- commonCertificateAttributes
245
 
  27    1:     OCTET STRING 01
246
 
         :     }
247
 
  30   12:   [1] {        -- certAttributes
248
 
  32   10:     SEQUENCE {
249
 
  34    8:       SEQUENCE {
250
 
  36    6:         OCTET STRING 3F 00 40 16 C0 00
251
 
         :         }
252
 
         :       }
253
 
         :     }
254
 
         :   }
255
 
 
256
 
 
257
 
 
258
 
6036 (trustedcertificates)
259
 
 
260
 
30 35 30 06 03 02 00 00 04  00 30 16 04 14 2D 36   050.......0...-6
261
 
33 39 33 33 39 34 30 33 39  37 37 36 34 30 31 32   3933940397764012
262
 
31 36 A1 13 30 11 30 0F 04  06 3F 00 40 16 C7 08   16..0.0...?.@...
263
 
02 01 00 80 02 02 29 30 35  30 06 03 02 00 00 04   ......)050......
264
 
00 30 16 04 14 2D 34 30 31  39 30 35 32 37 32 36   .0...-4019052726
265
 
38 30 31 36 39 33 34 39 32  A1 13 30 11 30 0F 04   801693492..0.0..
266
 
06 3F 00 40 16 C7 0E 02 01  00 80 02 04 12 30 34   .?.@..........04
267
 
30 06 03 02 00 00 04 00 30  15 04 13 37 39 36 33   0.......0...7963
268
 
32 38 33 36 35 30 37 36 36  34 38 32 39 36 30 A1   283650766482960.
269
 
13 30 11 30 0F 04 06 3F 00  40 16 C0 08 02 01 00   .0.0...?.@......
270
 
80 02 04 11 00 00 00 00 00  00 00 00 00 00 00 00   ................
271
 
00 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00   ................
272
 
 
273
 
   0   53: SEQUENCE {
274
 
   2    6:   SEQUENCE {
275
 
   4    2:     BIT STRING
276
 
         :       '00000000'B
277
 
         :       Error: Spurious zero bits in bitstring.
278
 
   8    0:     OCTET STRING
279
 
         :       Error: Object has zero length.
280
 
         :     }
281
 
  10   22:   SEQUENCE {
282
 
  12   20:     OCTET STRING '-6393394039776401216'
283
 
         :     }
284
 
  34   19:   [1] {
285
 
  36   17:     SEQUENCE {
286
 
  38   15:       SEQUENCE {
287
 
  40    6:         OCTET STRING 3F 00 40 16 C7 08
288
 
  48    1:         INTEGER 0       -- index
289
 
  51    2:         [0] 02 29       -- length
290
 
         :         }
291
 
         :       }
292
 
         :     }
293
 
         :   }
294
 
 
295
 
 
296
 
*/
297
 
static gpg_error_t
298
 
read_ef_cdf (app_t app)
299
 
{
300
 
  gpg_error_t err;
301
 
  unsigned char *buffer = NULL;
302
 
  size_t buflen;
303
 
  unsigned short value;
304
 
  unsigned short fid;
305
 
  const unsigned char *p;
306
 
  size_t n, objlen, hdrlen;
307
 
  int class, tag, constructed, ndef;
308
 
  
309
 
  fid = app->app_local->odf.certificates;
310
 
  if (!fid)
311
 
    return 0; /* No certificates. */
 
777
   0 30   51: SEQUENCE {
 
778
   2 30   17:   SEQUENCE { -- commonObjectAttributes
 
779
   4 0C    8:     UTF8String 'SK.CH.DS'
 
780
  14 03    2:     BIT STRING 6 unused bits
 
781
            :       '01'B (bit 0)
 
782
  18 04    1:     OCTET STRING --authid
 
783
            :       07
 
784
            :     }
 
785
  21 30   12:   SEQUENCE { -- commonKeyAttributes
 
786
  23 04    1:     OCTET STRING
 
787
            :       01
 
788
  26 03    3:     BIT STRING 6 unused bits
 
789
            :       '1000000000'B (bit 9)
 
790
  31 02    2:     INTEGER 80  -- keyReference (optional)
 
791
            :     }
 
792
  35 A1   16:   [1] {  -- keyAttributes
 
793
  37 30   14:     SEQUENCE { -- privateRSAKeyAttributes
 
794
  39 30    8:       SEQUENCE { -- objectValue
 
795
  41 04    6:         OCTET STRING --path
 
796
            :           3F 00 40 16 00 50
 
797
            :         }
 
798
  49 02    2:       INTEGER 1024 -- modulus
 
799
            :       }
 
800
            :     }
 
801
            :   }
 
802
 
 
803
 
 
804
*/
 
805
static gpg_error_t
 
806
read_ef_prkdf (app_t app, unsigned short fid, prkdf_object_t *result)
 
807
{
 
808
  gpg_error_t err;
 
809
  unsigned char *buffer = NULL;
 
810
  size_t buflen;
 
811
  const unsigned char *p;
 
812
  size_t n, objlen, hdrlen;
 
813
  int class, tag, constructed, ndef;
 
814
  prkdf_object_t prkdflist = NULL;
 
815
  int i;
 
816
  
 
817
  if (!fid)
 
818
    return gpg_error (GPG_ERR_NO_DATA); /* No private keys. */
 
819
  
 
820
  err = select_and_read_binary (app->slot, fid, "PrKDF", &buffer, &buflen);
 
821
  if (err)
 
822
    return err;
 
823
  
 
824
  p = buffer;
 
825
  n = buflen;
 
826
 
 
827
  /* FIXME: This shares a LOT of code with read_ef_cdf! */
 
828
 
 
829
  /* Loop over the records.  We stop as soon as we detect a new record
 
830
     starting with 0x00 or 0xff as these values are commonly used to
 
831
     pad data blocks and are no valid ASN.1 encoding. */
 
832
  while (n && *p && *p != 0xff)
 
833
    {
 
834
      const unsigned char *pp;
 
835
      size_t nn;
 
836
      int where;
 
837
      const char *errstr = NULL;
 
838
      prkdf_object_t prkdf = NULL;
 
839
      unsigned long ul;
 
840
      const unsigned char *objid;
 
841
      size_t objidlen;
 
842
      const unsigned char *authid = NULL;
 
843
      size_t authidlen = 0;
 
844
      keyusage_flags_t usageflags;
 
845
      unsigned long key_reference = 0;
 
846
      int key_reference_valid = 0;
 
847
      const char *s;
 
848
 
 
849
      err = parse_ber_header (&p, &n, &class, &tag, &constructed,
 
850
                              &ndef, &objlen, &hdrlen);
 
851
      if (!err && (objlen > n || tag != TAG_SEQUENCE))
 
852
        err = gpg_error (GPG_ERR_INV_OBJ);
 
853
      if (err)
 
854
        {
 
855
          log_error ("error parsing PrKDF record: %s\n", gpg_strerror (err));
 
856
          goto leave;
 
857
        }
 
858
      pp = p;
 
859
      nn = objlen;
 
860
      p += objlen;
 
861
      n -= objlen;
 
862
 
 
863
      /* Parse the commonObjectAttributes.  */
 
864
      where = __LINE__;
 
865
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
866
                              &ndef, &objlen, &hdrlen);
 
867
      if (!err && (objlen > nn || tag != TAG_SEQUENCE))
 
868
        err = gpg_error (GPG_ERR_INV_OBJ);
 
869
      if (err)
 
870
        goto parse_error;
 
871
      {
 
872
        const unsigned char *ppp = pp;
 
873
        size_t nnn = objlen;
 
874
 
 
875
        pp += objlen;
 
876
        nn -= objlen;
 
877
 
 
878
        /* Search the optional AuthId.  We need to skip the optional
 
879
           Label (UTF8STRING) and the optional CommonObjectFlags
 
880
           (BITSTRING). */
 
881
        where = __LINE__;
 
882
        err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
883
                                &ndef, &objlen, &hdrlen);
 
884
        if (!err && (objlen > nnn || class != CLASS_UNIVERSAL))
 
885
          err = gpg_error (GPG_ERR_INV_OBJ);
 
886
        if (gpg_err_code (err) == GPG_ERR_EOF)
 
887
          goto no_authid;
 
888
        if (err)
 
889
          goto parse_error;
 
890
        if (tag == TAG_UTF8_STRING)
 
891
          {
 
892
            ppp += objlen; /* Skip the Label. */
 
893
            nnn -= objlen;
 
894
 
 
895
            where = __LINE__;
 
896
            err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
897
                                    &ndef, &objlen, &hdrlen);
 
898
            if (!err && (objlen > nnn || class != CLASS_UNIVERSAL))
 
899
              err = gpg_error (GPG_ERR_INV_OBJ);
 
900
            if (gpg_err_code (err) == GPG_ERR_EOF)
 
901
              goto no_authid;
 
902
            if (err)
 
903
              goto parse_error;
 
904
          }
 
905
        if (tag == TAG_BIT_STRING)
 
906
          {
 
907
            ppp += objlen; /* Skip the CommonObjectFlags.  */
 
908
            nnn -= objlen;
 
909
 
 
910
            where = __LINE__;
 
911
            err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
912
                                    &ndef, &objlen, &hdrlen);
 
913
            if (!err && (objlen > nnn || class != CLASS_UNIVERSAL))
 
914
              err = gpg_error (GPG_ERR_INV_OBJ);
 
915
            if (gpg_err_code (err) == GPG_ERR_EOF)
 
916
              goto no_authid;
 
917
            if (err)
 
918
              goto parse_error;
 
919
          }
 
920
        if (tag == TAG_OCTET_STRING && objlen)
 
921
          {
 
922
            authid = ppp;
 
923
            authidlen = objlen;
 
924
          }
 
925
      no_authid:
 
926
        ;
 
927
      }
 
928
 
 
929
      /* Parse the commonKeyAttributes.  */
 
930
      where = __LINE__;
 
931
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
932
                              &ndef, &objlen, &hdrlen);
 
933
      if (!err && (objlen > nn || tag != TAG_SEQUENCE))
 
934
        err = gpg_error (GPG_ERR_INV_OBJ);
 
935
      if (err)
 
936
        goto parse_error;
 
937
      {
 
938
        const unsigned char *ppp = pp;
 
939
        size_t nnn = objlen;
 
940
 
 
941
        pp += objlen;
 
942
        nn -= objlen;
 
943
 
 
944
        /* Get the Id. */
 
945
        where = __LINE__;
 
946
        err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
947
                              &ndef, &objlen, &hdrlen);
 
948
        if (!err && (objlen > nnn
 
949
                     || class != CLASS_UNIVERSAL || tag != TAG_OCTET_STRING))
 
950
          err = gpg_error (GPG_ERR_INV_OBJ);
 
951
        if (err)
 
952
          goto parse_error;
 
953
        objid = ppp;
 
954
        objidlen = objlen;
 
955
        ppp += objlen;
 
956
        nnn -= objlen;
 
957
 
 
958
        /* Get the KeyUsageFlags. */
 
959
        where = __LINE__;
 
960
        err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
961
                              &ndef, &objlen, &hdrlen);
 
962
        if (!err && (objlen > nnn
 
963
                     || class != CLASS_UNIVERSAL || tag != TAG_BIT_STRING))
 
964
          err = gpg_error (GPG_ERR_INV_OBJ);
 
965
        if (err)
 
966
          goto parse_error;
 
967
        err = parse_keyusage_flags (ppp, objlen, &usageflags);
 
968
        if (err)
 
969
          goto parse_error;
 
970
        ppp += objlen;
 
971
        nnn -= objlen;
 
972
 
 
973
        /* Find the keyReference */
 
974
        where = __LINE__;
 
975
        err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
976
                              &ndef, &objlen, &hdrlen);
 
977
        if (gpg_err_code (err) == GPG_ERR_EOF)
 
978
          goto leave_cki;
 
979
        if (!err && objlen > nnn)
 
980
          err = gpg_error (GPG_ERR_INV_OBJ);
 
981
        if (err)
 
982
          goto parse_error;
 
983
        if (class == CLASS_UNIVERSAL && tag == TAG_BOOLEAN)
 
984
          {
 
985
            /* Skip the native element. */
 
986
            ppp += objlen;
 
987
            nnn -= objlen;
 
988
            
 
989
            err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
990
                                    &ndef, &objlen, &hdrlen);
 
991
            if (gpg_err_code (err) == GPG_ERR_EOF)
 
992
              goto leave_cki;
 
993
            if (!err && objlen > nnn)
 
994
              err = gpg_error (GPG_ERR_INV_OBJ);
 
995
            if (err)
 
996
              goto parse_error;
 
997
          }
 
998
        if (class == CLASS_UNIVERSAL && tag == TAG_BIT_STRING)
 
999
          {
 
1000
            /* Skip the accessFlags. */
 
1001
            ppp += objlen;
 
1002
            nnn -= objlen;
 
1003
            
 
1004
            err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
1005
                                    &ndef, &objlen, &hdrlen);
 
1006
            if (gpg_err_code (err) == GPG_ERR_EOF)
 
1007
              goto leave_cki;
 
1008
            if (!err && objlen > nnn)
 
1009
              err = gpg_error (GPG_ERR_INV_OBJ);
 
1010
            if (err)
 
1011
              goto parse_error;
 
1012
          }
 
1013
        if (class == CLASS_UNIVERSAL && tag == TAG_INTEGER)
 
1014
          {
 
1015
            /* Yep, this is the keyReference.  */
 
1016
            for (ul=0; objlen; objlen--)
 
1017
              {
 
1018
                ul <<= 8;
 
1019
                ul |= (*ppp++) & 0xff; 
 
1020
                nnn--;
 
1021
            }
 
1022
            key_reference = ul;
 
1023
            key_reference_valid = 1;
 
1024
          }
 
1025
 
 
1026
      leave_cki:
 
1027
        ;
 
1028
      }
 
1029
 
 
1030
 
 
1031
      /* Skip subClassAttributes.  */
 
1032
      where = __LINE__;
 
1033
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1034
                              &ndef, &objlen, &hdrlen);
 
1035
      if (!err && objlen > nn)
 
1036
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1037
      if (err)
 
1038
        goto parse_error;
 
1039
      if (class == CLASS_CONTEXT && tag == 0)
 
1040
        {
 
1041
          pp += objlen;
 
1042
          nn -= objlen;
 
1043
      
 
1044
          where = __LINE__;
 
1045
          err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1046
                                  &ndef, &objlen, &hdrlen);
 
1047
        }
 
1048
      /* Parse the keyAttributes.  */
 
1049
      if (!err && (objlen > nn || class != CLASS_CONTEXT || tag != 1))
 
1050
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1051
      if (err)
 
1052
        goto parse_error;
 
1053
      nn = objlen;
 
1054
 
 
1055
      where = __LINE__;
 
1056
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1057
                              &ndef, &objlen, &hdrlen);
 
1058
      if (!err && objlen > nn)
 
1059
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1060
      if (err)
 
1061
        goto parse_error;
 
1062
      if (class == CLASS_UNIVERSAL && tag == TAG_SEQUENCE)
 
1063
        ; /* RSA */
 
1064
      else if (class == CLASS_CONTEXT)
 
1065
        {
 
1066
          switch (tag)
 
1067
            {
 
1068
            case 0: errstr = "EC key objects are not supported"; break;
 
1069
            case 1: errstr = "DH key objects are not supported"; break;
 
1070
            case 2: errstr = "DSA key objects are not supported"; break;
 
1071
            case 3: errstr = "KEA key objects are not supported"; break;
 
1072
            default: errstr = "unknown privateKeyObject"; break;
 
1073
            }
 
1074
          goto parse_error;
 
1075
        }
 
1076
      else
 
1077
        {
 
1078
          err = gpg_error (GPG_ERR_INV_OBJ);
 
1079
          goto parse_error;
 
1080
        }
 
1081
 
 
1082
      nn = objlen;
 
1083
 
 
1084
      /* Check that the reference is a Path object.  */
 
1085
      where = __LINE__;
 
1086
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1087
                              &ndef, &objlen, &hdrlen);
 
1088
      if (!err && objlen > nn)
 
1089
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1090
      if (err)
 
1091
        goto parse_error;
 
1092
      if (class != CLASS_UNIVERSAL || tag != TAG_SEQUENCE)
 
1093
        {
 
1094
          errstr = "unsupported reference type";
 
1095
          goto parse_error;
 
1096
        }
 
1097
      nn = objlen;
 
1098
 
 
1099
      /* Parse the Path object. */
 
1100
      where = __LINE__;
 
1101
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1102
                              &ndef, &objlen, &hdrlen);
 
1103
      if (!err && objlen > nn)
 
1104
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1105
      if (err)
 
1106
        goto parse_error;
 
1107
 
 
1108
      /* Make sure that the next element is a non zero path and of
 
1109
         even length (FID are two bytes each). */
 
1110
      if (class != CLASS_UNIVERSAL || tag != TAG_OCTET_STRING
 
1111
          ||  !objlen || (objlen & 1) )
 
1112
        {
 
1113
          errstr = "invalid path reference";
 
1114
          goto parse_error;
 
1115
        }
 
1116
      /* Create a new PrKDF list item. */
 
1117
      prkdf = xtrycalloc (1, (sizeof *prkdf 
 
1118
                              - sizeof(unsigned short)
 
1119
                              + objlen/2 * sizeof(unsigned short)));
 
1120
      if (!prkdf)
 
1121
        {
 
1122
          err = gpg_error_from_errno (errno);
 
1123
          goto leave;
 
1124
        }
 
1125
      prkdf->objidlen = objidlen;
 
1126
      prkdf->objid = xtrymalloc (objidlen);
 
1127
      if (!prkdf->objid)
 
1128
        {
 
1129
          err = gpg_error_from_errno (errno);
 
1130
          xfree (prkdf);
 
1131
          goto leave;
 
1132
        }
 
1133
      memcpy (prkdf->objid, objid, objidlen);
 
1134
      if (authid)
 
1135
        {
 
1136
          prkdf->authidlen = authidlen;
 
1137
          prkdf->authid = xtrymalloc (authidlen);
 
1138
          if (!prkdf->authid)
 
1139
            {
 
1140
              err = gpg_error_from_errno (errno);
 
1141
              xfree (prkdf->objid);
 
1142
              xfree (prkdf);
 
1143
              goto leave;
 
1144
            }
 
1145
          memcpy (prkdf->authid, authid, authidlen);
 
1146
        }
 
1147
 
 
1148
      prkdf->pathlen = objlen/2;
 
1149
      for (i=0; i < prkdf->pathlen; i++, pp += 2, nn -= 2)
 
1150
        prkdf->path[i] = ((pp[0] << 8) | pp[1]);
 
1151
 
 
1152
      prkdf->usageflags = usageflags;
 
1153
      prkdf->key_reference = key_reference;
 
1154
      prkdf->key_reference_valid = key_reference_valid;
 
1155
 
 
1156
      if (nn)
 
1157
        {
 
1158
          /* An index and length follows. */
 
1159
          prkdf->have_off = 1;
 
1160
          where = __LINE__;
 
1161
          err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1162
                                  &ndef, &objlen, &hdrlen);
 
1163
          if (!err && (objlen > nn
 
1164
                       || class != CLASS_UNIVERSAL || tag != TAG_INTEGER))
 
1165
            err = gpg_error (GPG_ERR_INV_OBJ);
 
1166
          if (err)
 
1167
            goto parse_error;
 
1168
          
 
1169
          for (ul=0; objlen; objlen--)
 
1170
            {
 
1171
              ul <<= 8;
 
1172
              ul |= (*pp++) & 0xff; 
 
1173
              nn--;
 
1174
            }
 
1175
          prkdf->off = ul;
 
1176
          
 
1177
          where = __LINE__;
 
1178
          err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1179
                                  &ndef, &objlen, &hdrlen);
 
1180
          if (!err && (objlen > nn
 
1181
                       || class != CLASS_CONTEXT || tag != 0))
 
1182
            err = gpg_error (GPG_ERR_INV_OBJ);
 
1183
          if (err)
 
1184
            goto parse_error;
 
1185
          
 
1186
          for (ul=0; objlen; objlen--)
 
1187
            {
 
1188
              ul <<= 8;
 
1189
              ul |= (*pp++) & 0xff; 
 
1190
              nn--;
 
1191
            }
 
1192
          prkdf->len = ul;
 
1193
        }
 
1194
 
 
1195
 
 
1196
      log_debug ("PrKDF %04hX: id=", fid);
 
1197
      for (i=0; i < prkdf->objidlen; i++)
 
1198
        log_printf ("%02X", prkdf->objid[i]);
 
1199
      log_printf (" path=");
 
1200
      for (i=0; i < prkdf->pathlen; i++)
 
1201
        log_printf ("%04hX", prkdf->path[i]);
 
1202
      if (prkdf->have_off)
 
1203
        log_printf ("[%lu/%lu]", prkdf->off, prkdf->len);
 
1204
      if (prkdf->authid)
 
1205
        {
 
1206
          log_printf (" authid=");
 
1207
          for (i=0; i < prkdf->authidlen; i++)
 
1208
            log_printf ("%02X", prkdf->authid[i]);
 
1209
        }
 
1210
      if (prkdf->key_reference_valid)
 
1211
        log_printf (" keyref=0x%02lX", prkdf->key_reference);
 
1212
      log_printf (" usage=");
 
1213
      s = "";
 
1214
      if (prkdf->usageflags.encrypt) log_printf ("%sencrypt", s), s = ",";
 
1215
      if (prkdf->usageflags.decrypt) log_printf ("%sdecrypt", s), s = ",";
 
1216
      if (prkdf->usageflags.sign   ) log_printf ("%ssign", s), s = ",";
 
1217
      if (prkdf->usageflags.sign_recover)
 
1218
        log_printf ("%ssign_recover", s), s = ",";
 
1219
      if (prkdf->usageflags.wrap   ) log_printf ("%swrap", s), s = ",";
 
1220
      if (prkdf->usageflags.unwrap ) log_printf ("%sunwrap", s), s = ",";
 
1221
      if (prkdf->usageflags.verify ) log_printf ("%sverify", s), s = ",";
 
1222
      if (prkdf->usageflags.verify_recover)
 
1223
        log_printf ("%sverify_recover", s), s = ",";
 
1224
      if (prkdf->usageflags.derive ) log_printf ("%sderive", s), s = ",";
 
1225
      if (prkdf->usageflags.non_repudiation)
 
1226
        log_printf ("%snon_repudiation", s), s = ",";
 
1227
      log_printf ("\n");
 
1228
      
 
1229
      /* Put it into the list. */
 
1230
      prkdf->next = prkdflist;
 
1231
      prkdflist = prkdf;
 
1232
      prkdf = NULL;
 
1233
      continue; /* Ready. */
 
1234
 
 
1235
    parse_error:
 
1236
      log_error ("error parsing PrKDF record (%d): %s - skipped\n",
 
1237
                 where, errstr? errstr : gpg_strerror (err));
 
1238
      if (prkdf)
 
1239
        {
 
1240
          xfree (prkdf->objid);
 
1241
          xfree (prkdf->authid);
 
1242
          xfree (prkdf);
 
1243
        }
 
1244
      err = 0;
 
1245
    } /* End looping over all records. */
 
1246
 
 
1247
 leave:
 
1248
  xfree (buffer);
 
1249
  if (err)
 
1250
    release_prkdflist (prkdflist);
 
1251
  else
 
1252
    *result = prkdflist;
 
1253
  return err;
 
1254
}
 
1255
 
 
1256
 
 
1257
/* Read and parse the Certificate Directory Files identified by FID.
 
1258
   On success a newlist of CDF object gets stored at RESULT and the
 
1259
   caller is then responsible of releasing this list.  On error a
 
1260
   error code is returned and RESULT won't get changed.  */
 
1261
static gpg_error_t
 
1262
read_ef_cdf (app_t app, unsigned short fid, cdf_object_t *result)
 
1263
{
 
1264
  gpg_error_t err;
 
1265
  unsigned char *buffer = NULL;
 
1266
  size_t buflen;
 
1267
  const unsigned char *p;
 
1268
  size_t n, objlen, hdrlen;
 
1269
  int class, tag, constructed, ndef;
 
1270
  cdf_object_t cdflist = NULL;
 
1271
  int i;
 
1272
  
 
1273
  if (!fid)
 
1274
    return gpg_error (GPG_ERR_NO_DATA); /* No certificates. */
312
1275
  
313
1276
  err = select_and_read_binary (app->slot, fid, "CDF", &buffer, &buflen);
314
1277
  if (err)
318
1281
  n = buflen;
319
1282
 
320
1283
  /* Loop over the records.  We stop as soon as we detect a new record
321
 
     starting with 0x00 or 0xff as these values are commonly used to pad
322
 
     the the read datablocks and are no valid ASN.1 encoding. */
323
 
  while (n && *p && *p == 0xff)
 
1284
     starting with 0x00 or 0xff as these values are commonly used to
 
1285
     pad data blocks and are no valid ASN.1 encoding. */
 
1286
  while (n && *p && *p != 0xff)
324
1287
    {
325
1288
      const unsigned char *pp;
326
1289
      size_t nn;
327
 
 
 
1290
      int where;
 
1291
      const char *errstr = NULL;
 
1292
      cdf_object_t cdf = NULL;
 
1293
      unsigned long ul;
 
1294
      const unsigned char *objid;
 
1295
      size_t objidlen;
 
1296
      
328
1297
      err = parse_ber_header (&p, &n, &class, &tag, &constructed,
329
1298
                              &ndef, &objlen, &hdrlen);
330
1299
      if (!err && (objlen > n || tag != TAG_SEQUENCE))
340
1309
      n -= objlen;
341
1310
 
342
1311
      /* Skip the commonObjectAttributes.  */
343
 
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
344
 
                              &ndef, &objlen, &hdrlen);
345
 
      if (!err && (objlen > nn || tag != TAG_SEQUENCE))
346
 
        err = gpg_error (GPG_ERR_INV_OBJ);
347
 
      if (err)
348
 
        {
349
 
          log_error ("error parsing CDF record: %s - skipped\n",
350
 
                     gpg_strerror (err));
351
 
          continue;
352
 
        }
353
 
      pp += objlen;
354
 
      nn -= objlen;
355
 
 
356
 
      /* Skip the commonCertificateAttributes.  */
357
 
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
358
 
                              &ndef, &objlen, &hdrlen);
359
 
      if (!err && (objlen > nn || tag != TAG_SEQUENCE))
360
 
        err = gpg_error (GPG_ERR_INV_OBJ);
361
 
      if (err)
362
 
        {
363
 
          log_error ("error parsing CDF record: %s - skipped\n",
364
 
                     gpg_strerror (err));
365
 
          continue;
366
 
        }
367
 
      pp += objlen;
368
 
      nn -= objlen;
369
 
 
370
 
      /* FIXME: Check that this is a reference to a certificate. */
371
 
 
372
 
 
373
 
    }
374
 
 
 
1312
      where = __LINE__;
 
1313
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1314
                              &ndef, &objlen, &hdrlen);
 
1315
      if (!err && (objlen > nn || tag != TAG_SEQUENCE))
 
1316
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1317
      if (err)
 
1318
        goto parse_error;
 
1319
      pp += objlen;
 
1320
      nn -= objlen;
 
1321
 
 
1322
      /* Parse the commonCertificateAttributes.  */
 
1323
      where = __LINE__;
 
1324
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1325
                              &ndef, &objlen, &hdrlen);
 
1326
      if (!err && (objlen > nn || tag != TAG_SEQUENCE))
 
1327
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1328
      if (err)
 
1329
        goto parse_error;
 
1330
      {
 
1331
        const unsigned char *ppp = pp;
 
1332
        size_t nnn = objlen;
 
1333
 
 
1334
        pp += objlen;
 
1335
        nn -= objlen;
 
1336
 
 
1337
        /* Get the Id. */
 
1338
        where = __LINE__;
 
1339
        err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
1340
                              &ndef, &objlen, &hdrlen);
 
1341
        if (!err && (objlen > nnn
 
1342
                     || class != CLASS_UNIVERSAL || tag != TAG_OCTET_STRING))
 
1343
          err = gpg_error (GPG_ERR_INV_OBJ);
 
1344
        if (err)
 
1345
          goto parse_error;
 
1346
        objid = ppp;
 
1347
        objidlen = objlen;
 
1348
      }
 
1349
 
 
1350
      /* Parse the certAttribute.  */
 
1351
      where = __LINE__;
 
1352
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1353
                              &ndef, &objlen, &hdrlen);
 
1354
      if (!err && (objlen > nn || class != CLASS_CONTEXT || tag != 1))
 
1355
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1356
      if (err)
 
1357
        goto parse_error;
 
1358
      nn = objlen;
 
1359
 
 
1360
      where = __LINE__;
 
1361
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1362
                              &ndef, &objlen, &hdrlen);
 
1363
      if (!err && (objlen > nn
 
1364
                   || class != CLASS_UNIVERSAL || tag != TAG_SEQUENCE))
 
1365
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1366
      if (err)
 
1367
        goto parse_error;
 
1368
      nn = objlen;
 
1369
 
 
1370
      /* Check that the reference is a Path object.  */
 
1371
      where = __LINE__;
 
1372
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1373
                              &ndef, &objlen, &hdrlen);
 
1374
      if (!err && objlen > nn)
 
1375
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1376
      if (err)
 
1377
        goto parse_error;
 
1378
      if (class != CLASS_UNIVERSAL || tag != TAG_SEQUENCE)
 
1379
        {
 
1380
          errstr = "unsupported reference type";
 
1381
          continue;
 
1382
        }
 
1383
      nn = objlen;
 
1384
 
 
1385
      /* Parse the Path object. */
 
1386
      where = __LINE__;
 
1387
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1388
                              &ndef, &objlen, &hdrlen);
 
1389
      if (!err && objlen > nn)
 
1390
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1391
      if (err)
 
1392
        goto parse_error;
 
1393
 
 
1394
      /* Make sure that the next element is a non zero path and of
 
1395
         even length (FID are two bytes each). */
 
1396
      if (class != CLASS_UNIVERSAL || tag != TAG_OCTET_STRING
 
1397
          ||  !objlen || (objlen & 1) )
 
1398
        {
 
1399
          errstr = "invalid path reference";
 
1400
          goto parse_error;
 
1401
        }
 
1402
      /* Create a new CDF list item. */
 
1403
      cdf = xtrycalloc (1, (sizeof *cdf 
 
1404
                            - sizeof(unsigned short)
 
1405
                            + objlen/2 * sizeof(unsigned short)));
 
1406
      if (!cdf)
 
1407
        {
 
1408
          err = gpg_error_from_errno (errno);
 
1409
          goto leave;
 
1410
        }
 
1411
      cdf->objidlen = objidlen;
 
1412
      cdf->objid = xtrymalloc (objidlen);
 
1413
      if (!cdf->objid)
 
1414
        {
 
1415
          err = gpg_error_from_errno (errno);
 
1416
          xfree (cdf);
 
1417
          goto leave;
 
1418
        }
 
1419
      memcpy (cdf->objid, objid, objidlen);
 
1420
 
 
1421
      cdf->pathlen = objlen/2;
 
1422
      for (i=0; i < cdf->pathlen; i++, pp += 2, nn -= 2)
 
1423
        cdf->path[i] = ((pp[0] << 8) | pp[1]);
 
1424
 
 
1425
      if (nn)
 
1426
        {
 
1427
          /* An index and length follows. */
 
1428
          cdf->have_off = 1;
 
1429
          where = __LINE__;
 
1430
          err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1431
                                  &ndef, &objlen, &hdrlen);
 
1432
          if (!err && (objlen > nn
 
1433
                       || class != CLASS_UNIVERSAL || tag != TAG_INTEGER))
 
1434
            err = gpg_error (GPG_ERR_INV_OBJ);
 
1435
          if (err)
 
1436
            goto parse_error;
 
1437
          
 
1438
          for (ul=0; objlen; objlen--)
 
1439
            {
 
1440
              ul <<= 8;
 
1441
              ul |= (*pp++) & 0xff; 
 
1442
              nn--;
 
1443
            }
 
1444
          cdf->off = ul;
 
1445
          
 
1446
          where = __LINE__;
 
1447
          err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1448
                                  &ndef, &objlen, &hdrlen);
 
1449
          if (!err && (objlen > nn
 
1450
                       || class != CLASS_CONTEXT || tag != 0))
 
1451
            err = gpg_error (GPG_ERR_INV_OBJ);
 
1452
          if (err)
 
1453
            goto parse_error;
 
1454
          
 
1455
          for (ul=0; objlen; objlen--)
 
1456
            {
 
1457
              ul <<= 8;
 
1458
              ul |= (*pp++) & 0xff; 
 
1459
              nn--;
 
1460
            }
 
1461
          cdf->len = ul;
 
1462
        }
 
1463
 
 
1464
      log_debug ("CDF %04hX: id=", fid);
 
1465
      for (i=0; i < cdf->objidlen; i++)
 
1466
        log_printf ("%02X", cdf->objid[i]);
 
1467
      log_printf (" path=");
 
1468
      for (i=0; i < cdf->pathlen; i++)
 
1469
        log_printf ("%04hX", cdf->path[i]);
 
1470
      if (cdf->have_off)
 
1471
        log_printf ("[%lu/%lu]", cdf->off, cdf->len);
 
1472
      log_printf ("\n");
 
1473
      
 
1474
      /* Put it into the list. */
 
1475
      cdf->next = cdflist;
 
1476
      cdflist = cdf;
 
1477
      cdf = NULL;
 
1478
      continue; /* Ready. */
 
1479
 
 
1480
    parse_error:
 
1481
      log_error ("error parsing CDF record (%d): %s - skipped\n",
 
1482
                 where, errstr? errstr : gpg_strerror (err));
 
1483
      xfree (cdf);
 
1484
      err = 0;
 
1485
    } /* End looping over all records. */
375
1486
 
376
1487
 leave:
377
1488
  xfree (buffer);
 
1489
  if (err)
 
1490
    release_cdflist (cdflist);
 
1491
  else
 
1492
    *result = cdflist;
378
1493
  return err;
379
1494
}
380
1495
 
381
 
/* Read and parse Authentication Object Directory Files.  */
382
 
static gpg_error_t 
383
 
read_ef_aodf (app_t app)
 
1496
 
 
1497
/*
 
1498
SEQUENCE {
 
1499
  SEQUENCE { -- CommonObjectAttributes
 
1500
    UTF8String 'specific PIN for DS'
 
1501
    BIT STRING 0 unused bits
 
1502
      '00000011'B
 
1503
    }
 
1504
  SEQUENCE { -- CommonAuthenticationObjectAttributes
 
1505
    OCTET STRING
 
1506
      07    -- iD
 
1507
    }
 
1508
 
 
1509
  [1] { -- typeAttributes
 
1510
    SEQUENCE { -- PinAttributes
 
1511
      BIT STRING 0 unused bits
 
1512
        '0000100000110010'B  -- local,initialized,needs-padding
 
1513
                             -- exchangeRefData
 
1514
      ENUMERATED 1           -- ascii-numeric
 
1515
      INTEGER 6              -- minLength
 
1516
      INTEGER 6              -- storedLength
 
1517
      INTEGER 8              -- maxLength
 
1518
      [0]
 
1519
        02                   -- pinReference
 
1520
      GeneralizedTime 19/04/2002 12:12 GMT  -- lastPinChange
 
1521
      SEQUENCE {
 
1522
        OCTET STRING
 
1523
          3F 00 40 16        -- path to DF of PIN
 
1524
        }
 
1525
      }
 
1526
    }
 
1527
  }
 
1528
 
 
1529
*/
 
1530
/* Read and parse an Authentication Object Directory File identified
 
1531
   by FID.  On success a newlist of AODF objects gets stored at RESULT
 
1532
   and the caller is responsible of releasing this list.  On error a
 
1533
   error code is returned and RESULT won't get changed.  */
 
1534
static gpg_error_t
 
1535
read_ef_aodf (app_t app, unsigned short fid, aodf_object_t *result)
384
1536
{
385
 
 
 
1537
  gpg_error_t err;
 
1538
  unsigned char *buffer = NULL;
 
1539
  size_t buflen;
 
1540
  const unsigned char *p;
 
1541
  size_t n, objlen, hdrlen;
 
1542
  int class, tag, constructed, ndef;
 
1543
  aodf_object_t aodflist = NULL;
 
1544
  int i;
 
1545
  
 
1546
  if (!fid)
 
1547
    return gpg_error (GPG_ERR_NO_DATA); /* No authentication objects. */
 
1548
  
 
1549
  err = select_and_read_binary (app->slot, fid, "AODF", &buffer, &buflen);
 
1550
  if (err)
 
1551
    return err;
 
1552
  
 
1553
  p = buffer;
 
1554
  n = buflen;
 
1555
 
 
1556
  /* FIXME: This shares a LOT of code with read_ef_prkdf! */
 
1557
 
 
1558
  /* Loop over the records.  We stop as soon as we detect a new record
 
1559
     starting with 0x00 or 0xff as these values are commonly used to
 
1560
     pad data blocks and are no valid ASN.1 encoding. */
 
1561
  while (n && *p && *p != 0xff)
 
1562
    {
 
1563
      const unsigned char *pp;
 
1564
      size_t nn;
 
1565
      int where;
 
1566
      const char *errstr = NULL;
 
1567
      aodf_object_t aodf = NULL;
 
1568
      unsigned long ul;
 
1569
      const char *s;
 
1570
 
 
1571
      err = parse_ber_header (&p, &n, &class, &tag, &constructed,
 
1572
                              &ndef, &objlen, &hdrlen);
 
1573
      if (!err && (objlen > n || tag != TAG_SEQUENCE))
 
1574
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1575
      if (err)
 
1576
        {
 
1577
          log_error ("error parsing AODF record: %s\n", gpg_strerror (err));
 
1578
          goto leave;
 
1579
        }
 
1580
      pp = p;
 
1581
      nn = objlen;
 
1582
      p += objlen;
 
1583
      n -= objlen;
 
1584
 
 
1585
      /* Allocate memory for a new AODF list item. */
 
1586
      aodf = xtrycalloc (1, sizeof *aodf);
 
1587
      if (!aodf)
 
1588
        goto no_core;
 
1589
 
 
1590
      /* Parse the commonObjectAttributes.  */
 
1591
      where = __LINE__;
 
1592
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1593
                              &ndef, &objlen, &hdrlen);
 
1594
      if (!err && (objlen > nn || tag != TAG_SEQUENCE))
 
1595
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1596
      if (err)
 
1597
        goto parse_error;
 
1598
      {
 
1599
        const unsigned char *ppp = pp;
 
1600
        size_t nnn = objlen;
 
1601
 
 
1602
        pp += objlen;
 
1603
        nn -= objlen;
 
1604
 
 
1605
        /* Search the optional AuthId.  We need to skip the optional
 
1606
           Label (UTF8STRING) and the optional CommonObjectFlags
 
1607
           (BITSTRING). */
 
1608
        where = __LINE__;
 
1609
        err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
1610
                                &ndef, &objlen, &hdrlen);
 
1611
        if (!err && (objlen > nnn || class != CLASS_UNIVERSAL))
 
1612
          err = gpg_error (GPG_ERR_INV_OBJ);
 
1613
        if (gpg_err_code (err) == GPG_ERR_EOF)
 
1614
          goto no_authid;
 
1615
        if (err)
 
1616
          goto parse_error;
 
1617
        if (tag == TAG_UTF8_STRING)
 
1618
          {
 
1619
            ppp += objlen; /* Skip the Label. */
 
1620
            nnn -= objlen;
 
1621
 
 
1622
            where = __LINE__;
 
1623
            err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
1624
                                    &ndef, &objlen, &hdrlen);
 
1625
            if (!err && (objlen > nnn || class != CLASS_UNIVERSAL))
 
1626
              err = gpg_error (GPG_ERR_INV_OBJ);
 
1627
            if (gpg_err_code (err) == GPG_ERR_EOF)
 
1628
              goto no_authid;
 
1629
            if (err)
 
1630
              goto parse_error;
 
1631
          }
 
1632
        if (tag == TAG_BIT_STRING)
 
1633
          {
 
1634
            ppp += objlen; /* Skip the CommonObjectFlags.  */
 
1635
            nnn -= objlen;
 
1636
 
 
1637
            where = __LINE__;
 
1638
            err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
1639
                                    &ndef, &objlen, &hdrlen);
 
1640
            if (!err && (objlen > nnn || class != CLASS_UNIVERSAL))
 
1641
              err = gpg_error (GPG_ERR_INV_OBJ);
 
1642
            if (gpg_err_code (err) == GPG_ERR_EOF)
 
1643
              goto no_authid;
 
1644
            if (err)
 
1645
              goto parse_error;
 
1646
          }
 
1647
        if (tag == TAG_OCTET_STRING && objlen)
 
1648
          {
 
1649
            aodf->authidlen = objlen;
 
1650
            aodf->authid = xtrymalloc (objlen);
 
1651
            if (!aodf->authid)
 
1652
              goto no_core;
 
1653
            memcpy (aodf->authid, ppp, objlen);
 
1654
          }
 
1655
      no_authid:
 
1656
        ;
 
1657
      }
 
1658
 
 
1659
      /* Parse the CommonAuthenticationObjectAttributes.  */
 
1660
      where = __LINE__;
 
1661
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1662
                              &ndef, &objlen, &hdrlen);
 
1663
      if (!err && (objlen > nn || tag != TAG_SEQUENCE))
 
1664
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1665
      if (err)
 
1666
        goto parse_error;
 
1667
      {
 
1668
        const unsigned char *ppp = pp;
 
1669
        size_t nnn = objlen;
 
1670
 
 
1671
        pp += objlen;
 
1672
        nn -= objlen;
 
1673
 
 
1674
        /* Get the Id. */
 
1675
        where = __LINE__;
 
1676
        err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
1677
                              &ndef, &objlen, &hdrlen);
 
1678
        if (!err && (objlen > nnn
 
1679
                     || class != CLASS_UNIVERSAL || tag != TAG_OCTET_STRING))
 
1680
          err = gpg_error (GPG_ERR_INV_OBJ);
 
1681
        if (err)
 
1682
          goto parse_error;
 
1683
 
 
1684
        aodf->objidlen = objlen;
 
1685
        aodf->objid = xtrymalloc (objlen);
 
1686
        if (!aodf->objid)
 
1687
          goto no_core;
 
1688
        memcpy (aodf->objid, ppp, objlen);
 
1689
      }
 
1690
 
 
1691
      /* Parse the typeAttributes.  */
 
1692
      where = __LINE__;
 
1693
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1694
                              &ndef, &objlen, &hdrlen);
 
1695
      if (!err && (objlen > nn || class != CLASS_CONTEXT || tag != 1))
 
1696
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1697
      if (err)
 
1698
        goto parse_error;
 
1699
      nn = objlen;
 
1700
 
 
1701
      where = __LINE__;
 
1702
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1703
                              &ndef, &objlen, &hdrlen);
 
1704
      if (!err && objlen > nn)
 
1705
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1706
      if (err)
 
1707
        goto parse_error;
 
1708
      if (class == CLASS_UNIVERSAL && tag == TAG_SEQUENCE)
 
1709
        ; /* PinAttributes */
 
1710
      else if (class == CLASS_CONTEXT)
 
1711
        {
 
1712
          switch (tag)
 
1713
            {
 
1714
            case 0: errstr = "biometric auth types are not supported"; break;
 
1715
            case 1: errstr = "authKey auth types are not supported"; break;
 
1716
            case 2: errstr = "external auth type are not supported"; break;
 
1717
            default: errstr = "unknown privateKeyObject"; break;
 
1718
            }
 
1719
          goto parse_error;
 
1720
        }
 
1721
      else
 
1722
        {
 
1723
          err = gpg_error (GPG_ERR_INV_OBJ);
 
1724
          goto parse_error;
 
1725
        }
 
1726
 
 
1727
      nn = objlen;
 
1728
 
 
1729
      /* PinFlags */
 
1730
      where = __LINE__;
 
1731
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1732
                              &ndef, &objlen, &hdrlen);
 
1733
      if (!err && (objlen > nn || !objlen
 
1734
                   || class != CLASS_UNIVERSAL || tag != TAG_BIT_STRING))
 
1735
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1736
      if (err)
 
1737
        goto parse_error;
 
1738
 
 
1739
      {
 
1740
        unsigned int bits, mask;
 
1741
        int unused, full;
 
1742
        
 
1743
        unused = *pp++; nn--; objlen--;
 
1744
        if ((!objlen && unused) || unused/8 > objlen)
 
1745
          {
 
1746
            err = gpg_error (GPG_ERR_ENCODING_PROBLEM);
 
1747
            goto parse_error;
 
1748
          }
 
1749
        full = objlen - (unused+7)/8;
 
1750
        unused %= 8;
 
1751
        mask = 0;
 
1752
        for (i=1; unused; i <<= 1, unused--)
 
1753
          mask |= i;
 
1754
      
 
1755
        /* The first octet */
 
1756
        bits = 0;
 
1757
        if (objlen)
 
1758
          {
 
1759
            bits = *pp++; nn--; objlen--;
 
1760
            if (full)
 
1761
              full--;
 
1762
            else 
 
1763
              {
 
1764
                bits &= ~mask;
 
1765
                mask = 0; 
 
1766
              }
 
1767
          }
 
1768
        if ((bits & 0x80)) /* ASN.1 bit 0. */
 
1769
          aodf->pinflags.case_sensitive = 1;
 
1770
        if ((bits & 0x40)) /* ASN.1 bit 1. */
 
1771
          aodf->pinflags.local = 1;
 
1772
        if ((bits & 0x20)) 
 
1773
          aodf->pinflags.change_disabled = 1;
 
1774
        if ((bits & 0x10)) 
 
1775
          aodf->pinflags.unblock_disabled = 1;
 
1776
        if ((bits & 0x08)) 
 
1777
          aodf->pinflags.initialized = 1;
 
1778
        if ((bits & 0x04)) 
 
1779
          aodf->pinflags.needs_padding = 1;
 
1780
        if ((bits & 0x02)) 
 
1781
          aodf->pinflags.unblocking_pin = 1;
 
1782
        if ((bits & 0x01)) 
 
1783
          aodf->pinflags.so_pin = 1;
 
1784
        /* The second octet. */
 
1785
        bits = 0;
 
1786
        if (objlen)
 
1787
          {
 
1788
            bits = *pp++; nn--; objlen--;
 
1789
            if (full)
 
1790
              full--;
 
1791
            else 
 
1792
              {
 
1793
                bits &= ~mask;
 
1794
                mask = 0; 
 
1795
              }
 
1796
          }
 
1797
        if ((bits & 0x80)) 
 
1798
          aodf->pinflags.disable_allowed = 1;
 
1799
        if ((bits & 0x40)) 
 
1800
          aodf->pinflags.integrity_protected = 1;
 
1801
        if ((bits & 0x20)) 
 
1802
          aodf->pinflags.confidentiality_protected = 1;
 
1803
        if ((bits & 0x10)) 
 
1804
          aodf->pinflags.exchange_ref_data = 1;
 
1805
        /* Skip remaining bits. */
 
1806
        pp += objlen;
 
1807
        nn -= objlen;
 
1808
      }
 
1809
 
 
1810
 
 
1811
      /* PinType */
 
1812
      where = __LINE__;
 
1813
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1814
                              &ndef, &objlen, &hdrlen);
 
1815
      if (!err && (objlen > nn
 
1816
                   || class != CLASS_UNIVERSAL || tag != TAG_ENUMERATED))
 
1817
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1818
      if (!err && (objlen > sizeof (pin_type_t) || objlen > sizeof (ul)))
 
1819
        err = gpg_error (GPG_ERR_UNSUPPORTED_ENCODING);
 
1820
      if (err)
 
1821
        goto parse_error;
 
1822
 
 
1823
      for (ul=0; objlen; objlen--)
 
1824
        {
 
1825
          ul <<= 8;
 
1826
          ul |= (*pp++) & 0xff; 
 
1827
          nn--;
 
1828
        }
 
1829
      aodf->pintype = ul;
 
1830
 
 
1831
 
 
1832
      /* minLength */
 
1833
      where = __LINE__;
 
1834
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1835
                              &ndef, &objlen, &hdrlen);
 
1836
      if (!err && (objlen > nn
 
1837
                   || class != CLASS_UNIVERSAL || tag != TAG_INTEGER))
 
1838
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1839
      if (!err && objlen > sizeof (ul))
 
1840
        err = gpg_error (GPG_ERR_UNSUPPORTED_ENCODING);
 
1841
      if (err)
 
1842
        goto parse_error;
 
1843
      for (ul=0; objlen; objlen--)
 
1844
        {
 
1845
          ul <<= 8;
 
1846
          ul |= (*pp++) & 0xff; 
 
1847
          nn--;
 
1848
        }
 
1849
      aodf->min_length = ul;
 
1850
 
 
1851
 
 
1852
      /* storedLength */
 
1853
      where = __LINE__;
 
1854
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1855
                              &ndef, &objlen, &hdrlen);
 
1856
      if (!err && (objlen > nn
 
1857
                   || class != CLASS_UNIVERSAL || tag != TAG_INTEGER))
 
1858
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1859
      if (!err && objlen > sizeof (ul))
 
1860
        err = gpg_error (GPG_ERR_UNSUPPORTED_ENCODING);
 
1861
      if (err)
 
1862
        goto parse_error;
 
1863
      for (ul=0; objlen; objlen--)
 
1864
        {
 
1865
          ul <<= 8;
 
1866
          ul |= (*pp++) & 0xff; 
 
1867
          nn--;
 
1868
        }
 
1869
      aodf->stored_length = ul;
 
1870
 
 
1871
      /* optional maxLength */
 
1872
      where = __LINE__;
 
1873
      err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1874
                              &ndef, &objlen, &hdrlen);
 
1875
      if (gpg_err_code (err) == GPG_ERR_EOF)
 
1876
        goto ready;
 
1877
      if (!err && objlen > nn)
 
1878
        err = gpg_error (GPG_ERR_INV_OBJ);
 
1879
      if (err)
 
1880
        goto parse_error;
 
1881
      if (class == CLASS_UNIVERSAL && tag == TAG_INTEGER)
 
1882
        {
 
1883
          if (objlen > sizeof (ul))
 
1884
            {
 
1885
              err = gpg_error (GPG_ERR_UNSUPPORTED_ENCODING);
 
1886
              goto parse_error;
 
1887
            }
 
1888
          for (ul=0; objlen; objlen--)
 
1889
            {
 
1890
              ul <<= 8;
 
1891
              ul |= (*pp++) & 0xff; 
 
1892
              nn--;
 
1893
            }
 
1894
          aodf->max_length = ul;
 
1895
          aodf->max_length_valid = 1;
 
1896
          
 
1897
          where = __LINE__;
 
1898
          err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1899
                                  &ndef, &objlen, &hdrlen);
 
1900
          if (gpg_err_code (err) == GPG_ERR_EOF)
 
1901
            goto ready;
 
1902
          if (!err && objlen > nn)
 
1903
            err = gpg_error (GPG_ERR_INV_OBJ);
 
1904
          if (err)
 
1905
            goto parse_error;
 
1906
        }
 
1907
 
 
1908
      /* Optional pinReference. */
 
1909
      if (class == CLASS_CONTEXT && tag == 0)
 
1910
        {
 
1911
          if (objlen > sizeof (ul))
 
1912
            {
 
1913
              err = gpg_error (GPG_ERR_UNSUPPORTED_ENCODING);
 
1914
              goto parse_error;
 
1915
            }
 
1916
          for (ul=0; objlen; objlen--)
 
1917
            {
 
1918
              ul <<= 8;
 
1919
              ul |= (*pp++) & 0xff; 
 
1920
              nn--;
 
1921
            }
 
1922
          aodf->pin_reference = ul;
 
1923
          aodf->pin_reference_valid = 1;
 
1924
          
 
1925
          where = __LINE__;
 
1926
          err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1927
                                  &ndef, &objlen, &hdrlen);
 
1928
          if (gpg_err_code (err) == GPG_ERR_EOF)
 
1929
            goto ready;
 
1930
          if (!err && objlen > nn)
 
1931
            err = gpg_error (GPG_ERR_INV_OBJ);
 
1932
          if (err)
 
1933
            goto parse_error;
 
1934
        }
 
1935
 
 
1936
      /* Optional padChar. */
 
1937
      if (class == CLASS_UNIVERSAL && tag == TAG_OCTET_STRING)
 
1938
        {
 
1939
          if (objlen != 1)
 
1940
            {
 
1941
              errstr = "padChar is not of size(1)";
 
1942
              goto parse_error;
 
1943
            }
 
1944
          aodf->pad_char = *pp++; nn--;
 
1945
          aodf->pad_char_valid = 1;
 
1946
          
 
1947
          where = __LINE__;
 
1948
          err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1949
                                  &ndef, &objlen, &hdrlen);
 
1950
          if (gpg_err_code (err) == GPG_ERR_EOF)
 
1951
            goto ready;
 
1952
          if (!err && objlen > nn)
 
1953
            err = gpg_error (GPG_ERR_INV_OBJ);
 
1954
          if (err)
 
1955
            goto parse_error;
 
1956
        }
 
1957
 
 
1958
      /* Skip optional lastPinChange. */
 
1959
      if (class == CLASS_UNIVERSAL && tag == TAG_GENERALIZED_TIME)
 
1960
        {
 
1961
          pp += objlen;
 
1962
          nn -= objlen;
 
1963
          
 
1964
          where = __LINE__;
 
1965
          err = parse_ber_header (&pp, &nn, &class, &tag, &constructed,
 
1966
                                  &ndef, &objlen, &hdrlen);
 
1967
          if (gpg_err_code (err) == GPG_ERR_EOF)
 
1968
            goto ready;
 
1969
          if (!err && objlen > nn)
 
1970
            err = gpg_error (GPG_ERR_INV_OBJ);
 
1971
          if (err)
 
1972
            goto parse_error;
 
1973
        }
 
1974
 
 
1975
      /* Optional Path object.  */
 
1976
      if (class == CLASS_UNIVERSAL || tag == TAG_SEQUENCE)
 
1977
        {
 
1978
          const unsigned char *ppp = pp;
 
1979
          size_t nnn = objlen;
 
1980
          
 
1981
          pp += objlen;
 
1982
          nn -= objlen;
 
1983
 
 
1984
          where = __LINE__;
 
1985
          err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
1986
                                  &ndef, &objlen, &hdrlen);
 
1987
          if (!err && objlen > nnn)
 
1988
            err = gpg_error (GPG_ERR_INV_OBJ);
 
1989
          if (err)
 
1990
            goto parse_error;
 
1991
 
 
1992
          /* Make sure that the next element is a non zero FID and of
 
1993
             even length (FID are two bytes each). */
 
1994
          if (class != CLASS_UNIVERSAL || tag != TAG_OCTET_STRING
 
1995
              ||  !objlen || (objlen & 1) )
 
1996
            {
 
1997
              errstr = "invalid path reference";
 
1998
              goto parse_error;
 
1999
            }
 
2000
 
 
2001
          aodf->pathlen = objlen/2;
 
2002
          aodf->path = xtrymalloc (aodf->pathlen);
 
2003
          if (!aodf->path)
 
2004
            goto no_core;
 
2005
          for (i=0; i < aodf->pathlen; i++, ppp += 2, nnn -= 2)
 
2006
            aodf->path[i] = ((ppp[0] << 8) | ppp[1]);
 
2007
          
 
2008
          if (nnn)
 
2009
            {
 
2010
              /* An index and length follows. */
 
2011
              aodf->have_off = 1;
 
2012
              where = __LINE__;
 
2013
              err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
2014
                                      &ndef, &objlen, &hdrlen);
 
2015
              if (!err && (objlen > nnn
 
2016
                       || class != CLASS_UNIVERSAL || tag != TAG_INTEGER))
 
2017
                err = gpg_error (GPG_ERR_INV_OBJ);
 
2018
              if (err)
 
2019
                goto parse_error;
 
2020
              
 
2021
              for (ul=0; objlen; objlen--)
 
2022
                {
 
2023
                  ul <<= 8;
 
2024
                  ul |= (*ppp++) & 0xff; 
 
2025
                  nnn--;
 
2026
                }
 
2027
              aodf->off = ul;
 
2028
              
 
2029
              where = __LINE__;
 
2030
              err = parse_ber_header (&ppp, &nnn, &class, &tag, &constructed,
 
2031
                                      &ndef, &objlen, &hdrlen);
 
2032
              if (!err && (objlen > nnn
 
2033
                           || class != CLASS_CONTEXT || tag != 0))
 
2034
                err = gpg_error (GPG_ERR_INV_OBJ);
 
2035
              if (err)
 
2036
                goto parse_error;
 
2037
              
 
2038
              for (ul=0; objlen; objlen--)
 
2039
                {
 
2040
                  ul <<= 8;
 
2041
                  ul |= (*ppp++) & 0xff; 
 
2042
                  nnn--;
 
2043
                }
 
2044
              aodf->len = ul;
 
2045
            }
 
2046
        }
 
2047
      
 
2048
      /* Igonore further objects which might be there due to future
 
2049
         extensions of pkcs#15. */
 
2050
 
 
2051
    ready:
 
2052
      log_debug ("AODF %04hX: id=", fid);
 
2053
      for (i=0; i < aodf->objidlen; i++)
 
2054
        log_printf ("%02X", aodf->objid[i]);
 
2055
      if (aodf->authid)
 
2056
        {
 
2057
          log_printf (" authid=");
 
2058
          for (i=0; i < aodf->authidlen; i++)
 
2059
            log_printf ("%02X", aodf->authid[i]);
 
2060
        }
 
2061
      log_printf (" flags=");
 
2062
      s = "";
 
2063
      if (aodf->pinflags.case_sensitive)
 
2064
        log_printf ("%scase_sensitive", s), s = ",";
 
2065
      if (aodf->pinflags.local)
 
2066
        log_printf ("%slocal", s), s = ",";
 
2067
      if (aodf->pinflags.change_disabled)
 
2068
        log_printf ("%schange_disabled", s), s = ",";
 
2069
      if (aodf->pinflags.unblock_disabled)
 
2070
        log_printf ("%sunblock_disabled", s), s = ",";
 
2071
      if (aodf->pinflags.initialized)
 
2072
        log_printf ("%sinitialized", s), s = ",";
 
2073
      if (aodf->pinflags.needs_padding)
 
2074
        log_printf ("%sneeds_padding", s), s = ",";
 
2075
      if (aodf->pinflags.unblocking_pin)
 
2076
        log_printf ("%sunblocking_pin", s), s = ",";
 
2077
      if (aodf->pinflags.so_pin)
 
2078
        log_printf ("%sso_pin", s), s = ",";
 
2079
      if (aodf->pinflags.disable_allowed)
 
2080
        log_printf ("%sdisable_allowed", s), s = ",";
 
2081
      if (aodf->pinflags.integrity_protected)
 
2082
        log_printf ("%sintegrity_protected", s), s = ",";
 
2083
      if (aodf->pinflags.confidentiality_protected)
 
2084
        log_printf ("%sconfidentiality_protected", s), s = ",";
 
2085
      if (aodf->pinflags.exchange_ref_data)
 
2086
        log_printf ("%sexchange_ref_data", s), s = ",";
 
2087
      {
 
2088
        char numbuf[50];
 
2089
        switch (aodf->pintype)
 
2090
          {
 
2091
          case PIN_TYPE_BCD: s = "bcd"; break;
 
2092
          case PIN_TYPE_ASCII_NUMERIC: s = "ascii-numeric"; break;
 
2093
          case PIN_TYPE_UTF8: s = "utf8"; break;
 
2094
          case PIN_TYPE_HALF_NIBBLE_BCD: s = "half-nibble-bcd"; break;
 
2095
          case PIN_TYPE_ISO9564_1: s = "iso9564-1"; break;
 
2096
          default:
 
2097
            sprintf (numbuf, "%lu", (unsigned long)aodf->pintype);
 
2098
            s = numbuf;
 
2099
          }
 
2100
        log_printf (" type=%s", s);
 
2101
      }
 
2102
      log_printf (" min=%lu", aodf->min_length);
 
2103
      log_printf (" stored=%lu", aodf->stored_length);
 
2104
      if (aodf->max_length_valid)
 
2105
        log_printf (" max=%lu", aodf->max_length);
 
2106
      if (aodf->pad_char_valid)
 
2107
        log_printf (" pad=0x%02x", aodf->pad_char);
 
2108
      if (aodf->pin_reference_valid)
 
2109
        log_printf (" pinref=0x%02lX", aodf->pin_reference);
 
2110
      if (aodf->pathlen)
 
2111
        {
 
2112
          log_printf (" path=");
 
2113
          for (i=0; i < aodf->pathlen; i++)
 
2114
            log_printf ("%04hX", aodf->path[i]);
 
2115
          if (aodf->have_off)
 
2116
            log_printf ("[%lu/%lu]", aodf->off, aodf->len);
 
2117
        }
 
2118
      log_printf ("\n");
 
2119
      
 
2120
      /* Put it into the list. */
 
2121
      aodf->next = aodflist;
 
2122
      aodflist = aodf;
 
2123
      aodf = NULL;
 
2124
      continue; /* Ready. */
 
2125
 
 
2126
    no_core:
 
2127
      err = gpg_error_from_errno (errno);
 
2128
      release_aodf_object (aodf);
 
2129
      goto leave;
 
2130
 
 
2131
    parse_error:
 
2132
      log_error ("error parsing AODF record (%d): %s - skipped\n",
 
2133
                 where, errstr? errstr : gpg_strerror (err));
 
2134
      err = 0;
 
2135
      release_aodf_object (aodf);
 
2136
    } /* End looping over all records. */
 
2137
 
 
2138
 leave:
 
2139
  xfree (buffer);
 
2140
  if (err)
 
2141
    release_aodflist (aodflist);
 
2142
  else
 
2143
    *result = aodflist;
 
2144
  return err;
386
2145
}
387
2146
 
388
2147
 
389
 
/* 6037 (dataobjects)
390
 
 
391
 
30 1E 30 0B 0C 06 45 46 2E  47 44 4F 04 01 0A 30   0.0...EF.GDO...0
392
 
02 0C 00 A1 0B 30 09 04 04  3F 00 2F 02 80 01 0E   .....0...?./....
393
 
30 30 30 18 0C 0F 64 69 73  70 6C 61 79 20 6D 65   000...display me
394
 
73 73 61 67 65 03 02 06 C0  04 01 0A 30 05 0C 03   ssage.......0...
395
 
42 53 53 A1 0D 30 0B 04 06  3F 00 40 16 D0 00 80   BSS..0...?.@....
396
 
01 20 30 2B 30 0C 0C 03 53  53 4F 03 02 06 C0 04   . 0+0...SSO.....
397
 
01 0A 30 0B 0C 09 53 61 66  65 47 75 61 72 64 A1   ..0...SafeGuard.
398
 
0E 30 0C 04 06 3F 00 0F FF  30 02 80 02 03 00 30   .0...?...0.....0
399
 
30 30 11 0C 08 53 47 41 53  64 61 74 61 03 02 06   00...SGASdata...
400
 
C0 04 01 0A 30 0B 0C 09 53  61 66 65 47 75 61 72   ....0...SafeGuar
401
 
64 A1 0E 30 0C 04 06 3F 00  0F FF 40 01 80 02 00   d..0...?...@....
402
 
80 30 30 30 11 0C 08 55 73  65 72 64 61 74 61 03   .000...Userdata.
403
 
02 06 40 04 01 0A 30 0B 0C  09 53 61 66 65 47 75   ..@...0...SafeGu
404
 
61 72 64 A1 0E 30 0C 04 06  3F 00 0F FF 30 01 80   ard..0...?...0..
405
 
02 01 00 30 2C 30 13 0C 0A  62 61 73 69 63 20 64   ...0,0...basic d
406
 
61 74 61 03 02 06 C0 04 01  0A 30 05 0C 03 49 44   ata.......0...ID
407
 
44 A1 0E 30 0C 04 06 3F 00  40 17 D0 01 80 02 02   D..0...?.@......
408
 
00 30 2F 30 16 0C 0D 65 78  74 65 6E 64 65 64 20   .0/0...extended 
409
 
64 61 74 61 03 02 06 C0 04  01 0A 30 05 0C 03 49   data.......0...I
410
 
44 44 A1 0E 30 0C 04 06 3F  00 40 17 D0 02 80 02   DD..0...?.@.....
411
 
08 00 30 34 30 1B 0C 12 73  70 65 63 69 61 6C 20   ..040...special 
412
 
70 72 69 76 69 6C 65 67 65  73 03 02 06 C0 04 01   privileges......
413
 
0A 30 05 0C 03 49 44 44 A1  0E 30 0C 04 06 3F 00   .0...IDD..0...?.
414
 
40 17 D0 03 80 02 04 00                            @.......        
415
 
 
416
 
   0   30: SEQUENCE {
417
 
   2   11:   SEQUENCE {
418
 
   4    6:     UTF8String 'EF.GDO'
419
 
  12    1:     OCTET STRING 0A
420
 
         :     }
421
 
  15    2:   SEQUENCE {
422
 
  17    0:     UTF8String
423
 
         :       Error: Object has zero length.
424
 
         :     }
425
 
  19   11:   [1] {
426
 
  21    9:     SEQUENCE {
427
 
  23    4:       OCTET STRING 3F 00 2F 02
428
 
  29    1:       [0] 0E
429
 
         :       }
430
 
         :     }
431
 
         :   }
432
 
 
433
 
 
434
 
 
435
 
6038 (authobjects)
436
 
 
437
 
30 2A 30 0B 0C 05 62 61 73  69 63 03 02 00 C0 30   0*0...basic....0
438
 
03 04 01 0A A1 16 30 14 03  03 00 0C 10 0A 01 01   ......0.........
439
 
02 01 06 02 01 06 02 01 08  80 01 01 30 51 30 19   ............0Q0.
440
 
0C 13 73 70 65 63 69 66 69  63 20 50 49 4E 20 66   ..specific PIN f
441
 
6F 72 20 44 53 03 02 00 C0  30 03 04 01 07 A1 2F   or DS....0...../
442
 
30 2D 03 03 00 4C 10 0A 01  01 02 01 06 02 01 06   0-...L..........
443
 
02 01 08 80 01 02 18 0F 32  30 30 32 30 34 31 39   ........20020419
444
 
31 32 31 33 34 31 5A 30 06  04 04 3F 00 40 16      121341Z0...?.@. 
445
 
 
446
 
   0   42: SEQUENCE {
447
 
   2   11:   SEQUENCE {
448
 
   4    5:     UTF8String 'basic'
449
 
  11    2:     BIT STRING
450
 
         :       '00000011'B
451
 
         :       Error: Spurious zero bits in bitstring.
452
 
         :     }
453
 
  15    3:   SEQUENCE {
454
 
  17    1:     OCTET STRING 0A
455
 
         :     }
456
 
  20   22:   [1] {
457
 
  22   20:     SEQUENCE {
458
 
  24    3:       BIT STRING
459
 
         :         '0000100000110000'B
460
 
         :         Error: Spurious zero bits in bitstring.
461
 
  29    1:       ENUMERATED 1
462
 
  32    1:       INTEGER 6
463
 
  35    1:       INTEGER 6
464
 
  38    1:       INTEGER 8
465
 
  41    1:       [0] 01
466
 
         :       }
467
 
         :     }
468
 
         :   }
469
 
 
470
 
 
471
 
 
472
 
*/
 
2148
 
473
2149
 
474
2150
 
475
2151
/* Read and parse the EF(TokenInfo). 
524
2200
static gpg_error_t
525
2201
read_ef_tokeninfo (app_t app)
526
2202
{
527
 
  unsigned short efid = 0x5032;
528
 
 
 
2203
  gpg_error_t err;
 
2204
  unsigned char *buffer = NULL;
 
2205
  size_t buflen;
 
2206
  const unsigned char *p;
 
2207
  size_t n, objlen, hdrlen;
 
2208
  int class, tag, constructed, ndef;
 
2209
  unsigned long ul;
 
2210
  
 
2211
  err = select_and_read_binary (app->slot, 0x5032, "TokenInfo",
 
2212
                                &buffer, &buflen);
 
2213
  if (err)
 
2214
    return err;
 
2215
  
 
2216
  p = buffer;
 
2217
  n = buflen;
 
2218
 
 
2219
  err = parse_ber_header (&p, &n, &class, &tag, &constructed,
 
2220
                          &ndef, &objlen, &hdrlen);
 
2221
  if (!err && (objlen > n || tag != TAG_SEQUENCE))
 
2222
    err = gpg_error (GPG_ERR_INV_OBJ);
 
2223
  if (err)
 
2224
    {
 
2225
      log_error ("error parsing TokenInfo: %s\n", gpg_strerror (err));
 
2226
      goto leave;
 
2227
    }
 
2228
 
 
2229
  n = objlen;
 
2230
 
 
2231
  /* Version.  */
 
2232
  err = parse_ber_header (&p, &n, &class, &tag, &constructed,
 
2233
                          &ndef, &objlen, &hdrlen);
 
2234
  if (!err && (objlen > n || tag != TAG_INTEGER))
 
2235
    err = gpg_error (GPG_ERR_INV_OBJ);
 
2236
  if (err)
 
2237
    goto leave;
 
2238
 
 
2239
  for (ul=0; objlen; objlen--)
 
2240
    {
 
2241
      ul <<= 8;
 
2242
      ul |= (*p++) & 0xff; 
 
2243
      n--;
 
2244
    }
 
2245
  if (ul)
 
2246
    {
 
2247
      log_error ("invalid version %lu in TokenInfo\n", ul);
 
2248
      err = gpg_error (GPG_ERR_INV_OBJ);
 
2249
      goto leave;
 
2250
    }
 
2251
 
 
2252
  /* serialNumber.  */
 
2253
  err = parse_ber_header (&p, &n, &class, &tag, &constructed,
 
2254
                          &ndef, &objlen, &hdrlen);
 
2255
  if (!err && (objlen > n || tag != TAG_OCTET_STRING || !objlen))
 
2256
    err = gpg_error (GPG_ERR_INV_OBJ);
 
2257
  if (err)
 
2258
    goto leave;
 
2259
  
 
2260
  xfree (app->app_local->serialno);
 
2261
  app->app_local->serialno = xtrymalloc (objlen);
 
2262
  if (!app->app_local->serialno)
 
2263
    {
 
2264
      err = gpg_error_from_errno (errno);
 
2265
      goto leave;
 
2266
    }
 
2267
  memcpy (app->app_local->serialno, p, objlen);
 
2268
  app->app_local->serialnolen = objlen;
 
2269
  log_printhex ("Serialnumber from EF(TokenInfo) is:", p, objlen);
 
2270
 
 
2271
 leave:
 
2272
  xfree (buffer);
 
2273
  return err;
529
2274
}
530
2275
 
531
2276
 
532
2277
/* Get all the basic information from the pkcs#15 card, check the
533
 
   structure and init our context.  This is used once at application
534
 
   initialization. */
 
2278
   structure and initialize our local context.  This is used once at
 
2279
   application initialization. */
535
2280
static gpg_error_t
536
2281
read_p15_info (app_t app)
537
2282
{
538
2283
  gpg_error_t err;
539
2284
 
540
 
  err = read_ed_odf (app);
541
 
  if (err)
542
 
    return err;
543
 
 
544
 
}
545
 
 
546
 
 
547
 
static int
548
 
do_learn_status (APP app, CTRL ctrl)
549
 
{
550
 
  gpg_error_t err;
551
 
  char ct_buf[100], id_buf[100];
552
 
  int i;
553
 
 
554
 
  /* Output information about all useful objects. */
555
 
  for (i=0; objlist[i].fid; i++)
556
 
    {
557
 
      if (filelist[i].certtype)
558
 
        {
559
 
          size_t len;
560
 
 
561
 
          len = app_help_read_length_of_cert (app->slot,
562
 
                                              filelist[i].fid, NULL);
563
 
          if (len)
564
 
            {
565
 
              /* FIXME: We should store the length in the application's
566
 
                 context so that a following readcert does only need to
567
 
                 read that many bytes. */
568
 
              sprintf (ct_buf, "%d", filelist[i].certtype);
569
 
              sprintf (id_buf, "P15-DF01.%04X", filelist[i].fid);
570
 
              send_status_info (ctrl, "CERTINFO",
571
 
                                ct_buf, strlen (ct_buf), 
572
 
                                id_buf, strlen (id_buf), 
573
 
                                NULL, (size_t)0);
574
 
            }
575
 
        }
576
 
      else if (filelist[i].iskeypair)
577
 
        {
578
 
          char gripstr[40+1];
579
 
 
580
 
          err = keygripstr_from_pk_file (app->slot, filelist[i].fid, gripstr);
581
 
          if (err)
582
 
            log_error ("can't get keygrip from FID 0x%04X: %s\n",
583
 
                       filelist[i].fid, gpg_strerror (err));
584
 
          else
585
 
            {
586
 
              sprintf (id_buf, "P15-DF01.%04X", filelist[i].fid);
587
 
              send_status_info (ctrl, "KEYPAIRINFO",
588
 
                                gripstr, 40, 
589
 
                                id_buf, strlen (id_buf), 
590
 
                                NULL, (size_t)0);
591
 
            }
592
 
        }
593
 
    }
594
 
 
595
 
  return 0;
596
 
}
597
 
 
598
 
 
599
 
 
600
 
 
601
 
/* Release all resources.  */
602
 
static void
603
 
do_deinit (app_t app)
604
 
{
605
 
  if (app && app->app_local)
606
 
    {
607
 
      xfree (app->app_local);
608
 
      app->app_local = NULL;
609
 
    }
610
 
}
611
 
 
612
 
 
613
 
/* Select the PKCS#15 application on the card in SLOT.  */
614
 
int
615
 
app_select_p15 (APP app)
616
 
{
617
 
  static char const aid[] = { 0xA0, 0, 0, 0, 0x63,
618
 
                              0x50, 0x4B, 0x43, 0x53, 0x2D, 0x31, 0x35 };
 
2285
  if (!read_ef_tokeninfo (app))
 
2286
    {
 
2287
      /* If we don't have a serial number yet but the TokenInfo provides
 
2288
         one, use that. */
 
2289
      if (!app->serialno && app->app_local->serialno)
 
2290
        {
 
2291
          app->serialno = app->app_local->serialno;
 
2292
          app->serialnolen = app->app_local->serialnolen;
 
2293
          app->app_local->serialno = NULL;
 
2294
          app->app_local->serialnolen = 0;
 
2295
          err = app_munge_serialno (app);
 
2296
          if (err)
 
2297
            return err;
 
2298
        }
 
2299
    }
 
2300
  
 
2301
  /* Read the ODF so that we know the location of all directory
 
2302
     files. */
 
2303
  /* Fixme: We might need to get a non-standard ODF FID from TokenInfo. */
 
2304
  err = read_ef_odf (app, 0x5031);
 
2305
  if (err)
 
2306
    return err;
 
2307
 
 
2308
  /* Read certificate information. */
 
2309
  assert (!app->app_local->certificate_info);
 
2310
  assert (!app->app_local->trusted_certificate_info);
 
2311
  assert (!app->app_local->useful_certificate_info);
 
2312
  err = read_ef_cdf (app, app->app_local->odf.certificates, 
 
2313
                     &app->app_local->certificate_info);
 
2314
  if (!err || gpg_err_code (err) == GPG_ERR_NO_DATA)
 
2315
    err = read_ef_cdf (app, app->app_local->odf.trusted_certificates, 
 
2316
                       &app->app_local->trusted_certificate_info);
 
2317
  if (!err || gpg_err_code (err) == GPG_ERR_NO_DATA)
 
2318
    err = read_ef_cdf (app, app->app_local->odf.useful_certificates, 
 
2319
                       &app->app_local->useful_certificate_info);
 
2320
  if (gpg_err_code (err) == GPG_ERR_NO_DATA)
 
2321
    err = 0;
 
2322
  if (err)
 
2323
    return err;
 
2324
 
 
2325
  /* Read information about private keys. */
 
2326
  assert (!app->app_local->private_key_info);
 
2327
  err = read_ef_prkdf (app, app->app_local->odf.private_keys,
 
2328
                       &app->app_local->private_key_info);
 
2329
  if (gpg_err_code (err) == GPG_ERR_NO_DATA)
 
2330
    err = 0;
 
2331
  if (err)
 
2332
    return err;
 
2333
 
 
2334
  /* Read information about authentication objects. */
 
2335
  assert (!app->app_local->auth_object_info);
 
2336
  err = read_ef_aodf (app, app->app_local->odf.auth_objects,
 
2337
                      &app->app_local->auth_object_info);
 
2338
  if (gpg_err_code (err) == GPG_ERR_NO_DATA)
 
2339
    err = 0;
 
2340
 
 
2341
 
 
2342
  return err;
 
2343
}
 
2344
 
 
2345
 
 
2346
/* Helper to do_learn_status: Send information about all certificates
 
2347
   listed in CERTINFO back.  Use CERTTYPE as type of the
 
2348
   certificate. */
 
2349
static gpg_error_t
 
2350
send_certinfo (app_t app, ctrl_t ctrl, const char *certtype,
 
2351
               cdf_object_t certinfo)
 
2352
{
 
2353
  for (; certinfo; certinfo = certinfo->next)
 
2354
    {
 
2355
      char *buf, *p;
 
2356
      int i;
 
2357
 
 
2358
      buf = xtrymalloc (9 + certinfo->objidlen*2 + 1);
 
2359
      if (!buf)
 
2360
        return gpg_error_from_errno (errno);
 
2361
      p = stpcpy (buf, "P15");
 
2362
      if (app->app_local->home_df)
 
2363
        {
 
2364
          sprintf (p, "-%04hX", (app->app_local->home_df & 0xffff));
 
2365
          p += 5;
 
2366
        }
 
2367
      p = stpcpy (p, ".");
 
2368
      for (i=0; i < certinfo->objidlen; i++)
 
2369
        {
 
2370
          sprintf (p, "%02X", certinfo->objid[i]);
 
2371
          p += 2;
 
2372
        }
 
2373
 
 
2374
      send_status_info (ctrl, "CERTINFO",
 
2375
                        certtype, strlen (certtype),
 
2376
                        buf, strlen (buf), 
 
2377
                        NULL, (size_t)0);
 
2378
      xfree (buf);
 
2379
    }
 
2380
  return 0;
 
2381
}
 
2382
 
 
2383
 
 
2384
/* Get the keygrip of the private key object PRKDF.  On success the
 
2385
   keygrip gets returned in the caller provided 41 byte buffer
 
2386
   R_GRIPSTR. */
 
2387
static gpg_error_t
 
2388
keygripstr_from_prkdf (app_t app, prkdf_object_t prkdf, char *r_gripstr)
 
2389
{
 
2390
  gpg_error_t err;
 
2391
  cdf_object_t cdf;
 
2392
  unsigned char *der;
 
2393
  size_t derlen;
 
2394
  ksba_cert_t cert;
 
2395
 
 
2396
  /* FIXME: We should check whether a public key directory file and a
 
2397
     matching public key for PRKDF is available.  This should make
 
2398
     extraction of the key much easier.  My current test card doesn't
 
2399
     have one, so we can only use the fallback solution bu looking for
 
2400
     a matching certificate and extract the key from there. */
 
2401
 
 
2402
  /* Look for a matching certificate. A certificate matches if the Id
 
2403
     matches the obne of the private key info. */
 
2404
  for (cdf = app->app_local->certificate_info; cdf; cdf = cdf->next)
 
2405
    if (cdf->objidlen == prkdf->objidlen
 
2406
        && !memcmp (cdf->objid, prkdf->objid, prkdf->objidlen))
 
2407
      break;
 
2408
  if (!cdf)
 
2409
    for (cdf = app->app_local->trusted_certificate_info; cdf; cdf = cdf->next)
 
2410
      if (cdf->objidlen == prkdf->objidlen
 
2411
          && !memcmp (cdf->objid, prkdf->objid, prkdf->objidlen))
 
2412
        break;
 
2413
  if (!cdf)
 
2414
    for (cdf = app->app_local->useful_certificate_info; cdf; cdf = cdf->next)
 
2415
      if (cdf->objidlen == prkdf->objidlen
 
2416
          && !memcmp (cdf->objid, prkdf->objid, prkdf->objidlen))
 
2417
        break;
 
2418
  if (!cdf)
 
2419
    return gpg_error (GPG_ERR_NOT_FOUND);
 
2420
 
 
2421
  err = readcert_by_cdf (app, cdf, &der, &derlen);
 
2422
  if (err)
 
2423
    return err;
 
2424
 
 
2425
  err = ksba_cert_new (&cert);
 
2426
  if (!err)
 
2427
    err = ksba_cert_init_from_mem (cert, der, derlen);
 
2428
  xfree (der);
 
2429
  if (!err)
 
2430
    err = app_help_get_keygrip_string (cert, r_gripstr);
 
2431
  ksba_cert_release (cert);
 
2432
 
 
2433
  return err;
 
2434
}
 
2435
 
 
2436
 
 
2437
 
 
2438
 
 
2439
/* Helper to do_learn_status: Send information about all known
 
2440
   keypairs back.  FIXME: much code duplication from
 
2441
   send_sertinfo(). */
 
2442
static gpg_error_t
 
2443
send_keypairinfo (app_t app, ctrl_t ctrl, prkdf_object_t keyinfo)
 
2444
{
 
2445
  gpg_error_t err;
 
2446
 
 
2447
  for (; keyinfo; keyinfo = keyinfo->next)
 
2448
    {
 
2449
      char gripstr[40+1];
 
2450
      char *buf, *p;
 
2451
      int i, j;
 
2452
 
 
2453
      buf = xtrymalloc (9 + keyinfo->objidlen*2 + 1);
 
2454
      if (!buf)
 
2455
        return gpg_error_from_errno (errno);
 
2456
      p = stpcpy (buf, "P15");
 
2457
      if (app->app_local->home_df)
 
2458
        {
 
2459
          sprintf (p, "-%04hX", (app->app_local->home_df & 0xffff));
 
2460
          p += 5;
 
2461
        }
 
2462
      p = stpcpy (p, ".");
 
2463
      for (i=0; i < keyinfo->objidlen; i++)
 
2464
        {
 
2465
          sprintf (p, "%02X", keyinfo->objid[i]);
 
2466
          p += 2;
 
2467
        }
 
2468
 
 
2469
      err = keygripstr_from_prkdf (app, keyinfo, gripstr);
 
2470
      if (err)
 
2471
        {
 
2472
          log_error ("can't get keygrip from ");
 
2473
          for (j=0; j < keyinfo->pathlen; j++)
 
2474
            log_printf ("%04hX", keyinfo->path[j]);
 
2475
          log_printf (": %s\n", gpg_strerror (err));
 
2476
        }
 
2477
      else
 
2478
        {
 
2479
          assert (strlen (gripstr) == 40);
 
2480
          send_status_info (ctrl, "KEYPAIRINFO",
 
2481
                            gripstr, 40,
 
2482
                            buf, strlen (buf), 
 
2483
                            NULL, (size_t)0);
 
2484
        }
 
2485
      xfree (buf);
 
2486
    }
 
2487
  return 0;
 
2488
}
 
2489
 
 
2490
 
 
2491
 
 
2492
/* This is the handler for the LEARN command.  */
 
2493
static gpg_error_t 
 
2494
do_learn_status (app_t app, ctrl_t ctrl)
 
2495
{
 
2496
  gpg_error_t err;
 
2497
 
 
2498
  err = send_certinfo (app, ctrl, "100", app->app_local->certificate_info);
 
2499
  if (!err)
 
2500
    err = send_certinfo (app, ctrl, "101",
 
2501
                         app->app_local->trusted_certificate_info);
 
2502
  if (!err)
 
2503
    err = send_certinfo (app, ctrl, "102",
 
2504
                         app->app_local->useful_certificate_info);
 
2505
  if (!err)
 
2506
    err = send_keypairinfo (app, ctrl, app->app_local->private_key_info);
 
2507
 
 
2508
  return err;
 
2509
}
 
2510
 
 
2511
 
 
2512
/* Read a certifciate using the information in CDF and return the
 
2513
   certificate in a newly llocated buffer R_CERT and its length
 
2514
   R_CERTLEN. */
 
2515
static gpg_error_t
 
2516
readcert_by_cdf (app_t app, cdf_object_t cdf,
 
2517
                 unsigned char **r_cert, size_t *r_certlen)
 
2518
{
 
2519
  gpg_error_t err;
 
2520
  unsigned char *buffer = NULL;
 
2521
  const unsigned char *p, *save_p;
 
2522
  size_t buflen, n;
 
2523
  int class, tag, constructed, ndef;
 
2524
  size_t totobjlen, objlen, hdrlen;
 
2525
  int rootca;
 
2526
  int i;
 
2527
 
 
2528
  *r_cert = NULL;
 
2529
  *r_certlen = 0;
 
2530
 
 
2531
  /* First check whether it has been cached. */
 
2532
  if (cdf->image)
 
2533
    {
 
2534
      *r_cert = xtrymalloc (cdf->imagelen);
 
2535
      if (!*r_cert)
 
2536
        return gpg_error_from_errno (errno);
 
2537
      memcpy (*r_cert, cdf->image, cdf->imagelen);
 
2538
      *r_certlen = cdf->imagelen;
 
2539
      return 0;
 
2540
    }
 
2541
 
 
2542
  /* Read the entire file.  fixme: This could be optimized by first
 
2543
     reading the header to figure out how long the certificate
 
2544
     actually is. */
 
2545
  err = select_ef_by_path (app, cdf->path, cdf->pathlen);
 
2546
  if (err)
 
2547
    goto leave;
 
2548
 
 
2549
  err = iso7816_read_binary (app->slot, cdf->off, cdf->len, &buffer, &buflen);
 
2550
  if (!err && (!buflen || *buffer == 0xff))
 
2551
    err = gpg_error (GPG_ERR_NOT_FOUND);
 
2552
  if (err)
 
2553
    {
 
2554
      log_error ("error reading certificate with Id ");
 
2555
      for (i=0; i < cdf->objidlen; i++)
 
2556
        log_printf ("%02X", cdf->objid[i]);
 
2557
      log_printf (": %s\n", gpg_strerror (err));
 
2558
      goto leave;
 
2559
    }
 
2560
  
 
2561
  /* Check whether this is really a certificate.  */
 
2562
  p = buffer;
 
2563
  n = buflen;
 
2564
  err = parse_ber_header (&p, &n, &class, &tag, &constructed,
 
2565
                          &ndef, &objlen, &hdrlen);
 
2566
  if (err)
 
2567
    goto leave;
 
2568
 
 
2569
  if (class == CLASS_UNIVERSAL && tag == TAG_SEQUENCE && constructed)
 
2570
    rootca = 0;
 
2571
  else if ( class == CLASS_UNIVERSAL && tag == TAG_SET && constructed )
 
2572
    rootca = 1;
 
2573
  else
 
2574
    {
 
2575
      err = gpg_error (GPG_ERR_INV_OBJ);
 
2576
      goto leave;
 
2577
    }
 
2578
  totobjlen = objlen + hdrlen;
 
2579
  assert (totobjlen <= buflen);
 
2580
 
 
2581
  err = parse_ber_header (&p, &n, &class, &tag, &constructed,
 
2582
                          &ndef, &objlen, &hdrlen);
 
2583
  if (err)
 
2584
    goto leave;
 
2585
  
 
2586
  if (!rootca
 
2587
      && class == CLASS_UNIVERSAL && tag == TAG_OBJECT_ID && !constructed)
 
2588
    {
 
2589
      /* The certificate seems to be contained in a userCertificate
 
2590
         container.  Skip this and assume the following sequence is
 
2591
         the certificate. */
 
2592
      if (n < objlen)
 
2593
        {
 
2594
          err = gpg_error (GPG_ERR_INV_OBJ);
 
2595
          goto leave;
 
2596
        }
 
2597
      p += objlen;
 
2598
      n -= objlen;
 
2599
      save_p = p;
 
2600
      err = parse_ber_header (&p, &n, &class, &tag, &constructed,
 
2601
                              &ndef, &objlen, &hdrlen);
 
2602
      if (err) 
 
2603
        goto leave;
 
2604
      if ( !(class == CLASS_UNIVERSAL && tag == TAG_SEQUENCE && constructed) )
 
2605
        {
 
2606
          err = gpg_error (GPG_ERR_INV_OBJ);
 
2607
          goto leave;
 
2608
        }
 
2609
      totobjlen = objlen + hdrlen;
 
2610
      assert (save_p + totobjlen <= buffer + buflen);
 
2611
      memmove (buffer, save_p, totobjlen);
 
2612
    }
 
2613
 
 
2614
  *r_cert = buffer;
 
2615
  buffer = NULL;
 
2616
  *r_certlen = totobjlen;
 
2617
 
 
2618
  /* Try to cache it. */
 
2619
  if (!cdf->image && (cdf->image = xtrymalloc (*r_certlen)))
 
2620
    {
 
2621
      memcpy (cdf->image, *r_cert, *r_certlen);
 
2622
      cdf->imagelen = *r_certlen;
 
2623
    }
 
2624
 
 
2625
 
 
2626
 leave:
 
2627
  xfree (buffer);
 
2628
  return err;
 
2629
}
 
2630
 
 
2631
 
 
2632
/* Handler for the READCERT command.
 
2633
 
 
2634
   Read the certificate with id CERTID (as returned by learn_status in
 
2635
   the CERTINFO status lines) and return it in the freshly allocated
 
2636
   buffer to be stored at R_CERT and its length at R_CERTLEN.  A error
 
2637
   code will be returned on failure and R_CERT and R_CERTLEN will be
 
2638
   set to NULL/0. */
 
2639
static gpg_error_t 
 
2640
do_readcert (app_t app, const char *certid,
 
2641
             unsigned char **r_cert, size_t *r_certlen)
 
2642
{
 
2643
  gpg_error_t err;
 
2644
  cdf_object_t cdf;
 
2645
 
 
2646
  *r_cert = NULL;
 
2647
  *r_certlen = 0;
 
2648
  err = cdf_object_from_certid (app, certid, &cdf);
 
2649
  if (!err)
 
2650
    err =readcert_by_cdf (app, cdf, r_cert, r_certlen);
 
2651
  return err;
 
2652
}
 
2653
 
 
2654
 
 
2655
 
 
2656
/* Implement the GETATTR command.  This is similar to the LEARN
 
2657
   command but returns just one value via the status interface. */
 
2658
static gpg_error_t 
 
2659
do_getattr (app_t app, ctrl_t ctrl, const char *name)
 
2660
{
 
2661
  gpg_error_t err;
 
2662
  int i;
 
2663
 
 
2664
  if (!strcmp (name, "$AUTHKEYID"))
 
2665
    {
 
2666
      char *buf, *p;
 
2667
      prkdf_object_t prkdf;
 
2668
 
 
2669
      /* We return the ID of the first private keycapable of
 
2670
         signing. */
 
2671
      for (prkdf = app->app_local->private_key_info; prkdf;
 
2672
           prkdf = prkdf->next)
 
2673
        if (prkdf->usageflags.sign)
 
2674
          break;
 
2675
      if (prkdf)
 
2676
        {
 
2677
          buf = xtrymalloc (9 + prkdf->objidlen*2 + 1);
 
2678
          if (!buf)
 
2679
            return gpg_error_from_errno (errno);
 
2680
          p = stpcpy (buf, "P15");
 
2681
          if (app->app_local->home_df)
 
2682
            {
 
2683
              sprintf (p, "-%04hX", (app->app_local->home_df & 0xffff));
 
2684
              p += 5;
 
2685
            }
 
2686
          p = stpcpy (p, ".");
 
2687
          for (i=0; i < prkdf->objidlen; i++)
 
2688
            {
 
2689
              sprintf (p, "%02X", prkdf->objid[i]);
 
2690
              p += 2;
 
2691
            }
 
2692
 
 
2693
          send_status_info (ctrl, name, buf, strlen (buf), NULL, 0);
 
2694
          xfree (buf);
 
2695
          return 0;
 
2696
        }
 
2697
    }
 
2698
  else if (!strcmp (name, "$DISPSERIALNO"))
 
2699
    {
 
2700
      /* For certain cards we return special IDs.  There is no
 
2701
         general rule for it so we need to decide case by case. */
 
2702
      if (app->app_local->card_type == CARD_TYPE_BELPIC)
 
2703
        {
 
2704
          /* The eID card has a card number printed on the fron matter
 
2705
             which seems to be a good indication. */
 
2706
          unsigned char *buffer;
 
2707
          const unsigned char *p;
 
2708
          size_t buflen, n;
 
2709
          unsigned short path[] = { 0x3F00, 0xDF01, 0x4031 };
 
2710
 
 
2711
          err = select_ef_by_path (app, path, DIM(path) );
 
2712
          if (!err)
 
2713
            err = iso7816_read_binary (app->slot, 0, 0, &buffer, &buflen);
 
2714
          if (err)
 
2715
            {
 
2716
              log_error ("error accessing EF(ID): %s\n", gpg_strerror (err));
 
2717
              return err;
 
2718
            }
 
2719
 
 
2720
          p = find_tlv (buffer, buflen, 1, &n);
 
2721
          if (p && n == 12)
 
2722
            {
 
2723
              char tmp[12+2+1];
 
2724
              memcpy (tmp, p, 3);
 
2725
              tmp[3] = '-';
 
2726
              memcpy (tmp+4, p+3, 7);
 
2727
              tmp[11] = '-';
 
2728
              memcpy (tmp+12, p+10, 2);
 
2729
              tmp[14] = 0;
 
2730
              send_status_info (ctrl, name, tmp, strlen (tmp), NULL, 0);
 
2731
              xfree (buffer);
 
2732
              return 0;
 
2733
            }
 
2734
          xfree (buffer);
 
2735
        }
 
2736
 
 
2737
    }
 
2738
  return gpg_error (GPG_ERR_INV_NAME); 
 
2739
}
 
2740
 
 
2741
 
 
2742
 
 
2743
 
 
2744
/* Micardo cards require special treatment. This is a helper for the
 
2745
   crypto functions to manage the security environment.  We expect that
 
2746
   the key file has already been selected. FID is the one of the
 
2747
   selected key. */
 
2748
static gpg_error_t
 
2749
micardo_mse (app_t app, unsigned short fid)
 
2750
{
 
2751
  gpg_error_t err;
 
2752
  int recno;
 
2753
  unsigned short refdata = 0;
 
2754
  int se_num;
 
2755
  unsigned char msebuf[10];
 
2756
 
 
2757
  /* Read the KeyD file containing extra information on keys. */
 
2758
  err = iso7816_select_file (app->slot, 0x0013, 0, NULL, NULL);
 
2759
  if (err)
 
2760
    {
 
2761
      log_error ("error reading EF_keyD: %s\n", gpg_strerror (err));
 
2762
      return err;
 
2763
    }
 
2764
  
 
2765
  for (recno = 1, se_num = -1; ; recno++)
 
2766
    {
 
2767
      unsigned char *buffer;
 
2768
      size_t buflen;
 
2769
      size_t n, nn;
 
2770
      const unsigned char *p, *pp;
 
2771
      
 
2772
      err = iso7816_read_record (app->slot, recno, 1, 0, &buffer, &buflen);
 
2773
      if (gpg_err_code (err) == GPG_ERR_NOT_FOUND)
 
2774
        break; /* ready */
 
2775
      if (err)
 
2776
        {
 
2777
          log_error ("error reading EF_keyD record: %s\n",
 
2778
                     gpg_strerror (err));
 
2779
          return err;
 
2780
        }
 
2781
      log_printhex ("keyD record:", buffer, buflen);
 
2782
      p = find_tlv (buffer, buflen, 0x83, &n);
 
2783
      if (p && n == 4 && ((p[2]<<8)|p[3]) == fid)
 
2784
        {
 
2785
          refdata = ((p[0]<<8)|p[1]);
 
2786
          /* Locate the SE DO and the there included sec env number. */
 
2787
          p = find_tlv (buffer, buflen, 0x7b, &n);
 
2788
          if (p && n)
 
2789
            {
 
2790
              pp = find_tlv (p, n, 0x80, &nn);
 
2791
              if (pp && nn == 1)
 
2792
                {
 
2793
                  se_num = *pp;
 
2794
                  xfree (buffer);
 
2795
                  break; /* found. */
 
2796
                }
 
2797
            }
 
2798
        }
 
2799
      xfree (buffer);
 
2800
    }
 
2801
  if (se_num == -1)
 
2802
    {
 
2803
      log_error ("CRT for keyfile %04hX not found\n", fid);
 
2804
      return gpg_error (GPG_ERR_NOT_FOUND);
 
2805
    }
 
2806
  
 
2807
  
 
2808
  /* Restore the security environment to SE_NUM if needed */
 
2809
  if (se_num)
 
2810
    {
 
2811
      err = iso7816_manage_security_env (app->slot, 0xf3, se_num, NULL, 0);
 
2812
      if (err)
 
2813
        {
 
2814
          log_error ("restoring SE to %d failed: %s\n",
 
2815
                     se_num, gpg_strerror (err));
 
2816
          return err;
 
2817
        }
 
2818
    }
 
2819
 
 
2820
  /* Set the DST reference data. */
 
2821
  msebuf[0] = 0x83;
 
2822
  msebuf[1] = 0x03;
 
2823
  msebuf[2] = 0x80;
 
2824
  msebuf[3] = (refdata >> 8);
 
2825
  msebuf[4] = refdata;
 
2826
  err = iso7816_manage_security_env (app->slot, 0x41, 0xb6, msebuf, 5);
 
2827
  if (err)
 
2828
    {
 
2829
      log_error ("setting SE to reference file %04hX failed: %s\n",
 
2830
                 refdata, gpg_strerror (err));
 
2831
      return err;
 
2832
    }
 
2833
  return 0;
 
2834
}
 
2835
 
 
2836
 
 
2837
 
 
2838
/* Handler for the PKSIGN command. 
 
2839
 
 
2840
   Create the signature and return the allocated result in OUTDATA.
 
2841
   If a PIN is required, the PINCB will be used to ask for the PIN;
 
2842
   that callback should return the PIN in an allocated buffer and
 
2843
   store that as the 3rd argument.  */
 
2844
static gpg_error_t 
 
2845
do_sign (app_t app, const char *keyidstr, int hashalgo,
 
2846
         gpg_error_t (*pincb)(void*, const char *, char **),
 
2847
         void *pincb_arg,
 
2848
         const void *indata, size_t indatalen,
 
2849
         unsigned char **outdata, size_t *outdatalen )
 
2850
{
 
2851
  static unsigned char sha1_prefix[15] = /* Object ID is 1.3.14.3.2.26 */
 
2852
    { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
 
2853
      0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
 
2854
  static unsigned char rmd160_prefix[15] = /* Object ID is 1.3.36.3.2.1 */
 
2855
    { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x24, 0x03,
 
2856
      0x02, 0x01, 0x05, 0x00, 0x04, 0x14 };
 
2857
 
 
2858
  gpg_error_t err;
 
2859
  int i;
 
2860
  unsigned char data[35];   /* Must be large enough for a SHA-1 digest
 
2861
                               + the largest OID prefix above. */
 
2862
  prkdf_object_t prkdf;    /* The private key object. */
 
2863
  aodf_object_t aodf;      /* The associated authentication object. */
 
2864
  int no_data_padding = 0; /* True if the card want the data without padding.*/
 
2865
  int mse_done = 0;        /* Set to true if the MSE has been done. */
 
2866
 
 
2867
  if (!keyidstr || !*keyidstr)
 
2868
    return gpg_error (GPG_ERR_INV_VALUE);
 
2869
  if (indatalen != 20 && indatalen != 16 && indatalen != 35)
 
2870
    return gpg_error (GPG_ERR_INV_VALUE);
 
2871
 
 
2872
  err = prkdf_object_from_keyidstr (app, keyidstr, &prkdf);
 
2873
  if (err)
 
2874
    return err;
 
2875
  if (!(prkdf->usageflags.sign || prkdf->usageflags.sign_recover
 
2876
        ||prkdf->usageflags.non_repudiation))
 
2877
    {
 
2878
      log_error ("key %s may not be used for signing\n", keyidstr);
 
2879
      return gpg_error (GPG_ERR_WRONG_KEY_USAGE);
 
2880
    }
 
2881
 
 
2882
  if (!prkdf->authid)
 
2883
    {
 
2884
      log_error ("no authentication object defined for %s\n", keyidstr);
 
2885
      /* fixme: we might want to go ahead and do without PIN
 
2886
         verification. */
 
2887
      return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
 
2888
    }
 
2889
 
 
2890
  /* Find the authentication object to this private key object. */
 
2891
  for (aodf = app->app_local->auth_object_info; aodf; aodf = aodf->next)
 
2892
    if (aodf->objidlen == prkdf->authidlen 
 
2893
        && !memcmp (aodf->objid, prkdf->authid, prkdf->authidlen))
 
2894
      break;
 
2895
  if (!aodf)
 
2896
    {
 
2897
      log_error ("authentication object for %s missing\n", keyidstr);
 
2898
      return gpg_error (GPG_ERR_INV_CARD);
 
2899
    }
 
2900
  if (aodf->authid)
 
2901
    {
 
2902
      log_error ("PIN verification is protected by an "
 
2903
                 "additional authentication token\n");
 
2904
      return gpg_error (GPG_ERR_BAD_PIN_METHOD);
 
2905
    }
 
2906
  if (aodf->pinflags.integrity_protected
 
2907
      || aodf->pinflags.confidentiality_protected)
 
2908
    {
 
2909
      log_error ("PIN verification requires unsupported protecion method\n");
 
2910
      return gpg_error (GPG_ERR_BAD_PIN_METHOD);
 
2911
    }
 
2912
  if (!aodf->stored_length && aodf->pinflags.needs_padding)
 
2913
    {
 
2914
      log_error ("PIN verification requires padding but no length known\n");
 
2915
      return gpg_error (GPG_ERR_INV_CARD);
 
2916
    }
 
2917
 
 
2918
  /* Select the key file.  Note that this may change the security
 
2919
     environment thus we do it before PIN verification. */
 
2920
  err = select_ef_by_path (app, prkdf->path, prkdf->pathlen);
 
2921
  if (err)
 
2922
    {
 
2923
      log_error ("error selecting file for key %s: %s\n",
 
2924
                 keyidstr, gpg_strerror (errno));
 
2925
      return err;
 
2926
    }
 
2927
 
 
2928
 
 
2929
  /* Due to the fact that the non-repudiation signature on a BELPIC
 
2930
     card requires a ver verify immediately before the DSO we set the
 
2931
     MSE before we do the verification.  Other cards might allow to do
 
2932
     this also but I don't want to break anything, thus we do it only
 
2933
     for the BELPIC card here. */
 
2934
  if (app->app_local->card_type == CARD_TYPE_BELPIC)
 
2935
    {
 
2936
      unsigned char mse[5];
 
2937
      
 
2938
      mse[0] = 4;    /* Length of the template. */
 
2939
      mse[1] = 0x80; /* Algorithm reference tag. */
 
2940
      mse[2] = 0x02; /* Algorithm: RSASSA-PKCS1-v1.5 using SHA1. */
 
2941
      mse[3] = 0x84; /* Private key reference tag. */
 
2942
      mse[4] = prkdf->key_reference_valid? prkdf->key_reference : 0x82;
 
2943
 
 
2944
      err = iso7816_manage_security_env (app->slot, 
 
2945
                                         0x41, 0xB6,
 
2946
                                         mse, sizeof mse);
 
2947
      no_data_padding = 1;
 
2948
      mse_done = 1;
 
2949
    }
 
2950
  if (err)
 
2951
    {
 
2952
      log_error ("MSE failed: %s\n", gpg_strerror (err));
 
2953
      return err;
 
2954
    }
 
2955
 
 
2956
 
 
2957
  /* Now that we have all the information available, prepare and run
 
2958
     the PIN verification.*/
 
2959
  if (1)
 
2960
    {
 
2961
      char *pinvalue;
 
2962
      size_t pinvaluelen;
 
2963
      const char *errstr;
 
2964
      const char *s;
 
2965
 
 
2966
      if (prkdf->usageflags.non_repudiation
 
2967
          && app->app_local->card_type == CARD_TYPE_BELPIC)
 
2968
        err = pincb (pincb_arg, "PIN (qualified signature!)", &pinvalue);
 
2969
      else
 
2970
        err = pincb (pincb_arg, "PIN", &pinvalue);
 
2971
      if (err)
 
2972
        {
 
2973
          log_info ("PIN callback returned error: %s\n", gpg_strerror (err));
 
2974
          return err;
 
2975
        }
 
2976
 
 
2977
      /* We might need to cope with UTF8 things here.  Not sure how
 
2978
         min_length etc. are exactly defined, for now we take them as
 
2979
         a plain octet count. */
 
2980
 
 
2981
      if (strlen (pinvalue) < aodf->min_length)
 
2982
        {
 
2983
          log_error ("PIN is too short; minimum length is %lu\n",
 
2984
                     aodf->min_length);         
 
2985
          err = gpg_error (GPG_ERR_BAD_PIN);
 
2986
        }
 
2987
      else if (aodf->stored_length && strlen (pinvalue) > aodf->stored_length)
 
2988
        {
 
2989
          /* This would otherwise truncate the PIN silently. */
 
2990
          log_error ("PIN is too large; maximum length is %lu\n",
 
2991
                     aodf->stored_length);
 
2992
          err = gpg_error (GPG_ERR_BAD_PIN);
 
2993
        }
 
2994
      else if (aodf->max_length_valid && strlen (pinvalue) > aodf->max_length)
 
2995
        {
 
2996
          log_error ("PIN is too large; maximum length is %lu\n",
 
2997
                     aodf->max_length);
 
2998
          err = gpg_error (GPG_ERR_BAD_PIN);
 
2999
        }
 
3000
 
 
3001
      if (err)
 
3002
        {
 
3003
          xfree (pinvalue);
 
3004
          return err;
 
3005
        }
 
3006
 
 
3007
      errstr = NULL;
 
3008
      err = 0;
 
3009
      switch (aodf->pintype)
 
3010
        {
 
3011
        case PIN_TYPE_BCD:
 
3012
        case PIN_TYPE_ASCII_NUMERIC:
 
3013
          for (s=pinvalue; digitp (s); s++)
 
3014
            ;
 
3015
          if (*s)
 
3016
            {
 
3017
              errstr = "Non-numeric digits found in PIN";
 
3018
              err = gpg_error (GPG_ERR_BAD_PIN);
 
3019
            }
 
3020
          break;
 
3021
        case PIN_TYPE_UTF8:
 
3022
          break;
 
3023
        case PIN_TYPE_HALF_NIBBLE_BCD:
 
3024
          errstr = "PIN type Half-Nibble-BCD is not supported"; 
 
3025
          break;
 
3026
        case PIN_TYPE_ISO9564_1:
 
3027
          errstr = "PIN type ISO9564-1 is not supported";
 
3028
          break;
 
3029
        default:
 
3030
          errstr = "Unknown PIN type";
 
3031
          break;
 
3032
        }
 
3033
      if (errstr)
 
3034
        {
 
3035
          log_error ("can't verify PIN: %s\n", errstr);
 
3036
          xfree (pinvalue);
 
3037
          return err? err : gpg_error (GPG_ERR_BAD_PIN_METHOD);
 
3038
        }
 
3039
 
 
3040
 
 
3041
      if (aodf->pintype == PIN_TYPE_BCD )
 
3042
        {
 
3043
          char *paddedpin;
 
3044
          int ndigits;
 
3045
 
 
3046
          for (ndigits=0, s=pinvalue; *s; ndigits++, s++)
 
3047
            ;
 
3048
          paddedpin = xtrymalloc (aodf->stored_length+1);
 
3049
          if (!paddedpin)
 
3050
            {
 
3051
              err = gpg_error_from_errno (errno);
 
3052
              xfree (pinvalue);
 
3053
              return err;
 
3054
            }
 
3055
 
 
3056
          i = 0;
 
3057
          paddedpin[i++] = 0x20 | (ndigits & 0x0f);
 
3058
          for (s=pinvalue; i < aodf->stored_length && *s && s[1]; s = s+2 )
 
3059
            paddedpin[i++] = (((*s - '0') << 4) | ((s[1] - '0') & 0x0f));
 
3060
          if (i < aodf->stored_length && *s)
 
3061
            paddedpin[i++] = (((*s - '0') << 4) 
 
3062
                              |((aodf->pad_char_valid?aodf->pad_char:0)&0x0f));
 
3063
 
 
3064
          if (aodf->pinflags.needs_padding)
 
3065
            while (i < aodf->stored_length)
 
3066
              paddedpin[i++] = aodf->pad_char_valid? aodf->pad_char : 0;
 
3067
 
 
3068
          xfree (pinvalue);
 
3069
          pinvalue = paddedpin;
 
3070
          pinvaluelen = i;
 
3071
        }
 
3072
      else if (aodf->pinflags.needs_padding)
 
3073
        {
 
3074
          char *paddedpin;
 
3075
 
 
3076
          paddedpin = xtrymalloc (aodf->stored_length+1);
 
3077
          if (!paddedpin)
 
3078
            {
 
3079
              err = gpg_error_from_errno (errno);
 
3080
              xfree (pinvalue);
 
3081
              return err;
 
3082
            }
 
3083
          for (i=0, s=pinvalue; i < aodf->stored_length && *s; i++, s++)
 
3084
            paddedpin[i] = *s;
 
3085
          /* Not sure what padding char to use if none has been set.
 
3086
             For now we use 0x00; maybe a space would be better. */
 
3087
          for (; i < aodf->stored_length; i++)
 
3088
            paddedpin[i] = aodf->pad_char_valid? aodf->pad_char : 0;
 
3089
          paddedpin[i] = 0;
 
3090
          pinvaluelen = i;
 
3091
          xfree (pinvalue);
 
3092
          pinvalue = paddedpin;
 
3093
        }
 
3094
      else
 
3095
        pinvaluelen = strlen (pinvalue);
 
3096
 
 
3097
      err = iso7816_verify (app->slot,
 
3098
                            aodf->pin_reference_valid? aodf->pin_reference : 0,
 
3099
                            pinvalue, pinvaluelen);
 
3100
      xfree (pinvalue);
 
3101
      if (err)
 
3102
        {
 
3103
          log_error ("PIN verification failed: %s\n", gpg_strerror (err));
 
3104
          return err;
 
3105
        }
 
3106
      log_debug ("PIN verification succeeded\n");
 
3107
    }
 
3108
 
 
3109
  /* Prepare the DER object from INDATA. */
 
3110
  if (indatalen == 35)
 
3111
    {
 
3112
      /* Alright, the caller was so kind to send us an already
 
3113
         prepared DER object.  Check that it is what we want and that
 
3114
         it matches the hash algorithm. */
 
3115
      if (hashalgo == GCRY_MD_SHA1 && !memcmp (indata, sha1_prefix, 15))
 
3116
        ;
 
3117
      else if (hashalgo == GCRY_MD_RMD160
 
3118
               && !memcmp (indata, rmd160_prefix, 15))
 
3119
        ;
 
3120
      else 
 
3121
        return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM);
 
3122
      memcpy (data, indata, indatalen);
 
3123
    }
 
3124
  else
 
3125
    {
 
3126
      /* Need to prepend the prefix. */
 
3127
      if (hashalgo == GCRY_MD_SHA1)
 
3128
        memcpy (data, sha1_prefix, 15);
 
3129
      else if (hashalgo == GCRY_MD_RMD160)
 
3130
        memcpy (data, rmd160_prefix, 15);
 
3131
      else 
 
3132
        return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM);
 
3133
      memcpy (data+15, indata, indatalen);
 
3134
    }
 
3135
 
 
3136
  /* Manage security environment needs to be weaked for certain cards. */
 
3137
  if (mse_done)
 
3138
    err = 0;
 
3139
  else if (app->app_local->card_type == CARD_TYPE_TCOS)
 
3140
    {
 
3141
      /* TCOS creates signatures always using the local key 0.  MSE
 
3142
         may not be used. */
 
3143
    }
 
3144
  else if (app->app_local->card_type == CARD_TYPE_MICARDO)
 
3145
    {
 
3146
      if (!prkdf->pathlen)
 
3147
        err = gpg_error (GPG_ERR_BUG);
 
3148
      else
 
3149
        err = micardo_mse (app, prkdf->path[prkdf->pathlen-1]);
 
3150
    }
 
3151
  else if (prkdf->key_reference_valid)
 
3152
    {
 
3153
      unsigned char mse[3];
 
3154
      
 
3155
      mse[0] = 0x84; /* Select asym. key. */
 
3156
      mse[1] = 1;
 
3157
      mse[2] = prkdf->key_reference;
 
3158
 
 
3159
      err = iso7816_manage_security_env (app->slot, 
 
3160
                                         0x41, 0xB6,
 
3161
                                         mse, sizeof mse);
 
3162
    }
 
3163
  if (err)
 
3164
    {
 
3165
      log_error ("MSE failed: %s\n", gpg_strerror (err));
 
3166
      return err;
 
3167
    }
 
3168
 
 
3169
  if (no_data_padding)
 
3170
    err = iso7816_compute_ds (app->slot, data+15, 20, outdata, outdatalen);
 
3171
  else
 
3172
    err = iso7816_compute_ds (app->slot, data, 35, outdata, outdatalen);
 
3173
  return err;
 
3174
}
 
3175
 
 
3176
 
 
3177
/* Handler for the PKAUTH command. 
 
3178
 
 
3179
   This is basically the same as the PKSIGN command but we firstcheck
 
3180
   that the requested key is suitable for authentication; that is, it
 
3181
   must match the criteria used for the attribute $AUTHKEYID.  See
 
3182
   do_sign for calling conventions; there is no HASHALGO, though. */
 
3183
static gpg_error_t 
 
3184
do_auth (app_t app, const char *keyidstr, 
 
3185
         gpg_error_t (*pincb)(void*, const char *, char **),
 
3186
         void *pincb_arg,
 
3187
         const void *indata, size_t indatalen,
 
3188
         unsigned char **outdata, size_t *outdatalen )
 
3189
{
 
3190
  gpg_error_t err;
 
3191
  prkdf_object_t prkdf;
 
3192
 
 
3193
  if (!keyidstr || !*keyidstr)
 
3194
    return gpg_error (GPG_ERR_INV_VALUE);
 
3195
 
 
3196
  err = prkdf_object_from_keyidstr (app, keyidstr, &prkdf);
 
3197
  if (err)
 
3198
    return err;
 
3199
  if (!prkdf->usageflags.sign)
 
3200
    {
 
3201
      log_error ("key %s may not be used for authentication\n", keyidstr);
 
3202
      return gpg_error (GPG_ERR_WRONG_KEY_USAGE);
 
3203
    }
 
3204
  return do_sign (app, keyidstr, GCRY_MD_SHA1, pincb, pincb_arg, 
 
3205
                  indata, indatalen, outdata, outdatalen);
 
3206
}
 
3207
 
 
3208
 
 
3209
 
 
3210
/* Assume that EF(DIR) has been selected.  Read its content and figure
 
3211
   out the home EF of pkcs#15.  Return that home DF or 0 if not found
 
3212
   and the value at the address of BELPIC indicates whether it was
 
3213
   found by the belpic aid. */
 
3214
static unsigned short
 
3215
read_home_df (int slot, int *r_belpic)
 
3216
{
 
3217
  gpg_error_t err;
 
3218
  unsigned char *buffer;
 
3219
  const unsigned char *p, *pp;
 
3220
  size_t buflen, n, nn;
 
3221
  unsigned short result = 0;
 
3222
 
 
3223
  *r_belpic = 0;
 
3224
 
 
3225
  err = iso7816_read_binary (slot, 0, 0, &buffer, &buflen);
 
3226
  if (err)
 
3227
    {
 
3228
      log_error ("error reading EF{DIR}: %s\n", gpg_strerror (err));
 
3229
      return 0;
 
3230
    }
 
3231
 
 
3232
  /* FIXME: We need to scan all records. */
 
3233
  p = find_tlv (buffer, buflen, 0x61, &n);
 
3234
  if (p && n)
 
3235
    {
 
3236
      pp = find_tlv (p, n, 0x4f, &nn);
 
3237
      if (pp && ((nn == sizeof pkcs15_aid && !memcmp (pp, pkcs15_aid, nn))
 
3238
                 || (*r_belpic = (nn == sizeof pkcs15be_aid
 
3239
                                  && !memcmp (pp, pkcs15be_aid, nn)))))
 
3240
        {
 
3241
          pp = find_tlv (p, n, 0x50, &nn);
 
3242
          if (pp) /* fixme: Filter log value? */
 
3243
            log_info ("pkcs#15 application label from EF(DIR) is `%.*s'\n",
 
3244
                      (int)nn, pp);
 
3245
          pp = find_tlv (p, n, 0x51, &nn);
 
3246
          if (pp && nn == 4 && *pp == 0x3f && !pp[1])
 
3247
            {
 
3248
              result = ((pp[2] << 8) | pp[3]);
 
3249
              log_info ("pkcs#15 application directory is 0x%04hX\n", result);
 
3250
            }
 
3251
        }
 
3252
    }
 
3253
  xfree (buffer);
 
3254
  return result;
 
3255
}
 
3256
 
 
3257
 
 
3258
/* 
 
3259
   Select the PKCS#15 application on the card in SLOT. 
 
3260
 */
 
3261
gpg_error_t
 
3262
app_select_p15 (app_t app)
 
3263
{
619
3264
  int slot = app->slot;
620
3265
  int rc;
621
 
  
622
 
  rc = iso7816_select_application (slot, aid, sizeof aid);
 
3266
  unsigned short def_home_df = 0;
 
3267
  card_type_t card_type = CARD_TYPE_UNKNOWN;
 
3268
  int direct = 0;
 
3269
  int is_belpic = 0;
 
3270
 
 
3271
  rc = iso7816_select_application (slot, pkcs15_aid, sizeof pkcs15_aid);
 
3272
  if (rc)
 
3273
    {
 
3274
      rc = iso7816_select_application (slot, pkcs15be_aid,sizeof pkcs15be_aid);
 
3275
      if (!rc)
 
3276
        is_belpic = 1;
 
3277
    }
 
3278
  if (rc)
 
3279
    { /* Not found: Try to locate it from 2F00.  We use direct path
 
3280
         selection here because it seems that the Belgian eID card
 
3281
         does only allow for that.  Many other cards supports this
 
3282
         selection method too. */
 
3283
      unsigned short path[1] = { 0x2f00 };
 
3284
 
 
3285
      rc = iso7816_select_path (app->slot, path, 1, NULL, NULL);
 
3286
      if (!rc)
 
3287
        {
 
3288
          direct = 1;
 
3289
          def_home_df = read_home_df (slot, &is_belpic);
 
3290
          if (def_home_df)
 
3291
            {
 
3292
              path[0] = def_home_df;
 
3293
              rc = iso7816_select_path (app->slot, path, 1, NULL, NULL);
 
3294
            }
 
3295
        }
 
3296
    }
 
3297
  if (rc)
 
3298
    { /* Still not found:  Try the default DF. */
 
3299
      def_home_df = 0x5015;
 
3300
      rc = iso7816_select_file (slot, def_home_df, 1, NULL, NULL);
 
3301
    }
 
3302
  if (!rc)
 
3303
    {
 
3304
      /* Determine the type of the card.  The general case is to look
 
3305
         it up from the ATR table.  For the Belgian eID card we know
 
3306
         it instantly from the AID. */
 
3307
      if (is_belpic)
 
3308
        {
 
3309
          card_type = CARD_TYPE_BELPIC;
 
3310
        }
 
3311
      else
 
3312
        {
 
3313
          unsigned char *atr;
 
3314
          size_t atrlen;
 
3315
          int i;
 
3316
 
 
3317
          atr = apdu_get_atr (app->slot, &atrlen);
 
3318
          if (!atr)
 
3319
            rc = gpg_error (GPG_ERR_INV_CARD);
 
3320
          else
 
3321
            {
 
3322
              for (i=0; card_atr_list[i].atrlen; i++)
 
3323
                if (card_atr_list[i].atrlen == atrlen
 
3324
                    && !memcmp (card_atr_list[i].atr, atr, atrlen))
 
3325
                  {
 
3326
                    card_type = card_atr_list[i].type;
 
3327
                    break;
 
3328
                  }
 
3329
              xfree (atr);
 
3330
            }
 
3331
        }
 
3332
    }
623
3333
  if (!rc)
624
3334
    {
625
3335
      app->apptype = "P15";
631
3341
          goto leave;
632
3342
        }
633
3343
 
634
 
      /* Read basic information and check whether this is a real
 
3344
      /* Set the home DF.  Note that we currently can't do that if the
 
3345
         selection via application ID worked.  This will store 0 there
 
3346
         instead.  FIXME: We either need to figure the home_df via the
 
3347
         DIR file or using the return values from the select file
 
3348
         APDU. */
 
3349
      app->app_local->home_df = def_home_df;
 
3350
 
 
3351
      /* Store the card type.  FIXME: We might want to put this into
 
3352
         the common APP structure. */
 
3353
      app->app_local->card_type = card_type;
 
3354
 
 
3355
      /* Store whether we may and should use direct path selection. */
 
3356
      app->app_local->direct_path_selection = direct;
 
3357
 
 
3358
      /* Read basic information and thus check whether this is a real
635
3359
         card.  */
636
3360
      rc = read_p15_info (app);
637
 
      
638
 
      /* Special serial number munging.  We need to do one case here
639
 
         because we need to access the EF(TokenInfo).  */
 
3361
      if (rc)
 
3362
        goto leave;
 
3363
 
 
3364
      /* Special serial number munging.  We need to check for a German
 
3365
         prototype card right here because we need to access to
 
3366
         EF(TokenInfo).  We mark such a serial number by the using a
 
3367
         prefix of FF0100. */
640
3368
      if (app->serialnolen == 12
641
 
          && !memcmp (app->serial, "\xD2\x76\0\0\0\0\0\0\0\0\0\0", 12))
 
3369
          && !memcmp (app->serialno, "\xD2\x76\0\0\0\0\0\0\0\0\0\0", 12))
642
3370
        {
643
3371
          /* This is a German card with a silly serial number.  Try to get
644
 
             the serial number from the EF(TokenInfo). We indicate such a
645
 
             serial number by the using the prefix: "FF0100". */
646
 
          const char *efser = card->p15card->serial_number;
647
 
          char *p;
648
 
          
649
 
          if (!efser)
650
 
            efser = "";
651
 
          
652
 
          xfree (*serial);
653
 
          *serial = NULL;
654
 
          p = xtrymalloc (strlen (efser) + 7);
 
3372
             the serial number from the EF(TokenInfo). . */
 
3373
          unsigned char *p;
 
3374
 
 
3375
          /* FIXME: actually get it from EF(TokenInfo). */
 
3376
          
 
3377
          p = xtrymalloc (3 + app->serialnolen);
655
3378
          if (!p)
656
3379
            rc = gpg_error (gpg_err_code_from_errno (errno));
657
3380
          else
658
3381
            {
659
 
              strcpy (p, "FF0100");
660
 
              strcpy (p+6, efser);
661
 
              *serial = p;
 
3382
              memcpy (p, "\xff\x01", 3);
 
3383
              memcpy (p+3, app->serialno, app->serialnolen);
 
3384
              app->serialnolen += 3;
 
3385
              xfree (app->serialno);
 
3386
              app->serialno = p;
662
3387
            }
663
3388
        }
664
 
      else
665
 
        rc = app_munge_serialno (app);
666
3389
 
667
3390
      app->fnc.deinit = do_deinit;
668
3391
      app->fnc.learn_status = do_learn_status;
669
3392
      app->fnc.readcert = do_readcert;
670
 
      app->fnc.getattr = NULL;
 
3393
      app->fnc.getattr = do_getattr;
671
3394
      app->fnc.setattr = NULL;
672
3395
      app->fnc.genkey = NULL;
673
3396
      app->fnc.sign = do_sign;
674
 
      app->fnc.auth = NULL;
675
 
      app->fnc.decipher = do_decipher;
 
3397
      app->fnc.auth = do_auth;
 
3398
      app->fnc.decipher = NULL;
676
3399
      app->fnc.change_pin = NULL;
677
3400
      app->fnc.check_pin = NULL;
678
3401
 
679
3402
    leave:
680
3403
      if (rc)
681
 
        {
682
 
          xfree (app->app_local);
683
 
          app->app_local = NULL;
684
 
        }
685
 
      
 
3404
        do_deinit (app);
686
3405
   }
687
3406
 
688
3407
  return rc;
689
3408
}
690
 
 
691