/[winpt]/trunk/Src/wptKeyCache.cpp
ViewVC logotype

Diff of /trunk/Src/wptKeyCache.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 121 by twoaday, Mon Dec 12 11:19:56 2005 UTC revision 271 by twoaday, Sun Nov 5 08:57:45 2006 UTC
# Line 1  Line 1 
1  /* wptKeyCache.cpp- Caching for the pub- and the secring  /* wptKeyCache.cpp- Caching for the pub- and the secring
2   *      Copyright (C) 2001-2005 Timo Schulz   *      Copyright (C) 2001-2006 Timo Schulz
3   *   *
4   * This file is part of MyGPGME.   * This file is part of WinPT.
5   *   *
6   * MyGPGME is free software; you can redistribute it and/or modify   * WinPT is free software; you can redistribute it and/or modify
7   * it under the terms of the GNU General Public License as published by   * it under the terms of the GNU General Public License as published by
8   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
9   * (at your option) any later version.   * (at your option) any later version.
10   *   *
11   * MyGPGME is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.   * GNU General Public License for more details.
# Line 25  Line 25 
25  #include <windows.h>  #include <windows.h>
26  #include <stdio.h>  #include <stdio.h>
27  #include <string.h>  #include <string.h>
 #include <malloc.h>  
28  #include <ctype.h>  #include <ctype.h>
29  #include <assert.h>  #include <assert.h>
30  #include <gpgme.h>  #include <gpgme.h>
# Line 35  Line 34 
34  #include "wptNLS.h"  #include "wptNLS.h"
35  #include "wptErrors.h"  #include "wptErrors.h"
36  #include "wptW32API.h"  #include "wptW32API.h"
37    #include "wptGPG.h"
38    #include "wptTypes.h"
39    #include "wptCommonCtl.h"
40    #include "wptContext.h"
41    #include "wptKeyEdit.h"
42    #include "wptUTF8.h"
43    
44    
45    gpgme_error_t parse_keyserver_url (char **r_keyserver, unsigned short *r_port);
46    
47    /* Attribute list which holds the image data. */
48    struct attr_list_s {
49        struct attr_list_s *next;
50        char *fpr;                   /* fingerprint of the key */
51        unsigned char *d;            /* actual JPEG data. */
52        unsigned long octets;        /* length of the data. */
53        unsigned int flags;          /* status of the attribute. */
54    };
55    typedef struct attr_list_s *attr_list_t;
56    
57    
58    /* XXX: convert it to an inline function and place it in a header file. */
59    static unsigned char*
60    safe_uchar_alloc (size_t n)
61    {
62        unsigned char *p = new unsigned char[n];
63        if (!p)
64            BUG (0);
65        return p;
66    }
67    
68    
69  #if 0  /* Free attribute list @ctx. */
70  /* convert a binary buffer into its hex representation. */  void
71  static void  free_attr_list (attr_list_t ctx)
 buffer_to_string (char *dst, size_t dlen, const byte *buf, size_t nbytes)  
