~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to scd/app-p15.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *
6
6
 * GnuPG is free software; you can redistribute it and/or modify
7
7
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * the Free Software Foundation; either version 3 of the License, or
9
9
 * (at your option) any later version.
10
10
 *
11
11
 * GnuPG is distributed in the hope that it will be useful,
14
14
 * GNU General Public License for more details.
15
15
 *
16
16
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
18
 */
20
19
 
 
20
/* Information pertaining to the BELPIC developer card samples:
 
21
 
 
22
       Unblock PUK: "222222111111"
 
23
       Reset PIN:   "333333111111")
 
24
 
 
25
   e.g. the APDUs 00:20:00:02:08:2C:33:33:33:11:11:11:FF 
 
26
              and 00:24:01:01:08:24:12:34:FF:FF:FF:FF:FF
 
27
   should change the PIN into 1234.
 
28
*/
 
29
 
21
30
#include <config.h>
22
31
#include <errno.h>
23
32
#include <stdio.h>
509
518
  objidlen /= 2;
510
519
  objid = xtrymalloc (objidlen);
511
520
  if (!objid)
512
 
    return gpg_error_from_errno (errno);
 
521
    return gpg_error_from_syserror ();
513
522
  for (s=certid, i=0; i < objidlen; i++, s+=2)
514
523
    objid[i] = xtoi_2 (s);
515
524
  *r_objid = objid;
681
690
    }
682
691
 
683
692
  if (buflen)
684
 
    log_info ("warning: %u bytes of garbage detected at end of ODF\n", buflen);
 
693
    log_info ("warning: %u bytes of garbage detected at end of ODF\n",
 
694
              (unsigned int)buflen);
685
695
 
686
696
  xfree (buffer);
687
697
  return 0;
1119
1129
                              + objlen/2 * sizeof(unsigned short)));
1120
1130
      if (!prkdf)
1121
1131
        {
1122
 
          err = gpg_error_from_errno (errno);
 
1132
          err = gpg_error_from_syserror ();
1123
1133
          goto leave;
1124
1134
        }
1125
1135
      prkdf->objidlen = objidlen;
1126
1136
      prkdf->objid = xtrymalloc (objidlen);
1127
1137
      if (!prkdf->objid)
1128
1138
        {
1129
 
          err = gpg_error_from_errno (errno);
 
1139
          err = gpg_error_from_syserror ();
1130
1140
          xfree (prkdf);
1131
1141
          goto leave;
1132
1142
        }
1137
1147
          prkdf->authid = xtrymalloc (authidlen);
1138
1148
          if (!prkdf->authid)
1139
1149
            {
1140
 
              err = gpg_error_from_errno (errno);
 
1150
              err = gpg_error_from_syserror ();
1141
1151
              xfree (prkdf->objid);
1142
1152
              xfree (prkdf);
1143
1153
              goto leave;
1405
1415
                            + objlen/2 * sizeof(unsigned short)));
1406
1416
      if (!cdf)
1407
1417
        {
1408
 
          err = gpg_error_from_errno (errno);
 
1418
          err = gpg_error_from_syserror ();
1409
1419
          goto leave;
1410
1420
        }
1411
1421
      cdf->objidlen = objidlen;
1412
1422
      cdf->objid = xtrymalloc (objidlen);
1413
1423
      if (!cdf->objid)
1414
1424
        {
1415
 
          err = gpg_error_from_errno (errno);
 
1425
          err = gpg_error_from_syserror ();
1416
1426
          xfree (cdf);
1417
1427
          goto leave;
1418
1428
        }
2124
2134
      continue; /* Ready. */
2125
2135
 
2126
2136
    no_core:
2127
 
      err = gpg_error_from_errno (errno);
 
2137
      err = gpg_error_from_syserror ();
2128
2138
      release_aodf_object (aodf);
2129
2139
      goto leave;
2130
2140
 
2261
2271
  app->app_local->serialno = xtrymalloc (objlen);
2262
2272
  if (!app->app_local->serialno)
2263
2273
    {
2264
 
      err = gpg_error_from_errno (errno);
 
2274
      err = gpg_error_from_syserror ();
2265
2275
      goto leave;
2266
2276
    }
