summaryrefslogtreecommitdiff
path: root/usr/src/lib/libpcsc/common/libpcsc.c
blob: 41d646e8b189dfae5a5a416196de491423ba8a01 (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
614
615
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2019, Joyent, Inc.
 */

#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fts.h>
#include <errno.h>
#include <strings.h>
#include <unistd.h>
#include <sys/debug.h>
#include <sys/filio.h>
#include <sys/usb/clients/ccid/uccid.h>

#include <winscard.h>

/*
 * Implementation of the PCSC library leveraging the uccid framework.
 */

/*
 * The library handle is basically unused today. We keep this around such that
 * consumers which expect to receive a non-NULL opaque handle have something
 * they can use.
 */
typedef struct pcsc_hdl {
	hrtime_t pcsc_create_time;
} pcsc_hdl_t;

typedef struct pcsc_card {
	int pcc_fd;
} pcsc_card_t;

/*
 * Required globals
 */
SCARD_IO_REQUEST g_rgSCardT0Pci = {
	SCARD_PROTOCOL_T0,
	0
};

SCARD_IO_REQUEST g_rgSCardT1Pci = {
	SCARD_PROTOCOL_T1,
	0
};

SCARD_IO_REQUEST g_rgSCardRawPci = {
	SCARD_PROTOCOL_RAW,
	0
};

const char *
pcsc_stringify_error(const LONG err)
{
	switch (err) {
	case SCARD_S_SUCCESS:
		return ("no error");
	case SCARD_F_INTERNAL_ERROR:
		return ("internal error");
	case SCARD_E_CANCELLED:
		return ("request cancelled");
	case SCARD_E_INVALID_HANDLE:
		return ("invalid handle");
	case SCARD_E_INVALID_PARAMETER:
		return ("invalid parameter");
	case SCARD_E_NO_MEMORY:
		return ("no memory");
	case SCARD_E_INSUFFICIENT_BUFFER:
		return ("buffer was insufficiently sized");
	case SCARD_E_INVALID_VALUE:
		return ("invalid value passed");
	case SCARD_E_UNKNOWN_READER:
		return ("unknown reader");
	case SCARD_E_TIMEOUT:
		return ("timeout occurred");
	case SCARD_E_SHARING_VIOLATION:
		return ("sharing violation");
	case SCARD_E_NO_SMARTCARD:
		return ("no smartcard present");
	case SCARD_E_UNKNOWN_CARD:
		return ("unknown ICC");
	case SCARD_E_PROTO_MISMATCH:
		return ("protocol mismatch");
	case SCARD_F_COMM_ERROR:
		return ("communication error");
	case SCARD_F_UNKNOWN_ERROR:
		return ("unknown error");
	case SCARD_E_READER_UNAVAILABLE:
		return ("reader unavailable");
	case SCARD_E_NO_SERVICE:
		return ("service error");
	case SCARD_E_UNSUPPORTED_FEATURE:
		return ("ICC requires unsupported feature");
	case SCARD_E_NO_READERS_AVAILABLE:
		return ("no readers avaiable");
	case SCARD_W_UNSUPPORTED_CARD:
		return ("ICC unsupported");
	case SCARD_W_UNPOWERED_CARD:
		return ("ICC is not powered");
	case SCARD_W_RESET_CARD:
		return ("ICC was reset");
	case SCARD_W_REMOVED_CARD:
		return ("ICC has been removed");
	default:
		return ("unknown error");
	}
}


/*
 * This is called when a caller wishes to open a new Library context.
 */
LONG
SCardEstablishContext(DWORD scope, LPCVOID unused0, LPCVOID unused1,
    LPSCARDCONTEXT outp)
{
	pcsc_hdl_t *hdl;

	if (outp == NULL) {
		return (SCARD_E_INVALID_PARAMETER);
	}

	if (scope != SCARD_SCOPE_SYSTEM) {
		return (SCARD_E_INVALID_VALUE);
	}

	hdl = calloc(1, sizeof (pcsc_hdl_t));
	if (hdl == NULL) {
		return (SCARD_E_NO_MEMORY);
	}

	hdl->pcsc_create_time = gethrtime();
	*outp = hdl;
	return (SCARD_S_SUCCESS);
}

/*
 * This is called to free a library context from a client.
 */
LONG
SCardReleaseContext(SCARDCONTEXT hdl)
{
	free(hdl);
	return (SCARD_S_SUCCESS);
}

/*
 * This is called to release memory allocated by the library. No, it doesn't
 * make sense to take a const pointer when being given memory to free. It just
 * means we have to cast it, but remember: this isn't our API.
 */
LONG
SCardFreeMemory(SCARDCONTEXT unused, LPCVOID mem)
{
	free((void *)mem);
	return (SCARD_S_SUCCESS);
}

/*
 * This is called by a caller to get a list of readers that exist in the system.
 * If lenp is set to SCARD_AUTOALLOCATE, then we are responsible for dealing
 * with this memory.
 */
LONG
SCardListReaders(SCARDCONTEXT unused, LPCSTR groups, LPSTR bufp, LPDWORD lenp)
{
	FTS *fts;
	FTSENT *ent;
	char *const root[] = { "/dev/ccid", NULL };
	char *ubuf;
	char **readers;
	uint32_t len, ulen, npaths, nalloc, off, i;
	int ret;

	if (groups != NULL || lenp == NULL) {
		return (SCARD_E_INVALID_PARAMETER);
	}

	fts = fts_open(root, FTS_LOGICAL | FTS_NOCHDIR, NULL);
	if (fts == NULL) {
		switch (errno) {
		case ENOENT:
		case ENOTDIR:
			return (SCARD_E_NO_READERS_AVAILABLE);
		case ENOMEM:
		case EAGAIN:
			return (SCARD_E_NO_MEMORY);
		default:
			return (SCARD_E_NO_SERVICE);
		}
	}

	npaths = nalloc = 0;
	/*
	 * Account for the NUL we'll have to place at the end of this.
	 */
	len = 1;
	readers = NULL;
	while ((ent = fts_read(fts)) != NULL) {
		size_t plen;

		if (ent->fts_level != 2 || ent->fts_info == FTS_DP)
			continue;

		if (ent->fts_info == FTS_ERR || ent->fts_info == FTS_NS)
			continue;

		if (S_ISCHR(ent->fts_statp->st_mode) == 0)
			continue;

		plen = strlen(ent->fts_path) + 1;
		if (UINT32_MAX - len <= plen) {
			/*
			 * I mean, it's true. But I wish I could just give you
			 * EOVERFLOW.
			 */
			ret = SCARD_E_INSUFFICIENT_BUFFER;
			goto out;
		}

		if (npaths == nalloc) {
			char **tmp;

			nalloc += 8;
			tmp = reallocarray(readers, nalloc, sizeof (char *));
			if (tmp == NULL) {
				ret = SCARD_E_NO_MEMORY;
				goto out;
			}
			readers = tmp;
		}
		readers[npaths] = strdup(ent->fts_path);
		npaths++;
		len += plen;
	}

	if (npaths == 0) {
		ret = SCARD_E_NO_READERS_AVAILABLE;
		goto out;
	}

	ulen = *lenp;
	*lenp = len;
	if (ulen != SCARD_AUTOALLOCATE) {
		if (bufp == NULL) {
			ret = SCARD_S_SUCCESS;
			goto out;
		}

		if (ulen < len) {
			ret = SCARD_E_INSUFFICIENT_BUFFER;
			goto out;
		}

		ubuf = bufp;
	} else {
		char **bufpp;
		if (bufp == NULL) {
			ret = SCARD_E_INVALID_PARAMETER;
			goto out;
		}

		ubuf = malloc(ulen);
		if (ubuf == NULL) {
			ret = SCARD_E_NO_MEMORY;
			goto out;
		}

		bufpp = (void *)bufp;
		*bufpp = ubuf;
	}
	ret = SCARD_S_SUCCESS;

	for (off = 0, i = 0; i < npaths; i++) {
		size_t slen = strlen(readers[i]) + 1;
		bcopy(readers[i], ubuf + off, slen);
		off += slen;
		VERIFY3U(off, <=, len);
	}
	VERIFY3U(off, ==, len - 1);
	ubuf[off] = '\0';
out:
	for (i = 0; i < npaths; i++) {
		free(readers[i]);
	}
	free(readers);
	(void) fts_close(fts);
	return (ret);
}

static LONG
uccid_status_helper(int fd, DWORD prots, uccid_cmd_status_t *ucs)
{
	/*
	 * Get the status of this slot and find out information about the slot.
	 * We need to see if there's an ICC present and if it matches the
	 * current protocol. If not, then we have to fail this.
	 */
	bzero(ucs, sizeof (uccid_cmd_status_t));
	ucs->ucs_version = UCCID_CURRENT_VERSION;
	if (ioctl(fd, UCCID_CMD_STATUS, ucs) != 0) {
		return (SCARD_F_UNKNOWN_ERROR);
	}

	if ((ucs->ucs_status & UCCID_STATUS_F_CARD_PRESENT) == 0) {
		return (SCARD_W_REMOVED_CARD);
	}

	if ((ucs->ucs_status & UCCID_STATUS_F_CARD_ACTIVE) == 0) {
		return (SCARD_W_UNPOWERED_CARD);
	}

	if ((ucs->ucs_status & UCCID_STATUS_F_PARAMS_VALID) == 0) {
		return (SCARD_W_UNSUPPORTED_CARD);
	}

	if ((ucs->ucs_prot & prots) == 0) {
		return (SCARD_E_PROTO_MISMATCH);
	}

	return (0);
}

LONG
SCardConnect(SCARDCONTEXT hdl, LPCSTR reader, DWORD mode, DWORD prots,
    LPSCARDHANDLE iccp, LPDWORD protp)
{
	LONG ret;
	uccid_cmd_status_t ucs;
	pcsc_card_t *card;

	if (reader == NULL) {
		return (SCARD_E_UNKNOWN_READER);
	}

	if (iccp == NULL || protp == NULL) {
		return (SCARD_E_INVALID_PARAMETER);
	}

	if (mode != SCARD_SHARE_SHARED) {
		return (SCARD_E_INVALID_VALUE);
	}

	if ((prots & ~(SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1 |
	    SCARD_PROTOCOL_RAW | SCARD_PROTOCOL_T15)) != 0) {
		return (SCARD_E_INVALID_VALUE);
	}

	if ((prots & (SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)) == 0) {
		return (SCARD_E_UNSUPPORTED_FEATURE);
	}

	if ((card = malloc(sizeof (*card))) == NULL) {
		return (SCARD_E_NO_MEMORY);
	}

	if ((card->pcc_fd = open(reader, O_RDWR)) < 0) {
		free(card);
		switch (errno) {
		case ENOENT:
			return (SCARD_E_UNKNOWN_READER);
		default:
			return (SCARD_F_UNKNOWN_ERROR);
		}
	}

	if ((ret = uccid_status_helper(card->pcc_fd, prots, &ucs)) != 0)
		goto cleanup;

	*protp = ucs.ucs_prot;
	*iccp = card;
	return (SCARD_S_SUCCESS);
cleanup:
	(void) close(card->pcc_fd);
	free(card);
	return (ret);
}

LONG
SCardDisconnect(SCARDHANDLE arg, DWORD disposition)
{
	pcsc_card_t *card = arg;

	if (arg == NULL) {
		return (SCARD_E_INVALID_HANDLE);
	}

	if (disposition != SCARD_LEAVE_CARD) {
		return (SCARD_E_INVALID_VALUE);
	}

	if (close(card->pcc_fd) != 0) {
		return (SCARD_F_UNKNOWN_ERROR);
	}

	free(card);
	return (SCARD_S_SUCCESS);
}

LONG
SCardBeginTransaction(SCARDHANDLE arg)
{
	uccid_cmd_txn_begin_t txn;
	pcsc_card_t *card = arg;

	if (card == NULL) {
		return (SCARD_E_INVALID_HANDLE);
	}

	/*
	 * The semantics of pcsc are that this operation does not block, but
	 * instead fails if we cannot grab it immediately.
	 */
	bzero(&txn, sizeof (uccid_cmd_txn_begin_t));
	txn.uct_version = UCCID_CURRENT_VERSION;
	txn.uct_flags = UCCID_TXN_DONT_BLOCK;

	if (ioctl(card->pcc_fd, UCCID_CMD_TXN_BEGIN, &txn) != 0) {
		VERIFY3S(errno, !=, EFAULT);
		switch (errno) {
		case ENODEV:
			return (SCARD_E_READER_UNAVAILABLE);
		case EEXIST:
			/*
			 * This is an odd case. It's used to tell us that we
			 * already have it. For now, just treat it as success.
			 */
			return (SCARD_S_SUCCESS);
		case EBUSY:
			return (SCARD_E_SHARING_VIOLATION);
		/*
		 * EINPROGRESS is a weird case. It means that we were trying to
		 * grab a hold while another instance using the same handle was.
		 * For now, treat it as an unknown error.
		 */
		case EINPROGRESS:
		case EINTR:
		default:
			return (SCARD_F_UNKNOWN_ERROR);
		}
	}
	return (SCARD_S_SUCCESS);
}

LONG
SCardEndTransaction(SCARDHANDLE arg, DWORD state)
{
	uccid_cmd_txn_end_t txn;
	pcsc_card_t *card = arg;

	if (card == NULL) {
		return (SCARD_E_INVALID_HANDLE);
	}

	bzero(&txn, sizeof (uccid_cmd_txn_end_t));
	txn.uct_version = UCCID_CURRENT_VERSION;

	switch (state) {
	case SCARD_LEAVE_CARD:
		txn.uct_flags = UCCID_TXN_END_RELEASE;
		break;
	case SCARD_RESET_CARD:
		txn.uct_flags = UCCID_TXN_END_RESET;
		break;
	case SCARD_UNPOWER_CARD:
	case SCARD_EJECT_CARD:
	default:
		return (SCARD_E_INVALID_VALUE);
	}

	if (ioctl(card->pcc_fd, UCCID_CMD_TXN_END, &txn) != 0) {
		VERIFY3S(errno, !=, EFAULT);
		switch (errno) {
		case ENODEV:
			return (SCARD_E_READER_UNAVAILABLE);
		case ENXIO:
			return (SCARD_E_SHARING_VIOLATION);
		default:
			return (SCARD_F_UNKNOWN_ERROR);
		}
	}
	return (SCARD_S_SUCCESS);
}

LONG
SCardReconnect(SCARDHANDLE arg, DWORD mode, DWORD prots, DWORD init,
    LPDWORD protp)
{
	uccid_cmd_status_t ucs;
	pcsc_card_t *card = arg;
	LONG ret;

	if (card == NULL) {
		return (SCARD_E_INVALID_HANDLE);
	}

	if (protp == NULL) {
		return (SCARD_E_INVALID_PARAMETER);
	}

	if (mode != SCARD_SHARE_SHARED) {
		return (SCARD_E_INVALID_VALUE);
	}

	if ((prots & ~(SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1 |
	    SCARD_PROTOCOL_RAW | SCARD_PROTOCOL_T15)) != 0) {
		return (SCARD_E_INVALID_VALUE);
	}

	if ((prots & (SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)) == 0) {
		return (SCARD_E_UNSUPPORTED_FEATURE);
	}

	if (init != SCARD_LEAVE_CARD) {
		return (SCARD_E_INVALID_VALUE);
	}

	if ((ret = uccid_status_helper(card->pcc_fd, prots, &ucs)) != 0)
		return (ret);

	*protp = ucs.ucs_prot;
	return (SCARD_S_SUCCESS);
}

LONG
SCardTransmit(SCARDHANDLE arg, const SCARD_IO_REQUEST *sendreq,
    LPCBYTE sendbuf, DWORD sendlen, SCARD_IO_REQUEST *recvreq, LPBYTE recvbuf,
    LPDWORD recvlenp)
{
	int len;
	ssize_t ret;
	pcsc_card_t *card = arg;

	if (card == NULL) {
		return (SCARD_E_INVALID_HANDLE);
	}

	/*
	 * Ignore sendreq / recvreq.
	 */
	if (sendbuf == NULL || recvbuf == NULL || recvlenp == NULL) {
		return (SCARD_E_INVALID_PARAMETER);
	}

	/*
	 * The CCID write will always consume all data or none.
	 */
	ret = write(card->pcc_fd, sendbuf, sendlen);
	if (ret == -1) {
		switch (errno) {
		case E2BIG:
			return (SCARD_E_INVALID_PARAMETER);
		case ENODEV:
			return (SCARD_E_READER_UNAVAILABLE);
		case EACCES:
		case EBUSY:
			return (SCARD_E_SHARING_VIOLATION);
		case ENXIO:
			return (SCARD_W_REMOVED_CARD);
		case EFAULT:
			return (SCARD_E_INVALID_PARAMETER);
		case ENOMEM:
		default:
			return (SCARD_F_UNKNOWN_ERROR);
		}
	}
	ASSERT3S(ret, ==, sendlen);

	/*
	 * Now, we should be able to block in read.
	 */
	ret = read(card->pcc_fd, recvbuf, *recvlenp);
	if (ret == -1) {
		switch (errno) {
		case EINVAL:
		case EOVERFLOW:
			/*
			 * This means that we need to update len with the real
			 * one.
			 */
			if (ioctl(card->pcc_fd, FIONREAD, &len) != 0) {
				return (SCARD_F_UNKNOWN_ERROR);
			}
			*recvlenp = len;
			return (SCARD_E_INSUFFICIENT_BUFFER);
		case ENODEV:
			return (SCARD_E_READER_UNAVAILABLE);
		case EACCES:
		case EBUSY:
			return (SCARD_E_SHARING_VIOLATION);
		case EFAULT:
			return (SCARD_E_INVALID_PARAMETER);
		case ENODATA:
		default:
			return (SCARD_F_UNKNOWN_ERROR);
		}
	}

	*recvlenp = ret;

	return (SCARD_S_SUCCESS);
}