diff --git a/examples/listkeys_ext.c b/examples/listkeys_ext.c index c17653c7..3b95ca0a 100644 --- a/examples/listkeys_ext.c +++ b/examples/listkeys_ext.c @@ -36,15 +36,10 @@ * list the keys matching provided id or label. */ -#include -#include -#include +#define _POSIX_C_SOURCE 200809L #include #include -#include -#include #include -#include #define RANDOM_SOURCE "/dev/urandom" #define RANDOM_SIZE 20 @@ -123,8 +118,8 @@ int hex2bin(unsigned char *dst, const char *str, size_t *op_len) while(byte_len--) { /* start parsing from end of hexstring to beginning of hex string */ /* len is including '\0' so use pre-decrement */ - int lsb = getbin(str[--len]); // this never goes out of bounds, we will have at least one byte to process! - int msb = len-- > 0 ? getbin(str[len]) : 0; // avoid underflow on str (when len is not even we assume 0) + int lsb = getbin(str[--len]); /* this never goes out of bounds, we will have at least one byte to process! */ + int msb = len-- > 0 ? getbin(str[len]) : 0; /* avoid underflow on str (when len is not even we assume 0) */ /* combine msb and lsb to make uint8_t; */ dst[byte_len] = msb << 4 | lsb; diff --git a/src/eng_back.c b/src/eng_back.c index 232e4536..fa4714a4 100644 --- a/src/eng_back.c +++ b/src/eng_back.c @@ -236,7 +236,6 @@ static int ENGINE_CTX_ctrl_load_cert(ENGINE_CTX *ctx, void *p) X509 *cert; } *parms = p; X509 *cert; - PKCS11_CTX *pkcs11_ctx; if (!parms) { ENGerr(ENG_F_CTX_CTRL_LOAD_CERT, ERR_R_PASSED_NULL_PARAMETER); @@ -281,7 +280,7 @@ static int ENGINE_CTX_ctrl_set_vlog(ENGINE_CTX *ctx, void *cb) return 1; } -int ENGINE_CTX_ctrl(ENGINE_CTX *ctx, int cmd, long i, void *p, void (*f)()) +int ENGINE_CTX_ctrl(ENGINE_CTX *ctx, int cmd, long i, void *p, void (*f)(void)) { static UI_METHOD *ui_method = NULL; static void *ui_data = NULL; diff --git a/src/engine.h b/src/engine.h index 32d0f841..fe00e83a 100644 --- a/src/engine.h +++ b/src/engine.h @@ -73,7 +73,7 @@ typedef struct engine_ctx_st ENGINE_CTX; /* opaque */ /* defined in eng_back.c */ -ENGINE_CTX *ENGINE_CTX_new(); +ENGINE_CTX *ENGINE_CTX_new(void); int ENGINE_CTX_destroy(ENGINE_CTX *ctx); @@ -81,7 +81,7 @@ int ENGINE_CTX_init(ENGINE_CTX *ctx); int ENGINE_CTX_finish(ENGINE_CTX *ctx); -int ENGINE_CTX_ctrl(ENGINE_CTX *ctx, int cmd, long i, void *p, void (*f)()); +int ENGINE_CTX_ctrl(ENGINE_CTX *ctx, int cmd, long i, void *p, void (*f)(void)); EVP_PKEY *ENGINE_CTX_load_pubkey(ENGINE_CTX *ctx, const char *s_key_id, UI_METHOD *ui_method, void *ui_data); diff --git a/src/libp11-int.h b/src/libp11-int.h index ccac1332..d94bb3ac 100644 --- a/src/libp11-int.h +++ b/src/libp11-int.h @@ -150,7 +150,7 @@ extern char *pkcs11_strdup(char *, size_t); extern void pkcs11_log(PKCS11_CTX_private *pctx, int level, const char *format, ...); /* Reinitializing the module after fork (if detected) */ -extern unsigned int get_forkid(); +extern unsigned int get_forkid(void); extern int check_fork(PKCS11_CTX_private *ctx); extern int check_slot_fork(PKCS11_SLOT_private *slot); extern int check_object_fork(PKCS11_OBJECT_private *key); diff --git a/src/p11_ec.c b/src/p11_ec.c index 98c98d99..765ac791 100644 --- a/src/p11_ec.c +++ b/src/p11_ec.c @@ -98,6 +98,7 @@ struct ecdsa_method { ECDSA_METHOD *ECDSA_METHOD_new(const ECDSA_METHOD *m) { ECDSA_METHOD *out; + out = OPENSSL_malloc(sizeof(ECDSA_METHOD)); if (!out) return NULL; @@ -140,6 +141,7 @@ struct ecdh_method { ECDH_METHOD *ECDH_METHOD_new(const ECDH_METHOD *m) { ECDH_METHOD *out; + out = OPENSSL_malloc(sizeof(ECDH_METHOD)); if (!out) return NULL; @@ -170,7 +172,7 @@ void ECDH_METHOD_set_compute_key(ECDH_METHOD *m, compute_key_fn f) /********** Manage EC ex_data */ /* NOTE: ECDH also uses ECDSA ex_data and *not* ECDH ex_data */ -static void alloc_ec_ex_index() +static void alloc_ec_ex_index(void) { if (ec_ex_index == 0) { while (ec_ex_index == 0) /* Workaround for OpenSSL RT3710 */ @@ -186,7 +188,7 @@ static void alloc_ec_ex_index() } } -static void free_ec_ex_index() +static void free_ec_ex_index(void) { if (ec_ex_index > 0) { #if OPENSSL_VERSION_NUMBER >= 0x10100002L diff --git a/src/p11_pkey.c b/src/p11_pkey.c index 9c97bdb2..46fcf56d 100644 --- a/src/p11_pkey.c +++ b/src/p11_pkey.c @@ -476,7 +476,7 @@ static int pkcs11_pkey_rsa_decrypt(EVP_PKEY_CTX *evp_pkey_ctx, return ret; } -static EVP_PKEY_METHOD *pkcs11_pkey_method_rsa() +static EVP_PKEY_METHOD *pkcs11_pkey_method_rsa(void) { EVP_PKEY_METHOD *orig_meth, *new_meth; @@ -619,7 +619,7 @@ static int pkcs11_pkey_ec_sign(EVP_PKEY_CTX *evp_pkey_ctx, return ret; } -static EVP_PKEY_METHOD *pkcs11_pkey_method_ec() +static EVP_PKEY_METHOD *pkcs11_pkey_method_ec(void) { EVP_PKEY_METHOD *orig_meth, *new_meth; diff --git a/src/p11_rsa.c b/src/p11_rsa.c index 359e9de9..a8897d81 100644 --- a/src/p11_rsa.c +++ b/src/p11_rsa.c @@ -33,6 +33,7 @@ static RSA *pkcs11_rsa(PKCS11_OBJECT_private *key) { EVP_PKEY *evp_key = pkcs11_get_key(key, key->object_class); RSA *rsa; + if (!evp_key) return NULL; rsa = (RSA *)EVP_PKEY_get0_RSA(evp_key); @@ -52,6 +53,7 @@ int pkcs11_sign(int type, const unsigned char *m, unsigned int m_len, unsigned char *sigret, unsigned int *siglen, PKCS11_OBJECT_private *key) { RSA *rsa = pkcs11_rsa(key); + if (!rsa) return -1; return RSA_sign(type, m, m_len, sigret, siglen, rsa); @@ -361,6 +363,7 @@ int pkcs11_get_key_exponent(PKCS11_OBJECT_private *key, BIGNUM **bn) int pkcs11_get_key_size(PKCS11_OBJECT_private *key) { RSA *rsa = pkcs11_rsa(key); + if (!rsa) return 0; return RSA_size(rsa); @@ -395,6 +398,7 @@ static int pkcs11_rsa_priv_dec_method(int flen, const unsigned char *from, PKCS11_OBJECT_private *key = pkcs11_get_ex_data_rsa(rsa); int (*priv_dec) (int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding); + if (check_object_fork(key) < 0) { priv_dec = RSA_meth_get_priv_dec(RSA_get_default_method()); return priv_dec(flen, from, to, rsa, padding); @@ -408,6 +412,7 @@ static int pkcs11_rsa_priv_enc_method(int flen, const unsigned char *from, PKCS11_OBJECT_private *key = pkcs11_get_ex_data_rsa(rsa); int (*priv_enc) (int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding); + if (check_object_fork(key) < 0) { priv_enc = RSA_meth_get_priv_enc(RSA_get_default_method()); return priv_enc(flen, from, to, rsa, padding); @@ -418,19 +423,20 @@ static int pkcs11_rsa_priv_enc_method(int flen, const unsigned char *from, static int pkcs11_rsa_free_method(RSA *rsa) { PKCS11_OBJECT_private *key = pkcs11_get_ex_data_rsa(rsa); + int (*orig_rsa_free_method)(RSA *rsa) = + RSA_meth_get_finish(RSA_get_default_method()); + if (key) { pkcs11_set_ex_data_rsa(rsa, NULL); pkcs11_object_free(key); } - int (*orig_rsa_free_method)(RSA *rsa) = - RSA_meth_get_finish(RSA_get_default_method()); if (orig_rsa_free_method) { return orig_rsa_free_method(rsa); } return 1; } -static void alloc_rsa_ex_index() +static void alloc_rsa_ex_index(void) { if (rsa_ex_index == 0) { while (rsa_ex_index == 0) /* Workaround for OpenSSL RT3710 */ @@ -441,7 +447,7 @@ static void alloc_rsa_ex_index() } } -static void free_rsa_ex_index() +static void free_rsa_ex_index(void) { /* CRYPTO_free_ex_index requires OpenSSL version >= 1.1.0-pre1 */ #if OPENSSL_VERSION_NUMBER >= 0x10100001L && !defined(LIBRESSL_VERSION_NUMBER) @@ -457,6 +463,7 @@ static void free_rsa_ex_index() static RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth) { RSA_METHOD *ret = OPENSSL_malloc(sizeof(RSA_METHOD)); + if (!ret) return NULL; memcpy(ret, meth, sizeof(RSA_METHOD)); @@ -471,6 +478,7 @@ static RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth) static int RSA_meth_set1_name(RSA_METHOD *meth, const char *name) { char *tmp = OPENSSL_strdup(name); + if (!tmp) return 0; OPENSSL_free((char *)meth->name); diff --git a/src/p11_slot.c b/src/p11_slot.c index 93dc9cf5..10c94cd9 100644 --- a/src/p11_slot.c +++ b/src/p11_slot.c @@ -17,6 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#define _POSIX_C_SOURCE 200809L #include "libp11-int.h" #include #include diff --git a/src/pkcs11.h b/src/pkcs11.h index c38a33c8..dac5c9b1 100644 --- a/src/pkcs11.h +++ b/src/pkcs11.h @@ -361,8 +361,10 @@ typedef unsigned long ck_key_type_t; #define CKK_GOST28147 (0x32UL) #define CKK_VENDOR_DEFINED (1UL << 31) -// A mask for new GOST algorithms. -// For details visit https://tc26.ru/standarts/perevody/guidelines-the-pkcs-11-extensions-for-implementing-the-gost-r-34-10-2012-and-gost-r-34-11-2012-russian-standards-.html +/* + * A mask for new GOST algorithms. For details visit: + * https://tc26.ru/standarts/perevody/guidelines-the-pkcs-11-extensions-for-implementing-the-gost-r-34-10-2012-and-gost-r-34-11-2012-russian-standards-.html + */ #define NSSCK_VENDOR_PKCS11_RU_TEAM (CKK_VENDOR_DEFINED | 0x54321000) #define CK_VENDOR_PKCS11_RU_TEAM_TK26 NSSCK_VENDOR_PKCS11_RU_TEAM diff --git a/src/util.h b/src/util.h index ece0efaa..5f9f489d 100644 --- a/src/util.h +++ b/src/util.h @@ -49,7 +49,7 @@ /* defined in util_uri.c */ typedef struct util_ctx_st UTIL_CTX; /* opaque */ -UTIL_CTX *UTIL_CTX_new(); +UTIL_CTX *UTIL_CTX_new(void); void UTIL_CTX_free(UTIL_CTX *ctx); int UTIL_CTX_set_module(UTIL_CTX *ctx, const char *module); int UTIL_CTX_set_init_args(UTIL_CTX *ctx, const char *init_args); diff --git a/src/util_uri.c b/src/util_uri.c index a155f0ca..01f34e09 100644 --- a/src/util_uri.c +++ b/src/util_uri.c @@ -28,7 +28,7 @@ #include "util.h" #include -#include +#include #if defined(_WIN32) || defined(_WIN64) #define strncasecmp _strnicmp