Skip to content

adding PCSC support to nfcgate #7

@0dayfrp

Description

@0dayfrp

Hello, I'm trying to communicate from windows pc with Nfcgate server, I have not decided yet which is the best approach.
Can you please advise? below is an attempt I have made

import c2c_pb2.py, c2s_pb2.py
import socket
import socketserver
import struct
import sys
import datetime
from smartcard.System import readers
from smartcard.Exceptions import NoCardException

HOST = "0.0.0.0"
PORT = 5566

class PluginHandler:
# Existing PluginHandler code remains unchanged

class NFCGateClientHandler(socketserver.StreamRequestHandler):
# Existing init, log, and setup methods remain unchanged

def communicate_with_card(self, apdu):
    r = readers()
    if len(r) == 0:
        self.log("No smart card readers detected.", tag="SmartCard")
        return None

    try:
        connection = r[0].createConnection()
        connection.connect()
        response, sw1, sw2 = connection.transmit(apdu)
        return response + [sw1, sw2]
    except NoCardException:
        self.log("No smart card inserted.", tag="SmartCard")
        return None

def handle(self):
    # Existing handle setup code remains unchanged

    while True:
        try:
            # Existing code to read data from the socket remains unchanged

            # Here, integrate smart card communication
            # Convert data to APDU format if necessary
            apdu = list(data)  # Ensure it's in the correct format for pyscard
            card_response = self.communicate_with_card(apdu)
            
            if card_response is not None:
                self.log(f"Card response: {card_response}", tag="SmartCard")
                # Modify this part to send the card's response back
                # For example, you might send it directly back to the requester or to all clients in a session
                # This is a placeholder; adapt it to your needs
                self.wfile.write(struct.pack('!I', len(card_response)) + bytes(card_response))
            else:
                self.log("Error communicating with the card.", tag="SmartCard")

            # Break or continue based on your application's logic

        except socket.timeout:
            # Existing timeout handling remains unchanged

# Existing finish method remains unchanged

class NFCGateServer(socketserver.ThreadingTCPServer):
# Existing NFCGateServer code remains unchanged

if name == "main":
NFCGateServer((HOST, PORT), NFCGateClientHandler).serve_forever()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions