~ubuntu-branches/ubuntu/vivid/gnupg2/vivid-proposed

« back to all changes in this revision

Viewing changes to scd/pcsc-wrapper.c

  • Committer: Package Import Robot
  • Author(s): Stéphane Graber
  • Date: 2013-05-24 18:27:04 UTC
  • mfrom: (18.1.2 ubuntu)
  • Revision ID: package-import@ubuntu.com-20130524182704-d9uwh9lrj91elnm6
* Resynchronize on Debian, remaining changes:
  - Add udev rules to give gpg access to some smartcard readers;
    Debian #543217.
    . debian/gnupg2.udev: udev rules to set ACLs on SCM smartcard readers.
  - Add upstart user job for gpg-agent.

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
 
66
66
static int verbose;
67
67
 
 
68
#if defined(__APPLE__) || defined(_WIN32) || defined(__CYGWIN__)
 
69
typedef unsinged int pcsc_dword_t;
 
70
#else
 
71
typedef unsigned long pcsc_dword_t;
 
72
#endif
 
73
 
68
74
 
69
75
/* PC/SC constants and function pointer. */
70
76
#define PCSC_SCOPE_USER      0
112
118
 
113
119
typedef struct pcsc_io_request_s *pcsc_io_request_t;
114
120
 
 
121
#ifdef __APPLE__
 
122
#pragma pack(1)
 
123
#endif
 
124
 
115
125
struct pcsc_readerstate_s
116
126
{
117
127
  const char *reader;
118
128
  void *user_data;
119
 
  unsigned long current_state;
120
 
  unsigned long event_state;
121
 
  unsigned long atrlen;
 
129
  pcsc_dword_t current_state;
 
130
  pcsc_dword_t event_state;
 
131
  pcsc_dword_t atrlen;
122
132
  unsigned char atr[33];
123
133
};
124
134
 
 
135
#ifdef __APPLE__
 
136
#pragma pack()
 
137
#endif
 
138
 
125
139
typedef struct pcsc_readerstate_s *pcsc_readerstate_t;
126
140
 
127
141
 
129
143
                                  initialzied and is ready for
130
144
                                  operations.  The following variables
131
145
                                  are then valid. */
132
 
static unsigned long pcsc_context;  /* The current PC/CS context. */
 
146
static long pcsc_context;  /* The current PC/CS context. */
133
147
static char *current_rdrname;
134
 
static unsigned long pcsc_card;
135
 
static unsigned long pcsc_protocol;
 
148
static long pcsc_card;
 
149
static pcsc_dword_t pcsc_protocol;
136
150
static unsigned char current_atr[33];
137
151
static size_t current_atrlen;
138
152
 
139
 