2267
2277
  memcpy (app->app_local->serialno, p, objlen);
2357
2367
 
2358
2368
      buf = xtrymalloc (9 + certinfo->objidlen*2 + 1);
2359
2369
      if (!buf)
2360
 
        return gpg_error_from_errno (errno);
 
2370
        return gpg_error_from_syserror ();
2361
2371
      p = stpcpy (buf, "P15");
2362
2372
      if (app->app_local->home_df)
2363
2373
        {
2452
2462
 
2453
2463
      buf = xtrymalloc (9 + keyinfo->objidlen*2 + 1);
2454
2464
      if (!buf)
2455
 
        return gpg_error_from_errno (errno);
 
2465
        return gpg_error_from_syserror ();
2456
2466
      p = stpcpy (buf, "P15");
2457
2467
      if (app->app_local->home_df)
2458
2468
        {
2533
2543
    {
2534
2544
      *r_cert = xtrymalloc (cdf->imagelen);
2535
2545
      if (!*r_cert)
2536
 
        return gpg_error_from_errno (errno);
 
2546
        return gpg_error_from_syserror ();
2537
2547
      memcpy (*r_cert, cdf->image, cdf->imagelen);
2538
2548
      *r_certlen = cdf->imagelen;
2539
2549
      return 0;
2676
2686
        {
2677
2687
          buf = xtrymalloc (9 + prkdf->objidlen*2 + 1);
2678
2688
          if (!buf)
2679
 
            return gpg_error_from_errno (errno);
 
2689
            return gpg_error_from_syserror ();
2680
2690
          p = stpcpy (buf, "P15");
2681
2691
          if (app->app_local->home_df)
2682
2692
            {
2701
2711
         general rule for it so we need to decide case by case. */
2702
2712
      if (app->app_local->card_type == CARD_TYPE_BELPIC)
2703
2713
        {
2704
 
          /* The eID card has a card number printed on the fron matter
 
2714
          /* The eID card has a card number printed on the front matter
2705
2715
             which seems to be a good indication. */
2706
2716
          unsigned char *buffer;
2707
2717
          const unsigned char *p;
2857
2867
 
2858
2868
  gpg_error_t err;
2859
2869
  int i;
2860
 
  unsigned char data[35];   /* Must be large enough for a SHA-1 digest
2861
 
                               + the largest OID prefix above. */
 
2870
  unsigned char data[36];   /* Must be large enough for a SHA-1 digest
 
2871
                               + the largest OID prefix above and also
 
2872
                               fit the 36 bytes of md5sha1.  */
2862
2873
  prkdf_object_t prkdf;    /* The private key object. */
2863
2874
  aodf_object_t aodf;      /* The associated authentication object. */
2864
2875
  int no_data_padding = 0; /* True if the card want the data without padding.*/
2866
2877
 
2867
2878
  if (!keyidstr || !*keyidstr)
2868
2879
    return gpg_error (GPG_ERR_INV_VALUE);
2869
 
  if (indatalen != 20 && indatalen != 16 && indatalen != 35)
 
2880
  if (indatalen != 20 && indatalen != 16 && indatalen != 35 && indatalen != 36)
2870
2881
    return gpg_error (GPG_ERR_INV_VALUE);
2871
2882
 
2872
2883
  err = prkdf_object_from_keyidstr (app, keyidstr, &prkdf);
2927
2938
 
2928
2939
 
2929
2940
  /* Due to the fact that the non-repudiation signature on a BELPIC
2930
 
     card requires a ver verify immediately before the DSO we set the
 
2941
     card requires a verify immediately before the DSO we set the
2931
2942
     MSE before we do the verification.  Other cards might allow to do
2932
2943
     this also but I don't want to break anything, thus we do it only
2933
2944
     for the BELPIC card here. */
2937
2948
      
2938
2949
      mse[0] = 4;    /* Length of the template. */
2939
2950
      mse[1] = 0x80; /* Algorithm reference tag. */
2940
 
      mse[2] = 0x02; /* Algorithm: RSASSA-PKCS1-v1.5 using SHA1. */
 
2951
      if (hashalgo == GCRY_MD_USER_TLS_MD5SHA1)
 
2952
        mse[2] = 0x01; /* Let card do pkcs#1 0xFF padding. */
 
2953
      else
 
2954
        mse[2] = 0x02; /* RSASSA-PKCS1-v1.5 using SHA1. */
2941
2955
      mse[3] = 0x84; /* Private key reference tag. */
2942
2956
      mse[4] = prkdf->key_reference_valid? prkdf->key_reference : 0x82;
2943
2957
 
3048
3062
          paddedpin = xtrymalloc (aodf->stored_length+1);
3049
3063
          if (!paddedpin)
3050
3064
            {
3051
 
              err = gpg_error_from_errno (errno);
 
3065
              err = gpg_error_from_syserror ();
3052
3066
              xfree (pinvalue);
3053
3067
              return err;
3054
3068
            }
3076
3090
          paddedpin = xtrymalloc (aodf->stored_length+1);
3077
3091
          if (!paddedpin)
3078
3092
            {
3079
 
              err = gpg_error_from_errno (errno);
 
3093
              err = gpg_error_from_syserror ();
3080
3094
              xfree (pinvalue);
3081
3095
              return err;
3082
3096
            }
3107
3121
    }
3108
3122
 
3109
3123
  /* Prepare the DER object from INDATA. */
3110
 
  if (indatalen == 35)
 
3124
  if (indatalen == 36)
 
3125
    {
 
3126
      /* No ASN.1 container used. */
 
3127
      if (hashalgo != GCRY_MD_USER_TLS_MD5SHA1)
 
3128
        return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM);
 
3129
      memcpy (data, indata, indatalen);
 
3130
    }
 
3131
  else if (indatalen == 35)
3111
3132
    {
3112
3133
      /* Alright, the caller was so kind to send us an already
3113
3134
         prepared DER object.  Check that it is what we want and that
3166
3187
      return err;
3167
3188
    }
3168
3189
 
3169
 
  if (no_data_padding)
 
3190
  if (hashalgo == GCRY_MD_USER_TLS_MD5SHA1)
 
3191
    err = iso7816_compute_ds (app->slot, data, 36, outdata, outdatalen);
 
3192
  else if (no_data_padding)
3170
3193
    err = iso7816_compute_ds (app->slot, data+15, 20, outdata, outdatalen);
3171
3194
  else
3172
3195
    err = iso7816_compute_ds (app->slot, data, 35, outdata, outdatalen);
3176
3199
 
3177
3200
/* Handler for the PKAUTH command. 
3178
3201
 
3179
 
   This is basically the same as the PKSIGN command but we firstcheck
 
3202
   This is basically the same as the PKSIGN command but we first check
3180
3203
   that the requested key is suitable for authentication; that is, it
3181
3204
   must match the criteria used for the attribute $AUTHKEYID.  See
3182
3205
   do_sign for calling conventions; there is no HASHALGO, though. */
3189
3212
{
3190
3213
  gpg_error_t err;
3191
3214
  prkdf_object_t prkdf;
 
3215
  int algo;
3192
3216
 
3193
3217
  if (!keyidstr || !*keyidstr)
3194
3218
    return gpg_error (GPG_ERR_INV_VALUE);
3201
3225
      log_error ("key %s may not be used for authentication\n", keyidstr);
3202
3226
      return gpg_error (GPG_ERR_WRONG_KEY_USAGE);
3203
3227
    }
3204
 
  return do_sign (app, keyidstr, GCRY_MD_SHA1, pincb, pincb_arg, 
 
3228
 
 
3229
  algo = indatalen == 36? GCRY_MD_USER_TLS_MD5SHA1 : GCRY_MD_SHA1;
 
3230
  return do_sign (app, keyidstr, algo, pincb, pincb_arg, 
3205
3231
                  indata, indatalen, outdata, outdatalen);
3206
3232
}
3207
3233
 
3334
3360
      app->app_local = xtrycalloc (1, sizeof *app->app_local);
3335
3361
      if (!app->app_local)
3336
3362
        {
3337
 
          rc = gpg_error_from_errno (errno);
 
3363
          rc = gpg_error_from_syserror ();
3338
3364
          goto leave;
3339
3365
        }
3340
3366