1 /* apdu.h - ISO 7816 APDU functions and low level I/O
2 * Copyright (C) 2003, 2008 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 /* ISO 7816 values for the statusword are defined here because they
26 should not be visible to the users of the actual ISO command
29 SW_MORE_DATA
= 0x6100, /* Note: that the low byte must be
31 SW_EOF_REACHED
= 0x6282,
32 SW_TERM_STATE
= 0x6285, /* Selected file is in termination state. */
33 SW_EEPROM_FAILURE
= 0x6581,
34 SW_WRONG_LENGTH
= 0x6700,
35 SW_SM_NOT_SUP
= 0x6882, /* Secure Messaging is not supported. */
36 SW_CC_NOT_SUP
= 0x6884, /* Command Chaining is not supported. */
37 SW_CHV_WRONG
= 0x6982,
38 SW_CHV_BLOCKED
= 0x6983,
39 SW_USE_CONDITIONS
= 0x6985,
40 SW_BAD_PARAMETER
= 0x6a80, /* (in the data field) */
41 SW_NOT_SUPPORTED
= 0x6a81,
42 SW_FILE_NOT_FOUND
= 0x6a82,
43 SW_RECORD_NOT_FOUND
= 0x6a83,
44 SW_REF_NOT_FOUND
= 0x6a88,
45 SW_BAD_P0_P1
= 0x6b00,
46 SW_EXACT_LENGTH
= 0x6c00,
47 SW_INS_NOT_SUP
= 0x6d00,
48 SW_CLA_NOT_SUP
= 0x6e00,
51 /* The follwoing statuswords are no real ones but used to map host
52 OS errors into status words. A status word is 16 bit so that
53 those values can't be issued by a card. */
54 SW_HOST_OUT_OF_CORE
= 0x10001, /* No way yet to differentiate
55 between errnos on a failed malloc. */
56 SW_HOST_INV_VALUE
= 0x10002,
57 SW_HOST_INCOMPLETE_CARD_RESPONSE
= 0x10003,
58 SW_HOST_NO_DRIVER
= 0x10004,
59 SW_HOST_NOT_SUPPORTED
= 0x10005,
60 SW_HOST_LOCKING_FAILED
= 0x10006,
61 SW_HOST_BUSY
= 0x10007,
62 SW_HOST_NO_CARD
= 0x10008,
63 SW_HOST_CARD_INACTIVE
= 0x10009,
64 SW_HOST_CARD_IO_ERROR
= 0x1000a,
65 SW_HOST_GENERAL_ERROR
= 0x1000b,
66 SW_HOST_NO_READER
= 0x1000c,
67 SW_HOST_ABORTED
= 0x1000d,
68 SW_HOST_NO_KEYPAD
= 0x1000e,
69 SW_HOST_ALREADY_CONNECTED
= 0x1000f
73 #define SW_EXACT_LENGTH_P(a) (((a)&~0xff) == SW_EXACT_LENGTH)
76 /* Bit flags for the card status. */
77 #define APDU_CARD_USABLE (1) /* Card is present and ready for use. */
78 #define APDU_CARD_PRESENT (2) /* Card is just present. */
79 #define APDU_CARD_ACTIVE (4) /* Card is active. */
82 /* Note , that apdu_open_reader returns no status word but -1 on error. */
83 int apdu_open_reader (const char *portstr
);
84 int apdu_open_remote_reader (const char *portstr
,
85 const unsigned char *cookie
, size_t length
,
86 int (*readfnc
) (void *opaque
,
87 void *buffer
, size_t size
),
89 int (*writefnc
) (void *opaque
,
90 const void *buffer
, size_t size
),
92 void (*closefnc
) (void *opaque
),
93 void *closefnc_value
);
94 int apdu_shutdown_reader (int slot
);
95 int apdu_close_reader (int slot
);
96 int apdu_enum_reader (int slot
, int *used
);
97 unsigned char *apdu_get_atr (int slot
, size_t *atrlen
);
99 const char *apdu_strerror (int rc
);
102 /* These APDU functions return status words. */
104 int apdu_connect (int slot
);
105 int apdu_disconnect (int slot
);
107 int apdu_activate (int slot
);
108 int apdu_reset (int slot
);
109 int apdu_get_status (int slot
, int hang
,
110 unsigned int *status
, unsigned int *changed
);
111 int apdu_check_keypad (int slot
, int command
, int pin_mode
,
112 int pinlen_min
, int pinlen_max
, int pin_padlen
);
113 int apdu_send_simple (int slot
, int extended_mode
,
114 int class, int ins
, int p0
, int p1
,
115 int lc
, const char *data
);
116 int apdu_send_simple_kp (int slot
, int class, int ins
, int p0
, int p1
,
117 int lc
, const char *data
,
119 int pinlen_min
, int pinlen_max
, int pin_padlen
);
120 int apdu_send (int slot
, int class, int ins
, int p0
, int p1
,
121 int lc
, const char *data
,
122 unsigned char **retbuf
, size_t *retbuflen
);
123 int apdu_send_le (int slot
, int class, int ins
, int p0
, int p1
,
124 int lc
, const char *data
, int le
,
125 unsigned char **retbuf
, size_t *retbuflen
);
126 int apdu_send_direct (int slot
,
127 const unsigned char *apdudata
, size_t apdudatalen
,
129 unsigned char **retbuf
, size_t *retbuflen
);