Important!

Blog moved to https://blog.apdu.fr/

I moved my blog from https://ludovicrousseau.blogspot.com/ to https://blog.apdu.fr/ . Why? I wanted to move away from Blogger (owne...

Thursday, March 6, 2014

Evolution of Apple pcsc-lite (from Jaguar to Mavericks)

When Apple first introduced the smart card support in Mac OS X 10.2 Jaguar (August 2002) pcsc-lite was at version 1.0.x and I just started to add my patches to it. At that time the author and maintainer of pcsc-lite was David Corcoran.

In this article we will do some archaeology work. Apple moved the SmartCardServices to the SmartCard Services public project in January 2009 at the time of Leopard 10.5. The first tag on source code available in the subversion repository is for OSX-10.5.6. The detailed history of the source code before that version is not publicly available and must be reverse engineered "by hand".

On the "official" pcsc-lite side, the oldest tagged version is 1.1.2-beta1 from September 2002.

Terminology

The pcsc-lite provided by Apple is called, in this article, Apple pcsc-lite.

The original pcsc-lite and ancestor of Apple pcsc-lite is called "official" pcsc-lite. The "official" pcsc-lite is used mainly on GNU/Linux (and GNU/Linux distributions) and is also used on FreeBSD, NetBSD, OpenBSD, Android, and maybe other Unix-like systems I don't know about.

Oracle (ex-Sun) has its own forked version of pcsc-lite for Solaris. An old version of this source code is available in the Solaris branch.

10.2 Jaguar

August 2002

SmartCardServices-10

The PCSCLITE_VERSION_NUMBER in src/pcsclite.h is "1.1.1". The same 1.1.1 version of the "official" pcsc-lite was released 5 Jun, 2002. I wrote above I do not have the version 1.1.1 in the subversion history. So we will use version 1.1.2-beta1 instead.

A diffstat between the two versions gives us:
 debuglog.c         |   76 +++++++++++++++++++++++++++++++++++++-----
 eventhandler.c     |    1 
 ifdwrapper.c       |    8 ++++
 pcscdaemon.c       |   94 +++++++++++++++++++++++++++++++++++++++++++----------
 powermgt_generic.h |    2 -
 readerfactory.c    |    4 +-
 sys_unix.c         |    2 -
 winscard_clnt.c    |    2 -
 winscard_msg.c     |    2 -
 9 files changed, 157 insertions(+), 34 deletions(-)

New code in "official" pcsc-lite (and then not present in Apple pcsc-lite):
  • Win32 support in log functions
  • APDU log
  • store pcscd pid in a file to detect another running pcscd
