An Automated Testing of Smartcards in Opensc Project
An Automated Testing of Smartcards in Opensc Project
Faculty of Informatics
An automated testing of
smartcards in OpenSC project
Master’s Thesis
i
Acknowledgement
I would like to thank my supervisor RNDr. Petr Švenda, Ph.D., for his
guidance while writing the thesis. I would also like to thank dr. Nikos
Mavrogiannopoulos, for helping me with introduction to smart cards
community, valuable comments and his time invested in consultation
throughout the work.
ii
Abstract
The thesis investigates the common use of smart cards in the Fedora
distribution of the operating system Linux, in order to create a unit
testing application for OpenSC project. At first, the smart card usage
data are collected and analysed, in order to prepare a set of tests for
selected PKCS#11 drivers. Subsequently, the unit testing application
for OpenSC project is developed. The objective of the application is
to execute a test suite against selected PKCS#11 drivers. The selected
drivers for testing are the PIV driver and the smart card driver for
Cryptoflex card.
iii
Keywords
OpenSC, unit testing, smart cards, PKCS#11, YubiKey Neo, Cryptoflex
iv
Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Smart cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.1 Types of smart cards . . . . . . . . . . . . . . . . . . . . . . 3
2.1.1 Microprocessor based cards . . . . . . . . . . . . 4
2.1.2 Memory based cards . . . . . . . . . . . . . . . . 5
2.1.3 Contact cards . . . . . . . . . . . . . . . . . . . . 5
2.1.4 Contactless cards . . . . . . . . . . . . . . . . . . 6
2.1.5 Combination cards . . . . . . . . . . . . . . . . . 6
2.2 Smart card operating system . . . . . . . . . . . . . . . . . 6
3 Public-Key Cryptography Standards . . . . . . . . . . . . . 8
3.1 PKCS#11 . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.1.1 Microsoft CryptoAPI . . . . . . . . . . . . . . . . 10
3.1.2 PKCS#11 vulnerabilities . . . . . . . . . . . . . . 11
Attacks . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 PKCS#15 . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4 Analysis and design . . . . . . . . . . . . . . . . . . . . . . . 13
4.1 Usage of pkcs11-spy . . . . . . . . . . . . . . . . . . . . . . 14
4.1.1 Environment configuration . . . . . . . . . . . . 15
4.1.2 Pkcs11-spy with OpenSSH . . . . . . . . . . . . 17
4.1.3 Pkcs11-spy with pkcs11-tool . . . . . . . . . . . 21
4.1.4 Pkcs11-spy with Mozilla Firefox . . . . . . . . . 23
4.1.5 Pkcs11-spy with Linux-PAM . . . . . . . . . . . 26
5 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.1 Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.1.1 CMocka . . . . . . . . . . . . . . . . . . . . . . . 34
5.2 Test cases . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.2.1 General tests . . . . . . . . . . . . . . . . . . . . . 39
5.2.2 User PIN tests . . . . . . . . . . . . . . . . . . . . 40
5.2.3 Message digest tests . . . . . . . . . . . . . . . . 41
5.2.4 Key generation tests . . . . . . . . . . . . . . . . 42
5.2.5 Sign and verify tests . . . . . . . . . . . . . . . . 43
5.2.6 Encrypt and decrypt tests . . . . . . . . . . . . . 45
5.2.7 Find objects tests . . . . . . . . . . . . . . . . . . 45
5.2.8 Generate random data tests . . . . . . . . . . . . 47
5.2.9 Create and delete objects tests . . . . . . . . . . . 47
v
6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
A Pcsc_scan output . . . . . . . . . . . . . . . . . . . . . . . . . 52
B Pkcs11-tool utility . . . . . . . . . . . . . . . . . . . . . . . . 53
C Smart card with Mozilla Firefox . . . . . . . . . . . . . . . . 55
D YubiKey OpenSC tests output . . . . . . . . . . . . . . . . . 56
vi
1 Introduction
The rapid growth in the development of information technologies
during the last few years resulted in the necessity to secure user’s sen-
sitive data. Many protection methods have been already developed.
One of the popular options for increasing security of user’s personal
data nowadays is the use of smart cards.
The advantage of smart cards over other protection methods is
increased security. Furthermore, their use is convenient, mainly be-
cause, they are small and compact, so a user can carry and use them
at any time. The cards are used for authentication, but they can also
carry sensitive information like fingerprints, face or iris images. Fur-
thermore, the operating systems such as Microsoft Windows, Linux
and Mac OS provide direct support for the smart card usage.
There are various applications, which allow the user to use smart
cards in the Linux operating system. One of them is OpenSC. It pro-
vides many utility programs that are used for smart card manipu-
lation. Moreover, the OpenSC supplies the system with smart card
drivers, which enable communication with the card.
However, there still is a possibility of security issues during the
implementation of smart card drivers. In order to lower the number
of issues, the code should be covered by tests. For developers, the
benefit of such test suite is the possibility to verify that their code did
not break any functionality. Moreover, the test suite can also be used
by the end user to verify that the obtained version of code is working
with his/her smart card without any problems.
The goal of this master thesis is to develop a unit testing applica-
tion for OpenSC project. At first, a common smart card usage in the
Fedora distribution of the Linux operating system, is explored. Data
are collected and analysed from several selected use cases. This anal-
ysis is further used in the creation of test suite for selected smart card
drivers - PIV driver and driver for Cryptoflex card.
The theoretical part of the thesis introduces the reader to the basic
concepts and definitions. The second chapter contains basic informa-
tion about smart cards and smart card operating systems. Further-
more, this chapter describes the various types of smart cards.
1
1. Introduction
2
2 Smart cards
Smart card (chip card or integrated circuit card (ICC)) is a plastic card
with a microprocessor and memory embedded in it. These cards are
available in various sizes and different types.
There are some cards that have only non-programmable memory.
These cards are read-only and the information stored on them can-
not be changed or manipulated. The other group of cards containing
a microprocessor, have various functionalities. Smart cards can be de-
signed to be inserted into a slot and read by a special reader or to be
read from a distance.
– contact cards
– contactless cards
3
2. Smart cards
– combination cards
– hybrid cards
4
2. Smart cards
Cards in this category are used for applications in which the func-
tion of the card is fixed. They can perform only following operations:
store, read and write data to a particular memory location. These data
cannot be manipulated or processed.
Memory based cards contain a non-volatile memory EEPROM2 and
need a card reader to manipulate the data on the card. They com-
municate with the reader via synchronous protocols. Memory based
smart cards have no processing power and cannot manage the data
stored in them.
Due to the incapability of cryptography, this type of cards is used
in storing telephone credits, electronic cash or transportation tickets.
5
2. Smart cards
6
2. Smart cards
tion. The operating system instructions are not dependent on any spe-
cific application, but are frequently used by most of applications [2].
The smart card operating system provides the baseline functions that
are common across all smart card products. It is responsible for file
and data management held in memory and communication between
the card and the card reader. Additional responsibilities are access
control to information and functions (e.g. select file, read, write and
update data), management of card security and the cryptographic al-
gorithm procedures.
∙ JavaCard OS
7
3 Public-Key Cryptography Standards
The Public-Key Cryptography Standards (PKCS) are a set of stan-
dards for public-key cryptography developed by RSA Laboratories
in cooperation with an informal consortium, originally including Sun
Apple, Microsoft, DEC, Lotus and MIT [4].
The PKCS are designed for binary and ASCII data and they are
also compatible with the ITU-T1 X.509 standard.
8
3. Public-Key Cryptography Standards
3.1 PKCS#11
The PKCS#11 is a cryptographic token interface standard, that spec-
ifies an API3 , called Cryptoki. With this API, it is possible to imple-
ment and to perform cryptographic functions for cryptographic to-
kens.
The Cryptoki follows simple object-based approach, addressing
the goals of technology independence and resource sharing [5]. Con-
sequently, any kind of device thus can be used and multiple applica-
tions can access multiple devices. Furthermore, it presents a logical
view of the device, called a cryptographic token, to the application.
For instance, devices such as hardware security modules (HSM) and
smart cards belong to cryptographic token category.
The API defines the most commonly used cryptographic object types
(RSA keys, X.509 certificates, DES and Triple DES keys, etc.) [6]. Ad-
ditionally, the API specifies all necessary functions to use, create or
9
3. Public-Key Cryptography Standards
10
3. Public-Key Cryptography Standards
Attacks
One common way to derive a device-specific symmetric key is to take
a master key and some public data unique to the device, like a serial
number, and encrypt the data under the key.
In PKCS#11, this is done by calling the C_DeriveKey function. In
order to keep a key value as a secret, the CKA_SENSITIVE attribute is set
to TRUE. The C_DeriveKey is implemented as specified in the standard,
deriving the key from a key which is CKA_SENSITIVE and produces
another key which is CKA_SENSITIVE.
However, when the master key has the attribute CKA_DERIVE and
also CKA_ENCRYPT set to TRUE, an attacker can execute C_Encrypt using
the master key and recover the value of the derived secret keys from
the serial numbers.
4. http://www.lsv.ens-cachan.fr/Publis/PAPERS/PDF/BCFS-ccs10.pdf
11
3. Public-Key Cryptography Standards
3.2 PKCS#15
The PKCS#15 defines a standard that allows users of cryptographic
tokens to identify themselves to multiple applications, regardless of
the application’s Cryptoki (or other token interface) provider. The IC-
card-related parts of this standard are specified in ISO/IEC 7816-15
standard [11].
The "object-oriented" approach (treating keys, certificates and other
data as objects with attributes and values) selected for PKCS#11, has
been adopted for PKCS#15 as well [12].
12
4 Analysis and design
Smart cards usage during everyday tasks was analysed in order to
obtain various test scenarios. The analysis was mostly concerned with
the usage of smart cards in Fedora distribution of operating system
Linux. The obtained data were used to design test suite that covers
majority commonly used functions from PKCS#11 API. This way, all
the common use cases of smart card usage are covered by tests.
There are many applications using smart cards for various objectives.
They can be divided into two groups:
13
4. Analysis and design
∙ OpenSSH
∙ Linux-PAM
∙ Mozilla Firefox
∙ pkcs11-tool
14
4. Analysis and design
15
4. Analysis and design
patible with every software and card that does so as well [15].
For the purpose of this thesis was used the latest OpenSC version
cloned from GitHub. Before the installation of the OpenSC, two util-
ity programs and the OpenSSL have to be installed:
$ dnf install autoconf automake
$ dnf install openssl openssl−devel engine_pkcs11
The OpenSSL has to be installed before the OpenSC, so the OpenSC
can use some OpenSSL functions for reading certificates, working
with public or private key, etc. In addition, the OpenSSL is used to
check whether outputs from testing applications were correct. At this
point, the OpenSC project can be cloned from GitHub and installed:
$ cd ~/Downloads
$ git clone https://github.com/OpenSC/OpenSC.git
$ cd OpenSC/
$ autoreconf −−install −−verbose
$ make
$ sudo make install
The last package required by the test application is CMocka2 . The
version 1.0.1 of the CMocka package was used in this thesis and it
was downloaded from url https://cmocka.org/files/1.0/.
$ cd ~/Downloads
$ xz −d cmocka−1.0.1.tar.xz
$ tar vxf cmocka−1.0.1.tar
$ cd cmocka−1.0.1
$ mkdir build
$ cd build
$ cmake −DCMAKE_BUILD_TYPE=Release ..
$ make
$ sudo make install
After this configuration, the environment is ready for work with
the smart card and the test application. All examples with the pkcs11-
spy module assume, that the Cryptoflex 32k e-gate smart card is
used.
2. https://cmocka.org/
16
4. Analysis and design
The next step is to generate a RSA key pair and store the certificate to
the smart card. There are two possible options:
1. keys are generated on the smart card and the certificate will be
imported to the card. It includes 3 steps:
17
4. Analysis and design
(b) from the obtained private key generate a public key in DER
format
$ openssl rsa −pubout −in private_key.key −
inform DER −out public_key.key −outform
DER
18
4. Analysis and design
At this moment, there are 4 objects on the card: the user PIN, the
private key, the public key and the certificate. In order to list all objects
from the card the command pkcs15-tool -D is used. Output is as
follows:
PKCS#15 Card [OpenSC Card]:
Version : 0
Serial number : 0000D909FFFF0200
Manufacturer ID: OpenSC Project
Last update : 20160221162253Z
Flags : EID compliant
19
4. Analysis and design
MD:guid : {4a6c27d7-802a-5be2-b97d-88aab52f5238}
An ssh key has to be created to use the OpenSSH with the generated
certificate. (Note if the OpenSSL was not installed before the OpenSC,
there would be no option read-ssh-key for pkcs15-tool utility)
$ pkcs15−tool −−read−ssh−key a1
The obtained key should be copied to a ~/.ssh/authorized_keys file
on a remote server.
aisa:/home/xusername>$ cat ~/.ssh/authorized_keys
ssh−rsa ${obtained_key}
After all these steps, everything is set for the use of the OpenSSH
with the smart card to login via ssh to the remote server. The path to
the PKCS#11 module has to be specified as a parameter to ssh com-
mand. When the path to the pkcs11-spy module is set as a parameter,
all PKCS#11 functions used by the OpenSSH are logged to the file.
$ export PKCS11SPY=/lib64/opensc−pkcs11.so
$ export PKCS11SPY_OUTPUT=/path/to/output.log
20
4. Analysis and design
Note that before using any of these commands, the smart card has
to be reinitialized and pkcs11-spy environment variables have to be
exported. All examples of pkcs11-tool commands are included in Ap-
pendix B.
$ export PKCS11SPY=/lib64/opensc−pkcs11.so
$ export PKCS11SPY_OUTPUT=/path/to/output.log
∙ test – to run basic tests on the smart card e.g. random data gen-
eration tests, tests of signatures, verification, message digest, de-
cryption and key unwrap.
∙ list slots – to list all available slots (physical and virtual) and
display their information (e.g. card insertion, virtual or physical
slot). Note that it is not necessary to have a user PIN on the card.
21
4. Analysis and design
∙ list objects – to list all objects stored on the card. If this option is
used without logging in to the card, it will list only public avail-
able objects such as certificates or public keys. When the login
and pin options are provided, private objects are also shown.
∙ init pin – to initialize user’s PIN. Only the Security Officer (SO)
has privileges to initialize the user PIN, therefore he must be
logged in. A default SO PIN for the Cryptoflex smart card is
00000000. Note, that for this command, the card must not be
already initialized with the user PIN.
22
4. Analysis and design
Most of the data for analysis were obtained by means of the pkcs11-
spy utility module with the pkcs11-tool.
There are three ways how Mozilla Firefox works with certificates:
1. to read a certificate
In order to make Mozilla Firefox work with the smart card a new
Security Device has to be added to Firefox. Firstly, the pkcs11-spy en-
vironment variables are exported. Subsequently, Firefox is executed
from the command line, so it is possible to add the pkcs11-spy secu-
rity device.
23
4. Analysis and design
$ export PKCS11SPY=/lib64/opensc−pkcs11.so
$ export PKCS11SPY_OUTPUT=/path/to/firefox.log
$ firefox
The path to the configuration of security devices is as following:
Menu - Preferences - Advanced - Security Devices. Afterwards Load
is pressed and the Load PKCS#11 Device dialogue window is opened.
The name of the module and path to the pkcs11-spy utility module is
filled as shown in the picture 4.1-1
Importing of a certificate
2. from the obtained private key create the certificate in PEM3 for-
mat
$ openssl req −out cert.pem −key private_key.key −
new −x509 −days 365
24
4. Analysis and design
25
4. Analysis and design
The pkcs11-spy output log is used to verify, that the certificate has
been in fact imported to the smart card. In the log file, the function
C_CreateObject is used at least 3 times to subsequently create the
private key, the public key and the certificate. The log file includes all
input parameters of the created objects such as a modulus, a private
exponent, prime numbers and others.
26
4. Analysis and design
27
4. Analysis and design
The private key and the X.509 certificate for the key is generated and
stored on the card. Note that the Common Name (CN) field is used
to match the certificate to the account. Therefore either the login or
the real name has to be put in the field.
$ openssl genpkey −algorithm RSA −out private_key.key −
pkeyopt rsa_keygen_bits:2048 −outform DER
$ openssl req −out certificate.pem −key private_key.key −
keyform DER −new −x509 −days 365
$ openssl x509 −in certificate.pem −out certificate.der −
outform der
28
4. Analysis and design
Linux-PAM works correctly with the smart card, only when the used
certificate is registered as "trusted" in NSS5 . The first step is to create
a folder for NSS database and register it:
$ sudo mkdir −p /etc/pam_pkcs11/nssdb
$ sudo chmod 700 /etc/pam_pkcs11/nssdb
$ certutil −d /etc/pam_pkcs11/nssdb −N
The next step is to import the created certificate to the certificate
database. It has to be set as trusted, so that it can be used during au-
thorization via the smart card.
$ sudo certutil −A −n "Certificate nickname" −t "CT,C,
C" −a −d /etc/pam_pkcs11/nssdb −i /path/to/
created/certificate
By default, Linux-PAM is not set to use the OpenSC module. Hence,
there have to be changes in the configuration. These variables are
changed in configuration file /etc/pam_pkcs11/pam_pkcs11.conf:
29
4. Analysis and design
slot_num = 0;
ca_dir = /etc/pam_pkcs11/cacerts;
crl_dir = /etc/pam_pkcs11/crls;
The last step is to choose which services are using the smart card
authentication. All available services are listed in /etc/pam.d direc-
tory as shown in the picture 4.1-5.
30
4. Analysis and design
31
5 Implementation
The OpenSC project provides a PKCS#11 library (opensc-pkcs11.so),
which is responsible for a communication with smart cards. In order
to communicate with a card the specific card driver is used. There
are smart card drivers for PIV (Personal Identity Verification) cards,
PKCS#15 cards and for other types of cards.
The goal of the unit testing application, is to run a set of tests
against specified card drivers. Tested smart card drivers were:
All tests are part of the forked OpenSC repository1 and can be exe-
cuted during the OpenSC installation. In order to run the test suite,
these steps have to be followed:
∙ running the tests (Note that the token has to be connected to the
PC, otherwise tests are skipped)
$ make check
At the end of the command "make check", the test suite summary is
reported. It reports number of passed, skipped or failed tests. Two
tests are run:
1. https://github.com/strho/OpenSC
32
5. Implementation
When the PIV card is connected to the PC (e.g. YubiKey), the test
for the Cryptoflex card is skipped. Likewise, when there is no card
connected, both tests are skipped. This is the output of the "make check"
command:
FAIL: yubico_test.sh
==============================================
Testsuite summary for OpenSC 0.15.0
==============================================
# TOTAL: 2
# PASS: 0
# SKIP: 1
# XFAIL: 0
# FAIL: 1
# XPASS: 0
# ERROR: 0
==============================================
See tests/test-suite.log
==============================================
The log file yubico_test.sh.log contains single results of the in-
dividual tests. At the end of the file, there is a summary displaying
the number of launched and failed tests. The example of log file with
failed tests is included in Appendix D.
5.1 Technologies
The smart card test application was implemented in the C program-
ming language. In order to work with a smart card, the standard
PKCS#11 API (called Cryptoki) is used. The implementation of the
Cryptoki is provided by the opensc-pkcs11.so library. The CMocka is
selected as the unit testing framework. The CLion IDE2 by JetBrains3
was used during the implementation.
33
5. Implementation
5.1.1 CMocka
The CMocka is a unit testing framework for C. One of the principles
of the CMocka is that a test application requires only the standard C
library and the CMocka itself [18]. This minimizes conflicts with stan-
dard C library headers, especially on a variety of different platforms.
The CMocka supports [19]:
4. ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-11/v2-20/pkcs-11v2-20.pdf
34
5. Implementation
Function Description
C_Initialize* initializes the Cryptoki
C_Finalize* cleans up miscellaneous resources associ-
ated with the Cryptoki
C_GetInfo* obtains general information about the
Cryptoki
C_GetFunctionList* obtains entry points of the Cryptoki library
functions
Function Description
C_GetSlotList* obtains a list of slots in the system
C_GetSlotInfo* obtains information about a particular slot
C_GetTokenInfo* obtains information about a particular to-
ken
C_WaitForSlotEvent waits for a slot event (token insertion, re-
moval, etc.) to occur
C_GetMechanismList* obtains a list of mechanisms supported by
a token
C_GetMechanismInfo* obtains information about a particular
mechanism
C_InitToken initializes a token
C_InitPIN* initializes a normal user’s PIN
C_SetPIN* modifies a PIN of a current user
Function Description
C_GetFunctionStatus legacy function which always returns
CKR_FUNCTION_NOT_PARALLEL
C_CancelFunction legacy function which always returns
CKR_FUNCTION_NOT_PARALLEL
35
5. Implementation
36
5. Implementation
Function Description
C_DecryptInit* initializes a decryption operation
C_Decrypt* decrypts single-part encrypted data
C_DecryptUpdate continues a multiple-part decryption ope-
ration
C_DecryptFinal finishes a multiple-part decryption opera-
tion
Function Description
C_DigestInit* initializes a message-digesting operation
C_Digest* digests single-part data
C_DigestUpdate* continues a multiple-part digesting opera-
tion
C_DigestKey digests a key
C_DigestFinal* finishes a multiple-part digesting operation
Function Description
C_SignInit* initializes a signature operation
C_Sign* signs single-part data
C_SignUpdate continues a multiple-part signature opera-
tion
C_SignFinal finishes a multiple-part signature operation
C_SignRecoverInit initializes a signature operation, where the
data can be recovered from the signature
C_SignRecover signs single-part data, where the data can
be recovered from a signature
37
5. Implementation
Function Description
C_VerifyInit* initializes a verification operation
C_Verify* verifies a signature on single-part data
C_VerifyUpdate* continues a multiple-part verification ope-
ration
C_VerifyFina* finishes a multiple-part verification opera-
tion
C_VerifyRecoverInit initializes a verification operation that re-
covers data from a signature
C_VerifyRecover verifies a signature on single-part data,
where data are recovered from the signa-
ture
Function Description
C_DigestEncryptUpdate continues simultaneous multiple-part di-
gesting and encryption operations
C_DecryptDigestUpdate continues simultaneous multiple-part de-
cryption and digesting operations
C_SignEncryptUpdate continues simultaneous multiple-part sig-
nature and encryption operations
C_DecryptVerifyUpdate continues simultaneous multiple-part de-
cryption and verification operations
Function Description
C_GenerateKey generates a secret key
C_GenerateKeyPair* generates a public-key/private-key pair
C_WrapKey wraps (encrypts) a key
C_UnwrapKey unwraps (decrypts) a key
C_DeriveKey derives a key from a base key
38
5. Implementation
∙ General tests
39
5. Implementation
∙ Fail condition: The card does not have any supported mecha-
nisms
∙ Fail condition: The test cannot fail, but it is skipped when there
is no mechanism with the CKM_EC_KEY_PAIR_GEN flag
∙ Description: At first the test logs into the card with incorrect
PIN and the CKR_PIN_INCORRECT return value is expected. Sub-
sequently the correct PIN is used to log into the card and the
CKR_OK is returned.
40
5. Implementation
∙ Fail condition: The test fails, when an incorrect PIN is used and
the CKR_PIN_INCORRECT value is not returned from the C_Login
function. When the correct PIN is used, the CKR_OK is expected.
41
5. Implementation
∙ Fail condition: The test fails, when any Cryptoki functions re-
turns other value than CKR_OK. Furthermore, when the output
hash obtained by the card is different from the hash, created
with the OpenSSL, the test fails.
∙ Fail condition: Fail conditions are the same as for the Tests of
MD5 digest algorithm test.
Test name: Generate a RSA key pair on a token with a wrong template
42
5. Implementation
∙ Description: The test tries to generate a RSA key pair with an in-
correct template. After the C_GenerateKeyPair function is used,
the private key and the public key are not generated and stored
on the card
43
5. Implementation
44
5. Implementation
∙ Description: The certificate, the public key and the private key
are imported to the token. The C_FindObjects function is used
to find all objects on the card, The object class found is one of
these: CKO_PRIVATE_KEY, CKO_PUBLIC_KEY or CKO_CERTIFICATE.
45
5. Implementation
At the end of the test, there are exactly three (imported) objects
found that respect the criteria.
∙ Disclaimer: The Cryptoki C_FindObjects function cannot be
used to find all objects on the token. This is due to the fact that
some tokens (e.g. YubiKey Neo) have predefined objects, which
are present on the card as default.
∙ Tested Cryptoki functions: C_FindObjectsInit, C_FindObjects
and C_FindObjectsFinal
∙ Fail condition: The test fails when:
46
5. Implementation
47
5. Implementation
48
6 Conclusion
The goal of the master thesis was to create smart cards unit testing
applications for the OpenSC project. Foremost, the current situation
of smart card usage in Fedora distribution of Linux operating sys-
tem was mapped. Subsequently, data about smart card usage were
collected and analysed and a set of tests was created.
This goal was achieved and the output is documented in the pre-
vious chapters. In addition the smart card test application for two se-
lected smart card drivers (PIV driver and driver for Cryptoflex card),
was developed. The application was created as a part of test coverage
of the OpenSC project. However, it is only part of the forked branch.
In order to integrate the application to the master branch, there will
be some cooperation with the OpenSC developers.
The main advantage of the developed applications is the possibil-
ity for developers to verify that changes in existing functions did not
break any functionality.
Although the required functionality was implemented, there are
still many ways how to improve the smart card test application. The
drawback of the current version is, that only two smart card drivers
were tested. In order for the application to cover larger set of drivers,
the selected drivers have to be researched as each driver slightly dif-
fers from the others.
Another drawback of the application is the necessity to connect
smart card reader and inserted card. The solution is the usage of a vir-
tual smart card. One tested possibility was to use the Virtual Smart
Card1 project from Frank Morgner and Dominik Oepen. However,
the problem with this option was, that the provided virtual Cryptoflex
card is incomplete, hence it is not applicable.
The next tested option was to use a JavaCard instead of hardware
smart card. The JavaCard is also able to run in virtual environment
using the jCardSim2 simulator. However, the drawback of this solu-
tion is, that there is no existing suitable and working PIV applet for
JavaCards.
1. https://frankmorgner.github.io/vsmartcard/virtualsmartcard/README.html
2. https://jcardsim.org/
49
Bibliography
[1] CardLogix Corporation. Types of smart cards. 2010. url: http :
//www.smartcardbasics.com/smart_card_images/types-of-
smart-cards.gif (visited on 04/12/2016).
[2] Inc. Jacquinot Consulting. Smart Card Operating System. 2015.
url: http : / / www . cardwerk . com / smartcards / smartcard _
operatingsystems.aspx (visited on 04/12/2016).
[3] Smart Cards and their Operating Systems. Heng Guo HUT, Telecom-
munications Software and Multimedia Laboratory, May 3, 2015.
url: http://www.tml.tkk.fi/Studies/Tik-111.590/2001s/
papers/heng_guo.pdf (visited on 04/12/2016).
[4] RSA Laboratories. WHAT IS PKCS? url: http://www.emc.com/
emc-plus/rsa-labs/standards-initiatives/pkcs.htm (vis-
ited on 04/20/2016).
[5] RSA Laboratories. PKCS #11: CRYPTOGRAPHIC TOKEN IN-
TERFACE STANDARD. url: http://www.emc.com/emc-plus/
rsa-labs/standards-initiatives/pkcs-11-cryptographic-
token-interface-standard.htm (visited on 04/20/2016).
[6] Wikipedia. PKCS #11. url: https://en.wikipedia.org/wiki/
PKCS_11 (visited on 04/20/2016).
[7] Microsoft. The Cryptography API, or How to Keep a Secret. url:
https : / / msdn . microsoft . com / en - us / library / ms867086 .
aspx (visited on 04/22/2016).
[8] Microsoft. Smart Card Minidrivers. url: https://msdn.microsoft.
com/en-us/library/windows/hardware/dn468773(v=vs.85)
.aspx (visited on 04/22/2016).
[9] Secgroup Ca’ Foscari. Tookan. url: https : / / secgroup . dais .
unive.it/projects/tookan/ (visited on 04/22/2016).
[10] R. Behrends et al. Attacking and Fixing PKCS#11 Security Tokens.
Tech. rep. Venezia, Italy: Università Ca’ Foscari, Oct. 2010, p. 10.
url: http://www.lsv.ens- cachan.fr/Publis/PAPERS/PDF/
BCFS-ccs10.pdf (visited on 04/22/2016).
[11] RSA Laboratories. PKCS #15: CRYPTOGRAPHIC TOKEN IN-
FORMATION FORMAT STANDARD. url: http : / / www . emc .
com/emc-plus/rsa-labs/standards-initiatives/pkcs-15-
50
BIBLIOGRAPHY
51
A Pcsc_scan output
The example output of pcsc_scan, when Gemalto Twin Reader card
reader and Cryptoflex 32k e-gate smart card are used, looks as fol-
lows:
$ pcsc_scan
PC/SC device scanner
V 1.4.23 (c) 2001-2011, Ludovic Rousseau
Compiled with PC/SC lite version: 1.8.13
Using reader plug’n play mechanism
Scanning present readers...
0: Gemalto PC Twin Reader 00 00
ATR: 3B 95 18 40 FF 62 04 01 01 05
+ TS = 3B --> Direct Convention
+ T0 = 95, Y(1): 1001, K: 5 (historical bytes)
TA(1) = 18 --> Fi=372, Di=12, 31 cycles/ETU
TD(1) = 40 --> Y(i+1) = 0100, Protocol T = 0
-----
TC(2) = FF
+ Historical bytes: 62 04 01 01 05
Category indicator byte: 62 (proprietary format)
52
B Pkcs11-tool utility
Mostly every shown pkcs11-tool command is used with arguments
"--module /path/to/pkcs11-spy.so -l --pin 12345", so for sim-
plification these arguments are omitted.
∙ test
$ pkcs11−tool −−test
∙ list objects (without logging in the private objects are not listed)
$ pkcs11−tool −−list−objects
$ pkcs11−tool −−list−objects −−login −−pin 12345
∙ init pin
$ pkcs11−tool −−login −−login−type so −−so−pin
00000000 −−init−pin −−new−pin 12345
∙ change pin
$ pkcs11−tool −−change−pin −−new−pin 12345
∙ test ec
$ pkcs11−tool −−test−ec
53
B. Pkcs11-tool utility
∙ read object
$ pkcs11−tool −−read−object −−id a1 −−type
pubkey −−output−file /path/to/output.file
∙ write object
$ pkcs11−tool −−write−object /path/to/cert.der −−
type cert
$ pkcs11−tool −−write−object /path/to/data −−type
data −−label "My data" −−application−id 1.10.0.0
∙ delete object
$ pkcs11−tool −−delete−object −−type pubkey −−id
a1
∙ sign
$ pkcs11−tool −−sign −−id a1 −−input /path/to/
message_to_sign −−output /path/to/
message_to_sign.signature
∙ decrypt
$ pkcs11−tool −−decrypt −−id a1 −−input /path/to
/message.encrypted −−output /path/to/message.
decrypted
∙ hash
$ pkcs11−tool −−hash −−id a1 −m MD5 −−input /
path/to/message_to_hash −−output /path/to/
message_to_hash.hash
54
C Smart card with Mozilla Firefox
55
D YubiKey OpenSC tests output
The example of yubico_test.sh.log log file with failed tests looks
as follow:
Testing PKCS#11 implementation on Yubico
56
D. YubiKey OpenSC tests output
[ RUN ] verify_signed_message_test
[ FAILED ] verify_signed_message_test
[ RUN ] decrypt_encrypted_message_test
[ OK ] decrypt_encrypted_message_test
[ RUN ] find_all_objects_test
[ OK ] find_all_objects_test
[ RUN ] find_object_according_to_template_test
[ OK ] find_object_according_to_template_test
[ RUN ] find_object_and_read_attributes_test
[ OK ] find_object_and_read_attributes_test
[ RUN ] generate_random_data_test
Seed method is not supported.
[ OK ] generate_random_data_test
[ RUN ] create_object_test
Function C_CreateObject is not supported!
[ SKIPPED ] create_object_test
[ RUN ] destroy_object_test
Function C_DestroyObject is not supported!
[ SKIPPED ] destroy_object_test
[==========] 19 test(s) run.
[ PASSED ] 11 test(s).
[ FAILED ] 3 test(s), listed below:
[ FAILED ] initialize_token_with_user_pin_test
[ FAILED ] change_user_pin_test
[ FAILED ] verify_signed_message_test
3 FAILED TEST(S)
57