~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-updates

« back to all changes in this revision

Viewing changes to g10/call-agent.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2009-03-08 22:46:47 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090308224647-gq17gatcl71lrc2k
Tags: 2.0.11-1
* New upstream release. (Closes: #496663)
* debian/control: Make the description a little more distinctive than
  gnupg v1's. Thanks Jari Aalto. (Closes: #496323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
  size_t ciphertextlen;
54
54
};
55
55
 
 
56
struct writecert_parm_s
 
57
{
 
58
  assuan_context_t ctx;
 
59
  const unsigned char *certdata;
 
60
  size_t certdatalen;
 
61
};
 
62
 
56
63
struct writekey_parm_s
57
64
{
58
65
  assuan_context_t ctx;
141
148
   escaping.  Note that the provided buffer needs to be 3 times the
142
149
   size of ATEXT plus 1.  Returns a pointer to the leading Nul in P. */
143
150
static char *
144
 
percent_plus_escape (char *p, const char *atext)
 
151
my_percent_plus_escape (char *p, const char *atext)
145
152
{
146
153
  const unsigned char *s;
147
154
 
148
155
  for (s=atext; *s; s++)
149
156
    {
150
 
      if (*s < ' ' || *s == '+')
 
157
      if (*s < ' ' || *s == '+' || *s == '%')
151
158
        {
152
 
          sprintf (p, "%%%02X", *s);
 
159
          snprintf (p, 4, "%%%02X", *s);
153
160
          p += 3;
154
161
        }
155
162
      else if (*s == ' ')
201
208
 
202
209
 
203
210
 
 
211
/* This is a dummy data line callback.  */
 
212
static int
 
213
dummy_data_cb (void *opaque, const void *buffer, size_t length)
 
214
{
 
215
  (void)opaque;
 
216
  (void)buffer;
 
217
  (void)length;
 
218
  return 0;
 
219
}
 
220
 
 
221
 
204
222
/* This is the default inquiry callback.  It mainly handles the
205
223
   Pinentry notifications.  */
206
224
static int
232
250
    return;
233
251
 
234
252
  xfree (info->serialno); info->serialno = NULL;
 
253
  xfree (info->apptype); info->apptype = NULL;
235
254
  xfree (info->disp_name); info->disp_name = NULL;
236
255
  xfree (info->disp_lang); info->disp_lang = NULL;
237
256
  xfree (info->pubkey_url); info->pubkey_url = NULL;
257
276
    {
258
277
      xfree (parm->serialno);
259
278
      parm->serialno = store_serialno (line);
 
279
      parm->is_v2 = (strlen (parm->serialno) >= 16 
 
280
                     && xtoi_2 (parm->serialno+12) >= 2 );
 
281
    }
 
282
  else if (keywordlen == 7 && !memcmp (keyword, "APPTYPE", keywordlen))
 
283
    {
 
284
      xfree (parm->apptype);
 
285
      parm->apptype = unescape_status_string (line);
260
286
    }
261
287
  else if (keywordlen == 9 && !memcmp (keyword, "DISP-NAME", keywordlen))
262
288
    {
363
389
 
364
390
  memset (info, 0, sizeof *info);
365
391
  rc = assuan_transact (agent_ctx, "LEARN --send",
366
 
                        NULL, NULL, default_inq_cb, NULL,
 
392
                        dummy_data_cb, NULL, default_inq_cb, NULL,
367
393
                        learn_status_cb, info);
368
394
  
369
395
  return rc;
409
435
  char line[ASSUAN_LINELENGTH];
410
436
  char *p;
411
437
 
 
438
  (void)serialno;
 
439
 
412
440
  if (!*name || !valuelen)
413
441
    return gpg_error (GPG_ERR_INV_VALUE);
414
442
 
445
473
 
446
474
 
447
475
 
 
476
/* Handle a CERTDATA inquiry.  Note, we only send the data,
 
477
   assuan_transact takes care of flushing and writing the END
 
478
   command. */
 
479
static int
 
480
inq_writecert_parms (void *opaque, const char *line)
 
481
{
 
482
  int rc;
 
483
  struct writecert_parm_s *parm = opaque; 
 
484
 
 
485
  if (!strncmp (line, "CERTDATA", 8) && (line[8]==' '||!line[8]))
 
486
    {
 
487
      rc = assuan_send_data (parm->ctx, parm->certdata, parm->certdatalen);
 
488
    }
 
489
  else
 
490
    rc = default_inq_cb (opaque, line);
 
491
 
 
492
  return rc;
 
493
}
 
494
 
 
495
 
 
496
/* Send a WRITECERT command to the SCdaemon. */
 
497
int 
 
498
agent_scd_writecert (const char *certidstr,
 
499
                     const unsigned char *certdata, size_t certdatalen)
 
500
{
 
501
  int rc;
 
502
  char line[ASSUAN_LINELENGTH];
 
503
  struct writecert_parm_s parms;
 
504
 
 
505
  rc = start_agent ();
 
506
  if (rc)
 
507
    return rc;
 
508
 
 
509
  memset (&parms, 0, sizeof parms);
 
510
 
 
511
  snprintf (line, DIM(line)-1, "SCD WRITECERT %s", certidstr);
 
512
  line[DIM(line)-1] = 0;
 
513
  parms.ctx = agent_ctx;
 
514
  parms.certdata = certdata;
 
515
  parms.certdatalen = certdatalen;
 
516
  
 
517
  rc = assuan_transact (agent_ctx, line, NULL, NULL,
 
518
                        inq_writecert_parms, &parms, NULL, NULL);
 
519
 
 
520
  return rc;
 
521
}
 
522
 
 
523
 
 
524
 
 
525
 
448
526
/* Handle a KEYDATA inquiry.  Note, we only send the data,
449
527
   assuan_transact takes care of flushing and writing the end */
450
528
static int
473
551
  char line[ASSUAN_LINELENGTH];
474
552
  struct writekey_parm_s parms;
475
553
 
 
554
  (void)serialno;
 
555
 
476
556
  rc = start_agent ();
477
557
  if (rc)
478
558
    return rc;
557
637
  char line[ASSUAN_LINELENGTH];
558
638
  gnupg_isotime_t tbuf;
559
639
 
 
640
  (void)serialno;
 
641
 
560
642
  rc = start_agent ();
561
643
  if (rc)
562
644
    return rc;
616
698
  if (indatalen*2 + 50 > DIM(line))
617
699
    return gpg_error (GPG_ERR_GENERAL);
618
700
 
 
701
  /* Send the serialno command to initialize the connection. We don't
 
702
     care about the data returned.  If the card has already been
 
703
     initialized, this is a very fast command.  We request the openpgp
 
704
     card because that is waht we expect. */
 
705
  rc = assuan_transact (agent_ctx, "SCD SERIALNO openpgp",
 
706
                        NULL, NULL, NULL, NULL, NULL, NULL);
 
707
  if (rc)
 
708
    return rc;
 
709
 
619
710
  sprintf (line, "SCD SETDATA ");
620
711
  p = line + strlen (line);
621
712
  for (i=0; i < indatalen ; i++, p += 2 )
672
763
  if (indatalen*2 + 50 > DIM(line))
673
764
    return gpg_error (GPG_ERR_GENERAL);
674
765
 
 
766
  /* Send the serialno command to initialize the connection. We don't
 
767
     care about the data returned.  If the card has already been
 
768
     initialized, this is a very fast command.  We request the openpgp
 
769
     card because that is waht we expect. */
 
770
  rc = assuan_transact (agent_ctx, "SCD SERIALNO openpgp",
 
771
                        NULL, NULL, NULL, NULL, NULL, NULL);
 
772
  if (rc)
 
773
    return rc;
 
774
 
675
775
  sprintf (line, "SCD SETDATA ");
676
776
  p = line + strlen (line);
677
777
  for (i=0; i < indatalen ; i++, p += 2 )
701
801
 
702
802
/* Change the PIN of an OpenPGP card or reset the retry counter.
703
803
   CHVNO 1: Change the PIN
704
 
         2: Same as 1
 
804
         2: For v1 cards: Same as 1.
 
805
            For v2 cards: Reset the PIN using the Reset Code.
705
806
         3: Change the admin PIN
706
807
       101: Set a new PIN and reset the retry counter
707
 
       102: Same as 101
 
808
       102: For v1 cars: Same as 101.
 
809
            For v2 cards: Set a new Reset Code.
708
810
   SERIALNO is not used.
709
811
 */
710
812
int
714
816
  char line[ASSUAN_LINELENGTH];
715
817
  const char *reset = "";
716
818
 
 
819
  (void)serialno;
 
820
 
717
821
  if (chvno >= 100)
718
822
    reset = "--reset";
719
823
  chvno %= 100;
755
859
void
756
860
agent_clear_pin_cache (const char *sn)
757
861
{
758
 
 
 
862
  (void)sn;
759
863
}
760
864
 
761
865
 
796
900
 
797
901
  p = stpcpy (line, cmd);
798
902
  if (cache_id && *cache_id)
799
 
    p = percent_plus_escape (p, cache_id);
 
903
    p = my_percent_plus_escape (p, cache_id);
800
904
  else
801
905
    *p++ = 'X';
802
906
  *p++ = ' ';
803
907
 
804
908
  if (err_msg && *err_msg)
805
 
    p = percent_plus_escape (p, err_msg);
 
909
    p = my_percent_plus_escape (p, err_msg);
806
910
  else
807
911
    *p++ = 'X';
808
912
  *p++ = ' ';
809
913
 
810
914
  if (prompt && *prompt)
811
 
    p = percent_plus_escape (p, prompt);
 
915
    p = my_percent_plus_escape (p, prompt);
812
916
  else
813
917
    *p++ = 'X'; 
814
918
  *p++ = ' ';
815
919
 
816
920
  if (desc_msg && *desc_msg)
817
 
    p = percent_plus_escape (p, desc_msg);
 
921
    p = my_percent_plus_escape (p, desc_msg);
818
922
  else
819
923
    *p++ = 'X';
820
924
  *p = 0;