1
0
mirror of https://github.com/libssh2/libssh2.git synced 2026-10-24 03:48:58 +03:00

src: constify arguments where feasible

Closes #2589
This commit is contained in:
Viktor Szakats
2026-09-04 15:40:12 +02:00
parent da8d048dda
commit 8e181e7a8e
8 changed files with 68 additions and 49 deletions

View File

@@ -274,9 +274,8 @@ Initializes the Diffie-Hellman context at `dhctx`. No effective context
creation needed here.
```c
int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *public,
ssh2_bn *g, ssh2_bn *p, int group_order,
ssh2_bn_ctx *bnctx);
int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *public, const ssh2_bn *g,
const ssh2_bn *p, int group_order, ssh2_bn_ctx *bnctx);
```
Generates a Diffie-Hellman key pair using base `g`, prime `p` and the given
`group_order`. Can use the given big number context `bnctx` if needed.
@@ -286,7 +285,7 @@ the public key is returned in `public`.
```c
int ssh2_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret,
ssh2_bn *f, ssh2_bn *p, ssh2_bn_ctx *bnctx)
const ssh2_bn *f, const ssh2_bn *p, ssh2_bn_ctx *bnctx)
```
Computes the Diffie-Hellman secret from the previously created context
`*dhctx`, the public key `f` from the other party and the same prime `p` used
@@ -723,7 +722,7 @@ Return 0 if OK, else -1.
This procedure is already prototyped in `crypto.h`.
```c
ssh2_curve_type ssh2_ecdsa_get_curve_type(ssh2_ecdsa_ctx *ec_ctx);
ssh2_curve_type ssh2_ecdsa_get_curve_type(const ssh2_ecdsa_ctx *ec_ctx);
```
Returns the curve type associated with given context.
This procedure is already prototyped in `crypto.h`.

View File

@@ -194,7 +194,7 @@ int ssh2_ecdh_gen_k(ssh2_bn **k, LIBSSH2_SESSION *session,
const unsigned char *server_public_key,
size_t server_public_key_len);
ssh2_curve_type ssh2_ecdsa_get_curve_type(ssh2_ecdsa_ctx *ec_ctx);
ssh2_curve_type ssh2_ecdsa_get_curve_type(const ssh2_ecdsa_ctx *ec_ctx);
int ssh2_ecdsa_create_key(ssh2_ec_key **ec_ctx, LIBSSH2_SESSION *session,
unsigned char **out_public_key_octal,
@@ -313,11 +313,11 @@ int ssh2_bn_to_bin(const ssh2_bn *bn, unsigned char *bin);
#endif
void ssh2_dh_init(ssh2_dh_ctx *dhctx);
int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *pub, ssh2_bn *g,
ssh2_bn *p, int group_order, ssh2_bn_ctx *bnctx);
int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *pub, const ssh2_bn *g,
const ssh2_bn *p, int group_order, ssh2_bn_ctx *bnctx);
int ssh2_dh_validate(const ssh2_bn *f, const ssh2_bn *p); /* for unit tests */
int ssh2_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, ssh2_bn *f,
ssh2_bn *p, ssh2_bn_ctx *bnctx);
int ssh2_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret,
const ssh2_bn *f, const ssh2_bn *p, ssh2_bn_ctx *bnctx);
void ssh2_dh_dtor(ssh2_dh_ctx *dhctx);
#if LIBSSH2_RSA

View File

@@ -706,8 +706,8 @@ void ssh2_dh_init(ssh2_dh_ctx *dhctx)
*dhctx = gcry_mpi_new(0); /* Random from client */
}
int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *pub, ssh2_bn *g,
ssh2_bn *p, int group_order, ssh2_bn_ctx *bnctx)
int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *pub, const ssh2_bn *g,
const ssh2_bn *p, int group_order, ssh2_bn_ctx *bnctx)
{
(void)bnctx;
@@ -716,7 +716,7 @@ int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *pub, ssh2_bn *g,
/* Generate x and e */
gcry_mpi_randomize(*dhctx, (group_order * 8) - 1, GCRY_VERY_STRONG_RANDOM);
gcry_mpi_powm(pub, g, *dhctx, p);
gcry_mpi_powm(pub, SSH2_UNCONST(g), *dhctx, SSH2_UNCONST(p));
return 0;
}
@@ -749,8 +749,8 @@ int ssh2_dh_validate(const ssh2_bn *f, const ssh2_bn *p)
return 0;
}
int ssh2_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, ssh2_bn *f,
ssh2_bn *p, ssh2_bn_ctx *bnctx)
int ssh2_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret,
const ssh2_bn *f, const ssh2_bn *p, ssh2_bn_ctx *bnctx)
{
(void)bnctx;
@@ -758,7 +758,7 @@ int ssh2_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, ssh2_bn *f,
return -1;
/* Compute the shared secret */
gcry_mpi_powm(secret, f, *dhctx, p);
gcry_mpi_powm(secret, SSH2_UNCONST(f), *dhctx, SSH2_UNCONST(p));
return 0;
}

