summaryrefslogtreecommitdiffstats
path: root/agent/divert-scd.c
blob: e89c74a19cd52738159d7b6b80b770776b68c900 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/* divert-scd.c - divert operations to the scdaemon
 *	Copyright (C) 2002, 2003, 2009 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <unistd.h>
#include <sys/stat.h>

#include "agent.h"
#include "../common/i18n.h"
#include "../common/sexp-parse.h"


static int
ask_for_card (ctrl_t ctrl, const unsigned char *shadow_info, char **r_kid)
{
  int rc, i;
  char *serialno;
  int no_card = 0;
  char *desc;
  char *want_sn, *want_kid, *want_sn_disp;
  int len;

  *r_kid = NULL;

  rc = parse_shadow_info (shadow_info, &want_sn, &want_kid, NULL);
  if (rc)
    return rc;
  want_sn_disp = xtrystrdup (want_sn);
  if (!want_sn_disp)
    {
      rc = gpg_error_from_syserror ();
      xfree (want_sn);
      xfree (want_kid);
      return rc;
    }

  len = strlen (want_sn_disp);
  if (len == 32 && !strncmp (want_sn_disp, "D27600012401", 12))
    {
      /* This is an OpenPGP card - reformat  */
      memmove (want_sn_disp, want_sn_disp+16, 4);
      want_sn_disp[4] = ' ';
      memmove (want_sn_disp+5, want_sn_disp+20, 8);
      want_sn_disp[13] = 0;
    }
  else if (len == 20 && want_sn_disp[19] == '0')
    {
      /* We assume that a 20 byte serial number is a standard one
       * which has the property to have a zero in the last nibble (Due
       * to BCD representation).  We don't display this '0' because it
       * may confuse the user.  */
      want_sn_disp[19] = 0;
    }

  for (;;)
    {
      rc = agent_card_serialno (ctrl, &serialno, want_sn);
      if (!rc)
        {
          log_debug ("detected card with S/N %s\n", serialno);
          i = strcmp (serialno, want_sn);
          xfree (serialno);
          serialno = NULL;
          if (!i)
            {
              xfree (want_sn_disp);
              xfree (want_sn);
              *r_kid = want_kid;
              return 0; /* yes, we have the correct card */
            }
        }
      else if (gpg_err_code (rc) == GPG_ERR_ENODEV)
        {
          log_debug ("no device present\n");
          rc = 0;
          no_card = 1;
        }
      else if (gpg_err_code (rc) == GPG_ERR_CARD_NOT_PRESENT)
        {
          log_debug ("no card present\n");
          rc = 0;
          no_card = 2;
        }
      else
        {
          log_error ("error accessing card: %s\n", gpg_strerror (rc));
        }

      if (!rc)
        {
          if (asprintf (&desc,
                    "%s:%%0A%%0A"
                    "  %s",
                        no_card
                        ? L_("Please insert the card with serial number")
                        : L_("Please remove the current card and "
                             "insert the one with serial number"),
                        want_sn_disp) < 0)
            {
              rc = out_of_core ();
            }
          else
            {
              rc = agent_get_confirmation (ctrl, desc, NULL, NULL, 0);
              if (ctrl->pinentry_mode == PINENTRY_MODE_LOOPBACK &&
                  gpg_err_code (rc) == GPG_ERR_NO_PIN_ENTRY)
                rc = gpg_error (GPG_ERR_CARD_NOT_PRESENT);

              xfree (desc);
            }
        }
      if (rc)
        {
          xfree (want_sn_disp);
          xfree (want_sn);
          xfree (want_kid);
          return rc;
        }
    }
}


/* Put the DIGEST into an DER encoded container and return it in R_VAL. */
static int
encode_md_for_card (const unsigned char *digest, size_t digestlen, int algo,
                    unsigned char **r_val, size_t *r_len)
{
  unsigned char *frame;
  unsigned char asn[100];
  size_t asnlen;

  *r_val = NULL;
  *r_len = 0;

  asnlen = DIM(asn);
  if (!algo || gcry_md_test_algo (algo))
    return gpg_error (GPG_ERR_DIGEST_ALGO);
  if (gcry_md_algo_info (algo, GCRYCTL_GET_ASNOID, asn, &asnlen))
    {
      log_error ("no object identifier for algo %d\n", algo);
      return gpg_error (GPG_ERR_INTERNAL);
    }

  frame = xtrymalloc (asnlen + digestlen);
  if (!frame)
    return out_of_core ();
  memcpy (frame, asn, asnlen);
  memcpy (frame+asnlen, digest, digestlen);
  if (DBG_CRYPTO)
    log_printhex (frame, asnlen+digestlen, "encoded hash:");

  *r_val = frame;
  *r_len = asnlen+digestlen;
  return 0;
}


