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