View File

@@ -700,7 +700,7 @@ static unsigned char *mbed_gen_publickey_from_rsa(LIBSSH2_SESSION *session,
static int mbed_pub_priv_key(LIBSSH2_SESSION *session, char **method,
unsigned char **pubkeydata,
size_t *pubkeydata_len,
mbedtls_pk_context *pkey)
const mbedtls_pk_context *pkey)
{
char *method_buf = NULL;
unsigned char *key = NULL;
@@ -806,8 +806,8 @@ void ssh2_dh_init(ssh2_dh_ctx *dhctx)
*dhctx = ssh2_bn_init(); /* Random from client */
}
int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *pub, ssh2_bn *g,
ssh2_bn *p, int group_order, ssh2_bn_ctx *bnctx)
int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *pub, const ssh2_bn *g,
const ssh2_bn *p, int group_order, ssh2_bn_ctx *bnctx)
{
(void)bnctx;
@@ -863,8 +863,8 @@ int ssh2_dh_validate(const ssh2_bn *f, const ssh2_bn *p)
return 0;
}
int ssh2_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, ssh2_bn *f,
ssh2_bn *p, ssh2_bn_ctx *bnctx)
int ssh2_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret,
const ssh2_bn *f, const ssh2_bn *p, ssh2_bn_ctx *bnctx)
{
(void)bnctx;
@@ -1084,7 +1084,7 @@ cleanup:
/*
* returns key curve type that maps to ssh2_curve_type
*/
ssh2_curve_type ssh2_ecdsa_get_curve_type(ssh2_ecdsa_ctx *ec_ctx)
ssh2_curve_type ssh2_ecdsa_get_curve_type(const ssh2_ecdsa_ctx *ec_ctx)
{
return (ssh2_curve_type)ec_ctx->MBEDTLS_PRIVATE(grp).id;
}

View File

@@ -658,7 +658,7 @@ int ssh2_dsa_sha1_verify(ssh2_dsa_ctx *dsa, LIBSSH2_SESSION *session,
/*
* returns key curve type that maps to ssh2_curve_type
*/
ssh2_curve_type ssh2_ecdsa_get_curve_type(ssh2_ecdsa_ctx *ec_ctx)
ssh2_curve_type ssh2_ecdsa_get_curve_type(const ssh2_ecdsa_ctx *ec_ctx)
{
#ifdef USE_OPENSSL_3
int bits = 0;
@@ -1032,7 +1032,8 @@ static int ossl_passphrase_cb(char *buf, int size, int rwflag,
#if LIBSSH2_RSA
static unsigned char *ossl_rsa_to_pubkey(LIBSSH2_SESSION *session,
ssh2_rsa_ctx *rsa, size_t *key_len)
const ssh2_rsa_ctx *rsa,
size_t *key_len)
{
static const char method_name[] = "ssh-rsa";
int e_bytes, n_bytes;
@@ -1084,10 +1085,14 @@ fail:
static int ossl_rsa_evp_to_pubkey(LIBSSH2_SESSION *session, char **method,
unsigned char **pubkeydata,
size_t *pubkeydata_len,
EVP_PKEY *pk)
const EVP_PKEY *pk)
{
static const char method_name[] = "ssh-rsa";
ssh2_rsa_ctx *rsa = NULL;
#ifdef USE_OPENSSL_3
const ssh2_rsa_ctx *rsa;
#else
ssh2_rsa_ctx *rsa;
#endif
unsigned char *key;
char *method_buf = NULL;
size_t key_len;
@@ -1097,6 +1102,8 @@ static int ossl_rsa_evp_to_pubkey(LIBSSH2_SESSION *session, char **method,
#ifdef USE_OPENSSL_3
rsa = pk;
#elif defined(USE_OPENSSL111) || defined(LIBSSH2_WOLFSSL)
rsa = EVP_PKEY_get1_RSA(SSH2_UNCONST(pk));
#else
rsa = EVP_PKEY_get1_RSA(pk);
#endif
@@ -1333,7 +1340,8 @@ int ssh2_rsa_new_priv(ssh2_rsa_ctx **rsa,
#if LIBSSH2_DSA
static unsigned char *ossl_dsa_to_pubkey(LIBSSH2_SESSION *session,
ssh2_dsa_ctx *dsa, size_t *key_len)
const ssh2_dsa_ctx *dsa,
size_t *key_len)
{
static const char method_name[] = "ssh-dss";
int p_bytes, q_bytes, g_bytes, k_bytes;
@@ -1396,10 +1404,14 @@ fail:
static int ossl_dsa_evp_to_pubkey(LIBSSH2_SESSION *session, char **method,
unsigned char **pubkeydata,
size_t *pubkeydata_len,
EVP_PKEY *pk)
const EVP_PKEY *pk)
{
static const char method_name[] = "ssh-dss";
ssh2_dsa_ctx *dsa = NULL;
#ifdef USE_OPENSSL_3
const ssh2_dsa_ctx *dsa;
#else
ssh2_dsa_ctx *dsa;
#endif
unsigned char *key;
char *method_buf = NULL;
size_t key_len;
@@ -1409,6 +1421,8 @@ static int ossl_dsa_evp_to_pubkey(LIBSSH2_SESSION *session, char **method,
#ifdef USE_OPENSSL_3
dsa = pk;
#elif defined(USE_OPENSSL111) || defined(LIBSSH2_WOLFSSL)
dsa = EVP_PKEY_get1_DSA(SSH2_UNCONST(pk));
#else
dsa = EVP_PKEY_get1_DSA(pk);
#endif
@@ -1635,7 +1649,7 @@ clean_exit:
static int ossl_ed25519_evp_to_pubkey(LIBSSH2_SESSION *session, char **method,
unsigned char **pubkeydata,
size_t *pubkeydata_len,
EVP_PKEY *pk)
const EVP_PKEY *pk)
{
static const char method_name[] = "ssh-ed25519";
char *method_buf = NULL;
@@ -2424,7 +2438,7 @@ static int ossl_ecdsa_evp_to_pubkey(LIBSSH2_SESSION *session, char **method,
unsigned char **pubkeydata,
size_t *pubkeydata_len,
int is_sk,
EVP_PKEY *pk)
const EVP_PKEY *pk)
{
int rc = 0;
char *method_buf = NULL;
@@ -2454,7 +2468,11 @@ static int ossl_ecdsa_evp_to_pubkey(LIBSSH2_SESSION *session, char **method,
if(!bn_ctx)
return -1;
#if defined(USE_OPENSSL111) || defined(LIBSSH2_WOLFSSL)
ec = EVP_PKEY_get1_EC_KEY(SSH2_UNCONST(pk));
#else
ec = EVP_PKEY_get1_EC_KEY(pk);
#endif
if(!ec) {
rc = -1;
goto clean_exit;
@@ -3635,8 +3653,8 @@ void ssh2_dh_init(ssh2_dh_ctx *dhctx)
*dhctx = BN_new(); /* Random from client */
}
int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *pub, ssh2_bn *g,
ssh2_bn *p, int group_order, ssh2_bn_ctx *bnctx)
int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *pub, const ssh2_bn *g,
const ssh2_bn *p, int group_order, ssh2_bn_ctx *bnctx)
{
if(group_order <= 0)
return -1;
@@ -3679,8 +3697,8 @@ int ssh2_dh_validate(const ssh2_bn *f, const ssh2_bn *p)
return 0;
}
int ssh2_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, ssh2_bn *f,
ssh2_bn *p, ssh2_bn_ctx *bnctx)
int ssh2_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret,
const ssh2_bn *f, const ssh2_bn *p, ssh2_bn_ctx *bnctx)
{
if(ssh2_dh_validate(f, p)) /* Verify if parameters are valid */
return -1;

View File

@@ -128,6 +128,7 @@
# define SSH2_CRYPTO_ENGINE_NAME "BoringSSL"
# elif !defined(USE_OPENSSL_3)
# define SSH2_CRYPTO_ENGINE_NAME "OpenSSL/1.1.1"
# define USE_OPENSSL111
# else
# define SSH2_CRYPTO_ENGINE_NAME "OpenSSL"
# endif

View File

@@ -670,7 +670,7 @@ static void asn1delete(struct asn1Element *e)
}
}
static struct asn1Element *asn1uint(ssh2_bn *bn)
static struct asn1Element *asn1uint(const ssh2_bn *bn)
{
struct asn1Element *e;
int bits;
@@ -1273,8 +1273,8 @@ void ssh2_dh_init(ssh2_dh_ctx *dhctx)
memset((char *)dhctx, 0, sizeof(*dhctx));
}
int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *pub, ssh2_bn *g,
ssh2_bn *p, int group_order, ssh2_bn_ctx *bnctx)
int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *pub, const ssh2_bn *g,
const ssh2_bn *p, int group_order, ssh2_bn_ctx *bnctx)
{
struct asn1Element *prime;
struct asn1Element *base;
@@ -1344,8 +1344,8 @@ int ssh2_dh_validate(const ssh2_bn *f, const ssh2_bn *p)
return result;
}
int ssh2_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, ssh2_bn *f,
ssh2_bn *p, ssh2_bn_ctx *bnctx)
int ssh2_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret,
const ssh2_bn *f, const ssh2_bn *p, ssh2_bn_ctx *bnctx)
{
char *pubkey;
int pubkeysize;

View File

@@ -204,7 +204,8 @@ static int wcng_bn_random(ssh2_bn *rnd, int bits, int top, int bottom)
return 0;
}
static int wcng_bn_mod_exp(ssh2_bn *r, ssh2_bn *a, ssh2_bn *p, ssh2_bn *m)
static int wcng_bn_mod_exp(ssh2_bn *r, const ssh2_bn *a, const ssh2_bn *p,
const ssh2_bn *m)
{
BCRYPT_KEY_HANDLE hKey;
BCRYPT_RSAKEY_BLOB *rsakey;
@@ -1730,7 +1731,7 @@ static int wcng_p1363signature_from_point(IN LIBSSH2_SESSION *session,
*/
static int wcng_publickey_from_point(IN LIBSSH2_SESSION *session,
IN wcng_ecc_keytype keytype,
IN struct ecdsa_point *point,
IN const struct ecdsa_point *point,
OUT BCRYPT_KEY_HANDLE *key)
{
int result = LIBSSH2_ERROR_NONE;
@@ -1790,8 +1791,8 @@ cleanup:
*/
static int wcng_privatekey_from_point(IN LIBSSH2_SESSION *session,
IN wcng_ecc_keytype keytype,
IN struct ecdsa_point *q,
IN unsigned char *d,
IN const struct ecdsa_point *q,
IN const unsigned char *d,
IN size_t d_len,
OUT BCRYPT_KEY_HANDLE *key)
{
@@ -2577,7 +2578,7 @@ cleanup:
/*
* returns key curve type that maps to ssh2_curve_type
*/
ssh2_curve_type ssh2_ecdsa_get_curve_type(IN ssh2_ecdsa_ctx *ec_ctx)
ssh2_curve_type ssh2_ecdsa_get_curve_type(const ssh2_ecdsa_ctx *ec_ctx)
{
return ec_ctx->curve;
}
@@ -2959,8 +2960,8 @@ static size_t wcng_round_down(size_t number, size_t multiple)
* `group_order'. Can use the given big number context `bnctx' if needed. The
* private key is stored as opaque in the Diffie-Hellman context `*dhctx' and
* the public key is returned in `pub'. 0 is returned upon success, else -1. */
int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *pub, ssh2_bn *g,
ssh2_bn *p, int group_order, ssh2_bn_ctx *bnctx)
int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *pub, const ssh2_bn *g,
const ssh2_bn *p, int group_order, ssh2_bn_ctx *bnctx)
{
const int hasAlgDHwithKDF = ssh2_wcng.hasAlgDHwithKDF;
@@ -3136,8 +3137,8 @@ int ssh2_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *pub, ssh2_bn *g,
* `*dhctx', the public key `f' from the other party and the same prime `p'
* used at context creation. The result is stored in `secret'. 0 is returned
* upon success, else -1. */
int ssh2_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, ssh2_bn *f,
ssh2_bn *p, ssh2_bn_ctx *bnctx)
int ssh2_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret,
const ssh2_bn *f, const ssh2_bn *p, ssh2_bn_ctx *bnctx)
{
(void)bnctx;