/* Return true if STRING ends in "%0A". */
static int
has_percent0A_suffix (const char *string)
{
  size_t n;

  return (string
          && (n = strlen (string)) >= 3
          && !strcmp (string + n - 3, "%0A"));
}


/* Callback used to ask for the PIN which should be set into BUF.  The
   buf has been allocated by the caller and is of size MAXBUF which
   includes the terminating null.  The function should return an UTF-8
   string with the passphrase, the buffer may optionally be padded
   with arbitrary characters.

   If DESC_TEXT is not NULL it can be used as further information shown
   atop of the INFO message.

   INFO gets displayed as part of a generic string.  However if the
   first character of INFO is a vertical bar all up to the next
   verical bar are considered flags and only everything after the
   second vertical bar gets displayed as the full prompt.

   Flags:

      'N' = New PIN, this requests a second prompt to repeat the
            PIN.  If the PIN is not correctly repeated it starts from
            all over.
      'A' = The PIN is an Admin PIN, SO-PIN or alike.
      'P' = The PIN is a PUK (Personal Unblocking Key).
      'R' = The PIN is a Reset Code.

   Example:

     "|AN|Please enter the new security officer's PIN"

   The text "Please ..." will get displayed and the flags 'A' and 'N'
   are considered.
 */
static int
getpin_cb (void *opaque, const char *desc_text, const char *info,
           char *buf, size_t maxbuf)
{
  struct pin_entry_info_s *pi;
  int rc;
  ctrl_t ctrl = opaque;
  const char *ends, *s;
  int any_flags = 0;
  int newpin = 0;
  int resetcode = 0;
  int is_puk = 0;
  const char *again_text = NULL;
  const char *prompt = "PIN";

  if (buf && maxbuf < 2)
    return gpg_error (GPG_ERR_INV_VALUE);

  /* Parse the flags. */
  if (info && *info =='|' && (ends=strchr (info+1, '|')))
    {
      for (s=info+1; s < ends; s++)
        {
          if (*s == 'A')
            prompt = L_("Admin PIN");
          else if (*s == 'P')
            {
              /* TRANSLATORS: A PUK is the Personal Unblocking Code
                 used to unblock a PIN. */
              prompt = L_("PUK");
              is_puk = 1;
            }
          else if (*s == 'N')
            newpin = 1;
          else if (*s == 'R')
            {
              prompt = L_("Reset Code");
              resetcode = 1;
            }
        }
      info = ends+1;
      any_flags = 1;
    }
  else if (info && *info == '|')
    log_debug ("pin_cb called without proper PIN info hack\n");

  /* If BUF has been passed as NULL, we are in pinpad mode: The
     callback opens the popup and immediately returns. */
  if (!buf)
    {
      if (maxbuf == 0) /* Close the pinentry. */
        {
          agent_popup_message_stop (ctrl);
          rc = 0;
        }
      else if (maxbuf == 1)  /* Open the pinentry. */
        {
          if (info)
            {
              char *desc;
              const char *desc2;

              if (!strcmp (info, "--ack"))
                {
                  desc2 = L_("Push ACK button on card/token.");

                  if (desc_text)
                    {
                      desc = strconcat (desc_text,
                                        has_percent0A_suffix (desc_text)
                                        ? "%0A" : "%0A%0A",
                                        desc2, NULL);
                      desc2 = NULL;
                    }
                  else
                    desc = NULL;
                }
              else
                {
                  desc2 = NULL;

                  if (desc_text)
                    desc = strconcat (desc_text,
                                      has_percent0A_suffix (desc_text)
                                      ? "%0A" : "%0A%0A",
                                      info, "%0A%0A",
                                      L_("Use the reader's pinpad for input."),
                                      NULL);
                  else
                    desc = strconcat (info, "%0A%0A",
                                      L_("Use the reader's pinpad for input."),
                                      NULL);
                }

              if (!desc2 && !desc)
                rc = gpg_error_from_syserror ();
              else
                {
                  rc = agent_popup_message_start (ctrl,
                                                  desc2? desc2:desc, NULL);
                  xfree (desc);
                }
            }
          else
            rc = agent_popup_message_start (ctrl, desc_text, NULL);
        }
      else
        rc = gpg_error (GPG_ERR_INV_VALUE);
      return rc;
    }

  /* FIXME: keep PI and TRIES in OPAQUE.  Frankly this is a whole
     mess because we should call the card's verify function from the
     pinentry check pin CB. */
 again:
  pi = gcry_calloc_secure (1, sizeof (*pi) + maxbuf + 10);
  if (!pi)
    return gpg_error_from_syserror ();
  pi->max_length = maxbuf-1;
  pi->min_digits = 0;  /* we want a real passphrase */
  pi->max_digits = 16;
  pi->max_tries = 3;

  if (any_flags)
    {
      {
        char *desc2;

        if (desc_text)
          desc2 = strconcat (desc_text,
                             has_percent0A_suffix (desc_text)
                             ? "%0A" : "%0A%0A",
                             info, NULL);
        else
          desc2 = NULL;
        rc = agent_askpin (ctrl, desc2? desc2 : info,
                           prompt, again_text, pi, NULL, 0);
        xfree (desc2);
      }
      again_text = NULL;
      if (!rc && newpin)
        {
          struct pin_entry_info_s *pi2;
          pi2 = gcry_calloc_secure (1, sizeof (*pi) + maxbuf + 10);
          if (!pi2)
            {
              rc = gpg_error_from_syserror ();
              xfree (pi);
              return rc;
            }
          pi2->max_length = maxbuf-1;
          pi2->min_digits = 0;
          pi2->max_digits = 16;
          pi2->max_tries = 1;
          rc = agent_askpin (ctrl,
                             (resetcode?
                              L_("Repeat this Reset Code"):
                              is_puk?
                              L_("Repeat this PUK"):
                              L_("Repeat this PIN")),
                             prompt, NULL, pi2, NULL, 0);
          if (!rc && strcmp (pi->pin, pi2->pin))
            {
              again_text = (resetcode?
                            L_("Reset Code not correctly repeated; try again"):
                            is_puk?
                            L_("PUK not correctly repeated; try again"):
                            L_("PIN not correctly repeated; try again"));
              xfree (pi2);
              xfree (pi);
              goto again;
            }
          xfree (pi2);
        }
    }
  else
    {
      char *desc, *desc2;

      if ( asprintf (&desc,
                     L_("Please enter the PIN%s%s%s to unlock the card"),
                     info? " (":"",
                     info? info:"",
                     info? ")":"") < 0)
        desc = NULL;
      if (desc_text)
        desc2 = strconcat (desc_text,
                           has_percent0A_suffix (desc_text)
                           ? "%0A" : "%0A%0A",
                           desc, NULL);
      else
        desc2 = NULL;
      rc = agent_askpin (ctrl, desc2? desc2 : desc? desc : info,
                         prompt, NULL, pi, NULL, 0);
      xfree (desc2);
      xfree (desc);
    }

  if (!rc)
    {
      strncpy (buf, pi->pin, maxbuf-1);
      buf[maxbuf-1] = 0;
    }
  xfree (pi);
  return rc;
}



