libp11 0.4.13
libp11.h
Go to the documentation of this file.
1/* libp11, a simple layer on to of PKCS#11 API
2 * Copyright (C) 2005 Olaf Kirch <okir@lst.de>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
24#ifndef _LIB11_H
25#define _LIB11_H
26
27#include "p11_err.h"
28#include <openssl/bio.h>
29#include <openssl/err.h>
30#include <openssl/bn.h>
31#include <openssl/rsa.h>
32#include <openssl/x509.h>
33#include <openssl/evp.h>
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39int ERR_load_CKR_strings(void);
40void ERR_unload_CKR_strings(void);
41void ERR_CKR_error(int function, int reason, char *file, int line);
42# define CKRerr(f,r) ERR_CKR_error((f),(r),__FILE__,__LINE__)
43int ERR_get_CKR_code(void);
44
45/*
46 * The purpose of this library is to provide a simple PKCS#11
47 * interface to OpenSSL application. It was never a goal
48 * of this project to expose the entire PKCS#11 functionality.
49 */
50
52typedef struct PKCS11_key_st {
53 char *label;
54 unsigned char *id;
55 size_t id_len;
56 unsigned char isPrivate;
57 unsigned char needLogin;
58 void *_private;
60
62typedef struct PKCS11_cert_st {
63 char *label;
64 unsigned char *id;
65 size_t id_len;
66 X509 *x509;
67 void *_private;
69
71typedef struct PKCS11_token_st {
72 char *label;
73 char *manufacturer;
74 char *model;
75 char *serialnr;
76 unsigned char initialized;
77 unsigned char loginRequired;
78 unsigned char secureLogin;
79 unsigned char userPinSet;
80 unsigned char readOnly;
81 unsigned char hasRng;
82 unsigned char userPinCountLow;
83 unsigned char userPinFinalTry;
84 unsigned char userPinLocked;
85 unsigned char userPinToBeChanged;
86 unsigned char soPinCountLow;
87 unsigned char soPinFinalTry;
88 unsigned char soPinLocked;
89 unsigned char soPinToBeChanged;
90 struct PKCS11_slot_st *slot;
92
94typedef struct PKCS11_slot_st {
95 char *manufacturer;
96 char *description;
97 unsigned char removable;
99 void *_private;
101
103typedef struct PKCS11_ctx_st {
104 char *manufacturer;
105 char *description;
106 void *_private;
108
116
122extern void PKCS11_CTX_init_args(PKCS11_CTX *ctx, const char *init_args);
123
132extern int PKCS11_CTX_load(PKCS11_CTX *ctx, const char *ident);
133
140
146extern void PKCS11_CTX_free(PKCS11_CTX *ctx);
147
155extern int PKCS11_open_session(PKCS11_SLOT *slot, int rw);
156
167 PKCS11_SLOT **slotsp, unsigned int *nslotsp);
168
184 PKCS11_SLOT **slotsp, unsigned int *nslotsp);
185
192extern unsigned long PKCS11_get_slotid_from_slot(PKCS11_SLOT *slotp);
193
202 PKCS11_SLOT *slots, unsigned int nslots);
203
214 PKCS11_SLOT *slots, unsigned int nslots);
215
227 PKCS11_SLOT *slots, unsigned int nslots,
228 PKCS11_SLOT *slot);
229
239extern int PKCS11_is_logged_in(PKCS11_SLOT *slot, int so, int *res);
240
250extern int PKCS11_login(PKCS11_SLOT *slot, int so, const char *pin);
251
259extern int PKCS11_logout(PKCS11_SLOT *slot);
260
261/* Get a list of private keys associated with this token */
262extern int PKCS11_enumerate_keys(PKCS11_TOKEN *,
263 PKCS11_KEY **, unsigned int *);
264
265/* Get a list of private keys associated with this token and matching the key template */
266extern int PKCS11_enumerate_keys_ext(PKCS11_TOKEN *,
267 const PKCS11_KEY *, PKCS11_KEY **, unsigned int *);
268
269/* Remove the key from this token */
270extern int PKCS11_remove_key(PKCS11_KEY *);
271
272/* Get a list of public keys associated with this token */
273extern int PKCS11_enumerate_public_keys(PKCS11_TOKEN *,
274 PKCS11_KEY **, unsigned int *);
275
276/* Get a list of public keys associated with this token and matching the key template */
277extern int PKCS11_enumerate_public_keys_ext(PKCS11_TOKEN *,
278 const PKCS11_KEY *, PKCS11_KEY **, unsigned int *);
279
280/* Get the key type (as EVP_PKEY_XXX) */
281extern int PKCS11_get_key_type(PKCS11_KEY *);
282
290extern EVP_PKEY *PKCS11_get_private_key(PKCS11_KEY *key);
291
299extern EVP_PKEY *PKCS11_get_public_key(PKCS11_KEY *key);
300
301/* Find the corresponding certificate (if any) */
302extern PKCS11_CERT *PKCS11_find_certificate(PKCS11_KEY *);
303
304/* Find the corresponding key (if any) */
305extern PKCS11_KEY *PKCS11_find_key(PKCS11_CERT *);
306
307/* Get a list of all certificates associated with this token */
308extern int PKCS11_enumerate_certs(PKCS11_TOKEN *, PKCS11_CERT **, unsigned int *);
309
310/* Get a list of all certificates associated with this token and matching cert template */
311extern int PKCS11_enumerate_certs_ext(PKCS11_TOKEN *,
312 const PKCS11_CERT *, PKCS11_CERT **, unsigned int *);
313
314/* Remove the certificate from this token */
315extern int PKCS11_remove_certificate(PKCS11_CERT *);
316
317/* Set UI method to allow retrieving CKU_CONTEXT_SPECIFIC PINs interactively */
318extern int PKCS11_set_ui_method(PKCS11_CTX *ctx,
319 UI_METHOD *ui_method, void *ui_user_data);
320
330extern int PKCS11_init_token(PKCS11_TOKEN *token, const char *pin,
331 const char *label);
332
341extern int PKCS11_init_pin(PKCS11_TOKEN *token, const char *pin);
342
352extern int PKCS11_change_pin(PKCS11_SLOT *slot, const char *old_pin,
353 const char *new_pin);
354
366extern int PKCS11_store_private_key(PKCS11_TOKEN *token, EVP_PKEY *pk, char *label, unsigned char *id, size_t id_len);
367
379extern int PKCS11_store_public_key(PKCS11_TOKEN *token, EVP_PKEY *pk, char *label, unsigned char *id, size_t id_len);
380
393extern int PKCS11_store_certificate(PKCS11_TOKEN *token, X509 *x509,
394 char *label, unsigned char *id, size_t id_len,
395 PKCS11_CERT **ret_cert);
396
397/* Access the random number generator */
398extern int PKCS11_seed_random(PKCS11_SLOT *slot, const unsigned char *s, unsigned int s_len);
399extern int PKCS11_generate_random(PKCS11_SLOT *slot, unsigned char *r, unsigned int r_len);
400
401/*
402 * PKCS#11 implementation for OpenSSL methods
403 */
404RSA_METHOD *PKCS11_get_rsa_method(void);
405/* Also define unsupported methods to retain backward compatibility */
406#if OPENSSL_VERSION_NUMBER >= 0x10100002L && !defined(LIBRESSL_VERSION_NUMBER)
407EC_KEY_METHOD *PKCS11_get_ec_key_method(void);
408void *PKCS11_get_ecdsa_method(void);
409void *PKCS11_get_ecdh_method(void);
410#else
411void *PKCS11_get_ec_key_method(void);
412ECDSA_METHOD *PKCS11_get_ecdsa_method(void);
413ECDH_METHOD *PKCS11_get_ecdh_method(void);
414#endif
415int PKCS11_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
416 const int **nids, int nid);
417
424extern void ERR_load_PKCS11_strings(void);
425
426/*
427 * The following functions are discouraged, because they partially
428 * duplicate the functionality OpenSSL provides for EVP_PKEY objects
429 */
430
444 int algorithm, unsigned int bits,
445 char *label, unsigned char *id, size_t id_len);
446
447/* Get the RSA key modulus size (in bytes) */
448extern int PKCS11_get_key_size(PKCS11_KEY *);
449
450/* Get the RSA key modules as BIGNUM */
451extern int PKCS11_get_key_modulus(PKCS11_KEY *, BIGNUM **);
452
453/* Get the RSA key public exponent as BIGNUM */
454extern int PKCS11_get_key_exponent(PKCS11_KEY *, BIGNUM **);
455
456/* Sign with the EC private key */
457extern int PKCS11_ecdsa_sign(
458 const unsigned char *m, unsigned int m_len,
459 unsigned char *sigret, unsigned int *siglen, PKCS11_KEY *key);
460
461/* Sign with the RSA private key */
462extern int PKCS11_sign(int type,
463 const unsigned char *m, unsigned int m_len,
464 unsigned char *sigret, unsigned int *siglen, PKCS11_KEY *key);
465
466/* This function has never been implemented */
467extern int PKCS11_verify(int type,
468 const unsigned char *m, unsigned int m_len,
469 unsigned char *signature, unsigned int siglen, PKCS11_KEY *key);
470
471/* Encrypts data using the private key */
472extern int PKCS11_private_encrypt(
473 int flen, const unsigned char *from,
474 unsigned char *to, PKCS11_KEY *rsa, int padding);
475
487 int flen, const unsigned char *from,
488 unsigned char *to, PKCS11_KEY *key, int padding);
489
490/* Function codes */
491# define CKR_F_PKCS11_CHANGE_PIN 100
492# define CKR_F_PKCS11_CHECK_TOKEN 101
493# define CKR_F_PKCS11_CTX_LOAD 102
494# define CKR_F_PKCS11_ECDH_DERIVE 103
495# define CKR_F_PKCS11_ECDSA_SIGN 104
496# define CKR_F_PKCS11_ENUMERATE_SLOTS 105
497# define CKR_F_PKCS11_FIND_CERTS 106
498# define CKR_F_PKCS11_FIND_KEYS 107
499# define CKR_F_PKCS11_GENERATE_RANDOM 108
500# define CKR_F_PKCS11_GETATTR_ALLOC 109
501# define CKR_F_PKCS11_GETATTR_BN 110
502# define CKR_F_PKCS11_GETATTR_INT 111
503# define CKR_F_PKCS11_INIT_PIN 112
504# define CKR_F_PKCS11_INIT_SLOT 113
505# define CKR_F_PKCS11_INIT_TOKEN 114
506# define CKR_F_PKCS11_IS_LOGGED_IN 115
507# define CKR_F_PKCS11_LOGIN 116
508# define CKR_F_PKCS11_LOGOUT 117
509# define CKR_F_PKCS11_NEXT_CERT 118
510# define CKR_F_PKCS11_NEXT_KEY 119
511# define CKR_F_PKCS11_OPEN_SESSION 120
512# define CKR_F_PKCS11_PRIVATE_DECRYPT 121
513# define CKR_F_PKCS11_PRIVATE_ENCRYPT 122
514# define CKR_F_PKCS11_RELOAD_KEY 123
515# define CKR_F_PKCS11_SEED_RANDOM 125
516# define CKR_F_PKCS11_STORE_CERTIFICATE 126
517# define CKR_F_PKCS11_STORE_KEY 127
518# define CKR_F_PKCS11_REMOVE_KEY 128
519# define CKR_F_PKCS11_REMOVE_CERTIFICATE 129
520# define CKR_F_PKCS11_GENERATE_KEY 130
521# define CKR_F_PKCS11_RELOAD_CERTIFICATE 131
522# define CKR_F_PKCS11_GET_SESSION 132
523
524/* Backward compatibility of error function codes */
525#define PKCS11_F_PKCS11_CHANGE_PIN CKR_F_PKCS11_CHANGE_PIN
526#define PKCS11_F_PKCS11_CHECK_TOKEN CKR_F_PKCS11_CHECK_TOKEN
527#define PKCS11_F_PKCS11_CTX_LOAD CKR_F_PKCS11_CTX_LOAD
528#define PKCS11_F_PKCS11_ECDH_DERIVE CKR_F_PKCS11_ECDH_DERIVE
529#define PKCS11_F_PKCS11_ECDSA_SIGN CKR_F_PKCS11_ECDSA_SIGN
530#define PKCS11_F_PKCS11_ENUMERATE_SLOTS CKR_F_PKCS11_ENUMERATE_SLOTS
531#define PKCS11_F_PKCS11_FIND_CERTS CKR_F_PKCS11_FIND_CERTS
532#define PKCS11_F_PKCS11_FIND_KEYS CKR_F_PKCS11_FIND_KEYS
533#define PKCS11_F_PKCS11_GENERATE_RANDOM CKR_F_PKCS11_GENERATE_RANDOM
534#define PKCS11_F_PKCS11_GETATTR_ALLOC CKR_F_PKCS11_GETATTR_ALLOC
535#define PKCS11_F_PKCS11_GETATTR_BN CKR_F_PKCS11_GETATTR_BN
536#define PKCS11_F_PKCS11_GETATTR_INT CKR_F_PKCS11_GETATTR_INT
537#define PKCS11_F_PKCS11_INIT_PIN CKR_F_PKCS11_INIT_PIN
538#define PKCS11_F_PKCS11_INIT_SLOT CKR_F_PKCS11_INIT_SLOT
539#define PKCS11_F_PKCS11_INIT_TOKEN CKR_F_PKCS11_INIT_TOKEN
540#define PKCS11_F_PKCS11_IS_LOGGED_IN CKR_F_PKCS11_IS_LOGGED_IN
541#define PKCS11_F_PKCS11_LOGIN CKR_F_PKCS11_LOGIN
542#define PKCS11_F_PKCS11_LOGOUT CKR_F_PKCS11_LOGOUT
543#define PKCS11_F_PKCS11_NEXT_CERT CKR_F_PKCS11_NEXT_CERT
544#define PKCS11_F_PKCS11_NEXT_KEY CKR_F_PKCS11_NEXT_KEY
545#define PKCS11_F_PKCS11_OPEN_SESSION CKR_F_PKCS11_OPEN_SESSION
546#define PKCS11_F_PKCS11_PRIVATE_DECRYPT CKR_F_PKCS11_PRIVATE_DECRYPT
547#define PKCS11_F_PKCS11_PRIVATE_ENCRYPT CKR_F_PKCS11_PRIVATE_ENCRYPT
548#define PKCS11_F_PKCS11_RELOAD_KEY CKR_F_PKCS11_RELOAD_KEY
549#define PKCS11_F_PKCS11_SEED_RANDOM CKR_F_PKCS11_SEED_RANDOM
550#define PKCS11_F_PKCS11_STORE_CERTIFICATE CKR_F_PKCS11_STORE_CERTIFICATE
551#define PKCS11_F_PKCS11_STORE_KEY CKR_F_PKCS11_STORE_KEY
552#define PKCS11_F_PKCS11_REMOVE_KEY CKR_F_PKCS11_REMOVE_KEY
553#define PKCS11_F_PKCS11_REMOVE_CERTIFICATE CKR_F_PKCS11_REMOVE_CERTIFICATE
554#define PKCS11_F_PKCS11_GENERATE_KEY CKR_F_PKCS11_GENERATE_KEY
555
556/* Backward compatibility of error reason codes */
557#define PKCS11_LOAD_MODULE_ERROR P11_R_LOAD_MODULE_ERROR
558#define PKCS11_MODULE_LOADED_ERROR -1
559#define PKCS11_SYMBOL_NOT_FOUND_ERROR -1
560#define PKCS11_NOT_SUPPORTED P11_R_NOT_SUPPORTED
561#define PKCS11_NO_SESSION P11_R_NO_SESSION
562#define PKCS11_KEYGEN_FAILED P11_R_KEYGEN_FAILED
563#define PKCS11_UI_FAILED P11_R_UI_FAILED
564
565/* Backward compatibility emulation of the ERR_LIB_PKCS11 constant.
566 * We currently use two separate variables for library error codes:
567 * one for imported PKCS#11 module errors, and one for our own libp11 errors.
568 * We return the value for PKCS#11, as it is more likely to be needed. */
569#define ERR_LIB_PKCS11 (ERR_get_CKR_code())
570
571#ifdef __cplusplus
572}
573#endif
574#endif
575
576/* vim: set noexpandtab: */
struct PKCS11_ctx_st PKCS11_CTX
PKCS11 context.
unsigned long PKCS11_get_slotid_from_slot(PKCS11_SLOT *slotp)
Get the slot_id from a slot as it is stored in private.
int PKCS11_store_public_key(PKCS11_TOKEN *token, EVP_PKEY *pk, char *label, unsigned char *id, size_t id_len)
Store public key on a token.
void PKCS11_CTX_free(PKCS11_CTX *ctx)
Free a libp11 context.
struct PKCS11_slot_st PKCS11_SLOT
PKCS11 slot: card reader.
int PKCS11_change_pin(PKCS11_SLOT *slot, const char *old_pin, const char *new_pin)
Change the currently used (either USER or SO) PIN on a token.
struct PKCS11_key_st PKCS11_KEY
PKCS11 key object (public or private)
int PKCS11_init_pin(PKCS11_TOKEN *token, const char *pin)
Initialize the user PIN on a token.
int PKCS11_store_certificate(PKCS11_TOKEN *token, X509 *x509, char *label, unsigned char *id, size_t id_len, PKCS11_CERT **ret_cert)
Store certificate on a token.
EVP_PKEY * PKCS11_get_public_key(PKCS11_KEY *key)
Returns a EVP_PKEY object with the public key.
int PKCS11_generate_key(PKCS11_TOKEN *token, int algorithm, unsigned int bits, char *label, unsigned char *id, size_t id_len)
Generate a private key on the token.
int PKCS11_login(PKCS11_SLOT *slot, int so, const char *pin)
Authenticate to the card.
void PKCS11_CTX_unload(PKCS11_CTX *ctx)
Unload a PKCS#11 module.
PKCS11_SLOT * PKCS11_find_next_token(PKCS11_CTX *ctx, PKCS11_SLOT *slots, unsigned int nslots, PKCS11_SLOT *slot)
Find the next slot with a token.
int PKCS11_init_token(PKCS11_TOKEN *token, const char *pin, const char *label)
Initialize a token.
int PKCS11_enumerate_slots(PKCS11_CTX *ctx, PKCS11_SLOT **slotsp, unsigned int *nslotsp)
Get a list of all slots.
PKCS11_CTX * PKCS11_CTX_new(void)
Create a new libp11 context.
EVP_PKEY * PKCS11_get_private_key(PKCS11_KEY *key)
Returns a EVP_PKEY object for the private key.
int PKCS11_is_logged_in(PKCS11_SLOT *slot, int so, int *res)
Check if user is already authenticated to a card.
int PKCS11_update_slots(PKCS11_CTX *ctx, PKCS11_SLOT **slotsp, unsigned int *nslotsp)
Get or update a list of all slots.
void PKCS11_CTX_init_args(PKCS11_CTX *ctx, const char *init_args)
Specify any private PKCS#11 module initialization args, if necessary.
struct PKCS11_token_st PKCS11_TOKEN
PKCS11 token: smart card or USB key.
struct PKCS11_cert_st PKCS11_CERT
PKCS11 certificate object.
int PKCS11_CTX_load(PKCS11_CTX *ctx, const char *ident)
Load a PKCS#11 module.
void PKCS11_release_all_slots(PKCS11_CTX *ctx, PKCS11_SLOT *slots, unsigned int nslots)
Free the list of slots allocated by PKCS11_enumerate_slots()
PKCS11_SLOT * PKCS11_find_token(PKCS11_CTX *ctx, PKCS11_SLOT *slots, unsigned int nslots)
Find the first slot with a token.
int PKCS11_open_session(PKCS11_SLOT *slot, int rw)
Open a session in RO or RW mode.
int PKCS11_private_decrypt(int flen, const unsigned char *from, unsigned char *to, PKCS11_KEY *key, int padding)
Decrypts data using the private key.
int PKCS11_store_private_key(PKCS11_TOKEN *token, EVP_PKEY *pk, char *label, unsigned char *id, size_t id_len)
Store private key on a token.
void ERR_load_PKCS11_strings(void)
Load PKCS11 error strings.
int PKCS11_logout(PKCS11_SLOT *slot)
De-authenticate from the card.
PKCS11 certificate object.
Definition libp11.h:62
PKCS11 context.
Definition libp11.h:103
PKCS11 key object (public or private)
Definition libp11.h:52
unsigned char isPrivate
private key present?
Definition libp11.h:56
unsigned char needLogin
login to read private key?
Definition libp11.h:57
PKCS11 slot: card reader.
Definition libp11.h:94
PKCS11_TOKEN * token
NULL if no token present.
Definition libp11.h:98
PKCS11 token: smart card or USB key.
Definition libp11.h:71

libp11, Copyright (C) 2005 Olaf Kirch <okir@lst.de>OpenSC-Project.org Logo