72  {  {
73      char dig[3];      attr_list_t n;
     size_t i;  
74    
75      memset (dst, 0, dlen);      while (ctx) {
76      for (i = 0; i < nbytes && dlen > 0; i++) {          n = ctx->next;
77          sprintf (dig, "%02X", buf[i]);          free_if_alloc (ctx->fpr);
78          strcat (dst, dig);          free_if_alloc (ctx->d);
79          dlen -= 2;          free_if_alloc (ctx);
80            ctx = n;
81      }      }
82  }  }
83  #endif          
84    /* Parse the attribute list in @fp and store it into @ctx.
85       Return value: number of parsed items. */
86    int
87    parse_attr_list (FILE *fp, const BYTE *data, DWORD datlen, attr_list_t *ctx)
88    {
89        attr_list_t c, t;
90        char buf[512], *p, *buffer;
91        int pos, n=0;
92        
93        *ctx = NULL;
94        while (fgets (buf, 511, fp)) {
95            if (strstr (buf, "\r\n"))
96                buf[strlen (buf)-2]=0;
97            if (strstr (buf, "\n"))
98                buf[strlen (buf)-1]=0;
99            if (strlen (buf) < 2 || !strstr (buf, "ATTRIBUTE"))
100                continue;
101            buffer = buf+9+10;
102            pos = 0;
103            c = new attr_list_s;
104            if (!c)
105                BUG (0);
106            memset (c, 0, sizeof *c);
107    
108            p = strtok (buffer, " ");
109            while (p != NULL) {
110                switch (pos) {
111                case 0:
112                    c->fpr = m_strdup (p);
113                    break;
114                    
115                case 1:
116                    c->octets = strtoul (p, NULL, 10);
117                    break;
118                    
119                case 7:
120                    c->flags = strtoul (p, NULL, 10);
121                    break;
122                    
123                default:
124                    break;
125                }
126                pos++;
127                p = strtok (NULL, " ");
128            }
129            if (!*ctx)
130                *ctx = c;
131            else {
132                for (t = *ctx; t->next; t=t->next)
133                    ;
134                t->next = c;
135            }
136            c->d = safe_uchar_alloc (c->octets);
137            memcpy (c->d, data, c->octets);
138            data += c->octets;
139            datlen -= c->octets;
140            n++;
141        }
142        /*assert (datlen == 0); */
143        return n;
144    }
145    
146    
147    static int
148    parse_attr_data (const char *keyid, attr_list_t *list)
149    {
150        gpgme_error_t err;
151        FILE *tmp;    
152        BYTE *data;
153        char *status, tmpnam[MAX_PATH+1];
154        DWORD ndata = 0;
155    
156        err = gpg_get_photoid_data (keyid, &status, &data, &ndata);
157        if (err)
158            return err;
159    
160        get_temp_name (tmpnam, MAX_PATH, NULL);
161        tmp = fopen (tmpnam, "w+b");
162        if (ndata > 0 && tmp != NULL) {
163            fwrite (status, 1, strlen (status), tmp);
164            fflush (tmp);
165            rewind (tmp);
166            ndata = parse_attr_list (tmp, data, ndata, list);
167        }
168        else
169            *list = NULL;
170        if (tmp != NULL) {
171            fclose (tmp);
172            DeleteFile (tmpnam);
173        }
174    
175        safe_free (status);
176        safe_free (data);
177        return ndata;
178    }
179    
180    
181  /* Parse the secret keyring and retrieve some additional information  /* Parse the secret keyring and retrieve some additional information
# Line 60  buffer_to_string (char *dst, size_t dlen Line 183  buffer_to_string (char *dst, size_t dlen
183  static void  static void
184  parse_secring (gpg_keycache_t cache, const char *kid, const char *secring)  parse_secring (gpg_keycache_t cache, const char *kid, const char *secring)
185  {      {    
186      PACKET *pkt = (PACKET*)calloc (1, sizeof *pkt);      PACKET *pkt;
187      PKT_secret_key *sk;      PKT_secret_key *sk;
188      gpg_iobuf_t inp;      gpg_iobuf_t inp;
189      gpgme_error_t err;      gpgme_error_t err;
# Line 69  parse_secring (gpg_keycache_t cache, con Line 192  parse_secring (gpg_keycache_t cache, con
192      char keyid[16+1];      char keyid[16+1];
193    
194      inp = gpg_iobuf_open (secring);      inp = gpg_iobuf_open (secring);
195      if (!inp) {      if (!inp)
         safe_free (pkt);  
196          return;          return;
197      }  
198      gpg_iobuf_ioctl (inp, 3, 1, NULL);      gpg_iobuf_ioctl (inp, 3, 1, NULL);
199        pkt = (PACKET*)calloc (1, sizeof *pkt);
200        if (!pkt)
201            BUG (0);
202      gpg_init_packet (pkt);      gpg_init_packet (pkt);
203      while (gpg_parse_packet (inp, pkt) != -1) {      while (gpg_parse_packet (inp, pkt) != -1) {
204          if (pkt->pkttype == PKT_SECRET_KEY) {          if (pkt->pkttype == PKT_SECRET_KEY) {
# Line 88  parse_secring (gpg_keycache_t cache, con Line 213  parse_secring (gpg_keycache_t cache, con
213                  goto next;                  goto next;
214              c->gloflags.is_protected = sk->is_protected;              c->gloflags.is_protected = sk->is_protected;
215              c->gloflags.divert_to_card = sk->protect.s2k.mode==1002? 1 : 0;              c->gloflags.divert_to_card = sk->protect.s2k.mode==1002? 1 : 0;
216              if (c->pubpart != NULL) {                  if (c->pubpart != NULL) {
217                  c->pubpart->gloflags.is_protected = sk->is_protected;                      c->pubpart->gloflags.is_protected = sk->is_protected;
218                  c->pubpart->gloflags.divert_to_card = sk->protect.s2k.mode==1002? 1 : 0;                  c->pubpart->gloflags.divert_to_card = sk->protect.s2k.mode==1002? 1 : 0;
219              }              }
220          }          }
# Line 102  next: Line 227  next:
227  }  }
228    
229    
230    /* Update the photo image of a single key with the fingerprint
231       @fpr. The @dat struct contains the new item data. */
232    static gpgme_error_t
233    keycache_update_photo (gpg_keycache_t ctx, const char *fpr, attr_list_t dat)
234    {
235        struct keycache_s *fnd = NULL;
236        gpgme_key_t key;
237        
238        gpg_keycache_find_key2 (ctx, fpr, 0, &key, &fnd);
239        if (!fnd)
240            return gpg_error (GPG_ERR_NOT_FOUND);
241        free_if_alloc (fnd->attrib.d);
242        fnd->attrib.flags = dat->flags;
243        fnd->attrib.len = dat->octets;
244        fnd->attrib.d = safe_uchar_alloc (dat->octets);
245        memcpy (fnd->attrib.d, dat->d, dat->octets);
246        return 0;
247    }
248    
249    
250    /* Update all photo images in the cache. */
251    static gpgme_error_t
252    keycache_update_photos (gpg_keycache_t ctx)
253    {
254        attr_list_t list=NULL, n;
255        DWORD ndata;
256    
257        ndata = parse_attr_data (NULL, &list);
258        if (ndata < 1) {
259            free_attr_list (list);
260            return 0;
261        }
262    
263        for (n=list; n; n=n->next)
264            keycache_update_photo (ctx, n->fpr, n);
265        free_attr_list (list);
266        return 0;
267    }
268    
269    
270    void
271    keycache_decode_uid (struct keycache_s *ctx)
272    {
273        gpgme_user_id_t u;
274        struct native_uid_s *n, *t;
275    
276        for (u = ctx->key->uids; u; u = u->next) {
277            n = new native_uid_s;
278            if (!n)
279                BUG (0);
280            memset (n, 0, sizeof *n);
281            if (is_8bit_string (u->uid)) {
282                n->malloced = 1;
283                n->uid = utf8_to_native (u->uid);
284                if (u->name != NULL)
285                    n->name = utf8_to_native (u->name);
286                if (u->email != NULL)
287                    n->email = m_strdup (u->email);
288                if (u->comment != NULL)
289                    n->comment = utf8_to_native (u->comment);
290            }
291            else {
292                n->malloced = 0;
293                n->uid = u->uid;
294                n->name = u->name;
295                n->comment = u->comment;
296                n->email = u->email;
297            }
298            n->signatures = u->signatures;
299            n->validity = u->validity;
300            n->revoked = u->revoked;
301            if (!ctx->uids)
302                ctx->uids = n;
303            else {
304                for (t = ctx->uids; t->next; t=t->next)
305                    ;
306                t->next = n;          
307            }
308        }
309    }
310    
311    
312    /* Store utf8 decoded user IDs in the code to avoid in-place decoding. */
313    static void
314    keycache_decode_uids (gpg_keycache_t ctx)
315    {
316        struct keycache_s *c;
317    
318        for (c = ctx->item; c; c = c->next)
319            keycache_decode_uid (c);
320    }
321    
322    
323    static void
324    free_native_uids (struct native_uid_s **r_n)
325    {
326        struct native_uid_s *t;
327        struct native_uid_s *n = *r_n;
328    
329        while (n != NULL) {
330            t = n->next;
331            if (n->malloced) {
332                free_if_alloc (n->uid);
333                free_if_alloc (n->name);
334                free_if_alloc (n->comment);
335                free_if_alloc (n->email);
336            }
337            free_if_alloc (n);
338            n = t;
339        }
340        *r_n = NULL;
341    }
342    
343    
344    
345  /* Merge the information from the keyrings into the key cache structure. */  /* Merge the information from the keyrings into the key cache structure. */
346  gpgme_error_t  static gpgme_error_t
347  keycache_prepare2 (gpg_keycache_t ctx, const char *kid,  keycache_prepare2 (gpg_keycache_t ctx, const char *kid,
348                     const char *pubring, const char *secring)                     const char *pubring, const char *secring)
349  {  {
350      gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);      gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
351      gpgme_key_t key = NULL;      gpgme_key_t key = NULL;
352      gpg_iobuf_t inp;      gpg_iobuf_t inp;
353      PACKET *pkt = (PACKET*)calloc (1, sizeof * pkt);      PACKET *pkt;
354      struct keycache_s *c;      struct keycache_s *c;
355      const byte *sym_prefs;      const BYTE *sym_prefs;
356      char keyid[16+1], *id = NULL;      char keyid[16+1];
357      int key_seen = 0;      int key_seen = 0;
358      size_t nsym =0;      size_t nsym =0;
359    
360      if (secring) {      if (secring) {
361          parse_secring (ctx, kid, secring);          parse_secring (ctx, kid, secring);
362          if (!pubring) {          if (!pubring)
             safe_free(pkt);  
363              return 0;              return 0;
         }  
364      }      }
365      inp = gpg_iobuf_open (pubring);      inp = gpg_iobuf_open (pubring);
366      if (!inp) {      if (!inp)
         safe_free( pkt );  
367          return gpg_error (GPG_ERR_KEYRING_OPEN);          return gpg_error (GPG_ERR_KEYRING_OPEN);
368      }      gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */
     gpg_iobuf_ioctl( inp, 3, 1, NULL ); /* disable cache */  
369    
370      gpg_init_packet( pkt );      pkt = (PACKET*)calloc (1, sizeof * pkt);
371        if (!pkt)
372            BUG (0);
373        gpg_init_packet (pkt);
374      while (gpg_parse_packet (inp, pkt) != -1) {      while (gpg_parse_packet (inp, pkt) != -1) {
375          if (pkt->pkttype == PKT_PUBLIC_KEY) {          if (pkt->pkttype == PKT_PUBLIC_KEY) {
376              strcpy (keyid, "");              strcpy (keyid, "");
377              key_seen = 1;              key_seen = 1;
378          }          }
379            if (pkt->pkttype == PKT_SIGNATURE &&
380          if (pkt->pkttype == PKT_SIGNATURE && pkt->pkt.signature->sig_class == 0x1F) {              pkt->pkt.signature->sig_class == 0x1F) {
381              if (pkt->pkt.signature->numrevkeys == 0)              if (pkt->pkt.signature->numrevkeys == 0)
382                  goto next;                  goto next;
383              _snprintf (keyid, sizeof keyid -1, "%08X", pkt->pkt.signature->keyid[1]);              _snprintf (keyid, sizeof (keyid) -1, "%08X",
384                            pkt->pkt.signature->keyid[1]);
385              if (kid && strcmp (kid, keyid) != 0)              if (kid && strcmp (kid, keyid) != 0)
386                  goto next;                  goto next;
387              err = gpg_keycache_find_key2 (ctx, keyid, 0, &key, &c);              err = gpg_keycache_find_key2 (ctx, keyid, 0, &key, &c);
# Line 149  keycache_prepare2 (gpg_keycache_t ctx, c Line 389  keycache_prepare2 (gpg_keycache_t ctx, c
389                  goto next;                  goto next;
390              c->gloflags.has_desig_rev = 1;              c->gloflags.has_desig_rev = 1;
391          }          }
392          if (pkt->pkttype == PKT_SIGNATURE && key_seen == 1 ) {          if (pkt->pkttype == PKT_SIGNATURE && key_seen == 1 && c != NULL) {
393              sym_prefs=gpg_parse_sig_subpkt (pkt->pkt.signature->hashed,              sym_prefs = gpg_parse_sig_subpkt (pkt->pkt.signature->hashed,
394                                              SIGSUBPKT_PREF_SYM, &nsym);                                                SIGSUBPKT_PREF_SYM, &nsym);
395              if (sym_prefs == NULL)              if (!sym_prefs)
396                  goto next;                  goto next;
397              _snprintf (keyid, sizeof keyid - 1, "%08X", pkt->pkt.signature->keyid[1]);              _snprintf (keyid, sizeof (keyid) - 1, "%08X",
398                            pkt->pkt.signature->keyid[1]);
399              if (kid && strcmp (kid, keyid) != 0)              if (kid && strcmp (kid, keyid) != 0)
400                  goto next;                  goto next;
401              err = gpg_keycache_find_key2 (ctx, keyid, 0, &key, &c);              err = gpg_keycache_find_key2 (ctx, keyid, 0, &key, &c);
402              if (err)              if (err || !c)
403                    goto next;
404                if (c->sym_prefs) // only use the prefs from the primary uid.
405                  goto next;                  goto next;
406              else if (nsym > 0) {              else if (nsym > 0) {
407                  c->sym_prefs = (unsigned char*)calloc (1, nsym+1);                  c->sym_prefs = safe_uchar_alloc (nsym+1);
408                  if (!c->sym_prefs)                  memset (c->sym_prefs, 0, nsym+1);
                     return gpg_error (GPG_ERR_ENOMEM);  
409                  memcpy (c->sym_prefs, sym_prefs, nsym);                  memcpy (c->sym_prefs, sym_prefs, nsym);
410              }              }
411          }          }
         if (pkt->pkttype == PKT_USER_ID) {  
             if (id)  
                 free (id);  
             id = strdup (pkt->pkt.user_id->name);  
             if (!id) {  
                 err = gpg_error (GPG_ERR_ENOMEM);  
                 goto fail;  
             }  
         }  
         if ((pkt->pkttype == PKT_USER_ID || pkt->pkttype == PKT_ATTRIBUTE)  
             && pkt->pkt.user_id->attrib_data && key) {  
             PKT_user_id *uid = pkt->pkt.user_id;  
             c->attrib.used = 1;  
             c->attrib.len = uid->attrib_len;  
             c->attrib.d = (unsigned char*)calloc (1, uid->attrib_len + 1);  
             if (!c->attrib.d) {  
                 err = gpg_error (GPG_ERR_ENOMEM);  
                 goto fail;  
             }  
             memcpy (c->attrib.d, uid->attrib_data, uid->attrib_len);  
             key = NULL;  
             c = NULL;  
         }  
412  next:  next:
413          gpg_free_packet (pkt);          gpg_free_packet (pkt);
414          gpg_init_packet(pkt);          gpg_init_packet(pkt);
415      }      }
416    
 fail:  
     safe_free (id);  
417      safe_free (pkt);      safe_free (pkt);
418      gpg_iobuf_close (inp);      gpg_iobuf_close (inp);
419      return err;      return err;
# Line 228  gpg_keycache_new (gpg_keycache_t *r_ctx) Line 445  gpg_keycache_new (gpg_keycache_t *r_ctx)
445            
446      if (!r_ctx)      if (!r_ctx)
447          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
448      ctx = (gpg_keycache_t)calloc (1, sizeof *ctx);      ctx = new gpg_keycache_s;    
449      if (!ctx)      if (!ctx)
450          return gpg_error (GPG_ERR_ENOMEM);          BUG (0);
451        memset (ctx, 0, sizeof *ctx);
452      ctx->secret = 0;      ctx->secret = 0;
453      ctx->pos = 0;      ctx->pos = 0;
454      *r_ctx = ctx;      *r_ctx = ctx;
# Line 238  gpg_keycache_new (gpg_keycache_t *r_ctx) Line 456  gpg_keycache_new (gpg_keycache_t *r_ctx)
456  }  }
457    
458    
459    void
460    gpg_keycache_item_release (struct keycache_s *c)
461    {
462        if (c->key)
463            gpgme_key_release (c->key);    
464        c->key = NULL;      
465        if (c->rev != NULL)    
466            gpg_desig_rev_release (c->rev);
467        c->rev = NULL;
468        free_if_alloc (c->pref_keyserver);
469        free_if_alloc (c->sym_prefs);
470        free_if_alloc (c->attrib.d);
471        free_if_alloc (c->card_type);
472        free_native_uids (&c->uids);
473        free_if_alloc (c);
474    }
475    
476    
477  /* Release keycache object @ctx. */  /* Release keycache object @ctx. */
478  void  void
479  gpg_keycache_release (gpg_keycache_t ctx)  gpg_keycache_release (gpg_keycache_t ctx)
# Line 249  gpg_keycache_release (gpg_keycache_t ctx Line 485  gpg_keycache_release (gpg_keycache_t ctx
485    
486      for (c = ctx->item; c; c = c2) {      for (c = ctx->item; c; c = c2) {
487          c2 = c->next;          c2 = c->next;
488          gpgme_key_release (c->key);          gpg_keycache_item_release (c);
         c->key = NULL;  
         if (c->sym_prefs)  
             free (c->sym_prefs);  
         c->sym_prefs = NULL;  
         if (c->attrib.d)  
             free (c->attrib.d);  
         c->attrib.d = NULL;  
         if (c->card_type)  
             free (c->card_type);  
         free (c);  
489      }      }
490      if (ctx)      free_if_alloc (ctx);
         free (ctx);  