/* This function is used when a sign operation has been diverted to a
 * smartcard.  DESC_TEXT is the original text for a prompt has send by
 * gpg to gpg-agent.
 *
 * FIXME: Explain the other args.  */
int
divert_pksign (ctrl_t ctrl, const char *desc_text,
               const unsigned char *digest, size_t digestlen, int algo,
               const unsigned char *shadow_info, unsigned char **r_sig,
               size_t *r_siglen)
{
  int rc;
  char *kid;
  size_t siglen;
  unsigned char *sigval = NULL;

  (void)desc_text;

  rc = ask_for_card (ctrl, shadow_info, &kid);
  if (rc)
    return rc;

  if (algo == MD_USER_TLS_MD5SHA1)
    {
      int save = ctrl->use_auth_call;
      ctrl->use_auth_call = 1;
      rc = agent_card_pksign (ctrl, kid, getpin_cb, ctrl, NULL,
                              algo, digest, digestlen, &sigval, &siglen);
      ctrl->use_auth_call = save;
    }
  else
    {
      unsigned char *data;
      size_t ndata;

      rc = encode_md_for_card (digest, digestlen, algo, &data, &ndata);
      if (!rc)
        {
          rc = agent_card_pksign (ctrl, kid, getpin_cb, ctrl, NULL,
                                  algo, data, ndata, &sigval, &siglen);
          xfree (data);
        }
    }

  if (!rc)
    {
      *r_sig = sigval;
      *r_siglen = siglen;
    }

  xfree (kid);

  return rc;
}


