How to access PCSC card readers via erlang/elixir?

Hi,

i was wondering if there is a way to access smart cards via a PCSC reader.
I was unable to find any lib or bindings to do so (here’s a list samples in different languages [1]).

Maybe there is a different way to accomplish the task, or there has never been a use case for erlang centric applications.

Thanks in advance
T

A while back I wrote a library which interfaces with libnfc compatible card readers:

It uses a NIF under the hood to do the device IO. It is quote limited at the moment though, it has only been used to read Mifare NFC cards. But it maybe could get you started.

2 Likes

I’ve just recently had reason to put together a proper libpcsc binding for Erlang:

If you’re still looking, let me know what you think. Also includes some conveniences like ISO7816 command and response chaining support in an “APDU transform” so you can just send a long data field or get a long reply back.

I don’t use Elixir a lot myself, so I’m not sure how the API will look from that side, but I’m happy to try to improve it.

2 Likes

Thanks a lot. I will give it a try once I find some time a reader :).

In the meantime, you might want to contact Ludovic Rousseau to add the library to his list of PC/SC samples in different languages [1]

[1] https://ludovicrousseau.blogspot.com/2010/04/pcsc-sample-in-different-languages.html

2 Likes

I am trying to use erlang-pcsc from @arekinath.
But that is my first Erlang program and I blocked on a stupid problem :frowning:

I created a sample code at GitHub - LudovicRousseau/erlang-pcsc-sample: Sample code of PC/SC in Erlang
I use rebar3 escriptize to create a binary.
Then I start it using:

$ ./_build/default/bin/blog 
Hello world!
escript: exception error: undefined function pcsc_card_db:list_readers/0
  in function  escript:run/2 (escript.erl, line 750)
  in call from escript:start/1 (escript.erl, line 277)
  in call from init:start_em/1 
  in call from init:do_boot/3

My code does not find the pcsc functions. I guess it is trivial to fix once you know Erlang a bit more.

My goal is to add an Erlang PC/SC sample to Ludovic Rousseau's blog: PC/SC sample in different languages

I also tried:

$ rebar3 shell
===> Verifying dependencies...
===> Analyzing applications...
===> Compiling blog
Erlang/OTP 24 [erts-12.3.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit]

Eshell V12.3.1  (abort with ^G)
1> ===> Booted blog
pcsc_card_db:list_readers().
** exception exit: {noproc,{gen_server,call,[pcsc_card_db,list_readers]}}
     in function  gen_server:call/2 (gen_server.erl, line 239)
2>

Here the function list_readers() is found but I get an exception.

Sorry to ask stupid questions. Can someone help?

Hi,

changing my rebar.config to

{erl_opts, [debug_info]}.
{deps, [pcsc]}.

{escript_incl_apps, [pcsc]}.

{shell, [
  % {config, "config/sys.config"},
    {apps, [pcsc, blog]}
]}.

gets me further.

With the {escript_incl_apps, [pcsc]} the error changes from undefined function to noproc. The library should now be packaged properly with the escript.
The noproc error occurs in this case, because the pcsc application is not started. So I tried adding it in the apps declaration {apps, [pcsc, blog]} ← works only for shell so far.
When I run rebar3 shell I still get an error because my architectures don’t match, and the NIF can’t load the C code. I’ll try to get around this, but maybe this already solves your problem.
Edit:
I got it working in the shell on a different machine

pcsc_card_db:list_readers()
{ok,[]}

still no luck with the escript tough …

Thanks and good luck
Chris

So after some more research it is enough to add pcsc to the applications in src/blog.app.src

diff --git a/blog/src/blog.app.src b/blog/src/blog.app.src
index 4793c20..94b4591 100644
--- a/blog/src/blog.app.src
+++ b/blog/src/blog.app.src
@@ -5,7 +5,8 @@
   {mod, {blog_app, []}},
   {applications,
    [kernel,
-    stdlib
+    stdlib,
+    pcsc
    ]},
   {env,[]},
   {modules, []},

This makes the rebar3 shell command work. However, packaging a NIF with an escript seems to be more complicated [1], [2]. The shell example should be enough as a proof of concept though, as the packaging/release method is up to the user.

hope this helps

[1] Escript: can not load argon2_nif - wrong approach for CLI?
[2] escripts look for nifs (.so) in wrong location · Issue #2129 · erlang/rebar3 · GitHub

1 Like

I added “pcsc” in src/blog.app.src as you suggested and I see some progress.

Now I have:

$ rebar3 shell
===> Verifying dependencies...
===> Analyzing applications...
===> Compiling blog
Erlang/OTP 24 [erts-12.3.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit]

Eshell V12.3.1  (abort with ^G)
1> ===> Booted syntax_tools
===> Booted compiler
===> Booted goldrush
===> Booted lager
===> Booted pcsc
===> Booted blog
16:51:31.667 [warning] got pcsc_reader_error: {error,{pcsc_error,-2146435026,no_readers,"Cannot find a smart card reader."}}; restarted ok
16:51:31.721 [warning] got pcsc_reader_error: {error,{pcsc_error,-2146435026,no_readers,"Cannot find a smart card reader."}}; restarted ok
16:51:31.775 [warning] got pcsc_reader_error: {error,{pcsc_error,-2146435026,no_readers,"Cannot find a smart card reader."}}; restarted ok
16:51:31.831 [warning] got pcsc_reader_error: {error,{pcsc_error,-2146435026,no_readers,"Cannot find a smart card reader."}}; restarted ok
16:51:31.887 [warning] got pcsc_reader_error: {error,{pcsc_error,-2146435026,no_readers,"Cannot find a smart card reader."}}; restarted ok
16:51:31.945 [warning] got pcsc_reader_error: {error,{pcsc_error,-2146435026,no_readers,"Cannot find a smart card reader."}}; restarted ok
16:51:32.008 [warning] got pcsc_reader_error: {error,{pcsc_error,-2146435026,no_readers,"Cannot find a smart card reader."}}; restarted ok

Error -2146435026 is SCARD_E_NO_READERS_AVAILABLE and is not really an error.

Reported upstream at [warning] got pcsc_reader_error: {error,{pcsc_error,-2146435026,no_readers,"Cannot find a smart card reader."}}; restarted ok · Issue #4 · arekinath/erlang-pcsc · GitHub

Thanks @tofferoma.
I may ask for more help very soon about Elixir :slight_smile:

I added a pull request for running the code as a relx release via rebar3_run plugin. Maybe someone finds it interesting.

Looking forward to the elixir version :slight_smile:

@LudovicRousseau I took the liberty to put together an elixir example, which can be found here: GitHub - tofferoma/elixir_pcsc_sample

hope it helps

My Elixir sample is ready.
See Ludovic Rousseau's blog: PC/SC sample in Elixir

Thanks to alll

1 Like

My Erlang sample is also ready.
See Ludovic Rousseau's blog: PC/SC sample in Erlang

Thanks

Is it possible in principle to use those NFC libraries to exchange information between 2 NFC enabled mobile phones ? As an example use it between customer and merchant to get payment details instead of QR or card payment?

No idea.
Maybe.