• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

doc/H03-May-2022-1,335947

examples/H11-Oct-2020-934610

m4/H11-Oct-2020-4944

src/H03-May-2022-9,9597,261

tests/H03-May-2022-3,0931,922

.gitignoreH A D11-Oct-2020723 7872

.travis.shH A D11-Oct-20203 KiB10160

.travis.ymlH A D11-Oct-20201.6 KiB5142

COPYINGH A D11-Oct-202025.9 KiB511422

INSTALL.mdH A D11-Oct-20202.7 KiB11257

Makefile.amH A D11-Oct-2020784 2919

Makefile.makH A D11-Oct-2020139 95

NEWSH A D11-Oct-20209.9 KiB200179

README.mdH A D11-Oct-20207.9 KiB198146

appveyor.ymlH A D11-Oct-20202.3 KiB6659

bootstrapH A D11-Oct-202049 31

configure.acH A D03-May-20227.8 KiB302261

make.rules.makH A D11-Oct-20201.4 KiB4941

README.md

1# Build state
2
3[![Linux Build Status](https://travis-ci.org/OpenSC/libp11.png)](https://travis-ci.org/OpenSC/libp11)
4[![Windows Build status](https://ci.appveyor.com/api/projects/status/kmbu8nex5ogecoiq?svg=true)](https://ci.appveyor.com/project/LudovicRousseau/libp11)
5[![Coverity Scan Status](https://scan.coverity.com/projects/15472/badge.svg)](https://scan.coverity.com/projects/opensc-libp11)
6[![Code Quality: Cpp](https://img.shields.io/lgtm/grade/cpp/g/OpenSC/libp11.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/OpenSC/libp11/context:cpp)
7[![Total Alerts](https://img.shields.io/lgtm/alerts/g/OpenSC/libp11.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/OpenSC/libp11/alerts)
8
9
10# Overview
11
12This code repository produces two libraries:
13* libp11 provides a higher-level (compared to the PKCS#11 library)
14interface to access PKCS#11 objects.  It is designed to integrate with
15applications that use OpenSSL.
16* pkcs11 engine plugin for the OpenSSL library allows accessing
17PKCS#11 modules in a semi-transparent way.
18
19The wiki page for this project is at https://github.com/OpenSC/libp11/wiki
20and includes a bug tracker and source browser.
21
22## PKCS#11
23
24The PKCS#11 API is an abstract API to perform operations on cryptographic objects
25such as private keys, without requiring access to the objects themselves. That
26is, it provides a logical separation of the keys from the operations. The
27PKCS #11 API is mainly used to access objects in smart cards and Hardware or Software
28Security Modules (HSMs). That is because in these modules the cryptographic keys
29are isolated in hardware or software and are not made available to the applications
30using them.
31
32PKCS#11 API is an OASIS standard and it is supported by various hardware and software
33vendors. Usually, hardware vendors provide a PKCS#11 module to access their devices.
34A prominent example is the OpenSC PKCS #11 module which provides access to a variety
35of smart cards. Other libraries like NSS or GnuTLS already take advantage of PKCS #11
36to access cryptographic objects.
37
38## OpenSSL engines
39
40OpenSSL implements various cipher, digest, and signing features and it can
41consume and produce keys. However plenty of people think that these features
42should be implemented in separate hardware, like USB tokens, smart cards or
43hardware security modules. Therefore OpenSSL has an abstraction layer called
44"engine" which can delegate some of these features to different piece of
45software or hardware.
46
47engine_pkcs11 tries to fit the PKCS#11 API within the engine API of OpenSSL.
48That is, it provides a gateway between PKCS#11 modules and the OpenSSL engine API.
49One has to register the engine with OpenSSL and one has to provide the
50path to the PKCS#11 module which should be gatewayed to. This can be done by editing
51the OpenSSL configuration file, by engine specific controls,
52or by using the p11-kit proxy module.
53
54The p11-kit proxy module provides access to any configured PKCS #11 module
55in the system. See [the p11-kit web pages](http://p11-glue.freedesktop.org/p11-kit.html)
56for more information.
57
58
59# PKCS #11 module configuration
60
61## Copying the engine shared object to the proper location
62
63OpenSSL has a location where engine shared objects can be placed
64and they will be automatically loaded when requested. It is recommended
65to copy the engine_pkcs11 to that location as "libpkcs11.so" to ease usage.
66This is handle by 'make install' of engine_pkcs11.
67
68
69## Using the engine from the command line
70
71In systems with p11-kit-proxy engine_pkcs11 has access to all the configured
72PKCS #11 modules and requires no further OpenSSL configuration.
73In systems without p11-kit-proxy you need to configure OpenSSL to know about
74the engine and to use OpenSC PKCS#11 module by the engine_pkcs11. For that you
75add something like the following into your global OpenSSL configuration file
76(often in ``/etc/ssl/openssl.cnf``).  This line must be placed at the top,
77before any sections are defined:
78
79```
80openssl_conf = openssl_init
81```
82
83This should be added to the bottom of the file:
84
85```
86[openssl_init]
87engines=engine_section
88
89[engine_section]
90pkcs11 = pkcs11_section
91
92[pkcs11_section]
93engine_id = pkcs11
94dynamic_path = /usr/lib/ssl/engines/libpkcs11.so
95MODULE_PATH = opensc-pkcs11.so
96init = 0
97```
98
99The dynamic_path value is the engine_pkcs11 plug-in, the MODULE_PATH value is
100the OpenSC PKCS#11 plug-in. The engine_id value is an arbitrary identifier for
101OpenSSL applications to select the engine by the identifier. In systems
102with p11-kit-proxy installed and configured, you do not need to modify the
103OpenSSL configuration file; the configuration of p11-kit will be used.
104
105If you do not update the OpenSSL configuration file you will need to
106specify the engine configuration explicitly. The following line loads
107engine_pkcs11 with the PKCS#11 module opensc-pkcs11.so:
108
109```
110OpenSSL> engine -t dynamic -pre SO_PATH:/usr/lib/engines/engine_pkcs11.so
111         -pre ID:pkcs11 -pre LIST_ADD:1 -pre LOAD
112         -pre MODULE_PATH:opensc-pkcs11.so
113```
114
115
116## Testing the engine operation
117
118To verify that the engine is properly operating you can use the following example.
119
120```
121$ openssl engine pkcs11 -t
122(pkcs11) pkcs11 engine
123     [ available ]
124```
125
126## Using p11tool and OpenSSL from the command line
127
128This section demonstrates how to use the command line to create a self signed
129certificate for "Andreas Jellinghaus". The key of the certificate will be generated
130in the token and will not exportable.
131
132For the examples that follow, we need to generate a private key in the token and
133obtain its private key URL. The following commands utilize p11tool for that.
134
135```
136$ p11tool --provider /usr/lib/opensc-pkcs11.so --login --generate-rsa --bits 1024 --label test-key
137$ p11tool --provider /usr/lib/opensc-pkcs11.so --list-privkeys --login
138```
139
140Note the PKCS #11 URL shown above and use it in the commands below.
141
142To generate a certificate with its key in the PKCS #11 module, the following commands commands
143can be used. The first command creates a self signed Certificate for "Andreas Jellinghaus". The
144signing is done using the key specified by the URL. The second command creates a self-signed
145certificate for the request, the private key used to sign the certificate is the same private key
146used to create the request. Note that in a PKCS #11 URL you can specify the PIN using the
147"pin-value" attribute.
148
149```
150$ openssl
151OpenSSL> req -engine pkcs11 -new -key "pkcs11:object=test-key;type=private;pin-value=XXXX" \
152         -keyform engine -out req.pem -text -x509 -subj "/CN=Andreas Jellinghaus"
153OpenSSL> x509 -engine pkcs11 -signkey "pkcs11:object=test-key;type=private;pin-value=XXXX" \
154         -keyform engine -in req.pem -out cert.pem
155```
156
157
158## Engine controls
159
160The supported engine controls are the following.
161
162* **SO_PATH**: Specifies the path to the 'pkcs11-engine' shared library
163* **MODULE_PATH**: Specifies the path to the pkcs11 module shared library
164* **PIN**: Specifies the pin code
165* **VERBOSE**: Print additional details
166* **QUIET**: Do not print additional details
167* **LOAD_CERT_CTRL**: Load a certificate from token
168* **SET_USER_INTERFACE**: Set the global user interface
169* **SET_CALLBACK_DATA**: Set the global user interface extra data
170* **FORCE_LOGIN**: Force login to the PKCS#11 module
171
172An example code snippet setting specific module is shown below.
173
174```
175ENGINE_ctrl_cmd(engine, "MODULE_PATH",
176		0, "/path/to/pkcs11module.so", NULL, 1);
177```
178
179In systems with p11-kit, if this engine control is not called engine_pkcs11
180defaults to loading the p11-kit proxy module.
181
182
183# Developer information
184
185## Thread safety in libp11
186
187Thread-safety requires dynamic callbacks to be registered by the calling
188application with the following OpenSSL functions:
189* CRYPTO_set_dynlock_create_callback
190* CRYPTO_set_dynlock_destroy_callback
191* CRYPTO_set_dynlock_lock_callback
192
193## Submitting pull requests
194
195For adding new features or extending functionality in addition to the code,
196please also submit a test program which verifies the correctness of operation.
197See tests/ for the existing test suite.
198