long (* pcsc_establish_context) (unsigned long scope,
 
153
long (* pcsc_establish_context) (pcsc_dword_t scope,
140
154
                                 const void *reserved1,
141
155
                                 const void *reserved2,
142
 
                                 unsigned long *r_context);
143
 
long (* pcsc_release_context) (unsigned long context);
144
 
long (* pcsc_list_readers) (unsigned long context,
 
156
                                 long *r_context);
 
157
long (* pcsc_release_context) (long context);
 
158
long (* pcsc_list_readers) (long context,
145
159
                            const char *groups,
146
 
                            char *readers, unsigned long*readerslen);
147
 
long (* pcsc_get_status_change) (unsigned long context,
148
 
                                 unsigned long timeout,
 
160
                            char *readers, pcsc_dword_t *readerslen);
 
161
long (* pcsc_get_status_change) (long context,
 
162
                                 pcsc_dword_t timeout,
149
163
                                 pcsc_readerstate_t readerstates,
150
 
                                 unsigned long nreaderstates);
151
 
long (* pcsc_connect) (unsigned long context,
 
164
                                 pcsc_dword_t nreaderstates);
 
165
long (* pcsc_connect) (long context,
152
166
                       const char *reader,
153
 
                       unsigned long share_mode,
154
 
                       unsigned long preferred_protocols,
155
 
                       unsigned long *r_card,
156
 
                       unsigned long *r_active_protocol);
157
 
long (* pcsc_reconnect) (unsigned long card,
158
 
                         unsigned long share_mode,
159
 
                         unsigned long preferred_protocols,
160
 
                         unsigned long initialization,
161
 
                         unsigned long *r_active_protocol);
162
 
long (* pcsc_disconnect) (unsigned long card,
163
 
                          unsigned long disposition);
164
 
long (* pcsc_status) (unsigned long card,
165
 
                      char *reader, unsigned long *readerlen,
166
 
                      unsigned long *r_state,
167
 
                      unsigned long *r_protocol,
168
 
                      unsigned char *atr, unsigned long *atrlen);
169
 
long (* pcsc_begin_transaction) (unsigned long card);
170
 
long (* pcsc_end_transaction) (unsigned long card,
171
 
                               unsigned long disposition);
172
 
long (* pcsc_transmit) (unsigned long card,
 
167
                       pcsc_dword_t share_mode,
 
168
                       pcsc_dword_t preferred_protocols,
 
169
                       long *r_card,
 
170
                       pcsc_dword_t *r_active_protocol);
 
171
long (* pcsc_reconnect) (long card,
 
172
                         pcsc_dword_t share_mode,
 
173
                         pcsc_dword_t preferred_protocols,
 
174
                         pcsc_dword_t initialization,
 
175
                         pcsc_dword_t *r_active_protocol);
 
176
long (* pcsc_disconnect) (long card,
 
177
                          pcsc_dword_t disposition);
 
178
long (* pcsc_status) (long card,
 
179
                      char *reader, pcsc_dword_t *readerlen,
 
180
                      pcsc_dword_t *r_state,
 
181
                      pcsc_dword_t *r_protocol,
 
182
                      unsigned char *atr, pcsc_dword_t *atrlen);
 
183
long (* pcsc_begin_transaction) (long card);
 
184
long (* pcsc_end_transaction) (long card,
 
185
                               pcsc_dword_t disposition);
 
186
long (* pcsc_transmit) (long card,
173
187
                        const pcsc_io_request_t send_pci,
174
188
                        const unsigned char *send_buffer,
175
 
                        unsigned long send_len,
 
189
                        pcsc_dword_t send_len,
176
190
                        pcsc_io_request_t recv_pci,
177
191
                        unsigned char *recv_buffer,
178
 
                        unsigned long *recv_len);
179
 
long (* pcsc_set_timeout) (unsigned long context,
180
 
                           unsigned long timeout);
 
192
                        pcsc_dword_t *recv_len);
 
193
long (* pcsc_set_timeout) (long context,
 
194
                           pcsc_dword_t timeout);
 
195
long (* pcsc_control) (long card,
 
196
                       pcsc_dword_t control_code,
 
197
                       const void *send_buffer,
 
198
                       pcsc_dword_t send_len,
 
199
                       void *recv_buffer,
 
200
                       pcsc_dword_t recv_len,
 
201
                       pcsc_dword_t *bytes_returned);
181
202
 
182
203
 
183
204
 
335
356
  pcsc_end_transaction   = dlsym (handle, "SCardEndTransaction");
336
357
  pcsc_transmit          = dlsym (handle, "SCardTransmit");
337
358
  pcsc_set_timeout       = dlsym (handle, "SCardSetTimeout");
 
359
  pcsc_control           = dlsym (handle, "SCardControl");
338
360
 
339
361
  if (!pcsc_establish_context
340
362
      || !pcsc_release_context
347
369
      || !pcsc_begin_transaction
348
370
      || !pcsc_end_transaction
349
371
      || !pcsc_transmit
 
372
      || !pcsc_control
350
373
      /* || !pcsc_set_timeout */)
351
374
    {
352
375
      /* Note that set_timeout is currently not used and also not
353
376
         available under Windows. */
354
377
      fprintf (stderr,
355
378
               "apdu_open_reader: invalid PC/SC driver "
356
 
               "(%d%d%d%d%d%d%d%d%d%d%d%d)\n",
 
379
               "(%d%d%d%d%d%d%d%d%d%d%d%d%d)\n",
357
380
               !!pcsc_establish_context,
358
381
               !!pcsc_release_context,
359
382
               !!pcsc_list_readers,
365
388
               !!pcsc_begin_transaction,
366
389
               !!pcsc_end_transaction,
367
390
               !!pcsc_transmit,
368
 
               !!pcsc_set_timeout );
 
391
               !!pcsc_set_timeout,
 
392
               !!pcsc_control );
