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

Diff of /trunk/Src/wptGPGUtil.cpp

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

revision 133 by twoaday, Mon Jan 9 09:15:29 2006 UTC revision 181 by twoaday, Tue Mar 14 11:01:22 2006 UTC
# Line 25  Line 25 
25    
26  #include <windows.h>  #include <windows.h>
27  #include <sys/stat.h>  #include <sys/stat.h>
28    #include <string.h>
29    #include <errno.h>
30    
31  #include "gpgme.h"  #include "gpgme.h"
32  #include "wptTypes.h"  #include "wptTypes.h"
33  #include "wptErrors.h"  #include "wptErrors.h"
34    #include "wptW32API.h"
35    #include "wptGPG.h"
36    #include "openpgp.h"
37    
38    
39  #define NROFHEXDIGITS 2    #define NROFHEXDIGITS 2  
40  /* Convert two hexadecimal digits from STR to the value they  /* Convert two hexadecimal digits from STR to the value they
# Line 221  create_tmpfile (const char *name) Line 227  create_tmpfile (const char *name)
227      sec_attr.lpSecurityDescriptor = NULL;      sec_attr.lpSecurityDescriptor = NULL;
228      sec_attr.nLength = sizeof sec_attr;      sec_attr.nLength = sizeof sec_attr;
229    
230      GetTempPath (sizeof (tmp)-1 - strlen (name)-1, tmp);      get_temp_name (tmp, DIM (tmp)-1, name);
     strcat (tmp, name);  
231      out = CreateFile (tmp, GENERIC_READ|GENERIC_WRITE,      out = CreateFile (tmp, GENERIC_READ|GENERIC_WRITE,
232                        FILE_SHARE_WRITE, &sec_attr,                        FILE_SHARE_WRITE, &sec_attr,
233                        OPEN_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);                        OPEN_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
# Line 572  gpg_get_photoid_data (const char *keyid, Line 577  gpg_get_photoid_data (const char *keyid,
577      char *p, *cmd;      char *p, *cmd;
578      DWORD n;      DWORD n;
579    
580        if (ndata)
581            *ndata = 0;
582      p = read_gpg_program ();      p = read_gpg_program ();
583      if (!p)      if (!p)
584          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
# Line 602  gpg_get_photoid_data (const char *keyid, Line 609  gpg_get_photoid_data (const char *keyid,
609  }  }
610    
611    
612    /* Extract one or more keys from the key file @keyfile.
613       The keys to extract are give in @keys and the size of it is @nkeys.
614       @new_keyfile is a file with the extract keys.
615       Return value: 0 on success. */
616    gpgme_error_t
617    gpg_extract_keys (const char *keyfile, const char **keys, DWORD nkeys,
618                      char **new_keyfile)
619    {
620        gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
621        const char *fmt;
622        char *p, *cmd;
623        char tmpnam[MAX_PATH], tmpdir[MAX_PATH];
624        int i, n;
625    
626        /* copy key file to temp dir. */
627        GetTempPath (MAX_PATH-1, tmpdir);
628        get_temp_name (tmpnam, MAX_PATH-1, NULL);
629        CopyFile (keyfile, tmpnam, FALSE);
630    
631        /* create temp file for output. */
632        *new_keyfile = new char[MAX_PATH];
633        if (!*new_keyfile)
634            BUG (NULL);
635        get_temp_name (*new_keyfile, MAX_PATH-1, "sel_keys");
636    
637        p = read_gpg_program ();
638        if (!p)
639            return gpg_error (GPG_ERR_INV_ARG);
640    
641        /* Use the temp key file as a keyring and export the selected
642           keys from it. */
643        fmt = "%s --yes --output %s --no-options --homedir %s --keyring %s --export ";
644        n = strlen (fmt) + strlen (p)+1 + strlen (tmpdir)+1 + strlen (tmpnam) + 1;
645        n += strlen (*new_keyfile)+1;
646        for (i=0; i < (int)nkeys; i++)
647            n += strlen (keys[i])+1+2;
648        cmd = (char*)calloc (1, n+1);
649        sprintf (cmd, fmt, p, *new_keyfile, tmpdir, tmpnam);
650        for (i=0; i < (int)nkeys; i++) {
651            strcat (cmd, keys[i]);
652            strcat (cmd, " " );
653        }
654    
655        if (create_process (cmd, NULL, NULL, NULL))
656            err = gpgme_error (GPG_ERR_INTERNAL);
657    
658        DeleteFile (tmpnam);
659        safe_free (cmd);
660        safe_free (p);
661        return err;
662    }
663    
664    
665  /* Return the validity of the user attribute, informerly known  /* Return the validity of the user attribute, informerly known
666     as photo-ID. If no uat was found, return 0 for unknown. */     as photo-ID. If no uat was found, return 0 for unknown. */
667  gpgme_error_t  gpgme_error_t
# Line 627  get_uat_validity (const char *keyid, gpg Line 687  get_uat_validity (const char *keyid, gpg
687      if (create_process (cmd, NULL, out, NULL))      if (create_process (cmd, NULL, out, NULL))
688          err = gpg_error (GPG_ERR_INTERNAL);          err = gpg_error (GPG_ERR_INTERNAL);
689    
690      free (p);      safe_free (p);
691      free (cmd);      safe_free (cmd);
692            
693      p = map_tmpfile (out, NULL);      p = map_tmpfile (out, NULL);
694      if ((uat = strstr (p, "uat:"))) {      if ((uat = strstr (p, "uat:"))) {
# Line 646  get_uat_validity (const char *keyid, gpg Line 706  get_uat_validity (const char *keyid, gpg
706  }  }
707    
708    
709  #if 0  static gpgme_error_t
710    clip_store_data (char *tmp_outname, DWORD outlen)
711    {
712        gpgme_data_t in;
713        gpgme_error_t err;
714    
715        get_temp_name (tmp_outname, outlen-1, NULL);
716        err = gpg_data_new_from_clipboard (&in, 0);
717        if (err)
718            return err;
719        err = gpg_data_release_and_set_file (in, tmp_outname);
720        return err;
721    }
722    
723    
724  /* Extract all recipients from the file @file.  /* Extract all recipients from the file @file.
725     Return value: 0 on success. */     Return value: 0 on success. */
726  static int  static gpgme_error_t
727  do_get_recipients (const char *file, gpgme_recipient_t *r_list)  file_extract_recipient (const char *file, gpgme_recipient_t *r_list)
728  {  {    
     gpgme_recipient_t l;  
729      PACKET *pkt;      PACKET *pkt;
730        PKT_pubkey_enc *enc;
731      gpg_iobuf_t inp = NULL;      gpg_iobuf_t inp = NULL;
732      armor_filter_context_t afx;      armor_filter_context_t afx;
733        gpgme_recipient_t l;
734      int rc = 0, quit=0;      int rc = 0, quit=0;
735    
736      if (!file || !r_list) {      if (!file || !r_list) {
737          log_debug ("do_list_packets: !r_list || !file");          log_debug ("do_list_packets: !r_list || !file");
738          return -1;          return gpgme_error (GPG_ERR_INV_ARG);
739      }      }
740    
741      *r_list=NULL;      *r_list=NULL;
742      inp = gpg_iobuf_open (file);      inp = gpg_iobuf_open (file);
743      if (!inp)      if (!inp)
744          return WPTERR_FILE_OPEN;          return gpgme_err_code_from_errno (errno);
745      gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */      gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */
746      if (gpg_use_armor_filter (inp)) {      if (gpg_use_armor_filter (inp)) {
747          memset (&afx, 0, sizeof (afx));          memset (&afx, 0, sizeof (afx));
# Line 677  do_get_recipients (const char *file, gpg Line 752  do_get_recipients (const char *file, gpg
752      while (!quit && (rc = gpg_parse_packet (inp, pkt)) != -1) {      while (!quit && (rc = gpg_parse_packet (inp, pkt)) != -1) {
753          switch (pkt->pkttype) {          switch (pkt->pkttype) {
754          case PKT_PUBKEY_ENC:          case PKT_PUBKEY_ENC:
755              {PKT_pubkey_enc *enc = pkt->pkt.pubkey_enc;              enc = pkt->pkt.pubkey_enc;
756              if (!enc)              if (!enc)
757                  break;                  break;
758              l = calloc (1, sizeof *l);              l = (gpgme_recipient_t)calloc (1, sizeof *l);          
759              l->keyid = calloc (1, 16+1);              if (!l)
760                    BUG (NULL);
761                l->keyid = (char*)calloc (1, 16+1);
762              _snprintf (l->keyid, 16, "%08lX%08lX", enc->keyid[0], enc->keyid[1]);              _snprintf (l->keyid, 16, "%08lX%08lX", enc->keyid[0], enc->keyid[1]);
763              l->pubkey_algo = enc->pubkey_algo;              l->pubkey_algo = (gpgme_pubkey_algo_t)enc->pubkey_algo;
764              l->status = 0;              l->status = 0;
765              l->next = (*r_list);              l->next = (*r_list);
766              *r_list = l;              *r_list = l;
767              break;}              break;
768    
769          case PKT_ENCRYPTED:              case PKT_ENCRYPTED:    
770          case PKT_ENCRYPTED_MDC:          case PKT_ENCRYPTED_MDC:
# Line 696  do_get_recipients (const char *file, gpg Line 773  do_get_recipients (const char *file, gpg
773          case PKT_SECRET_KEY:          case PKT_SECRET_KEY:
774              quit = 1;              quit = 1;
775              break;              break;
776    
777            default:
778                break;
779          }          }
780          gpg_free_packet (pkt);          gpg_free_packet (pkt);
781          gpg_init_packet (pkt);          gpg_init_packet (pkt);
# Line 704  do_get_recipients (const char *file, gpg Line 784  do_get_recipients (const char *file, gpg
784      safe_free (pkt);      safe_free (pkt);
785      return 0;      return 0;
786  }  }
787  #endif  
788    
789    /* Either extract the list of recipients from the file @file or
790       if the string is NULL, try to extract them from the clipboard.
791       Return value: 0 on success. */
792    gpgme_error_t
793    gpg_get_recipients (const char *file, gpgme_recipient_t *r_list)
794    {
795        gpgme_error_t err;
796        char tmp[MAX_PATH+1];
797    
798        if (!file) {
799            clip_store_data (tmp, DIM (tmp)-2);
800            err = file_extract_recipient (tmp, r_list);
801            DeleteFile (tmp);
802        }
803        else
804            err = file_extract_recipient (file, r_list);
805        return err;
806    }
807    
808    
809    /* Try to find a subpacket with the given id @subpktid
810       inside the key @key.
811       Return value: 0 on success. */
812    gpgme_error_t
813    gpg_find_key_subpacket (const char *key, int subpktid,
814                            char **value)
815    {
816        gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
817        const char *fmt;
818        const char *spk;
819        char *p, *cmd;
820        HANDLE out;
821    
822        *value = NULL;
823        p = read_gpg_program ();
824        fmt = "%s --with-colons --list-options show-sig-subpackets=%d --list-sigs %s";
825        cmd = (char*)calloc (1, strlen (fmt) + strlen (p) + strlen (key) + 32 + 1);
826        sprintf (cmd, fmt, p, subpktid, key);
827    
828        out = create_tmpfile ("gpg_subpackets");
829        if (create_process (cmd, NULL, out, NULL))
830            err = gpg_error (GPG_ERR_INTERNAL);
831    
832        free (p);
833        free (cmd);
834    
835        p = map_tmpfile (out, NULL);
836        if (p && (spk=strstr (p, "spk"))) {
837            char *end = strstr (spk, "\n");
838            if (end) {
839                *value = (char*)calloc (1, (end-spk)+1);
840                memcpy (*value, spk, (end-spk)-1);
841            }
842        }
843    
844        free (p);
845        CloseHandle (out);
846        return err;
847    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26