/[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 180 by twoaday, Tue Feb 7 08:58:04 2006 UTC revision 181 by twoaday, Tue Mar 14 11:01:22 2006 UTC
# Line 36  Line 36 
36  #include "wptErrors.h"  #include "wptErrors.h"
37  #include "wptW32API.h"  #include "wptW32API.h"
38  #include "wptGPG.h"  #include "wptGPG.h"
39    #include "wptTypes.h"
40    
41    
42  /* Attribute list which holds the image data. */  /* Attribute list which holds the image data. */
# Line 49  struct attr_list_s { Line 50  struct attr_list_s {
50  typedef struct attr_list_s *attr_list_t;  typedef struct attr_list_s *attr_list_t;
51    
52    
53    /* Free attribute list @ctx. */
54  void  void
55  free_attr_list (attr_list_t ctx)  free_attr_list (attr_list_t ctx)
56  {  {
# Line 81  parse_attr_list (FILE *fp, const BYTE *d Line 83  parse_attr_list (FILE *fp, const BYTE *d
83          buffer = buf+9+10;          buffer = buf+9+10;
84          pos = 0;          pos = 0;
85          c = (attr_list_t)calloc (1, sizeof *c);          c = (attr_list_t)calloc (1, sizeof *c);
86            if (!c)
87                BUG (0);
88          p = strtok (buffer, " ");          p = strtok (buffer, " ");
89          while (p != NULL) {          while (p != NULL) {
90              switch (pos) {              switch (pos) {
# Line 102  parse_attr_list (FILE *fp, const BYTE *d Line 106  parse_attr_list (FILE *fp, const BYTE *d
106              pos++;              pos++;
107              p = strtok (NULL, " ");              p = strtok (NULL, " ");
108          }          }
         /*printf ("id=%s octets=%d flags=%d\n", c->fpr, c->octets, c->flags);*/  
109          if (!*ctx)          if (!*ctx)
110              *ctx = c;              *ctx = c;
111          else {          else {
# Line 111  parse_attr_list (FILE *fp, const BYTE *d Line 114  parse_attr_list (FILE *fp, const BYTE *d
114              t->next = c;              t->next = c;
115          }          }
116          c->d = (unsigned char*)malloc (c->octets);          c->d = (unsigned char*)malloc (c->octets);
117            if (!c->d)
118                BUG (0);
119          memcpy (c->d, data, c->octets);          memcpy (c->d, data, c->octets);
120          data += c->octets;          data += c->octets;
121          datlen -= c->octets;          datlen -= c->octets;
# Line 173  parse_secring (gpg_keycache_t cache, con Line 178  parse_secring (gpg_keycache_t cache, con
178    
179      gpg_iobuf_ioctl (inp, 3, 1, NULL);      gpg_iobuf_ioctl (inp, 3, 1, NULL);
180      pkt = (PACKET*)calloc (1, sizeof *pkt);      pkt = (PACKET*)calloc (1, sizeof *pkt);
181        if (!pkt)
182            BUG (0);
183      gpg_init_packet (pkt);      gpg_init_packet (pkt);
184      while (gpg_parse_packet (inp, pkt) != -1) {      while (gpg_parse_packet (inp, pkt) != -1) {
185          if (pkt->pkttype == PKT_SECRET_KEY) {          if (pkt->pkttype == PKT_SECRET_KEY) {
# Line 216  keycache_update_photo (gpg_keycache_t ct Line 223  keycache_update_photo (gpg_keycache_t ct
223      fnd->attrib.flags = dat->flags;      fnd->attrib.flags = dat->flags;
224      fnd->attrib.len = dat->octets;      fnd->attrib.len = dat->octets;
225      fnd->attrib.d = (unsigned char*)malloc (dat->octets);      fnd->attrib.d = (unsigned char*)malloc (dat->octets);
226        if (!fnd->attrib.d)
227            BUG (0);
228      memcpy (fnd->attrib.d, dat->d, dat->octets);      memcpy (fnd->attrib.d, dat->d, dat->octets);
229      return 0;      return 0;
230  }  }
# Line 267  keycache_prepare2 (gpg_keycache_t ctx, c Line 276  keycache_prepare2 (gpg_keycache_t ctx, c
276      gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */      gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */
277    
278      pkt = (PACKET*)calloc (1, sizeof * pkt);      pkt = (PACKET*)calloc (1, sizeof * pkt);
279        if (!pkt)
280            BUG (0);
281      gpg_init_packet (pkt);      gpg_init_packet (pkt);
282      while (gpg_parse_packet (inp, pkt) != -1) {      while (gpg_parse_packet (inp, pkt) != -1) {
283          if (pkt->pkttype == PKT_PUBLIC_KEY) {          if (pkt->pkttype == PKT_PUBLIC_KEY) {
# Line 302  keycache_prepare2 (gpg_keycache_t ctx, c Line 313  keycache_prepare2 (gpg_keycache_t ctx, c
313              else if (nsym > 0) {              else if (nsym > 0) {
314                  c->sym_prefs = (unsigned char*)calloc (1, nsym+1);                  c->sym_prefs = (unsigned char*)calloc (1, nsym+1);
315                  if (!c->sym_prefs)                  if (!c->sym_prefs)
316                      return gpg_error (GPG_ERR_ENOMEM);                      BUG (0);
317                  memcpy (c->sym_prefs, sym_prefs, nsym);                  memcpy (c->sym_prefs, sym_prefs, nsym);
318              }              }
319          }          }
# Line 344  gpg_keycache_new (gpg_keycache_t *r_ctx) Line 355  gpg_keycache_new (gpg_keycache_t *r_ctx)
355          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
356      ctx = (gpg_keycache_t)calloc (1, sizeof *ctx);      ctx = (gpg_keycache_t)calloc (1, sizeof *ctx);
357      if (!ctx)      if (!ctx)
358          return gpg_error (GPG_ERR_ENOMEM);          BUG (0);
359      ctx->secret = 0;      ctx->secret = 0;
360      ctx->pos = 0;      ctx->pos = 0;
361      *r_ctx = ctx;      *r_ctx = ctx;
# Line 365  gpg_keycache_release (gpg_keycache_t ctx Line 376  gpg_keycache_release (gpg_keycache_t ctx
376          c2 = c->next;          c2 = c->next;
377          gpgme_key_release (c->key);          gpgme_key_release (c->key);
378          c->key = NULL;          c->key = NULL;
379            safe_free (c->pref_keyserver);
380          safe_free (c->sym_prefs);          safe_free (c->sym_prefs);
381          safe_free (c->attrib.d);          safe_free (c->attrib.d);
382          safe_free (c->card_type);          safe_free (c->card_type);
# Line 404  gpg_keycache_add_key (gpg_keycache_t ctx Line 416  gpg_keycache_add_key (gpg_keycache_t ctx
416            
417      c = (struct keycache_s*)calloc (1, sizeof *c);      c = (struct keycache_s*)calloc (1, sizeof *c);
418      if (!c)      if (!c)
419          return gpg_error (GPG_ERR_ENOMEM);          BUG (0);
420      c->gloflags.is_protected = 1; /*default: assume protection. */      c->gloflags.is_protected = 1; /*default: assume protection. */
421      c->key = key;      c->key = key;
422      if (!ctx->item)      if (!ctx->item)
# Line 682  copy_uid_prefs (const unsigned char *pre Line 694  copy_uid_prefs (const unsigned char *pre
694          pos++;          pos++;
695      p = (unsigned char*)calloc (1, pos+1);      p = (unsigned char*)calloc (1, pos+1);
696      if (!p)      if (!p)
697          abort ();          BUG (0);
698      memcpy (p, prefs, pos);      memcpy (p, prefs, pos);
699      return p;      return p;
700  }  }
# Line 779  gpg_keycache_next_key (gpg_keycache_t ct Line 791  gpg_keycache_next_key (gpg_keycache_t ct
791      return err;      return err;
792  }  }
793    
794    
795  /* Search for a key with the pattern @pattern and mark  /* Search for a key with the pattern @pattern and mark
796     this key as the default signing key if found.     this key as the default signing key if found.
797     Return value: 0 on success. */     Return value: 0 on success. */
# Line 821  gpg_keycache_get_default_key (gpg_keycac Line 834  gpg_keycache_get_default_key (gpg_keycac
834  }  }
835    
836    
837    static gpgme_error_t
838    decode_subpacket (const char *subpkt_data, int *type,
839                      char **out, WORD *outlen)
840    {
841        char tmp[128], *p = tmp, *val;
842        char *enc;
843        size_t pos = 0, i=0;
844    
845        /* example: spk:24:1:21:http%3A//subkeys.pgp.de */
846        *outlen = 0;
847        *out = NULL;
848        
849        if (strncmp (subpkt_data, "spk:", 4))
850            return gpg_error (GPG_ERR_NO_DATA);
851    
852        strncpy (tmp, subpkt_data, 62);
853        val = strtok (tmp, ":");
854        while (val != NULL) {
855            switch (pos++) {
856            case 0:
857                break;
858    
859            case 1:
860                if (type)
861                    *type = atoi (val);
862                break;
863    
864            case 2:
865                break;
866    
867            case 3:
868                *outlen = atoi (val);
869                break;
870    
871            case 4:
872                enc = strdup (val);
873                break;
874            }
875            val = strtok (NULL, ":");
876        }
877        if (!enc)
878            return gpg_error (GPG_ERR_NO_DATA);;
879        *out = (char*)calloc (1, strlen (enc)+1);
880        for (pos = 0; pos < strlen (enc); pos++) {
881            if (enc[pos] == '%' && enc[pos+1] == '%')
882                (*out)[i++] = '%';
883            else if (enc[pos] == '%') {
884                char tmp[3];
885                tmp[0] = enc[++pos];
886                tmp[1] = enc[++pos];
887                tmp[2] = 0;
888                (*out)[i++] = (char)strtoul (tmp, NULL, 16);
889            }
890            else
891                (*out)[i++] = enc[pos];
892        }
893        (*out)[i] = 0;
894        free (enc);
895        return 0;
896    }
897    
898    
899    /* If the attribute given in @attr is not set in the
900       key cache object, try to update it. */
901    gpgme_error_t
902    gpg_keycache_update_attr (struct keycache_s *item,
903                              int attr, int force)
904    {
905        gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
906        char *val = NULL;
907        WORD n = 0;    
908    
909        switch (attr) {
910        case KC_ATTR_PREFSYM:
911            if (!force && item->sym_prefs)
912                break;
913            safe_free (item->sym_prefs);
914            err = gpg_find_key_subpacket (item->key->subkeys->keyid+8, attr, &val);
915            if (!err && val != NULL)
916                err = decode_subpacket (val, NULL, (char**)&item->sym_prefs, &n);
917            break;
918    
919        case KC_ATTR_PREFKSERV:
920            if (!force && item->pref_keyserver)
921                break;
922            safe_free (item->pref_keyserver);
923            err = gpg_find_key_subpacket (item->key->subkeys->keyid+8, attr, &val);
924            if (!err && val != NULL)
925                err = decode_subpacket (val, NULL, &item->pref_keyserver, &n);
926            break;
927        }
928        safe_free (val);
929        return err;
930    }
931    

Legend:
Removed from v.180  
changed lines
  Added in v.181

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26