Git Product home page Git Product logo

pcsc-sharp's Introduction

PC/SC wrapper classes for .NET

Introduction

The pcsc-sharp library is wrapper that provides access to the Personal Computer/Smart Card Resource Manager using the system's native PC/SC API. It implements partial ISO7816 support and is written to run on both Windows and Unix (Mono using PCSC Lite).

pcsc-sharp is not a fully featured library for accessing vendor specific protocols. You must implement those protocols / applications yourself. For example: You can use pcsc-sharp to access NXP's Mirfare RFID chips, but pcsc-sharp does not provide any APDUs to request KEYs, authorize, etc.

You can find PC/SC specific documentation here:

Supported Operating systems

  • Windows (winscard.dll)

    • Windows 10 64-bit Professional (confirmed)
    • Windows 10 32-bit Professional
    • Windows 7 64-bit
    • Windows 7 32-bit
  • Linux (PC/SC lite)

    • Ubuntu Linux 64-bit (confirmed)
    • Ubuntu Linux 32-bit
  • MacOS X (unconfirmed, software prerequisites unclear)

Quick start

Establish the resource manager context

Each operation requires a valid context. See SCardEstablishContext for more information.

var contextFactory = ContextFactory.Instance;
using (var context = contextFactory.Establish(SCardScope.System)) {
   // your code
}

Basic rules / best practices:

  • One context per smartcard / reader.
  • One context per SCardMonitor .
  • The context must be disposed of after usage to free system resources.
  • The context must not be disposed of if your application still accesses the smartcard / reader.

List all connected smartcard readers

See SCardListReaders.

var contextFactory = ContextFactory.Instance;
using (var context = contextFactory.Establish(SCardScope.System)) {
    Console.WriteLine("Currently connected readers: ");
    var readerNames = context.GetReaders();
    foreach (var readerName in readerNames) {
        Console.WriteLine("\t" + readerName);
    }
}

Send APDUs

var contextFactory = ContextFactory.Instance;
using (var ctx = contextFactory.Establish(SCardScope.System)) {
    using (var isoReader = new IsoReader(ctx, "ACME Smartcard reader", SCardShareMode.Shared, SCardProtocol.Any, false)) {
        
        var apdu = new CommandApdu(IsoCase.Case2Short, isoReader.ActiveProtocol) {
            CLA = 0x00, // Class
            Instruction = InstructionCode.GetChallenge,
            P1 = 0x00, // Parameter 1
            P2 = 0x00, // Parameter 2
            Le = 0x08 // Expected length of the returned data
        };

        var response = isoReader.Transmit(apdu);
        Console.WriteLine("SW1 SW2 = {0:X2} {1:X2}", response.SW1, response.SW2);
        // ..
    }
}

Monitor reader events

var contextFactory = ContextFactory.Instance;
var monitor = new SCardMonitor(contextFactory, SCardScope.System);

// connect events here..
monitor.StatusChanged += (sender, args) => 
    Console.WriteLine($"New state: {args.NewState}");

monitor.Start("OMNIKEY CardMan 5x21-CL 0");

Console.ReadKey(); // Press any key to exit

monitor.Cancel();
monitor.Dispose();

More example code

Checkout the Examples directory.

Build from source

pcsc-sharp uses the great FAKE DSL for build tasks and the awesome Paket for NuGet package management. To build the solution, simply start the build.cmd on Windows or the build shell script on Unix. Add NuGet as command line argument to create NuGet packages.

If you want to open the solution with your favorite IDE, restore the NuGet packages first.

On Windows run

 paket.cmd restore 

On Unix/Linux run

 ./paket restore

on Unix/Linux. to download NuGet dependencies before opening the solution in Visual Studio or MonoDevelop.

pcsc-sharp's People

Contributors

danm-de avatar mtausig avatar glenebob avatar kdeeley avatar pfirpfel avatar emptysamurai avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.