1
1
/* call-agent.c - Divert GPG operations to the agent.
2
* Copyright (C) 2001, 2002, 2003, 2006, 2007,
2
* Copyright (C) 2001, 2002, 2003, 2006, 2007,
3
3
* 2008, 2009 Free Software Foundation, Inc.
5
5
* This file is part of GnuPG.
330
336
xfree (parm->serialno);
331
337
parm->serialno = store_serialno (line);
332
parm->is_v2 = (strlen (parm->serialno) >= 16
338
parm->is_v2 = (strlen (parm->serialno) >= 16
333
339
&& xtoi_2 (parm->serialno+12) >= 2 );
335
341
else if (keywordlen == 7 && !memcmp (keyword, "APPTYPE", keywordlen))
529
535
/* We assume that NAME does not need escaping. */
530
536
if (12 + strlen (name) > DIM(line)-1)
531
537
return gpg_error (GPG_ERR_TOO_LARGE);
532
stpcpy (stpcpy (line, "SCD GETATTR "), name);
538
stpcpy (stpcpy (line, "SCD GETATTR "), name);
534
540
rc = start_agent (1);
562
568
/* We assume that NAME does not need escaping. */
563
569
if (12 + strlen (name) > DIM(line)-1)
564
570
return gpg_error (GPG_ERR_TOO_LARGE);
566
p = stpcpy (stpcpy (line, "SCD SETATTR "), name);
572
p = stpcpy (stpcpy (line, "SCD SETATTR "), name);
568
574
for (; valuelen; value++, valuelen--)
584
590
rc = start_agent (1);
587
rc = assuan_transact (agent_ctx, line, NULL, NULL,
593
rc = assuan_transact (agent_ctx, line, NULL, NULL,
588
594
default_inq_cb, NULL, NULL, NULL);
601
607
inq_writecert_parms (void *opaque, const char *line)
604
struct writecert_parm_s *parm = opaque;
610
struct writecert_parm_s *parm = opaque;
606
612
if (!strncmp (line, "CERTDATA", 8) && (line[8]==' '||!line[8]))
634
640
parms.ctx = agent_ctx;
635
641
parms.certdata = certdata;
636
642
parms.certdatalen = certdatalen;
638
644
rc = assuan_transact (agent_ctx, line, NULL, NULL,
639
645
inq_writecert_parms, &parms, NULL, NULL);
649
655
inq_writekey_parms (void *opaque, const char *line)
652
struct writekey_parm_s *parm = opaque;
658
struct writekey_parm_s *parm = opaque;
654
660
if (!strncmp (line, "KEYDATA", 7) && (line[7]==' '||!line[7]))
665
671
/* Send a WRITEKEY command to the SCdaemon. */
667
673
agent_scd_writekey (int keyno, const char *serialno,
668
674
const unsigned char *keydata, size_t keydatalen)
684
690
parms.ctx = agent_ctx;
685
691
parms.keydata = keydata;
686
692
parms.keydatalen = keydatalen;
688
694
rc = assuan_transact (agent_ctx, line, NULL, NULL,
689
695
inq_writekey_parms, &parms, NULL, NULL);
704
scd_genkey_cb_append_savedbytes (struct scd_genkey_parm_s *parm,
710
if (!parm->savedbytes)
712
parm->savedbytes = xtrystrdup (line);
713
if (!parm->savedbytes)
714
err = gpg_error_from_syserror ();
718
p = xtrymalloc (strlen (parm->savedbytes) + strlen (line) + 1);
720
err = gpg_error_from_syserror ();
723
strcpy (stpcpy (p, parm->savedbytes), line);
724
xfree (parm->savedbytes);
725
parm->savedbytes = p;
697
733
/* Status callback for the SCD GENKEY command. */
698
734
static gpg_error_t
699
735
scd_genkey_cb (void *opaque, const char *line)
701
struct agent_card_genkey_s *parm = opaque;
737
struct scd_genkey_parm_s *parm = opaque;
702
738
const char *keyword = line;
706
742
for (keywordlen=0; *line && !spacep (line); line++, keywordlen++)
711
747
if (keywordlen == 7 && !memcmp (keyword, "KEY-FPR", keywordlen))
713
parm->fprvalid = unhexify_fpr (line, parm->fpr);
749
parm->cgk->fprvalid = unhexify_fpr (line, parm->cgk->fpr);
715
751
else if (keywordlen == 8 && !memcmp (keyword, "KEY-DATA", keywordlen))
722
758
while (spacep (line))
725
rc = gcry_mpi_scan (&a, GCRYMPI_FMT_HEX, line, 0, NULL);
727
log_error ("error parsing received key data: %s\n", gpg_strerror (rc));
728
else if (*name == 'n' && spacep (name+1))
730
else if (*name == 'e' && spacep (name+1))
761
if (*name == '-' && spacep (name+1))
762
rc = scd_genkey_cb_append_savedbytes (parm, line);
734
log_info ("unknown parameter name in received key data\n");
735
gcry_mpi_release (a);
765
if (parm->savedbytes)
767
rc = scd_genkey_cb_append_savedbytes (parm, line);
769
rc = gcry_mpi_scan (&a, GCRYMPI_FMT_HEX,
770
parm->savedbytes, 0, NULL);
773
rc = gcry_mpi_scan (&a, GCRYMPI_FMT_HEX, line, 0, NULL);
775
log_error ("error parsing received key data: %s\n",
777
else if (*name == 'n' && spacep (name+1))
779
else if (*name == 'e' && spacep (name+1))
783
log_info ("unknown parameter name in received key data\n");
784
gcry_mpi_release (a);
785
rc = gpg_error (GPG_ERR_INV_PARAMETER);
788
xfree (parm->savedbytes);
789
parm->savedbytes = NULL;
738
792
else if (keywordlen == 14 && !memcmp (keyword,"KEY-CREATED-AT", keywordlen))
740
parm->created_at = (u32)strtoul (line, NULL, 10);
794
parm->cgk->created_at = (u32)strtoul (line, NULL, 10);
742
796
else if (keywordlen == 8 && !memcmp (keyword, "PROGRESS", keywordlen))
744
798
write_status_text (STATUS_PROGRESS, line);
750
804
/* Send a GENKEY command to the SCdaemon. SERIALNO is not used in
774
832
memset (info, 0, sizeof *info);
775
833
snprintf (line, DIM(line)-1, "SCD GENKEY %s%s %s %d",
776
834
*tbuf? "--timestamp=":"", tbuf,
779
837
line[DIM(line)-1] = 0;
781
839
memset (info, 0, sizeof *info);
782
840
rc = assuan_transact (agent_ctx, line,
783
841
NULL, NULL, default_inq_cb, NULL,
784
scd_genkey_cb, info);
842
scd_genkey_cb, &parms);
844
xfree (parms.savedbytes);
786
846
status_sc_op_failure (rc);
800
860
/* Send the serialno command to initialize the connection. Without
801
861
a given S/N we don't care about the data returned. If the card
802
862
has already been initialized, this is a very fast command. We
803
request the openpgp card because that is what we expect.
863
request the openpgp card because that is what we expect.
805
865
Note that an opt.limit_card_insert_tries of 1 means: No tries at
806
866
all whereas 0 means do not limit the number of tries. Due to the
830
890
err = assuan_transact (agent_ctx, "SCD SERIALNO openpgp",
831
NULL, NULL, NULL, NULL,
891
NULL, NULL, NULL, NULL,
832
892
get_serialno_cb, &this_sn);
833
893
if (gpg_err_code (err) == GPG_ERR_CARD_NOT_PRESENT)
835
895
else if (gpg_err_code (err) == GPG_ERR_NOT_SUPPORTED)
850
910
char *formatted = NULL;
851
911
char *ocodeset = i18n_switchto_utf8 ();
853
if (!strncmp (want_sn, "D27600012401", 12)
913
if (!strncmp (want_sn, "D27600012401", 12)
854
914
&& strlen (want_sn) == 32 )
855
915
formatted = xtryasprintf ("(%.4s) %.8s",
856
916
want_sn + 16, want_sn + 20);
1004
1064
rc = select_openpgp (serialno);
1008
1068
sprintf (line, "SCD SETDATA ");
1009
1069
p = line + strlen (line);
1010
1070
for (i=0; i < indatalen ; i++, p += 2 )
1171
1231
/* Check that the gpg-agent understands the repeat option. */
1172
if (assuan_transact (agent_ctx,
1232
if (assuan_transact (agent_ctx,
1173
1233
"GETINFO cmd_has_option GET_PASSPHRASE repeat",
1174
1234
NULL, NULL, NULL, NULL, NULL, NULL))
1175
1235
return gpg_error (GPG_ERR_NOT_SUPPORTED);
1187
1247
if (!(arg4 = percent_plus_escape (desc_msg)))
1190
snprintf (line, DIM(line)-1,
1191
"GET_PASSPHRASE --data --repeat=%d%s -- %s %s %s %s",
1250
snprintf (line, DIM(line)-1,
1251
"GET_PASSPHRASE --data --repeat=%d%s -- %s %s %s %s",
1193
1253
check? " --check --qualitybar":"",
1194
1254
arg1? arg1:"X",
1195
1255
arg2? arg2:"X",
1204
1264
init_membuf_secure (&data, 64);
1205
rc = assuan_transact (agent_ctx, line,
1265
rc = assuan_transact (agent_ctx, line,
1206
1266
membuf_data_cb, &data,
1207
1267
default_inq_cb, NULL, NULL, NULL);
1210
1270
xfree (get_membuf (&data, NULL));
1213
1273
put_membuf (&data, "", 1);
1214
1274
*r_passphrase = get_membuf (&data, NULL);
1289
1349
init_membuf (&data, 32);
1290
err = assuan_transact (agent_ctx, "GETINFO s2k_count",
1350
err = assuan_transact (agent_ctx, "GETINFO s2k_count",
1291
1351
membuf_data_cb, &data,
1292
1352
NULL, NULL, NULL, NULL);
1294
1354
xfree (get_membuf (&data, NULL));
1297
1357
put_membuf (&data, "", 1);
1298
1358
buf = get_membuf (&data, NULL);