369
393
      dlclose (handle);
370
394
      exit (1);
371
395
    }
384
408
  long err;
385
409
  const char * portstr;
386
410
  char *list = NULL;
387
 
  unsigned long nreader, atrlen;
 
411
  pcsc_dword_t nreader, atrlen;
388
412
  char *p;
389
 
  unsigned long card_state, card_protocol;
 
413
  pcsc_dword_t card_state, card_protocol;
390
414
  unsigned char atr[33];
391
415
 
392
416
  /* Make sure there is only the port string */
482
506
  if (!err)
483
507
    {
484
508
      char reader[250];
485
 
      unsigned long readerlen;
 
509
      pcsc_dword_t readerlen;
486
510
 
487
511
      atrlen = 33;
488
512
      readerlen = sizeof reader -1;
578
602
  if ( !(rdrstates[0].event_state & PCSC_STATE_UNKNOWN) )
579
603
    {
580
604
      if ( (rdrstates[0].event_state & PCSC_STATE_PRESENT) )
581
 
        status |= 2;
582
 
      if ( !(rdrstates[0].event_state & PCSC_STATE_MUTE) )
583
 
        status |= 4;
 
605
        {
 
606
          status |= 2;
 
607
          if ( !(rdrstates[0].event_state & PCSC_STATE_MUTE) )
 
608
            status |= 4;
 
609
        }
584
610
      /* We indicate a useful card if it is not in use by another
585
611
         application.  This is because we only use exclusive access
586
612
         mode.  */
616
642
{
617
643
  long err;
618
644
  char reader[250];
619
 
  unsigned long nreader, atrlen;
620
 
  unsigned long card_state, card_protocol;
 
645
  pcsc_dword_t nreader, atrlen;
 
646
  pcsc_dword_t card_state, card_protocol;
621
647
 
622
648
  (void)argbuf;
623
649
  (void)arglen;
687
713
{
688
714
  long err;
689
715
  struct pcsc_io_request_s send_pci;
690
 
  unsigned long recv_len;
 
716
  pcsc_dword_t recv_len;
691
717
  unsigned char buffer[1024];
692
718
 
693
719
  /* The apdu should at least be one byte. */
720
746
}
721
747
 
722
748
 
 
749
/* Handle a control request.  The argument is expected to be a buffer
 
750
   which contains CONTROL_CODE (4-byte) and INPUT_BYTES.
 
751
 */
 
752
static void
 
753
handle_control (unsigned char *argbuf, size_t arglen)
 
754
{
 
755
  long err;
 
756
  pcsc_dword_t ioctl_code;
 
757
  pcsc_dword_t recv_len = 1024;
 
758
  unsigned char buffer[1024];
 
759
 
 
760
  if (arglen < 4)
 
761
    bad_request ("CONTROL");
 
762
 
 
763
  ioctl_code = (argbuf[0] << 24) | (argbuf[1] << 16) | (argbuf[2] << 8) | argbuf[3];
 
764
  argbuf += 4;
 
765
  arglen -= 4;
 
766
 
 
767
  recv_len = sizeof (buffer);
 
768
  err = pcsc_control (pcsc_card, ioctl_code, argbuf, arglen,
 
769
                      buffer, recv_len, &recv_len);
 
770
  if (err)
 
771
    {
 
772
      if (verbose)
 
773
        fprintf (stderr, PGM": pcsc_control failed: %s (0x%lx)\n",
 
774
                 pcsc_error_string (err), err);
 
775
      request_failed (err);
 
776
      return;
 
777
    }
 
778
  request_succeeded (buffer, recv_len);
 
779
}
 
780
 
723
781
 
724
782
static void
725
783
print_version (int with_help)
831
889
          handle_reset (argbuffer, arglen);
832
890
          break;
833
891
 
 
892
        case 6:
 
893
          handle_control (argbuffer, arglen);
 
894
          break;
 
895
 
834
896
        default:
835
897
          fprintf (stderr, PGM ": invalid request 0x%02X\n", c);
836
898
          exit (1);