libp11  0.3.0
libp11.h
Go to the documentation of this file.
00001 /* libp11, a simple layer on to of PKCS#11 API
00002  * Copyright (C) 2005 Olaf Kirch <okir@lst.de>
00003  *
00004  *  This library is free software; you can redistribute it and/or
00005  *  modify it under the terms of the GNU Lesser General Public
00006  *  License as published by the Free Software Foundation; either
00007  *  version 2.1 of the License, or (at your option) any later version.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Lesser General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Lesser General Public
00015  *  License along with this library; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00017  */
00018 
00024 #ifndef _LIB11_H
00025 #define _LIB11_H
00026 
00027 #include <openssl/bio.h>
00028 #include <openssl/err.h>
00029 #include <openssl/x509.h>
00030 
00031 #ifdef __cplusplus
00032 extern "C" {
00033 #endif
00034 
00035 /* get some structures for local code to handle pkcs11 data readily */
00036 #define ERR_LIB_PKCS11  ERR_LIB_USER
00037 
00038 #define PKCS11err(f,r) \
00039 ERR_PUT_error(ERR_LIB_PKCS11,(f),(r),__FILE__,__LINE__)
00040 
00041 /*
00042  * The purpose of this library is to provide a simple PKCS11
00043  * interface to OpenSSL application that wish to use a previously
00044  * initialized card (as opposed to initializing it, etc).
00045  *
00046  * I am therefore making some simplifying assumptions:
00047  *
00048  *  -   no support for any operations that alter the card,
00049  *      i.e. readonly-login
00050  */
00051 
00053 typedef struct PKCS11_key_st {
00054         char *label;
00055         unsigned char *id;
00056         size_t id_len;
00057         unsigned char isPrivate;        
00058         unsigned char needLogin;        
00059         EVP_PKEY *evp_key;              
00060         void *_private;
00061 } PKCS11_KEY;
00062 
00064 typedef struct PKCS11_cert_st {
00065         char *label;
00066         unsigned char *id;
00067         size_t id_len;
00068         X509 *x509;
00069         void *_private;
00070 } PKCS11_CERT;
00071 
00073 typedef struct PKCS11_token_st {
00074         char *label;
00075         char *manufacturer;
00076         char *model;
00077         char *serialnr;
00078         unsigned char initialized;
00079         unsigned char loginRequired;
00080         unsigned char secureLogin;
00081         unsigned char userPinSet;
00082         unsigned char readOnly;
00083         unsigned char hasRng;
00084         unsigned char userPinCountLow;
00085         unsigned char userPinFinalTry;
00086         unsigned char userPinLocked;
00087         unsigned char userPinToBeChanged;
00088         unsigned char soPinCountLow;
00089         unsigned char soPinFinalTry;
00090         unsigned char soPinLocked;
00091         unsigned char soPinToBeChanged;
00092         void *_private;
00093 } PKCS11_TOKEN;
00094 
00096 typedef struct PKCS11_slot_st {
00097         char *manufacturer;
00098         char *description;
00099         unsigned char removable;
00100         PKCS11_TOKEN *token;    
00101         void *_private;
00102 } PKCS11_SLOT;
00103 
00105 typedef struct PKCS11_ctx_st {
00106         char *manufacturer;
00107         char *description;
00108         void *_private;
00109 } PKCS11_CTX;
00110 
00117 extern PKCS11_CTX *PKCS11_CTX_new(void);
00118 
00124 extern void PKCS11_CTX_init_args(PKCS11_CTX * ctx, const char * init_args);
00125 
00134 extern int PKCS11_CTX_load(PKCS11_CTX * ctx, const char * ident);
00135 
00143 extern int PKCS11_CTX_reload(PKCS11_CTX * ctx);
00144 
00150 extern void PKCS11_CTX_unload(PKCS11_CTX * ctx);
00151 
00157 extern void PKCS11_CTX_free(PKCS11_CTX * ctx);
00158 
00166 extern int PKCS11_open_session(PKCS11_SLOT * slot, int rw);
00167 
00177 extern int PKCS11_enumerate_slots(PKCS11_CTX * ctx,
00178                         PKCS11_SLOT **slotsp, unsigned int *nslotsp);
00179 
00186 extern unsigned long PKCS11_get_slotid_from_slot(PKCS11_SLOT *slotp);
00187 
00195 extern void PKCS11_release_all_slots(PKCS11_CTX * ctx,
00196                         PKCS11_SLOT *slots, unsigned int nslots);
00197 
00207 PKCS11_SLOT *PKCS11_find_token(PKCS11_CTX * ctx, 
00208                         PKCS11_SLOT *slots, unsigned int nslots);
00209 
00219 extern int PKCS11_login(PKCS11_SLOT * slot, int so, const char *pin);
00220 
00228 extern int PKCS11_logout(PKCS11_SLOT * slot);
00229 
00230 /* Get a list of all keys associated with this token */
00231 extern int PKCS11_enumerate_keys(PKCS11_TOKEN *, PKCS11_KEY **, unsigned int *);
00232 
00233 /* Get the key type (as EVP_PKEY_XXX) */
00234 extern int PKCS11_get_key_type(PKCS11_KEY *);
00235 
00236 /* Get size of key modulus in number of bytes */
00237 extern int PKCS11_get_key_size(const PKCS11_KEY *);
00238 /* Get actual modules and public exponent as BIGNUM */
00239 extern int PKCS11_get_key_modulus(PKCS11_KEY *, BIGNUM **);
00240 extern int PKCS11_get_key_exponent(PKCS11_KEY *, BIGNUM **);
00241 
00242 /* Get the enveloped private key */
00252 extern EVP_PKEY *PKCS11_get_private_key(PKCS11_KEY *key);
00262 extern EVP_PKEY *PKCS11_get_public_key(PKCS11_KEY *key);
00263 
00264 /* Find the corresponding certificate (if any) */
00265 extern PKCS11_CERT *PKCS11_find_certificate(PKCS11_KEY *);
00266 
00267 /* Find the corresponding key (if any) */
00268 extern PKCS11_KEY *PKCS11_find_key(PKCS11_CERT *);
00269 
00270 /* Find the corresponding key (if any)  pub <-> priv base on ID */
00271 extern PKCS11_KEY *PKCS11_find_key_from_key(PKCS11_KEY *);
00272 
00273 /* Get a list of all certificates associated with this token */
00274 extern int PKCS11_enumerate_certs(PKCS11_TOKEN *, PKCS11_CERT **, unsigned int *);
00275 
00285 extern int PKCS11_init_token(PKCS11_TOKEN * token, const char *pin,
00286         const char *label);
00287 
00296 extern int PKCS11_init_pin(PKCS11_TOKEN * token, const char *pin);
00297 
00307 extern int PKCS11_change_pin(PKCS11_SLOT * slot, const char *old_pin,
00308         const char *new_pin);
00309 
00323 extern int PKCS11_generate_key(PKCS11_TOKEN * token, int algorithm, unsigned int bits, char *label, unsigned char* id, size_t id_len);
00324 
00336 extern int PKCS11_store_private_key(PKCS11_TOKEN * token, EVP_PKEY * pk, char *label, unsigned char *id, size_t id_len);
00337 
00349 extern int PKCS11_store_public_key(PKCS11_TOKEN * token, EVP_PKEY * pk, char *label, unsigned char *id, size_t id_len);
00350 
00363 extern int PKCS11_store_certificate(PKCS11_TOKEN * token, X509 * x509,
00364                 char *label, unsigned char *id, size_t id_len,
00365                 PKCS11_CERT **ret_cert);
00366 
00367 /* rsa private key operations */
00368 extern int PKCS11_sign(int type, const unsigned char *m, unsigned int m_len,
00369         unsigned char *sigret, unsigned int *siglen, PKCS11_KEY * key);
00370 extern int PKCS11_private_encrypt(int flen, const unsigned char *from,
00371         unsigned char *to, PKCS11_KEY * rsa, int padding);
00382 extern int PKCS11_private_decrypt(int flen, const unsigned char *from,
00383         unsigned char *to, PKCS11_KEY * key, int padding);
00384 extern int PKCS11_verify(int type, const unsigned char *m, unsigned int m_len,
00385         unsigned char *signature, unsigned int siglen, PKCS11_KEY * key);
00386 
00387 /* access random number generator */
00388 extern int PKCS11_seed_random(PKCS11_SLOT *, const unsigned char *s, unsigned int s_len);
00389 extern int PKCS11_generate_random(PKCS11_SLOT *, unsigned char *r, unsigned int r_len);
00390 
00391 /* using with openssl method mechanism */
00392 RSA_METHOD *PKCS11_get_rsa_method(void);
00393 ECDSA_METHOD  *PKCS11_get_ecdsa_method(void);
00394 void PKCS11_ecdsa_method_free(void);
00395 
00402 extern void ERR_load_PKCS11_strings(void);
00403 
00404 /*
00405  * Function and reason codes
00406  */
00407 #define PKCS11_F_PKCS11_CTX_LOAD                1
00408 #define PKCS11_F_PKCS11_ENUM_SLOTS              2
00409 #define PKCS11_F_PKCS11_CHECK_TOKEN             3
00410 #define PKCS11_F_PKCS11_OPEN_SESSION            4
00411 #define PKCS11_F_PKCS11_LOGIN                   5
00412 #define PKCS11_F_PKCS11_ENUM_KEYS               6
00413 #define PKCS11_F_PKCS11_GET_KEY                 7
00414 #define PKCS11_F_PKCS11_RSA_DECRYPT             8
00415 #define PKCS11_F_PKCS11_RSA_ENCRYPT             9
00416 #define PKCS11_F_PKCS11_RSA_SIGN                10
00417 #define PKCS11_F_PKCS11_RSA_VERIFY              11
00418 #define PKCS11_F_PKCS11_ENUM_CERTS              12
00419 #define PKCS11_F_PKCS11_INIT_TOKEN              13
00420 #define PKCS11_F_PKCS11_INIT_PIN                14
00421 #define PKCS11_F_PKCS11_LOGOUT                  15
00422 #define PKCS11_F_PKCS11_STORE_PRIVATE_KEY       16
00423 #define PKCS11_F_PKCS11_GENERATE_KEY            17
00424 #define PKCS11_F_PKCS11_STORE_PUBLIC_KEY        18
00425 #define PKCS11_F_PKCS11_STORE_CERTIFICATE       19
00426 #define PKCS11_F_PKCS11_SEED_RANDOM             20
00427 #define PKCS11_F_PKCS11_GENERATE_RANDOM         21
00428 #define PKCS11_F_PKCS11_CHANGE_PIN              22
00429 #define PKCS11_F_PKCS11_GETATTR                 40
00430 #define PKCS11_F_PKCS11_EC_KEY_SIGN                     41
00431 #define PKCS11_F_PKCS11_EC_KEY_VERIFY           42
00432 
00433 #define PKCS11_ERR_BASE                         1024
00434 #define PKCS11_LOAD_MODULE_ERROR                (PKCS11_ERR_BASE+1)
00435 #define PKCS11_MODULE_LOADED_ERROR              (PKCS11_ERR_BASE+2)
00436 #define PKCS11_SYMBOL_NOT_FOUND_ERROR           (PKCS11_ERR_BASE+3)
00437 #define PKCS11_NOT_SUPPORTED                    (PKCS11_ERR_BASE+4)
00438 #define PKCS11_NO_SESSION                       (PKCS11_ERR_BASE+5)
00439 #define PKCS11_KEYGEN_FAILED                    (PKCS11_ERR_BASE+6)
00440 
00441 #ifdef __cplusplus
00442 }
00443 #endif
00444 #endif

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