No code added in Apple version of pcsc-lite.
    So it looks like that Apple just used version 1.1.1 of pcsc-lite without changes at this first step.

    10.3 Panther

    October 2003

    SmartCardServices-15

    Diffstat against the previous version:
    $ diff -ru SmartCardServices-10/src/ SmartCardServices-15/src/PCSC/ | diffstat
     atrhandler.c       |   18 
     atrhandler.h       |   17 
     configfile.c       |   17 
     configfile.h       |   17 
     debuglog.c         |   93 +++-
     debuglog.h         |   49 +-
     dyn_generic.h      |   17 
     dyn_macosx.c       |   17 
     eventhandler.c     |   23 +
     eventhandler.h     |   17 
     hotplug.h          |   28 +
     hotplug_macosx.c   | 1211 +++++++++++++++++++++++++++--------------------------
     ifdhandler.h       |   17 
     ifdwrapper.c       |   17 
     ifdwrapper.h       |   17 
     mscdefines.h       |   30 -
     musclecard.c       |   28 +
     musclecard.h       |   30 -
     muscletest.c       |   19 
     pcscdaemon.c       |  117 ++++-
     pcsclite.h         |   32 -
     powermgt_generic.h |   19 
     powermgt_macosx.c  |   40 +
     prothandler.c      |   19 
     prothandler.h      |   17 
     readerfactory.c    |   28 +
     readerfactory.h    |   17 
     sys_generic.h      |   19 
     sys_unix.c         |   19 
     testpcsc.c         |   17 
     thread_generic.h   |   17 
     thread_macosx.c    |   17 
     tokenfactory.c     |   37 +
     tokenfactory.h     |   29 +
     tokenparser.c      |   17 
     utils/bundleTool.c |   62 +-
     winscard.c         |   25 +
     winscard.h         |   30 -
     winscard_clnt.c    |   19 
     winscard_msg.c     |   19 
     winscard_msg.h     |   17 
     winscard_svc.c     |   17 
     winscard_svc.h     |   17 
     wintypes.h         |   32 -
     44 files changed, 1617 insertions(+), 764 deletions(-)
    

    Changes:
    • integration of CACPlugin, CFlexPlugin, MCardPlugin and PKCS11 directories. pcsc-lite is now in its own PCSC directory
    • add the Apple license (Apple Public Source License Version 1.2) in each source code file
    • merge with pcsc-lite 1.2.0 rc1 (or a more recent version)
    • move from /usr/local/pcsc to /usr/libexec/SmartCardServices in hotplug_macosx.c plus other changes specific to Mac OS X
    It looks like Apple resynchronised with the "official" pcsc-lite at that time.

    10.4 Tiger

    April 2005
    Diffstat between 10.3 and 10.4.0 version
    $ diff -ru SmartCardServices-15 SmartCardServices-31 | diffstat 
     APPLE_LICENSE                      |  509 ++++++++---------
     Makefile.installPhase              |   45 +
     installPhase/man/pcscd.8           |   16 
     installPhase/man/pcsctool.8        |    2 
     src/CACPlugin/commonAccessCard.c   |   24 
     src/CFlexPlugin/cryptoflex.c       |    3 
     src/CFlexPlugin/cryptoflex.h       |    1 
     src/MCardPlugin/musclecardApplet.c |   35 -
     src/PCSC/eventhandler.c            |    7 
     src/PCSC/hotplug_macosx.c          | 1065 +++++++++++++++++++++----------------
     src/PCSC/mscdefines.h              |   29 -
     src/PCSC/musclecard.c              |   83 +-
     src/PCSC/musclecard.h              |   29 -
     src/PCSC/pcscdaemon.c              |   88 ++-
     src/PCSC/pcsclite.h                |   31 -
     src/PCSC/powermgt_macosx.c         |   25 
     src/PCSC/readerfactory.c           |    4 
     src/PCSC/tokenfactory.c            |   12 
     src/PCSC/tokenfactory.h            |    5 
     src/PCSC/winscard.c                |   38 -
     src/PCSC/winscard.h                |   29 -
     src/PCSC/winscard_clnt.c           |  387 +++++++------
     src/PCSC/winscard_msg.c            |  320 -----------
     src/PCSC/winscard_msg.h            |  203 +++----
     src/PCSC/winscard_svc.c            |  496 ++++++++++++++---
     src/PCSC/winscard_svc.h            |   65 ++
     src/PCSC/wintypes.h                |   29 -
     src/PKCS11/p11_crypt.c             |    3 
     src/PKCS11/p11x_object.c           |    6 
     29 files changed, 2016 insertions(+), 1573 deletions(-)
    

    Changes:
    • hotplug_macosx.c: iterate on all the interfaces of a USB device and other changes
    • client/server communication protocol: redesign the protocol to use a 2 parts header+body in the commands

    Diffstat between 10.4.0 and 10.4.11 PowerPC
    $ diff -ru SmartCardServices-31 SmartCardServices-27738 | diffstat
     SmartCardServices.xcode/project.pbxproj |   35 
     src/PCSC/sys_generic.h                  |    4 
     src/PCSC/sys_macosx.cpp                 |    7 
     src/PCSC/sys_unix.c                     |    7 
     src/PCSC/winscard_clnt.c                | 1127 +++++++++++++++-----------------
     src/PKCS11/p11x_prefs.c                 |   32 
     6 files changed, 590 insertions(+), 622 deletions(-)
    

    Changes:
    • minor changes
    • a new function SYS_MUnmap is added and used in winscard_clnt.c. This function was never used in the "official" pcsc-lite.

    Diffstat between 10.4.11 PowerPC and 10.4.11 x86
    $ diff -ru SmartCardServices-27738 SmartCardServices-30487 | diffstat
     SmartCardServices.xcode/project.pbxproj                     |   55 +-
     installPhase/drivers/SCR24XHndlr.bundle/Contents/Info.plist |    2 
     src/CACPlugin/commonAccessCard.c                            |    2 
     src/CCIDDriver/USB/MacOSX/usbserial_mosx.c                  |   22 
     src/CCIDDriver/USB/MacOSX/usbserial_mosx.h                  |    4 
     src/CCIDDriver/common/CCID.c                                |    8 
     src/CCIDDriver/common/CCIDPropExt.c                         |    2 
     src/PCSC/debuglog.c                                         |   10 
     src/PCSC/debuglog.h                                         |    2 
     src/PCSC/eventhandler.c                                     |   42 -
     src/PCSC/hotplug_macosx.c                                   |  267 ++++++------
     src/PCSC/mscdefines.h                                       |   28 -
     src/PCSC/musclecard.c                                       |    4 
     src/PCSC/musclecard.h                                       |    2 
     src/PCSC/pcscdaemon.c                                       |   15 
     src/PCSC/pcsclite.h                                         |   18 
     src/PCSC/readerfactory.c                                    |   10 
     src/PCSC/testpcsc.c                                         |   10 
     src/PCSC/winscard.h                                         |   74 +--
     src/PCSC/winscard_clnt.c                                    |   92 ++--
     src/PCSC/winscard_msg.h                                     |   10 
     src/PCSC/winscard_svc.c                                     |  122 +++++
     src/PCSC/winscard_svc.h                                     |    5 
     src/PCSC/wintypes.h                                         |   26 -
     src/PKCS11/p11_crypt.c                                      |    8 
     src/PKCS11/p11_key.c                                        |    5 
     src/PKCS11/p11_sign.c                                       |   19 
     27 files changed, 515 insertions(+), 349 deletions(-)
    

    Changes:
    • use of htonl() and ntohl() to convert between little and big endian integers
    • hotplug_macosx.c updated again
    • use of uint8_t, uint16_t, uint32_t types to support 32 and 64 bits client applications with the same pcscd server

    The major change here is the support of PowerPC and Intel CPUs at the same time.

    On an Intel Mac it is possible to run a PowerPC appplication using Rosetta. In that case the application code is for PowerPC but the PC/SC daemon is running x86 code (native code). The daemon needs to convert the byte order in all the messages exchanged between the client (PowerPC code) and server (x86 code).

    10.5 Leopard

    October 2007

    SmartCardServices-32672

    $ diff -ruN SmartCardServices-30487 SmartCardServices-32672 | diffstat
     Makefile.installPhase                       |    9 
     SmartCardServices.xcode/project.pbxproj     | 3277 ------------------------
     SmartCardServices.xcodeproj/project.pbxproj | 3030 ++++++++++++++++++++++
     installPhase/man/sc_auth.8                  |   91 
     installPhase/scripts/sc_auth                |  148 +
     src/CCIDDriver/USB/MacOSX/usbserial_mosx.c  |    4 
     src/CCIDDriver/common/tools.c               |    2 
     src/PCSC/PCSC.exp                           |   71 
     src/PCSC/PCSCDevice.cpp                     |   66 
     src/PCSC/PCSCDevice.h                       |  100 
     src/PCSC/PCSCDriverBundle.cpp               |  261 +
     src/PCSC/PCSCDriverBundle.h                 |  125 
     src/PCSC/PCSCDriverBundles.cpp              |   97 
     src/PCSC/PCSCDriverBundles.h                |   76 
     src/PCSC/atrhandler.c                       |    2 
     src/PCSC/atrhandler.h                       |    2 
     src/PCSC/config.h                           |  215 +
     src/PCSC/configfile.c                       |    4 
     src/PCSC/configfile.l                       |    2 
     src/PCSC/debug.c                            |  160 +
     src/PCSC/debug.h                            |   78 
     src/PCSC/debuglog.c                         |  340 +-
     src/PCSC/debuglog.h                         |  157 -
     src/PCSC/eventhandler.c                     |    9 
     src/PCSC/eventhandler.cpp                   |  533 +++
     src/PCSC/eventhandler.h                     |   72 
     src/PCSC/hotplug.h                          |   86 
     src/PCSC/hotplug_macosx.cpp                 |  151 +
     src/PCSC/ifdhandler.h                       |  123 
     src/PCSC/ifdwrapper.c                       |  813 ++---
     src/PCSC/ifdwrapper.h                       |   73 
     src/PCSC/musclecard.c                       |   52 
     src/PCSC/pcscdaemon.c                       |  664 ++--
     src/PCSC/pcscdmonitor.cpp                   | 1129 ++++++++
     src/PCSC/pcscdmonitor.h                     |  191 +
     src/PCSC/pcscdserver.cpp                    |  137 +
     src/PCSC/pcscdserver.h                      |  101 
     src/PCSC/pcscexport.h                       |   62 
     src/PCSC/pcsclite.h                         |  402 +-
     src/PCSC/powermgt_macosx.c                  |   31 
     src/PCSC/prothandler.c                      |  247 -
     src/PCSC/prothandler.h                      |   70 
     src/PCSC/reader.cpp                         |  165 +
     src/PCSC/reader.h                           |  147 +
     src/PCSC/readerfactory.c                    | 2070 ++++++---------
     src/PCSC/readerfactory.h                    |  200 +
     src/PCSC/readerstate.cpp                    |  108 
     src/PCSC/readerstate.h                      |  116 
     src/PCSC/sys_generic.h                      |  108 
     src/PCSC/sys_macosx.cpp                     |  222 -
     src/PCSC/sys_unix.c                         |  316 +-
     src/PCSC/testpcsc.c                         |    4 
     src/PCSC/thread_generic.h                   |   26 
     src/PCSC/thread_macosx.c                    |   38 
     src/PCSC/tokenfactory.c                     |   16 
     src/PCSC/tokenparser.c                      |    4 
     src/PCSC/winscard.c                         | 1558 ++++++-----
     src/PCSC/winscard.h                         |  103 
     src/PCSC/winscard_clnt.c                    | 3798 ++++++++++++++++++----------
     src/PCSC/winscard_msg.cpp                   | 1002 +++++++
     src/PCSC/winscard_msg.h                     |  458 ++-
     src/PCSC/winscard_msg_srv.c                 |  305 ++
     src/PCSC/winscard_svc.c                     | 1311 +++++----
     src/PCSC/winscard_svc.h                     |  136 -
     src/PCSC/wintypes.h                         |   56 
     src/PCSC/xiodevices.cpp                     |  141 +
     src/PCSC/xiodevices.h                       |   70 
     src/PKCS11/p11x_msc.c                       |   12 
     68 files changed, 16622 insertions(+), 9131 deletions(-)
    

    Changes:
    • install the sc_auth command used to configure a smart card Mac OS X login
    • support colored logs but only on Linux terminals. Apple just reused debuglog.c from pcsc-lite 1.3.3
    • use secdebug() to log debug messages (visible using dtrace)
    • reuse ifdwrapper.c, prothandler.c and pcscdaemon.c from pcsc-lite 1.4.0
    • integrate with securityd
    • create new C++ files:
      • eventhandler.cpp
      • hotplug_macosx.cpp
      • PCSCDevice.cpp
      • pcscdmonitor.cpp
      • PCSCDriverBundle.cpp
      • PCSCDriverBundles.cpp
      • pcscdserver.cpp
      • reader.cpp
      • readerstate.cpp
      • sys_macosx.cpp
      • winscard_msg.cpp
      • xiodevices.cpp
    The detection and management of smart card readers is now a C++ code.

    The new file pcscdmonitor.cpp is the link with securityd.
    From the source code:
    pcscmonitor - use PCSC to monitor smartcard reader/card state for securityd

    PCSCDMonitor is the "glue" between PCSC and the securityd objects representing smartcard-related things. Its job is to manage the daemon and translate real-world events (such as card and device insertions) into the securityd object web.

    PCSCDMonitor uses multiple inheritance to the hilt. It is (among others)
      (*) A notification listener, to listen to pcscd state notifications
      (*) A MachServer::Timer, to handle timed actions
      (*) A NotificationPort::Receiver, to get IOKit notifications of device insertions

    This version of Apple pcsc-lite is a major deviation from the "official" pcsc-lite.
    At the same time Apple merged code from the "official" pcsc-lite.

    10.6 Snow Leopard

    August 2009

    SmartCardServices-36160

    $ diff -ruN SmartCardServices-32672/src/PCSC/ SmartCardServices-36160/src/PCSC/ | diffstat
     eventhandler.cpp   |   12 +-
     hotplug.h          |    4 
     hotplug_macosx.cpp |   17 +++
     ifdwrapper.c       |   15 +--
     pcscdaemon.c       |  116 +++++++++++++++++------
     pcscdmonitor.cpp   |   96 ++++++++++++++-----
     readerfactory.c    |  141 +++++++++++++++++++++++++++-
     testpcsc.c         |   34 ++++--
     thread_macosx.c    |   23 ++--
     winscard.c         |  113 +++++++++++-----------
     winscard.h         |    2 
     winscard_clnt.c    |  131 +++++++++++++++-----------
     winscard_msg.cpp   |  122 ++++++++++++------------
     winscard_msg.h     |  264 ++++++++++++++++++++++++++---------------------------
     winscard_msg_srv.c |   50 ++++++----
     winscard_svc.c     |   43 +++++++-
     16 files changed, 761 insertions(+), 422 deletions(-)
    

    Changes:
    • Apple removed the code of the CCID driver from SmartCardServices
    • Apple now provides my CCID driver instead of another (limited) CCID driver
    • this version of SmartCardServices includes patches I made in pcsc-lite 1.5.0 and I backported to the Apple version (ifdwrapper.c and winscard.c) since I am a member of the SmartCardServices project
    • support 32 and 64-bits smart card reader driver. pcscd will restart in 32-bits if a 32-bits only driver is found
    • use OSSwapHostToBigInt64() to support big/little endian conversion and also 32/64 bits conversion

    10.7 Lion

    July 2011

    See my blog article "Mac OS X Lion and smart cards status" for more details.

    SmartCardServices-55000

    $ diff -ru SmartCardServices-36160 SmartCardServices-55000 | diffstat
     Info-PCSC.plist                             |    2 
     SmartCardServices.xcodeproj/project.pbxproj |  106 -
     src/PCSC/PCSCDevice.h                       |    1 
     src/PCSC/PCSCDriverBundle.h                 |    1 
     src/PCSC/atrhandler.c                       |  157 +
     src/PCSC/atrhandler.h                       |   49 
     src/PCSC/configfile.c                       | 2406 ++++++++++++++--------------
     src/PCSC/configfile.h                       |    2 
     src/PCSC/configfile.l                       |  211 +-
     src/PCSC/debug.c                            |   51 
     src/PCSC/debug.h                            |    2 
     src/PCSC/debuglog.c                         |  165 -
     src/PCSC/debuglog.h                         |    2 
     src/PCSC/dyn_generic.h                      |    2 
     src/PCSC/dyn_macosx.c                       |   26 
     src/PCSC/eventhandler.cpp                   |    6 
     src/PCSC/eventhandler.h                     |    2 
     src/PCSC/hotplug.h                          |    2 
     src/PCSC/hotplug_macosx.cpp                 |    1 
     src/PCSC/ifdhandler.h                       |  798 +++++++--
     src/PCSC/ifdwrapper.c                       |  232 +-
     src/PCSC/ifdwrapper.h                       |    2 
     src/PCSC/musclecard.h                       |    2 
     src/PCSC/pcscdaemon.c                       |    2 
     src/PCSC/pcscdmonitor.cpp                   |    4 
     src/PCSC/pcscdmonitor.h                     |    1 
     src/PCSC/pcscdserver.h                      |    1 
     src/PCSC/pcscexport.h                       |    2 
     src/PCSC/pcsclite.h                         |  127 -
     src/PCSC/powermgt_generic.h                 |    2 
     src/PCSC/prothandler.c                      |    2 
     src/PCSC/prothandler.h                      |    2 
     src/PCSC/reader.h                           |  340 ++-
     src/PCSC/readerfactory.c                    |   12 
     src/PCSC/readerfactory.h                    |    2 
     src/PCSC/readerstate.h                      |    1 
     src/PCSC/sys_generic.h                      |    2 
     src/PCSC/sys_macosx.cpp                     |    3 
     src/PCSC/sys_unix.c                         |    2 
     src/PCSC/thread_macosx.c                    |    2 
     src/PCSC/tokenfactory.c                     |    2 
     src/PCSC/utils/bundleTool.c                 |    2 
     src/PCSC/winscard.c                         |    2 
     src/PCSC/winscard.h                         |    2 
     src/PCSC/winscard_clnt.c                    |    2 
     src/PCSC/winscard_msg.c                     |    2 
     src/PCSC/winscard_msg.cpp                   |    2 
     src/PCSC/winscard_msg.h                     |    2 
     src/PCSC/winscard_msg_srv.c                 |    2 
     src/PCSC/winscard_svc.c                     |   10 
     src/PCSC/winscard_svc.h                     |    2 
     src/PCSC/wintypes.h                         |   16 
     52 files changed, 2727 insertions(+), 2054 deletions(-)
    

    Changes:
    • some bugs fixes I made in the "official" pcsc-lite and I then ported to Apple version of pcsc-lite. See my blog article for details.
    • the CCID driver has been updated from 1.3.8 to 1.3.11
    I integrated the changes in SmartCardServices in July 2009 and they were available two years later in the next major version of Mac OS X.

    10.8 Mountain Lion

    July 2012

    Blog article "Mac OS X Mountain Lion and smart card status"

    SmartCardServices-55105

    No change in the source code.

    10.9 Mavericks

    October 2013

    Blog article "OS X Mavericks and smart cards status"

    SmartCardServices-55111

    Changes:
    • The ability to respawn in 32-bits to run a 32-bits only driver (introduced in Snow Leopard) has been removed
    • The CCID driver is now in 64-bits but the version is still 1.3.11

    Conclusion

    The evolution of Apple pcsc-lite is quite different from the evolution of the "official" pcsc-lite.

    In a next article I will describe the differences between the 2 projects after more than 10 years of evolution.