LCOV - code coverage report
Current view: top level - g10 - export.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 541 0.0 %
Date: 2015-11-05 17:10:59 Functions: 0 14 0.0 %

          Line data    Source code
       1             : /* export.c - Export keys in the OpenPGP defined format.
       2             :  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
       3             :  *               2005, 2010 Free Software Foundation, Inc.
       4             :  * Copyright (C) 1998-2015  Werner Koch
       5             :  *
       6             :  * This file is part of GnuPG.
       7             :  *
       8             :  * GnuPG is free software; you can redistribute it and/or modify
       9             :  * it under the terms of the GNU General Public License as published by
      10             :  * the Free Software Foundation; either version 3 of the License, or
      11             :  * (at your option) any later version.
      12             :  *
      13             :  * GnuPG is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :  * GNU General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU General Public License
      19             :  * along with this program; if not, see <http://www.gnu.org/licenses/>.
      20             :  */
      21             : 
      22             : #include <config.h>
      23             : #include <stdio.h>
      24             : #include <stdlib.h>
      25             : #include <string.h>
      26             : #include <errno.h>
      27             : #include <assert.h>
      28             : 
      29             : #include "gpg.h"
      30             : #include "options.h"
      31             : #include "packet.h"
      32             : #include "status.h"
      33             : #include "keydb.h"
      34             : #include "util.h"
      35             : #include "main.h"
      36             : #include "i18n.h"
      37             : #include "trustdb.h"
      38             : #include "call-agent.h"
      39             : 
      40             : /* An object to keep track of subkeys. */
      41             : struct subkey_list_s
      42             : {
      43             :   struct subkey_list_s *next;
      44             :   u32 kid[2];
      45             : };
      46             : typedef struct subkey_list_s *subkey_list_t;
      47             : 
      48             : 
      49             : static int do_export (ctrl_t ctrl,
      50             :                       strlist_t users, int secret, unsigned int options );
      51             : static int do_export_stream (ctrl_t ctrl, iobuf_t out,
      52             :                              strlist_t users, int secret,
      53             :                              kbnode_t *keyblock_out, unsigned int options,
      54             :                              int *any);
      55             : 
      56             : 
      57             : int
      58           0 : parse_export_options(char *str,unsigned int *options,int noisy)
      59             : {
      60           0 :   struct parse_options export_opts[]=
      61             :     {
      62             :       {"export-local-sigs",EXPORT_LOCAL_SIGS,NULL,
      63             :        N_("export signatures that are marked as local-only")},
      64             :       {"export-attributes",EXPORT_ATTRIBUTES,NULL,
      65             :        N_("export attribute user IDs (generally photo IDs)")},
      66             :       {"export-sensitive-revkeys",EXPORT_SENSITIVE_REVKEYS,NULL,
      67             :        N_("export revocation keys marked as \"sensitive\"")},
      68             :       {"export-clean",EXPORT_CLEAN,NULL,
      69             :        N_("remove unusable parts from key during export")},
      70             :       {"export-minimal",EXPORT_MINIMAL|EXPORT_CLEAN,NULL,
      71             :        N_("remove as much as possible from key during export")},
      72             :       /* Aliases for backward compatibility */
      73             :       {"include-local-sigs",EXPORT_LOCAL_SIGS,NULL,NULL},
      74             :       {"include-attributes",EXPORT_ATTRIBUTES,NULL,NULL},
      75             :       {"include-sensitive-revkeys",EXPORT_SENSITIVE_REVKEYS,NULL,NULL},
      76             :       /* dummy */
      77             :       {"export-unusable-sigs",0,NULL,NULL},
      78             :       {"export-clean-sigs",0,NULL,NULL},
      79             :       {"export-clean-uids",0,NULL,NULL},
      80             :       {NULL,0,NULL,NULL}
      81             :       /* add tags for include revoked and disabled? */
      82             :     };
      83             : 
      84           0 :   return parse_options(str,options,export_opts,noisy);
      85             : }
      86             : 
      87             : 
      88             : /****************
      89             :  * Export the public keys (to standard out or --output).
      90             :  * Depending on opt.armor the output is armored.
      91             :  * options are defined in main.h.
      92             :  * If USERS is NULL, the complete ring will be exported.  */
      93             : int
      94           0 : export_pubkeys (ctrl_t ctrl, strlist_t users, unsigned int options )
      95             : {
      96           0 :   return do_export (ctrl, users, 0, options );
      97             : }
      98             : 
      99             : /****************
     100             :  * Export to an already opened stream; return -1 if no keys have
     101             :  * been exported
     102             :  */
     103             : int
     104           0 : export_pubkeys_stream (ctrl_t ctrl, iobuf_t out, strlist_t users,
     105             :                        kbnode_t *keyblock_out, unsigned int options )
     106             : {
     107             :   int any, rc;
     108             : 
     109           0 :   rc = do_export_stream (ctrl, out, users, 0, keyblock_out, options, &any);
     110           0 :   if (!rc && !any)
     111           0 :     rc = -1;
     112           0 :   return rc;
     113             : }
     114             : 
     115             : 
     116             : /*
     117             :  * Export a single key into a memory buffer.
     118             :  */
     119             : gpg_error_t
     120           0 : export_pubkey_buffer (ctrl_t ctrl, const char *keyspec, unsigned int options,
     121             :                       kbnode_t *r_keyblock, void **r_data, size_t *r_datalen)
     122             : {
     123             :   gpg_error_t err;
     124             :   iobuf_t iobuf;
     125             :   int any;
     126             :   strlist_t helplist;
     127             : 
     128           0 :   *r_keyblock = NULL;
     129           0 :   *r_data = NULL;
     130           0 :   *r_datalen = 0;
     131             : 
     132           0 :   helplist = NULL;
     133           0 :   if (!add_to_strlist_try (&helplist, keyspec))
     134           0 :     return gpg_error_from_syserror ();
     135             : 
     136           0 :   iobuf = iobuf_temp ();
     137           0 :   err = do_export_stream (ctrl, iobuf, helplist, 0, r_keyblock, options, &any);
     138           0 :   if (!err && !any)
     139           0 :     err = gpg_error (GPG_ERR_NOT_FOUND);
     140           0 :   if (!err)
     141             :     {
     142             :       const void *src;
     143             :       size_t datalen;
     144             : 
     145           0 :       iobuf_flush_temp (iobuf);
     146           0 :       src = iobuf_get_temp_buffer (iobuf);
     147           0 :       datalen = iobuf_get_temp_length (iobuf);
     148           0 :       if (!datalen)
     149           0 :         err = gpg_error (GPG_ERR_NO_PUBKEY);
     150           0 :       else if (!(*r_data = xtrymalloc (datalen)))
     151           0 :         err = gpg_error_from_syserror ();
     152             :       else
     153             :         {
     154           0 :           memcpy (*r_data, src, datalen);
     155           0 :           *r_datalen = datalen;
     156             :         }
     157             :     }
     158           0 :   iobuf_close (iobuf);
     159           0 :   free_strlist (helplist);
     160           0 :   if (err && *r_keyblock)
     161             :     {
     162           0 :       release_kbnode (*r_keyblock);
     163           0 :       *r_keyblock = NULL;
     164             :     }
     165           0 :   return err;
     166             : }
     167             : 
     168             : 
     169             : int
     170           0 : export_seckeys (ctrl_t ctrl, strlist_t users )
     171             : {
     172           0 :   return do_export (ctrl, users, 1, 0);
     173             : }
     174             : 
     175             : int
     176           0 : export_secsubkeys (ctrl_t ctrl, strlist_t users )
     177             : {
     178           0 :   return do_export (ctrl, users, 2, 0);
     179             : }
     180             : 
     181             : 
     182             : /* Export the keys identified by the list of strings in USERS.  If
     183             :    Secret is false public keys will be exported.  With secret true
     184             :    secret keys will be exported; in this case 1 means the entire
     185             :    secret keyblock and 2 only the subkeys.  OPTIONS are the export
     186             :    options to apply.  */
     187             : static int
     188           0 : do_export (ctrl_t ctrl, strlist_t users, int secret, unsigned int options )
     189             : {
     190           0 :   IOBUF out = NULL;
     191             :   int any, rc;
     192           0 :   armor_filter_context_t *afx = NULL;
     193             :   compress_filter_context_t zfx;
     194             : 
     195           0 :   memset( &zfx, 0, sizeof zfx);
     196             : 
     197           0 :   rc = open_outfile (-1, NULL, 0, !!secret, &out );
     198           0 :   if (rc)
     199           0 :     return rc;
     200             : 
     201           0 :   if ( opt.armor )
     202             :     {
     203           0 :       afx = new_armor_context ();
     204           0 :       afx->what = secret? 5 : 1;
     205           0 :       push_armor_filter (afx, out);
     206             :     }
     207             : 
     208           0 :   rc = do_export_stream (ctrl, out, users, secret, NULL, options, &any );
     209             : 
     210           0 :   if ( rc || !any )
     211           0 :     iobuf_cancel (out);
     212             :   else
     213           0 :     iobuf_close (out);
     214           0 :   release_armor_context (afx);
     215           0 :   return rc;
     216             : }
     217             : 
     218             : 
     219             : 
     220             : /* Release an entire subkey list. */
     221             : static void
     222           0 : release_subkey_list (subkey_list_t list)
     223             : {
     224           0 :   while (list)
     225             :     {
     226           0 :       subkey_list_t tmp = list->next;;
     227           0 :       xfree (list);
     228           0 :       list = tmp;
     229             :     }
     230           0 : }
     231             : 
     232             : 
     233             : /* Returns true if NODE is a subkey and contained in LIST. */
     234             : static int
     235           0 : subkey_in_list_p (subkey_list_t list, KBNODE node)
     236             : {
     237           0 :   if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
     238           0 :       || node->pkt->pkttype == PKT_SECRET_SUBKEY )
     239             :     {
     240             :       u32 kid[2];
     241             : 
     242           0 :       keyid_from_pk (node->pkt->pkt.public_key, kid);
     243             : 
     244           0 :       for (; list; list = list->next)
     245           0 :         if (list->kid[0] == kid[0] && list->kid[1] == kid[1])
     246           0 :           return 1;
     247             :     }
     248           0 :   return 0;
     249             : }
     250             : 
     251             : /* Allocate a new subkey list item from NODE. */
     252             : static subkey_list_t
     253           0 : new_subkey_list_item (KBNODE node)
     254             : {
     255           0 :   subkey_list_t list = xcalloc (1, sizeof *list);
     256             : 
     257           0 :   if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
     258           0 :       || node->pkt->pkttype == PKT_SECRET_SUBKEY)
     259           0 :     keyid_from_pk (node->pkt->pkt.public_key, list->kid);
     260             : 
     261           0 :   return list;
     262             : }
     263             : 
     264             : 
     265             : /* Helper function to check whether the subkey at NODE actually
     266             :    matches the description at DESC.  The function returns true if the
     267             :    key under question has been specified by an exact specification
     268             :    (keyID or fingerprint) and does match the one at NODE.  It is
     269             :    assumed that the packet at NODE is either a public or secret
     270             :    subkey. */
     271             : static int
     272           0 : exact_subkey_match_p (KEYDB_SEARCH_DESC *desc, KBNODE node)
     273             : {
     274             :   u32 kid[2];
     275             :   byte fpr[MAX_FINGERPRINT_LEN];
     276             :   size_t fprlen;
     277           0 :   int result = 0;
     278             : 
     279           0 :   switch(desc->mode)
     280             :     {
     281             :     case KEYDB_SEARCH_MODE_SHORT_KID:
     282             :     case KEYDB_SEARCH_MODE_LONG_KID:
     283           0 :       keyid_from_pk (node->pkt->pkt.public_key, kid);
     284           0 :       break;
     285             : 
     286             :     case KEYDB_SEARCH_MODE_FPR16:
     287             :     case KEYDB_SEARCH_MODE_FPR20:
     288             :     case KEYDB_SEARCH_MODE_FPR:
     289           0 :       fingerprint_from_pk (node->pkt->pkt.public_key, fpr,&fprlen);
     290           0 :       break;
     291             : 
     292             :     default:
     293           0 :       break;
     294             :     }
     295             : 
     296           0 :   switch(desc->mode)
     297             :     {
     298             :     case KEYDB_SEARCH_MODE_SHORT_KID:
     299           0 :       if (desc->u.kid[1] == kid[1])
     300           0 :         result = 1;
     301           0 :       break;
     302             : 
     303             :     case KEYDB_SEARCH_MODE_LONG_KID:
     304           0 :       if (desc->u.kid[0] == kid[0] && desc->u.kid[1] == kid[1])
     305           0 :         result = 1;
     306           0 :       break;
     307             : 
     308             :     case KEYDB_SEARCH_MODE_FPR16:
     309           0 :       if (!memcmp (desc->u.fpr, fpr, 16))
     310           0 :         result = 1;
     311           0 :       break;
     312             : 
     313             :     case KEYDB_SEARCH_MODE_FPR20:
     314             :     case KEYDB_SEARCH_MODE_FPR:
     315           0 :       if (!memcmp (desc->u.fpr, fpr, 20))
     316           0 :         result = 1;
     317           0 :       break;
     318             : 
     319             :     default:
     320           0 :       break;
     321             :     }
     322             : 
     323           0 :   return result;
     324             : }
     325             : 
     326             : 
     327             : /* Return a canonicalized public key algoithms.  This is used to
     328             :    compare different flavors of algorithms (e.g. ELG and ELG_E are
     329             :    considered the same).  */
     330             : static enum gcry_pk_algos
     331           0 : canon_pk_algo (enum gcry_pk_algos algo)
     332             : {
     333           0 :   switch (algo)
     334             :     {
     335             :     case GCRY_PK_RSA:
     336             :     case GCRY_PK_RSA_E:
     337           0 :     case GCRY_PK_RSA_S: return GCRY_PK_RSA;
     338             :     case GCRY_PK_ELG:
     339           0 :     case GCRY_PK_ELG_E: return GCRY_PK_ELG;
     340             :     case GCRY_PK_ECC:
     341             :     case GCRY_PK_ECDSA:
     342           0 :     case GCRY_PK_ECDH: return GCRY_PK_ECC;
     343           0 :     default: return algo;
     344             :     }
     345             : }
     346             : 
     347             : 
     348             : /* Use the key transfer format given in S_PGP to create the secinfo
     349             :    structure in PK and change the parameter array in PK to include the
     350             :    secret parameters.  */
     351             : static gpg_error_t
     352           0 : transfer_format_to_openpgp (gcry_sexp_t s_pgp, PKT_public_key *pk)
     353             : {
     354             :   gpg_error_t err;
     355             :   gcry_sexp_t top_list;
     356           0 :   gcry_sexp_t list = NULL;
     357           0 :   char *curve = NULL;
     358             :   const char *value;
     359             :   size_t valuelen;
     360             :   char *string;
     361             :   int  idx;
     362             :   int  is_v4, is_protected;
     363             :   enum gcry_pk_algos pk_algo;
     364           0 :   int  protect_algo = 0;
     365             :   char iv[16];
     366           0 :   int  ivlen = 0;
     367           0 :   int  s2k_mode = 0;
     368           0 :   int  s2k_algo = 0;
     369             :   byte s2k_salt[8];
     370           0 :   u32  s2k_count = 0;
     371           0 :   int  is_ecdh = 0;
     372             :   size_t npkey, nskey;
     373             :   gcry_mpi_t skey[10];  /* We support up to 9 parameters.  */
     374           0 :   int skeyidx = 0;
     375             :   struct seckey_info *ski;
     376             : 
     377             :   /* gcry_log_debugsxp ("transferkey", s_pgp); */
     378           0 :   top_list = gcry_sexp_find_token (s_pgp, "openpgp-private-key", 0);
     379           0 :   if (!top_list)
     380           0 :     goto bad_seckey;
     381             : 
     382           0 :   list = gcry_sexp_find_token (top_list, "version", 0);
     383           0 :   if (!list)
     384           0 :     goto bad_seckey;
     385           0 :   value = gcry_sexp_nth_data (list, 1, &valuelen);
     386           0 :   if (!value || valuelen != 1 || !(value[0] == '3' || value[0] == '4'))
     387             :     goto bad_seckey;
     388           0 :   is_v4 = (value[0] == '4');
     389             : 
     390           0 :   gcry_sexp_release (list);
     391           0 :   list = gcry_sexp_find_token (top_list, "protection", 0);
     392           0 :   if (!list)
     393           0 :     goto bad_seckey;
     394           0 :   value = gcry_sexp_nth_data (list, 1, &valuelen);
     395           0 :   if (!value)
     396           0 :     goto bad_seckey;
     397           0 :   if (valuelen == 4 && !memcmp (value, "sha1", 4))
     398           0 :     is_protected = 2;
     399           0 :   else if (valuelen == 3 && !memcmp (value, "sum", 3))
     400           0 :     is_protected = 1;
     401           0 :   else if (valuelen == 4 && !memcmp (value, "none", 4))
     402           0 :     is_protected = 0;
     403             :   else
     404             :     goto bad_seckey;
     405           0 :   if (is_protected)
     406             :     {
     407           0 :       string = gcry_sexp_nth_string (list, 2);
     408           0 :       if (!string)
     409           0 :         goto bad_seckey;
     410           0 :       protect_algo = gcry_cipher_map_name (string);
     411           0 :       xfree (string);
     412             : 
     413           0 :       value = gcry_sexp_nth_data (list, 3, &valuelen);
     414           0 :       if (!value || !valuelen || valuelen > sizeof iv)
     415             :         goto bad_seckey;
     416           0 :       memcpy (iv, value, valuelen);
     417           0 :       ivlen = valuelen;
     418             : 
     419           0 :       string = gcry_sexp_nth_string (list, 4);
     420           0 :       if (!string)
     421           0 :         goto bad_seckey;
     422           0 :       s2k_mode = strtol (string, NULL, 10);
     423           0 :       xfree (string);
     424             : 
     425           0 :       string = gcry_sexp_nth_string (list, 5);
     426           0 :       if (!string)
     427           0 :         goto bad_seckey;
     428           0 :       s2k_algo = gcry_md_map_name (string);
     429           0 :       xfree (string);
     430             : 
     431           0 :       value = gcry_sexp_nth_data (list, 6, &valuelen);
     432           0 :       if (!value || !valuelen || valuelen > sizeof s2k_salt)
     433             :         goto bad_seckey;
     434           0 :       memcpy (s2k_salt, value, valuelen);
     435             : 
     436           0 :       string = gcry_sexp_nth_string (list, 7);
     437           0 :       if (!string)
     438           0 :         goto bad_seckey;
     439           0 :       s2k_count = strtoul (string, NULL, 10);
     440           0 :       xfree (string);
     441             :     }
     442             : 
     443             :   /* Parse the gcrypt PK algo and check that it is okay.  */
     444           0 :   gcry_sexp_release (list);
     445           0 :   list = gcry_sexp_find_token (top_list, "algo", 0);
     446           0 :   if (!list)
     447           0 :     goto bad_seckey;
     448           0 :   string = gcry_sexp_nth_string (list, 1);
     449           0 :   if (!string)
     450           0 :     goto bad_seckey;
     451           0 :   pk_algo = gcry_pk_map_name (string);
     452           0 :   xfree (string); string = NULL;
     453           0 :   if (gcry_pk_algo_info (pk_algo, GCRYCTL_GET_ALGO_NPKEY, NULL, &npkey)
     454           0 :       || gcry_pk_algo_info (pk_algo, GCRYCTL_GET_ALGO_NSKEY, NULL, &nskey)
     455           0 :       || !npkey || npkey >= nskey)
     456             :     goto bad_seckey;
     457             : 
     458             :   /* Check that the pubkey algo matches the one from the public key.  */
     459           0 :   switch (canon_pk_algo (pk_algo))
     460             :     {
     461             :     case GCRY_PK_RSA:
     462           0 :       if (!is_RSA (pk->pubkey_algo))
     463           0 :         pk_algo = 0;  /* Does not match.  */
     464           0 :       break;
     465             :     case GCRY_PK_DSA:
     466           0 :       if (!is_DSA (pk->pubkey_algo))
     467           0 :         pk_algo = 0;  /* Does not match.  */
     468           0 :       break;
     469             :     case GCRY_PK_ELG:
     470           0 :       if (!is_ELGAMAL (pk->pubkey_algo))
     471           0 :         pk_algo = 0;  /* Does not match.  */
     472           0 :       break;
     473             :     case GCRY_PK_ECC:
     474           0 :       if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA)
     475             :         ;
     476           0 :       else if (pk->pubkey_algo == PUBKEY_ALGO_ECDH)
     477           0 :         is_ecdh = 1;
     478           0 :       else if (pk->pubkey_algo == PUBKEY_ALGO_EDDSA)
     479             :         ;
     480             :       else
     481           0 :         pk_algo = 0;  /* Does not match.  */
     482             :       /* For ECC we do not have the domain parameters thus fix our info.  */
     483           0 :       npkey = 1;
     484           0 :       nskey = 2;
     485           0 :       break;
     486             :     default:
     487           0 :       pk_algo = 0;   /* Oops.  */
     488           0 :       break;
     489             :     }
     490           0 :   if (!pk_algo)
     491             :     {
     492           0 :       err = gpg_error (GPG_ERR_PUBKEY_ALGO);
     493           0 :       goto leave;
     494             :     }
     495             : 
     496             :   /* This check has to go after the ecc adjustments. */
     497           0 :   if (nskey > PUBKEY_MAX_NSKEY)
     498           0 :     goto bad_seckey;
     499             : 
     500             :   /* Parse the key parameters.  */
     501           0 :   gcry_sexp_release (list);
     502           0 :   list = gcry_sexp_find_token (top_list, "skey", 0);
     503           0 :   if (!list)
     504           0 :     goto bad_seckey;
     505           0 :   for (idx=0;;)
     506             :     {
     507             :       int is_enc;
     508             : 
     509           0 :       value = gcry_sexp_nth_data (list, ++idx, &valuelen);
     510           0 :       if (!value && skeyidx >= npkey)
     511           0 :         break;  /* Ready.  */
     512             : 
     513             :       /* Check for too many parameters.  Note that depending on the
     514             :          protection mode and version number we may see less than NSKEY
     515             :          (but at least NPKEY+1) parameters.  */
     516           0 :       if (idx >= 2*nskey)
     517           0 :         goto bad_seckey;
     518           0 :       if (skeyidx >= DIM (skey)-1)
     519           0 :         goto bad_seckey;
     520             : 
     521           0 :       if (!value || valuelen != 1 || !(value[0] == '_' || value[0] == 'e'))
     522             :         goto bad_seckey;
     523           0 :       is_enc = (value[0] == 'e');
     524           0 :       value = gcry_sexp_nth_data (list, ++idx, &valuelen);
     525           0 :       if (!value || !valuelen)
     526             :         goto bad_seckey;
     527           0 :       if (is_enc)
     528             :         {
     529           0 :           void *p = xtrymalloc (valuelen);
     530           0 :           if (!p)
     531           0 :             goto outofmem;
     532           0 :           memcpy (p, value, valuelen);
     533           0 :           skey[skeyidx] = gcry_mpi_set_opaque (NULL, p, valuelen*8);
     534           0 :           if (!skey[skeyidx])
     535           0 :             goto outofmem;
     536             :         }
     537             :       else
     538             :         {
     539           0 :           if (gcry_mpi_scan (skey + skeyidx, GCRYMPI_FMT_STD,
     540             :                              value, valuelen, NULL))
     541           0 :             goto bad_seckey;
     542             :         }
     543           0 :       skeyidx++;
     544           0 :     }
     545           0 :   skey[skeyidx++] = NULL;
     546             : 
     547           0 :   gcry_sexp_release (list); list = NULL;
     548             : 
     549             :   /* We have no need for the CSUM value thus we don't parse it.  */
     550             :   /* list = gcry_sexp_find_token (top_list, "csum", 0); */
     551             :   /* if (list) */
     552             :   /*   { */
     553             :   /*     string = gcry_sexp_nth_string (list, 1); */
     554             :   /*     if (!string) */
     555             :   /*       goto bad_seckey; */
     556             :   /*     desired_csum = strtoul (string, NULL, 10); */
     557             :   /*     xfree (string); */
     558             :   /*   } */
     559             :   /* else */
     560             :   /*   desired_csum = 0; */
     561             :   /* gcry_sexp_release (list); list = NULL; */
     562             : 
     563             :   /* Get the curve name if any,  */
     564           0 :   list = gcry_sexp_find_token (top_list, "curve", 0);
     565           0 :   if (list)
     566             :     {
     567           0 :       curve = gcry_sexp_nth_string (list, 1);
     568           0 :       gcry_sexp_release (list); list = NULL;
     569             :     }
     570             : 
     571           0 :   gcry_sexp_release (top_list); top_list = NULL;
     572             : 
     573             :   /* log_debug ("XXX is_v4=%d\n", is_v4); */
     574             :   /* log_debug ("XXX pubkey_algo=%d\n", pubkey_algo); */
     575             :   /* log_debug ("XXX is_protected=%d\n", is_protected); */
     576             :   /* log_debug ("XXX protect_algo=%d\n", protect_algo); */
     577             :   /* log_printhex ("XXX iv", iv, ivlen); */
     578             :   /* log_debug ("XXX ivlen=%d\n", ivlen); */
     579             :   /* log_debug ("XXX s2k_mode=%d\n", s2k_mode); */
     580             :   /* log_debug ("XXX s2k_algo=%d\n", s2k_algo); */
     581             :   /* log_printhex ("XXX s2k_salt", s2k_salt, sizeof s2k_salt); */
     582             :   /* log_debug ("XXX s2k_count=%lu\n", (unsigned long)s2k_count); */
     583             :   /* for (idx=0; skey[idx]; idx++) */
     584             :   /*   { */
     585             :   /*     int is_enc = gcry_mpi_get_flag (skey[idx], GCRYMPI_FLAG_OPAQUE); */
     586             :   /*     log_info ("XXX skey[%d]%s:", idx, is_enc? " (enc)":""); */
     587             :   /*     if (is_enc) */
     588             :   /*       { */
     589             :   /*         void *p; */
     590             :   /*         unsigned int nbits; */
     591             :   /*         p = gcry_mpi_get_opaque (skey[idx], &nbits); */
     592             :   /*         log_printhex (NULL, p, (nbits+7)/8); */
     593             :   /*       } */
     594             :   /*     else */
     595             :   /*       gcry_mpi_dump (skey[idx]); */
     596             :   /*     log_printf ("\n"); */
     597             :   /*   } */
     598             : 
     599           0 :   if (!is_v4 || is_protected != 2 )
     600             :     {
     601             :       /* We only support the v4 format and a SHA-1 checksum.  */
     602           0 :       err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
     603           0 :       goto leave;
     604             :     }
     605             : 
     606             :   /* We need to change the received parameters for ECC algorithms.
     607             :      The transfer format has the curve name and the parameters
     608             :      separate.  We put them all into the SKEY array.  */
     609           0 :   if (canon_pk_algo (pk_algo) == GCRY_PK_ECC)
     610             :     {
     611             :       const char *oidstr;
     612             : 
     613             :       /* Assert that all required parameters are available.  We also
     614             :          check that the array does not contain more parameters than
     615             :          needed (this was used by some beta versions of 2.1.  */
     616           0 :       if (!curve || !skey[0] || !skey[1] || skey[2])
     617             :         {
     618           0 :           err = gpg_error (GPG_ERR_INTERNAL);
     619           0 :           goto leave;
     620             :         }
     621             : 
     622           0 :       oidstr = openpgp_curve_to_oid (curve, NULL);
     623           0 :       if (!oidstr)
     624             :         {
     625           0 :           log_error ("no OID known for curve '%s'\n", curve);
     626           0 :           err = gpg_error (GPG_ERR_UNKNOWN_CURVE);
     627           0 :           goto leave;
     628             :         }
     629             :       /* Put the curve's OID into into the MPI array.  This requires
     630             :          that we shift Q and D.  For ECDH also insert the KDF parms. */
     631           0 :       if (is_ecdh)
     632             :         {
     633           0 :           skey[4] = NULL;
     634           0 :           skey[3] = skey[1];
     635           0 :           skey[2] = gcry_mpi_copy (pk->pkey[2]);
     636             :         }
     637             :       else
     638             :         {
     639           0 :           skey[3] = NULL;
     640           0 :           skey[2] = skey[1];
     641             :         }
     642           0 :       skey[1] = skey[0];
     643           0 :       skey[0] = NULL;
     644           0 :       err = openpgp_oid_from_str (oidstr, skey + 0);
     645           0 :       if (err)
     646           0 :         goto leave;
     647             :       /* Fixup the NPKEY and NSKEY to match OpenPGP reality.  */
     648           0 :       npkey = 2 + is_ecdh;
     649           0 :       nskey = 3 + is_ecdh;
     650             : 
     651             :       /* for (idx=0; skey[idx]; idx++) */
     652             :       /*   { */
     653             :       /*     log_info ("YYY skey[%d]:", idx); */
     654             :       /*     if (gcry_mpi_get_flag (skey[idx], GCRYMPI_FLAG_OPAQUE)) */
     655             :       /*       { */
     656             :       /*         void *p; */
     657             :       /*         unsigned int nbits; */
     658             :       /*         p = gcry_mpi_get_opaque (skey[idx], &nbits); */
     659             :       /*         log_printhex (NULL, p, (nbits+7)/8); */
     660             :       /*       } */
     661             :       /*     else */
     662             :       /*       gcry_mpi_dump (skey[idx]); */
     663             :       /*     log_printf ("\n"); */
     664             :       /*   } */
     665             :     }
     666             : 
     667             :   /* Do some sanity checks.  */
     668           0 :   if (s2k_count > 255)
     669             :     {
     670             :       /* We expect an already encoded S2K count.  */
     671           0 :       err = gpg_error (GPG_ERR_INV_DATA);
     672           0 :       goto leave;
     673             :     }
     674           0 :   err = openpgp_cipher_test_algo (protect_algo);
     675           0 :   if (err)
     676           0 :     goto leave;
     677           0 :   err = openpgp_md_test_algo (s2k_algo);
     678           0 :   if (err)
     679           0 :     goto leave;
     680             : 
     681             :   /* Check that the public key parameters match.  Note that since
     682             :      Libgcrypt 1.5 gcry_mpi_cmp handles opaque MPI correctly.  */
     683           0 :   for (idx=0; idx < npkey; idx++)
     684           0 :     if (gcry_mpi_cmp (pk->pkey[idx], skey[idx]))
     685             :       {
     686           0 :         err = gpg_error (GPG_ERR_BAD_PUBKEY);
     687           0 :         goto leave;
     688             :       }
     689             : 
     690             :   /* Check that the first secret key parameter in SKEY is encrypted
     691             :      and that there are no more secret key parameters.  The latter is
     692             :      guaranteed by the v4 packet format.  */
     693           0 :   if (!gcry_mpi_get_flag (skey[npkey], GCRYMPI_FLAG_OPAQUE))
     694           0 :     goto bad_seckey;
     695           0 :   if (npkey+1 < DIM (skey) && skey[npkey+1])
     696           0 :     goto bad_seckey;
     697             : 
     698             :   /* Check that the secret key parameters in PK are all set to NULL. */
     699           0 :   for (idx=npkey; idx < nskey; idx++)
     700           0 :     if (pk->pkey[idx])
     701           0 :       goto bad_seckey;
     702             : 
     703             :   /* Now build the protection info. */
     704           0 :   pk->seckey_info = ski = xtrycalloc (1, sizeof *ski);
     705           0 :   if (!ski)
     706             :     {
     707           0 :       err = gpg_error_from_syserror ();
     708           0 :       goto leave;
     709             :     }
     710             : 
     711           0 :   ski->is_protected = 1;
     712           0 :   ski->sha1chk = 1;
     713           0 :   ski->algo = protect_algo;
     714           0 :   ski->s2k.mode = s2k_mode;
     715           0 :   ski->s2k.hash_algo = s2k_algo;
     716             :   assert (sizeof ski->s2k.salt == sizeof s2k_salt);
     717           0 :   memcpy (ski->s2k.salt, s2k_salt, sizeof s2k_salt);
     718           0 :   ski->s2k.count = s2k_count;
     719           0 :   assert (ivlen <= sizeof ski->iv);
     720           0 :   memcpy (ski->iv, iv, ivlen);
     721           0 :   ski->ivlen = ivlen;
     722             : 
     723             :   /* Store the protected secret key parameter.  */
     724           0 :   pk->pkey[npkey] = skey[npkey];
     725           0 :   skey[npkey] = NULL;
     726             : 
     727             :   /* That's it.  */
     728             : 
     729             :  leave:
     730           0 :   gcry_free (curve);
     731           0 :   gcry_sexp_release (list);
     732           0 :   gcry_sexp_release (top_list);
     733           0 :   for (idx=0; idx < skeyidx; idx++)
     734           0 :     gcry_mpi_release (skey[idx]);
     735           0 :   return err;
     736             : 
     737             :  bad_seckey:
     738           0 :   err = gpg_error (GPG_ERR_BAD_SECKEY);
     739           0 :   goto leave;
     740             : 
     741             :  outofmem:
     742           0 :   err = gpg_error (GPG_ERR_ENOMEM);
     743           0 :   goto leave;
     744             : }
     745             : 
     746             : /* Export the keys identified by the list of strings in USERS to the
     747             :    stream OUT.  If Secret is false public keys will be exported.  With
     748             :    secret true secret keys will be exported; in this case 1 means the
     749             :    entire secret keyblock and 2 only the subkeys.  OPTIONS are the
     750             :    export options to apply.  If KEYBLOCK_OUT is not NULL, AND the exit
     751             :    code is zero, a pointer to the first keyblock found and exported
     752             :    will be stored at this address; no other keyblocks are exported in
     753             :    this case.  The caller must free the returned keyblock.  If any
     754             :    key has been exported true is stored at ANY. */
     755             : static int
     756           0 : do_export_stream (ctrl_t ctrl, iobuf_t out, strlist_t users, int secret,
     757             :                   kbnode_t *keyblock_out, unsigned int options, int *any)
     758             : {
     759           0 :   gpg_error_t err = 0;
     760             :   PACKET pkt;
     761           0 :   KBNODE keyblock = NULL;
     762             :   KBNODE kbctx, node;
     763             :   size_t ndesc, descindex;
     764           0 :   KEYDB_SEARCH_DESC *desc = NULL;
     765           0 :   subkey_list_t subkey_list = NULL;  /* Track already processed subkeys. */
     766             :   KEYDB_HANDLE kdbhd;
     767             :   strlist_t sl;
     768           0 :   gcry_cipher_hd_t cipherhd = NULL;
     769           0 :   char *cache_nonce = NULL;
     770             : 
     771           0 :   *any = 0;
     772           0 :   init_packet (&pkt);
     773           0 :   kdbhd = keydb_new ();
     774             : 
     775             :   /* For the DANE format override the options.  */
     776           0 :   if ((options & EXPORT_DANE_FORMAT))
     777           0 :     options = (EXPORT_DANE_FORMAT | EXPORT_MINIMAL | EXPORT_CLEAN);
     778             : 
     779             : 
     780           0 :   if (!users)
     781             :     {
     782           0 :       ndesc = 1;
     783           0 :       desc = xcalloc (ndesc, sizeof *desc);
     784           0 :       desc[0].mode = KEYDB_SEARCH_MODE_FIRST;
     785             :     }
     786             :   else
     787             :     {
     788           0 :       for (ndesc=0, sl=users; sl; sl = sl->next, ndesc++)
     789             :         ;
     790           0 :       desc = xmalloc ( ndesc * sizeof *desc);
     791             : 
     792           0 :       for (ndesc=0, sl=users; sl; sl = sl->next)
     793             :         {
     794           0 :           if (!(err=classify_user_id (sl->d, desc+ndesc, 1)))
     795           0 :             ndesc++;
     796             :           else
     797           0 :             log_error (_("key \"%s\" not found: %s\n"),
     798           0 :                        sl->d, gpg_strerror (err));
     799             :         }
     800             : 
     801           0 :       keydb_disable_caching (kdbhd);  /* We are looping the search.  */
     802             : 
     803             :       /* It would be nice to see which of the given users did actually
     804             :          match one in the keyring.  To implement this we need to have
     805             :          a found flag for each entry in desc.  To set this flag we
     806             :          must check all those entries after a match to mark all
     807             :          matched one - currently we stop at the first match.  To do
     808             :          this we need an extra flag to enable this feature.  */
     809             :     }
     810             : 
     811             : #ifdef ENABLE_SELINUX_HACKS
     812             :   if (secret)
     813             :     {
     814             :       log_error (_("exporting secret keys not allowed\n"));
     815             :       err = gpg_error (GPG_ERR_NOT_SUPPORTED);
     816             :       goto leave;
     817             :     }
     818             : #endif
     819             : 
     820             :   /* For secret key export we need to setup a decryption context.  */
     821           0 :   if (secret)
     822             :     {
     823           0 :       void *kek = NULL;
     824             :       size_t keklen;
     825             : 
     826           0 :       err = agent_keywrap_key (ctrl, 1, &kek, &keklen);
     827           0 :       if (err)
     828             :         {
     829           0 :           log_error ("error getting the KEK: %s\n", gpg_strerror (err));
     830           0 :           goto leave;
     831             :         }
     832             : 
     833             :       /* Prepare a cipher context.  */
     834           0 :       err = gcry_cipher_open (&cipherhd, GCRY_CIPHER_AES128,
     835             :                               GCRY_CIPHER_MODE_AESWRAP, 0);
     836           0 :       if (!err)
     837           0 :         err = gcry_cipher_setkey (cipherhd, kek, keklen);
     838           0 :       if (err)
     839             :         {
     840           0 :           log_error ("error setting up an encryption context: %s\n",
     841             :                      gpg_strerror (err));
     842           0 :           goto leave;
     843             :         }
     844           0 :       xfree (kek);
     845           0 :       kek = NULL;
     846             :     }
     847             : 
     848             :   for (;;)
     849             :     {
     850           0 :       int skip_until_subkey = 0;
     851             :       u32 keyid[2];
     852             :       PKT_public_key *pk;
     853             : 
     854           0 :       err = keydb_search (kdbhd, desc, ndesc, &descindex);
     855           0 :       if (!users)
     856           0 :         desc[0].mode = KEYDB_SEARCH_MODE_NEXT;
     857           0 :       if (gpg_err_code (err) == GPG_ERR_LEGACY_KEY)
     858           0 :         continue;  /* Skip PGP2 keys.  */
     859           0 :       if (err)
     860           0 :         break;
     861             : 
     862             :       /* Read the keyblock. */
     863           0 :       release_kbnode (keyblock);
     864           0 :       keyblock = NULL;
     865           0 :       err = keydb_get_keyblock (kdbhd, &keyblock);
     866           0 :       if (gpg_err_code (err) == GPG_ERR_LEGACY_KEY)
     867           0 :         continue;  /* Skip PGP2 keys.  */
     868           0 :       if (err)
     869             :         {
     870           0 :           log_error (_("error reading keyblock: %s\n"), gpg_strerror (err));
     871           0 :           goto leave;
     872             :         }
     873             : 
     874           0 :       node = find_kbnode (keyblock, PKT_PUBLIC_KEY);
     875           0 :       if (!node)
     876             :         {
     877           0 :           log_error ("public key packet not found in keyblock - skipped\n");
     878           0 :           continue;
     879             :         }
     880           0 :       setup_main_keyids (keyblock);  /* gpg_format_keydesc needs it.  */
     881           0 :       pk = node->pkt->pkt.public_key;
     882           0 :       keyid_from_pk (pk, keyid);
     883             : 
     884             :       /* If a secret key export is required we need to check whether
     885             :          we have a secret key at all and if so create the seckey_info
     886             :          structure.  */
     887           0 :       if (secret)
     888             :         {
     889           0 :           if (agent_probe_any_secret_key (ctrl, keyblock))
     890           0 :             continue;  /* No secret key (neither primary nor subkey).  */
     891             : 
     892             :           /* No v3 keys with GNU mode 1001. */
     893           0 :           if (secret == 2 && pk->version == 3)
     894             :             {
     895           0 :               log_info (_("key %s: PGP 2.x style key - skipped\n"),
     896             :                         keystr (keyid));
     897           0 :               continue;
     898             :             }
     899             : 
     900             :           /* The agent does not yet allow to export v3 packets.  It is
     901             :              actually questionable whether we should allow them at
     902             :              all.  */
     903           0 :           if (pk->version == 3)
     904             :             {
     905           0 :               log_info ("key %s: PGP 2.x style key (v3) export "
     906             :                         "not yet supported - skipped\n", keystr (keyid));
     907           0 :               continue;
     908             :             }
     909             :         }
     910             : 
     911             :       /* Always do the cleaning on the public key part if requested.
     912             :          Note that we don't yet set this option if we are exporting
     913             :          secret keys.  Note that both export-clean and export-minimal
     914             :          only apply to UID sigs (0x10, 0x11, 0x12, and 0x13).  A
     915             :          designated revocation is never stripped, even with
     916             :          export-minimal set.  */
     917           0 :       if ((options & EXPORT_CLEAN))
     918           0 :         clean_key (keyblock, opt.verbose, (options&EXPORT_MINIMAL), NULL, NULL);
     919             : 
     920             :       /* And write it. */
     921           0 :       xfree (cache_nonce);
     922           0 :       cache_nonce = NULL;
     923           0 :       for (kbctx=NULL; (node = walk_kbnode (keyblock, &kbctx, 0)); )
     924             :         {
     925           0 :           if (skip_until_subkey)
     926             :             {
     927           0 :               if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
     928           0 :                 skip_until_subkey = 0;
     929             :               else
     930           0 :                 continue;
     931             :             }
     932             : 
     933             :           /* We used to use comment packets, but not any longer.  In
     934             :              case we still have comments on a key, strip them here
     935             :              before we call build_packet(). */
     936           0 :           if (node->pkt->pkttype == PKT_COMMENT)
     937           0 :             continue;
     938             : 
     939             :           /* Make sure that ring_trust packets never get exported. */
     940           0 :           if (node->pkt->pkttype == PKT_RING_TRUST)
     941           0 :             continue;
     942             : 
     943             :           /* If exact is set, then we only export what was requested
     944             :              (plus the primary key, if the user didn't specifically
     945             :              request it). */
     946           0 :           if (desc[descindex].exact
     947           0 :               && node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
     948             :             {
     949           0 :               if (!exact_subkey_match_p (desc+descindex, node))
     950             :                 {
     951             :                   /* Before skipping this subkey, check whether any
     952             :                      other description wants an exact match on a
     953             :                      subkey and include that subkey into the output
     954             :                      too.  Need to add this subkey to a list so that
     955             :                      it won't get processed a second time.
     956             : 
     957             :                      So the first step here is to check that list and
     958             :                      skip in any case if the key is in that list.
     959             : 
     960             :                      We need this whole mess because the import
     961             :                      function of GnuPG < 2.1 is not able to merge
     962             :                      secret keys and thus it is useless to output them
     963             :                      as two separate keys and have import merge them.  */
     964           0 :                   if (subkey_in_list_p (subkey_list, node))
     965           0 :                     skip_until_subkey = 1; /* Already processed this one. */
     966             :                   else
     967             :                     {
     968             :                       size_t j;
     969             : 
     970           0 :                       for (j=0; j < ndesc; j++)
     971           0 :                         if (j != descindex && desc[j].exact
     972           0 :                             && exact_subkey_match_p (desc+j, node))
     973           0 :                           break;
     974           0 :                       if (!(j < ndesc))
     975           0 :                         skip_until_subkey = 1; /* No other one matching. */
     976             :                     }
     977             :                 }
     978             : 
     979           0 :               if(skip_until_subkey)
     980           0 :                 continue;
     981             : 
     982             :               /* Mark this one as processed. */
     983             :               {
     984           0 :                 subkey_list_t tmp = new_subkey_list_item (node);
     985           0 :                 tmp->next = subkey_list;
     986           0 :                 subkey_list = tmp;
     987             :               }
     988             :             }
     989             : 
     990           0 :           if (node->pkt->pkttype == PKT_SIGNATURE)
     991             :             {
     992             :               /* Do not export packets which are marked as not
     993             :                  exportable.  */
     994           0 :               if (!(options&EXPORT_LOCAL_SIGS)
     995           0 :                   && !node->pkt->pkt.signature->flags.exportable)
     996           0 :                 continue; /* not exportable */
     997             : 
     998             :               /* Do not export packets with a "sensitive" revocation
     999             :                  key unless the user wants us to.  Note that we do
    1000             :                  export these when issuing the actual revocation
    1001             :                  (see revoke.c). */
    1002           0 :               if (!(options&EXPORT_SENSITIVE_REVKEYS)
    1003           0 :                   && node->pkt->pkt.signature->revkey)
    1004             :                 {
    1005             :                   int i;
    1006             : 
    1007           0 :                   for (i=0;i<node->pkt->pkt.signature->numrevkeys;i++)
    1008           0 :                     if ( (node->pkt->pkt.signature->revkey[i].class & 0x40))
    1009           0 :                       break;
    1010             : 
    1011           0 :                   if (i < node->pkt->pkt.signature->numrevkeys)
    1012           0 :                     continue;
    1013             :                 }
    1014             :             }
    1015             : 
    1016             :           /* Don't export attribs? */
    1017           0 :           if (!(options&EXPORT_ATTRIBUTES)
    1018           0 :               && node->pkt->pkttype == PKT_USER_ID
    1019           0 :               && node->pkt->pkt.user_id->attrib_data )
    1020             :             {
    1021             :               /* Skip until we get to something that is not an attrib
    1022             :                  or a signature on an attrib */
    1023           0 :               while (kbctx->next && kbctx->next->pkt->pkttype==PKT_SIGNATURE)
    1024           0 :                 kbctx = kbctx->next;
    1025             : 
    1026           0 :               continue;
    1027             :             }
    1028             : 
    1029           0 :           if (secret && (node->pkt->pkttype == PKT_PUBLIC_KEY
    1030           0 :                          || node->pkt->pkttype == PKT_PUBLIC_SUBKEY))
    1031           0 :             {
    1032             :               u32 subkidbuf[2], *subkid;
    1033             :               char *hexgrip, *serialno;
    1034             : 
    1035           0 :               pk = node->pkt->pkt.public_key;
    1036           0 :               if (node->pkt->pkttype == PKT_PUBLIC_KEY)
    1037           0 :                 subkid = NULL;
    1038             :               else
    1039             :                 {
    1040           0 :                   keyid_from_pk (pk, subkidbuf);
    1041           0 :                   subkid = subkidbuf;
    1042             :                 }
    1043             : 
    1044           0 :               if (pk->seckey_info)
    1045             :                 {
    1046           0 :                   log_error ("key %s: oops: seckey_info already set"
    1047             :                              " - skipped\n", keystr_with_sub (keyid, subkid));
    1048           0 :                   skip_until_subkey = 1;
    1049           0 :                   continue;
    1050             :                 }
    1051             : 
    1052           0 :               err = hexkeygrip_from_pk (pk, &hexgrip);
    1053           0 :               if (err)
    1054             :                 {
    1055           0 :                   log_error ("key %s: error computing keygrip: %s"
    1056             :                              " - skipped\n", keystr_with_sub (keyid, subkid),
    1057             :                              gpg_strerror (err));
    1058           0 :                   skip_until_subkey = 1;
    1059           0 :                   err = 0;
    1060           0 :                   continue;
    1061             :                 }
    1062             : 
    1063           0 :               if (secret == 2 && node->pkt->pkttype == PKT_PUBLIC_KEY)
    1064             :                 {
    1065             :                   /* We are asked not to export the secret parts of
    1066             :                      the primary key.  Make up an error code to create
    1067             :                      the stub.  */
    1068           0 :                   err = GPG_ERR_NOT_FOUND;
    1069           0 :                   serialno = NULL;
    1070             :                 }
    1071             :               else
    1072           0 :                 err = agent_get_keyinfo (ctrl, hexgrip, &serialno);
    1073             : 
    1074           0 :               if ((!err && serialno)
    1075           0 :                   && secret == 2 && node->pkt->pkttype == PKT_PUBLIC_KEY)
    1076             :                 {
    1077             :                   /* It does not make sense to export a key with its
    1078             :                      primary key on card using a non-key stub.  Thus
    1079             :                      we skip those keys when used with
    1080             :                      --export-secret-subkeys. */
    1081           0 :                   log_info (_("key %s: key material on-card - skipped\n"),
    1082             :                             keystr_with_sub (keyid, subkid));
    1083           0 :                   skip_until_subkey = 1;
    1084             :                 }
    1085           0 :               else if (gpg_err_code (err) == GPG_ERR_NOT_FOUND
    1086           0 :                        || (!err && serialno))
    1087           0 :                 {
    1088             :                   /* Create a key stub.  */
    1089             :                   struct seckey_info *ski;
    1090             :                   const char *s;
    1091             : 
    1092           0 :                   pk->seckey_info = ski = xtrycalloc (1, sizeof *ski);
    1093           0 :                   if (!ski)
    1094             :                     {
    1095           0 :                       err = gpg_error_from_syserror ();
    1096           0 :                       xfree (hexgrip);
    1097           0 :                       goto leave;
    1098             :                     }
    1099             : 
    1100           0 :                   ski->is_protected = 1;
    1101           0 :                   if (err)
    1102           0 :                     ski->s2k.mode = 1001; /* GNU dummy (no secret key).  */
    1103             :                   else
    1104             :                     {
    1105           0 :                       ski->s2k.mode = 1002; /* GNU-divert-to-card.  */
    1106           0 :                       for (s=serialno; sizeof (ski->ivlen) && *s && s[1];
    1107           0 :                            ski->ivlen++, s += 2)
    1108           0 :                         ski->iv[ski->ivlen] = xtoi_2 (s);
    1109             :                     }
    1110             : 
    1111           0 :                   err = build_packet (out, node->pkt);
    1112             :                 }
    1113           0 :               else if (!err)
    1114             :                 {
    1115             :                   /* FIXME: Move this spaghetti code into a separate
    1116             :                      function.  */
    1117           0 :                   unsigned char *wrappedkey = NULL;
    1118             :                   size_t wrappedkeylen;
    1119           0 :                   unsigned char *key = NULL;
    1120             :                   size_t keylen, realkeylen;
    1121             :                   gcry_sexp_t s_skey;
    1122             : 
    1123           0 :                   if (opt.verbose)
    1124           0 :                     log_info ("key %s: asking agent for the secret parts\n",
    1125             :                               keystr_with_sub (keyid, subkid));
    1126             : 
    1127             :                   {
    1128           0 :                     char *prompt = gpg_format_keydesc (pk,
    1129             :                                                        FORMAT_KEYDESC_EXPORT,1);
    1130           0 :                     err = agent_export_key (ctrl, hexgrip, prompt, &cache_nonce,
    1131             :                                             &wrappedkey, &wrappedkeylen);
    1132           0 :                     xfree (prompt);
    1133             :                   }
    1134           0 :                   if (err)
    1135           0 :                     goto unwraperror;
    1136           0 :                   if (wrappedkeylen < 24)
    1137             :                     {
    1138           0 :                       err = gpg_error (GPG_ERR_INV_LENGTH);
    1139           0 :                       goto unwraperror;
    1140             :                     }
    1141           0 :                   keylen = wrappedkeylen - 8;
    1142           0 :                   key = xtrymalloc_secure (keylen);
    1143           0 :                   if (!key)
    1144             :                     {
    1145           0 :                       err = gpg_error_from_syserror ();
    1146           0 :                       goto unwraperror;
    1147             :                     }
    1148           0 :                   err = gcry_cipher_decrypt (cipherhd, key, keylen,
    1149             :                                              wrappedkey, wrappedkeylen);
    1150           0 :                   if (err)
    1151           0 :                     goto unwraperror;
    1152           0 :                   realkeylen = gcry_sexp_canon_len (key, keylen, NULL, &err);
    1153           0 :                   if (!realkeylen)
    1154           0 :                     goto unwraperror; /* Invalid csexp.  */
    1155             : 
    1156           0 :                   err = gcry_sexp_sscan (&s_skey, NULL, key, realkeylen);
    1157           0 :                   xfree (key);
    1158           0 :                   key = NULL;
    1159           0 :                   if (err)
    1160           0 :                     goto unwraperror;
    1161           0 :                   err = transfer_format_to_openpgp (s_skey, pk);
    1162           0 :                   gcry_sexp_release (s_skey);
    1163           0 :                   if (err)
    1164           0 :                     goto unwraperror;
    1165             : 
    1166           0 :                   err = build_packet (out, node->pkt);
    1167           0 :                   goto unwraperror_leave;
    1168             : 
    1169             :                 unwraperror:
    1170           0 :                   xfree (wrappedkey);
    1171           0 :                   xfree (key);
    1172           0 :                   if (err)
    1173             :                     {
    1174           0 :                       log_error ("key %s: error receiving key from agent:"
    1175             :                                  " %s%s\n",
    1176             :                                  keystr_with_sub (keyid, subkid),
    1177             :                                  gpg_strerror (err),
    1178           0 :                                  gpg_err_code (err) == GPG_ERR_FULLY_CANCELED?
    1179             :                                  "":_(" - skipped"));
    1180           0 :                       if (gpg_err_code (err) == GPG_ERR_FULLY_CANCELED)
    1181           0 :                         goto leave;
    1182           0 :                       skip_until_subkey = 1;
    1183           0 :                       err = 0;
    1184             :                     }
    1185             :                 unwraperror_leave:
    1186             :                   ;
    1187             :                 }
    1188             :               else
    1189             :                 {
    1190           0 :                   log_error ("key %s: error getting keyinfo from agent: %s"
    1191             :                              " - skipped\n", keystr_with_sub (keyid, subkid),
    1192             :                              gpg_strerror (err));
    1193           0 :                   skip_until_subkey = 1;
    1194           0 :                   err = 0;
    1195             :                 }
    1196             : 
    1197           0 :               xfree (pk->seckey_info);
    1198           0 :               pk->seckey_info = NULL;
    1199           0 :               xfree (hexgrip);
    1200             :             }
    1201             :           else
    1202             :             {
    1203           0 :               err = build_packet (out, node->pkt);
    1204             :             }
    1205             : 
    1206           0 :           if (err)
    1207             :             {
    1208           0 :               log_error ("build_packet(%d) failed: %s\n",
    1209           0 :                          node->pkt->pkttype, gpg_strerror (err));
    1210           0 :               goto leave;
    1211             :             }
    1212             : 
    1213           0 :           if (!skip_until_subkey)
    1214           0 :             *any = 1;
    1215             :         }
    1216             : 
    1217           0 :       if (keyblock_out)
    1218             :         {
    1219           0 :           *keyblock_out = keyblock;
    1220           0 :           break;
    1221             :         }
    1222           0 :     }
    1223           0 :   if (gpg_err_code (err) == GPG_ERR_NOT_FOUND)
    1224           0 :     err = 0;
    1225             : 
    1226             :  leave:
    1227           0 :   gcry_cipher_close (cipherhd);
    1228           0 :   release_subkey_list (subkey_list);
    1229           0 :   xfree(desc);
    1230           0 :   keydb_release (kdbhd);
    1231           0 :   if (err || !keyblock_out)
    1232           0 :     release_kbnode( keyblock );
    1233           0 :   xfree (cache_nonce);
    1234           0 :   if( !*any )
    1235           0 :     log_info(_("WARNING: nothing exported\n"));
    1236           0 :   return err;
    1237             : }

Generated by: LCOV version 1.11