/* Decrypt the value given asn an S-expression in CIPHER using the
   key identified by SHADOW_INFO and return the plaintext in an
   allocated buffer in R_BUF.  The padding information is stored at
   R_PADDING with -1 for not known.  */
int
divert_pkdecrypt (ctrl_t ctrl, const char *desc_text,
                  const unsigned char *cipher,
                  const unsigned char *shadow_info,
                  char **r_buf, size_t *r_len, int *r_padding)
{
  int rc;
  char *kid;
  const unsigned char *s;
  size_t n;
  int depth;
  const unsigned char *ciphertext;
  size_t ciphertextlen;
  char *plaintext;
  size_t plaintextlen;

  (void)desc_text;

  *r_padding = -1;
  s = cipher;
  if (*s != '(')
    return gpg_error (GPG_ERR_INV_SEXP);
  s++;
  n = snext (&s);
  if (!n)
    return gpg_error (GPG_ERR_INV_SEXP);
  if (!smatch (&s, n, "enc-val"))
    return gpg_error (GPG_ERR_UNKNOWN_SEXP);
  if (*s != '(')
    return gpg_error (GPG_ERR_UNKNOWN_SEXP);
  s++;
  n = snext (&s);
  if (!n)
    return gpg_error (GPG_ERR_INV_SEXP);

  /* First check whether we have a flags parameter and skip it.  */
  if (smatch (&s, n, "flags"))
    {
      depth = 1;
      if (sskip (&s, &depth) || depth)
        return gpg_error (GPG_ERR_INV_SEXP);
      if (*s != '(')
        return gpg_error (GPG_ERR_INV_SEXP);
      s++;
      n = snext (&s);
      if (!n)
        return gpg_error (GPG_ERR_INV_SEXP);
    }

  if (smatch (&s, n, "rsa"))
    {
      if (*s != '(')
        return gpg_error (GPG_ERR_UNKNOWN_SEXP);
      s++;
      n = snext (&s);
      if (!n)
        return gpg_error (GPG_ERR_INV_SEXP);
      if (!smatch (&s, n, "a"))
        return gpg_error (GPG_ERR_UNKNOWN_SEXP);
      n = snext (&s);
    }
  else if (smatch (&s, n, "ecdh"))
    {
      if (*s != '(')
        return gpg_error (GPG_ERR_UNKNOWN_SEXP);
      s++;
      n = snext (&s);
      if (!n)
        return gpg_error (GPG_ERR_INV_SEXP);
      if (smatch (&s, n, "s"))
        {
          n = snext (&s);
          s += n;
          if (*s++ != ')')
            return gpg_error (GPG_ERR_INV_SEXP);
          if (*s++ != '(')
            return gpg_error (GPG_ERR_UNKNOWN_SEXP);
          n = snext (&s);
          if (!n)
            return gpg_error (GPG_ERR_INV_SEXP);
        }
      if (!smatch (&s, n, "e"))
        return gpg_error (GPG_ERR_UNKNOWN_SEXP);
      n = snext (&s);
    }
  else
    return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM);

  if (!n)
    return gpg_error (GPG_ERR_UNKNOWN_SEXP);
  ciphertext = s;
  ciphertextlen = n;

  rc = ask_for_card (ctrl, shadow_info, &kid);
  if (rc)
    return rc;

  rc = agent_card_pkdecrypt (ctrl, kid, getpin_cb, ctrl, NULL,
                             ciphertext, ciphertextlen,
                             &plaintext, &plaintextlen, r_padding);
  if (!rc)
    {
      *r_buf = plaintext;
      *r_len = plaintextlen;
    }
  xfree (kid);
  return rc;
}


gpg_error_t
divert_writekey (ctrl_t ctrl, int force, const char *serialno,
                 const char *keyref, const char *keydata, size_t keydatalen)
{
  return agent_card_writekey (ctrl, force, serialno, keyref,
                              keydata, keydatalen, getpin_cb, ctrl);
}

int
divert_generic_cmd (ctrl_t ctrl, const char *cmdline, void *assuan_context)
{
  return agent_card_scd (ctrl, cmdline, getpin_cb, ctrl, assuan_context);
}