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
|
# Copyright (c) 2019 Yubico AB
# Copyright (c) 2019 Oleg Moiseenko
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from __future__ import annotations
import logging
import struct
from threading import Event
from typing import Callable, Iterator
from smartcard import System
from smartcard.CardConnection import CardConnection
from smartcard.pcsc.PCSCExceptions import ListReadersException
from .ctap import STATUS, CtapDevice, CtapError
from .hid import CAPABILITY, CTAPHID
from .utils import LOG_LEVEL_TRAFFIC
AID_FIDO = b"\xa0\x00\x00\x06\x47\x2f\x00\x01"
SW_SUCCESS = (0x90, 0x00)
SW_UPDATE = (0x91, 0x00)
SW1_MORE_DATA = 0x61
logger = logging.getLogger(__name__)
class CtapPcscDevice(CtapDevice):
"""
CtapDevice implementation using pyscard (PCSC).
This class is intended for use with NFC readers.
"""
def __init__(self, connection: CardConnection, name: str):
self._name = name
self._capabilities = CAPABILITY(0)
self.use_ext_apdu = False
self._conn = connection
self.connect()
try: # Probe for CTAP2 by calling GET_INFO
self.call(CTAPHID.CBOR, b"\x04")
self._capabilities |= CAPABILITY.CBOR
except CtapError:
if not self._capabilities:
raise ValueError("Unsupported device")
def connect(self):
self._conn.connect()
self._select()
def __repr__(self):
return f"CtapPcscDevice({self._name})"
@property
def version(self) -> int:
"""CTAPHID protocol version."""
return 2 if CAPABILITY.CBOR in self._capabilities else 1
@property
def capabilities(self) -> CAPABILITY:
"""Capabilities supported by the device."""
return self._capabilities
@property
def product_name(self) -> str | None:
"""Product name of device."""
return None
@property
def serial_number(self) -> int | None:
"""Serial number of device."""
return None
def get_atr(self) -> bytes:
"""Get the ATR/ATS of the connected card."""
return bytes(self._conn.getATR())
def apdu_exchange(
self, apdu: bytes, protocol: int | None = None
) -> tuple[bytes, int, int]:
"""Exchange data with smart card.
:param apdu: byte string. data to exchange with card
:return: byte string. response from card
"""
logger.log(LOG_LEVEL_TRAFFIC, "SEND: %s", apdu.hex())
resp, sw1, sw2 = self._conn.transmit(list(apdu), protocol)
response = bytes(resp)
logger.log(LOG_LEVEL_TRAFFIC, "RECV: %s SW=%02X%02X", response.hex(), sw1, sw2)
return response, sw1, sw2
def control_exchange(self, control_code: int, control_data: bytes = b"") -> bytes:
"""Sends control sequence to reader's driver.
:param control_code: int. code to send to reader driver.
:param control_data: byte string. data to send to driver
:return: byte string. response
"""
logger.log(LOG_LEVEL_TRAFFIC, "Send control: %s", control_data.hex())
response = self._conn.control(control_code, list(control_data))
response = bytes(response)
logger.log(LOG_LEVEL_TRAFFIC, "Control response: %s", response.hex())
return response
def _select(self) -> None:
apdu = b"\x00\xa4\x04\x00" + struct.pack("!B", len(AID_FIDO)) + AID_FIDO
resp, sw1, sw2 = self._chained_apdu_exchange(apdu)
if (sw1, sw2) != SW_SUCCESS:
raise ValueError("FIDO applet selection failure.")
if resp == b"U2F_V2":
self._capabilities |= CAPABILITY.NMSG
def _chain_apdus(
self, cla: int, ins: int, p1: int, p2: int, data: bytes = b""
) -> tuple[bytes, int, int]:
if self.use_ext_apdu:
header = struct.pack("!BBBBBH", cla, ins, p1, p2, 0x00, len(data))
resp, sw1, sw2 = self.apdu_exchange(header + data)
return resp, sw1, sw2
else:
while len(data) > 250:
to_send, data = data[:250], data[250:]
header = struct.pack("!BBBBB", 0x10 | cla, ins, p1, p2, len(to_send))
resp, sw1, sw2 = self.apdu_exchange(header + to_send)
if (sw1, sw2) != SW_SUCCESS:
return resp, sw1, sw2
apdu = struct.pack("!BBBB", cla, ins, p1, p2)
if data:
apdu += struct.pack("!B", len(data)) + data
resp, sw1, sw2 = self.apdu_exchange(apdu + b"\x00")
while sw1 == SW1_MORE_DATA:
apdu = b"\x00\xc0\x00\x00" + struct.pack("!B", sw2) # sw2 == le
lres, sw1, sw2 = self.apdu_exchange(apdu)
resp += lres
return resp, sw1, sw2
def _chained_apdu_exchange(self, apdu: bytes) -> tuple[bytes, int, int]:
if len(apdu) >= 7 and apdu[4] == 0:
# Extended APDU
data_len = struct.unpack("!H", apdu[5:7])[0]
data = apdu[7 : 7 + data_len]
elif len(apdu) == 4:
data = b""
else:
# Short APDU
data_len = apdu[4]
data = apdu[5 : 5 + data_len]
(cla, ins, p1, p2) = apdu[:4]
return self._chain_apdus(cla, ins, p1, p2, data)
def _call_apdu(self, apdu: bytes) -> bytes:
resp, sw1, sw2 = self._chained_apdu_exchange(apdu)
return resp + struct.pack("!BB", sw1, sw2)
def _call_cbor(
self,
data: bytes = b"",
event: Event | None = None,
on_keepalive: Callable[[STATUS], None] | None = None,
) -> bytes:
event = event or Event()
# NFCCTAP_MSG
resp, sw1, sw2 = self._chain_apdus(0x80, 0x10, 0x80, 0x00, data)
last_ka = None
while not event.is_set():
while (sw1, sw2) == SW_UPDATE:
ka_status = STATUS(resp[0])
if on_keepalive and last_ka != ka_status:
last_ka = ka_status
on_keepalive(ka_status)
# NFCCTAP_GETRESPONSE
resp, sw1, sw2 = self._chain_apdus(0x80, 0x11, 0x00, 0x00)
if (sw1, sw2) != SW_SUCCESS:
raise CtapError(CtapError.ERR.OTHER) # TODO: Map from SW error
return resp
raise CtapError(CtapError.ERR.KEEPALIVE_CANCEL)
def call(
self,
cmd: int,
data: bytes = b"",
event: Event | None = None,
on_keepalive: Callable[[STATUS], None] | None = None,
) -> bytes:
if cmd == CTAPHID.CBOR:
return self._call_cbor(data, event, on_keepalive)
elif cmd == CTAPHID.MSG:
return self._call_apdu(data)
else:
raise CtapError(CtapError.ERR.INVALID_COMMAND)
def close(self) -> None:
self._conn.disconnect()
@classmethod
def list_devices(cls, name: str = "") -> Iterator[CtapPcscDevice]:
for reader in _list_readers():
if name in reader.name:
try:
yield cls(reader.createConnection(), reader.name)
except Exception as e:
logger.debug("Error %r", e)
def _list_readers():
try:
return System.readers()
except ListReadersException as e:
# If the PCSC system has restarted the context might be stale, try
# forcing a new context (This happens on Windows if the last reader is
# removed):
try:
from smartcard.pcsc.PCSCContext import PCSCContext
PCSCContext.instance = None
return System.readers()
except ImportError:
# As of pyscard 2.2.2 the PCSCContext singleton has been removed
raise e
|