Skip to content

Commit

Permalink
Coding style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrojnar committed Feb 20, 2025
1 parent ff3b110 commit c398dc8
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 25 deletions.
11 changes: 3 additions & 8 deletions examples/listkeys_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,10 @@
* list the keys matching provided id or label.
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <libp11.h>
#include <unistd.h>

#define RANDOM_SOURCE "/dev/urandom"
#define RANDOM_SIZE 20
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/eng_back.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ 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);

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);
Expand Down
2 changes: 1 addition & 1 deletion src/libp11-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/p11_ec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/p11_pkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
16 changes: 12 additions & 4 deletions src/p11_rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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 */
Expand All @@ -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)
Expand All @@ -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));
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/p11_slot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <string.h>
#include <openssl/buffer.h>
Expand Down
6 changes: 4 additions & 2 deletions src/pkcs11.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/util_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include "util.h"
#include <stdio.h>
#include <string.h>
#include <strings.h>

#if defined(_WIN32) || defined(_WIN64)
#define strncasecmp _strnicmp
Expand Down

0 comments on commit c398dc8

Please sign in to comment.