491  }  }
492    
493    
# Line 294  gpg_keycache_add_key (gpg_keycache_t ctx Line 519  gpg_keycache_add_key (gpg_keycache_t ctx
519      if (!ctx)      if (!ctx)
520          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
521            
522      c = (struct keycache_s*)calloc (1, sizeof *c);      c = new keycache_s;
523      if (!c)      if (!c)
524          return gpg_error (GPG_ERR_ENOMEM);          BUG (0);
525        memset (c, 0, sizeof *c);
526      c->gloflags.is_protected = 1; /*default: assume protection. */      c->gloflags.is_protected = 1; /*default: assume protection. */
527      c->key = key;      c->key = key;
528      if (!ctx->item)      if (!ctx->item)
# Line 367  gpg_keycache_find_key2 (gpg_keycache_t c Line 593  gpg_keycache_find_key2 (gpg_keycache_t c
593      }      }
594      *r_key = NULL;      *r_key = NULL;
595      return gpg_error (GPG_ERR_INTERNAL);      return gpg_error (GPG_ERR_INTERNAL);
596  } /* keycache_find_key */  }
597    
598    
599  gpgme_error_t  gpgme_error_t
# Line 378  gpg_keycache_find_key (gpg_keycache_t ct Line 604  gpg_keycache_find_key (gpg_keycache_t ct
604  }  }
605    
606    
607    /* Reload a photo image of a single key with the keyid @keyid.
608       Return value: 0 on success. */
609    static gpgme_error_t
610    keycache_reload_photo (gpg_keycache_t ctx, const char *keyid)
611    {
612        attr_list_t list;
613    
614        if (parse_attr_data (keyid, &list) < 1) {
615            free_attr_list (list);
616            return 0;
617        }
618        keycache_update_photo (ctx, list->fpr, list);
619        free_attr_list (list);
620        return 0;
621    }
622    
623    
624    /* Return the next key which was updated. Before it is
625       returned the update flag is cleared.
626       @r_status is 1 for a new key and 2 for an updated key.
627       Return value: 0 on success. */
628    gpgme_error_t
629    gpg_keycache_next_updated_key (gpg_keycache_t ctx,
630                                   struct keycache_s **r_obj,
631                                   int *r_status)
632    {
633        struct keycache_s *c;
634    
635        for (c = ctx->item; c; c = c->next) {
636            if (c->flags != 0) {
637                *r_status = c->flags;
638                *r_obj = c;
639                c->flags = 0; /* reset update flag. */
640                return 0;
641            }
642        }
643        return gpg_error (GPG_ERR_NOT_FOUND);
644    }
645    
646    
647    /* Helper to retrieve a GPG key. */
648    static gpgme_error_t
649    get_gpg_key (const char *keyid, int is_sec, gpgme_key_t *r_key)
650    {
651        gpgme_ctx_t ctx;
652        gpgme_error_t err;
653    
654        err = gpgme_new (&ctx);
655        if (err)
656            return err;
657        gpgme_set_keylist_mode  (ctx, GPGME_KEYLIST_MODE_SIGS);
658        err = gpgme_get_key (ctx, keyid, r_key, is_sec);
659        gpgme_release (ctx);
660        return err;
661    }
662    
663    
664    /* Fetch a key directly from gpg but without adding
665       it to the key cache. Caller must free @r_ctx. */
666    gpgme_error_t
667    gpg_keycache_fetch_key (const char *keyid, int is_sec,
668                            gpgme_key_t *r_key, struct keycache_s **r_c)
669    {
670        gpgme_error_t err;
671        gpgme_key_t key;
672        struct keycache_s *c;
673    
674        *r_key = NULL;
675        *r_c = NULL;
676        err = get_gpg_key (keyid, is_sec, &key);
677        if (err)
678            return err;
679    
680        c = new keycache_s;
681        if (!c)
682            BUG (0);
683        memset (c, 0, sizeof *c);
684        c->gloflags.is_protected = 1; /*default: assume protection. */
685        c->key = key;    
686        keycache_decode_uid (c);
687        *r_key = key;
688        *r_c = c;
689        return 0;
690    }
691    
692    
693    /* Update the key with the keyid @key in the key cache.
694       If the key does not exist, it is added otherwise all
695       parts are first freed and then replaced with the updated data. */
696  gpgme_error_t  gpgme_error_t
697  gpg_keycache_update_key (gpg_keycache_t ctx, int is_sec,  gpg_keycache_update_key (gpg_keycache_t ctx, int is_sec,
698                           void *opaque, const char *keyid)                           void *opaque, const char *keyid)
699  {  {    
     struct keycache_s *c = NULL, *c_new=NULL;  
700      gpgme_key_t key=NULL, fndkey=NULL;      gpgme_key_t key=NULL, fndkey=NULL;
701      gpgme_error_t err;      gpgme_error_t err;
702      gpgme_ctx_t gctx;      struct keycache_s *c = NULL, *c_new=NULL;
703      gpg_keycache_t pub = (gpg_keycache_t)opaque;      gpg_keycache_t pub = (gpg_keycache_t)opaque;
704    
705      err = gpgme_new (&gctx);      err = get_gpg_key (keyid, is_sec, &key);
     if (err)  
         return err;  
     err = gpgme_get_key (gctx, keyid, &key, is_sec);  
     gpgme_release (gctx);  
706      if (err)      if (err)
707          return err;          return err;
708      err = gpg_keycache_find_key2 (ctx, keyid, 0, &fndkey, &c);      err = gpg_keycache_find_key2 (ctx, keyid, 0, &fndkey, &c);
# Line 400  gpg_keycache_update_key (gpg_keycache_t Line 710  gpg_keycache_update_key (gpg_keycache_t
710          log_debug ("keycache update: keyid=%s %p\r\n", keyid, pub);          log_debug ("keycache update: keyid=%s %p\r\n", keyid, pub);
711          gpgme_key_release (fndkey);          gpgme_key_release (fndkey);
712          c->key = key;          c->key = key;
713          c->flags = 0;          c->flags = KC_FLAG_UPD;
714          if (is_sec && pub != NULL &&          if (is_sec && pub != NULL &&
715              !gpg_keycache_find_key (pub, keyid, 0, &fndkey)) {              !gpg_keycache_find_key (pub, keyid, 0, &fndkey)) {
716              log_debug ("keycache update: set public part %p\r\n", fndkey);              log_debug ("keycache update: set public part %p\r\n", fndkey);
717              c->pubpart->key = fndkey;              c->pubpart->key = fndkey;
718          }          }
719            /* XXX: this is also called for keys without a photo-id. */
720            keycache_reload_photo (ctx, keyid);
721      }      }
722      else {      else {
723          log_debug ("keycache add: sync public part\r\n");          log_debug ("keycache add: sync public part\r\n");
# Line 421  gpg_keycache_update_key (gpg_keycache_t Line 733  gpg_keycache_update_key (gpg_keycache_t
733                  c->gloflags.divert_to_card = c_new->gloflags.divert_to_card;                  c->gloflags.divert_to_card = c_new->gloflags.divert_to_card;
734              }              }
735          }          }
736            if (c != NULL)
737                c->flags = KC_FLAG_ADD;
738      }      }
739    
740        /* refresh utf8 user ID list. */
741        if (c != NULL && c->key) {
742            free_native_uids (&c->uids);
743            keycache_decode_uid (c);
744        }
745    
746      return 0;      return 0;
747  }  }
748    
# Line 442  gpg_keycache_delete_key (gpg_keycache_t Line 763  gpg_keycache_delete_key (gpg_keycache_t
763          return rc;          return rc;
764            
765      c = ctx->item;      c = ctx->item;
766      if (c->next == NULL) {      if (!c) /* empty */
767          gpgme_key_release (itm->key);          return 0;
768          if (itm)      else if (c->next == NULL) {
769              free (itm);          if (itm->key)
770                gpgme_key_release (itm->key);
771            itm->key = NULL;
772            free_if_alloc (itm);
773            /* the cache has no other items, so we set the context to NULL
774               to indicate that the entire cache is empty. */
775          ctx->item = NULL;          ctx->item = NULL;
776      }      }
777      else {      else {
778          while (c && c->next != itm)          for (; c != NULL; c = c->next) {
779              c = c->next;              if (c->next == itm)
780                    break;
781            }
782            assert (c != NULL); /* XXX: sometimes access violation. */
783          c->next = c->next->next;          c->next = c->next->next;
784          gpgme_key_release (itm->key);          if (itm->key)
785          if (itm)              gpgme_key_release (itm->key);
786              free (itm);          itm->key = NULL;
787            free_if_alloc (itm);
788      }      }
789      return 0;      return 0;
790  }  }
# Line 478  gpg_keycache_init (gpg_keycache_t ctx, c Line 808  gpg_keycache_init (gpg_keycache_t ctx, c
808      if (err)      if (err)
809          return err;          return err;
810    
811        /* XXX: GPGME_KEYLIST_MODE_SIG_NOTATIONS causes an internal error! */
812      gpgme_set_keylist_mode  (c, GPGME_KEYLIST_MODE_SIGS);      gpgme_set_keylist_mode  (c, GPGME_KEYLIST_MODE_SIGS);
813      err = gpgme_op_keylist_start (c, pattern, secret);      err = gpgme_op_keylist_start (c, pattern, secret);
814      while(!err) {      while(!err) {
# Line 490  gpg_keycache_init (gpg_keycache_t ctx, c Line 821  gpg_keycache_init (gpg_keycache_t ctx, c
821      }      }
822      if (gpgme_err_code (err) == GPG_ERR_EOF)      if (gpgme_err_code (err) == GPG_ERR_EOF)
823          err = gpg_error (GPG_ERR_NO_ERROR);          err = gpg_error (GPG_ERR_NO_ERROR);
824      /* XXX: make sure the progress dialog is closed. */      keycache_update_photos (ctx);
825        keycache_decode_uids (ctx);
826      gpgme_op_keylist_end (c);      gpgme_op_keylist_end (c);
827      gpgme_release (c);      gpgme_release (c);
828      return err;      return err;
829  }  }
830    
831    
832  /* XXX: kludge to see if the key is stored on a card. */  /* Return 1 if we can assume that the actual private key is
833       stored on a smart card. This is not bullet proof, but the
834       card provides 3 keys (RSA) and each key for a different purpose. */
835  static int  static int
836  key_divert_to_card (gpgme_key_t key)  key_divert_to_card (gpgme_key_t key)
837  {  {
838      gpgme_subkey_t k;      gpgme_subkey_t k;
839      int n=0, n_alg=0, can_auth = 0;      int n=0;
840        int can_auth = 0, can_encr = 0;
841    
842      for (k = key->subkeys; k; k = k->next) {      for (k = key->subkeys; k; k = k->next) {
843          n++;          n++;
844          if (k->pubkey_algo == GPGME_PK_RSA && k->length == 1024)          if (k->pubkey_algo != GPGME_PK_RSA || k->length != 1024) {
845              n_alg++;              return 0;
846                break;
847            }
848          if (k->can_authenticate)          if (k->can_authenticate)
849              can_auth++;              can_auth++;
850            if (k->can_encrypt)
851                can_encr++;
852      }      }
853      if (n == 3 && n_alg == 3 && can_auth == 1)      if (n >= 3 && can_auth >= 1 && can_encr >= 1)
854          return 1;          return 1;
855      return 0;      return 0;
856  }  }
857    
858    
859    static unsigned char*
860    copy_uid_prefs (const unsigned char *prefs)
861    {
862        unsigned char *p;
863        size_t pos=0;
864    
865        while (prefs[pos] != 0)
866            pos++;
867        p = safe_uchar_alloc (pos+1);
868        memset (p, 0, pos+1);
869        memcpy (p, prefs, pos);
870        return p;
871    }
872    
873    
874    /* Sync the secret and the public key cache information. */
875  gpgme_error_t  gpgme_error_t
876  gpg_keycache_sync (gpg_keycache_t pub, gpg_keycache_t sec)  gpg_keycache_sync (gpg_keycache_t pub, gpg_keycache_t sec)
877  {  {
# Line 526  gpg_keycache_sync (gpg_keycache_t pub, g Line 881  gpg_keycache_sync (gpg_keycache_t pub, g
881      if (!pub || !sec)      if (!pub || !sec)
882          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
883            
884      for (c=sec->item; c; c=c->next) {        for (c=sec->item; c; c=c->next) {
885          if (!gpg_keycache_find_key2 (pub, c->key->subkeys->keyid, 0, &key, &c_sec)) {          if (!gpg_keycache_find_key2 (pub, c->key->subkeys->keyid, 0,
886                                         &key, &c_sec)) {
887              c_sec->gloflags.is_protected = c->gloflags.is_protected;              c_sec->gloflags.is_protected = c->gloflags.is_protected;
888              c_sec->gloflags.divert_to_card = c->gloflags.divert_to_card;              c_sec->gloflags.divert_to_card = c->gloflags.divert_to_card;
889              if (!c->gloflags.divert_to_card)              if (!c->gloflags.divert_to_card)
890                  c->gloflags.divert_to_card = key_divert_to_card (key);                  c->gloflags.divert_to_card = key_divert_to_card (key);
891                if (c_sec->sym_prefs)
892                    c->sym_prefs = copy_uid_prefs (c_sec->sym_prefs);
893              c->pubpart = c_sec;              c->pubpart = c_sec;
894              c->pubpart->key = key;              c->pubpart->key = key;
895          }          }
# Line 580  keycache_next_key (gpg_keycache_t ctx, i Line 938  keycache_next_key (gpg_keycache_t ctx, i
938          *r_key = NULL;          *r_key = NULL;
939          return gpg_error (GPG_ERR_EOF);          return gpg_error (GPG_ERR_EOF);
940      }      }
941            if (ctx->tmp->flags != 0)
942            ctx->tmp->flags = 0; /* reset the 'updated' status. */
943    
944        /* it might be possible there is no public key. */
945        if (flags && ctx->tmp->pubpart == NULL)
946            flags = 0;
947      *r_key = flags? ctx->tmp->pubpart->key : ctx->tmp->key;      *r_key = flags? ctx->tmp->pubpart->key : ctx->tmp->key;
948      *c = ctx->tmp = ctx->tmp->next;      *c = ctx->tmp;
949        ctx->tmp = ctx->tmp->next;
950      ctx->pos++;      ctx->pos++;
951    
952      return 0;      return 0;
# Line 596  gpgme_error_t Line 960  gpgme_error_t
960  gpg_keycache_next_key (gpg_keycache_t ctx, int flags, gpgme_key_t *r_key)  gpg_keycache_next_key (gpg_keycache_t ctx, int flags, gpgme_key_t *r_key)
961  {  {
962      struct keycache_s *c=NULL;      struct keycache_s *c=NULL;
963      gpgme_error_t err = 0;      gpgme_error_t err;
964    
965      err = keycache_next_key (ctx, flags, &c, r_key);      err = keycache_next_key (ctx, flags, &c, r_key);
966      return err;      return err;
967  }  }
968    
969    
970    gpgme_error_t
971    gpg_keycache_next_key2 (gpg_keycache_t ctx, int flags,
972                            struct keycache_s **c, gpgme_key_t *r_key)
973    {
974        return keycache_next_key (ctx, flags, c, r_key);
975    }
976    
977    
978    /* Search for a key with the pattern @pattern and mark
979       this key as the default signing key if found.
980       Return value: 0 on success. */
981    gpgme_error_t
982    gpg_keycache_set_default_key (gpg_keycache_t ctx,
983                                  const char *pattern)
984    {
985        gpgme_error_t err;
986        gpgme_key_t key;
987        struct keycache_s *itm;
988    
989        err = gpg_keycache_find_key2 (ctx, pattern, 0, &key, &itm);
990        if (err)
991            return err;
992    
993        if (itm)
994            itm->default_key = 1;
995        return 0;
996    }
997    
998    /* Return the default key from the cache. If no was
999       marked before, NULL is returned in @r_key.
1000       Return value: 0 on success. */
1001    gpgme_error_t
1002    gpg_keycache_get_default_key (gpg_keycache_t ctx,
1003                                  gpgme_key_t *r_key)
1004    {
1005        struct keycache_s *itm;
1006    
1007        *r_key = NULL;
1008        for (itm = ctx->item; itm; itm = itm->next) {
1009            if (itm->default_key) {
1010                *r_key = itm->key;
1011                break;
1012            }
1013        }
1014        if (!*r_key)
1015            return gpgme_error (GPG_ERR_NOT_FOUND);
1016        return 0;
1017    }
1018    
1019    
1020    static gpgme_error_t
1021    decode_subpacket (const char *subpkt_data, int *type,
1022                      char **out, WORD *outlen)
1023    {
1024        char tmp[128], *val;
1025        char *enc = NULL;      
1026        size_t pos = 0;
1027    
1028        /* example: spk:24:1:21:http%3A//subkeys.pgp.de */
1029        *outlen = 0;
1030        *out = NULL;
1031        
1032        if (strncmp (subpkt_data, "spk:", 4))
1033            return gpg_error (GPG_ERR_NO_DATA);
1034    
1035        /* XXX: do not use static buffer sizes. */
1036        strncpy (tmp, subpkt_data, DIM (tmp)-4);
1037        val = strtok (tmp, ":");
1038        while (val != NULL) {
1039            switch (pos++) {
1040            case 0:
1041                break;
1042    
1043            case 1:
1044                if (type)
1045                    *type = atoi (val);
1046                break;
1047    
1048            case 2:
1049                break;
1050    
1051            case 3:
1052                *outlen = atoi (val);
1053                break;
1054    
1055            case 4:
1056                enc = m_strdup (val);
1057                break;
1058            }
1059            val = strtok (NULL, ":");
1060        }
1061        if (!enc)
1062            return gpg_error (GPG_ERR_NO_DATA);
1063        unhexify_buffer (enc, out);
1064        free_if_alloc (enc);
1065        return 0;
1066    }
1067    
1068    
1069    /* If the attribute given in @attr is not set in the
1070       key cache object, try to update it. */
1071    gpgme_error_t
1072    gpg_keycache_update_attr (struct keycache_s *item,
1073                              int attr, int force)
1074    {
1075        gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
1076        char *val = NULL;
1077        WORD n = 0;    
1078    
1079        switch (attr) {
1080        case KC_ATTR_PREFSYM:
1081            if (!force && item->sym_prefs)
1082                break;
1083            free_if_alloc (item->sym_prefs);
1084            err = gpg_find_key_subpacket (item->key->subkeys->keyid+8, attr, &val);
1085            if (!err && val != NULL)
1086                err = decode_subpacket (val, NULL, (char**)&item->sym_prefs, &n);
1087            break;
1088    
1089        case KC_ATTR_PREFKSERV:
1090            if (!force && item->pref_keyserver)
1091                break;
1092            free_if_alloc (item->pref_keyserver);
1093            err = gpg_find_key_subpacket (item->key->subkeys->keyid+8, attr, &val);
1094            if (!err && val != NULL)
1095                err = decode_subpacket (val, NULL, &item->pref_keyserver, &n);
1096            if (!err && item->pref_keyserver)
1097                err = parse_keyserver_url (&item->pref_keyserver,
1098                                           &item->pref_keyserver_port);
1099            break;
1100        }
1101        safe_free (val);
1102        return err;
1103    }

Legend:
Removed from v.121  
changed lines
  Added in v.271

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26